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