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