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