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