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