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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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