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