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