Debian Codegraph @

clang/unittests/Format/ConfigParseTest.cpp

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