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