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