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