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