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