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