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