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