xref: /llvm-project/clang/unittests/Format/ConfigParseTest.cpp (revision 486ec4bd7466cda444a7da6386a1bbb2db89a33f)
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   // FIXME: This is required because parsing a configuration simply overwrites
869   // the first N elements of the list instead of resetting it.
870   Style.ForEachMacros.clear();
871   std::vector<std::string> BoostForeach;
872   BoostForeach.push_back("BOOST_FOREACH");
873   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
874   std::vector<std::string> BoostAndQForeach;
875   BoostAndQForeach.push_back("BOOST_FOREACH");
876   BoostAndQForeach.push_back("Q_FOREACH");
877   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
878               BoostAndQForeach);
879 
880   Style.IfMacros.clear();
881   std::vector<std::string> CustomIfs;
882   CustomIfs.push_back("MYIF");
883   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
884 
885   Style.AttributeMacros.clear();
886   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
887               std::vector<std::string>{"__capability"});
888   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
889               std::vector<std::string>({"attr1", "attr2"}));
890 
891   Style.StatementAttributeLikeMacros.clear();
892   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
893               StatementAttributeLikeMacros,
894               std::vector<std::string>({"emit", "Q_EMIT"}));
895 
896   Style.StatementMacros.clear();
897   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
898               std::vector<std::string>{"QUNUSED"});
899   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
900               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
901 
902   Style.NamespaceMacros.clear();
903   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
904               std::vector<std::string>{"TESTSUITE"});
905   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
906               std::vector<std::string>({"TESTSUITE", "SUITE"}));
907 
908   Style.WhitespaceSensitiveMacros.clear();
909   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
910               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
911   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
912               WhitespaceSensitiveMacros,
913               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
914   Style.WhitespaceSensitiveMacros.clear();
915   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
916               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
917   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
918               WhitespaceSensitiveMacros,
919               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
920 
921   Style.IncludeStyle.IncludeCategories.clear();
922   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
923       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
924   CHECK_PARSE("IncludeCategories:\n"
925               "  - Regex: abc/.*\n"
926               "    Priority: 2\n"
927               "  - Regex: .*\n"
928               "    Priority: 1\n"
929               "    CaseSensitive: true",
930               IncludeStyle.IncludeCategories, ExpectedCategories);
931   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
932               "abc$");
933   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
934               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
935 
936   Style.SortIncludes = FormatStyle::SI_Never;
937   CHECK_PARSE("SortIncludes: true", SortIncludes,
938               FormatStyle::SI_CaseSensitive);
939   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
940   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
941               FormatStyle::SI_CaseInsensitive);
942   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
943               FormatStyle::SI_CaseSensitive);
944   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
945 
946   Style.RawStringFormats.clear();
947   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
948       {
949           FormatStyle::LK_TextProto,
950           {"pb", "proto"},
951           {"PARSE_TEXT_PROTO"},
952           /*CanonicalDelimiter=*/"",
953           "llvm",
954       },
955       {
956           FormatStyle::LK_Cpp,
957           {"cc", "cpp"},
958           {"C_CODEBLOCK", "CPPEVAL"},
959           /*CanonicalDelimiter=*/"cc",
960           /*BasedOnStyle=*/"",
961       },
962   };
963 
964   CHECK_PARSE("RawStringFormats:\n"
965               "  - Language: TextProto\n"
966               "    Delimiters:\n"
967               "      - 'pb'\n"
968               "      - 'proto'\n"
969               "    EnclosingFunctions:\n"
970               "      - 'PARSE_TEXT_PROTO'\n"
971               "    BasedOnStyle: llvm\n"
972               "  - Language: Cpp\n"
973               "    Delimiters:\n"
974               "      - 'cc'\n"
975               "      - 'cpp'\n"
976               "    EnclosingFunctions:\n"
977               "      - 'C_CODEBLOCK'\n"
978               "      - 'CPPEVAL'\n"
979               "    CanonicalDelimiter: 'cc'",
980               RawStringFormats, ExpectedRawStringFormats);
981 
982   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
983               "  Minimum: 0\n"
984               "  Maximum: 0",
985               SpacesInLineCommentPrefix.Minimum, 0u);
986   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
987   Style.SpacesInLineCommentPrefix.Minimum = 1;
988   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
989               "  Minimum: 2",
990               SpacesInLineCommentPrefix.Minimum, 0u);
991   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
992               "  Maximum: -1",
993               SpacesInLineCommentPrefix.Maximum, -1u);
994   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
995               "  Minimum: 2",
996               SpacesInLineCommentPrefix.Minimum, 2u);
997   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
998               "  Maximum: 1",
999               SpacesInLineCommentPrefix.Maximum, 1u);
1000   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
1001 
1002   Style.SpacesInAngles = FormatStyle::SIAS_Always;
1003   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
1004   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
1005               FormatStyle::SIAS_Always);
1006   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
1007   // For backward compatibility:
1008   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
1009   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
1010 
1011   CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition,
1012               FormatStyle::RCPS_WithPreceding);
1013   CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition,
1014               FormatStyle::RCPS_WithFollowing);
1015   CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition,
1016               FormatStyle::RCPS_SingleLine);
1017   CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
1018               FormatStyle::RCPS_OwnLine);
1019 
1020   CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
1021               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
1022   CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
1023               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
1024   CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed",
1025               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
1026   // For backward compatibility:
1027   CHECK_PARSE("BreakBeforeConceptDeclarations: true",
1028               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always);
1029   CHECK_PARSE("BreakBeforeConceptDeclarations: false",
1030               BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed);
1031 
1032   CHECK_PARSE("BreakAfterAttributes: Always", BreakAfterAttributes,
1033               FormatStyle::ABS_Always);
1034   CHECK_PARSE("BreakAfterAttributes: Leave", BreakAfterAttributes,
1035               FormatStyle::ABS_Leave);
1036   CHECK_PARSE("BreakAfterAttributes: Never", BreakAfterAttributes,
1037               FormatStyle::ABS_Never);
1038 
1039   const auto DefaultLineEnding = FormatStyle::LE_DeriveLF;
1040   CHECK_PARSE("LineEnding: LF", LineEnding, FormatStyle::LE_LF);
1041   CHECK_PARSE("LineEnding: CRLF", LineEnding, FormatStyle::LE_CRLF);
1042   CHECK_PARSE("LineEnding: DeriveCRLF", LineEnding, FormatStyle::LE_DeriveCRLF);
1043   CHECK_PARSE("LineEnding: DeriveLF", LineEnding, DefaultLineEnding);
1044   // For backward compatibility:
1045   CHECK_PARSE("DeriveLineEnding: false", LineEnding, FormatStyle::LE_LF);
1046   Style.LineEnding = DefaultLineEnding;
1047   CHECK_PARSE("DeriveLineEnding: false\n"
1048               "UseCRLF: true",
1049               LineEnding, FormatStyle::LE_CRLF);
1050   Style.LineEnding = DefaultLineEnding;
1051   CHECK_PARSE("UseCRLF: true", LineEnding, FormatStyle::LE_DeriveCRLF);
1052 
1053   CHECK_PARSE("RemoveParentheses: MultipleParentheses", RemoveParentheses,
1054               FormatStyle::RPS_MultipleParentheses);
1055   CHECK_PARSE("RemoveParentheses: ReturnStatement", RemoveParentheses,
1056               FormatStyle::RPS_ReturnStatement);
1057   CHECK_PARSE("RemoveParentheses: Leave", RemoveParentheses,
1058               FormatStyle::RPS_Leave);
1059 
1060   CHECK_PARSE("AllowBreakBeforeNoexceptSpecifier: Always",
1061               AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Always);
1062   CHECK_PARSE("AllowBreakBeforeNoexceptSpecifier: OnlyWithParen",
1063               AllowBreakBeforeNoexceptSpecifier,
1064               FormatStyle::BBNSS_OnlyWithParen);
1065   CHECK_PARSE("AllowBreakBeforeNoexceptSpecifier: Never",
1066               AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
1067 
1068   Style.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
1069   CHECK_PARSE("SeparateDefinitionBlocks: Always", SeparateDefinitionBlocks,
1070               FormatStyle::SDS_Always);
1071   CHECK_PARSE("SeparateDefinitionBlocks: Leave", SeparateDefinitionBlocks,
1072               FormatStyle::SDS_Leave);
1073   CHECK_PARSE("SeparateDefinitionBlocks: Never", SeparateDefinitionBlocks,
1074               FormatStyle::SDS_Never);
1075 }
1076 
1077 TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
1078   FormatStyle Style = {};
1079   Style.Language = FormatStyle::LK_Cpp;
1080   CHECK_PARSE("Language: Cpp\n"
1081               "IndentWidth: 12",
1082               IndentWidth, 12u);
1083   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
1084                                "IndentWidth: 34",
1085                                &Style),
1086             ParseError::Unsuitable);
1087   FormatStyle BinPackedTCS = {};
1088   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
1089   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
1090                                "InsertTrailingCommas: Wrapped",
1091                                &BinPackedTCS),
1092             ParseError::BinPackTrailingCommaConflict);
1093   EXPECT_EQ(12u, Style.IndentWidth);
1094   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
1095   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
1096 
1097   Style.Language = FormatStyle::LK_JavaScript;
1098   CHECK_PARSE("Language: JavaScript\n"
1099               "IndentWidth: 12",
1100               IndentWidth, 12u);
1101   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
1102   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
1103                                "IndentWidth: 34",
1104                                &Style),
1105             ParseError::Unsuitable);
1106   EXPECT_EQ(23u, Style.IndentWidth);
1107   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
1108   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
1109 
1110   CHECK_PARSE("BasedOnStyle: LLVM\n"
1111               "IndentWidth: 67",
1112               IndentWidth, 67u);
1113 
1114   CHECK_PARSE("---\n"
1115               "Language: JavaScript\n"
1116               "IndentWidth: 12\n"
1117               "---\n"
1118               "Language: Cpp\n"
1119               "IndentWidth: 34\n"
1120               "...\n",
1121               IndentWidth, 12u);
1122 
1123   Style.Language = FormatStyle::LK_Cpp;
1124   CHECK_PARSE("---\n"
1125               "Language: JavaScript\n"
1126               "IndentWidth: 12\n"
1127               "---\n"
1128               "Language: Cpp\n"
1129               "IndentWidth: 34\n"
1130               "...\n",
1131               IndentWidth, 34u);
1132   CHECK_PARSE("---\n"
1133               "IndentWidth: 78\n"
1134               "---\n"
1135               "Language: JavaScript\n"
1136               "IndentWidth: 56\n"
1137               "...\n",
1138               IndentWidth, 78u);
1139 
1140   Style.ColumnLimit = 123;
1141   Style.IndentWidth = 234;
1142   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
1143   Style.TabWidth = 345;
1144   EXPECT_FALSE(parseConfiguration("---\n"
1145                                   "IndentWidth: 456\n"
1146                                   "BreakBeforeBraces: Allman\n"
1147                                   "---\n"
1148                                   "Language: JavaScript\n"
1149                                   "IndentWidth: 111\n"
1150                                   "TabWidth: 111\n"
1151                                   "---\n"
1152                                   "Language: Cpp\n"
1153                                   "BreakBeforeBraces: Stroustrup\n"
1154                                   "TabWidth: 789\n"
1155                                   "...\n",
1156                                   &Style));
1157   EXPECT_EQ(123u, Style.ColumnLimit);
1158   EXPECT_EQ(456u, Style.IndentWidth);
1159   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
1160   EXPECT_EQ(789u, Style.TabWidth);
1161 
1162   EXPECT_EQ(parseConfiguration("---\n"
1163                                "Language: JavaScript\n"
1164                                "IndentWidth: 56\n"
1165                                "---\n"
1166                                "IndentWidth: 78\n"
1167                                "...\n",
1168                                &Style),
1169             ParseError::Error);
1170   EXPECT_EQ(parseConfiguration("---\n"
1171                                "Language: JavaScript\n"
1172                                "IndentWidth: 56\n"
1173                                "---\n"
1174                                "Language: JavaScript\n"
1175                                "IndentWidth: 78\n"
1176                                "...\n",
1177                                &Style),
1178             ParseError::Error);
1179 
1180   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
1181 
1182   Style.Language = FormatStyle::LK_Verilog;
1183   CHECK_PARSE("---\n"
1184               "Language: Verilog\n"
1185               "IndentWidth: 12\n"
1186               "---\n"
1187               "Language: Cpp\n"
1188               "IndentWidth: 34\n"
1189               "...\n",
1190               IndentWidth, 12u);
1191   CHECK_PARSE("---\n"
1192               "IndentWidth: 78\n"
1193               "---\n"
1194               "Language: Verilog\n"
1195               "IndentWidth: 56\n"
1196               "...\n",
1197               IndentWidth, 56u);
1198 }
1199 
1200 TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
1201   FormatStyle Style = {};
1202   Style.Language = FormatStyle::LK_JavaScript;
1203   Style.BreakBeforeTernaryOperators = true;
1204   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
1205   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
1206 
1207   Style.BreakBeforeTernaryOperators = true;
1208   EXPECT_EQ(0, parseConfiguration("---\n"
1209                                   "BasedOnStyle: Google\n"
1210                                   "---\n"
1211                                   "Language: JavaScript\n"
1212                                   "IndentWidth: 76\n"
1213                                   "...\n",
1214                                   &Style)
1215                    .value());
1216   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
1217   EXPECT_EQ(76u, Style.IndentWidth);
1218   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
1219 }
1220 
1221 TEST(ConfigParseTest, ConfigurationRoundTripTest) {
1222   FormatStyle Style = getLLVMStyle();
1223   std::string YAML = configurationAsText(Style);
1224   FormatStyle ParsedStyle = {};
1225   ParsedStyle.Language = FormatStyle::LK_Cpp;
1226   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
1227   EXPECT_EQ(Style, ParsedStyle);
1228 }
1229 
1230 TEST(ConfigParseTest, GetStyleWithEmptyFileName) {
1231   llvm::vfs::InMemoryFileSystem FS;
1232   auto Style1 = getStyle("file", "", "Google", "", &FS);
1233   ASSERT_TRUE((bool)Style1);
1234   ASSERT_EQ(*Style1, getGoogleStyle());
1235 }
1236 
1237 TEST(ConfigParseTest, GetStyleOfFile) {
1238   llvm::vfs::InMemoryFileSystem FS;
1239   // Test 1: format file in the same directory.
1240   ASSERT_TRUE(
1241       FS.addFile("/a/.clang-format", 0,
1242                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
1243   ASSERT_TRUE(
1244       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1245   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
1246   ASSERT_TRUE((bool)Style1);
1247   ASSERT_EQ(*Style1, getLLVMStyle());
1248 
1249   // Test 2.1: fallback to default.
1250   ASSERT_TRUE(
1251       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1252   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
1253   ASSERT_TRUE((bool)Style2);
1254   ASSERT_EQ(*Style2, getMozillaStyle());
1255 
1256   // Test 2.2: no format on 'none' fallback style.
1257   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
1258   ASSERT_TRUE((bool)Style2);
1259   ASSERT_EQ(*Style2, getNoStyle());
1260 
1261   // Test 2.3: format if config is found with no based style while fallback is
1262   // 'none'.
1263   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
1264                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
1265   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
1266   ASSERT_TRUE((bool)Style2);
1267   ASSERT_EQ(*Style2, getLLVMStyle());
1268 
1269   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
1270   Style2 = getStyle("{}", "a.h", "none", "", &FS);
1271   ASSERT_TRUE((bool)Style2);
1272   ASSERT_EQ(*Style2, getLLVMStyle());
1273 
1274   // Test 3: format file in parent directory.
1275   ASSERT_TRUE(
1276       FS.addFile("/c/.clang-format", 0,
1277                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1278   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
1279                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1280   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1281   ASSERT_TRUE((bool)Style3);
1282   ASSERT_EQ(*Style3, getGoogleStyle());
1283 
1284   // Test 4: error on invalid fallback style
1285   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
1286   ASSERT_FALSE((bool)Style4);
1287   llvm::consumeError(Style4.takeError());
1288 
1289   // Test 5: error on invalid yaml on command line
1290   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
1291                          /*AllowUnknownOptions=*/false, dropDiagnosticHandler);
1292   ASSERT_FALSE((bool)Style5);
1293   llvm::consumeError(Style5.takeError());
1294 
1295   // Test 6: error on invalid style
1296   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
1297   ASSERT_FALSE((bool)Style6);
1298   llvm::consumeError(Style6.takeError());
1299 
1300   // Test 7: found config file, error on parsing it
1301   ASSERT_TRUE(
1302       FS.addFile("/d/.clang-format", 0,
1303                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
1304                                                   "InvalidKey: InvalidValue")));
1305   ASSERT_TRUE(
1306       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
1307   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS,
1308                           /*AllowUnknownOptions=*/false, dropDiagnosticHandler);
1309   ASSERT_FALSE((bool)Style7a);
1310   llvm::consumeError(Style7a.takeError());
1311 
1312   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS,
1313                           /*AllowUnknownOptions=*/true, dropDiagnosticHandler);
1314   ASSERT_TRUE((bool)Style7b);
1315 
1316   // Test 8: inferred per-language defaults apply.
1317   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
1318   ASSERT_TRUE((bool)StyleTd);
1319   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
1320 
1321   // Test 9.1.1: overwriting a file style, when no parent file exists with no
1322   // fallback style.
1323   ASSERT_TRUE(FS.addFile(
1324       "/e/sub/.clang-format", 0,
1325       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
1326                                        "ColumnLimit: 20")));
1327   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
1328                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1329   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
1330   ASSERT_TRUE(static_cast<bool>(Style9));
1331   ASSERT_EQ(*Style9, [] {
1332     auto Style = getNoStyle();
1333     Style.ColumnLimit = 20;
1334     return Style;
1335   }());
1336 
1337   // Test 9.1.2: propagate more than one level with no parent file.
1338   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
1339                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1340   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
1341                          llvm::MemoryBuffer::getMemBuffer(
1342                              "BasedOnStyle: InheritParentConfig\n"
1343                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
1344   std::vector<std::string> NonDefaultWhiteSpaceMacros =
1345       Style9->WhitespaceSensitiveMacros;
1346   NonDefaultWhiteSpaceMacros[0] = "FOO";
1347   NonDefaultWhiteSpaceMacros[1] = "BAR";
1348 
1349   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
1350   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
1351   ASSERT_TRUE(static_cast<bool>(Style9));
1352   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
1353     auto Style = getNoStyle();
1354     Style.ColumnLimit = 20;
1355     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
1356     return Style;
1357   }());
1358 
1359   // Test 9.2: with LLVM fallback style
1360   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
1361   ASSERT_TRUE(static_cast<bool>(Style9));
1362   ASSERT_EQ(*Style9, [] {
1363     auto Style = getLLVMStyle();
1364     Style.ColumnLimit = 20;
1365     return Style;
1366   }());
1367 
1368   // Test 9.3: with a parent file
1369   ASSERT_TRUE(
1370       FS.addFile("/e/.clang-format", 0,
1371                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
1372                                                   "UseTab: Always")));
1373   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
1374   ASSERT_TRUE(static_cast<bool>(Style9));
1375   ASSERT_EQ(*Style9, [] {
1376     auto Style = getGoogleStyle();
1377     Style.ColumnLimit = 20;
1378     Style.UseTab = FormatStyle::UT_Always;
1379     return Style;
1380   }());
1381 
1382   // Test 9.4: propagate more than one level with a parent file.
1383   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
1384     auto Style = getGoogleStyle();
1385     Style.ColumnLimit = 20;
1386     Style.UseTab = FormatStyle::UT_Always;
1387     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
1388     return Style;
1389   }();
1390 
1391   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
1392   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
1393   ASSERT_TRUE(static_cast<bool>(Style9));
1394   ASSERT_EQ(*Style9, SubSubStyle);
1395 
1396   // Test 9.5: use InheritParentConfig as style name
1397   Style9 =
1398       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
1399   ASSERT_TRUE(static_cast<bool>(Style9));
1400   ASSERT_EQ(*Style9, SubSubStyle);
1401 
1402   // Test 9.6: use command line style with inheritance
1403   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}",
1404                     "/e/sub/sub/code.cpp", "none", "", &FS);
1405   ASSERT_TRUE(static_cast<bool>(Style9));
1406   ASSERT_EQ(*Style9, SubSubStyle);
1407 
1408   // Test 9.7: use command line style with inheritance and own config
1409   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
1410                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
1411                     "/e/sub/code.cpp", "none", "", &FS);
1412   ASSERT_TRUE(static_cast<bool>(Style9));
1413   ASSERT_EQ(*Style9, SubSubStyle);
1414 
1415   // Test 9.8: use inheritance from a file without BasedOnStyle
1416   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
1417                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
1418   ASSERT_TRUE(
1419       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
1420                  llvm::MemoryBuffer::getMemBuffer(
1421                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
1422   // Make sure we do not use the fallback style
1423   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
1424   ASSERT_TRUE(static_cast<bool>(Style9));
1425   ASSERT_EQ(*Style9, [] {
1426     auto Style = getLLVMStyle();
1427     Style.ColumnLimit = 123;
1428     return Style;
1429   }());
1430 
1431   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
1432   ASSERT_TRUE(static_cast<bool>(Style9));
1433   ASSERT_EQ(*Style9, [] {
1434     auto Style = getLLVMStyle();
1435     Style.ColumnLimit = 123;
1436     Style.IndentWidth = 7;
1437     return Style;
1438   }());
1439 
1440   // Test 9.9: use inheritance from a specific config file.
1441   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
1442                     "none", "", &FS);
1443   ASSERT_TRUE(static_cast<bool>(Style9));
1444   ASSERT_EQ(*Style9, SubSubStyle);
1445 }
1446 
1447 TEST(ConfigParseTest, GetStyleOfSpecificFile) {
1448   llvm::vfs::InMemoryFileSystem FS;
1449   // Specify absolute path to a format file in a parent directory.
1450   ASSERT_TRUE(
1451       FS.addFile("/e/.clang-format", 0,
1452                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
1453   ASSERT_TRUE(
1454       FS.addFile("/e/explicit.clang-format", 0,
1455                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1456   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
1457                          llvm::MemoryBuffer::getMemBuffer("int i;")));
1458   auto Style = getStyle("file:/e/explicit.clang-format",
1459                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1460   ASSERT_TRUE(static_cast<bool>(Style));
1461   ASSERT_EQ(*Style, getGoogleStyle());
1462 
1463   // Specify relative path to a format file.
1464   ASSERT_TRUE(
1465       FS.addFile("../../e/explicit.clang-format", 0,
1466                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
1467   Style = getStyle("file:../../e/explicit.clang-format",
1468                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
1469   ASSERT_TRUE(static_cast<bool>(Style));
1470   ASSERT_EQ(*Style, getGoogleStyle());
1471 
1472   // Specify path to a format file that does not exist.
1473   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
1474                    "LLVM", "", &FS);
1475   ASSERT_FALSE(static_cast<bool>(Style));
1476   llvm::consumeError(Style.takeError());
1477 
1478   // Specify path to a file on the filesystem.
1479   SmallString<128> FormatFilePath;
1480   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
1481       "FormatFileTest", "tpl", FormatFilePath);
1482   EXPECT_FALSE((bool)ECF);
1483   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
1484   EXPECT_FALSE((bool)ECF);
1485   FormatFileTest << "BasedOnStyle: Google\n";
1486   FormatFileTest.close();
1487 
1488   SmallString<128> TestFilePath;
1489   std::error_code ECT =
1490       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
1491   EXPECT_FALSE((bool)ECT);
1492   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
1493   CodeFileTest << "int i;\n";
1494   CodeFileTest.close();
1495 
1496   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
1497   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
1498 
1499   llvm::sys::fs::remove(FormatFilePath.c_str());
1500   llvm::sys::fs::remove(TestFilePath.c_str());
1501   ASSERT_TRUE(static_cast<bool>(Style));
1502   ASSERT_EQ(*Style, getGoogleStyle());
1503 }
1504 
1505 TEST(ConfigParseTest, GetStyleOutput) {
1506   llvm::vfs::InMemoryFileSystem FS;
1507 
1508   // Don't suppress output.
1509   testing::internal::CaptureStderr();
1510   auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
1511                         /*AllowUnknownOptions=*/true);
1512   auto Output = testing::internal::GetCapturedStderr();
1513   ASSERT_TRUE((bool)Style);
1514   ASSERT_FALSE(Output.empty());
1515 
1516   // Suppress stderr.
1517   testing::internal::CaptureStderr();
1518   Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
1519                    /*AllowUnknownOptions=*/true, dropDiagnosticHandler);
1520   Output = testing::internal::GetCapturedStderr();
1521   ASSERT_TRUE((bool)Style);
1522   ASSERT_TRUE(Output.empty());
1523 }
1524 
1525 } // namespace
1526 } // namespace format
1527 } // namespace clang
1528