Debian Codegraph @

llvm-project/clang/unittests/Format/ConfigParseTest.cpp

raw
1 //===- unittest/Format/ConfigParseTest.cpp - Config parsing unit tests ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Format/Format.h"
10
11 #include "llvm/Support/VirtualFileSystem.h"
12 #include "gtest/gtest.h"
13
14 namespace clang {
15 namespace format {
16 namespace {
17
18 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
19
20 #define EXPECT_ALL_STYLES_EQUAL(Styles) \
21 for (size_t i = 1; i < Styles.size(); ++i) \
22 EXPECT_EQ(Styles[0], Styles[i]) \
23 << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
24
25 TEST(ConfigParseTest, GetsPredefinedStyleByName) {
26 SmallVector<FormatStyle, 3> Styles;
27 Styles.resize(3);
28
29 Styles[0] = getLLVMStyle();
30 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
31 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
32 EXPECT_ALL_STYLES_EQUAL(Styles);
33
34 Styles[0] = getGoogleStyle();
35 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
36 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
37 EXPECT_ALL_STYLES_EQUAL(Styles);
38
39 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
40 EXPECT_TRUE(
41 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
42 EXPECT_TRUE(
43 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
44 EXPECT_ALL_STYLES_EQUAL(Styles);
45
46 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
47 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
48 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
49 EXPECT_ALL_STYLES_EQUAL(Styles);
50
51 Styles[0] = getMozillaStyle();
52 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
53 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
54 EXPECT_ALL_STYLES_EQUAL(Styles);
55
56 Styles[0] = getWebKitStyle();
57 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
58 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
59 EXPECT_ALL_STYLES_EQUAL(Styles);
60
61 Styles[0] = getGNUStyle();
62 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
63 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
64 EXPECT_ALL_STYLES_EQUAL(Styles);
65
66 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
67 }
68
69 TEST(ConfigParseTest, GetsCorrectBasedOnStyle) {
70 SmallVector<FormatStyle, 8> Styles;
71 Styles.resize(2);
72
73 Styles[0] = getGoogleStyle();
74 Styles[1] = getLLVMStyle();
75 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
76 EXPECT_ALL_STYLES_EQUAL(Styles);
77
78 Styles.resize(5);
79 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
80 Styles[1] = getLLVMStyle();
81 Styles[1].Language = FormatStyle::LK_JavaScript;
82 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
83
84 Styles[2] = getLLVMStyle();
85 Styles[2].Language = FormatStyle::LK_JavaScript;
86 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
87 "BasedOnStyle: Google",
88 &Styles[2])
89 .value());
90
91 Styles[3] = getLLVMStyle();
92 Styles[3].Language = FormatStyle::LK_JavaScript;
93 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
94 "Language: JavaScript",
95 &Styles[3])
96 .value());
97
98 Styles[4] = getLLVMStyle();
99 Styles[4].Language = FormatStyle::LK_JavaScript;
100 EXPECT_EQ(0, parseConfiguration("---\n"
101 "BasedOnStyle: LLVM\n"
102 "IndentWidth: 123\n"
103 "---\n"
104 "BasedOnStyle: Google\n"
105 "Language: JavaScript",
106 &Styles[4])
107 .value());
108 EXPECT_ALL_STYLES_EQUAL(Styles);
109 }
110
111 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
112 Style.FIELD = false; \
113 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
114 EXPECT_TRUE(Style.FIELD); \
115 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
116 EXPECT_FALSE(Style.FIELD)
117
118 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
119
120 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
121 Style.STRUCT.FIELD = false; \
122 EXPECT_EQ(0, \
123 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
124 .value()); \
125 EXPECT_TRUE(Style.STRUCT.FIELD); \
126 EXPECT_EQ(0, \
127 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
128 .value()); \
129 EXPECT_FALSE(Style.STRUCT.FIELD)
130
131 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
132 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
133
134 #define CHECK_PARSE(TEXT, FIELD, VALUE) \
135 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \
136 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
137 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
138
139 #define CHECK_PARSE_NESTED_VALUE(TEXT, STRUCT, FIELD, VALUE) \
140 EXPECT_NE(VALUE, Style.STRUCT.FIELD) << "Initial value already the same!"; \
141 EXPECT_EQ(0, parseConfiguration(#STRUCT ":\n " TEXT, &Style).value()); \
142 EXPECT_EQ(VALUE, Style.STRUCT.FIELD) << "Unexpected value after parsing!"
143
144 TEST(ConfigParseTest, ParsesConfigurationBools) {
145 FormatStyle Style = {};
146 Style.Language = FormatStyle::LK_Cpp;
147 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
148 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
149 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
150 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
151 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
152 CHECK_PARSE_BOOL(BinPackArguments);
153 CHECK_PARSE_BOOL(BinPackParameters);
154 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
155 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
156 CHECK_PARSE_BOOL(BreakStringLiterals);
157 CHECK_PARSE_BOOL(CompactNamespaces);
158 CHECK_PARSE_BOOL(DerivePointerAlignment);
159 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
160 CHECK_PARSE_BOOL(DisableFormat);
161 CHECK_PARSE_BOOL(IndentAccessModifiers);
162 CHECK_PARSE_BOOL(IndentCaseLabels);
163 CHECK_PARSE_BOOL(IndentCaseBlocks);
164 CHECK_PARSE_BOOL(IndentGotoLabels);
165 CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires");
166 CHECK_PARSE_BOOL(IndentRequiresClause);
167 CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
168 CHECK_PARSE_BOOL(InsertBraces);
169 CHECK_PARSE_BOOL(InsertNewlineAtEOF);
170 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
171 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
172 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
173 CHECK_PARSE_BOOL(Cpp11BracedListStyle);
174 CHECK_PARSE_BOOL(ReflowComments);
175 CHECK_PARSE_BOOL(RemoveBracesLLVM);
176 CHECK_PARSE_BOOL(RemoveSemicolon);
177 CHECK_PARSE_BOOL(SpacesInParentheses);
178 CHECK_PARSE_BOOL(SpacesInSquareBrackets);
179 CHECK_PARSE_BOOL(SpacesInConditionalStatement);
180 CHECK_PARSE_BOOL(SpaceInEmptyBlock);
181 CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
182 CHECK_PARSE_BOOL(SpacesInContainerLiterals);
183 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
184 CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
185 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
186 CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
187 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
188 CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
189 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
190 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
191 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
192 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
193 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
194
195 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
196 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
197 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
198 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
199 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
200 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
201 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
202 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
203 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
204 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
205 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
206 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
207 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
208 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
209 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
210 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
211 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
212 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
213 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
214 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
215 AfterFunctionDeclarationName);
216 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
217 AfterFunctionDefinitionName);
218 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
219 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
220 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
221 }
222
223 #undef CHECK_PARSE_BOOL
224
225 TEST(ConfigParseTest, ParsesConfiguration) {
226 FormatStyle Style = {};
227 Style.Language = FormatStyle::LK_Cpp;
228 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
229 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
230 ConstructorInitializerIndentWidth, 1234u);
231 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
232 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
233 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
234 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
235 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
236 PenaltyBreakBeforeFirstCallParameter, 1234u);
237 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
238 PenaltyBreakTemplateDeclaration, 1234u);
239 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
240 1234u);
241 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
242 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
243 PenaltyReturnTypeOnItsOwnLine, 1234u);
244 CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
245 SpacesBeforeTrailingComments, 1234u);
246 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
247 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
248 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
249
250 Style.QualifierAlignment = FormatStyle::QAS_Right;
251 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
252 FormatStyle::QAS_Leave);
253 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
254 FormatStyle::QAS_Right);
255 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
256 FormatStyle::QAS_Left);
257 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
258 FormatStyle::QAS_Custom);
259
260 Style.QualifierOrder.clear();
261 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
262 std::vector<std::string>({"const", "volatile", "type"}));
263 Style.QualifierOrder.clear();
264 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
265 std::vector<std::string>({"const", "type"}));
266 Style.QualifierOrder.clear();
267 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
268 std::vector<std::string>({"volatile", "type"}));
269
270 #define CHECK_ALIGN_CONSECUTIVE(FIELD) \
271 do { \
272 Style.FIELD.Enabled = true; \
273 CHECK_PARSE(#FIELD ": None", FIELD, \
274 FormatStyle::AlignConsecutiveStyle( \
275 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
276 /*AcrossComments=*/false, /*AlignCompound=*/false, \
277 /*PadOperators=*/true})); \
278 CHECK_PARSE(#FIELD ": Consecutive", FIELD, \
279 FormatStyle::AlignConsecutiveStyle( \
280 {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
281 /*AcrossComments=*/false, /*AlignCompound=*/false, \
282 /*PadOperators=*/true})); \
283 CHECK_PARSE(#FIELD ": AcrossEmptyLines", FIELD, \
284 FormatStyle::AlignConsecutiveStyle( \
285 {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
286 /*AcrossComments=*/false, /*AlignCompound=*/false, \
287 /*PadOperators=*/true})); \
288 CHECK_PARSE(#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
289 FormatStyle::AlignConsecutiveStyle( \
290 {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
291 /*AcrossComments=*/true, /*AlignCompound=*/false, \
292 /*PadOperators=*/true})); \
293 /* For backwards compability, false / true should still parse */ \
294 CHECK_PARSE(#FIELD ": false", FIELD, \
295 FormatStyle::AlignConsecutiveStyle( \
296 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
297 /*AcrossComments=*/false, /*AlignCompound=*/false, \
298 /*PadOperators=*/true})); \
299 CHECK_PARSE(#FIELD ": true", FIELD, \
300 FormatStyle::AlignConsecutiveStyle( \
301 {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
302 /*AcrossComments=*/false, /*AlignCompound=*/false, \
303 /*PadOperators=*/true})); \
304 \
305 CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \
306 CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \
307 CHECK_PARSE_NESTED_BOOL(FIELD, AcrossComments); \
308 CHECK_PARSE_NESTED_BOOL(FIELD, AlignCompound); \
309 CHECK_PARSE_NESTED_BOOL(FIELD, PadOperators); \
310 } while (false)
311
312 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveAssignments);
313 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveBitFields);
314 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveMacros);
315 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveDeclarations);
316
317 #undef CHECK_ALIGN_CONSECUTIVE
318
319 Style.PointerAlignment = FormatStyle::PAS_Middle;
320 CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
321 FormatStyle::PAS_Left);
322 CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
323 FormatStyle::PAS_Right);
324 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
325 FormatStyle::PAS_Middle);
326 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
327 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
328 FormatStyle::RAS_Pointer);
329 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
330 FormatStyle::RAS_Left);
331 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
332 FormatStyle::RAS_Right);
333 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
334 FormatStyle::RAS_Middle);
335 // For backward compatibility:
336 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
337 FormatStyle::PAS_Left);
338 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
339 FormatStyle::PAS_Right);
340 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
341 FormatStyle::PAS_Middle);
342
343 Style.Standard = FormatStyle::LS_Auto;
344 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
345 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
346 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
347 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
348 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
349 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
350 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
351 // Legacy aliases:
352 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
353 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
354 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
355 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
356
357 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
358 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
359 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
360 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
361 FormatStyle::BOS_None);
362 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
363 FormatStyle::BOS_All);
364 // For backward compatibility:
365 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
366 FormatStyle::BOS_None);
367 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
368 FormatStyle::BOS_All);
369
370 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
371 CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
372 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
373 CHECK_PARSE("BreakConstructorInitializers: AfterColon",
374 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
375 CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
376 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
377 // For backward compatibility:
378 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
379 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
380
381 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
382 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
383 FormatStyle::BILS_AfterComma);
384 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
385 FormatStyle::BILS_BeforeComma);
386 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
387 FormatStyle::BILS_AfterColon);
388 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
389 FormatStyle::BILS_BeforeColon);
390 // For backward compatibility:
391 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
392 FormatStyle::BILS_BeforeComma);
393
394 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
395 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
396 FormatStyle::PCIS_Never);
397 CHECK_PARSE("PackConstructorInitializers: BinPack",
398 PackConstructorInitializers, FormatStyle::PCIS_BinPack);
399 CHECK_PARSE("PackConstructorInitializers: CurrentLine",
400 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
401 CHECK_PARSE("PackConstructorInitializers: NextLine",
402 PackConstructorInitializers, FormatStyle::PCIS_NextLine);
403 // For backward compatibility:
404 CHECK_PARSE("BasedOnStyle: Google\n"
405 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
406 "AllowAllConstructorInitializersOnNextLine: false",
407 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
408 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
409 CHECK_PARSE("BasedOnStyle: Google\n"
410 "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
411 PackConstructorInitializers, FormatStyle::PCIS_BinPack);
412 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
413 "AllowAllConstructorInitializersOnNextLine: true",
414 PackConstructorInitializers, FormatStyle::PCIS_NextLine);
415 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
416 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
417 "AllowAllConstructorInitializersOnNextLine: false",
418 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
419
420 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
421 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
422 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
423 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
424 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
425 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
426 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
427 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
428 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
429
430 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
431 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
432 FormatStyle::BAS_Align);
433 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
434 FormatStyle::BAS_DontAlign);
435 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
436 FormatStyle::BAS_AlwaysBreak);
437 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
438 FormatStyle::BAS_BlockIndent);
439 // For backward compatibility:
440 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
441 FormatStyle::BAS_DontAlign);
442 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
443 FormatStyle::BAS_Align);
444
445 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
446 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
447 FormatStyle::ENAS_DontAlign);
448 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
449 FormatStyle::ENAS_Left);
450 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
451 FormatStyle::ENAS_Right);
452 // For backward compatibility:
453 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
454 FormatStyle::ENAS_Left);
455 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
456 FormatStyle::ENAS_Right);
457
458 Style.AlignOperands = FormatStyle::OAS_Align;
459 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
460 FormatStyle::OAS_DontAlign);
461 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
462 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
463 FormatStyle::OAS_AlignAfterOperator);
464 // For backward compatibility:
465 CHECK_PARSE("AlignOperands: false", AlignOperands,
466 FormatStyle::OAS_DontAlign);
467 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
468
469 CHECK_PARSE("AlignTrailingComments: Leave", AlignTrailingComments,
470 FormatStyle::TrailingCommentsAlignmentStyle(
471 {FormatStyle::TCAS_Leave, 0}));
472 CHECK_PARSE("AlignTrailingComments: Always", AlignTrailingComments,
473 FormatStyle::TrailingCommentsAlignmentStyle(
474 {FormatStyle::TCAS_Always, 0}));
475 CHECK_PARSE("AlignTrailingComments: Never", AlignTrailingComments,
476 FormatStyle::TrailingCommentsAlignmentStyle(
477 {FormatStyle::TCAS_Never, 0}));
478 // For backwards compatibility
479 CHECK_PARSE("AlignTrailingComments: true", AlignTrailingComments,
480 FormatStyle::TrailingCommentsAlignmentStyle(
481 {FormatStyle::TCAS_Always, 0}));
482 CHECK_PARSE("AlignTrailingComments: false", AlignTrailingComments,
483 FormatStyle::TrailingCommentsAlignmentStyle(
484 {FormatStyle::TCAS_Never, 0}));
485 CHECK_PARSE_NESTED_VALUE("Kind: Always", AlignTrailingComments, Kind,
486 FormatStyle::TCAS_Always);
487 CHECK_PARSE_NESTED_VALUE("Kind: Never", AlignTrailingComments, Kind,
488 FormatStyle::TCAS_Never);
489 CHECK_PARSE_NESTED_VALUE("Kind: Leave", AlignTrailingComments, Kind,
490 FormatStyle::TCAS_Leave);
491 CHECK_PARSE_NESTED_VALUE("OverEmptyLines: 1234", AlignTrailingComments,
492 OverEmptyLines, 1234u);
493
494 Style.UseTab = FormatStyle::UT_ForIndentation;
495 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
496 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
497 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
498 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
499 FormatStyle::UT_ForContinuationAndIndentation);
500 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
501 FormatStyle::UT_AlignWithSpaces);
502 // For backward compatibility:
503 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
504 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
505
506 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
507 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
508 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
509 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
510 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
511 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
512 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
513 // For backward compatibility:
514 CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
515 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
516 CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
517 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
518
519 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
520 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
521 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
522 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
523 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
524 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
525 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
526 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
527 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
528 // For backward compatibility:
529 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
530 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
531 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
532 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
533
534 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
535 CHECK_PARSE("AllowShortLambdasOnASingleLine: None",
536 AllowShortLambdasOnASingleLine, FormatStyle::SLS_None);
537 CHECK_PARSE("AllowShortLambdasOnASingleLine: Empty",
538 AllowShortLambdasOnASingleLine, FormatStyle::SLS_Empty);
539 CHECK_PARSE("AllowShortLambdasOnASingleLine: Inline",
540 AllowShortLambdasOnASingleLine, FormatStyle::SLS_Inline);
541 CHECK_PARSE("AllowShortLambdasOnASingleLine: All",
542 AllowShortLambdasOnASingleLine, FormatStyle::SLS_All);
543 // For backward compatibility:
544 CHECK_PARSE("AllowShortLambdasOnASingleLine: false",
545 AllowShortLambdasOnASingleLine, FormatStyle::SLS_None);
546 CHECK_PARSE("AllowShortLambdasOnASingleLine: true",
547 AllowShortLambdasOnASingleLine, FormatStyle::SLS_All);
548
549 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
550 CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
551 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
552 CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
553 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
554 CHECK_PARSE("SpaceAroundPointerQualifiers: After",
555 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
556 CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
557 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
558
559 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
560 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
561 FormatStyle::SBPO_Never);
562 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
563 FormatStyle::SBPO_Always);
564 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
565 FormatStyle::SBPO_ControlStatements);
566 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
567 SpaceBeforeParens,
568 FormatStyle::SBPO_ControlStatementsExceptControlMacros);
569 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
570 FormatStyle::SBPO_NonEmptyParentheses);
571 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
572 FormatStyle::SBPO_Custom);
573 // For backward compatibility:
574 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
575 FormatStyle::SBPO_Never);
576 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
577 FormatStyle::SBPO_ControlStatements);
578 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
579 SpaceBeforeParens,
580 FormatStyle::SBPO_ControlStatementsExceptControlMacros);
581
582 Style.ColumnLimit = 123;
583 FormatStyle BaseStyle = getLLVMStyle();
584 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
585 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
586
587 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
588 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
589 FormatStyle::BS_Attach);
590 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
591 FormatStyle::BS_Linux);
592 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
593 FormatStyle::BS_Mozilla);
594 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
595 FormatStyle::BS_Stroustrup);
596 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
597 FormatStyle::BS_Allman);
598 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
599 FormatStyle::BS_Whitesmiths);
600 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
601 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
602 FormatStyle::BS_WebKit);
603 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
604 FormatStyle::BS_Custom);
605
606 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
607 CHECK_PARSE("BraceWrapping:\n"
608 " AfterControlStatement: MultiLine",
609 BraceWrapping.AfterControlStatement,
610 FormatStyle::BWACS_MultiLine);
611 CHECK_PARSE("BraceWrapping:\n"
612 " AfterControlStatement: Always",
613 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
614 CHECK_PARSE("BraceWrapping:\n"
615 " AfterControlStatement: Never",
616 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
617 // For backward compatibility:
618 CHECK_PARSE("BraceWrapping:\n"
619 " AfterControlStatement: true",
620 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
621 CHECK_PARSE("BraceWrapping:\n"
622 " AfterControlStatement: false",
623 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
624
625 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
626 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
627 FormatStyle::RTBS_None);
628 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
629 FormatStyle::RTBS_All);
630 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
631 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
632 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
633 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
634 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
635 AlwaysBreakAfterReturnType,
636 FormatStyle::RTBS_TopLevelDefinitions);
637
638 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
639 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
640 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
641 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
642 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
643 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
644 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
645 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
646 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
647 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
648 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
649
650 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
651 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
652 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
653 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
654 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
655 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
656 AlwaysBreakAfterDefinitionReturnType,
657 FormatStyle::DRTBS_TopLevel);
658
659 Style.NamespaceIndentation = FormatStyle::NI_All;
660 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
661 FormatStyle::NI_None);
662 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
663 FormatStyle::NI_Inner);
664 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
665 FormatStyle::NI_All);
666
667 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
668 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
669 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
670 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
671 AllowShortIfStatementsOnASingleLine,
672 FormatStyle::SIS_WithoutElse);
673 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
674 AllowShortIfStatementsOnASingleLine,
675 FormatStyle::SIS_OnlyFirstIf);
676 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
677 AllowShortIfStatementsOnASingleLine,
678 FormatStyle::SIS_AllIfsAndElse);
679 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
680 AllowShortIfStatementsOnASingleLine,
681 FormatStyle::SIS_OnlyFirstIf);
682 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
683 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
684 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
685 AllowShortIfStatementsOnASingleLine,
686 FormatStyle::SIS_WithoutElse);
687
688 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
689 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
690 FormatStyle::IEBS_AfterExternBlock);
691 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
692 FormatStyle::IEBS_Indent);
693 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
694 FormatStyle::IEBS_NoIndent);
695 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
696 FormatStyle::IEBS_Indent);
697 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
698 FormatStyle::IEBS_NoIndent);
699
700 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
701 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
702 FormatStyle::BFCS_Both);
703 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
704 FormatStyle::BFCS_None);
705 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
706 FormatStyle::BFCS_Before);
707 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
708 FormatStyle::BFCS_After);
709
710 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
711 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
712 FormatStyle::SJSIO_After);
713 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
714 FormatStyle::SJSIO_Before);
715
716 Style.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
717 CHECK_PARSE("SortUsingDeclarations: Never", SortUsingDeclarations,
718 FormatStyle::SUD_Never);
719 CHECK_PARSE("SortUsingDeclarations: Lexicographic", SortUsingDeclarations,
720 FormatStyle::SUD_Lexicographic);
721 CHECK_PARSE("SortUsingDeclarations: LexicographicNumeric",
722 SortUsingDeclarations, FormatStyle::SUD_LexicographicNumeric);
723 // For backward compatibility:
724 CHECK_PARSE("SortUsingDeclarations: false", SortUsingDeclarations,
725 FormatStyle::SUD_Never);
726 CHECK_PARSE("SortUsingDeclarations: true", SortUsingDeclarations,
727 FormatStyle::SUD_LexicographicNumeric);
728
729 // FIXME: This is required because parsing a configuration simply overwrites
730 // the first N elements of the list instead of resetting it.
731 Style.ForEachMacros.clear();
732 std::vector<std::string> BoostForeach;
733 BoostForeach.push_back("BOOST_FOREACH");
734 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
735 std::vector<std::string> BoostAndQForeach;
736 BoostAndQForeach.push_back("BOOST_FOREACH");
737 BoostAndQForeach.push_back("Q_FOREACH");
738 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
739 BoostAndQForeach);
740
741 Style.IfMacros.clear();
742 std::vector<std::string> CustomIfs;
743 CustomIfs.push_back("MYIF");
744 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
745
746 Style.AttributeMacros.clear();
747 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
748 std::vector<std::string>{"__capability"});
749 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
750 std::vector<std::string>({"attr1", "attr2"}));
751
752 Style.StatementAttributeLikeMacros.clear();
753 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
754 StatementAttributeLikeMacros,
755 std::vector<std::string>({"emit", "Q_EMIT"}));
756
757 Style.StatementMacros.clear();
758 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
759 std::vector<std::string>{"QUNUSED"});
760 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
761 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
762
763 Style.NamespaceMacros.clear();
764 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
765 std::vector<std::string>{"TESTSUITE"});
766 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
767 std::vector<std::string>({"TESTSUITE", "SUITE"}));
768
769 Style.WhitespaceSensitiveMacros.clear();
770 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
771 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
772 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
773 WhitespaceSensitiveMacros,
774 std::vector<std::string>({"STRINGIZE", "ASSERT"}));
775 Style.WhitespaceSensitiveMacros.clear();
776 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
777 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
778 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
779 WhitespaceSensitiveMacros,
780 std::vector<std::string>({"STRINGIZE", "ASSERT"}));
781
782 Style.IncludeStyle.IncludeCategories.clear();
783 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
784 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
785 CHECK_PARSE("IncludeCategories:\n"
786 " - Regex: abc/.*\n"
787 " Priority: 2\n"
788 " - Regex: .*\n"
789 " Priority: 1\n"
790 " CaseSensitive: true\n",
791 IncludeStyle.IncludeCategories, ExpectedCategories);
792 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
793 "abc$");
794 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
795 IncludeStyle.IncludeIsMainSourceRegex, "abc$");
796
797 Style.SortIncludes = FormatStyle::SI_Never;
798 CHECK_PARSE("SortIncludes: true", SortIncludes,
799 FormatStyle::SI_CaseSensitive);
800 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
801 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
802 FormatStyle::SI_CaseInsensitive);
803 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
804 FormatStyle::SI_CaseSensitive);
805 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
806
807 Style.RawStringFormats.clear();
808 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
809 {
810 FormatStyle::LK_TextProto,
811 {"pb", "proto"},
812 {"PARSE_TEXT_PROTO"},
813 /*CanonicalDelimiter=*/"",
814 "llvm",
815 },
816 {
817 FormatStyle::LK_Cpp,
818 {"cc", "cpp"},
819 {"C_CODEBLOCK", "CPPEVAL"},
820 /*CanonicalDelimiter=*/"cc",
821 /*BasedOnStyle=*/"",
822 },
823 };
824
825 CHECK_PARSE("RawStringFormats:\n"
826 " - Language: TextProto\n"
827 " Delimiters:\n"
828 " - 'pb'\n"
829 " - 'proto'\n"
830 " EnclosingFunctions:\n"
831 " - 'PARSE_TEXT_PROTO'\n"
832 " BasedOnStyle: llvm\n"
833 " - Language: Cpp\n"
834 " Delimiters:\n"
835 " - 'cc'\n"
836 " - 'cpp'\n"
837 " EnclosingFunctions:\n"
838 " - 'C_CODEBLOCK'\n"
839 " - 'CPPEVAL'\n"
840 " CanonicalDelimiter: 'cc'",
841 RawStringFormats, ExpectedRawStringFormats);
842
843 CHECK_PARSE("SpacesInLineCommentPrefix:\n"
844 " Minimum: 0\n"
845 " Maximum: 0",
846 SpacesInLineCommentPrefix.Minimum, 0u);
847 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
848 Style.SpacesInLineCommentPrefix.Minimum = 1;
849 CHECK_PARSE("SpacesInLineCommentPrefix:\n"
850 " Minimum: 2",
851 SpacesInLineCommentPrefix.Minimum, 0u);
852 CHECK_PARSE("SpacesInLineCommentPrefix:\n"
853 " Maximum: -1",
854 SpacesInLineCommentPrefix.Maximum, -1u);
855 CHECK_PARSE("SpacesInLineCommentPrefix:\n"
856 " Minimum: 2",
857 SpacesInLineCommentPrefix.Minimum, 2u);
858 CHECK_PARSE("SpacesInLineCommentPrefix:\n"
859 " Maximum: 1",
860 SpacesInLineCommentPrefix.Maximum, 1u);
861 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
862
863 Style.SpacesInAngles = FormatStyle::SIAS_Always;
864 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
865 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
866 FormatStyle::SIAS_Always);
867 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
868 // For backward compatibility:
869 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
870 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
871
872 CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition,
873 FormatStyle::RCPS_WithPreceding);
874 CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition,
875 FormatStyle::RCPS_WithFollowing);
876 CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition,
877 FormatStyle::RCPS_SingleLine);
878 CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
879 FormatStyle::RCPS_OwnLine);
880
881 CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
882 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
883 CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
884 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
885 CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed",
886 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
887 // For backward compatibility:
888 CHECK_PARSE("BreakBeforeConceptDeclarations: true",
889 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
890 CHECK_PARSE("BreakBeforeConceptDeclarations: false",
891 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
892
893 CHECK_PARSE("BreakAfterAttributes: Always", BreakAfterAttributes,
894 FormatStyle::ABS_Always);
895 CHECK_PARSE("BreakAfterAttributes: Leave", BreakAfterAttributes,
896 FormatStyle::ABS_Leave);
897 CHECK_PARSE("BreakAfterAttributes: Never", BreakAfterAttributes,
898 FormatStyle::ABS_Never);
899
900 const auto DefaultLineEnding = FormatStyle::LE_DeriveLF;
901 CHECK_PARSE("LineEnding: LF", LineEnding, FormatStyle::LE_LF);
902 CHECK_PARSE("LineEnding: CRLF", LineEnding, FormatStyle::LE_CRLF);
903 CHECK_PARSE("LineEnding: DeriveCRLF", LineEnding, FormatStyle::LE_DeriveCRLF);
904 CHECK_PARSE("LineEnding: DeriveLF", LineEnding, DefaultLineEnding);
905 // For backward compatibility:
906 CHECK_PARSE("DeriveLineEnding: false", LineEnding, FormatStyle::LE_LF);
907 Style.LineEnding = DefaultLineEnding;
908 CHECK_PARSE("DeriveLineEnding: false\n"
909 "UseCRLF: true",
910 LineEnding, FormatStyle::LE_CRLF);
911 Style.LineEnding = DefaultLineEnding;
912 CHECK_PARSE("UseCRLF: true", LineEnding, FormatStyle::LE_DeriveCRLF);
913 }
914
915 TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
916 FormatStyle Style = {};
917 Style.Language = FormatStyle::LK_Cpp;
918 CHECK_PARSE("Language: Cpp\n"
919 "IndentWidth: 12",
920 IndentWidth, 12u);
921 EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
922 "IndentWidth: 34",
923 &Style),
924 ParseError::Unsuitable);
925 FormatStyle BinPackedTCS = {};
926 BinPackedTCS.Language = FormatStyle::LK_JavaScript;
927 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
928 "InsertTrailingCommas: Wrapped",
929 &BinPackedTCS),
930 ParseError::BinPackTrailingCommaConflict);
931 EXPECT_EQ(12u, Style.IndentWidth);
932 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
933 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
934
935 Style.Language = FormatStyle::LK_JavaScript;
936 CHECK_PARSE("Language: JavaScript\n"
937 "IndentWidth: 12",
938 IndentWidth, 12u);
939 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
940 EXPECT_EQ(parseConfiguration("Language: Cpp\n"
941 "IndentWidth: 34",
942 &Style),
943 ParseError::Unsuitable);
944 EXPECT_EQ(23u, Style.IndentWidth);
945 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
946 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
947
948 CHECK_PARSE("BasedOnStyle: LLVM\n"
949 "IndentWidth: 67",
950 IndentWidth, 67u);
951
952 CHECK_PARSE("---\n"
953 "Language: JavaScript\n"
954 "IndentWidth: 12\n"
955 "---\n"
956 "Language: Cpp\n"
957 "IndentWidth: 34\n"
958 "...\n",
959 IndentWidth, 12u);
960
961 Style.Language = FormatStyle::LK_Cpp;
962 CHECK_PARSE("---\n"
963 "Language: JavaScript\n"
964 "IndentWidth: 12\n"
965 "---\n"
966 "Language: Cpp\n"
967 "IndentWidth: 34\n"
968 "...\n",
969 IndentWidth, 34u);
970 CHECK_PARSE("---\n"
971 "IndentWidth: 78\n"
972 "---\n"
973 "Language: JavaScript\n"
974 "IndentWidth: 56\n"
975 "...\n",
976 IndentWidth, 78u);
977
978 Style.ColumnLimit = 123;
979 Style.IndentWidth = 234;
980 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
981 Style.TabWidth = 345;
982 EXPECT_FALSE(parseConfiguration("---\n"
983 "IndentWidth: 456\n"
984 "BreakBeforeBraces: Allman\n"
985 "---\n"
986 "Language: JavaScript\n"
987 "IndentWidth: 111\n"
988 "TabWidth: 111\n"
989 "---\n"
990 "Language: Cpp\n"
991 "BreakBeforeBraces: Stroustrup\n"
992 "TabWidth: 789\n"
993 "...\n",
994 &Style));
995 EXPECT_EQ(123u, Style.ColumnLimit);
996 EXPECT_EQ(456u, Style.IndentWidth);
997 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
998 EXPECT_EQ(789u, Style.TabWidth);
999
1000 EXPECT_EQ(parseConfiguration("---\n"
1001 "Language: JavaScript\n"
1002 "IndentWidth: 56\n"
1003 "---\n"
1004 "IndentWidth: 78\n"
1005 "...\n",
1006 &Style),
1007 ParseError::Error);
1008 EXPECT_EQ(parseConfiguration("---\n"
1009 "Language: JavaScript\n"
1010 "IndentWidth: 56\n"
1011 "---\n"
1012 "Language: JavaScript\n"
1013 "IndentWidth: 78\n"
1014 "...\n",
1015 &Style),
1016 ParseError::Error);
1017
1018 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
1019 }
1020
1021 TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
1022 FormatStyle Style = {};
1023 Style.Language = FormatStyle::LK_JavaScript;
1024 Style.BreakBeforeTernaryOperators = true;
1025 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
1026 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
1027
1028 Style.BreakBeforeTernaryOperators = true;
1029 EXPECT_EQ(0, parseConfiguration("---\n"
1030 "BasedOnStyle: Google\n"
1031 "---\n"
1032 "Language: JavaScript\n"
1033 "IndentWidth: 76\n"
1034 "...\n",
1035 &Style)
1036 .value());
1037 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
1038 EXPECT_EQ(76u, Style.IndentWidth);
1039 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
1040 }
1041
1042 TEST(ConfigParseTest, ConfigurationRoundTripTest) {
1043 FormatStyle Style = getLLVMStyle();
1044 std::string YAML = configurationAsText(Style);
1045 FormatStyle ParsedStyle = {};
1046 ParsedStyle.Language = FormatStyle::LK_Cpp;
1047 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
1048 EXPECT_EQ(Style, ParsedStyle);
1049 }
1050
1051 TEST(ConfigParseTest, GetStyleWithEmptyFileName) {
1052 llvm::vfs::InMemoryFileSystem FS;
1053 auto Style1 = getStyle("file", "", "Google", "", &FS);
1054 ASSERT_TRUE((bool)Style1);
1055 ASSERT_EQ(*Style1, getGoogleStyle());
1056 }
1057
1058 TEST(ConfigParseTest, GetStyleOfFile) {
1059 llvm::vfs::InMemoryFileSystem FS;
1060 // Test 1: format file in the same directory.
1061 ASSERT_TRUE(
1062 FS.addFile("/a/.clang-format", 0,
1063 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
1064 ASSERT_TRUE(
1065 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1066 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
1067 ASSERT_TRUE((bool)Style1);
1068 ASSERT_EQ(*Style1, getLLVMStyle());
1069
1070 // Test 2.1: fallback to default.
1071 ASSERT_TRUE(
1072 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1073 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
1074 ASSERT_TRUE((bool)Style2);
1075 ASSERT_EQ(*Style2, getMozillaStyle());
1076
1077 // Test 2.2: no format on 'none' fallback style.
1078 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
1079 ASSERT_TRUE((bool)Style2);
1080 ASSERT_EQ(*Style2, getNoStyle());
1081
1082 // Test 2.3: format if config is found with no based style while fallback is
1083 // 'none'.
1084 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
1085 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
1086 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
1087 ASSERT_TRUE((bool)Style2);
1088 ASSERT_EQ(*Style2, getLLVMStyle());
1089
1090 // Test 2.4: format if yaml with no based style, while fallback is 'none'.
1091 Style2 = getStyle("{}", "a.h", "none", "", &FS);
1092 ASSERT_TRUE((bool)Style2);
1093 ASSERT_EQ(*Style2, getLLVMStyle());
1094
1095 // Test 3: format file in parent directory.
1096 ASSERT_TRUE(
1097 FS.addFile("/c/.clang-format", 0,
1098 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1099 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
1100 llvm::MemoryBuffer::getMemBuffer("int i;")));
1101 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1102 ASSERT_TRUE((bool)Style3);
1103 ASSERT_EQ(*Style3, getGoogleStyle());
1104
1105 // Test 4: error on invalid fallback style
1106 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
1107 ASSERT_FALSE((bool)Style4);
1108 llvm::consumeError(Style4.takeError());
1109
1110 // Test 5: error on invalid yaml on command line
1111 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
1112 ASSERT_FALSE((bool)Style5);
1113 llvm::consumeError(Style5.takeError());
1114
1115 // Test 6: error on invalid style
1116 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
1117 ASSERT_FALSE((bool)Style6);
1118 llvm::consumeError(Style6.takeError());
1119
1120 // Test 7: found config file, error on parsing it
1121 ASSERT_TRUE(
1122 FS.addFile("/d/.clang-format", 0,
1123 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
1124 "InvalidKey: InvalidValue")));
1125 ASSERT_TRUE(
1126 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1127 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
1128 ASSERT_FALSE((bool)Style7a);
1129 llvm::consumeError(Style7a.takeError());
1130
1131 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
1132 ASSERT_TRUE((bool)Style7b);
1133
1134 // Test 8: inferred per-language defaults apply.
1135 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
1136 ASSERT_TRUE((bool)StyleTd);
1137 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
1138
1139 // Test 9.1.1: overwriting a file style, when no parent file exists with no
1140 // fallback style.
1141 ASSERT_TRUE(FS.addFile(
1142 "/e/sub/.clang-format", 0,
1143 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
1144 "ColumnLimit: 20")));
1145 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
1146 llvm::MemoryBuffer::getMemBuffer("int i;")));
1147 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
1148 ASSERT_TRUE(static_cast<bool>(Style9));
1149 ASSERT_EQ(*Style9, [] {
1150 auto Style = getNoStyle();
1151 Style.ColumnLimit = 20;
1152 return Style;
1153 }());
1154
1155 // Test 9.1.2: propagate more than one level with no parent file.
1156 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
1157 llvm::MemoryBuffer::getMemBuffer("int i;")));
1158 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
1159 llvm::MemoryBuffer::getMemBuffer(
1160 "BasedOnStyle: InheritParentConfig\n"
1161 "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
1162 std::vector<std::string> NonDefaultWhiteSpaceMacros =
1163 Style9->WhitespaceSensitiveMacros;
1164 NonDefaultWhiteSpaceMacros[0] = "FOO";
1165 NonDefaultWhiteSpaceMacros[1] = "BAR";
1166
1167 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
1168 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
1169 ASSERT_TRUE(static_cast<bool>(Style9));
1170 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
1171 auto Style = getNoStyle();
1172 Style.ColumnLimit = 20;
1173 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
1174 return Style;
1175 }());
1176
1177 // Test 9.2: with LLVM fallback style
1178 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
1179 ASSERT_TRUE(static_cast<bool>(Style9));
1180 ASSERT_EQ(*Style9, [] {
1181 auto Style = getLLVMStyle();
1182 Style.ColumnLimit = 20;
1183 return Style;
1184 }());
1185
1186 // Test 9.3: with a parent file
1187 ASSERT_TRUE(
1188 FS.addFile("/e/.clang-format", 0,
1189 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
1190 "UseTab: Always")));
1191 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
1192 ASSERT_TRUE(static_cast<bool>(Style9));
1193 ASSERT_EQ(*Style9, [] {
1194 auto Style = getGoogleStyle();
1195 Style.ColumnLimit = 20;
1196 Style.UseTab = FormatStyle::UT_Always;
1197 return Style;
1198 }());
1199
1200 // Test 9.4: propagate more than one level with a parent file.
1201 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
1202 auto Style = getGoogleStyle();
1203 Style.ColumnLimit = 20;
1204 Style.UseTab = FormatStyle::UT_Always;
1205 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
1206 return Style;
1207 }();
1208
1209 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
1210 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
1211 ASSERT_TRUE(static_cast<bool>(Style9));
1212 ASSERT_EQ(*Style9, SubSubStyle);
1213
1214 // Test 9.5: use InheritParentConfig as style name
1215 Style9 =
1216 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
1217 ASSERT_TRUE(static_cast<bool>(Style9));
1218 ASSERT_EQ(*Style9, SubSubStyle);
1219
1220 // Test 9.6: use command line style with inheritance
1221 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}",
1222 "/e/sub/sub/code.cpp", "none", "", &FS);
1223 ASSERT_TRUE(static_cast<bool>(Style9));
1224 ASSERT_EQ(*Style9, SubSubStyle);
1225
1226 // Test 9.7: use command line style with inheritance and own config
1227 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
1228 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
1229 "/e/sub/code.cpp", "none", "", &FS);
1230 ASSERT_TRUE(static_cast<bool>(Style9));
1231 ASSERT_EQ(*Style9, SubSubStyle);
1232
1233 // Test 9.8: use inheritance from a file without BasedOnStyle
1234 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
1235 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
1236 ASSERT_TRUE(
1237 FS.addFile("/e/withoutbase/sub/.clang-format", 0,
1238 llvm::MemoryBuffer::getMemBuffer(
1239 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
1240 // Make sure we do not use the fallback style
1241 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
1242 ASSERT_TRUE(static_cast<bool>(Style9));
1243 ASSERT_EQ(*Style9, [] {
1244 auto Style = getLLVMStyle();
1245 Style.ColumnLimit = 123;
1246 return Style;
1247 }());
1248
1249 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
1250 ASSERT_TRUE(static_cast<bool>(Style9));
1251 ASSERT_EQ(*Style9, [] {
1252 auto Style = getLLVMStyle();
1253 Style.ColumnLimit = 123;
1254 Style.IndentWidth = 7;
1255 return Style;
1256 }());
1257
1258 // Test 9.9: use inheritance from a specific config file.
1259 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
1260 "none", "", &FS);
1261 ASSERT_TRUE(static_cast<bool>(Style9));
1262 ASSERT_EQ(*Style9, SubSubStyle);
1263 }
1264
1265 TEST(ConfigParseTest, GetStyleOfSpecificFile) {
1266 llvm::vfs::InMemoryFileSystem FS;
1267 // Specify absolute path to a format file in a parent directory.
1268 ASSERT_TRUE(
1269 FS.addFile("/e/.clang-format", 0,
1270 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
1271 ASSERT_TRUE(
1272 FS.addFile("/e/explicit.clang-format", 0,
1273 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1274 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
1275 llvm::MemoryBuffer::getMemBuffer("int i;")));
1276 auto Style = getStyle("file:/e/explicit.clang-format",
1277 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1278 ASSERT_TRUE(static_cast<bool>(Style));
1279 ASSERT_EQ(*Style, getGoogleStyle());
1280
1281 // Specify relative path to a format file.
1282 ASSERT_TRUE(
1283 FS.addFile("../../e/explicit.clang-format", 0,
1284 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1285 Style = getStyle("file:../../e/explicit.clang-format",
1286 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1287 ASSERT_TRUE(static_cast<bool>(Style));
1288 ASSERT_EQ(*Style, getGoogleStyle());
1289
1290 // Specify path to a format file that does not exist.
1291 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
1292 "LLVM", "", &FS);
1293 ASSERT_FALSE(static_cast<bool>(Style));
1294 llvm::consumeError(Style.takeError());
1295
1296 // Specify path to a file on the filesystem.
1297 SmallString<128> FormatFilePath;
1298 std::error_code ECF = llvm::sys::fs::createTemporaryFile(
1299 "FormatFileTest", "tpl", FormatFilePath);
1300 EXPECT_FALSE((bool)ECF);
1301 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
1302 EXPECT_FALSE((bool)ECF);
1303 FormatFileTest << "BasedOnStyle: Google\n";
1304 FormatFileTest.close();
1305
1306 SmallString<128> TestFilePath;
1307 std::error_code ECT =
1308 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
1309 EXPECT_FALSE((bool)ECT);
1310 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
1311 CodeFileTest << "int i;\n";
1312 CodeFileTest.close();
1313
1314 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
1315 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
1316
1317 llvm::sys::fs::remove(FormatFilePath.c_str());
1318 llvm::sys::fs::remove(TestFilePath.c_str());
1319 ASSERT_TRUE(static_cast<bool>(Style));
1320 ASSERT_EQ(*Style, getGoogleStyle());
1321 }
1322
1323 } // namespace
1324 } // namespace format
1325 } // namespace clang