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