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

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