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