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 1001 TEST(ConfigParseTest, ParsesConfigurationWithLanguages) { 1002 FormatStyle Style = {}; 1003 Style.Language = FormatStyle::LK_Cpp; 1004 CHECK_PARSE("Language: Cpp\n" 1005 "IndentWidth: 12", 1006 IndentWidth, 12u); 1007 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 1008 "IndentWidth: 34", 1009 &Style), 1010 ParseError::Unsuitable); 1011 FormatStyle BinPackedTCS = {}; 1012 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 1013 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 1014 "InsertTrailingCommas: Wrapped", 1015 &BinPackedTCS), 1016 ParseError::BinPackTrailingCommaConflict); 1017 EXPECT_EQ(12u, Style.IndentWidth); 1018 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 1019 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 1020 1021 Style.Language = FormatStyle::LK_JavaScript; 1022 CHECK_PARSE("Language: JavaScript\n" 1023 "IndentWidth: 12", 1024 IndentWidth, 12u); 1025 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 1026 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 1027 "IndentWidth: 34", 1028 &Style), 1029 ParseError::Unsuitable); 1030 EXPECT_EQ(23u, Style.IndentWidth); 1031 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 1032 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 1033 1034 CHECK_PARSE("BasedOnStyle: LLVM\n" 1035 "IndentWidth: 67", 1036 IndentWidth, 67u); 1037 1038 CHECK_PARSE("---\n" 1039 "Language: JavaScript\n" 1040 "IndentWidth: 12\n" 1041 "---\n" 1042 "Language: Cpp\n" 1043 "IndentWidth: 34\n" 1044 "...\n", 1045 IndentWidth, 12u); 1046 1047 Style.Language = FormatStyle::LK_Cpp; 1048 CHECK_PARSE("---\n" 1049 "Language: JavaScript\n" 1050 "IndentWidth: 12\n" 1051 "---\n" 1052 "Language: Cpp\n" 1053 "IndentWidth: 34\n" 1054 "...\n", 1055 IndentWidth, 34u); 1056 CHECK_PARSE("---\n" 1057 "IndentWidth: 78\n" 1058 "---\n" 1059 "Language: JavaScript\n" 1060 "IndentWidth: 56\n" 1061 "...\n", 1062 IndentWidth, 78u); 1063 1064 Style.ColumnLimit = 123; 1065 Style.IndentWidth = 234; 1066 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 1067 Style.TabWidth = 345; 1068 EXPECT_FALSE(parseConfiguration("---\n" 1069 "IndentWidth: 456\n" 1070 "BreakBeforeBraces: Allman\n" 1071 "---\n" 1072 "Language: JavaScript\n" 1073 "IndentWidth: 111\n" 1074 "TabWidth: 111\n" 1075 "---\n" 1076 "Language: Cpp\n" 1077 "BreakBeforeBraces: Stroustrup\n" 1078 "TabWidth: 789\n" 1079 "...\n", 1080 &Style)); 1081 EXPECT_EQ(123u, Style.ColumnLimit); 1082 EXPECT_EQ(456u, Style.IndentWidth); 1083 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 1084 EXPECT_EQ(789u, Style.TabWidth); 1085 1086 EXPECT_EQ(parseConfiguration("---\n" 1087 "Language: JavaScript\n" 1088 "IndentWidth: 56\n" 1089 "---\n" 1090 "IndentWidth: 78\n" 1091 "...\n", 1092 &Style), 1093 ParseError::Error); 1094 EXPECT_EQ(parseConfiguration("---\n" 1095 "Language: JavaScript\n" 1096 "IndentWidth: 56\n" 1097 "---\n" 1098 "Language: JavaScript\n" 1099 "IndentWidth: 78\n" 1100 "...\n", 1101 &Style), 1102 ParseError::Error); 1103 1104 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 1105 1106 Style.Language = FormatStyle::LK_Verilog; 1107 CHECK_PARSE("---\n" 1108 "Language: Verilog\n" 1109 "IndentWidth: 12\n" 1110 "---\n" 1111 "Language: Cpp\n" 1112 "IndentWidth: 34\n" 1113 "...\n", 1114 IndentWidth, 12u); 1115 CHECK_PARSE("---\n" 1116 "IndentWidth: 78\n" 1117 "---\n" 1118 "Language: Verilog\n" 1119 "IndentWidth: 56\n" 1120 "...\n", 1121 IndentWidth, 56u); 1122 } 1123 1124 TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) { 1125 FormatStyle Style = {}; 1126 Style.Language = FormatStyle::LK_JavaScript; 1127 Style.BreakBeforeTernaryOperators = true; 1128 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 1129 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 1130 1131 Style.BreakBeforeTernaryOperators = true; 1132 EXPECT_EQ(0, parseConfiguration("---\n" 1133 "BasedOnStyle: Google\n" 1134 "---\n" 1135 "Language: JavaScript\n" 1136 "IndentWidth: 76\n" 1137 "...\n", 1138 &Style) 1139 .value()); 1140 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 1141 EXPECT_EQ(76u, Style.IndentWidth); 1142 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 1143 } 1144 1145 TEST(ConfigParseTest, ConfigurationRoundTripTest) { 1146 FormatStyle Style = getLLVMStyle(); 1147 std::string YAML = configurationAsText(Style); 1148 FormatStyle ParsedStyle = {}; 1149 ParsedStyle.Language = FormatStyle::LK_Cpp; 1150 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 1151 EXPECT_EQ(Style, ParsedStyle); 1152 } 1153 1154 TEST(ConfigParseTest, GetStyleWithEmptyFileName) { 1155 llvm::vfs::InMemoryFileSystem FS; 1156 auto Style1 = getStyle("file", "", "Google", "", &FS); 1157 ASSERT_TRUE((bool)Style1); 1158 ASSERT_EQ(*Style1, getGoogleStyle()); 1159 } 1160 1161 TEST(ConfigParseTest, GetStyleOfFile) { 1162 llvm::vfs::InMemoryFileSystem FS; 1163 // Test 1: format file in the same directory. 1164 ASSERT_TRUE( 1165 FS.addFile("/a/.clang-format", 0, 1166 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 1167 ASSERT_TRUE( 1168 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 1169 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 1170 ASSERT_TRUE((bool)Style1); 1171 ASSERT_EQ(*Style1, getLLVMStyle()); 1172 1173 // Test 2.1: fallback to default. 1174 ASSERT_TRUE( 1175 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 1176 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 1177 ASSERT_TRUE((bool)Style2); 1178 ASSERT_EQ(*Style2, getMozillaStyle()); 1179 1180 // Test 2.2: no format on 'none' fallback style. 1181 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 1182 ASSERT_TRUE((bool)Style2); 1183 ASSERT_EQ(*Style2, getNoStyle()); 1184 1185 // Test 2.3: format if config is found with no based style while fallback is 1186 // 'none'. 1187 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 1188 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 1189 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 1190 ASSERT_TRUE((bool)Style2); 1191 ASSERT_EQ(*Style2, getLLVMStyle()); 1192 1193 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 1194 Style2 = getStyle("{}", "a.h", "none", "", &FS); 1195 ASSERT_TRUE((bool)Style2); 1196 ASSERT_EQ(*Style2, getLLVMStyle()); 1197 1198 // Test 3: format file in parent directory. 1199 ASSERT_TRUE( 1200 FS.addFile("/c/.clang-format", 0, 1201 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 1202 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 1203 llvm::MemoryBuffer::getMemBuffer("int i;"))); 1204 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 1205 ASSERT_TRUE((bool)Style3); 1206 ASSERT_EQ(*Style3, getGoogleStyle()); 1207 1208 // Test 4: error on invalid fallback style 1209 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 1210 ASSERT_FALSE((bool)Style4); 1211 llvm::consumeError(Style4.takeError()); 1212 1213 // Test 5: error on invalid yaml on command line 1214 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 1215 ASSERT_FALSE((bool)Style5); 1216 llvm::consumeError(Style5.takeError()); 1217 1218 // Test 6: error on invalid style 1219 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 1220 ASSERT_FALSE((bool)Style6); 1221 llvm::consumeError(Style6.takeError()); 1222 1223 // Test 7: found config file, error on parsing it 1224 ASSERT_TRUE( 1225 FS.addFile("/d/.clang-format", 0, 1226 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 1227 "InvalidKey: InvalidValue"))); 1228 ASSERT_TRUE( 1229 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 1230 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 1231 ASSERT_FALSE((bool)Style7a); 1232 llvm::consumeError(Style7a.takeError()); 1233 1234 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 1235 ASSERT_TRUE((bool)Style7b); 1236 1237 // Test 8: inferred per-language defaults apply. 1238 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 1239 ASSERT_TRUE((bool)StyleTd); 1240 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 1241 1242 // Test 9.1.1: overwriting a file style, when no parent file exists with no 1243 // fallback style. 1244 ASSERT_TRUE(FS.addFile( 1245 "/e/sub/.clang-format", 0, 1246 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 1247 "ColumnLimit: 20"))); 1248 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 1249 llvm::MemoryBuffer::getMemBuffer("int i;"))); 1250 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 1251 ASSERT_TRUE(static_cast<bool>(Style9)); 1252 ASSERT_EQ(*Style9, [] { 1253 auto Style = getNoStyle(); 1254 Style.ColumnLimit = 20; 1255 return Style; 1256 }()); 1257 1258 // Test 9.1.2: propagate more than one level with no parent file. 1259 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 1260 llvm::MemoryBuffer::getMemBuffer("int i;"))); 1261 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 1262 llvm::MemoryBuffer::getMemBuffer( 1263 "BasedOnStyle: InheritParentConfig\n" 1264 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 1265 std::vector<std::string> NonDefaultWhiteSpaceMacros = 1266 Style9->WhitespaceSensitiveMacros; 1267 NonDefaultWhiteSpaceMacros[0] = "FOO"; 1268 NonDefaultWhiteSpaceMacros[1] = "BAR"; 1269 1270 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 1271 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 1272 ASSERT_TRUE(static_cast<bool>(Style9)); 1273 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 1274 auto Style = getNoStyle(); 1275 Style.ColumnLimit = 20; 1276 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 1277 return Style; 1278 }()); 1279 1280 // Test 9.2: with LLVM fallback style 1281 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 1282 ASSERT_TRUE(static_cast<bool>(Style9)); 1283 ASSERT_EQ(*Style9, [] { 1284 auto Style = getLLVMStyle(); 1285 Style.ColumnLimit = 20; 1286 return Style; 1287 }()); 1288 1289 // Test 9.3: with a parent file 1290 ASSERT_TRUE( 1291 FS.addFile("/e/.clang-format", 0, 1292 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 1293 "UseTab: Always"))); 1294 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 1295 ASSERT_TRUE(static_cast<bool>(Style9)); 1296 ASSERT_EQ(*Style9, [] { 1297 auto Style = getGoogleStyle(); 1298 Style.ColumnLimit = 20; 1299 Style.UseTab = FormatStyle::UT_Always; 1300 return Style; 1301 }()); 1302 1303 // Test 9.4: propagate more than one level with a parent file. 1304 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 1305 auto Style = getGoogleStyle(); 1306 Style.ColumnLimit = 20; 1307 Style.UseTab = FormatStyle::UT_Always; 1308 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 1309 return Style; 1310 }(); 1311 1312 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 1313 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 1314 ASSERT_TRUE(static_cast<bool>(Style9)); 1315 ASSERT_EQ(*Style9, SubSubStyle); 1316 1317 // Test 9.5: use InheritParentConfig as style name 1318 Style9 = 1319 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 1320 ASSERT_TRUE(static_cast<bool>(Style9)); 1321 ASSERT_EQ(*Style9, SubSubStyle); 1322 1323 // Test 9.6: use command line style with inheritance 1324 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", 1325 "/e/sub/sub/code.cpp", "none", "", &FS); 1326 ASSERT_TRUE(static_cast<bool>(Style9)); 1327 ASSERT_EQ(*Style9, SubSubStyle); 1328 1329 // Test 9.7: use command line style with inheritance and own config 1330 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 1331 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 1332 "/e/sub/code.cpp", "none", "", &FS); 1333 ASSERT_TRUE(static_cast<bool>(Style9)); 1334 ASSERT_EQ(*Style9, SubSubStyle); 1335 1336 // Test 9.8: use inheritance from a file without BasedOnStyle 1337 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 1338 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 1339 ASSERT_TRUE( 1340 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 1341 llvm::MemoryBuffer::getMemBuffer( 1342 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 1343 // Make sure we do not use the fallback style 1344 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 1345 ASSERT_TRUE(static_cast<bool>(Style9)); 1346 ASSERT_EQ(*Style9, [] { 1347 auto Style = getLLVMStyle(); 1348 Style.ColumnLimit = 123; 1349 return Style; 1350 }()); 1351 1352 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 1353 ASSERT_TRUE(static_cast<bool>(Style9)); 1354 ASSERT_EQ(*Style9, [] { 1355 auto Style = getLLVMStyle(); 1356 Style.ColumnLimit = 123; 1357 Style.IndentWidth = 7; 1358 return Style; 1359 }()); 1360 1361 // Test 9.9: use inheritance from a specific config file. 1362 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 1363 "none", "", &FS); 1364 ASSERT_TRUE(static_cast<bool>(Style9)); 1365 ASSERT_EQ(*Style9, SubSubStyle); 1366 } 1367 1368 TEST(ConfigParseTest, GetStyleOfSpecificFile) { 1369 llvm::vfs::InMemoryFileSystem FS; 1370 // Specify absolute path to a format file in a parent directory. 1371 ASSERT_TRUE( 1372 FS.addFile("/e/.clang-format", 0, 1373 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 1374 ASSERT_TRUE( 1375 FS.addFile("/e/explicit.clang-format", 0, 1376 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 1377 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 1378 llvm::MemoryBuffer::getMemBuffer("int i;"))); 1379 auto Style = getStyle("file:/e/explicit.clang-format", 1380 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 1381 ASSERT_TRUE(static_cast<bool>(Style)); 1382 ASSERT_EQ(*Style, getGoogleStyle()); 1383 1384 // Specify relative path to a format file. 1385 ASSERT_TRUE( 1386 FS.addFile("../../e/explicit.clang-format", 0, 1387 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 1388 Style = getStyle("file:../../e/explicit.clang-format", 1389 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 1390 ASSERT_TRUE(static_cast<bool>(Style)); 1391 ASSERT_EQ(*Style, getGoogleStyle()); 1392 1393 // Specify path to a format file that does not exist. 1394 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 1395 "LLVM", "", &FS); 1396 ASSERT_FALSE(static_cast<bool>(Style)); 1397 llvm::consumeError(Style.takeError()); 1398 1399 // Specify path to a file on the filesystem. 1400 SmallString<128> FormatFilePath; 1401 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 1402 "FormatFileTest", "tpl", FormatFilePath); 1403 EXPECT_FALSE((bool)ECF); 1404 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 1405 EXPECT_FALSE((bool)ECF); 1406 FormatFileTest << "BasedOnStyle: Google\n"; 1407 FormatFileTest.close(); 1408 1409 SmallString<128> TestFilePath; 1410 std::error_code ECT = 1411 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 1412 EXPECT_FALSE((bool)ECT); 1413 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 1414 CodeFileTest << "int i;\n"; 1415 CodeFileTest.close(); 1416 1417 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 1418 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 1419 1420 llvm::sys::fs::remove(FormatFilePath.c_str()); 1421 llvm::sys::fs::remove(TestFilePath.c_str()); 1422 ASSERT_TRUE(static_cast<bool>(Style)); 1423 ASSERT_EQ(*Style, getGoogleStyle()); 1424 } 1425 1426 } // namespace 1427 } // namespace format 1428 } // namespace clang 1429