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