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

source code of clang/unittests/Format/ConfigParseTest.cpp