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