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