xref: /llvm-project/clang/unittests/Format/ConfigParseTest.cpp (revision 84e3fdfc65232a4c940c2c3c9a2a8fe6c9b7806d)
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   // For backward compatibility:
595   Style.SpacesInParens = FormatStyle::SIPO_Never;
596   Style.SpacesInParensOptions = {};
597   CHECK_PARSE("SpacesInParentheses: true", SpacesInParens,
598               FormatStyle::SIPO_Custom);
599   Style.SpacesInParens = FormatStyle::SIPO_Never;
600   Style.SpacesInParensOptions = {};
601   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
602               FormatStyle::SpacesInParensCustom(true, false, false, true));
603   Style.SpacesInParens = FormatStyle::SIPO_Never;
604   Style.SpacesInParensOptions = {};
605   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
606               FormatStyle::SpacesInParensCustom(true, false, false, false));
607   Style.SpacesInParens = FormatStyle::SIPO_Never;
608   Style.SpacesInParensOptions = {};
609   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
610               FormatStyle::SpacesInParensCustom(false, true, false, false));
611   Style.SpacesInParens = FormatStyle::SIPO_Never;
612   Style.SpacesInParensOptions = {};
613   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
614               FormatStyle::SpacesInParensCustom(false, false, true, false));
615   Style.SpacesInParens = FormatStyle::SIPO_Never;
616   Style.SpacesInParensOptions = {};
617 
618   Style.ColumnLimit = 123;
619   FormatStyle BaseStyle = getLLVMStyle();
620   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
621   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
622 
623   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
624   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
625               FormatStyle::BS_Attach);
626   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
627               FormatStyle::BS_Linux);
628   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
629               FormatStyle::BS_Mozilla);
630   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
631               FormatStyle::BS_Stroustrup);
632   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
633               FormatStyle::BS_Allman);
634   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
635               FormatStyle::BS_Whitesmiths);
636   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
637   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
638               FormatStyle::BS_WebKit);
639   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
640               FormatStyle::BS_Custom);
641 
642   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
643   CHECK_PARSE("BraceWrapping:\n"
644               "  AfterControlStatement: MultiLine",
645               BraceWrapping.AfterControlStatement,
646               FormatStyle::BWACS_MultiLine);
647   CHECK_PARSE("BraceWrapping:\n"
648               "  AfterControlStatement: Always",
649               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
650   CHECK_PARSE("BraceWrapping:\n"
651               "  AfterControlStatement: Never",
652               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
653   // For backward compatibility:
654   CHECK_PARSE("BraceWrapping:\n"
655               "  AfterControlStatement: true",
656               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
657   CHECK_PARSE("BraceWrapping:\n"
658               "  AfterControlStatement: false",
659               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
660 
661   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
662   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
663               FormatStyle::RTBS_None);
664   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
665               FormatStyle::RTBS_All);
666   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
667               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
668   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
669               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
670   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
671               AlwaysBreakAfterReturnType,
672               FormatStyle::RTBS_TopLevelDefinitions);
673 
674   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
675   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
676               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
677   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
678               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
679   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
680               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
681   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
682               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
683   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
684               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
685 
686   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
687   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
688               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
689   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
690               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
691   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
692               AlwaysBreakAfterDefinitionReturnType,
693               FormatStyle::DRTBS_TopLevel);
694 
695   Style.NamespaceIndentation = FormatStyle::NI_All;
696   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
697               FormatStyle::NI_None);
698   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
699               FormatStyle::NI_Inner);
700   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
701               FormatStyle::NI_All);
702 
703   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
704   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
705               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
706   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
707               AllowShortIfStatementsOnASingleLine,
708               FormatStyle::SIS_WithoutElse);
709   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
710               AllowShortIfStatementsOnASingleLine,
711               FormatStyle::SIS_OnlyFirstIf);
712   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
713               AllowShortIfStatementsOnASingleLine,
714               FormatStyle::SIS_AllIfsAndElse);
715   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
716               AllowShortIfStatementsOnASingleLine,
717               FormatStyle::SIS_OnlyFirstIf);
718   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
719               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
720   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
721               AllowShortIfStatementsOnASingleLine,
722               FormatStyle::SIS_WithoutElse);
723 
724   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
725   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
726               FormatStyle::IEBS_AfterExternBlock);
727   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
728               FormatStyle::IEBS_Indent);
729   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
730               FormatStyle::IEBS_NoIndent);
731   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
732               FormatStyle::IEBS_Indent);
733   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
734               FormatStyle::IEBS_NoIndent);
735 
736   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
737   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
738               FormatStyle::BFCS_Both);
739   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
740               FormatStyle::BFCS_None);
741   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
742               FormatStyle::BFCS_Before);
743   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
744               FormatStyle::BFCS_After);
745 
746   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
747   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
748               FormatStyle::SJSIO_After);
749   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
750               FormatStyle::SJSIO_Before);
751 
752   Style.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
753   CHECK_PARSE("SortUsingDeclarations: Never", SortUsingDeclarations,
754               FormatStyle::SUD_Never);
755   CHECK_PARSE("SortUsingDeclarations: Lexicographic", SortUsingDeclarations,
756               FormatStyle::SUD_Lexicographic);
757   CHECK_PARSE("SortUsingDeclarations: LexicographicNumeric",
758               SortUsingDeclarations, FormatStyle::SUD_LexicographicNumeric);
759   // For backward compatibility:
760   CHECK_PARSE("SortUsingDeclarations: false", SortUsingDeclarations,
761               FormatStyle::SUD_Never);
762   CHECK_PARSE("SortUsingDeclarations: true", SortUsingDeclarations,
763               FormatStyle::SUD_LexicographicNumeric);
764 
765   // FIXME: This is required because parsing a configuration simply overwrites
766   // the first N elements of the list instead of resetting it.
767   Style.ForEachMacros.clear();
768   std::vector<std::string> BoostForeach;
769   BoostForeach.push_back("BOOST_FOREACH");
770   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
771   std::vector<std::string> BoostAndQForeach;
772   BoostAndQForeach.push_back("BOOST_FOREACH");
773   BoostAndQForeach.push_back("Q_FOREACH");
774   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
775               BoostAndQForeach);
776 
777   Style.IfMacros.clear();
778   std::vector<std::string> CustomIfs;
779   CustomIfs.push_back("MYIF");
780   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
781 
782   Style.AttributeMacros.clear();
783   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
784               std::vector<std::string>{"__capability"});
785   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
786               std::vector<std::string>({"attr1", "attr2"}));
787 
788   Style.StatementAttributeLikeMacros.clear();
789   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
790               StatementAttributeLikeMacros,
791               std::vector<std::string>({"emit", "Q_EMIT"}));
792 
793   Style.StatementMacros.clear();
794   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
795               std::vector<std::string>{"QUNUSED"});
796   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
797               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
798 
799   Style.NamespaceMacros.clear();
800   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
801               std::vector<std::string>{"TESTSUITE"});
802   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
803               std::vector<std::string>({"TESTSUITE", "SUITE"}));
804 
805   Style.WhitespaceSensitiveMacros.clear();
806   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
807               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
808   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
809               WhitespaceSensitiveMacros,
810               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
811   Style.WhitespaceSensitiveMacros.clear();
812   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
813               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
814   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
815               WhitespaceSensitiveMacros,
816               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
817 
818   Style.IncludeStyle.IncludeCategories.clear();
819   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
820       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
821   CHECK_PARSE("IncludeCategories:\n"
822               "  - Regex: abc/.*\n"
823               "    Priority: 2\n"
824               "  - Regex: .*\n"
825               "    Priority: 1\n"
826               "    CaseSensitive: true\n",
827               IncludeStyle.IncludeCategories, ExpectedCategories);
828   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
829               "abc$");
830   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
831               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
832 
833   Style.SortIncludes = FormatStyle::SI_Never;
834   CHECK_PARSE("SortIncludes: true", SortIncludes,
835               FormatStyle::SI_CaseSensitive);
836   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
837   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
838               FormatStyle::SI_CaseInsensitive);
839   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
840               FormatStyle::SI_CaseSensitive);
841   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
842 
843   Style.RawStringFormats.clear();
844   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
845       {
846           FormatStyle::LK_TextProto,
847           {"pb", "proto"},
848           {"PARSE_TEXT_PROTO"},
849           /*CanonicalDelimiter=*/"",
850           "llvm",
851       },
852       {
853           FormatStyle::LK_Cpp,
854           {"cc", "cpp"},
855           {"C_CODEBLOCK", "CPPEVAL"},
856           /*CanonicalDelimiter=*/"cc",
857           /*BasedOnStyle=*/"",
858       },
859   };
860 
861   CHECK_PARSE("RawStringFormats:\n"
862               "  - Language: TextProto\n"
863               "    Delimiters:\n"
864               "      - 'pb'\n"
865               "      - 'proto'\n"
866               "    EnclosingFunctions:\n"
867               "      - 'PARSE_TEXT_PROTO'\n"
868               "    BasedOnStyle: llvm\n"
869               "  - Language: Cpp\n"
870               "    Delimiters:\n"
871               "      - 'cc'\n"
872               "      - 'cpp'\n"
873               "    EnclosingFunctions:\n"
874               "      - 'C_CODEBLOCK'\n"
875               "      - 'CPPEVAL'\n"
876               "    CanonicalDelimiter: 'cc'",
877               RawStringFormats, ExpectedRawStringFormats);
878 
879   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
880               "  Minimum: 0\n"
881               "  Maximum: 0",
882               SpacesInLineCommentPrefix.Minimum, 0u);
883   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
884   Style.SpacesInLineCommentPrefix.Minimum = 1;
885   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
886               "  Minimum: 2",
887               SpacesInLineCommentPrefix.Minimum, 0u);
888   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
889               "  Maximum: -1",
890               SpacesInLineCommentPrefix.Maximum, -1u);
891   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
892               "  Minimum: 2",
893               SpacesInLineCommentPrefix.Minimum, 2u);
894   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
895               "  Maximum: 1",
896               SpacesInLineCommentPrefix.Maximum, 1u);
897   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
898 
899   Style.SpacesInAngles = FormatStyle::SIAS_Always;
900   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
901   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
902               FormatStyle::SIAS_Always);
903   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
904   // For backward compatibility:
905   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
906   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
907 
908   CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition,
909               FormatStyle::RCPS_WithPreceding);
910   CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition,
911               FormatStyle::RCPS_WithFollowing);
912   CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition,
913               FormatStyle::RCPS_SingleLine);
914   CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
915               FormatStyle::RCPS_OwnLine);
916 
917   CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
918               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
919   CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
920               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
921   CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed",
922               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
923   // For backward compatibility:
924   CHECK_PARSE("BreakBeforeConceptDeclarations: true",
925               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
926   CHECK_PARSE("BreakBeforeConceptDeclarations: false",
927               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
928 
929   CHECK_PARSE("BreakAfterAttributes: Always", BreakAfterAttributes,
930               FormatStyle::ABS_Always);
931   CHECK_PARSE("BreakAfterAttributes: Leave", BreakAfterAttributes,
932               FormatStyle::ABS_Leave);
933   CHECK_PARSE("BreakAfterAttributes: Never", BreakAfterAttributes,
934               FormatStyle::ABS_Never);
935 
936   const auto DefaultLineEnding = FormatStyle::LE_DeriveLF;
937   CHECK_PARSE("LineEnding: LF", LineEnding, FormatStyle::LE_LF);
938   CHECK_PARSE("LineEnding: CRLF", LineEnding, FormatStyle::LE_CRLF);
939   CHECK_PARSE("LineEnding: DeriveCRLF", LineEnding, FormatStyle::LE_DeriveCRLF);
940   CHECK_PARSE("LineEnding: DeriveLF", LineEnding, DefaultLineEnding);
941   // For backward compatibility:
942   CHECK_PARSE("DeriveLineEnding: false", LineEnding, FormatStyle::LE_LF);
943   Style.LineEnding = DefaultLineEnding;
944   CHECK_PARSE("DeriveLineEnding: false\n"
945               "UseCRLF: true",
946               LineEnding, FormatStyle::LE_CRLF);
947   Style.LineEnding = DefaultLineEnding;
948   CHECK_PARSE("UseCRLF: true", LineEnding, FormatStyle::LE_DeriveCRLF);
949 
950   CHECK_PARSE("RemoveParentheses: MultipleParentheses", RemoveParentheses,
951               FormatStyle::RPS_MultipleParentheses);
952   CHECK_PARSE("RemoveParentheses: ReturnStatement", RemoveParentheses,
953               FormatStyle::RPS_ReturnStatement);
954   CHECK_PARSE("RemoveParentheses: Leave", RemoveParentheses,
955               FormatStyle::RPS_Leave);
956 
957   CHECK_PARSE("AllowBreakBeforeNoexceptSpecifier: Always",
958               AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Always);
959   CHECK_PARSE("AllowBreakBeforeNoexceptSpecifier: OnlyWithParen",
960               AllowBreakBeforeNoexceptSpecifier,
961               FormatStyle::BBNSS_OnlyWithParen);
962   CHECK_PARSE("AllowBreakBeforeNoexceptSpecifier: Never",
963               AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
964 }
965 
966 TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
967   FormatStyle Style = {};
968   Style.Language = FormatStyle::LK_Cpp;
969   CHECK_PARSE("Language: Cpp\n"
970               "IndentWidth: 12",
971               IndentWidth, 12u);
972   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
973                                "IndentWidth: 34",
974                                &Style),
975             ParseError::Unsuitable);
976   FormatStyle BinPackedTCS = {};
977   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
978   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
979                                "InsertTrailingCommas: Wrapped",
980                                &BinPackedTCS),
981             ParseError::BinPackTrailingCommaConflict);
982   EXPECT_EQ(12u, Style.IndentWidth);
983   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
984   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
985 
986   Style.Language = FormatStyle::LK_JavaScript;
987   CHECK_PARSE("Language: JavaScript\n"
988               "IndentWidth: 12",
989               IndentWidth, 12u);
990   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
991   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
992                                "IndentWidth: 34",
993                                &Style),
994             ParseError::Unsuitable);
995   EXPECT_EQ(23u, Style.IndentWidth);
996   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
997   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
998 
999   CHECK_PARSE("BasedOnStyle: LLVM\n"
1000               "IndentWidth: 67",
1001               IndentWidth, 67u);
1002 
1003   CHECK_PARSE("---\n"
1004               "Language: JavaScript\n"
1005               "IndentWidth: 12\n"
1006               "---\n"
1007               "Language: Cpp\n"
1008               "IndentWidth: 34\n"
1009               "...\n",
1010               IndentWidth, 12u);
1011 
1012   Style.Language = FormatStyle::LK_Cpp;
1013   CHECK_PARSE("---\n"
1014               "Language: JavaScript\n"
1015               "IndentWidth: 12\n"
1016               "---\n"
1017               "Language: Cpp\n"
1018               "IndentWidth: 34\n"
1019               "...\n",
1020               IndentWidth, 34u);
1021   CHECK_PARSE("---\n"
1022               "IndentWidth: 78\n"
1023               "---\n"
1024               "Language: JavaScript\n"
1025               "IndentWidth: 56\n"
1026               "...\n",
1027               IndentWidth, 78u);
1028 
1029   Style.ColumnLimit = 123;
1030   Style.IndentWidth = 234;
1031   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
1032   Style.TabWidth = 345;
1033   EXPECT_FALSE(parseConfiguration("---\n"
1034                                   "IndentWidth: 456\n"
1035                                   "BreakBeforeBraces: Allman\n"
1036                                   "---\n"
1037                                   "Language: JavaScript\n"
1038                                   "IndentWidth: 111\n"
1039                                   "TabWidth: 111\n"
1040                                   "---\n"
1041                                   "Language: Cpp\n"
1042                                   "BreakBeforeBraces: Stroustrup\n"
1043                                   "TabWidth: 789\n"
1044                                   "...\n",
1045                                   &Style));
1046   EXPECT_EQ(123u, Style.ColumnLimit);
1047   EXPECT_EQ(456u, Style.IndentWidth);
1048   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
1049   EXPECT_EQ(789u, Style.TabWidth);
1050 
1051   EXPECT_EQ(parseConfiguration("---\n"
1052                                "Language: JavaScript\n"
1053                                "IndentWidth: 56\n"
1054                                "---\n"
1055                                "IndentWidth: 78\n"
1056                                "...\n",
1057                                &Style),
1058             ParseError::Error);
1059   EXPECT_EQ(parseConfiguration("---\n"
1060                                "Language: JavaScript\n"
1061                                "IndentWidth: 56\n"
1062                                "---\n"
1063                                "Language: JavaScript\n"
1064                                "IndentWidth: 78\n"
1065                                "...\n",
1066                                &Style),
1067             ParseError::Error);
1068 
1069   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
1070 
1071   Style.Language = FormatStyle::LK_Verilog;
1072   CHECK_PARSE("---\n"
1073               "Language: Verilog\n"
1074               "IndentWidth: 12\n"
1075               "---\n"
1076               "Language: Cpp\n"
1077               "IndentWidth: 34\n"
1078               "...\n",
1079               IndentWidth, 12u);
1080   CHECK_PARSE("---\n"
1081               "IndentWidth: 78\n"
1082               "---\n"
1083               "Language: Verilog\n"
1084               "IndentWidth: 56\n"
1085               "...\n",
1086               IndentWidth, 56u);
1087 }
1088 
1089 TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
1090   FormatStyle Style = {};
1091   Style.Language = FormatStyle::LK_JavaScript;
1092   Style.BreakBeforeTernaryOperators = true;
1093   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
1094   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
1095 
1096   Style.BreakBeforeTernaryOperators = true;
1097   EXPECT_EQ(0, parseConfiguration("---\n"
1098                                   "BasedOnStyle: Google\n"
1099                                   "---\n"
1100                                   "Language: JavaScript\n"
1101                                   "IndentWidth: 76\n"
1102                                   "...\n",
1103                                   &Style)
1104                    .value());
1105   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
1106   EXPECT_EQ(76u, Style.IndentWidth);
1107   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
1108 }
1109 
1110 TEST(ConfigParseTest, ConfigurationRoundTripTest) {
1111   FormatStyle Style = getLLVMStyle();
1112   std::string YAML = configurationAsText(Style);
1113   FormatStyle ParsedStyle = {};
1114   ParsedStyle.Language = FormatStyle::LK_Cpp;
1115   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
1116   EXPECT_EQ(Style, ParsedStyle);
1117 }
1118 
1119 TEST(ConfigParseTest, GetStyleWithEmptyFileName) {
1120   llvm::vfs::InMemoryFileSystem FS;
1121   auto Style1 = getStyle("file", "", "Google", "", &FS);
1122   ASSERT_TRUE((bool)Style1);
1123   ASSERT_EQ(*Style1, getGoogleStyle());
1124 }
1125 
1126 TEST(ConfigParseTest, GetStyleOfFile) {
1127   llvm::vfs::InMemoryFileSystem FS;
1128   // Test 1: format file in the same directory.
1129   ASSERT_TRUE(
1130       FS.addFile("/a/.clang-format", 0,
1131                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
1132   ASSERT_TRUE(
1133       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1134   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
1135   ASSERT_TRUE((bool)Style1);
1136   ASSERT_EQ(*Style1, getLLVMStyle());
1137 
1138   // Test 2.1: fallback to default.
1139   ASSERT_TRUE(
1140       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1141   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
1142   ASSERT_TRUE((bool)Style2);
1143   ASSERT_EQ(*Style2, getMozillaStyle());
1144 
1145   // Test 2.2: no format on 'none' fallback style.
1146   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
1147   ASSERT_TRUE((bool)Style2);
1148   ASSERT_EQ(*Style2, getNoStyle());
1149 
1150   // Test 2.3: format if config is found with no based style while fallback is
1151   // 'none'.
1152   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
1153                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
1154   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
1155   ASSERT_TRUE((bool)Style2);
1156   ASSERT_EQ(*Style2, getLLVMStyle());
1157 
1158   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
1159   Style2 = getStyle("{}", "a.h", "none", "", &FS);
1160   ASSERT_TRUE((bool)Style2);
1161   ASSERT_EQ(*Style2, getLLVMStyle());
1162 
1163   // Test 3: format file in parent directory.
1164   ASSERT_TRUE(
1165       FS.addFile("/c/.clang-format", 0,
1166                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1167   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
1168                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1169   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1170   ASSERT_TRUE((bool)Style3);
1171   ASSERT_EQ(*Style3, getGoogleStyle());
1172 
1173   // Test 4: error on invalid fallback style
1174   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
1175   ASSERT_FALSE((bool)Style4);
1176   llvm::consumeError(Style4.takeError());
1177 
1178   // Test 5: error on invalid yaml on command line
1179   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
1180   ASSERT_FALSE((bool)Style5);
1181   llvm::consumeError(Style5.takeError());
1182 
1183   // Test 6: error on invalid style
1184   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
1185   ASSERT_FALSE((bool)Style6);
1186   llvm::consumeError(Style6.takeError());
1187 
1188   // Test 7: found config file, error on parsing it
1189   ASSERT_TRUE(
1190       FS.addFile("/d/.clang-format", 0,
1191                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
1192                                                   "InvalidKey: InvalidValue")));
1193   ASSERT_TRUE(
1194       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1195   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
1196   ASSERT_FALSE((bool)Style7a);
1197   llvm::consumeError(Style7a.takeError());
1198 
1199   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
1200   ASSERT_TRUE((bool)Style7b);
1201 
1202   // Test 8: inferred per-language defaults apply.
1203   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
1204   ASSERT_TRUE((bool)StyleTd);
1205   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
1206 
1207   // Test 9.1.1: overwriting a file style, when no parent file exists with no
1208   // fallback style.
1209   ASSERT_TRUE(FS.addFile(
1210       "/e/sub/.clang-format", 0,
1211       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
1212                                        "ColumnLimit: 20")));
1213   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
1214                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1215   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
1216   ASSERT_TRUE(static_cast<bool>(Style9));
1217   ASSERT_EQ(*Style9, [] {
1218     auto Style = getNoStyle();
1219     Style.ColumnLimit = 20;
1220     return Style;
1221   }());
1222 
1223   // Test 9.1.2: propagate more than one level with no parent file.
1224   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
1225                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1226   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
1227                          llvm::MemoryBuffer::getMemBuffer(
1228                              "BasedOnStyle: InheritParentConfig\n"
1229                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
1230   std::vector<std::string> NonDefaultWhiteSpaceMacros =
1231       Style9->WhitespaceSensitiveMacros;
1232   NonDefaultWhiteSpaceMacros[0] = "FOO";
1233   NonDefaultWhiteSpaceMacros[1] = "BAR";
1234 
1235   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
1236   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
1237   ASSERT_TRUE(static_cast<bool>(Style9));
1238   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
1239     auto Style = getNoStyle();
1240     Style.ColumnLimit = 20;
1241     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
1242     return Style;
1243   }());
1244 
1245   // Test 9.2: with LLVM fallback style
1246   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
1247   ASSERT_TRUE(static_cast<bool>(Style9));
1248   ASSERT_EQ(*Style9, [] {
1249     auto Style = getLLVMStyle();
1250     Style.ColumnLimit = 20;
1251     return Style;
1252   }());
1253 
1254   // Test 9.3: with a parent file
1255   ASSERT_TRUE(
1256       FS.addFile("/e/.clang-format", 0,
1257                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
1258                                                   "UseTab: Always")));
1259   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
1260   ASSERT_TRUE(static_cast<bool>(Style9));
1261   ASSERT_EQ(*Style9, [] {
1262     auto Style = getGoogleStyle();
1263     Style.ColumnLimit = 20;
1264     Style.UseTab = FormatStyle::UT_Always;
1265     return Style;
1266   }());
1267 
1268   // Test 9.4: propagate more than one level with a parent file.
1269   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
1270     auto Style = getGoogleStyle();
1271     Style.ColumnLimit = 20;
1272     Style.UseTab = FormatStyle::UT_Always;
1273     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
1274     return Style;
1275   }();
1276 
1277   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
1278   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
1279   ASSERT_TRUE(static_cast<bool>(Style9));
1280   ASSERT_EQ(*Style9, SubSubStyle);
1281 
1282   // Test 9.5: use InheritParentConfig as style name
1283   Style9 =
1284       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
1285   ASSERT_TRUE(static_cast<bool>(Style9));
1286   ASSERT_EQ(*Style9, SubSubStyle);
1287 
1288   // Test 9.6: use command line style with inheritance
1289   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}",
1290                     "/e/sub/sub/code.cpp", "none", "", &FS);
1291   ASSERT_TRUE(static_cast<bool>(Style9));
1292   ASSERT_EQ(*Style9, SubSubStyle);
1293 
1294   // Test 9.7: use command line style with inheritance and own config
1295   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
1296                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
1297                     "/e/sub/code.cpp", "none", "", &FS);
1298   ASSERT_TRUE(static_cast<bool>(Style9));
1299   ASSERT_EQ(*Style9, SubSubStyle);
1300 
1301   // Test 9.8: use inheritance from a file without BasedOnStyle
1302   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
1303                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
1304   ASSERT_TRUE(
1305       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
1306                  llvm::MemoryBuffer::getMemBuffer(
1307                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
1308   // Make sure we do not use the fallback style
1309   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
1310   ASSERT_TRUE(static_cast<bool>(Style9));
1311   ASSERT_EQ(*Style9, [] {
1312     auto Style = getLLVMStyle();
1313     Style.ColumnLimit = 123;
1314     return Style;
1315   }());
1316 
1317   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
1318   ASSERT_TRUE(static_cast<bool>(Style9));
1319   ASSERT_EQ(*Style9, [] {
1320     auto Style = getLLVMStyle();
1321     Style.ColumnLimit = 123;
1322     Style.IndentWidth = 7;
1323     return Style;
1324   }());
1325 
1326   // Test 9.9: use inheritance from a specific config file.
1327   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
1328                     "none", "", &FS);
1329   ASSERT_TRUE(static_cast<bool>(Style9));
1330   ASSERT_EQ(*Style9, SubSubStyle);
1331 }
1332 
1333 TEST(ConfigParseTest, GetStyleOfSpecificFile) {
1334   llvm::vfs::InMemoryFileSystem FS;
1335   // Specify absolute path to a format file in a parent directory.
1336   ASSERT_TRUE(
1337       FS.addFile("/e/.clang-format", 0,
1338                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
1339   ASSERT_TRUE(
1340       FS.addFile("/e/explicit.clang-format", 0,
1341                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1342   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
1343                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1344   auto Style = getStyle("file:/e/explicit.clang-format",
1345                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1346   ASSERT_TRUE(static_cast<bool>(Style));
1347   ASSERT_EQ(*Style, getGoogleStyle());
1348 
1349   // Specify relative path to a format file.
1350   ASSERT_TRUE(
1351       FS.addFile("../../e/explicit.clang-format", 0,
1352                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1353   Style = getStyle("file:../../e/explicit.clang-format",
1354                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1355   ASSERT_TRUE(static_cast<bool>(Style));
1356   ASSERT_EQ(*Style, getGoogleStyle());
1357 
1358   // Specify path to a format file that does not exist.
1359   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
1360                    "LLVM", "", &FS);
1361   ASSERT_FALSE(static_cast<bool>(Style));
1362   llvm::consumeError(Style.takeError());
1363 
1364   // Specify path to a file on the filesystem.
1365   SmallString<128> FormatFilePath;
1366   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
1367       "FormatFileTest", "tpl", FormatFilePath);
1368   EXPECT_FALSE((bool)ECF);
1369   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
1370   EXPECT_FALSE((bool)ECF);
1371   FormatFileTest << "BasedOnStyle: Google\n";
1372   FormatFileTest.close();
1373 
1374   SmallString<128> TestFilePath;
1375   std::error_code ECT =
1376       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
1377   EXPECT_FALSE((bool)ECT);
1378   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
1379   CodeFileTest << "int i;\n";
1380   CodeFileTest.close();
1381 
1382   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
1383   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
1384 
1385   llvm::sys::fs::remove(FormatFilePath.c_str());
1386   llvm::sys::fs::remove(TestFilePath.c_str());
1387   ASSERT_TRUE(static_cast<bool>(Style));
1388   ASSERT_EQ(*Style, getGoogleStyle());
1389 }
1390 
1391 } // namespace
1392 } // namespace format
1393 } // namespace clang
1394