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