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