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