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