1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyIncompleteFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 EXPECT_EQ(Code.str(), 88 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 89 } 90 91 void verifyGoogleFormat(llvm::StringRef Code) { 92 verifyFormat(Code, getGoogleStyle()); 93 } 94 95 void verifyIndependentOfContext(llvm::StringRef text) { 96 verifyFormat(text); 97 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 98 } 99 100 /// \brief Verify that clang-format does not crash on the given input. 101 void verifyNoCrash(llvm::StringRef Code, 102 const FormatStyle &Style = getLLVMStyle()) { 103 format(Code, Style, SC_DoNotCheck); 104 } 105 106 int ReplacementCount; 107 }; 108 109 TEST_F(FormatTest, MessUp) { 110 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 111 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 112 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 113 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 114 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 115 } 116 117 //===----------------------------------------------------------------------===// 118 // Basic function tests. 119 //===----------------------------------------------------------------------===// 120 121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 122 EXPECT_EQ(";", format(";")); 123 } 124 125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 126 EXPECT_EQ("int i;", format(" int i;")); 127 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 128 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 129 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 130 } 131 132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 133 EXPECT_EQ("int i;", format("int\ni;")); 134 } 135 136 TEST_F(FormatTest, FormatsNestedBlockStatements) { 137 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 138 } 139 140 TEST_F(FormatTest, FormatsNestedCall) { 141 verifyFormat("Method(f1, f2(f3));"); 142 verifyFormat("Method(f1(f2, f3()));"); 143 verifyFormat("Method(f1(f2, (f3())));"); 144 } 145 146 TEST_F(FormatTest, NestedNameSpecifiers) { 147 verifyFormat("vector<::Type> v;"); 148 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 149 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 150 verifyFormat("bool a = 2 < ::SomeFunction();"); 151 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 152 verifyFormat("some::string getName();"); 153 } 154 155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 156 EXPECT_EQ("if (a) {\n" 157 " f();\n" 158 "}", 159 format("if(a){f();}")); 160 EXPECT_EQ(4, ReplacementCount); 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if (a) {\n" 165 " f();\n" 166 "}")); 167 EXPECT_EQ(0, ReplacementCount); 168 EXPECT_EQ("/*\r\n" 169 "\r\n" 170 "*/\r\n", 171 format("/*\r\n" 172 "\r\n" 173 "*/\r\n")); 174 EXPECT_EQ(0, ReplacementCount); 175 } 176 177 TEST_F(FormatTest, RemovesEmptyLines) { 178 EXPECT_EQ("class C {\n" 179 " int i;\n" 180 "};", 181 format("class C {\n" 182 " int i;\n" 183 "\n" 184 "};")); 185 186 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 187 EXPECT_EQ("namespace N {\n" 188 "\n" 189 "int i;\n" 190 "}", 191 format("namespace N {\n" 192 "\n" 193 "int i;\n" 194 "}", 195 getGoogleStyle())); 196 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 format("extern /**/ \"C\" /**/ {\n" 201 "\n" 202 "int i;\n" 203 "}", 204 getGoogleStyle())); 205 206 // ...but do keep inlining and removing empty lines for non-block extern "C" 207 // functions. 208 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 209 EXPECT_EQ("extern \"C\" int f() {\n" 210 " int i = 42;\n" 211 " return i;\n" 212 "}", 213 format("extern \"C\" int f() {\n" 214 "\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 getGoogleStyle())); 219 220 // Remove empty lines at the beginning and end of blocks. 221 EXPECT_EQ("void f() {\n" 222 "\n" 223 " if (a) {\n" 224 "\n" 225 " f();\n" 226 " }\n" 227 "}", 228 format("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 "\n" 234 " }\n" 235 "\n" 236 "}", 237 getLLVMStyle())); 238 EXPECT_EQ("void f() {\n" 239 " if (a) {\n" 240 " f();\n" 241 " }\n" 242 "}", 243 format("void f() {\n" 244 "\n" 245 " if (a) {\n" 246 "\n" 247 " f();\n" 248 "\n" 249 " }\n" 250 "\n" 251 "}", 252 getGoogleStyle())); 253 254 // Don't remove empty lines in more complex control statements. 255 EXPECT_EQ("void f() {\n" 256 " if (a) {\n" 257 " f();\n" 258 "\n" 259 " } else if (b) {\n" 260 " f();\n" 261 " }\n" 262 "}", 263 format("void f() {\n" 264 " if (a) {\n" 265 " f();\n" 266 "\n" 267 " } else if (b) {\n" 268 " f();\n" 269 "\n" 270 " }\n" 271 "\n" 272 "}")); 273 274 // FIXME: This is slightly inconsistent. 275 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 276 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 277 EXPECT_EQ("namespace {\n" 278 "int i;\n" 279 "}", 280 format("namespace {\n" 281 "int i;\n" 282 "\n" 283 "}", LLVMWithNoNamespaceFix)); 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "}", 287 format("namespace {\n" 288 "int i;\n" 289 "\n" 290 "}")); 291 EXPECT_EQ("namespace {\n" 292 "int i;\n" 293 "\n" 294 "} // namespace", 295 format("namespace {\n" 296 "int i;\n" 297 "\n" 298 "} // namespace")); 299 300 FormatStyle Style = getLLVMStyle(); 301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 302 Style.MaxEmptyLinesToKeep = 2; 303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 304 Style.BraceWrapping.AfterClass = true; 305 Style.BraceWrapping.AfterFunction = true; 306 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 307 308 EXPECT_EQ("class Foo\n" 309 "{\n" 310 " Foo() {}\n" 311 "\n" 312 " void funk() {}\n" 313 "};", 314 format("class Foo\n" 315 "{\n" 316 " Foo()\n" 317 " {\n" 318 " }\n" 319 "\n" 320 " void funk() {}\n" 321 "};", 322 Style)); 323 } 324 325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 326 verifyFormat("x = (a) and (b);"); 327 verifyFormat("x = (a) or (b);"); 328 verifyFormat("x = (a) bitand (b);"); 329 verifyFormat("x = (a) bitor (b);"); 330 verifyFormat("x = (a) not_eq (b);"); 331 verifyFormat("x = (a) and_eq (b);"); 332 verifyFormat("x = (a) or_eq (b);"); 333 verifyFormat("x = (a) xor (b);"); 334 } 335 336 //===----------------------------------------------------------------------===// 337 // Tests for control statements. 338 //===----------------------------------------------------------------------===// 339 340 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 341 verifyFormat("if (true)\n f();\ng();"); 342 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 343 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 344 verifyFormat("if constexpr (true)\n" 345 " f();\ng();"); 346 verifyFormat("if constexpr (a)\n" 347 " if constexpr (b)\n" 348 " if constexpr (c)\n" 349 " g();\n" 350 "h();"); 351 verifyFormat("if constexpr (a)\n" 352 " if constexpr (b) {\n" 353 " f();\n" 354 " }\n" 355 "g();"); 356 357 FormatStyle AllowsMergedIf = getLLVMStyle(); 358 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 359 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 360 verifyFormat("if (a)\n" 361 " // comment\n" 362 " f();", 363 AllowsMergedIf); 364 verifyFormat("{\n" 365 " if (a)\n" 366 " label:\n" 367 " f();\n" 368 "}", 369 AllowsMergedIf); 370 verifyFormat("#define A \\\n" 371 " if (a) \\\n" 372 " label: \\\n" 373 " f()", 374 AllowsMergedIf); 375 verifyFormat("if (a)\n" 376 " ;", 377 AllowsMergedIf); 378 verifyFormat("if (a)\n" 379 " if (b) return;", 380 AllowsMergedIf); 381 382 verifyFormat("if (a) // Can't merge this\n" 383 " f();\n", 384 AllowsMergedIf); 385 verifyFormat("if (a) /* still don't merge */\n" 386 " f();", 387 AllowsMergedIf); 388 verifyFormat("if (a) { // Never merge this\n" 389 " f();\n" 390 "}", 391 AllowsMergedIf); 392 verifyFormat("if (a) { /* Never merge this */\n" 393 " f();\n" 394 "}", 395 AllowsMergedIf); 396 397 AllowsMergedIf.ColumnLimit = 14; 398 verifyFormat("if (a) return;", AllowsMergedIf); 399 verifyFormat("if (aaaaaaaaa)\n" 400 " return;", 401 AllowsMergedIf); 402 403 AllowsMergedIf.ColumnLimit = 13; 404 verifyFormat("if (a)\n return;", AllowsMergedIf); 405 } 406 407 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 408 FormatStyle AllowsMergedLoops = getLLVMStyle(); 409 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 410 verifyFormat("while (true) continue;", AllowsMergedLoops); 411 verifyFormat("for (;;) continue;", AllowsMergedLoops); 412 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 413 verifyFormat("while (true)\n" 414 " ;", 415 AllowsMergedLoops); 416 verifyFormat("for (;;)\n" 417 " ;", 418 AllowsMergedLoops); 419 verifyFormat("for (;;)\n" 420 " for (;;) continue;", 421 AllowsMergedLoops); 422 verifyFormat("for (;;) // Can't merge this\n" 423 " continue;", 424 AllowsMergedLoops); 425 verifyFormat("for (;;) /* still don't merge */\n" 426 " continue;", 427 AllowsMergedLoops); 428 } 429 430 TEST_F(FormatTest, FormatShortBracedStatements) { 431 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 432 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 433 434 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 435 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 436 437 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 438 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 439 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 440 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 441 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 442 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 443 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 444 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 445 verifyFormat("if (true) { //\n" 446 " f();\n" 447 "}", 448 AllowSimpleBracedStatements); 449 verifyFormat("if (true) {\n" 450 " f();\n" 451 " f();\n" 452 "}", 453 AllowSimpleBracedStatements); 454 verifyFormat("if (true) {\n" 455 " f();\n" 456 "} else {\n" 457 " f();\n" 458 "}", 459 AllowSimpleBracedStatements); 460 461 verifyFormat("struct A2 {\n" 462 " int X;\n" 463 "};", 464 AllowSimpleBracedStatements); 465 verifyFormat("typedef struct A2 {\n" 466 " int X;\n" 467 "} A2_t;", 468 AllowSimpleBracedStatements); 469 verifyFormat("template <int> struct A2 {\n" 470 " struct B {};\n" 471 "};", 472 AllowSimpleBracedStatements); 473 474 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 475 verifyFormat("if (true) {\n" 476 " f();\n" 477 "}", 478 AllowSimpleBracedStatements); 479 verifyFormat("if (true) {\n" 480 " f();\n" 481 "} else {\n" 482 " f();\n" 483 "}", 484 AllowSimpleBracedStatements); 485 486 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 487 verifyFormat("while (true) {\n" 488 " f();\n" 489 "}", 490 AllowSimpleBracedStatements); 491 verifyFormat("for (;;) {\n" 492 " f();\n" 493 "}", 494 AllowSimpleBracedStatements); 495 } 496 497 TEST_F(FormatTest, ParseIfElse) { 498 verifyFormat("if (true)\n" 499 " if (true)\n" 500 " if (true)\n" 501 " f();\n" 502 " else\n" 503 " g();\n" 504 " else\n" 505 " h();\n" 506 "else\n" 507 " i();"); 508 verifyFormat("if (true)\n" 509 " if (true)\n" 510 " if (true) {\n" 511 " if (true)\n" 512 " f();\n" 513 " } else {\n" 514 " g();\n" 515 " }\n" 516 " else\n" 517 " h();\n" 518 "else {\n" 519 " i();\n" 520 "}"); 521 verifyFormat("if (true)\n" 522 " if constexpr (true)\n" 523 " if (true) {\n" 524 " if constexpr (true)\n" 525 " f();\n" 526 " } else {\n" 527 " g();\n" 528 " }\n" 529 " else\n" 530 " h();\n" 531 "else {\n" 532 " i();\n" 533 "}"); 534 verifyFormat("void f() {\n" 535 " if (a) {\n" 536 " } else {\n" 537 " }\n" 538 "}"); 539 } 540 541 TEST_F(FormatTest, ElseIf) { 542 verifyFormat("if (a) {\n} else if (b) {\n}"); 543 verifyFormat("if (a)\n" 544 " f();\n" 545 "else if (b)\n" 546 " g();\n" 547 "else\n" 548 " h();"); 549 verifyFormat("if constexpr (a)\n" 550 " f();\n" 551 "else if constexpr (b)\n" 552 " g();\n" 553 "else\n" 554 " h();"); 555 verifyFormat("if (a) {\n" 556 " f();\n" 557 "}\n" 558 "// or else ..\n" 559 "else {\n" 560 " g()\n" 561 "}"); 562 563 verifyFormat("if (a) {\n" 564 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 566 "}"); 567 verifyFormat("if (a) {\n" 568 "} else if (\n" 569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 570 "}", 571 getLLVMStyleWithColumns(62)); 572 verifyFormat("if (a) {\n" 573 "} else if constexpr (\n" 574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 575 "}", 576 getLLVMStyleWithColumns(62)); 577 } 578 579 TEST_F(FormatTest, FormatsForLoop) { 580 verifyFormat( 581 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 582 " ++VeryVeryLongLoopVariable)\n" 583 " ;"); 584 verifyFormat("for (;;)\n" 585 " f();"); 586 verifyFormat("for (;;) {\n}"); 587 verifyFormat("for (;;) {\n" 588 " f();\n" 589 "}"); 590 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 591 592 verifyFormat( 593 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 594 " E = UnwrappedLines.end();\n" 595 " I != E; ++I) {\n}"); 596 597 verifyFormat( 598 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 599 " ++IIIII) {\n}"); 600 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 601 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 602 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 603 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 604 " I = FD->getDeclsInPrototypeScope().begin(),\n" 605 " E = FD->getDeclsInPrototypeScope().end();\n" 606 " I != E; ++I) {\n}"); 607 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 608 " I = Container.begin(),\n" 609 " E = Container.end();\n" 610 " I != E; ++I) {\n}", 611 getLLVMStyleWithColumns(76)); 612 613 verifyFormat( 614 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 618 " ++aaaaaaaaaaa) {\n}"); 619 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 620 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 621 " ++i) {\n}"); 622 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 623 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 624 "}"); 625 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 626 " aaaaaaaaaa);\n" 627 " iter; ++iter) {\n" 628 "}"); 629 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 631 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 632 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 633 634 FormatStyle NoBinPacking = getLLVMStyle(); 635 NoBinPacking.BinPackParameters = false; 636 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 637 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 638 " aaaaaaaaaaaaaaaa,\n" 639 " aaaaaaaaaaaaaaaa,\n" 640 " aaaaaaaaaaaaaaaa);\n" 641 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 642 "}", 643 NoBinPacking); 644 verifyFormat( 645 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 646 " E = UnwrappedLines.end();\n" 647 " I != E;\n" 648 " ++I) {\n}", 649 NoBinPacking); 650 } 651 652 TEST_F(FormatTest, RangeBasedForLoops) { 653 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 655 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 656 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 657 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 659 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 660 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 661 } 662 663 TEST_F(FormatTest, ForEachLoops) { 664 verifyFormat("void f() {\n" 665 " foreach (Item *item, itemlist) {}\n" 666 " Q_FOREACH (Item *item, itemlist) {}\n" 667 " BOOST_FOREACH (Item *item, itemlist) {}\n" 668 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 669 "}"); 670 671 // As function-like macros. 672 verifyFormat("#define foreach(x, y)\n" 673 "#define Q_FOREACH(x, y)\n" 674 "#define BOOST_FOREACH(x, y)\n" 675 "#define UNKNOWN_FOREACH(x, y)\n"); 676 677 // Not as function-like macros. 678 verifyFormat("#define foreach (x, y)\n" 679 "#define Q_FOREACH (x, y)\n" 680 "#define BOOST_FOREACH (x, y)\n" 681 "#define UNKNOWN_FOREACH (x, y)\n"); 682 } 683 684 TEST_F(FormatTest, FormatsWhileLoop) { 685 verifyFormat("while (true) {\n}"); 686 verifyFormat("while (true)\n" 687 " f();"); 688 verifyFormat("while () {\n}"); 689 verifyFormat("while () {\n" 690 " f();\n" 691 "}"); 692 } 693 694 TEST_F(FormatTest, FormatsDoWhile) { 695 verifyFormat("do {\n" 696 " do_something();\n" 697 "} while (something());"); 698 verifyFormat("do\n" 699 " do_something();\n" 700 "while (something());"); 701 } 702 703 TEST_F(FormatTest, FormatsSwitchStatement) { 704 verifyFormat("switch (x) {\n" 705 "case 1:\n" 706 " f();\n" 707 " break;\n" 708 "case kFoo:\n" 709 "case ns::kBar:\n" 710 "case kBaz:\n" 711 " break;\n" 712 "default:\n" 713 " g();\n" 714 " break;\n" 715 "}"); 716 verifyFormat("switch (x) {\n" 717 "case 1: {\n" 718 " f();\n" 719 " break;\n" 720 "}\n" 721 "case 2: {\n" 722 " break;\n" 723 "}\n" 724 "}"); 725 verifyFormat("switch (x) {\n" 726 "case 1: {\n" 727 " f();\n" 728 " {\n" 729 " g();\n" 730 " h();\n" 731 " }\n" 732 " break;\n" 733 "}\n" 734 "}"); 735 verifyFormat("switch (x) {\n" 736 "case 1: {\n" 737 " f();\n" 738 " if (foo) {\n" 739 " g();\n" 740 " h();\n" 741 " }\n" 742 " break;\n" 743 "}\n" 744 "}"); 745 verifyFormat("switch (x) {\n" 746 "case 1: {\n" 747 " f();\n" 748 " g();\n" 749 "} break;\n" 750 "}"); 751 verifyFormat("switch (test)\n" 752 " ;"); 753 verifyFormat("switch (x) {\n" 754 "default: {\n" 755 " // Do nothing.\n" 756 "}\n" 757 "}"); 758 verifyFormat("switch (x) {\n" 759 "// comment\n" 760 "// if 1, do f()\n" 761 "case 1:\n" 762 " f();\n" 763 "}"); 764 verifyFormat("switch (x) {\n" 765 "case 1:\n" 766 " // Do amazing stuff\n" 767 " {\n" 768 " f();\n" 769 " g();\n" 770 " }\n" 771 " break;\n" 772 "}"); 773 verifyFormat("#define A \\\n" 774 " switch (x) { \\\n" 775 " case a: \\\n" 776 " foo = b; \\\n" 777 " }", 778 getLLVMStyleWithColumns(20)); 779 verifyFormat("#define OPERATION_CASE(name) \\\n" 780 " case OP_name: \\\n" 781 " return operations::Operation##name\n", 782 getLLVMStyleWithColumns(40)); 783 verifyFormat("switch (x) {\n" 784 "case 1:;\n" 785 "default:;\n" 786 " int i;\n" 787 "}"); 788 789 verifyGoogleFormat("switch (x) {\n" 790 " case 1:\n" 791 " f();\n" 792 " break;\n" 793 " case kFoo:\n" 794 " case ns::kBar:\n" 795 " case kBaz:\n" 796 " break;\n" 797 " default:\n" 798 " g();\n" 799 " break;\n" 800 "}"); 801 verifyGoogleFormat("switch (x) {\n" 802 " case 1: {\n" 803 " f();\n" 804 " break;\n" 805 " }\n" 806 "}"); 807 verifyGoogleFormat("switch (test)\n" 808 " ;"); 809 810 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 811 " case OP_name: \\\n" 812 " return operations::Operation##name\n"); 813 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 814 " // Get the correction operation class.\n" 815 " switch (OpCode) {\n" 816 " CASE(Add);\n" 817 " CASE(Subtract);\n" 818 " default:\n" 819 " return operations::Unknown;\n" 820 " }\n" 821 "#undef OPERATION_CASE\n" 822 "}"); 823 verifyFormat("DEBUG({\n" 824 " switch (x) {\n" 825 " case A:\n" 826 " f();\n" 827 " break;\n" 828 " // On B:\n" 829 " case B:\n" 830 " g();\n" 831 " break;\n" 832 " }\n" 833 "});"); 834 verifyFormat("switch (a) {\n" 835 "case (b):\n" 836 " return;\n" 837 "}"); 838 839 verifyFormat("switch (a) {\n" 840 "case some_namespace::\n" 841 " some_constant:\n" 842 " return;\n" 843 "}", 844 getLLVMStyleWithColumns(34)); 845 } 846 847 TEST_F(FormatTest, CaseRanges) { 848 verifyFormat("switch (x) {\n" 849 "case 'A' ... 'Z':\n" 850 "case 1 ... 5:\n" 851 "case a ... b:\n" 852 " break;\n" 853 "}"); 854 } 855 856 TEST_F(FormatTest, ShortCaseLabels) { 857 FormatStyle Style = getLLVMStyle(); 858 Style.AllowShortCaseLabelsOnASingleLine = true; 859 verifyFormat("switch (a) {\n" 860 "case 1: x = 1; break;\n" 861 "case 2: return;\n" 862 "case 3:\n" 863 "case 4:\n" 864 "case 5: return;\n" 865 "case 6: // comment\n" 866 " return;\n" 867 "case 7:\n" 868 " // comment\n" 869 " return;\n" 870 "case 8:\n" 871 " x = 8; // comment\n" 872 " break;\n" 873 "default: y = 1; break;\n" 874 "}", 875 Style); 876 verifyFormat("switch (a) {\n" 877 "#if FOO\n" 878 "case 0: return 0;\n" 879 "#endif\n" 880 "}", 881 Style); 882 verifyFormat("switch (a) {\n" 883 "case 1: {\n" 884 "}\n" 885 "case 2: {\n" 886 " return;\n" 887 "}\n" 888 "case 3: {\n" 889 " x = 1;\n" 890 " return;\n" 891 "}\n" 892 "case 4:\n" 893 " if (x)\n" 894 " return;\n" 895 "}", 896 Style); 897 Style.ColumnLimit = 21; 898 verifyFormat("switch (a) {\n" 899 "case 1: x = 1; break;\n" 900 "case 2: return;\n" 901 "case 3:\n" 902 "case 4:\n" 903 "case 5: return;\n" 904 "default:\n" 905 " y = 1;\n" 906 " break;\n" 907 "}", 908 Style); 909 } 910 911 TEST_F(FormatTest, FormatsLabels) { 912 verifyFormat("void f() {\n" 913 " some_code();\n" 914 "test_label:\n" 915 " some_other_code();\n" 916 " {\n" 917 " some_more_code();\n" 918 " another_label:\n" 919 " some_more_code();\n" 920 " }\n" 921 "}"); 922 verifyFormat("{\n" 923 " some_code();\n" 924 "test_label:\n" 925 " some_other_code();\n" 926 "}"); 927 verifyFormat("{\n" 928 " some_code();\n" 929 "test_label:;\n" 930 " int i = 0;\n" 931 "}"); 932 } 933 934 //===----------------------------------------------------------------------===// 935 // Tests for classes, namespaces, etc. 936 //===----------------------------------------------------------------------===// 937 938 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 939 verifyFormat("class A {};"); 940 } 941 942 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 943 verifyFormat("class A {\n" 944 "public:\n" 945 "public: // comment\n" 946 "protected:\n" 947 "private:\n" 948 " void f() {}\n" 949 "};"); 950 verifyGoogleFormat("class A {\n" 951 " public:\n" 952 " protected:\n" 953 " private:\n" 954 " void f() {}\n" 955 "};"); 956 verifyFormat("class A {\n" 957 "public slots:\n" 958 " void f1() {}\n" 959 "public Q_SLOTS:\n" 960 " void f2() {}\n" 961 "protected slots:\n" 962 " void f3() {}\n" 963 "protected Q_SLOTS:\n" 964 " void f4() {}\n" 965 "private slots:\n" 966 " void f5() {}\n" 967 "private Q_SLOTS:\n" 968 " void f6() {}\n" 969 "signals:\n" 970 " void g1();\n" 971 "Q_SIGNALS:\n" 972 " void g2();\n" 973 "};"); 974 975 // Don't interpret 'signals' the wrong way. 976 verifyFormat("signals.set();"); 977 verifyFormat("for (Signals signals : f()) {\n}"); 978 verifyFormat("{\n" 979 " signals.set(); // This needs indentation.\n" 980 "}"); 981 verifyFormat("void f() {\n" 982 "label:\n" 983 " signals.baz();\n" 984 "}"); 985 } 986 987 TEST_F(FormatTest, SeparatesLogicalBlocks) { 988 EXPECT_EQ("class A {\n" 989 "public:\n" 990 " void f();\n" 991 "\n" 992 "private:\n" 993 " void g() {}\n" 994 " // test\n" 995 "protected:\n" 996 " int h;\n" 997 "};", 998 format("class A {\n" 999 "public:\n" 1000 "void f();\n" 1001 "private:\n" 1002 "void g() {}\n" 1003 "// test\n" 1004 "protected:\n" 1005 "int h;\n" 1006 "};")); 1007 EXPECT_EQ("class A {\n" 1008 "protected:\n" 1009 "public:\n" 1010 " void f();\n" 1011 "};", 1012 format("class A {\n" 1013 "protected:\n" 1014 "\n" 1015 "public:\n" 1016 "\n" 1017 " void f();\n" 1018 "};")); 1019 1020 // Even ensure proper spacing inside macros. 1021 EXPECT_EQ("#define B \\\n" 1022 " class A { \\\n" 1023 " protected: \\\n" 1024 " public: \\\n" 1025 " void f(); \\\n" 1026 " };", 1027 format("#define B \\\n" 1028 " class A { \\\n" 1029 " protected: \\\n" 1030 " \\\n" 1031 " public: \\\n" 1032 " \\\n" 1033 " void f(); \\\n" 1034 " };", 1035 getGoogleStyle())); 1036 // But don't remove empty lines after macros ending in access specifiers. 1037 EXPECT_EQ("#define A private:\n" 1038 "\n" 1039 "int i;", 1040 format("#define A private:\n" 1041 "\n" 1042 "int i;")); 1043 } 1044 1045 TEST_F(FormatTest, FormatsClasses) { 1046 verifyFormat("class A : public B {};"); 1047 verifyFormat("class A : public ::B {};"); 1048 1049 verifyFormat( 1050 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1051 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1052 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1053 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1054 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1055 verifyFormat( 1056 "class A : public B, public C, public D, public E, public F {};"); 1057 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1058 " public C,\n" 1059 " public D,\n" 1060 " public E,\n" 1061 " public F,\n" 1062 " public G {};"); 1063 1064 verifyFormat("class\n" 1065 " ReallyReallyLongClassName {\n" 1066 " int i;\n" 1067 "};", 1068 getLLVMStyleWithColumns(32)); 1069 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1070 " aaaaaaaaaaaaaaaa> {};"); 1071 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1072 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1073 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1074 verifyFormat("template <class R, class C>\n" 1075 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1076 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1077 verifyFormat("class ::A::B {};"); 1078 } 1079 1080 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1081 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1082 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1083 1084 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1085 verifyFormat("class MyClass\n" 1086 " : public X\n" 1087 " , public Y {};", 1088 StyleWithInheritanceBreak); 1089 } 1090 1091 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1092 verifyFormat("class A {\n} a, b;"); 1093 verifyFormat("struct A {\n} a, b;"); 1094 verifyFormat("union A {\n} a;"); 1095 } 1096 1097 TEST_F(FormatTest, FormatsEnum) { 1098 verifyFormat("enum {\n" 1099 " Zero,\n" 1100 " One = 1,\n" 1101 " Two = One + 1,\n" 1102 " Three = (One + Two),\n" 1103 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1104 " Five = (One, Two, Three, Four, 5)\n" 1105 "};"); 1106 verifyGoogleFormat("enum {\n" 1107 " Zero,\n" 1108 " One = 1,\n" 1109 " Two = One + 1,\n" 1110 " Three = (One + Two),\n" 1111 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1112 " Five = (One, Two, Three, Four, 5)\n" 1113 "};"); 1114 verifyFormat("enum Enum {};"); 1115 verifyFormat("enum {};"); 1116 verifyFormat("enum X E {} d;"); 1117 verifyFormat("enum __attribute__((...)) E {} d;"); 1118 verifyFormat("enum __declspec__((...)) E {} d;"); 1119 verifyFormat("enum {\n" 1120 " Bar = Foo<int, int>::value\n" 1121 "};", 1122 getLLVMStyleWithColumns(30)); 1123 1124 verifyFormat("enum ShortEnum { A, B, C };"); 1125 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1126 1127 EXPECT_EQ("enum KeepEmptyLines {\n" 1128 " ONE,\n" 1129 "\n" 1130 " TWO,\n" 1131 "\n" 1132 " THREE\n" 1133 "}", 1134 format("enum KeepEmptyLines {\n" 1135 " ONE,\n" 1136 "\n" 1137 " TWO,\n" 1138 "\n" 1139 "\n" 1140 " THREE\n" 1141 "}")); 1142 verifyFormat("enum E { // comment\n" 1143 " ONE,\n" 1144 " TWO\n" 1145 "};\n" 1146 "int i;"); 1147 // Not enums. 1148 verifyFormat("enum X f() {\n" 1149 " a();\n" 1150 " return 42;\n" 1151 "}"); 1152 verifyFormat("enum X Type::f() {\n" 1153 " a();\n" 1154 " return 42;\n" 1155 "}"); 1156 verifyFormat("enum ::X f() {\n" 1157 " a();\n" 1158 " return 42;\n" 1159 "}"); 1160 verifyFormat("enum ns::X f() {\n" 1161 " a();\n" 1162 " return 42;\n" 1163 "}"); 1164 } 1165 1166 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1167 verifyFormat("enum Type {\n" 1168 " One = 0; // These semicolons should be commas.\n" 1169 " Two = 1;\n" 1170 "};"); 1171 verifyFormat("namespace n {\n" 1172 "enum Type {\n" 1173 " One,\n" 1174 " Two, // missing };\n" 1175 " int i;\n" 1176 "}\n" 1177 "void g() {}"); 1178 } 1179 1180 TEST_F(FormatTest, FormatsEnumStruct) { 1181 verifyFormat("enum struct {\n" 1182 " Zero,\n" 1183 " One = 1,\n" 1184 " Two = One + 1,\n" 1185 " Three = (One + Two),\n" 1186 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1187 " Five = (One, Two, Three, Four, 5)\n" 1188 "};"); 1189 verifyFormat("enum struct Enum {};"); 1190 verifyFormat("enum struct {};"); 1191 verifyFormat("enum struct X E {} d;"); 1192 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1193 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1194 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1195 } 1196 1197 TEST_F(FormatTest, FormatsEnumClass) { 1198 verifyFormat("enum class {\n" 1199 " Zero,\n" 1200 " One = 1,\n" 1201 " Two = One + 1,\n" 1202 " Three = (One + Two),\n" 1203 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1204 " Five = (One, Two, Three, Four, 5)\n" 1205 "};"); 1206 verifyFormat("enum class Enum {};"); 1207 verifyFormat("enum class {};"); 1208 verifyFormat("enum class X E {} d;"); 1209 verifyFormat("enum class __attribute__((...)) E {} d;"); 1210 verifyFormat("enum class __declspec__((...)) E {} d;"); 1211 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1212 } 1213 1214 TEST_F(FormatTest, FormatsEnumTypes) { 1215 verifyFormat("enum X : int {\n" 1216 " A, // Force multiple lines.\n" 1217 " B\n" 1218 "};"); 1219 verifyFormat("enum X : int { A, B };"); 1220 verifyFormat("enum X : std::uint32_t { A, B };"); 1221 } 1222 1223 TEST_F(FormatTest, FormatsNSEnums) { 1224 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1225 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1226 " // Information about someDecentlyLongValue.\n" 1227 " someDecentlyLongValue,\n" 1228 " // Information about anotherDecentlyLongValue.\n" 1229 " anotherDecentlyLongValue,\n" 1230 " // Information about aThirdDecentlyLongValue.\n" 1231 " aThirdDecentlyLongValue\n" 1232 "};"); 1233 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1234 " a = 1,\n" 1235 " b = 2,\n" 1236 " c = 3,\n" 1237 "};"); 1238 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1239 " a = 1,\n" 1240 " b = 2,\n" 1241 " c = 3,\n" 1242 "};"); 1243 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1244 " a = 1,\n" 1245 " b = 2,\n" 1246 " c = 3,\n" 1247 "};"); 1248 } 1249 1250 TEST_F(FormatTest, FormatsBitfields) { 1251 verifyFormat("struct Bitfields {\n" 1252 " unsigned sClass : 8;\n" 1253 " unsigned ValueKind : 2;\n" 1254 "};"); 1255 verifyFormat("struct A {\n" 1256 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1257 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1258 "};"); 1259 verifyFormat("struct MyStruct {\n" 1260 " uchar data;\n" 1261 " uchar : 8;\n" 1262 " uchar : 8;\n" 1263 " uchar other;\n" 1264 "};"); 1265 } 1266 1267 TEST_F(FormatTest, FormatsNamespaces) { 1268 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1269 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1270 1271 verifyFormat("namespace some_namespace {\n" 1272 "class A {};\n" 1273 "void f() { f(); }\n" 1274 "}", 1275 LLVMWithNoNamespaceFix); 1276 verifyFormat("namespace {\n" 1277 "class A {};\n" 1278 "void f() { f(); }\n" 1279 "}", 1280 LLVMWithNoNamespaceFix); 1281 verifyFormat("inline namespace X {\n" 1282 "class A {};\n" 1283 "void f() { f(); }\n" 1284 "}", 1285 LLVMWithNoNamespaceFix); 1286 verifyFormat("using namespace some_namespace;\n" 1287 "class A {};\n" 1288 "void f() { f(); }", 1289 LLVMWithNoNamespaceFix); 1290 1291 // This code is more common than we thought; if we 1292 // layout this correctly the semicolon will go into 1293 // its own line, which is undesirable. 1294 verifyFormat("namespace {};", 1295 LLVMWithNoNamespaceFix); 1296 verifyFormat("namespace {\n" 1297 "class A {};\n" 1298 "};", 1299 LLVMWithNoNamespaceFix); 1300 1301 verifyFormat("namespace {\n" 1302 "int SomeVariable = 0; // comment\n" 1303 "} // namespace", 1304 LLVMWithNoNamespaceFix); 1305 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1306 "#define HEADER_GUARD\n" 1307 "namespace my_namespace {\n" 1308 "int i;\n" 1309 "} // my_namespace\n" 1310 "#endif // HEADER_GUARD", 1311 format("#ifndef HEADER_GUARD\n" 1312 " #define HEADER_GUARD\n" 1313 " namespace my_namespace {\n" 1314 "int i;\n" 1315 "} // my_namespace\n" 1316 "#endif // HEADER_GUARD", 1317 LLVMWithNoNamespaceFix)); 1318 1319 EXPECT_EQ("namespace A::B {\n" 1320 "class C {};\n" 1321 "}", 1322 format("namespace A::B {\n" 1323 "class C {};\n" 1324 "}", 1325 LLVMWithNoNamespaceFix)); 1326 1327 FormatStyle Style = getLLVMStyle(); 1328 Style.NamespaceIndentation = FormatStyle::NI_All; 1329 EXPECT_EQ("namespace out {\n" 1330 " int i;\n" 1331 " namespace in {\n" 1332 " int i;\n" 1333 " } // namespace in\n" 1334 "} // namespace out", 1335 format("namespace out {\n" 1336 "int i;\n" 1337 "namespace in {\n" 1338 "int i;\n" 1339 "} // namespace in\n" 1340 "} // namespace out", 1341 Style)); 1342 1343 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1344 EXPECT_EQ("namespace out {\n" 1345 "int i;\n" 1346 "namespace in {\n" 1347 " int i;\n" 1348 "} // namespace in\n" 1349 "} // namespace out", 1350 format("namespace out {\n" 1351 "int i;\n" 1352 "namespace in {\n" 1353 "int i;\n" 1354 "} // namespace in\n" 1355 "} // namespace out", 1356 Style)); 1357 } 1358 1359 TEST_F(FormatTest, FormatsCompactNamespaces) { 1360 FormatStyle Style = getLLVMStyle(); 1361 Style.CompactNamespaces = true; 1362 1363 verifyFormat("namespace A { namespace B {\n" 1364 "}} // namespace A::B", 1365 Style); 1366 1367 EXPECT_EQ("namespace out { namespace in {\n" 1368 "}} // namespace out::in", 1369 format("namespace out {\n" 1370 "namespace in {\n" 1371 "} // namespace in\n" 1372 "} // namespace out", 1373 Style)); 1374 1375 // Only namespaces which have both consecutive opening and end get compacted 1376 EXPECT_EQ("namespace out {\n" 1377 "namespace in1 {\n" 1378 "} // namespace in1\n" 1379 "namespace in2 {\n" 1380 "} // namespace in2\n" 1381 "} // namespace out", 1382 format("namespace out {\n" 1383 "namespace in1 {\n" 1384 "} // namespace in1\n" 1385 "namespace in2 {\n" 1386 "} // namespace in2\n" 1387 "} // namespace out", 1388 Style)); 1389 1390 EXPECT_EQ("namespace out {\n" 1391 "int i;\n" 1392 "namespace in {\n" 1393 "int j;\n" 1394 "} // namespace in\n" 1395 "int k;\n" 1396 "} // namespace out", 1397 format("namespace out { int i;\n" 1398 "namespace in { int j; } // namespace in\n" 1399 "int k; } // namespace out", 1400 Style)); 1401 1402 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1403 "}}} // namespace A::B::C\n", 1404 format("namespace A { namespace B {\n" 1405 "namespace C {\n" 1406 "}} // namespace B::C\n" 1407 "} // namespace A\n", 1408 Style)); 1409 1410 Style.ColumnLimit = 40; 1411 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1412 "namespace bbbbbbbbbb {\n" 1413 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1414 format("namespace aaaaaaaaaa {\n" 1415 "namespace bbbbbbbbbb {\n" 1416 "} // namespace bbbbbbbbbb\n" 1417 "} // namespace aaaaaaaaaa", 1418 Style)); 1419 1420 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1421 "namespace cccccc {\n" 1422 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1423 format("namespace aaaaaa {\n" 1424 "namespace bbbbbb {\n" 1425 "namespace cccccc {\n" 1426 "} // namespace cccccc\n" 1427 "} // namespace bbbbbb\n" 1428 "} // namespace aaaaaa", 1429 Style)); 1430 Style.ColumnLimit = 80; 1431 1432 // Extra semicolon after 'inner' closing brace prevents merging 1433 EXPECT_EQ("namespace out { namespace in {\n" 1434 "}; } // namespace out::in", 1435 format("namespace out {\n" 1436 "namespace in {\n" 1437 "}; // namespace in\n" 1438 "} // namespace out", 1439 Style)); 1440 1441 // Extra semicolon after 'outer' closing brace is conserved 1442 EXPECT_EQ("namespace out { namespace in {\n" 1443 "}}; // namespace out::in", 1444 format("namespace out {\n" 1445 "namespace in {\n" 1446 "} // namespace in\n" 1447 "}; // namespace out", 1448 Style)); 1449 1450 Style.NamespaceIndentation = FormatStyle::NI_All; 1451 EXPECT_EQ("namespace out { namespace in {\n" 1452 " int i;\n" 1453 "}} // namespace out::in", 1454 format("namespace out {\n" 1455 "namespace in {\n" 1456 "int i;\n" 1457 "} // namespace in\n" 1458 "} // namespace out", 1459 Style)); 1460 EXPECT_EQ("namespace out { namespace mid {\n" 1461 " namespace in {\n" 1462 " int j;\n" 1463 " } // namespace in\n" 1464 " int k;\n" 1465 "}} // namespace out::mid", 1466 format("namespace out { namespace mid {\n" 1467 "namespace in { int j; } // namespace in\n" 1468 "int k; }} // namespace out::mid", 1469 Style)); 1470 1471 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1472 EXPECT_EQ("namespace out { namespace in {\n" 1473 " int i;\n" 1474 "}} // namespace out::in", 1475 format("namespace out {\n" 1476 "namespace in {\n" 1477 "int i;\n" 1478 "} // namespace in\n" 1479 "} // namespace out", 1480 Style)); 1481 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1482 " int i;\n" 1483 "}}} // namespace out::mid::in", 1484 format("namespace out {\n" 1485 "namespace mid {\n" 1486 "namespace in {\n" 1487 "int i;\n" 1488 "} // namespace in\n" 1489 "} // namespace mid\n" 1490 "} // namespace out", 1491 Style)); 1492 } 1493 1494 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1495 1496 TEST_F(FormatTest, FormatsInlineASM) { 1497 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1498 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1499 verifyFormat( 1500 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1501 " \"cpuid\\n\\t\"\n" 1502 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1503 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1504 " : \"a\"(value));"); 1505 EXPECT_EQ( 1506 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1507 " __asm {\n" 1508 " mov edx,[that] // vtable in edx\n" 1509 " mov eax,methodIndex\n" 1510 " call [edx][eax*4] // stdcall\n" 1511 " }\n" 1512 "}", 1513 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1514 " __asm {\n" 1515 " mov edx,[that] // vtable in edx\n" 1516 " mov eax,methodIndex\n" 1517 " call [edx][eax*4] // stdcall\n" 1518 " }\n" 1519 "}")); 1520 EXPECT_EQ("_asm {\n" 1521 " xor eax, eax;\n" 1522 " cpuid;\n" 1523 "}", 1524 format("_asm {\n" 1525 " xor eax, eax;\n" 1526 " cpuid;\n" 1527 "}")); 1528 verifyFormat("void function() {\n" 1529 " // comment\n" 1530 " asm(\"\");\n" 1531 "}"); 1532 EXPECT_EQ("__asm {\n" 1533 "}\n" 1534 "int i;", 1535 format("__asm {\n" 1536 "}\n" 1537 "int i;")); 1538 } 1539 1540 TEST_F(FormatTest, FormatTryCatch) { 1541 verifyFormat("try {\n" 1542 " throw a * b;\n" 1543 "} catch (int a) {\n" 1544 " // Do nothing.\n" 1545 "} catch (...) {\n" 1546 " exit(42);\n" 1547 "}"); 1548 1549 // Function-level try statements. 1550 verifyFormat("int f() try { return 4; } catch (...) {\n" 1551 " return 5;\n" 1552 "}"); 1553 verifyFormat("class A {\n" 1554 " int a;\n" 1555 " A() try : a(0) {\n" 1556 " } catch (...) {\n" 1557 " throw;\n" 1558 " }\n" 1559 "};\n"); 1560 1561 // Incomplete try-catch blocks. 1562 verifyIncompleteFormat("try {} catch ("); 1563 } 1564 1565 TEST_F(FormatTest, FormatSEHTryCatch) { 1566 verifyFormat("__try {\n" 1567 " int a = b * c;\n" 1568 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1569 " // Do nothing.\n" 1570 "}"); 1571 1572 verifyFormat("__try {\n" 1573 " int a = b * c;\n" 1574 "} __finally {\n" 1575 " // Do nothing.\n" 1576 "}"); 1577 1578 verifyFormat("DEBUG({\n" 1579 " __try {\n" 1580 " } __finally {\n" 1581 " }\n" 1582 "});\n"); 1583 } 1584 1585 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1586 verifyFormat("try {\n" 1587 " f();\n" 1588 "} catch {\n" 1589 " g();\n" 1590 "}"); 1591 verifyFormat("try {\n" 1592 " f();\n" 1593 "} catch (A a) MACRO(x) {\n" 1594 " g();\n" 1595 "} catch (B b) MACRO(x) {\n" 1596 " g();\n" 1597 "}"); 1598 } 1599 1600 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1601 FormatStyle Style = getLLVMStyle(); 1602 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1603 FormatStyle::BS_WebKit}) { 1604 Style.BreakBeforeBraces = BraceStyle; 1605 verifyFormat("try {\n" 1606 " // something\n" 1607 "} catch (...) {\n" 1608 " // something\n" 1609 "}", 1610 Style); 1611 } 1612 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1613 verifyFormat("try {\n" 1614 " // something\n" 1615 "}\n" 1616 "catch (...) {\n" 1617 " // something\n" 1618 "}", 1619 Style); 1620 verifyFormat("__try {\n" 1621 " // something\n" 1622 "}\n" 1623 "__finally {\n" 1624 " // something\n" 1625 "}", 1626 Style); 1627 verifyFormat("@try {\n" 1628 " // something\n" 1629 "}\n" 1630 "@finally {\n" 1631 " // something\n" 1632 "}", 1633 Style); 1634 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1635 verifyFormat("try\n" 1636 "{\n" 1637 " // something\n" 1638 "}\n" 1639 "catch (...)\n" 1640 "{\n" 1641 " // something\n" 1642 "}", 1643 Style); 1644 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1645 verifyFormat("try\n" 1646 " {\n" 1647 " // something\n" 1648 " }\n" 1649 "catch (...)\n" 1650 " {\n" 1651 " // something\n" 1652 " }", 1653 Style); 1654 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1655 Style.BraceWrapping.BeforeCatch = true; 1656 verifyFormat("try {\n" 1657 " // something\n" 1658 "}\n" 1659 "catch (...) {\n" 1660 " // something\n" 1661 "}", 1662 Style); 1663 } 1664 1665 TEST_F(FormatTest, StaticInitializers) { 1666 verifyFormat("static SomeClass SC = {1, 'a'};"); 1667 1668 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1669 " 100000000, " 1670 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1671 1672 // Here, everything other than the "}" would fit on a line. 1673 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1674 " 10000000000000000000000000};"); 1675 EXPECT_EQ("S s = {a,\n" 1676 "\n" 1677 " b};", 1678 format("S s = {\n" 1679 " a,\n" 1680 "\n" 1681 " b\n" 1682 "};")); 1683 1684 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1685 // line. However, the formatting looks a bit off and this probably doesn't 1686 // happen often in practice. 1687 verifyFormat("static int Variable[1] = {\n" 1688 " {1000000000000000000000000000000000000}};", 1689 getLLVMStyleWithColumns(40)); 1690 } 1691 1692 TEST_F(FormatTest, DesignatedInitializers) { 1693 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1694 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1695 " .bbbbbbbbbb = 2,\n" 1696 " .cccccccccc = 3,\n" 1697 " .dddddddddd = 4,\n" 1698 " .eeeeeeeeee = 5};"); 1699 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1700 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1701 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1702 " .ccccccccccccccccccccccccccc = 3,\n" 1703 " .ddddddddddddddddddddddddddd = 4,\n" 1704 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1705 1706 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1707 1708 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1709 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1710 " [2] = bbbbbbbbbb,\n" 1711 " [3] = cccccccccc,\n" 1712 " [4] = dddddddddd,\n" 1713 " [5] = eeeeeeeeee};"); 1714 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1715 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1716 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1717 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1718 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1719 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1720 } 1721 1722 TEST_F(FormatTest, NestedStaticInitializers) { 1723 verifyFormat("static A x = {{{}}};\n"); 1724 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1725 " {init1, init2, init3, init4}}};", 1726 getLLVMStyleWithColumns(50)); 1727 1728 verifyFormat("somes Status::global_reps[3] = {\n" 1729 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1730 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1731 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1732 getLLVMStyleWithColumns(60)); 1733 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1734 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1735 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1736 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 1737 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1738 " {rect.fRight - rect.fLeft, rect.fBottom - " 1739 "rect.fTop}};"); 1740 1741 verifyFormat( 1742 "SomeArrayOfSomeType a = {\n" 1743 " {{1, 2, 3},\n" 1744 " {1, 2, 3},\n" 1745 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1746 " 333333333333333333333333333333},\n" 1747 " {1, 2, 3},\n" 1748 " {1, 2, 3}}};"); 1749 verifyFormat( 1750 "SomeArrayOfSomeType a = {\n" 1751 " {{1, 2, 3}},\n" 1752 " {{1, 2, 3}},\n" 1753 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 1754 " 333333333333333333333333333333}},\n" 1755 " {{1, 2, 3}},\n" 1756 " {{1, 2, 3}}};"); 1757 1758 verifyFormat("struct {\n" 1759 " unsigned bit;\n" 1760 " const char *const name;\n" 1761 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 1762 " {kOsWin, \"Windows\"},\n" 1763 " {kOsLinux, \"Linux\"},\n" 1764 " {kOsCrOS, \"Chrome OS\"}};"); 1765 verifyFormat("struct {\n" 1766 " unsigned bit;\n" 1767 " const char *const name;\n" 1768 "} kBitsToOs[] = {\n" 1769 " {kOsMac, \"Mac\"},\n" 1770 " {kOsWin, \"Windows\"},\n" 1771 " {kOsLinux, \"Linux\"},\n" 1772 " {kOsCrOS, \"Chrome OS\"},\n" 1773 "};"); 1774 } 1775 1776 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1777 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1778 " \\\n" 1779 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1780 } 1781 1782 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1783 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 1784 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1785 1786 // Do break defaulted and deleted functions. 1787 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1788 " default;", 1789 getLLVMStyleWithColumns(40)); 1790 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1791 " delete;", 1792 getLLVMStyleWithColumns(40)); 1793 } 1794 1795 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 1796 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 1797 getLLVMStyleWithColumns(40)); 1798 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1799 getLLVMStyleWithColumns(40)); 1800 EXPECT_EQ("#define Q \\\n" 1801 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 1802 " \"aaaaaaaa.cpp\"", 1803 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1804 getLLVMStyleWithColumns(40))); 1805 } 1806 1807 TEST_F(FormatTest, UnderstandsLinePPDirective) { 1808 EXPECT_EQ("# 123 \"A string literal\"", 1809 format(" # 123 \"A string literal\"")); 1810 } 1811 1812 TEST_F(FormatTest, LayoutUnknownPPDirective) { 1813 EXPECT_EQ("#;", format("#;")); 1814 verifyFormat("#\n;\n;\n;"); 1815 } 1816 1817 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1818 EXPECT_EQ("#line 42 \"test\"\n", 1819 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1820 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1821 getLLVMStyleWithColumns(12))); 1822 } 1823 1824 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1825 EXPECT_EQ("#line 42 \"test\"", 1826 format("# \\\n line \\\n 42 \\\n \"test\"")); 1827 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1828 } 1829 1830 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 1831 verifyFormat("#define A \\x20"); 1832 verifyFormat("#define A \\ x20"); 1833 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 1834 verifyFormat("#define A ''"); 1835 verifyFormat("#define A ''qqq"); 1836 verifyFormat("#define A `qqq"); 1837 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 1838 EXPECT_EQ("const char *c = STRINGIFY(\n" 1839 "\\na : b);", 1840 format("const char * c = STRINGIFY(\n" 1841 "\\na : b);")); 1842 1843 verifyFormat("a\r\\"); 1844 verifyFormat("a\v\\"); 1845 verifyFormat("a\f\\"); 1846 } 1847 1848 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1849 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1850 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1851 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1852 // FIXME: We never break before the macro name. 1853 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 1854 1855 verifyFormat("#define A A\n#define A A"); 1856 verifyFormat("#define A(X) A\n#define A A"); 1857 1858 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1859 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1860 } 1861 1862 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1863 EXPECT_EQ("// somecomment\n" 1864 "#include \"a.h\"\n" 1865 "#define A( \\\n" 1866 " A, B)\n" 1867 "#include \"b.h\"\n" 1868 "// somecomment\n", 1869 format(" // somecomment\n" 1870 " #include \"a.h\"\n" 1871 "#define A(A,\\\n" 1872 " B)\n" 1873 " #include \"b.h\"\n" 1874 " // somecomment\n", 1875 getLLVMStyleWithColumns(13))); 1876 } 1877 1878 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1879 1880 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1881 EXPECT_EQ("#define A \\\n" 1882 " c; \\\n" 1883 " e;\n" 1884 "f;", 1885 format("#define A c; e;\n" 1886 "f;", 1887 getLLVMStyleWithColumns(14))); 1888 } 1889 1890 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1891 1892 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1893 EXPECT_EQ("int x,\n" 1894 "#define A\n" 1895 " y;", 1896 format("int x,\n#define A\ny;")); 1897 } 1898 1899 TEST_F(FormatTest, HashInMacroDefinition) { 1900 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 1901 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 1902 verifyFormat("#define A \\\n" 1903 " { \\\n" 1904 " f(#c); \\\n" 1905 " }", 1906 getLLVMStyleWithColumns(11)); 1907 1908 verifyFormat("#define A(X) \\\n" 1909 " void function##X()", 1910 getLLVMStyleWithColumns(22)); 1911 1912 verifyFormat("#define A(a, b, c) \\\n" 1913 " void a##b##c()", 1914 getLLVMStyleWithColumns(22)); 1915 1916 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 1917 } 1918 1919 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1920 EXPECT_EQ("#define A (x)", format("#define A (x)")); 1921 EXPECT_EQ("#define A(x)", format("#define A(x)")); 1922 } 1923 1924 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 1925 EXPECT_EQ("#define A b;", format("#define A \\\n" 1926 " \\\n" 1927 " b;", 1928 getLLVMStyleWithColumns(25))); 1929 EXPECT_EQ("#define A \\\n" 1930 " \\\n" 1931 " a; \\\n" 1932 " b;", 1933 format("#define A \\\n" 1934 " \\\n" 1935 " a; \\\n" 1936 " b;", 1937 getLLVMStyleWithColumns(11))); 1938 EXPECT_EQ("#define A \\\n" 1939 " a; \\\n" 1940 " \\\n" 1941 " b;", 1942 format("#define A \\\n" 1943 " a; \\\n" 1944 " \\\n" 1945 " b;", 1946 getLLVMStyleWithColumns(11))); 1947 } 1948 1949 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 1950 verifyIncompleteFormat("#define A :"); 1951 verifyFormat("#define SOMECASES \\\n" 1952 " case 1: \\\n" 1953 " case 2\n", 1954 getLLVMStyleWithColumns(20)); 1955 verifyFormat("#define MACRO(a) \\\n" 1956 " if (a) \\\n" 1957 " f(); \\\n" 1958 " else \\\n" 1959 " g()", 1960 getLLVMStyleWithColumns(18)); 1961 verifyFormat("#define A template <typename T>"); 1962 verifyIncompleteFormat("#define STR(x) #x\n" 1963 "f(STR(this_is_a_string_literal{));"); 1964 verifyFormat("#pragma omp threadprivate( \\\n" 1965 " y)), // expected-warning", 1966 getLLVMStyleWithColumns(28)); 1967 verifyFormat("#d, = };"); 1968 verifyFormat("#if \"a"); 1969 verifyIncompleteFormat("({\n" 1970 "#define b \\\n" 1971 " } \\\n" 1972 " a\n" 1973 "a", 1974 getLLVMStyleWithColumns(15)); 1975 verifyFormat("#define A \\\n" 1976 " { \\\n" 1977 " {\n" 1978 "#define B \\\n" 1979 " } \\\n" 1980 " }", 1981 getLLVMStyleWithColumns(15)); 1982 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 1983 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 1984 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 1985 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 1986 } 1987 1988 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 1989 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 1990 EXPECT_EQ("class A : public QObject {\n" 1991 " Q_OBJECT\n" 1992 "\n" 1993 " A() {}\n" 1994 "};", 1995 format("class A : public QObject {\n" 1996 " Q_OBJECT\n" 1997 "\n" 1998 " A() {\n}\n" 1999 "} ;")); 2000 EXPECT_EQ("MACRO\n" 2001 "/*static*/ int i;", 2002 format("MACRO\n" 2003 " /*static*/ int i;")); 2004 EXPECT_EQ("SOME_MACRO\n" 2005 "namespace {\n" 2006 "void f();\n" 2007 "} // namespace", 2008 format("SOME_MACRO\n" 2009 " namespace {\n" 2010 "void f( );\n" 2011 "} // namespace")); 2012 // Only if the identifier contains at least 5 characters. 2013 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2014 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2015 // Only if everything is upper case. 2016 EXPECT_EQ("class A : public QObject {\n" 2017 " Q_Object A() {}\n" 2018 "};", 2019 format("class A : public QObject {\n" 2020 " Q_Object\n" 2021 " A() {\n}\n" 2022 "} ;")); 2023 2024 // Only if the next line can actually start an unwrapped line. 2025 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2026 format("SOME_WEIRD_LOG_MACRO\n" 2027 "<< SomeThing;")); 2028 2029 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2030 "(n, buffers))\n", 2031 getChromiumStyle(FormatStyle::LK_Cpp)); 2032 } 2033 2034 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2035 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2036 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2037 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2038 "class X {};\n" 2039 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2040 "int *createScopDetectionPass() { return 0; }", 2041 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2042 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2043 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2044 " class X {};\n" 2045 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2046 " int *createScopDetectionPass() { return 0; }")); 2047 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2048 // braces, so that inner block is indented one level more. 2049 EXPECT_EQ("int q() {\n" 2050 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2051 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2052 " IPC_END_MESSAGE_MAP()\n" 2053 "}", 2054 format("int q() {\n" 2055 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2056 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2057 " IPC_END_MESSAGE_MAP()\n" 2058 "}")); 2059 2060 // Same inside macros. 2061 EXPECT_EQ("#define LIST(L) \\\n" 2062 " L(A) \\\n" 2063 " L(B) \\\n" 2064 " L(C)", 2065 format("#define LIST(L) \\\n" 2066 " L(A) \\\n" 2067 " L(B) \\\n" 2068 " L(C)", 2069 getGoogleStyle())); 2070 2071 // These must not be recognized as macros. 2072 EXPECT_EQ("int q() {\n" 2073 " f(x);\n" 2074 " f(x) {}\n" 2075 " f(x)->g();\n" 2076 " f(x)->*g();\n" 2077 " f(x).g();\n" 2078 " f(x) = x;\n" 2079 " f(x) += x;\n" 2080 " f(x) -= x;\n" 2081 " f(x) *= x;\n" 2082 " f(x) /= x;\n" 2083 " f(x) %= x;\n" 2084 " f(x) &= x;\n" 2085 " f(x) |= x;\n" 2086 " f(x) ^= x;\n" 2087 " f(x) >>= x;\n" 2088 " f(x) <<= x;\n" 2089 " f(x)[y].z();\n" 2090 " LOG(INFO) << x;\n" 2091 " ifstream(x) >> x;\n" 2092 "}\n", 2093 format("int q() {\n" 2094 " f(x)\n;\n" 2095 " f(x)\n {}\n" 2096 " f(x)\n->g();\n" 2097 " f(x)\n->*g();\n" 2098 " f(x)\n.g();\n" 2099 " f(x)\n = x;\n" 2100 " f(x)\n += x;\n" 2101 " f(x)\n -= x;\n" 2102 " f(x)\n *= x;\n" 2103 " f(x)\n /= x;\n" 2104 " f(x)\n %= x;\n" 2105 " f(x)\n &= x;\n" 2106 " f(x)\n |= x;\n" 2107 " f(x)\n ^= x;\n" 2108 " f(x)\n >>= x;\n" 2109 " f(x)\n <<= x;\n" 2110 " f(x)\n[y].z();\n" 2111 " LOG(INFO)\n << x;\n" 2112 " ifstream(x)\n >> x;\n" 2113 "}\n")); 2114 EXPECT_EQ("int q() {\n" 2115 " F(x)\n" 2116 " if (1) {\n" 2117 " }\n" 2118 " F(x)\n" 2119 " while (1) {\n" 2120 " }\n" 2121 " F(x)\n" 2122 " G(x);\n" 2123 " F(x)\n" 2124 " try {\n" 2125 " Q();\n" 2126 " } catch (...) {\n" 2127 " }\n" 2128 "}\n", 2129 format("int q() {\n" 2130 "F(x)\n" 2131 "if (1) {}\n" 2132 "F(x)\n" 2133 "while (1) {}\n" 2134 "F(x)\n" 2135 "G(x);\n" 2136 "F(x)\n" 2137 "try { Q(); } catch (...) {}\n" 2138 "}\n")); 2139 EXPECT_EQ("class A {\n" 2140 " A() : t(0) {}\n" 2141 " A(int i) noexcept() : {}\n" 2142 " A(X x)\n" // FIXME: function-level try blocks are broken. 2143 " try : t(0) {\n" 2144 " } catch (...) {\n" 2145 " }\n" 2146 "};", 2147 format("class A {\n" 2148 " A()\n : t(0) {}\n" 2149 " A(int i)\n noexcept() : {}\n" 2150 " A(X x)\n" 2151 " try : t(0) {} catch (...) {}\n" 2152 "};")); 2153 EXPECT_EQ("class SomeClass {\n" 2154 "public:\n" 2155 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2156 "};", 2157 format("class SomeClass {\n" 2158 "public:\n" 2159 " SomeClass()\n" 2160 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2161 "};")); 2162 EXPECT_EQ("class SomeClass {\n" 2163 "public:\n" 2164 " SomeClass()\n" 2165 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2166 "};", 2167 format("class SomeClass {\n" 2168 "public:\n" 2169 " SomeClass()\n" 2170 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2171 "};", 2172 getLLVMStyleWithColumns(40))); 2173 2174 verifyFormat("MACRO(>)"); 2175 } 2176 2177 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2178 verifyFormat("#define A \\\n" 2179 " f({ \\\n" 2180 " g(); \\\n" 2181 " });", 2182 getLLVMStyleWithColumns(11)); 2183 } 2184 2185 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2186 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2187 } 2188 2189 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2190 verifyFormat("{\n { a #c; }\n}"); 2191 } 2192 2193 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2194 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2195 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2196 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2197 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2198 } 2199 2200 TEST_F(FormatTest, EscapedNewlines) { 2201 EXPECT_EQ( 2202 "#define A \\\n int i; \\\n int j;", 2203 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2204 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2205 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2206 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2207 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2208 } 2209 2210 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2211 verifyFormat("#define A \\\n" 2212 " int v( \\\n" 2213 " a); \\\n" 2214 " int i;", 2215 getLLVMStyleWithColumns(11)); 2216 } 2217 2218 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2219 EXPECT_EQ( 2220 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2221 " \\\n" 2222 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2223 "\n" 2224 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2225 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2226 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2227 "\\\n" 2228 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2229 " \n" 2230 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2231 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2232 } 2233 2234 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2235 EXPECT_EQ("int\n" 2236 "#define A\n" 2237 " a;", 2238 format("int\n#define A\na;")); 2239 verifyFormat("functionCallTo(\n" 2240 " someOtherFunction(\n" 2241 " withSomeParameters, whichInSequence,\n" 2242 " areLongerThanALine(andAnotherCall,\n" 2243 "#define A B\n" 2244 " withMoreParamters,\n" 2245 " whichStronglyInfluenceTheLayout),\n" 2246 " andMoreParameters),\n" 2247 " trailing);", 2248 getLLVMStyleWithColumns(69)); 2249 verifyFormat("Foo::Foo()\n" 2250 "#ifdef BAR\n" 2251 " : baz(0)\n" 2252 "#endif\n" 2253 "{\n" 2254 "}"); 2255 verifyFormat("void f() {\n" 2256 " if (true)\n" 2257 "#ifdef A\n" 2258 " f(42);\n" 2259 " x();\n" 2260 "#else\n" 2261 " g();\n" 2262 " x();\n" 2263 "#endif\n" 2264 "}"); 2265 verifyFormat("void f(param1, param2,\n" 2266 " param3,\n" 2267 "#ifdef A\n" 2268 " param4(param5,\n" 2269 "#ifdef A1\n" 2270 " param6,\n" 2271 "#ifdef A2\n" 2272 " param7),\n" 2273 "#else\n" 2274 " param8),\n" 2275 " param9,\n" 2276 "#endif\n" 2277 " param10,\n" 2278 "#endif\n" 2279 " param11)\n" 2280 "#else\n" 2281 " param12)\n" 2282 "#endif\n" 2283 "{\n" 2284 " x();\n" 2285 "}", 2286 getLLVMStyleWithColumns(28)); 2287 verifyFormat("#if 1\n" 2288 "int i;"); 2289 verifyFormat("#if 1\n" 2290 "#endif\n" 2291 "#if 1\n" 2292 "#else\n" 2293 "#endif\n"); 2294 verifyFormat("DEBUG({\n" 2295 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2297 "});\n" 2298 "#if a\n" 2299 "#else\n" 2300 "#endif"); 2301 2302 verifyIncompleteFormat("void f(\n" 2303 "#if A\n" 2304 ");\n" 2305 "#else\n" 2306 "#endif"); 2307 } 2308 2309 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2310 verifyFormat("#endif\n" 2311 "#if B"); 2312 } 2313 2314 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2315 FormatStyle SingleLine = getLLVMStyle(); 2316 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2317 verifyFormat("#if 0\n" 2318 "#elif 1\n" 2319 "#endif\n" 2320 "void foo() {\n" 2321 " if (test) foo2();\n" 2322 "}", 2323 SingleLine); 2324 } 2325 2326 TEST_F(FormatTest, LayoutBlockInsideParens) { 2327 verifyFormat("functionCall({ int i; });"); 2328 verifyFormat("functionCall({\n" 2329 " int i;\n" 2330 " int j;\n" 2331 "});"); 2332 verifyFormat("functionCall(\n" 2333 " {\n" 2334 " int i;\n" 2335 " int j;\n" 2336 " },\n" 2337 " aaaa, bbbb, cccc);"); 2338 verifyFormat("functionA(functionB({\n" 2339 " int i;\n" 2340 " int j;\n" 2341 " }),\n" 2342 " aaaa, bbbb, cccc);"); 2343 verifyFormat("functionCall(\n" 2344 " {\n" 2345 " int i;\n" 2346 " int j;\n" 2347 " },\n" 2348 " aaaa, bbbb, // comment\n" 2349 " cccc);"); 2350 verifyFormat("functionA(functionB({\n" 2351 " int i;\n" 2352 " int j;\n" 2353 " }),\n" 2354 " aaaa, bbbb, // comment\n" 2355 " cccc);"); 2356 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2357 verifyFormat("functionCall(aaaa, bbbb, {\n" 2358 " int i;\n" 2359 " int j;\n" 2360 "});"); 2361 verifyFormat( 2362 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2363 " {\n" 2364 " int i; // break\n" 2365 " },\n" 2366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2367 " ccccccccccccccccc));"); 2368 verifyFormat("DEBUG({\n" 2369 " if (a)\n" 2370 " f();\n" 2371 "});"); 2372 } 2373 2374 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2375 EXPECT_EQ("SOME_MACRO { int i; }\n" 2376 "int i;", 2377 format(" SOME_MACRO {int i;} int i;")); 2378 } 2379 2380 TEST_F(FormatTest, LayoutNestedBlocks) { 2381 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2382 " struct s {\n" 2383 " int i;\n" 2384 " };\n" 2385 " s kBitsToOs[] = {{10}};\n" 2386 " for (int i = 0; i < 10; ++i)\n" 2387 " return;\n" 2388 "}"); 2389 verifyFormat("call(parameter, {\n" 2390 " something();\n" 2391 " // Comment using all columns.\n" 2392 " somethingelse();\n" 2393 "});", 2394 getLLVMStyleWithColumns(40)); 2395 verifyFormat("DEBUG( //\n" 2396 " { f(); }, a);"); 2397 verifyFormat("DEBUG( //\n" 2398 " {\n" 2399 " f(); //\n" 2400 " },\n" 2401 " a);"); 2402 2403 EXPECT_EQ("call(parameter, {\n" 2404 " something();\n" 2405 " // Comment too\n" 2406 " // looooooooooong.\n" 2407 " somethingElse();\n" 2408 "});", 2409 format("call(parameter, {\n" 2410 " something();\n" 2411 " // Comment too looooooooooong.\n" 2412 " somethingElse();\n" 2413 "});", 2414 getLLVMStyleWithColumns(29))); 2415 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2416 EXPECT_EQ("DEBUG({ // comment\n" 2417 " int i;\n" 2418 "});", 2419 format("DEBUG({ // comment\n" 2420 "int i;\n" 2421 "});")); 2422 EXPECT_EQ("DEBUG({\n" 2423 " int i;\n" 2424 "\n" 2425 " // comment\n" 2426 " int j;\n" 2427 "});", 2428 format("DEBUG({\n" 2429 " int i;\n" 2430 "\n" 2431 " // comment\n" 2432 " int j;\n" 2433 "});")); 2434 2435 verifyFormat("DEBUG({\n" 2436 " if (a)\n" 2437 " return;\n" 2438 "});"); 2439 verifyGoogleFormat("DEBUG({\n" 2440 " if (a) return;\n" 2441 "});"); 2442 FormatStyle Style = getGoogleStyle(); 2443 Style.ColumnLimit = 45; 2444 verifyFormat("Debug(aaaaa,\n" 2445 " {\n" 2446 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2447 " },\n" 2448 " a);", 2449 Style); 2450 2451 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2452 2453 verifyNoCrash("^{v^{a}}"); 2454 } 2455 2456 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2457 EXPECT_EQ("#define MACRO() \\\n" 2458 " Debug(aaa, /* force line break */ \\\n" 2459 " { \\\n" 2460 " int i; \\\n" 2461 " int j; \\\n" 2462 " })", 2463 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2464 " { int i; int j; })", 2465 getGoogleStyle())); 2466 2467 EXPECT_EQ("#define A \\\n" 2468 " [] { \\\n" 2469 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2470 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2471 " }", 2472 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2473 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2474 getGoogleStyle())); 2475 } 2476 2477 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2478 EXPECT_EQ("{}", format("{}")); 2479 verifyFormat("enum E {};"); 2480 verifyFormat("enum E {}"); 2481 } 2482 2483 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2484 FormatStyle Style = getLLVMStyle(); 2485 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2486 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2487 verifyFormat("FOO_BEGIN\n" 2488 " FOO_ENTRY\n" 2489 "FOO_END", Style); 2490 verifyFormat("FOO_BEGIN\n" 2491 " NESTED_FOO_BEGIN\n" 2492 " NESTED_FOO_ENTRY\n" 2493 " NESTED_FOO_END\n" 2494 "FOO_END", Style); 2495 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2496 " int x;\n" 2497 " x = 1;\n" 2498 "FOO_END(Baz)", Style); 2499 } 2500 2501 //===----------------------------------------------------------------------===// 2502 // Line break tests. 2503 //===----------------------------------------------------------------------===// 2504 2505 TEST_F(FormatTest, PreventConfusingIndents) { 2506 verifyFormat( 2507 "void f() {\n" 2508 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2509 " parameter, parameter, parameter)),\n" 2510 " SecondLongCall(parameter));\n" 2511 "}"); 2512 verifyFormat( 2513 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2514 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2515 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2516 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2517 verifyFormat( 2518 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2519 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2520 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2521 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2522 verifyFormat( 2523 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2524 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2526 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2527 verifyFormat("int a = bbbb && ccc &&\n" 2528 " fffff(\n" 2529 "#define A Just forcing a new line\n" 2530 " ddd);"); 2531 } 2532 2533 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2534 verifyFormat( 2535 "bool aaaaaaa =\n" 2536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2537 " bbbbbbbb();"); 2538 verifyFormat( 2539 "bool aaaaaaa =\n" 2540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 2541 " bbbbbbbb();"); 2542 2543 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2545 " ccccccccc == ddddddddddd;"); 2546 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 2548 " ccccccccc == ddddddddddd;"); 2549 verifyFormat( 2550 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 2552 " ccccccccc == ddddddddddd;"); 2553 2554 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2555 " aaaaaa) &&\n" 2556 " bbbbbb && cccccc;"); 2557 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2558 " aaaaaa) >>\n" 2559 " bbbbbb;"); 2560 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 2561 " SourceMgr.getSpellingColumnNumber(\n" 2562 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2563 " 1);"); 2564 2565 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2566 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2567 " cccccc) {\n}"); 2568 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2569 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 2570 " cccccc) {\n}"); 2571 verifyFormat("b = a &&\n" 2572 " // Comment\n" 2573 " b.c && d;"); 2574 2575 // If the LHS of a comparison is not a binary expression itself, the 2576 // additional linebreak confuses many people. 2577 verifyFormat( 2578 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 2580 "}"); 2581 verifyFormat( 2582 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2584 "}"); 2585 verifyFormat( 2586 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 2587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2588 "}"); 2589 // Even explicit parentheses stress the precedence enough to make the 2590 // additional break unnecessary. 2591 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2593 "}"); 2594 // This cases is borderline, but with the indentation it is still readable. 2595 verifyFormat( 2596 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2597 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2599 "}", 2600 getLLVMStyleWithColumns(75)); 2601 2602 // If the LHS is a binary expression, we should still use the additional break 2603 // as otherwise the formatting hides the operator precedence. 2604 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2606 " 5) {\n" 2607 "}"); 2608 2609 FormatStyle OnePerLine = getLLVMStyle(); 2610 OnePerLine.BinPackParameters = false; 2611 verifyFormat( 2612 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2615 OnePerLine); 2616 2617 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 2618 " .aaa(aaaaaaaaaaaaa) *\n" 2619 " aaaaaaa +\n" 2620 " aaaaaaa;", 2621 getLLVMStyleWithColumns(40)); 2622 } 2623 2624 TEST_F(FormatTest, ExpressionIndentation) { 2625 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2629 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2630 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2633 " ccccccccccccccccccccccccccccccccccccccccc;"); 2634 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2637 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2638 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2641 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2642 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2644 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2645 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2646 verifyFormat("if () {\n" 2647 "} else if (aaaaa && bbbbb > // break\n" 2648 " ccccc) {\n" 2649 "}"); 2650 verifyFormat("if () {\n" 2651 "} else if (aaaaa &&\n" 2652 " bbbbb > // break\n" 2653 " ccccc &&\n" 2654 " ddddd) {\n" 2655 "}"); 2656 2657 // Presence of a trailing comment used to change indentation of b. 2658 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 2659 " b;\n" 2660 "return aaaaaaaaaaaaaaaaaaa +\n" 2661 " b; //", 2662 getLLVMStyleWithColumns(30)); 2663 } 2664 2665 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 2666 // Not sure what the best system is here. Like this, the LHS can be found 2667 // immediately above an operator (everything with the same or a higher 2668 // indent). The RHS is aligned right of the operator and so compasses 2669 // everything until something with the same indent as the operator is found. 2670 // FIXME: Is this a good system? 2671 FormatStyle Style = getLLVMStyle(); 2672 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 2673 verifyFormat( 2674 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2675 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2676 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2677 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2678 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2679 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2680 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2681 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2682 " > ccccccccccccccccccccccccccccccccccccccccc;", 2683 Style); 2684 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2685 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2686 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2687 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2688 Style); 2689 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2690 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2691 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2692 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2693 Style); 2694 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2695 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2696 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2697 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2698 Style); 2699 verifyFormat("if () {\n" 2700 "} else if (aaaaa\n" 2701 " && bbbbb // break\n" 2702 " > ccccc) {\n" 2703 "}", 2704 Style); 2705 verifyFormat("return (a)\n" 2706 " // comment\n" 2707 " + b;", 2708 Style); 2709 verifyFormat( 2710 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2711 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2712 " + cc;", 2713 Style); 2714 2715 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2716 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 2717 Style); 2718 2719 // Forced by comments. 2720 verifyFormat( 2721 "unsigned ContentSize =\n" 2722 " sizeof(int16_t) // DWARF ARange version number\n" 2723 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 2724 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 2725 " + sizeof(int8_t); // Segment Size (in bytes)"); 2726 2727 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 2728 " == boost::fusion::at_c<1>(iiii).second;", 2729 Style); 2730 2731 Style.ColumnLimit = 60; 2732 verifyFormat("zzzzzzzzzz\n" 2733 " = bbbbbbbbbbbbbbbbb\n" 2734 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 2735 Style); 2736 } 2737 2738 TEST_F(FormatTest, EnforcedOperatorWraps) { 2739 // Here we'd like to wrap after the || operators, but a comment is forcing an 2740 // earlier wrap. 2741 verifyFormat("bool x = aaaaa //\n" 2742 " || bbbbb\n" 2743 " //\n" 2744 " || cccc;"); 2745 } 2746 2747 TEST_F(FormatTest, NoOperandAlignment) { 2748 FormatStyle Style = getLLVMStyle(); 2749 Style.AlignOperands = false; 2750 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 2751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2753 Style); 2754 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2755 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2756 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2757 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2758 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2759 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2760 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2761 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2762 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2763 " > ccccccccccccccccccccccccccccccccccccccccc;", 2764 Style); 2765 2766 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2767 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2768 " + cc;", 2769 Style); 2770 verifyFormat("int a = aa\n" 2771 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2772 " * cccccccccccccccccccccccccccccccccccc;\n", 2773 Style); 2774 2775 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 2776 verifyFormat("return (a > b\n" 2777 " // comment1\n" 2778 " // comment2\n" 2779 " || c);", 2780 Style); 2781 } 2782 2783 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 2784 FormatStyle Style = getLLVMStyle(); 2785 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2786 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2788 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2789 Style); 2790 } 2791 2792 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 2793 FormatStyle Style = getLLVMStyle(); 2794 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2795 Style.BinPackArguments = false; 2796 Style.ColumnLimit = 40; 2797 verifyFormat("void test() {\n" 2798 " someFunction(\n" 2799 " this + argument + is + quite\n" 2800 " + long + so + it + gets + wrapped\n" 2801 " + but + remains + bin - packed);\n" 2802 "}", 2803 Style); 2804 verifyFormat("void test() {\n" 2805 " someFunction(arg1,\n" 2806 " this + argument + is\n" 2807 " + quite + long + so\n" 2808 " + it + gets + wrapped\n" 2809 " + but + remains + bin\n" 2810 " - packed,\n" 2811 " arg3);\n" 2812 "}", 2813 Style); 2814 verifyFormat("void test() {\n" 2815 " someFunction(\n" 2816 " arg1,\n" 2817 " this + argument + has\n" 2818 " + anotherFunc(nested,\n" 2819 " calls + whose\n" 2820 " + arguments\n" 2821 " + are + also\n" 2822 " + wrapped,\n" 2823 " in + addition)\n" 2824 " + to + being + bin - packed,\n" 2825 " arg3);\n" 2826 "}", 2827 Style); 2828 2829 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 2830 verifyFormat("void test() {\n" 2831 " someFunction(\n" 2832 " arg1,\n" 2833 " this + argument + has +\n" 2834 " anotherFunc(nested,\n" 2835 " calls + whose +\n" 2836 " arguments +\n" 2837 " are + also +\n" 2838 " wrapped,\n" 2839 " in + addition) +\n" 2840 " to + being + bin - packed,\n" 2841 " arg3);\n" 2842 "}", 2843 Style); 2844 } 2845 2846 TEST_F(FormatTest, ConstructorInitializers) { 2847 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2848 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2849 getLLVMStyleWithColumns(45)); 2850 verifyFormat("Constructor()\n" 2851 " : Inttializer(FitsOnTheLine) {}", 2852 getLLVMStyleWithColumns(44)); 2853 verifyFormat("Constructor()\n" 2854 " : Inttializer(FitsOnTheLine) {}", 2855 getLLVMStyleWithColumns(43)); 2856 2857 verifyFormat("template <typename T>\n" 2858 "Constructor() : Initializer(FitsOnTheLine) {}", 2859 getLLVMStyleWithColumns(45)); 2860 2861 verifyFormat( 2862 "SomeClass::Constructor()\n" 2863 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2864 2865 verifyFormat( 2866 "SomeClass::Constructor()\n" 2867 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2868 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2869 verifyFormat( 2870 "SomeClass::Constructor()\n" 2871 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2872 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2873 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2874 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2875 " : aaaaaaaaaa(aaaaaa) {}"); 2876 2877 verifyFormat("Constructor()\n" 2878 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2879 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2880 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2881 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 2882 2883 verifyFormat("Constructor()\n" 2884 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2886 2887 verifyFormat("Constructor(int Parameter = 0)\n" 2888 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2889 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 2890 verifyFormat("Constructor()\n" 2891 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 2892 "}", 2893 getLLVMStyleWithColumns(60)); 2894 verifyFormat("Constructor()\n" 2895 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2896 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 2897 2898 // Here a line could be saved by splitting the second initializer onto two 2899 // lines, but that is not desirable. 2900 verifyFormat("Constructor()\n" 2901 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 2902 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 2903 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2904 2905 FormatStyle OnePerLine = getLLVMStyle(); 2906 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2907 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 2908 verifyFormat("SomeClass::Constructor()\n" 2909 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2910 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2911 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2912 OnePerLine); 2913 verifyFormat("SomeClass::Constructor()\n" 2914 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 2915 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2916 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2917 OnePerLine); 2918 verifyFormat("MyClass::MyClass(int var)\n" 2919 " : some_var_(var), // 4 space indent\n" 2920 " some_other_var_(var + 1) { // lined up\n" 2921 "}", 2922 OnePerLine); 2923 verifyFormat("Constructor()\n" 2924 " : aaaaa(aaaaaa),\n" 2925 " aaaaa(aaaaaa),\n" 2926 " aaaaa(aaaaaa),\n" 2927 " aaaaa(aaaaaa),\n" 2928 " aaaaa(aaaaaa) {}", 2929 OnePerLine); 2930 verifyFormat("Constructor()\n" 2931 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 2932 " aaaaaaaaaaaaaaaaaaaaaa) {}", 2933 OnePerLine); 2934 OnePerLine.BinPackParameters = false; 2935 verifyFormat( 2936 "Constructor()\n" 2937 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2938 " aaaaaaaaaaa().aaa(),\n" 2939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2940 OnePerLine); 2941 OnePerLine.ColumnLimit = 60; 2942 verifyFormat("Constructor()\n" 2943 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 2944 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 2945 OnePerLine); 2946 2947 EXPECT_EQ("Constructor()\n" 2948 " : // Comment forcing unwanted break.\n" 2949 " aaaa(aaaa) {}", 2950 format("Constructor() :\n" 2951 " // Comment forcing unwanted break.\n" 2952 " aaaa(aaaa) {}")); 2953 } 2954 2955 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 2956 FormatStyle Style = getLLVMStyle(); 2957 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 2958 2959 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2960 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 2961 getStyleWithColumns(Style, 45)); 2962 verifyFormat("Constructor() :\n" 2963 " Initializer(FitsOnTheLine) {}", 2964 getStyleWithColumns(Style, 44)); 2965 verifyFormat("Constructor() :\n" 2966 " Initializer(FitsOnTheLine) {}", 2967 getStyleWithColumns(Style, 43)); 2968 2969 verifyFormat("template <typename T>\n" 2970 "Constructor() : Initializer(FitsOnTheLine) {}", 2971 getStyleWithColumns(Style, 50)); 2972 2973 verifyFormat( 2974 "SomeClass::Constructor() :\n" 2975 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 2976 Style); 2977 2978 verifyFormat( 2979 "SomeClass::Constructor() :\n" 2980 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2981 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2982 Style); 2983 verifyFormat( 2984 "SomeClass::Constructor() :\n" 2985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2986 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 2987 Style); 2988 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2989 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 2990 " aaaaaaaaaa(aaaaaa) {}", 2991 Style); 2992 2993 verifyFormat("Constructor() :\n" 2994 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2995 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2996 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2997 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 2998 Style); 2999 3000 verifyFormat("Constructor() :\n" 3001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3003 Style); 3004 3005 verifyFormat("Constructor(int Parameter = 0) :\n" 3006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3007 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3008 Style); 3009 verifyFormat("Constructor() :\n" 3010 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3011 "}", 3012 getStyleWithColumns(Style, 60)); 3013 verifyFormat("Constructor() :\n" 3014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3015 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3016 Style); 3017 3018 // Here a line could be saved by splitting the second initializer onto two 3019 // lines, but that is not desirable. 3020 verifyFormat("Constructor() :\n" 3021 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3022 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3023 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3024 Style); 3025 3026 FormatStyle OnePerLine = Style; 3027 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3028 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3029 verifyFormat("SomeClass::Constructor() :\n" 3030 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3031 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3032 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3033 OnePerLine); 3034 verifyFormat("SomeClass::Constructor() :\n" 3035 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3036 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3037 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3038 OnePerLine); 3039 verifyFormat("MyClass::MyClass(int var) :\n" 3040 " some_var_(var), // 4 space indent\n" 3041 " some_other_var_(var + 1) { // lined up\n" 3042 "}", 3043 OnePerLine); 3044 verifyFormat("Constructor() :\n" 3045 " aaaaa(aaaaaa),\n" 3046 " aaaaa(aaaaaa),\n" 3047 " aaaaa(aaaaaa),\n" 3048 " aaaaa(aaaaaa),\n" 3049 " aaaaa(aaaaaa) {}", 3050 OnePerLine); 3051 verifyFormat("Constructor() :\n" 3052 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3053 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3054 OnePerLine); 3055 OnePerLine.BinPackParameters = false; 3056 verifyFormat( 3057 "Constructor() :\n" 3058 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3059 " aaaaaaaaaaa().aaa(),\n" 3060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3061 OnePerLine); 3062 OnePerLine.ColumnLimit = 60; 3063 verifyFormat("Constructor() :\n" 3064 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3065 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3066 OnePerLine); 3067 3068 EXPECT_EQ("Constructor() :\n" 3069 " // Comment forcing unwanted break.\n" 3070 " aaaa(aaaa) {}", 3071 format("Constructor() :\n" 3072 " // Comment forcing unwanted break.\n" 3073 " aaaa(aaaa) {}", 3074 Style)); 3075 3076 Style.ColumnLimit = 0; 3077 verifyFormat("SomeClass::Constructor() :\n" 3078 " a(a) {}", 3079 Style); 3080 verifyFormat("SomeClass::Constructor() noexcept :\n" 3081 " a(a) {}", 3082 Style); 3083 verifyFormat("SomeClass::Constructor() :\n" 3084 " a(a), b(b), c(c) {}", 3085 Style); 3086 verifyFormat("SomeClass::Constructor() :\n" 3087 " a(a) {\n" 3088 " foo();\n" 3089 " bar();\n" 3090 "}", 3091 Style); 3092 3093 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3094 verifyFormat("SomeClass::Constructor() :\n" 3095 " a(a), b(b), c(c) {\n" 3096 "}", 3097 Style); 3098 verifyFormat("SomeClass::Constructor() :\n" 3099 " a(a) {\n" 3100 "}", 3101 Style); 3102 3103 Style.ColumnLimit = 80; 3104 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3105 Style.ConstructorInitializerIndentWidth = 2; 3106 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3107 Style); 3108 verifyFormat("SomeClass::Constructor() :\n" 3109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3110 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3111 Style); 3112 } 3113 3114 TEST_F(FormatTest, MemoizationTests) { 3115 // This breaks if the memoization lookup does not take \c Indent and 3116 // \c LastSpace into account. 3117 verifyFormat( 3118 "extern CFRunLoopTimerRef\n" 3119 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3120 " CFTimeInterval interval, CFOptionFlags flags,\n" 3121 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3122 " CFRunLoopTimerContext *context) {}"); 3123 3124 // Deep nesting somewhat works around our memoization. 3125 verifyFormat( 3126 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3127 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3128 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3129 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3130 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3131 getLLVMStyleWithColumns(65)); 3132 verifyFormat( 3133 "aaaaa(\n" 3134 " aaaaa,\n" 3135 " aaaaa(\n" 3136 " aaaaa,\n" 3137 " aaaaa(\n" 3138 " aaaaa,\n" 3139 " aaaaa(\n" 3140 " aaaaa,\n" 3141 " aaaaa(\n" 3142 " aaaaa,\n" 3143 " aaaaa(\n" 3144 " aaaaa,\n" 3145 " aaaaa(\n" 3146 " aaaaa,\n" 3147 " aaaaa(\n" 3148 " aaaaa,\n" 3149 " aaaaa(\n" 3150 " aaaaa,\n" 3151 " aaaaa(\n" 3152 " aaaaa,\n" 3153 " aaaaa(\n" 3154 " aaaaa,\n" 3155 " aaaaa(\n" 3156 " aaaaa,\n" 3157 " aaaaa))))))))))));", 3158 getLLVMStyleWithColumns(65)); 3159 verifyFormat( 3160 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n" 3161 " a),\n" 3162 " a),\n" 3163 " a),\n" 3164 " a),\n" 3165 " a),\n" 3166 " a),\n" 3167 " a),\n" 3168 " a),\n" 3169 " a),\n" 3170 " a),\n" 3171 " a),\n" 3172 " a),\n" 3173 " a),\n" 3174 " a),\n" 3175 " a),\n" 3176 " a),\n" 3177 " a)", 3178 getLLVMStyleWithColumns(65)); 3179 3180 // This test takes VERY long when memoization is broken. 3181 FormatStyle OnePerLine = getLLVMStyle(); 3182 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3183 OnePerLine.BinPackParameters = false; 3184 std::string input = "Constructor()\n" 3185 " : aaaa(a,\n"; 3186 for (unsigned i = 0, e = 80; i != e; ++i) { 3187 input += " a,\n"; 3188 } 3189 input += " a) {}"; 3190 verifyFormat(input, OnePerLine); 3191 } 3192 3193 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3194 verifyFormat( 3195 "void f() {\n" 3196 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3197 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3198 " f();\n" 3199 "}"); 3200 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3201 " Intervals[i - 1].getRange().getLast()) {\n}"); 3202 } 3203 3204 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3205 // Principially, we break function declarations in a certain order: 3206 // 1) break amongst arguments. 3207 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3208 " Cccccccccccccc cccccccccccccc);"); 3209 verifyFormat("template <class TemplateIt>\n" 3210 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3211 " TemplateIt *stop) {}"); 3212 3213 // 2) break after return type. 3214 verifyFormat( 3215 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3216 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3217 getGoogleStyle()); 3218 3219 // 3) break after (. 3220 verifyFormat( 3221 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3222 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3223 getGoogleStyle()); 3224 3225 // 4) break before after nested name specifiers. 3226 verifyFormat( 3227 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3228 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3229 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3230 getGoogleStyle()); 3231 3232 // However, there are exceptions, if a sufficient amount of lines can be 3233 // saved. 3234 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3235 // more adjusting. 3236 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3237 " Cccccccccccccc cccccccccc,\n" 3238 " Cccccccccccccc cccccccccc,\n" 3239 " Cccccccccccccc cccccccccc,\n" 3240 " Cccccccccccccc cccccccccc);"); 3241 verifyFormat( 3242 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3243 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3244 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3245 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3246 getGoogleStyle()); 3247 verifyFormat( 3248 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3249 " Cccccccccccccc cccccccccc,\n" 3250 " Cccccccccccccc cccccccccc,\n" 3251 " Cccccccccccccc cccccccccc,\n" 3252 " Cccccccccccccc cccccccccc,\n" 3253 " Cccccccccccccc cccccccccc,\n" 3254 " Cccccccccccccc cccccccccc);"); 3255 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3256 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3257 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3258 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3259 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3260 3261 // Break after multi-line parameters. 3262 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3265 " bbbb bbbb);"); 3266 verifyFormat("void SomeLoooooooooooongFunction(\n" 3267 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3268 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3269 " int bbbbbbbbbbbbb);"); 3270 3271 // Treat overloaded operators like other functions. 3272 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3273 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3274 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3275 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3276 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3277 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3278 verifyGoogleFormat( 3279 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3280 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3281 verifyGoogleFormat( 3282 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3283 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3284 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3285 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3286 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3287 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3288 verifyGoogleFormat( 3289 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3290 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3291 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3292 verifyGoogleFormat( 3293 "template <typename T>\n" 3294 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3295 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3296 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3297 3298 FormatStyle Style = getLLVMStyle(); 3299 Style.PointerAlignment = FormatStyle::PAS_Left; 3300 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3301 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3302 Style); 3303 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3305 Style); 3306 } 3307 3308 TEST_F(FormatTest, TrailingReturnType) { 3309 verifyFormat("auto foo() -> int;\n"); 3310 verifyFormat("struct S {\n" 3311 " auto bar() const -> int;\n" 3312 "};"); 3313 verifyFormat("template <size_t Order, typename T>\n" 3314 "auto load_img(const std::string &filename)\n" 3315 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3316 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3317 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3318 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3319 verifyFormat("template <typename T>\n" 3320 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3321 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3322 3323 // Not trailing return types. 3324 verifyFormat("void f() { auto a = b->c(); }"); 3325 } 3326 3327 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3328 // Avoid breaking before trailing 'const' or other trailing annotations, if 3329 // they are not function-like. 3330 FormatStyle Style = getGoogleStyle(); 3331 Style.ColumnLimit = 47; 3332 verifyFormat("void someLongFunction(\n" 3333 " int someLoooooooooooooongParameter) const {\n}", 3334 getLLVMStyleWithColumns(47)); 3335 verifyFormat("LoooooongReturnType\n" 3336 "someLoooooooongFunction() const {}", 3337 getLLVMStyleWithColumns(47)); 3338 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3339 " const {}", 3340 Style); 3341 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3342 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3343 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3344 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3345 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3346 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3347 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3348 " aaaaaaaaaaa aaaaa) const override;"); 3349 verifyGoogleFormat( 3350 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3351 " const override;"); 3352 3353 // Even if the first parameter has to be wrapped. 3354 verifyFormat("void someLongFunction(\n" 3355 " int someLongParameter) const {}", 3356 getLLVMStyleWithColumns(46)); 3357 verifyFormat("void someLongFunction(\n" 3358 " int someLongParameter) const {}", 3359 Style); 3360 verifyFormat("void someLongFunction(\n" 3361 " int someLongParameter) override {}", 3362 Style); 3363 verifyFormat("void someLongFunction(\n" 3364 " int someLongParameter) OVERRIDE {}", 3365 Style); 3366 verifyFormat("void someLongFunction(\n" 3367 " int someLongParameter) final {}", 3368 Style); 3369 verifyFormat("void someLongFunction(\n" 3370 " int someLongParameter) FINAL {}", 3371 Style); 3372 verifyFormat("void someLongFunction(\n" 3373 " int parameter) const override {}", 3374 Style); 3375 3376 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3377 verifyFormat("void someLongFunction(\n" 3378 " int someLongParameter) const\n" 3379 "{\n" 3380 "}", 3381 Style); 3382 3383 // Unless these are unknown annotations. 3384 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3385 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3386 " LONG_AND_UGLY_ANNOTATION;"); 3387 3388 // Breaking before function-like trailing annotations is fine to keep them 3389 // close to their arguments. 3390 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3391 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3392 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3393 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3394 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3395 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3396 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3397 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3398 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3399 3400 verifyFormat( 3401 "void aaaaaaaaaaaaaaaaaa()\n" 3402 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3403 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3404 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3405 " __attribute__((unused));"); 3406 verifyGoogleFormat( 3407 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3408 " GUARDED_BY(aaaaaaaaaaaa);"); 3409 verifyGoogleFormat( 3410 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3411 " GUARDED_BY(aaaaaaaaaaaa);"); 3412 verifyGoogleFormat( 3413 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3414 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3415 verifyGoogleFormat( 3416 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3417 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3418 } 3419 3420 TEST_F(FormatTest, FunctionAnnotations) { 3421 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3422 "int OldFunction(const string ¶meter) {}"); 3423 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3424 "string OldFunction(const string ¶meter) {}"); 3425 verifyFormat("template <typename T>\n" 3426 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3427 "string OldFunction(const string ¶meter) {}"); 3428 3429 // Not function annotations. 3430 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3431 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3432 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3433 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3434 verifyFormat("MACRO(abc).function() // wrap\n" 3435 " << abc;"); 3436 verifyFormat("MACRO(abc)->function() // wrap\n" 3437 " << abc;"); 3438 verifyFormat("MACRO(abc)::function() // wrap\n" 3439 " << abc;"); 3440 } 3441 3442 TEST_F(FormatTest, BreaksDesireably) { 3443 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3444 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3445 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3446 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3448 "}"); 3449 3450 verifyFormat( 3451 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3453 3454 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3457 3458 verifyFormat( 3459 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3460 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3462 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3464 3465 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3466 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3467 3468 verifyFormat( 3469 "void f() {\n" 3470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3472 "}"); 3473 verifyFormat( 3474 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3476 verifyFormat( 3477 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3479 verifyFormat( 3480 "aaaaaa(aaa,\n" 3481 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3483 " aaaa);"); 3484 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3487 3488 // Indent consistently independent of call expression and unary operator. 3489 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3490 " dddddddddddddddddddddddddddddd));"); 3491 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3492 " dddddddddddddddddddddddddddddd));"); 3493 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3494 " dddddddddddddddddddddddddddddd));"); 3495 3496 // This test case breaks on an incorrect memoization, i.e. an optimization not 3497 // taking into account the StopAt value. 3498 verifyFormat( 3499 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3500 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3501 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3502 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3503 3504 verifyFormat("{\n {\n {\n" 3505 " Annotation.SpaceRequiredBefore =\n" 3506 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3507 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3508 " }\n }\n}"); 3509 3510 // Break on an outer level if there was a break on an inner level. 3511 EXPECT_EQ("f(g(h(a, // comment\n" 3512 " b, c),\n" 3513 " d, e),\n" 3514 " x, y);", 3515 format("f(g(h(a, // comment\n" 3516 " b, c), d, e), x, y);")); 3517 3518 // Prefer breaking similar line breaks. 3519 verifyFormat( 3520 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3521 " NSTrackingMouseEnteredAndExited |\n" 3522 " NSTrackingActiveAlways;"); 3523 } 3524 3525 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 3526 FormatStyle NoBinPacking = getGoogleStyle(); 3527 NoBinPacking.BinPackParameters = false; 3528 NoBinPacking.BinPackArguments = true; 3529 verifyFormat("void f() {\n" 3530 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 3531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3532 "}", 3533 NoBinPacking); 3534 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 3535 " int aaaaaaaaaaaaaaaaaaaa,\n" 3536 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3537 NoBinPacking); 3538 3539 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3540 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3541 " vector<int> bbbbbbbbbbbbbbb);", 3542 NoBinPacking); 3543 // FIXME: This behavior difference is probably not wanted. However, currently 3544 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 3545 // template arguments from BreakBeforeParameter being set because of the 3546 // one-per-line formatting. 3547 verifyFormat( 3548 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3549 " aaaaaaaaaa> aaaaaaaaaa);", 3550 NoBinPacking); 3551 verifyFormat( 3552 "void fffffffffff(\n" 3553 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 3554 " aaaaaaaaaa);"); 3555 } 3556 3557 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 3558 FormatStyle NoBinPacking = getGoogleStyle(); 3559 NoBinPacking.BinPackParameters = false; 3560 NoBinPacking.BinPackArguments = false; 3561 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 3562 " aaaaaaaaaaaaaaaaaaaa,\n" 3563 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 3564 NoBinPacking); 3565 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 3566 " aaaaaaaaaaaaa,\n" 3567 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 3568 NoBinPacking); 3569 verifyFormat( 3570 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3571 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3573 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 3575 NoBinPacking); 3576 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3577 " .aaaaaaaaaaaaaaaaaa();", 3578 NoBinPacking); 3579 verifyFormat("void f() {\n" 3580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3581 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 3582 "}", 3583 NoBinPacking); 3584 3585 verifyFormat( 3586 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3587 " aaaaaaaaaaaa,\n" 3588 " aaaaaaaaaaaa);", 3589 NoBinPacking); 3590 verifyFormat( 3591 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 3592 " ddddddddddddddddddddddddddddd),\n" 3593 " test);", 3594 NoBinPacking); 3595 3596 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 3598 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 3599 " aaaaaaaaaaaaaaaaaa;", 3600 NoBinPacking); 3601 verifyFormat("a(\"a\"\n" 3602 " \"a\",\n" 3603 " a);"); 3604 3605 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3606 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 3607 " aaaaaaaaa,\n" 3608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3609 NoBinPacking); 3610 verifyFormat( 3611 "void f() {\n" 3612 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3613 " .aaaaaaa();\n" 3614 "}", 3615 NoBinPacking); 3616 verifyFormat( 3617 "template <class SomeType, class SomeOtherType>\n" 3618 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 3619 NoBinPacking); 3620 } 3621 3622 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 3623 FormatStyle Style = getLLVMStyleWithColumns(15); 3624 Style.ExperimentalAutoDetectBinPacking = true; 3625 EXPECT_EQ("aaa(aaaa,\n" 3626 " aaaa,\n" 3627 " aaaa);\n" 3628 "aaa(aaaa,\n" 3629 " aaaa,\n" 3630 " aaaa);", 3631 format("aaa(aaaa,\n" // one-per-line 3632 " aaaa,\n" 3633 " aaaa );\n" 3634 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3635 Style)); 3636 EXPECT_EQ("aaa(aaaa, aaaa,\n" 3637 " aaaa);\n" 3638 "aaa(aaaa, aaaa,\n" 3639 " aaaa);", 3640 format("aaa(aaaa, aaaa,\n" // bin-packed 3641 " aaaa );\n" 3642 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3643 Style)); 3644 } 3645 3646 TEST_F(FormatTest, FormatsBuilderPattern) { 3647 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 3648 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 3649 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 3650 " .StartsWith(\".init\", ORDER_INIT)\n" 3651 " .StartsWith(\".fini\", ORDER_FINI)\n" 3652 " .StartsWith(\".hash\", ORDER_HASH)\n" 3653 " .Default(ORDER_TEXT);\n"); 3654 3655 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 3656 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 3657 verifyFormat( 3658 "aaaaaaa->aaaaaaa\n" 3659 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3661 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3662 verifyFormat( 3663 "aaaaaaa->aaaaaaa\n" 3664 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3665 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3666 verifyFormat( 3667 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 3668 " aaaaaaaaaaaaaa);"); 3669 verifyFormat( 3670 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 3671 " aaaaaa->aaaaaaaaaaaa()\n" 3672 " ->aaaaaaaaaaaaaaaa(\n" 3673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3674 " ->aaaaaaaaaaaaaaaaa();"); 3675 verifyGoogleFormat( 3676 "void f() {\n" 3677 " someo->Add((new util::filetools::Handler(dir))\n" 3678 " ->OnEvent1(NewPermanentCallback(\n" 3679 " this, &HandlerHolderClass::EventHandlerCBA))\n" 3680 " ->OnEvent2(NewPermanentCallback(\n" 3681 " this, &HandlerHolderClass::EventHandlerCBB))\n" 3682 " ->OnEvent3(NewPermanentCallback(\n" 3683 " this, &HandlerHolderClass::EventHandlerCBC))\n" 3684 " ->OnEvent5(NewPermanentCallback(\n" 3685 " this, &HandlerHolderClass::EventHandlerCBD))\n" 3686 " ->OnEvent6(NewPermanentCallback(\n" 3687 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 3688 "}"); 3689 3690 verifyFormat( 3691 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 3692 verifyFormat("aaaaaaaaaaaaaaa()\n" 3693 " .aaaaaaaaaaaaaaa()\n" 3694 " .aaaaaaaaaaaaaaa()\n" 3695 " .aaaaaaaaaaaaaaa()\n" 3696 " .aaaaaaaaaaaaaaa();"); 3697 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3698 " .aaaaaaaaaaaaaaa()\n" 3699 " .aaaaaaaaaaaaaaa()\n" 3700 " .aaaaaaaaaaaaaaa();"); 3701 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3702 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3703 " .aaaaaaaaaaaaaaa();"); 3704 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 3705 " ->aaaaaaaaaaaaaae(0)\n" 3706 " ->aaaaaaaaaaaaaaa();"); 3707 3708 // Don't linewrap after very short segments. 3709 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3710 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3711 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3712 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3713 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3714 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3715 verifyFormat("aaa()\n" 3716 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3717 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3718 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3719 3720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3721 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3722 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 3723 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3724 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 3726 3727 // Prefer not to break after empty parentheses. 3728 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 3729 " First->LastNewlineOffset);"); 3730 3731 // Prefer not to create "hanging" indents. 3732 verifyFormat( 3733 "return !soooooooooooooome_map\n" 3734 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3735 " .second;"); 3736 verifyFormat( 3737 "return aaaaaaaaaaaaaaaa\n" 3738 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 3739 " .aaaa(aaaaaaaaaaaaaa);"); 3740 // No hanging indent here. 3741 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 3742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3743 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 3744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3745 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3746 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3747 getLLVMStyleWithColumns(60)); 3748 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 3749 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3750 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3751 getLLVMStyleWithColumns(59)); 3752 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3754 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3755 } 3756 3757 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 3758 verifyFormat( 3759 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3760 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 3761 verifyFormat( 3762 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 3763 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 3764 3765 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3766 " ccccccccccccccccccccccccc) {\n}"); 3767 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 3768 " ccccccccccccccccccccccccc) {\n}"); 3769 3770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3771 " ccccccccccccccccccccccccc) {\n}"); 3772 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 3773 " ccccccccccccccccccccccccc) {\n}"); 3774 3775 verifyFormat( 3776 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 3777 " ccccccccccccccccccccccccc) {\n}"); 3778 verifyFormat( 3779 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 3780 " ccccccccccccccccccccccccc) {\n}"); 3781 3782 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 3783 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 3784 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 3785 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3786 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 3787 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 3788 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 3789 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3790 3791 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 3792 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 3793 " aaaaaaaaaaaaaaa != aa) {\n}"); 3794 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 3795 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 3796 " aaaaaaaaaaaaaaa != aa) {\n}"); 3797 } 3798 3799 TEST_F(FormatTest, BreaksAfterAssignments) { 3800 verifyFormat( 3801 "unsigned Cost =\n" 3802 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 3803 " SI->getPointerAddressSpaceee());\n"); 3804 verifyFormat( 3805 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 3806 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 3807 3808 verifyFormat( 3809 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 3810 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 3811 verifyFormat("unsigned OriginalStartColumn =\n" 3812 " SourceMgr.getSpellingColumnNumber(\n" 3813 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 3814 " 1;"); 3815 } 3816 3817 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 3818 FormatStyle Style = getLLVMStyle(); 3819 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3820 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 3821 Style); 3822 3823 Style.PenaltyBreakAssignment = 20; 3824 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3825 " cccccccccccccccccccccccccc;", 3826 Style); 3827 } 3828 3829 TEST_F(FormatTest, AlignsAfterAssignments) { 3830 verifyFormat( 3831 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3832 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3833 verifyFormat( 3834 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3835 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3836 verifyFormat( 3837 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3838 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3839 verifyFormat( 3840 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3841 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3842 verifyFormat( 3843 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3844 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3845 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 3846 } 3847 3848 TEST_F(FormatTest, AlignsAfterReturn) { 3849 verifyFormat( 3850 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3852 verifyFormat( 3853 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3854 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3855 verifyFormat( 3856 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3857 " aaaaaaaaaaaaaaaaaaaaaa();"); 3858 verifyFormat( 3859 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3860 " aaaaaaaaaaaaaaaaaaaaaa());"); 3861 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3863 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 3865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3866 verifyFormat("return\n" 3867 " // true if code is one of a or b.\n" 3868 " code == a || code == b;"); 3869 } 3870 3871 TEST_F(FormatTest, AlignsAfterOpenBracket) { 3872 verifyFormat( 3873 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3874 " aaaaaaaaa aaaaaaa) {}"); 3875 verifyFormat( 3876 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3877 " aaaaaaaaaaa aaaaaaaaa);"); 3878 verifyFormat( 3879 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3880 " aaaaaaaaaaaaaaaaaaaaa));"); 3881 FormatStyle Style = getLLVMStyle(); 3882 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3883 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3884 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 3885 Style); 3886 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3887 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 3888 Style); 3889 verifyFormat("SomeLongVariableName->someFunction(\n" 3890 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 3891 Style); 3892 verifyFormat( 3893 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3894 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3895 Style); 3896 verifyFormat( 3897 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3898 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3899 Style); 3900 verifyFormat( 3901 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3902 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3903 Style); 3904 3905 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 3906 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 3907 " b));", 3908 Style); 3909 3910 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 3911 Style.BinPackArguments = false; 3912 Style.BinPackParameters = false; 3913 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3914 " aaaaaaaaaaa aaaaaaaa,\n" 3915 " aaaaaaaaa aaaaaaa,\n" 3916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3917 Style); 3918 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3919 " aaaaaaaaaaa aaaaaaaaa,\n" 3920 " aaaaaaaaaaa aaaaaaaaa,\n" 3921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3922 Style); 3923 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 3924 " aaaaaaaaaaaaaaa,\n" 3925 " aaaaaaaaaaaaaaaaaaaaa,\n" 3926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3927 Style); 3928 verifyFormat( 3929 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 3930 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3931 Style); 3932 verifyFormat( 3933 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 3934 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3935 Style); 3936 verifyFormat( 3937 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3938 " aaaaaaaaaaaaaaaaaaaaa(\n" 3939 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 3940 " aaaaaaaaaaaaaaaa);", 3941 Style); 3942 verifyFormat( 3943 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3944 " aaaaaaaaaaaaaaaaaaaaa(\n" 3945 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 3946 " aaaaaaaaaaaaaaaa);", 3947 Style); 3948 } 3949 3950 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 3951 FormatStyle Style = getLLVMStyleWithColumns(40); 3952 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3953 " bbbbbbbbbbbbbbbbbbbbbb);", 3954 Style); 3955 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 3956 Style.AlignOperands = false; 3957 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3958 " bbbbbbbbbbbbbbbbbbbbbb);", 3959 Style); 3960 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3961 Style.AlignOperands = true; 3962 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3963 " bbbbbbbbbbbbbbbbbbbbbb);", 3964 Style); 3965 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3966 Style.AlignOperands = false; 3967 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3968 " bbbbbbbbbbbbbbbbbbbbbb);", 3969 Style); 3970 } 3971 3972 TEST_F(FormatTest, BreaksConditionalExpressions) { 3973 verifyFormat( 3974 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3975 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3976 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3977 verifyFormat( 3978 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 3979 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3980 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3981 verifyFormat( 3982 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3983 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3984 verifyFormat( 3985 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 3986 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3987 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3988 verifyFormat( 3989 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 3990 " : aaaaaaaaaaaaa);"); 3991 verifyFormat( 3992 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3993 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3994 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3995 " aaaaaaaaaaaaa);"); 3996 verifyFormat( 3997 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3998 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3999 " aaaaaaaaaaaaa);"); 4000 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4001 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4003 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4005 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4007 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4009 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4011 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4012 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4014 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4016 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4017 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4018 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4019 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4020 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4022 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4023 " : aaaaaaaaaaaaaaaa;"); 4024 verifyFormat( 4025 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4026 " ? aaaaaaaaaaaaaaa\n" 4027 " : aaaaaaaaaaaaaaa;"); 4028 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4029 " aaaaaaaaa\n" 4030 " ? b\n" 4031 " : c);"); 4032 verifyFormat("return aaaa == bbbb\n" 4033 " // comment\n" 4034 " ? aaaa\n" 4035 " : bbbb;"); 4036 verifyFormat("unsigned Indent =\n" 4037 " format(TheLine.First,\n" 4038 " IndentForLevel[TheLine.Level] >= 0\n" 4039 " ? IndentForLevel[TheLine.Level]\n" 4040 " : TheLine * 2,\n" 4041 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4042 getLLVMStyleWithColumns(60)); 4043 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4044 " ? aaaaaaaaaaaaaaa\n" 4045 " : bbbbbbbbbbbbbbb //\n" 4046 " ? ccccccccccccccc\n" 4047 " : ddddddddddddddd;"); 4048 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4049 " ? aaaaaaaaaaaaaaa\n" 4050 " : (bbbbbbbbbbbbbbb //\n" 4051 " ? ccccccccccccccc\n" 4052 " : ddddddddddddddd);"); 4053 verifyFormat( 4054 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4055 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4056 " aaaaaaaaaaaaaaaaaaaaa +\n" 4057 " aaaaaaaaaaaaaaaaaaaaa\n" 4058 " : aaaaaaaaaa;"); 4059 verifyFormat( 4060 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4061 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4062 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4063 4064 FormatStyle NoBinPacking = getLLVMStyle(); 4065 NoBinPacking.BinPackArguments = false; 4066 verifyFormat( 4067 "void f() {\n" 4068 " g(aaa,\n" 4069 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4071 " ? aaaaaaaaaaaaaaa\n" 4072 " : aaaaaaaaaaaaaaa);\n" 4073 "}", 4074 NoBinPacking); 4075 verifyFormat( 4076 "void f() {\n" 4077 " g(aaa,\n" 4078 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4080 " ?: aaaaaaaaaaaaaaa);\n" 4081 "}", 4082 NoBinPacking); 4083 4084 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4085 " // comment.\n" 4086 " ccccccccccccccccccccccccccccccccccccccc\n" 4087 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4088 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4089 4090 // Assignments in conditional expressions. Apparently not uncommon :-(. 4091 verifyFormat("return a != b\n" 4092 " // comment\n" 4093 " ? a = b\n" 4094 " : a = b;"); 4095 verifyFormat("return a != b\n" 4096 " // comment\n" 4097 " ? a = a != b\n" 4098 " // comment\n" 4099 " ? a = b\n" 4100 " : a\n" 4101 " : a;\n"); 4102 verifyFormat("return a != b\n" 4103 " // comment\n" 4104 " ? a\n" 4105 " : a = a != b\n" 4106 " // comment\n" 4107 " ? a = b\n" 4108 " : a;"); 4109 } 4110 4111 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4112 FormatStyle Style = getLLVMStyle(); 4113 Style.BreakBeforeTernaryOperators = false; 4114 Style.ColumnLimit = 70; 4115 verifyFormat( 4116 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4119 Style); 4120 verifyFormat( 4121 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4122 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4124 Style); 4125 verifyFormat( 4126 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4128 Style); 4129 verifyFormat( 4130 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4131 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4133 Style); 4134 verifyFormat( 4135 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4136 " aaaaaaaaaaaaa);", 4137 Style); 4138 verifyFormat( 4139 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4140 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4142 " aaaaaaaaaaaaa);", 4143 Style); 4144 verifyFormat( 4145 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4146 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4147 " aaaaaaaaaaaaa);", 4148 Style); 4149 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4154 Style); 4155 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4161 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4162 Style); 4163 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4167 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4168 Style); 4169 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4170 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4171 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4172 Style); 4173 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4177 Style); 4178 verifyFormat( 4179 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4180 " aaaaaaaaaaaaaaa :\n" 4181 " aaaaaaaaaaaaaaa;", 4182 Style); 4183 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4184 " aaaaaaaaa ?\n" 4185 " b :\n" 4186 " c);", 4187 Style); 4188 verifyFormat("unsigned Indent =\n" 4189 " format(TheLine.First,\n" 4190 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4191 " IndentForLevel[TheLine.Level] :\n" 4192 " TheLine * 2,\n" 4193 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4194 Style); 4195 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4196 " aaaaaaaaaaaaaaa :\n" 4197 " bbbbbbbbbbbbbbb ? //\n" 4198 " ccccccccccccccc :\n" 4199 " ddddddddddddddd;", 4200 Style); 4201 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4202 " aaaaaaaaaaaaaaa :\n" 4203 " (bbbbbbbbbbbbbbb ? //\n" 4204 " ccccccccccccccc :\n" 4205 " ddddddddddddddd);", 4206 Style); 4207 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4208 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4209 " ccccccccccccccccccccccccccc;", 4210 Style); 4211 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4212 " aaaaa :\n" 4213 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4214 Style); 4215 } 4216 4217 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4218 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4219 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4220 verifyFormat("bool a = true, b = false;"); 4221 4222 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4224 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4225 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4226 verifyFormat( 4227 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4228 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4229 " d = e && f;"); 4230 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4231 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4232 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4233 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4234 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4235 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4236 4237 FormatStyle Style = getGoogleStyle(); 4238 Style.PointerAlignment = FormatStyle::PAS_Left; 4239 Style.DerivePointerAlignment = false; 4240 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4241 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4242 " *b = bbbbbbbbbbbbbbbbbbb;", 4243 Style); 4244 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4245 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4246 Style); 4247 verifyFormat("vector<int*> a, b;", Style); 4248 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4249 } 4250 4251 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4252 verifyFormat("arr[foo ? bar : baz];"); 4253 verifyFormat("f()[foo ? bar : baz];"); 4254 verifyFormat("(a + b)[foo ? bar : baz];"); 4255 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4256 } 4257 4258 TEST_F(FormatTest, AlignsStringLiterals) { 4259 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4260 " \"short literal\");"); 4261 verifyFormat( 4262 "looooooooooooooooooooooooongFunction(\n" 4263 " \"short literal\"\n" 4264 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4265 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4266 " \" string literals\",\n" 4267 " and, other, parameters);"); 4268 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4269 " \"5678\";", 4270 format("fun + \"1243\" /* comment */\n" 4271 " \"5678\";", 4272 getLLVMStyleWithColumns(28))); 4273 EXPECT_EQ( 4274 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4275 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4276 " \"aaaaaaaaaaaaaaaa\";", 4277 format("aaaaaa =" 4278 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4279 "aaaaaaaaaaaaaaaaaaaaa\" " 4280 "\"aaaaaaaaaaaaaaaa\";")); 4281 verifyFormat("a = a + \"a\"\n" 4282 " \"a\"\n" 4283 " \"a\";"); 4284 verifyFormat("f(\"a\", \"b\"\n" 4285 " \"c\");"); 4286 4287 verifyFormat( 4288 "#define LL_FORMAT \"ll\"\n" 4289 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4290 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4291 4292 verifyFormat("#define A(X) \\\n" 4293 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4294 " \"ccccc\"", 4295 getLLVMStyleWithColumns(23)); 4296 verifyFormat("#define A \"def\"\n" 4297 "f(\"abc\" A \"ghi\"\n" 4298 " \"jkl\");"); 4299 4300 verifyFormat("f(L\"a\"\n" 4301 " L\"b\");"); 4302 verifyFormat("#define A(X) \\\n" 4303 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4304 " L\"ccccc\"", 4305 getLLVMStyleWithColumns(25)); 4306 4307 verifyFormat("f(@\"a\"\n" 4308 " @\"b\");"); 4309 verifyFormat("NSString s = @\"a\"\n" 4310 " @\"b\"\n" 4311 " @\"c\";"); 4312 verifyFormat("NSString s = @\"a\"\n" 4313 " \"b\"\n" 4314 " \"c\";"); 4315 } 4316 4317 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4318 FormatStyle Style = getLLVMStyle(); 4319 // No declarations or definitions should be moved to own line. 4320 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4321 verifyFormat("class A {\n" 4322 " int f() { return 1; }\n" 4323 " int g();\n" 4324 "};\n" 4325 "int f() { return 1; }\n" 4326 "int g();\n", 4327 Style); 4328 4329 // All declarations and definitions should have the return type moved to its 4330 // own 4331 // line. 4332 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4333 verifyFormat("class E {\n" 4334 " int\n" 4335 " f() {\n" 4336 " return 1;\n" 4337 " }\n" 4338 " int\n" 4339 " g();\n" 4340 "};\n" 4341 "int\n" 4342 "f() {\n" 4343 " return 1;\n" 4344 "}\n" 4345 "int\n" 4346 "g();\n", 4347 Style); 4348 4349 // Top-level definitions, and no kinds of declarations should have the 4350 // return type moved to its own line. 4351 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4352 verifyFormat("class B {\n" 4353 " int f() { return 1; }\n" 4354 " int g();\n" 4355 "};\n" 4356 "int\n" 4357 "f() {\n" 4358 " return 1;\n" 4359 "}\n" 4360 "int g();\n", 4361 Style); 4362 4363 // Top-level definitions and declarations should have the return type moved 4364 // to its own line. 4365 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4366 verifyFormat("class C {\n" 4367 " int f() { return 1; }\n" 4368 " int g();\n" 4369 "};\n" 4370 "int\n" 4371 "f() {\n" 4372 " return 1;\n" 4373 "}\n" 4374 "int\n" 4375 "g();\n", 4376 Style); 4377 4378 // All definitions should have the return type moved to its own line, but no 4379 // kinds of declarations. 4380 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4381 verifyFormat("class D {\n" 4382 " int\n" 4383 " f() {\n" 4384 " return 1;\n" 4385 " }\n" 4386 " int g();\n" 4387 "};\n" 4388 "int\n" 4389 "f() {\n" 4390 " return 1;\n" 4391 "}\n" 4392 "int g();\n", 4393 Style); 4394 verifyFormat("const char *\n" 4395 "f(void) {\n" // Break here. 4396 " return \"\";\n" 4397 "}\n" 4398 "const char *bar(void);\n", // No break here. 4399 Style); 4400 verifyFormat("template <class T>\n" 4401 "T *\n" 4402 "f(T &c) {\n" // Break here. 4403 " return NULL;\n" 4404 "}\n" 4405 "template <class T> T *f(T &c);\n", // No break here. 4406 Style); 4407 verifyFormat("class C {\n" 4408 " int\n" 4409 " operator+() {\n" 4410 " return 1;\n" 4411 " }\n" 4412 " int\n" 4413 " operator()() {\n" 4414 " return 1;\n" 4415 " }\n" 4416 "};\n", 4417 Style); 4418 verifyFormat("void\n" 4419 "A::operator()() {}\n" 4420 "void\n" 4421 "A::operator>>() {}\n" 4422 "void\n" 4423 "A::operator+() {}\n", 4424 Style); 4425 verifyFormat("void *operator new(std::size_t s);", // No break here. 4426 Style); 4427 verifyFormat("void *\n" 4428 "operator new(std::size_t s) {}", 4429 Style); 4430 verifyFormat("void *\n" 4431 "operator delete[](void *ptr) {}", 4432 Style); 4433 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4434 verifyFormat("const char *\n" 4435 "f(void)\n" // Break here. 4436 "{\n" 4437 " return \"\";\n" 4438 "}\n" 4439 "const char *bar(void);\n", // No break here. 4440 Style); 4441 verifyFormat("template <class T>\n" 4442 "T *\n" // Problem here: no line break 4443 "f(T &c)\n" // Break here. 4444 "{\n" 4445 " return NULL;\n" 4446 "}\n" 4447 "template <class T> T *f(T &c);\n", // No break here. 4448 Style); 4449 } 4450 4451 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4452 FormatStyle NoBreak = getLLVMStyle(); 4453 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4454 FormatStyle Break = getLLVMStyle(); 4455 Break.AlwaysBreakBeforeMultilineStrings = true; 4456 verifyFormat("aaaa = \"bbbb\"\n" 4457 " \"cccc\";", 4458 NoBreak); 4459 verifyFormat("aaaa =\n" 4460 " \"bbbb\"\n" 4461 " \"cccc\";", 4462 Break); 4463 verifyFormat("aaaa(\"bbbb\"\n" 4464 " \"cccc\");", 4465 NoBreak); 4466 verifyFormat("aaaa(\n" 4467 " \"bbbb\"\n" 4468 " \"cccc\");", 4469 Break); 4470 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4471 " \"cccc\");", 4472 NoBreak); 4473 verifyFormat("aaaa(qqq,\n" 4474 " \"bbbb\"\n" 4475 " \"cccc\");", 4476 Break); 4477 verifyFormat("aaaa(qqq,\n" 4478 " L\"bbbb\"\n" 4479 " L\"cccc\");", 4480 Break); 4481 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4482 " \"bbbb\"));", 4483 Break); 4484 verifyFormat("string s = someFunction(\n" 4485 " \"abc\"\n" 4486 " \"abc\");", 4487 Break); 4488 4489 // As we break before unary operators, breaking right after them is bad. 4490 verifyFormat("string foo = abc ? \"x\"\n" 4491 " \"blah blah blah blah blah blah\"\n" 4492 " : \"y\";", 4493 Break); 4494 4495 // Don't break if there is no column gain. 4496 verifyFormat("f(\"aaaa\"\n" 4497 " \"bbbb\");", 4498 Break); 4499 4500 // Treat literals with escaped newlines like multi-line string literals. 4501 EXPECT_EQ("x = \"a\\\n" 4502 "b\\\n" 4503 "c\";", 4504 format("x = \"a\\\n" 4505 "b\\\n" 4506 "c\";", 4507 NoBreak)); 4508 EXPECT_EQ("xxxx =\n" 4509 " \"a\\\n" 4510 "b\\\n" 4511 "c\";", 4512 format("xxxx = \"a\\\n" 4513 "b\\\n" 4514 "c\";", 4515 Break)); 4516 4517 EXPECT_EQ("NSString *const kString =\n" 4518 " @\"aaaa\"\n" 4519 " @\"bbbb\";", 4520 format("NSString *const kString = @\"aaaa\"\n" 4521 "@\"bbbb\";", 4522 Break)); 4523 4524 Break.ColumnLimit = 0; 4525 verifyFormat("const char *hello = \"hello llvm\";", Break); 4526 } 4527 4528 TEST_F(FormatTest, AlignsPipes) { 4529 verifyFormat( 4530 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4531 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4532 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4533 verifyFormat( 4534 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4535 " << aaaaaaaaaaaaaaaaaaaa;"); 4536 verifyFormat( 4537 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4538 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4539 verifyFormat( 4540 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4541 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4542 verifyFormat( 4543 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4544 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4545 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4546 verifyFormat( 4547 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4548 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4549 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4550 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4553 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4554 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4555 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4556 verifyFormat( 4557 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4559 verifyFormat( 4560 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 4561 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4562 4563 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4564 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4565 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4567 " aaaaaaaaaaaaaaaaaaaaa)\n" 4568 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4569 verifyFormat("LOG_IF(aaa == //\n" 4570 " bbb)\n" 4571 " << a << b;"); 4572 4573 // But sometimes, breaking before the first "<<" is desirable. 4574 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4575 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4576 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4577 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4578 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4579 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4580 " << BEF << IsTemplate << Description << E->getType();"); 4581 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4582 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4584 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4585 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4587 " << aaa;"); 4588 4589 verifyFormat( 4590 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4591 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4592 4593 // Incomplete string literal. 4594 EXPECT_EQ("llvm::errs() << \"\n" 4595 " << a;", 4596 format("llvm::errs() << \"\n<<a;")); 4597 4598 verifyFormat("void f() {\n" 4599 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4600 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4601 "}"); 4602 4603 // Handle 'endl'. 4604 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4605 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4606 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4607 4608 // Handle '\n'. 4609 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 4610 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4611 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 4612 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 4613 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 4614 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 4615 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4616 } 4617 4618 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 4619 verifyFormat("return out << \"somepacket = {\\n\"\n" 4620 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4621 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4622 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4623 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4624 " << \"}\";"); 4625 4626 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4627 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4628 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4629 verifyFormat( 4630 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4631 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4632 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4633 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4634 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4635 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4636 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4637 verifyFormat( 4638 "void f() {\n" 4639 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4640 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4641 "}"); 4642 4643 // Breaking before the first "<<" is generally not desirable. 4644 verifyFormat( 4645 "llvm::errs()\n" 4646 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4647 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4648 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4649 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4650 getLLVMStyleWithColumns(70)); 4651 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4652 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4653 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4654 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4655 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4656 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4657 getLLVMStyleWithColumns(70)); 4658 4659 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4660 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4661 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 4662 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4663 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4664 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 4665 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 4666 " (aaaa + aaaa);", 4667 getLLVMStyleWithColumns(40)); 4668 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 4669 " (aaaaaaa + aaaaa));", 4670 getLLVMStyleWithColumns(40)); 4671 verifyFormat( 4672 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 4673 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 4674 " bbbbbbbbbbbbbbbbbbbbbbb);"); 4675 } 4676 4677 TEST_F(FormatTest, UnderstandsEquals) { 4678 verifyFormat( 4679 "aaaaaaaaaaaaaaaaa =\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4681 verifyFormat( 4682 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4684 verifyFormat( 4685 "if (a) {\n" 4686 " f();\n" 4687 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4689 "}"); 4690 4691 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4692 " 100000000 + 10000000) {\n}"); 4693 } 4694 4695 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4696 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4697 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4698 4699 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4700 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4701 4702 verifyFormat( 4703 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4704 " Parameter2);"); 4705 4706 verifyFormat( 4707 "ShortObject->shortFunction(\n" 4708 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4709 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4710 4711 verifyFormat("loooooooooooooongFunction(\n" 4712 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4713 4714 verifyFormat( 4715 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4716 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4717 4718 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4719 " .WillRepeatedly(Return(SomeValue));"); 4720 verifyFormat("void f() {\n" 4721 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4722 " .Times(2)\n" 4723 " .WillRepeatedly(Return(SomeValue));\n" 4724 "}"); 4725 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 4726 " ccccccccccccccccccccccc);"); 4727 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4729 " .aaaaa(aaaaa),\n" 4730 " aaaaaaaaaaaaaaaaaaaaa);"); 4731 verifyFormat("void f() {\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4733 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 4734 "}"); 4735 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4737 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4738 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4739 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4740 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4741 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4742 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4743 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 4744 "}"); 4745 4746 // Here, it is not necessary to wrap at "." or "->". 4747 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 4748 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4749 verifyFormat( 4750 "aaaaaaaaaaa->aaaaaaaaa(\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4752 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 4753 4754 verifyFormat( 4755 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 4757 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 4758 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4759 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 4760 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4761 4762 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4764 " .a();"); 4765 4766 FormatStyle NoBinPacking = getLLVMStyle(); 4767 NoBinPacking.BinPackParameters = false; 4768 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4769 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4770 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 4771 " aaaaaaaaaaaaaaaaaaa,\n" 4772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4773 NoBinPacking); 4774 4775 // If there is a subsequent call, change to hanging indentation. 4776 verifyFormat( 4777 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4778 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 4779 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4780 verifyFormat( 4781 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4782 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 4783 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4785 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4786 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4788 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4789 } 4790 4791 TEST_F(FormatTest, WrapsTemplateDeclarations) { 4792 verifyFormat("template <typename T>\n" 4793 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4794 verifyFormat("template <typename T>\n" 4795 "// T should be one of {A, B}.\n" 4796 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4797 verifyFormat( 4798 "template <typename T>\n" 4799 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 4800 verifyFormat("template <typename T>\n" 4801 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 4802 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 4803 verifyFormat( 4804 "template <typename T>\n" 4805 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 4806 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 4807 verifyFormat( 4808 "template <typename T>\n" 4809 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 4810 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 4811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4812 verifyFormat("template <typename T>\n" 4813 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4814 " int aaaaaaaaaaaaaaaaaaaaaa);"); 4815 verifyFormat( 4816 "template <typename T1, typename T2 = char, typename T3 = char,\n" 4817 " typename T4 = char>\n" 4818 "void f();"); 4819 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 4820 " template <typename> class cccccccccccccccccccccc,\n" 4821 " typename ddddddddddddd>\n" 4822 "class C {};"); 4823 verifyFormat( 4824 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 4825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4826 4827 verifyFormat("void f() {\n" 4828 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 4829 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 4830 "}"); 4831 4832 verifyFormat("template <typename T> class C {};"); 4833 verifyFormat("template <typename T> void f();"); 4834 verifyFormat("template <typename T> void f() {}"); 4835 verifyFormat( 4836 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 4839 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 4842 " bbbbbbbbbbbbbbbbbbbbbbbb);", 4843 getLLVMStyleWithColumns(72)); 4844 EXPECT_EQ("static_cast<A< //\n" 4845 " B> *>(\n" 4846 "\n" 4847 ");", 4848 format("static_cast<A<//\n" 4849 " B>*>(\n" 4850 "\n" 4851 " );")); 4852 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4853 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 4854 4855 FormatStyle AlwaysBreak = getLLVMStyle(); 4856 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 4857 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 4858 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 4859 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 4860 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4861 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 4862 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 4863 verifyFormat("template <template <typename> class Fooooooo,\n" 4864 " template <typename> class Baaaaaaar>\n" 4865 "struct C {};", 4866 AlwaysBreak); 4867 verifyFormat("template <typename T> // T can be A, B or C.\n" 4868 "struct C {};", 4869 AlwaysBreak); 4870 verifyFormat("template <enum E> class A {\n" 4871 "public:\n" 4872 " E *f();\n" 4873 "};"); 4874 } 4875 4876 TEST_F(FormatTest, WrapsTemplateParameters) { 4877 FormatStyle Style = getLLVMStyle(); 4878 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4879 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4880 verifyFormat( 4881 "template <typename... a> struct q {};\n" 4882 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4883 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4884 " y;", 4885 Style); 4886 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4887 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4888 verifyFormat( 4889 "template <typename... a> struct r {};\n" 4890 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4891 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4892 " y;", 4893 Style); 4894 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4895 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4896 verifyFormat( 4897 "template <typename... a> struct s {};\n" 4898 "extern s<\n" 4899 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4900 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4901 " y;", 4902 Style); 4903 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4904 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4905 verifyFormat( 4906 "template <typename... a> struct t {};\n" 4907 "extern t<\n" 4908 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4909 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4910 " y;", 4911 Style); 4912 } 4913 4914 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 4915 verifyFormat( 4916 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4918 verifyFormat( 4919 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4922 4923 // FIXME: Should we have the extra indent after the second break? 4924 verifyFormat( 4925 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4928 4929 verifyFormat( 4930 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 4931 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 4932 4933 // Breaking at nested name specifiers is generally not desirable. 4934 verifyFormat( 4935 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4936 " aaaaaaaaaaaaaaaaaaaaaaa);"); 4937 4938 verifyFormat( 4939 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 4940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4942 " aaaaaaaaaaaaaaaaaaaaa);", 4943 getLLVMStyleWithColumns(74)); 4944 4945 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4947 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4948 } 4949 4950 TEST_F(FormatTest, UnderstandsTemplateParameters) { 4951 verifyFormat("A<int> a;"); 4952 verifyFormat("A<A<A<int>>> a;"); 4953 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 4954 verifyFormat("bool x = a < 1 || 2 > a;"); 4955 verifyFormat("bool x = 5 < f<int>();"); 4956 verifyFormat("bool x = f<int>() > 5;"); 4957 verifyFormat("bool x = 5 < a<int>::x;"); 4958 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 4959 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 4960 4961 verifyGoogleFormat("A<A<int>> a;"); 4962 verifyGoogleFormat("A<A<A<int>>> a;"); 4963 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 4964 verifyGoogleFormat("A<A<int> > a;"); 4965 verifyGoogleFormat("A<A<A<int> > > a;"); 4966 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 4967 verifyGoogleFormat("A<::A<int>> a;"); 4968 verifyGoogleFormat("A<::A> a;"); 4969 verifyGoogleFormat("A< ::A> a;"); 4970 verifyGoogleFormat("A< ::A<int> > a;"); 4971 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 4972 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 4973 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 4974 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 4975 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 4976 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 4977 4978 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 4979 4980 verifyFormat("test >> a >> b;"); 4981 verifyFormat("test << a >> b;"); 4982 4983 verifyFormat("f<int>();"); 4984 verifyFormat("template <typename T> void f() {}"); 4985 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 4986 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 4987 "sizeof(char)>::type>;"); 4988 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 4989 verifyFormat("f(a.operator()<A>());"); 4990 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4991 " .template operator()<A>());", 4992 getLLVMStyleWithColumns(35)); 4993 4994 // Not template parameters. 4995 verifyFormat("return a < b && c > d;"); 4996 verifyFormat("void f() {\n" 4997 " while (a < b && c > d) {\n" 4998 " }\n" 4999 "}"); 5000 verifyFormat("template <typename... Types>\n" 5001 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5002 5003 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5005 getLLVMStyleWithColumns(60)); 5006 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5007 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5008 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5009 } 5010 5011 TEST_F(FormatTest, BitshiftOperatorWidth) { 5012 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5013 " bar */", 5014 format("int a=1<<2; /* foo\n" 5015 " bar */")); 5016 5017 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5018 " bar */", 5019 format("int b =256>>1 ; /* foo\n" 5020 " bar */")); 5021 } 5022 5023 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5024 verifyFormat("COMPARE(a, ==, b);"); 5025 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5026 } 5027 5028 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5029 verifyFormat("int A::*x;"); 5030 verifyFormat("int (S::*func)(void *);"); 5031 verifyFormat("void f() { int (S::*func)(void *); }"); 5032 verifyFormat("typedef bool *(Class::*Member)() const;"); 5033 verifyFormat("void f() {\n" 5034 " (a->*f)();\n" 5035 " a->*x;\n" 5036 " (a.*f)();\n" 5037 " ((*a).*f)();\n" 5038 " a.*x;\n" 5039 "}"); 5040 verifyFormat("void f() {\n" 5041 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5042 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5043 "}"); 5044 verifyFormat( 5045 "(aaaaaaaaaa->*bbbbbbb)(\n" 5046 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5047 FormatStyle Style = getLLVMStyle(); 5048 Style.PointerAlignment = FormatStyle::PAS_Left; 5049 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5050 } 5051 5052 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5053 verifyFormat("int a = -2;"); 5054 verifyFormat("f(-1, -2, -3);"); 5055 verifyFormat("a[-1] = 5;"); 5056 verifyFormat("int a = 5 + -2;"); 5057 verifyFormat("if (i == -1) {\n}"); 5058 verifyFormat("if (i != -1) {\n}"); 5059 verifyFormat("if (i > -1) {\n}"); 5060 verifyFormat("if (i < -1) {\n}"); 5061 verifyFormat("++(a->f());"); 5062 verifyFormat("--(a->f());"); 5063 verifyFormat("(a->f())++;"); 5064 verifyFormat("a[42]++;"); 5065 verifyFormat("if (!(a->f())) {\n}"); 5066 5067 verifyFormat("a-- > b;"); 5068 verifyFormat("b ? -a : c;"); 5069 verifyFormat("n * sizeof char16;"); 5070 verifyFormat("n * alignof char16;", getGoogleStyle()); 5071 verifyFormat("sizeof(char);"); 5072 verifyFormat("alignof(char);", getGoogleStyle()); 5073 5074 verifyFormat("return -1;"); 5075 verifyFormat("switch (a) {\n" 5076 "case -1:\n" 5077 " break;\n" 5078 "}"); 5079 verifyFormat("#define X -1"); 5080 verifyFormat("#define X -kConstant"); 5081 5082 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5083 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5084 5085 verifyFormat("int a = /* confusing comment */ -1;"); 5086 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5087 verifyFormat("int a = i /* confusing comment */++;"); 5088 } 5089 5090 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5091 verifyFormat("if (!aaaaaaaaaa( // break\n" 5092 " aaaaa)) {\n" 5093 "}"); 5094 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5095 " aaaaa));"); 5096 verifyFormat("*aaa = aaaaaaa( // break\n" 5097 " bbbbbb);"); 5098 } 5099 5100 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5101 verifyFormat("bool operator<();"); 5102 verifyFormat("bool operator>();"); 5103 verifyFormat("bool operator=();"); 5104 verifyFormat("bool operator==();"); 5105 verifyFormat("bool operator!=();"); 5106 verifyFormat("int operator+();"); 5107 verifyFormat("int operator++();"); 5108 verifyFormat("bool operator,();"); 5109 verifyFormat("bool operator();"); 5110 verifyFormat("bool operator()();"); 5111 verifyFormat("bool operator[]();"); 5112 verifyFormat("operator bool();"); 5113 verifyFormat("operator int();"); 5114 verifyFormat("operator void *();"); 5115 verifyFormat("operator SomeType<int>();"); 5116 verifyFormat("operator SomeType<int, int>();"); 5117 verifyFormat("operator SomeType<SomeType<int>>();"); 5118 verifyFormat("void *operator new(std::size_t size);"); 5119 verifyFormat("void *operator new[](std::size_t size);"); 5120 verifyFormat("void operator delete(void *ptr);"); 5121 verifyFormat("void operator delete[](void *ptr);"); 5122 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5123 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5124 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5125 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5126 5127 verifyFormat( 5128 "ostream &operator<<(ostream &OutputStream,\n" 5129 " SomeReallyLongType WithSomeReallyLongValue);"); 5130 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5131 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5132 " return left.group < right.group;\n" 5133 "}"); 5134 verifyFormat("SomeType &operator=(const SomeType &S);"); 5135 verifyFormat("f.template operator()<int>();"); 5136 5137 verifyGoogleFormat("operator void*();"); 5138 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5139 verifyGoogleFormat("operator ::A();"); 5140 5141 verifyFormat("using A::operator+;"); 5142 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5143 "int i;"); 5144 } 5145 5146 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5147 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5148 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5149 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5150 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5151 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5152 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5153 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5154 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5155 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5156 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5157 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5158 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5159 verifyFormat("template <typename T>\n" 5160 "void F(T) && = delete;", 5161 getGoogleStyle()); 5162 5163 FormatStyle AlignLeft = getLLVMStyle(); 5164 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5165 verifyFormat("void A::b() && {}", AlignLeft); 5166 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5167 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5168 AlignLeft); 5169 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5170 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5171 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5172 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5173 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5174 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5175 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5176 5177 FormatStyle Spaces = getLLVMStyle(); 5178 Spaces.SpacesInCStyleCastParentheses = true; 5179 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5180 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5181 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5182 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5183 5184 Spaces.SpacesInCStyleCastParentheses = false; 5185 Spaces.SpacesInParentheses = true; 5186 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5187 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5188 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5189 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5190 } 5191 5192 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5193 verifyFormat("void f() {\n" 5194 " A *a = new A;\n" 5195 " A *a = new (placement) A;\n" 5196 " delete a;\n" 5197 " delete (A *)a;\n" 5198 "}"); 5199 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5200 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5201 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5202 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5203 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5204 verifyFormat("delete[] h->p;"); 5205 } 5206 5207 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5208 verifyFormat("int *f(int *a) {}"); 5209 verifyFormat("int main(int argc, char **argv) {}"); 5210 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5211 verifyIndependentOfContext("f(a, *a);"); 5212 verifyFormat("void g() { f(*a); }"); 5213 verifyIndependentOfContext("int a = b * 10;"); 5214 verifyIndependentOfContext("int a = 10 * b;"); 5215 verifyIndependentOfContext("int a = b * c;"); 5216 verifyIndependentOfContext("int a += b * c;"); 5217 verifyIndependentOfContext("int a -= b * c;"); 5218 verifyIndependentOfContext("int a *= b * c;"); 5219 verifyIndependentOfContext("int a /= b * c;"); 5220 verifyIndependentOfContext("int a = *b;"); 5221 verifyIndependentOfContext("int a = *b * c;"); 5222 verifyIndependentOfContext("int a = b * *c;"); 5223 verifyIndependentOfContext("int a = b * (10);"); 5224 verifyIndependentOfContext("S << b * (10);"); 5225 verifyIndependentOfContext("return 10 * b;"); 5226 verifyIndependentOfContext("return *b * *c;"); 5227 verifyIndependentOfContext("return a & ~b;"); 5228 verifyIndependentOfContext("f(b ? *c : *d);"); 5229 verifyIndependentOfContext("int a = b ? *c : *d;"); 5230 verifyIndependentOfContext("*b = a;"); 5231 verifyIndependentOfContext("a * ~b;"); 5232 verifyIndependentOfContext("a * !b;"); 5233 verifyIndependentOfContext("a * +b;"); 5234 verifyIndependentOfContext("a * -b;"); 5235 verifyIndependentOfContext("a * ++b;"); 5236 verifyIndependentOfContext("a * --b;"); 5237 verifyIndependentOfContext("a[4] * b;"); 5238 verifyIndependentOfContext("a[a * a] = 1;"); 5239 verifyIndependentOfContext("f() * b;"); 5240 verifyIndependentOfContext("a * [self dostuff];"); 5241 verifyIndependentOfContext("int x = a * (a + b);"); 5242 verifyIndependentOfContext("(a *)(a + b);"); 5243 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5244 verifyIndependentOfContext("int *pa = (int *)&a;"); 5245 verifyIndependentOfContext("return sizeof(int **);"); 5246 verifyIndependentOfContext("return sizeof(int ******);"); 5247 verifyIndependentOfContext("return (int **&)a;"); 5248 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5249 verifyFormat("void f(Type (*parameter)[10]) {}"); 5250 verifyFormat("void f(Type (¶meter)[10]) {}"); 5251 verifyGoogleFormat("return sizeof(int**);"); 5252 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5253 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5254 verifyFormat("auto a = [](int **&, int ***) {};"); 5255 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5256 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5257 verifyFormat("[](const decltype(*a) &value) {}"); 5258 verifyFormat("decltype(a * b) F();"); 5259 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5260 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5261 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5262 verifyIndependentOfContext("int i{a * b};"); 5263 verifyIndependentOfContext("aaa && aaa->f();"); 5264 verifyIndependentOfContext("int x = ~*p;"); 5265 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5266 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5267 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5268 verifyFormat("void f() { f(a, c * d); }"); 5269 verifyFormat("void f() { f(new a(), c * d); }"); 5270 verifyFormat("void f(const MyOverride &override);"); 5271 verifyFormat("void f(const MyFinal &final);"); 5272 verifyIndependentOfContext("bool a = f() && override.f();"); 5273 verifyIndependentOfContext("bool a = f() && final.f();"); 5274 5275 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5276 5277 verifyIndependentOfContext("A<int *> a;"); 5278 verifyIndependentOfContext("A<int **> a;"); 5279 verifyIndependentOfContext("A<int *, int *> a;"); 5280 verifyIndependentOfContext("A<int *[]> a;"); 5281 verifyIndependentOfContext( 5282 "const char *const p = reinterpret_cast<const char *const>(q);"); 5283 verifyIndependentOfContext("A<int **, int **> a;"); 5284 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5285 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5286 verifyFormat("for (; a && b;) {\n}"); 5287 verifyFormat("bool foo = true && [] { return false; }();"); 5288 5289 verifyFormat( 5290 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5292 5293 verifyGoogleFormat("int const* a = &b;"); 5294 verifyGoogleFormat("**outparam = 1;"); 5295 verifyGoogleFormat("*outparam = a * b;"); 5296 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5297 verifyGoogleFormat("A<int*> a;"); 5298 verifyGoogleFormat("A<int**> a;"); 5299 verifyGoogleFormat("A<int*, int*> a;"); 5300 verifyGoogleFormat("A<int**, int**> a;"); 5301 verifyGoogleFormat("f(b ? *c : *d);"); 5302 verifyGoogleFormat("int a = b ? *c : *d;"); 5303 verifyGoogleFormat("Type* t = **x;"); 5304 verifyGoogleFormat("Type* t = *++*x;"); 5305 verifyGoogleFormat("*++*x;"); 5306 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5307 verifyGoogleFormat("Type* t = x++ * y;"); 5308 verifyGoogleFormat( 5309 "const char* const p = reinterpret_cast<const char* const>(q);"); 5310 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5311 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5312 verifyGoogleFormat("template <typename T>\n" 5313 "void f(int i = 0, SomeType** temps = NULL);"); 5314 5315 FormatStyle Left = getLLVMStyle(); 5316 Left.PointerAlignment = FormatStyle::PAS_Left; 5317 verifyFormat("x = *a(x) = *a(y);", Left); 5318 verifyFormat("for (;; *a = b) {\n}", Left); 5319 verifyFormat("return *this += 1;", Left); 5320 5321 verifyIndependentOfContext("a = *(x + y);"); 5322 verifyIndependentOfContext("a = &(x + y);"); 5323 verifyIndependentOfContext("*(x + y).call();"); 5324 verifyIndependentOfContext("&(x + y)->call();"); 5325 verifyFormat("void f() { &(*I).first; }"); 5326 5327 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5328 verifyFormat( 5329 "int *MyValues = {\n" 5330 " *A, // Operator detection might be confused by the '{'\n" 5331 " *BB // Operator detection might be confused by previous comment\n" 5332 "};"); 5333 5334 verifyIndependentOfContext("if (int *a = &b)"); 5335 verifyIndependentOfContext("if (int &a = *b)"); 5336 verifyIndependentOfContext("if (a & b[i])"); 5337 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5338 verifyIndependentOfContext("if (*b[i])"); 5339 verifyIndependentOfContext("if (int *a = (&b))"); 5340 verifyIndependentOfContext("while (int *a = &b)"); 5341 verifyIndependentOfContext("size = sizeof *a;"); 5342 verifyIndependentOfContext("if (a && (b = c))"); 5343 verifyFormat("void f() {\n" 5344 " for (const int &v : Values) {\n" 5345 " }\n" 5346 "}"); 5347 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5348 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5349 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5350 5351 verifyFormat("#define A (!a * b)"); 5352 verifyFormat("#define MACRO \\\n" 5353 " int *i = a * b; \\\n" 5354 " void f(a *b);", 5355 getLLVMStyleWithColumns(19)); 5356 5357 verifyIndependentOfContext("A = new SomeType *[Length];"); 5358 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5359 verifyIndependentOfContext("T **t = new T *;"); 5360 verifyIndependentOfContext("T **t = new T *();"); 5361 verifyGoogleFormat("A = new SomeType*[Length]();"); 5362 verifyGoogleFormat("A = new SomeType*[Length];"); 5363 verifyGoogleFormat("T** t = new T*;"); 5364 verifyGoogleFormat("T** t = new T*();"); 5365 5366 FormatStyle PointerLeft = getLLVMStyle(); 5367 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5368 verifyFormat("delete *x;", PointerLeft); 5369 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5370 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5371 verifyFormat("template <bool a, bool b> " 5372 "typename t::if<x && y>::type f() {}"); 5373 verifyFormat("template <int *y> f() {}"); 5374 verifyFormat("vector<int *> v;"); 5375 verifyFormat("vector<int *const> v;"); 5376 verifyFormat("vector<int *const **const *> v;"); 5377 verifyFormat("vector<int *volatile> v;"); 5378 verifyFormat("vector<a * b> v;"); 5379 verifyFormat("foo<b && false>();"); 5380 verifyFormat("foo<b & 1>();"); 5381 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5382 verifyFormat( 5383 "template <class T, class = typename std::enable_if<\n" 5384 " std::is_integral<T>::value &&\n" 5385 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5386 "void F();", 5387 getLLVMStyleWithColumns(70)); 5388 verifyFormat( 5389 "template <class T,\n" 5390 " class = typename std::enable_if<\n" 5391 " std::is_integral<T>::value &&\n" 5392 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5393 " class U>\n" 5394 "void F();", 5395 getLLVMStyleWithColumns(70)); 5396 verifyFormat( 5397 "template <class T,\n" 5398 " class = typename ::std::enable_if<\n" 5399 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5400 "void F();", 5401 getGoogleStyleWithColumns(68)); 5402 5403 verifyIndependentOfContext("MACRO(int *i);"); 5404 verifyIndependentOfContext("MACRO(auto *a);"); 5405 verifyIndependentOfContext("MACRO(const A *a);"); 5406 verifyIndependentOfContext("MACRO(A *const a);"); 5407 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5408 verifyFormat("void f() { f(float{1}, a * a); }"); 5409 // FIXME: Is there a way to make this work? 5410 // verifyIndependentOfContext("MACRO(A *a);"); 5411 5412 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5413 verifyFormat("return options != nullptr && operator==(*options);"); 5414 5415 EXPECT_EQ("#define OP(x) \\\n" 5416 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5417 " return s << a.DebugString(); \\\n" 5418 " }", 5419 format("#define OP(x) \\\n" 5420 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5421 " return s << a.DebugString(); \\\n" 5422 " }", 5423 getLLVMStyleWithColumns(50))); 5424 5425 // FIXME: We cannot handle this case yet; we might be able to figure out that 5426 // foo<x> d > v; doesn't make sense. 5427 verifyFormat("foo<a<b && c> d> v;"); 5428 5429 FormatStyle PointerMiddle = getLLVMStyle(); 5430 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5431 verifyFormat("delete *x;", PointerMiddle); 5432 verifyFormat("int * x;", PointerMiddle); 5433 verifyFormat("template <int * y> f() {}", PointerMiddle); 5434 verifyFormat("int * f(int * a) {}", PointerMiddle); 5435 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5436 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5437 verifyFormat("A<int *> a;", PointerMiddle); 5438 verifyFormat("A<int **> a;", PointerMiddle); 5439 verifyFormat("A<int *, int *> a;", PointerMiddle); 5440 verifyFormat("A<int * []> a;", PointerMiddle); 5441 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5442 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5443 verifyFormat("T ** t = new T *;", PointerMiddle); 5444 5445 // Member function reference qualifiers aren't binary operators. 5446 verifyFormat("string // break\n" 5447 "operator()() & {}"); 5448 verifyFormat("string // break\n" 5449 "operator()() && {}"); 5450 verifyGoogleFormat("template <typename T>\n" 5451 "auto x() & -> int {}"); 5452 } 5453 5454 TEST_F(FormatTest, UnderstandsAttributes) { 5455 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5456 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5457 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5458 FormatStyle AfterType = getLLVMStyle(); 5459 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5460 verifyFormat("__attribute__((nodebug)) void\n" 5461 "foo() {}\n", 5462 AfterType); 5463 } 5464 5465 TEST_F(FormatTest, UnderstandsEllipsis) { 5466 verifyFormat("int printf(const char *fmt, ...);"); 5467 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5468 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5469 5470 FormatStyle PointersLeft = getLLVMStyle(); 5471 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5472 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5473 } 5474 5475 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5476 EXPECT_EQ("int *a;\n" 5477 "int *a;\n" 5478 "int *a;", 5479 format("int *a;\n" 5480 "int* a;\n" 5481 "int *a;", 5482 getGoogleStyle())); 5483 EXPECT_EQ("int* a;\n" 5484 "int* a;\n" 5485 "int* a;", 5486 format("int* a;\n" 5487 "int* a;\n" 5488 "int *a;", 5489 getGoogleStyle())); 5490 EXPECT_EQ("int *a;\n" 5491 "int *a;\n" 5492 "int *a;", 5493 format("int *a;\n" 5494 "int * a;\n" 5495 "int * a;", 5496 getGoogleStyle())); 5497 EXPECT_EQ("auto x = [] {\n" 5498 " int *a;\n" 5499 " int *a;\n" 5500 " int *a;\n" 5501 "};", 5502 format("auto x=[]{int *a;\n" 5503 "int * a;\n" 5504 "int * a;};", 5505 getGoogleStyle())); 5506 } 5507 5508 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5509 verifyFormat("int f(int &&a) {}"); 5510 verifyFormat("int f(int a, char &&b) {}"); 5511 verifyFormat("void f() { int &&a = b; }"); 5512 verifyGoogleFormat("int f(int a, char&& b) {}"); 5513 verifyGoogleFormat("void f() { int&& a = b; }"); 5514 5515 verifyIndependentOfContext("A<int &&> a;"); 5516 verifyIndependentOfContext("A<int &&, int &&> a;"); 5517 verifyGoogleFormat("A<int&&> a;"); 5518 verifyGoogleFormat("A<int&&, int&&> a;"); 5519 5520 // Not rvalue references: 5521 verifyFormat("template <bool B, bool C> class A {\n" 5522 " static_assert(B && C, \"Something is wrong\");\n" 5523 "};"); 5524 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5525 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5526 verifyFormat("#define A(a, b) (a && b)"); 5527 } 5528 5529 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5530 verifyFormat("void f() {\n" 5531 " x[aaaaaaaaa -\n" 5532 " b] = 23;\n" 5533 "}", 5534 getLLVMStyleWithColumns(15)); 5535 } 5536 5537 TEST_F(FormatTest, FormatsCasts) { 5538 verifyFormat("Type *A = static_cast<Type *>(P);"); 5539 verifyFormat("Type *A = (Type *)P;"); 5540 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5541 verifyFormat("int a = (int)(2.0f);"); 5542 verifyFormat("int a = (int)2.0f;"); 5543 verifyFormat("x[(int32)y];"); 5544 verifyFormat("x = (int32)y;"); 5545 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5546 verifyFormat("int a = (int)*b;"); 5547 verifyFormat("int a = (int)2.0f;"); 5548 verifyFormat("int a = (int)~0;"); 5549 verifyFormat("int a = (int)++a;"); 5550 verifyFormat("int a = (int)sizeof(int);"); 5551 verifyFormat("int a = (int)+2;"); 5552 verifyFormat("my_int a = (my_int)2.0f;"); 5553 verifyFormat("my_int a = (my_int)sizeof(int);"); 5554 verifyFormat("return (my_int)aaa;"); 5555 verifyFormat("#define x ((int)-1)"); 5556 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5557 verifyFormat("#define p(q) ((int *)&q)"); 5558 verifyFormat("fn(a)(b) + 1;"); 5559 5560 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5561 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5562 verifyFormat("my_int a = (my_int)~0;"); 5563 verifyFormat("my_int a = (my_int)++a;"); 5564 verifyFormat("my_int a = (my_int)-2;"); 5565 verifyFormat("my_int a = (my_int)1;"); 5566 verifyFormat("my_int a = (my_int *)1;"); 5567 verifyFormat("my_int a = (const my_int)-1;"); 5568 verifyFormat("my_int a = (const my_int *)-1;"); 5569 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5570 verifyFormat("my_int a = (ns::my_int)-2;"); 5571 verifyFormat("case (my_int)ONE:"); 5572 verifyFormat("auto x = (X)this;"); 5573 5574 // FIXME: single value wrapped with paren will be treated as cast. 5575 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5576 5577 verifyFormat("{ (void)F; }"); 5578 5579 // Don't break after a cast's 5580 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5581 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5582 " bbbbbbbbbbbbbbbbbbbbbb);"); 5583 5584 // These are not casts. 5585 verifyFormat("void f(int *) {}"); 5586 verifyFormat("f(foo)->b;"); 5587 verifyFormat("f(foo).b;"); 5588 verifyFormat("f(foo)(b);"); 5589 verifyFormat("f(foo)[b];"); 5590 verifyFormat("[](foo) { return 4; }(bar);"); 5591 verifyFormat("(*funptr)(foo)[4];"); 5592 verifyFormat("funptrs[4](foo)[4];"); 5593 verifyFormat("void f(int *);"); 5594 verifyFormat("void f(int *) = 0;"); 5595 verifyFormat("void f(SmallVector<int>) {}"); 5596 verifyFormat("void f(SmallVector<int>);"); 5597 verifyFormat("void f(SmallVector<int>) = 0;"); 5598 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5599 verifyFormat("int a = sizeof(int) * b;"); 5600 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5601 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5602 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5603 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5604 5605 // These are not casts, but at some point were confused with casts. 5606 verifyFormat("virtual void foo(int *) override;"); 5607 verifyFormat("virtual void foo(char &) const;"); 5608 verifyFormat("virtual void foo(int *a, char *) const;"); 5609 verifyFormat("int a = sizeof(int *) + b;"); 5610 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5611 verifyFormat("bool b = f(g<int>) && c;"); 5612 verifyFormat("typedef void (*f)(int i) func;"); 5613 5614 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5615 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5616 // FIXME: The indentation here is not ideal. 5617 verifyFormat( 5618 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5619 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5620 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5621 } 5622 5623 TEST_F(FormatTest, FormatsFunctionTypes) { 5624 verifyFormat("A<bool()> a;"); 5625 verifyFormat("A<SomeType()> a;"); 5626 verifyFormat("A<void (*)(int, std::string)> a;"); 5627 verifyFormat("A<void *(int)>;"); 5628 verifyFormat("void *(*a)(int *, SomeType *);"); 5629 verifyFormat("int (*func)(void *);"); 5630 verifyFormat("void f() { int (*func)(void *); }"); 5631 verifyFormat("template <class CallbackClass>\n" 5632 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5633 5634 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5635 verifyGoogleFormat("void* (*a)(int);"); 5636 verifyGoogleFormat( 5637 "template <class CallbackClass>\n" 5638 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5639 5640 // Other constructs can look somewhat like function types: 5641 verifyFormat("A<sizeof(*x)> a;"); 5642 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5643 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5644 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5645 verifyFormat("int x = f(&h)();"); 5646 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 5647 verifyFormat("std::function<\n" 5648 " LooooooooooongTemplatedType<\n" 5649 " SomeType>*(\n" 5650 " LooooooooooooooooongType type)>\n" 5651 " function;", 5652 getGoogleStyleWithColumns(40)); 5653 } 5654 5655 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5656 verifyFormat("A (*foo_)[6];"); 5657 verifyFormat("vector<int> (*foo_)[6];"); 5658 } 5659 5660 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5661 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5662 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5663 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5664 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5665 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5666 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5667 5668 // Different ways of ()-initializiation. 5669 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5670 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5671 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5672 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5673 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5674 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5675 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5676 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5677 5678 // Lambdas should not confuse the variable declaration heuristic. 5679 verifyFormat("LooooooooooooooooongType\n" 5680 " variable(nullptr, [](A *a) {});", 5681 getLLVMStyleWithColumns(40)); 5682 } 5683 5684 TEST_F(FormatTest, BreaksLongDeclarations) { 5685 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5686 " AnotherNameForTheLongType;"); 5687 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5689 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5690 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5691 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5692 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5693 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5694 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5695 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5696 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5697 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5698 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5699 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5700 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5701 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5702 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 5703 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5704 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 5705 FormatStyle Indented = getLLVMStyle(); 5706 Indented.IndentWrappedFunctionNames = true; 5707 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5708 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5709 Indented); 5710 verifyFormat( 5711 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5712 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5713 Indented); 5714 verifyFormat( 5715 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5716 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5717 Indented); 5718 verifyFormat( 5719 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5720 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5721 Indented); 5722 5723 // FIXME: Without the comment, this breaks after "(". 5724 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5725 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5726 getGoogleStyle()); 5727 5728 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5729 " int LoooooooooooooooooooongParam2) {}"); 5730 verifyFormat( 5731 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5732 " SourceLocation L, IdentifierIn *II,\n" 5733 " Type *T) {}"); 5734 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5735 "ReallyReaaallyLongFunctionName(\n" 5736 " const std::string &SomeParameter,\n" 5737 " const SomeType<string, SomeOtherTemplateParameter>\n" 5738 " &ReallyReallyLongParameterName,\n" 5739 " const SomeType<string, SomeOtherTemplateParameter>\n" 5740 " &AnotherLongParameterName) {}"); 5741 verifyFormat("template <typename A>\n" 5742 "SomeLoooooooooooooooooooooongType<\n" 5743 " typename some_namespace::SomeOtherType<A>::Type>\n" 5744 "Function() {}"); 5745 5746 verifyGoogleFormat( 5747 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5748 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5749 verifyGoogleFormat( 5750 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5751 " SourceLocation L) {}"); 5752 verifyGoogleFormat( 5753 "some_namespace::LongReturnType\n" 5754 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5755 " int first_long_parameter, int second_parameter) {}"); 5756 5757 verifyGoogleFormat("template <typename T>\n" 5758 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5759 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5760 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5761 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5762 5763 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5764 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5765 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5766 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5767 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5768 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5769 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5770 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5773 5774 verifyFormat("template <typename T> // Templates on own line.\n" 5775 "static int // Some comment.\n" 5776 "MyFunction(int a);", 5777 getLLVMStyle()); 5778 } 5779 5780 TEST_F(FormatTest, FormatsArrays) { 5781 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5782 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5783 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 5784 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 5785 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 5786 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 5787 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5788 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5790 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5792 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5793 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5794 verifyFormat( 5795 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5796 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5797 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 5799 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5800 5801 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5803 verifyFormat( 5804 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5805 " .aaaaaaa[0]\n" 5806 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5807 verifyFormat("a[::b::c];"); 5808 5809 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5810 5811 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 5812 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 5813 } 5814 5815 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5816 verifyFormat("(a)->b();"); 5817 verifyFormat("--a;"); 5818 } 5819 5820 TEST_F(FormatTest, HandlesIncludeDirectives) { 5821 verifyFormat("#include <string>\n" 5822 "#include <a/b/c.h>\n" 5823 "#include \"a/b/string\"\n" 5824 "#include \"string.h\"\n" 5825 "#include \"string.h\"\n" 5826 "#include <a-a>\n" 5827 "#include < path with space >\n" 5828 "#include_next <test.h>" 5829 "#include \"abc.h\" // this is included for ABC\n" 5830 "#include \"some long include\" // with a comment\n" 5831 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 5832 getLLVMStyleWithColumns(35)); 5833 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 5834 EXPECT_EQ("#include <a>", format("#include<a>")); 5835 5836 verifyFormat("#import <string>"); 5837 verifyFormat("#import <a/b/c.h>"); 5838 verifyFormat("#import \"a/b/string\""); 5839 verifyFormat("#import \"string.h\""); 5840 verifyFormat("#import \"string.h\""); 5841 verifyFormat("#if __has_include(<strstream>)\n" 5842 "#include <strstream>\n" 5843 "#endif"); 5844 5845 verifyFormat("#define MY_IMPORT <a/b>"); 5846 5847 verifyFormat("#if __has_include(<a/b>)"); 5848 verifyFormat("#if __has_include_next(<a/b>)"); 5849 verifyFormat("#define F __has_include(<a/b>)"); 5850 verifyFormat("#define F __has_include_next(<a/b>)"); 5851 5852 // Protocol buffer definition or missing "#". 5853 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 5854 getLLVMStyleWithColumns(30)); 5855 5856 FormatStyle Style = getLLVMStyle(); 5857 Style.AlwaysBreakBeforeMultilineStrings = true; 5858 Style.ColumnLimit = 0; 5859 verifyFormat("#import \"abc.h\"", Style); 5860 5861 // But 'import' might also be a regular C++ namespace. 5862 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5864 } 5865 5866 //===----------------------------------------------------------------------===// 5867 // Error recovery tests. 5868 //===----------------------------------------------------------------------===// 5869 5870 TEST_F(FormatTest, IncompleteParameterLists) { 5871 FormatStyle NoBinPacking = getLLVMStyle(); 5872 NoBinPacking.BinPackParameters = false; 5873 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 5874 " double *min_x,\n" 5875 " double *max_x,\n" 5876 " double *min_y,\n" 5877 " double *max_y,\n" 5878 " double *min_z,\n" 5879 " double *max_z, ) {}", 5880 NoBinPacking); 5881 } 5882 5883 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 5884 verifyFormat("void f() { return; }\n42"); 5885 verifyFormat("void f() {\n" 5886 " if (0)\n" 5887 " return;\n" 5888 "}\n" 5889 "42"); 5890 verifyFormat("void f() { return }\n42"); 5891 verifyFormat("void f() {\n" 5892 " if (0)\n" 5893 " return\n" 5894 "}\n" 5895 "42"); 5896 } 5897 5898 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 5899 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 5900 EXPECT_EQ("void f() {\n" 5901 " if (a)\n" 5902 " return\n" 5903 "}", 5904 format("void f ( ) { if ( a ) return }")); 5905 EXPECT_EQ("namespace N {\n" 5906 "void f()\n" 5907 "}", 5908 format("namespace N { void f() }")); 5909 EXPECT_EQ("namespace N {\n" 5910 "void f() {}\n" 5911 "void g()\n" 5912 "} // namespace N", 5913 format("namespace N { void f( ) { } void g( ) }")); 5914 } 5915 5916 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 5917 verifyFormat("int aaaaaaaa =\n" 5918 " // Overlylongcomment\n" 5919 " b;", 5920 getLLVMStyleWithColumns(20)); 5921 verifyFormat("function(\n" 5922 " ShortArgument,\n" 5923 " LoooooooooooongArgument);\n", 5924 getLLVMStyleWithColumns(20)); 5925 } 5926 5927 TEST_F(FormatTest, IncorrectAccessSpecifier) { 5928 verifyFormat("public:"); 5929 verifyFormat("class A {\n" 5930 "public\n" 5931 " void f() {}\n" 5932 "};"); 5933 verifyFormat("public\n" 5934 "int qwerty;"); 5935 verifyFormat("public\n" 5936 "B {}"); 5937 verifyFormat("public\n" 5938 "{}"); 5939 verifyFormat("public\n" 5940 "B { int x; }"); 5941 } 5942 5943 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 5944 verifyFormat("{"); 5945 verifyFormat("#})"); 5946 verifyNoCrash("(/**/[:!] ?[)."); 5947 } 5948 5949 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 5950 verifyFormat("do {\n}"); 5951 verifyFormat("do {\n}\n" 5952 "f();"); 5953 verifyFormat("do {\n}\n" 5954 "wheeee(fun);"); 5955 verifyFormat("do {\n" 5956 " f();\n" 5957 "}"); 5958 } 5959 5960 TEST_F(FormatTest, IncorrectCodeMissingParens) { 5961 verifyFormat("if {\n foo;\n foo();\n}"); 5962 verifyFormat("switch {\n foo;\n foo();\n}"); 5963 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 5964 verifyFormat("while {\n foo;\n foo();\n}"); 5965 verifyFormat("do {\n foo;\n foo();\n} while;"); 5966 } 5967 5968 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 5969 verifyIncompleteFormat("namespace {\n" 5970 "class Foo { Foo (\n" 5971 "};\n" 5972 "} // namespace"); 5973 } 5974 5975 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 5976 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 5977 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 5978 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 5979 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 5980 5981 EXPECT_EQ("{\n" 5982 " {\n" 5983 " breakme(\n" 5984 " qwe);\n" 5985 " }\n", 5986 format("{\n" 5987 " {\n" 5988 " breakme(qwe);\n" 5989 "}\n", 5990 getLLVMStyleWithColumns(10))); 5991 } 5992 5993 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 5994 verifyFormat("int x = {\n" 5995 " avariable,\n" 5996 " b(alongervariable)};", 5997 getLLVMStyleWithColumns(25)); 5998 } 5999 6000 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6001 verifyFormat("return (a)(b){1, 2, 3};"); 6002 } 6003 6004 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6005 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6006 verifyFormat("vector<int> x{\n" 6007 " 1, 2, 3, 4,\n" 6008 "};"); 6009 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6010 verifyFormat("f({1, 2});"); 6011 verifyFormat("auto v = Foo{-1};"); 6012 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6013 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6014 verifyFormat("new vector<int>{1, 2, 3};"); 6015 verifyFormat("new int[3]{1, 2, 3};"); 6016 verifyFormat("new int{1};"); 6017 verifyFormat("return {arg1, arg2};"); 6018 verifyFormat("return {arg1, SomeType{parameter}};"); 6019 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6020 verifyFormat("new T{arg1, arg2};"); 6021 verifyFormat("f(MyMap[{composite, key}]);"); 6022 verifyFormat("class Class {\n" 6023 " T member = {arg1, arg2};\n" 6024 "};"); 6025 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6026 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6027 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6028 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6029 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6030 6031 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6032 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6033 verifyFormat("auto i = decltype(x){};"); 6034 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6035 verifyFormat("Node n{1, Node{1000}, //\n" 6036 " 2};"); 6037 verifyFormat("Aaaa aaaaaaa{\n" 6038 " {\n" 6039 " aaaa,\n" 6040 " },\n" 6041 "};"); 6042 verifyFormat("class C : public D {\n" 6043 " SomeClass SC{2};\n" 6044 "};"); 6045 verifyFormat("class C : public A {\n" 6046 " class D : public B {\n" 6047 " void f() { int i{2}; }\n" 6048 " };\n" 6049 "};"); 6050 verifyFormat("#define A {a, a},"); 6051 6052 // Cases where distinguising braced lists and blocks is hard. 6053 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6054 verifyFormat("void f() {\n" 6055 " return; // comment\n" 6056 "}\n" 6057 "SomeType t;"); 6058 verifyFormat("void f() {\n" 6059 " if (a) {\n" 6060 " f();\n" 6061 " }\n" 6062 "}\n" 6063 "SomeType t;"); 6064 6065 // In combination with BinPackArguments = false. 6066 FormatStyle NoBinPacking = getLLVMStyle(); 6067 NoBinPacking.BinPackArguments = false; 6068 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6069 " bbbbb,\n" 6070 " ccccc,\n" 6071 " ddddd,\n" 6072 " eeeee,\n" 6073 " ffffff,\n" 6074 " ggggg,\n" 6075 " hhhhhh,\n" 6076 " iiiiii,\n" 6077 " jjjjjj,\n" 6078 " kkkkkk};", 6079 NoBinPacking); 6080 verifyFormat("const Aaaaaa aaaaa = {\n" 6081 " aaaaa,\n" 6082 " bbbbb,\n" 6083 " ccccc,\n" 6084 " ddddd,\n" 6085 " eeeee,\n" 6086 " ffffff,\n" 6087 " ggggg,\n" 6088 " hhhhhh,\n" 6089 " iiiiii,\n" 6090 " jjjjjj,\n" 6091 " kkkkkk,\n" 6092 "};", 6093 NoBinPacking); 6094 verifyFormat( 6095 "const Aaaaaa aaaaa = {\n" 6096 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6097 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6098 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6099 "};", 6100 NoBinPacking); 6101 6102 // FIXME: The alignment of these trailing comments might be bad. Then again, 6103 // this might be utterly useless in real code. 6104 verifyFormat("Constructor::Constructor()\n" 6105 " : some_value{ //\n" 6106 " aaaaaaa, //\n" 6107 " bbbbbbb} {}"); 6108 6109 // In braced lists, the first comment is always assumed to belong to the 6110 // first element. Thus, it can be moved to the next or previous line as 6111 // appropriate. 6112 EXPECT_EQ("function({// First element:\n" 6113 " 1,\n" 6114 " // Second element:\n" 6115 " 2});", 6116 format("function({\n" 6117 " // First element:\n" 6118 " 1,\n" 6119 " // Second element:\n" 6120 " 2});")); 6121 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6122 " // First element:\n" 6123 " 1,\n" 6124 " // Second element:\n" 6125 " 2};", 6126 format("std::vector<int> MyNumbers{// First element:\n" 6127 " 1,\n" 6128 " // Second element:\n" 6129 " 2};", 6130 getLLVMStyleWithColumns(30))); 6131 // A trailing comma should still lead to an enforced line break. 6132 EXPECT_EQ("vector<int> SomeVector = {\n" 6133 " // aaa\n" 6134 " 1, 2,\n" 6135 "};", 6136 format("vector<int> SomeVector = { // aaa\n" 6137 " 1, 2, };")); 6138 6139 FormatStyle ExtraSpaces = getLLVMStyle(); 6140 ExtraSpaces.Cpp11BracedListStyle = false; 6141 ExtraSpaces.ColumnLimit = 75; 6142 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6143 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6144 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6145 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6146 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6147 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6148 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6149 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6150 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6151 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6152 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6153 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6154 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6155 verifyFormat("class Class {\n" 6156 " T member = { arg1, arg2 };\n" 6157 "};", 6158 ExtraSpaces); 6159 verifyFormat( 6160 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6161 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6162 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6163 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6164 ExtraSpaces); 6165 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6166 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6167 ExtraSpaces); 6168 verifyFormat( 6169 "someFunction(OtherParam,\n" 6170 " BracedList{ // comment 1 (Forcing interesting break)\n" 6171 " param1, param2,\n" 6172 " // comment 2\n" 6173 " param3, param4 });", 6174 ExtraSpaces); 6175 verifyFormat( 6176 "std::this_thread::sleep_for(\n" 6177 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6178 ExtraSpaces); 6179 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6180 " aaaaaaa,\n" 6181 " aaaaaaaaaa,\n" 6182 " aaaaa,\n" 6183 " aaaaaaaaaaaaaaa,\n" 6184 " aaa,\n" 6185 " aaaaaaaaaa,\n" 6186 " a,\n" 6187 " aaaaaaaaaaaaaaaaaaaaa,\n" 6188 " aaaaaaaaaaaa,\n" 6189 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6190 " aaaaaaa,\n" 6191 " a};"); 6192 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6193 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6194 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6195 } 6196 6197 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6198 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6199 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6200 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6201 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6202 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6203 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6204 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6205 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6206 " 1, 22, 333, 4444, 55555, //\n" 6207 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6208 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6209 verifyFormat( 6210 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6211 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6212 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6213 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6214 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6215 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6216 " 7777777};"); 6217 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6218 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6219 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6220 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6221 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6222 " // Separating comment.\n" 6223 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6224 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6225 " // Leading comment\n" 6226 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6227 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6228 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6229 " 1, 1, 1, 1};", 6230 getLLVMStyleWithColumns(39)); 6231 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6232 " 1, 1, 1, 1};", 6233 getLLVMStyleWithColumns(38)); 6234 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6235 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6236 getLLVMStyleWithColumns(43)); 6237 verifyFormat( 6238 "static unsigned SomeValues[10][3] = {\n" 6239 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6240 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6241 verifyFormat("static auto fields = new vector<string>{\n" 6242 " \"aaaaaaaaaaaaa\",\n" 6243 " \"aaaaaaaaaaaaa\",\n" 6244 " \"aaaaaaaaaaaa\",\n" 6245 " \"aaaaaaaaaaaaaa\",\n" 6246 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6247 " \"aaaaaaaaaaaa\",\n" 6248 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6249 "};"); 6250 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6251 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6252 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6253 " 3, cccccccccccccccccccccc};", 6254 getLLVMStyleWithColumns(60)); 6255 6256 // Trailing commas. 6257 verifyFormat("vector<int> x = {\n" 6258 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6259 "};", 6260 getLLVMStyleWithColumns(39)); 6261 verifyFormat("vector<int> x = {\n" 6262 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6263 "};", 6264 getLLVMStyleWithColumns(39)); 6265 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6266 " 1, 1, 1, 1,\n" 6267 " /**/ /**/};", 6268 getLLVMStyleWithColumns(39)); 6269 6270 // Trailing comment in the first line. 6271 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6272 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6273 " 111111111, 222222222, 3333333333, 444444444, //\n" 6274 " 11111111, 22222222, 333333333, 44444444};"); 6275 // Trailing comment in the last line. 6276 verifyFormat("int aaaaa[] = {\n" 6277 " 1, 2, 3, // comment\n" 6278 " 4, 5, 6 // comment\n" 6279 "};"); 6280 6281 // With nested lists, we should either format one item per line or all nested 6282 // lists one on line. 6283 // FIXME: For some nested lists, we can do better. 6284 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6285 " {aaaaaaaaaaaaaaaaaaa},\n" 6286 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6287 " {aaaaaaaaaaaaaaaaa}};", 6288 getLLVMStyleWithColumns(60)); 6289 verifyFormat( 6290 "SomeStruct my_struct_array = {\n" 6291 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6292 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6293 " {aaa, aaa},\n" 6294 " {aaa, aaa},\n" 6295 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6296 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6297 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6298 6299 // No column layout should be used here. 6300 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6301 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6302 6303 verifyNoCrash("a<,"); 6304 6305 // No braced initializer here. 6306 verifyFormat("void f() {\n" 6307 " struct Dummy {};\n" 6308 " f(v);\n" 6309 "}"); 6310 6311 // Long lists should be formatted in columns even if they are nested. 6312 verifyFormat( 6313 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6314 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6315 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6316 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6317 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6318 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6319 6320 // Allow "single-column" layout even if that violates the column limit. There 6321 // isn't going to be a better way. 6322 verifyFormat("std::vector<int> a = {\n" 6323 " aaaaaaaa,\n" 6324 " aaaaaaaa,\n" 6325 " aaaaaaaa,\n" 6326 " aaaaaaaa,\n" 6327 " aaaaaaaaaa,\n" 6328 " aaaaaaaa,\n" 6329 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6330 getLLVMStyleWithColumns(30)); 6331 verifyFormat("vector<int> aaaa = {\n" 6332 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6333 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6334 " aaaaaa.aaaaaaa,\n" 6335 " aaaaaa.aaaaaaa,\n" 6336 " aaaaaa.aaaaaaa,\n" 6337 " aaaaaa.aaaaaaa,\n" 6338 "};"); 6339 6340 // Don't create hanging lists. 6341 verifyFormat("someFunction(Param, {List1, List2,\n" 6342 " List3});", 6343 getLLVMStyleWithColumns(35)); 6344 verifyFormat("someFunction(Param, Param,\n" 6345 " {List1, List2,\n" 6346 " List3});", 6347 getLLVMStyleWithColumns(35)); 6348 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6349 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6350 } 6351 6352 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6353 FormatStyle DoNotMerge = getLLVMStyle(); 6354 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6355 6356 verifyFormat("void f() { return 42; }"); 6357 verifyFormat("void f() {\n" 6358 " return 42;\n" 6359 "}", 6360 DoNotMerge); 6361 verifyFormat("void f() {\n" 6362 " // Comment\n" 6363 "}"); 6364 verifyFormat("{\n" 6365 "#error {\n" 6366 " int a;\n" 6367 "}"); 6368 verifyFormat("{\n" 6369 " int a;\n" 6370 "#error {\n" 6371 "}"); 6372 verifyFormat("void f() {} // comment"); 6373 verifyFormat("void f() { int a; } // comment"); 6374 verifyFormat("void f() {\n" 6375 "} // comment", 6376 DoNotMerge); 6377 verifyFormat("void f() {\n" 6378 " int a;\n" 6379 "} // comment", 6380 DoNotMerge); 6381 verifyFormat("void f() {\n" 6382 "} // comment", 6383 getLLVMStyleWithColumns(15)); 6384 6385 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6386 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6387 6388 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6389 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6390 verifyFormat("class C {\n" 6391 " C()\n" 6392 " : iiiiiiii(nullptr),\n" 6393 " kkkkkkk(nullptr),\n" 6394 " mmmmmmm(nullptr),\n" 6395 " nnnnnnn(nullptr) {}\n" 6396 "};", 6397 getGoogleStyle()); 6398 6399 FormatStyle NoColumnLimit = getLLVMStyle(); 6400 NoColumnLimit.ColumnLimit = 0; 6401 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6402 EXPECT_EQ("class C {\n" 6403 " A() : b(0) {}\n" 6404 "};", 6405 format("class C{A():b(0){}};", NoColumnLimit)); 6406 EXPECT_EQ("A()\n" 6407 " : b(0) {\n" 6408 "}", 6409 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6410 6411 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6412 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6413 FormatStyle::SFS_None; 6414 EXPECT_EQ("A()\n" 6415 " : b(0) {\n" 6416 "}", 6417 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6418 EXPECT_EQ("A()\n" 6419 " : b(0) {\n" 6420 "}", 6421 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6422 6423 verifyFormat("#define A \\\n" 6424 " void f() { \\\n" 6425 " int i; \\\n" 6426 " }", 6427 getLLVMStyleWithColumns(20)); 6428 verifyFormat("#define A \\\n" 6429 " void f() { int i; }", 6430 getLLVMStyleWithColumns(21)); 6431 verifyFormat("#define A \\\n" 6432 " void f() { \\\n" 6433 " int i; \\\n" 6434 " } \\\n" 6435 " int j;", 6436 getLLVMStyleWithColumns(22)); 6437 verifyFormat("#define A \\\n" 6438 " void f() { int i; } \\\n" 6439 " int j;", 6440 getLLVMStyleWithColumns(23)); 6441 } 6442 6443 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6444 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6445 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6446 verifyFormat("class C {\n" 6447 " int f() {}\n" 6448 "};", 6449 MergeEmptyOnly); 6450 verifyFormat("class C {\n" 6451 " int f() {\n" 6452 " return 42;\n" 6453 " }\n" 6454 "};", 6455 MergeEmptyOnly); 6456 verifyFormat("int f() {}", MergeEmptyOnly); 6457 verifyFormat("int f() {\n" 6458 " return 42;\n" 6459 "}", 6460 MergeEmptyOnly); 6461 6462 // Also verify behavior when BraceWrapping.AfterFunction = true 6463 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6464 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6465 verifyFormat("int f() {}", MergeEmptyOnly); 6466 verifyFormat("class C {\n" 6467 " int f() {}\n" 6468 "};", 6469 MergeEmptyOnly); 6470 } 6471 6472 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6473 FormatStyle MergeInlineOnly = getLLVMStyle(); 6474 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6475 verifyFormat("class C {\n" 6476 " int f() { return 42; }\n" 6477 "};", 6478 MergeInlineOnly); 6479 verifyFormat("int f() {\n" 6480 " return 42;\n" 6481 "}", 6482 MergeInlineOnly); 6483 6484 // SFS_Inline implies SFS_Empty 6485 verifyFormat("class C {\n" 6486 " int f() {}\n" 6487 "};", 6488 MergeInlineOnly); 6489 verifyFormat("int f() {}", MergeInlineOnly); 6490 6491 // Also verify behavior when BraceWrapping.AfterFunction = true 6492 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6493 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6494 verifyFormat("class C {\n" 6495 " int f() { return 42; }\n" 6496 "};", 6497 MergeInlineOnly); 6498 verifyFormat("int f()\n" 6499 "{\n" 6500 " return 42;\n" 6501 "}", 6502 MergeInlineOnly); 6503 6504 // SFS_Inline implies SFS_Empty 6505 verifyFormat("int f() {}", MergeInlineOnly); 6506 verifyFormat("class C {\n" 6507 " int f() {}\n" 6508 "};", 6509 MergeInlineOnly); 6510 } 6511 6512 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 6513 FormatStyle MergeInlineOnly = getLLVMStyle(); 6514 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 6515 FormatStyle::SFS_InlineOnly; 6516 verifyFormat("class C {\n" 6517 " int f() { return 42; }\n" 6518 "};", 6519 MergeInlineOnly); 6520 verifyFormat("int f() {\n" 6521 " return 42;\n" 6522 "}", 6523 MergeInlineOnly); 6524 6525 // SFS_InlineOnly does not imply SFS_Empty 6526 verifyFormat("class C {\n" 6527 " int f() {}\n" 6528 "};", 6529 MergeInlineOnly); 6530 verifyFormat("int f() {\n" 6531 "}", 6532 MergeInlineOnly); 6533 6534 // Also verify behavior when BraceWrapping.AfterFunction = true 6535 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6536 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6537 verifyFormat("class C {\n" 6538 " int f() { return 42; }\n" 6539 "};", 6540 MergeInlineOnly); 6541 verifyFormat("int f()\n" 6542 "{\n" 6543 " return 42;\n" 6544 "}", 6545 MergeInlineOnly); 6546 6547 // SFS_InlineOnly does not imply SFS_Empty 6548 verifyFormat("int f()\n" 6549 "{\n" 6550 "}", 6551 MergeInlineOnly); 6552 verifyFormat("class C {\n" 6553 " int f() {}\n" 6554 "};", 6555 MergeInlineOnly); 6556 } 6557 6558 TEST_F(FormatTest, SplitEmptyFunctionBody) { 6559 FormatStyle Style = getLLVMStyle(); 6560 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6561 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6562 Style.BraceWrapping.AfterFunction = true; 6563 Style.BraceWrapping.SplitEmptyFunctionBody = false; 6564 Style.ColumnLimit = 40; 6565 6566 verifyFormat("int f()\n" 6567 "{}", 6568 Style); 6569 verifyFormat("int f()\n" 6570 "{\n" 6571 " return 42;\n" 6572 "}", 6573 Style); 6574 verifyFormat("int f()\n" 6575 "{\n" 6576 " // some comment\n" 6577 "}", 6578 Style); 6579 6580 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6581 verifyFormat("int f() {}", Style); 6582 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6583 "{}", 6584 Style); 6585 verifyFormat("int f()\n" 6586 "{\n" 6587 " return 0;\n" 6588 "}", 6589 Style); 6590 6591 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6592 verifyFormat("class Foo {\n" 6593 " int f() {}\n" 6594 "};\n", 6595 Style); 6596 verifyFormat("class Foo {\n" 6597 " int f() { return 0; }\n" 6598 "};\n", 6599 Style); 6600 verifyFormat("class Foo {\n" 6601 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6602 " {}\n" 6603 "};\n", 6604 Style); 6605 verifyFormat("class Foo {\n" 6606 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6607 " {\n" 6608 " return 0;\n" 6609 " }\n" 6610 "};\n", 6611 Style); 6612 6613 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6614 verifyFormat("int f() {}", Style); 6615 verifyFormat("int f() { return 0; }", Style); 6616 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6617 "{}", 6618 Style); 6619 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6620 "{\n" 6621 " return 0;\n" 6622 "}", 6623 Style); 6624 } 6625 6626 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6627 // Elaborate type variable declarations. 6628 verifyFormat("struct foo a = {bar};\nint n;"); 6629 verifyFormat("class foo a = {bar};\nint n;"); 6630 verifyFormat("union foo a = {bar};\nint n;"); 6631 6632 // Elaborate types inside function definitions. 6633 verifyFormat("struct foo f() {}\nint n;"); 6634 verifyFormat("class foo f() {}\nint n;"); 6635 verifyFormat("union foo f() {}\nint n;"); 6636 6637 // Templates. 6638 verifyFormat("template <class X> void f() {}\nint n;"); 6639 verifyFormat("template <struct X> void f() {}\nint n;"); 6640 verifyFormat("template <union X> void f() {}\nint n;"); 6641 6642 // Actual definitions... 6643 verifyFormat("struct {\n} n;"); 6644 verifyFormat( 6645 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6646 verifyFormat("union Z {\n int n;\n} x;"); 6647 verifyFormat("class MACRO Z {\n} n;"); 6648 verifyFormat("class MACRO(X) Z {\n} n;"); 6649 verifyFormat("class __attribute__(X) Z {\n} n;"); 6650 verifyFormat("class __declspec(X) Z {\n} n;"); 6651 verifyFormat("class A##B##C {\n} n;"); 6652 verifyFormat("class alignas(16) Z {\n} n;"); 6653 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6654 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6655 6656 // Redefinition from nested context: 6657 verifyFormat("class A::B::C {\n} n;"); 6658 6659 // Template definitions. 6660 verifyFormat( 6661 "template <typename F>\n" 6662 "Matcher(const Matcher<F> &Other,\n" 6663 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6664 " !is_same<F, T>::value>::type * = 0)\n" 6665 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6666 6667 // FIXME: This is still incorrectly handled at the formatter side. 6668 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6669 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6670 6671 // FIXME: 6672 // This now gets parsed incorrectly as class definition. 6673 // verifyFormat("class A<int> f() {\n}\nint n;"); 6674 6675 // Elaborate types where incorrectly parsing the structural element would 6676 // break the indent. 6677 verifyFormat("if (true)\n" 6678 " class X x;\n" 6679 "else\n" 6680 " f();\n"); 6681 6682 // This is simply incomplete. Formatting is not important, but must not crash. 6683 verifyFormat("class A:"); 6684 } 6685 6686 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6687 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6688 format("#error Leave all white!!!!! space* alone!\n")); 6689 EXPECT_EQ( 6690 "#warning Leave all white!!!!! space* alone!\n", 6691 format("#warning Leave all white!!!!! space* alone!\n")); 6692 EXPECT_EQ("#error 1", format(" # error 1")); 6693 EXPECT_EQ("#warning 1", format(" # warning 1")); 6694 } 6695 6696 TEST_F(FormatTest, FormatHashIfExpressions) { 6697 verifyFormat("#if AAAA && BBBB"); 6698 verifyFormat("#if (AAAA && BBBB)"); 6699 verifyFormat("#elif (AAAA && BBBB)"); 6700 // FIXME: Come up with a better indentation for #elif. 6701 verifyFormat( 6702 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6703 " defined(BBBBBBBB)\n" 6704 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6705 " defined(BBBBBBBB)\n" 6706 "#endif", 6707 getLLVMStyleWithColumns(65)); 6708 } 6709 6710 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6711 FormatStyle AllowsMergedIf = getGoogleStyle(); 6712 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6713 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6714 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6715 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6716 EXPECT_EQ("if (true) return 42;", 6717 format("if (true)\nreturn 42;", AllowsMergedIf)); 6718 FormatStyle ShortMergedIf = AllowsMergedIf; 6719 ShortMergedIf.ColumnLimit = 25; 6720 verifyFormat("#define A \\\n" 6721 " if (true) return 42;", 6722 ShortMergedIf); 6723 verifyFormat("#define A \\\n" 6724 " f(); \\\n" 6725 " if (true)\n" 6726 "#define B", 6727 ShortMergedIf); 6728 verifyFormat("#define A \\\n" 6729 " f(); \\\n" 6730 " if (true)\n" 6731 "g();", 6732 ShortMergedIf); 6733 verifyFormat("{\n" 6734 "#ifdef A\n" 6735 " // Comment\n" 6736 " if (true) continue;\n" 6737 "#endif\n" 6738 " // Comment\n" 6739 " if (true) continue;\n" 6740 "}", 6741 ShortMergedIf); 6742 ShortMergedIf.ColumnLimit = 33; 6743 verifyFormat("#define A \\\n" 6744 " if constexpr (true) return 42;", 6745 ShortMergedIf); 6746 ShortMergedIf.ColumnLimit = 29; 6747 verifyFormat("#define A \\\n" 6748 " if (aaaaaaaaaa) return 1; \\\n" 6749 " return 2;", 6750 ShortMergedIf); 6751 ShortMergedIf.ColumnLimit = 28; 6752 verifyFormat("#define A \\\n" 6753 " if (aaaaaaaaaa) \\\n" 6754 " return 1; \\\n" 6755 " return 2;", 6756 ShortMergedIf); 6757 verifyFormat("#define A \\\n" 6758 " if constexpr (aaaaaaa) \\\n" 6759 " return 1; \\\n" 6760 " return 2;", 6761 ShortMergedIf); 6762 } 6763 6764 TEST_F(FormatTest, FormatStarDependingOnContext) { 6765 verifyFormat("void f(int *a);"); 6766 verifyFormat("void f() { f(fint * b); }"); 6767 verifyFormat("class A {\n void f(int *a);\n};"); 6768 verifyFormat("class A {\n int *a;\n};"); 6769 verifyFormat("namespace a {\n" 6770 "namespace b {\n" 6771 "class A {\n" 6772 " void f() {}\n" 6773 " int *a;\n" 6774 "};\n" 6775 "} // namespace b\n" 6776 "} // namespace a"); 6777 } 6778 6779 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6780 verifyFormat("while"); 6781 verifyFormat("operator"); 6782 } 6783 6784 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 6785 // This code would be painfully slow to format if we didn't skip it. 6786 std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x 6787 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6788 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6789 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6790 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6791 "A(1, 1)\n" 6792 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 6793 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6794 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6795 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6796 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6797 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6798 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6799 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6800 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6801 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 6802 // Deeply nested part is untouched, rest is formatted. 6803 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 6804 format(std::string("int i;\n") + Code + "int j;\n", 6805 getLLVMStyle(), SC_ExpectIncomplete)); 6806 } 6807 6808 //===----------------------------------------------------------------------===// 6809 // Objective-C tests. 6810 //===----------------------------------------------------------------------===// 6811 6812 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 6813 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 6814 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 6815 format("-(NSUInteger)indexOfObject:(id)anObject;")); 6816 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 6817 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 6818 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 6819 format("-(NSInteger)Method3:(id)anObject;")); 6820 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 6821 format("-(NSInteger)Method4:(id)anObject;")); 6822 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 6823 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 6824 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 6825 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 6826 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6827 "forAllCells:(BOOL)flag;", 6828 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6829 "forAllCells:(BOOL)flag;")); 6830 6831 // Very long objectiveC method declaration. 6832 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 6833 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 6834 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 6835 " inRange:(NSRange)range\n" 6836 " outRange:(NSRange)out_range\n" 6837 " outRange1:(NSRange)out_range1\n" 6838 " outRange2:(NSRange)out_range2\n" 6839 " outRange3:(NSRange)out_range3\n" 6840 " outRange4:(NSRange)out_range4\n" 6841 " outRange5:(NSRange)out_range5\n" 6842 " outRange6:(NSRange)out_range6\n" 6843 " outRange7:(NSRange)out_range7\n" 6844 " outRange8:(NSRange)out_range8\n" 6845 " outRange9:(NSRange)out_range9;"); 6846 6847 // When the function name has to be wrapped. 6848 FormatStyle Style = getLLVMStyle(); 6849 Style.IndentWrappedFunctionNames = false; 6850 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6851 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6852 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6853 "}", 6854 Style); 6855 Style.IndentWrappedFunctionNames = true; 6856 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6857 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6858 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6859 "}", 6860 Style); 6861 6862 verifyFormat("- (int)sum:(vector<int>)numbers;"); 6863 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 6864 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 6865 // protocol lists (but not for template classes): 6866 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 6867 6868 verifyFormat("- (int (*)())foo:(int (*)())f;"); 6869 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 6870 6871 // If there's no return type (very rare in practice!), LLVM and Google style 6872 // agree. 6873 verifyFormat("- foo;"); 6874 verifyFormat("- foo:(int)f;"); 6875 verifyGoogleFormat("- foo:(int)foo;"); 6876 } 6877 6878 6879 TEST_F(FormatTest, BreaksStringLiterals) { 6880 EXPECT_EQ("\"some text \"\n" 6881 "\"other\";", 6882 format("\"some text other\";", getLLVMStyleWithColumns(12))); 6883 EXPECT_EQ("\"some text \"\n" 6884 "\"other\";", 6885 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 6886 EXPECT_EQ( 6887 "#define A \\\n" 6888 " \"some \" \\\n" 6889 " \"text \" \\\n" 6890 " \"other\";", 6891 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 6892 EXPECT_EQ( 6893 "#define A \\\n" 6894 " \"so \" \\\n" 6895 " \"text \" \\\n" 6896 " \"other\";", 6897 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 6898 6899 EXPECT_EQ("\"some text\"", 6900 format("\"some text\"", getLLVMStyleWithColumns(1))); 6901 EXPECT_EQ("\"some text\"", 6902 format("\"some text\"", getLLVMStyleWithColumns(11))); 6903 EXPECT_EQ("\"some \"\n" 6904 "\"text\"", 6905 format("\"some text\"", getLLVMStyleWithColumns(10))); 6906 EXPECT_EQ("\"some \"\n" 6907 "\"text\"", 6908 format("\"some text\"", getLLVMStyleWithColumns(7))); 6909 EXPECT_EQ("\"some\"\n" 6910 "\" tex\"\n" 6911 "\"t\"", 6912 format("\"some text\"", getLLVMStyleWithColumns(6))); 6913 EXPECT_EQ("\"some\"\n" 6914 "\" tex\"\n" 6915 "\" and\"", 6916 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 6917 EXPECT_EQ("\"some\"\n" 6918 "\"/tex\"\n" 6919 "\"/and\"", 6920 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 6921 6922 EXPECT_EQ("variable =\n" 6923 " \"long string \"\n" 6924 " \"literal\";", 6925 format("variable = \"long string literal\";", 6926 getLLVMStyleWithColumns(20))); 6927 6928 EXPECT_EQ("variable = f(\n" 6929 " \"long string \"\n" 6930 " \"literal\",\n" 6931 " short,\n" 6932 " loooooooooooooooooooong);", 6933 format("variable = f(\"long string literal\", short, " 6934 "loooooooooooooooooooong);", 6935 getLLVMStyleWithColumns(20))); 6936 6937 EXPECT_EQ( 6938 "f(g(\"long string \"\n" 6939 " \"literal\"),\n" 6940 " b);", 6941 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 6942 EXPECT_EQ("f(g(\"long string \"\n" 6943 " \"literal\",\n" 6944 " a),\n" 6945 " b);", 6946 format("f(g(\"long string literal\", a), b);", 6947 getLLVMStyleWithColumns(20))); 6948 EXPECT_EQ( 6949 "f(\"one two\".split(\n" 6950 " variable));", 6951 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 6952 EXPECT_EQ("f(\"one two three four five six \"\n" 6953 " \"seven\".split(\n" 6954 " really_looooong_variable));", 6955 format("f(\"one two three four five six seven\"." 6956 "split(really_looooong_variable));", 6957 getLLVMStyleWithColumns(33))); 6958 6959 EXPECT_EQ("f(\"some \"\n" 6960 " \"text\",\n" 6961 " other);", 6962 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 6963 6964 // Only break as a last resort. 6965 verifyFormat( 6966 "aaaaaaaaaaaaaaaaaaaa(\n" 6967 " aaaaaaaaaaaaaaaaaaaa,\n" 6968 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 6969 6970 EXPECT_EQ("\"splitmea\"\n" 6971 "\"trandomp\"\n" 6972 "\"oint\"", 6973 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 6974 6975 EXPECT_EQ("\"split/\"\n" 6976 "\"pathat/\"\n" 6977 "\"slashes\"", 6978 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6979 6980 EXPECT_EQ("\"split/\"\n" 6981 "\"pathat/\"\n" 6982 "\"slashes\"", 6983 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6984 EXPECT_EQ("\"split at \"\n" 6985 "\"spaces/at/\"\n" 6986 "\"slashes.at.any$\"\n" 6987 "\"non-alphanumeric%\"\n" 6988 "\"1111111111characte\"\n" 6989 "\"rs\"", 6990 format("\"split at " 6991 "spaces/at/" 6992 "slashes.at." 6993 "any$non-" 6994 "alphanumeric%" 6995 "1111111111characte" 6996 "rs\"", 6997 getLLVMStyleWithColumns(20))); 6998 6999 // Verify that splitting the strings understands 7000 // Style::AlwaysBreakBeforeMultilineStrings. 7001 EXPECT_EQ( 7002 "aaaaaaaaaaaa(\n" 7003 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7004 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7005 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7006 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7007 "aaaaaaaaaaaaaaaaaaaaaa\");", 7008 getGoogleStyle())); 7009 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7010 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7011 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7012 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7013 "aaaaaaaaaaaaaaaaaaaaaa\";", 7014 getGoogleStyle())); 7015 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7016 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7017 format("llvm::outs() << " 7018 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7019 "aaaaaaaaaaaaaaaaaaa\";")); 7020 EXPECT_EQ("ffff(\n" 7021 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7022 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7023 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7024 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7025 getGoogleStyle())); 7026 7027 FormatStyle Style = getLLVMStyleWithColumns(12); 7028 Style.BreakStringLiterals = false; 7029 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7030 7031 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7032 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7033 EXPECT_EQ("#define A \\\n" 7034 " \"some \" \\\n" 7035 " \"text \" \\\n" 7036 " \"other\";", 7037 format("#define A \"some text other\";", AlignLeft)); 7038 } 7039 7040 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7041 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7042 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7043 EXPECT_EQ("int i = a(b());", 7044 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7045 } 7046 7047 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7048 EXPECT_EQ( 7049 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7050 "(\n" 7051 " \"x\t\");", 7052 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7053 "aaaaaaa(" 7054 "\"x\t\");")); 7055 } 7056 7057 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7058 EXPECT_EQ( 7059 "u8\"utf8 string \"\n" 7060 "u8\"literal\";", 7061 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7062 EXPECT_EQ( 7063 "u\"utf16 string \"\n" 7064 "u\"literal\";", 7065 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7066 EXPECT_EQ( 7067 "U\"utf32 string \"\n" 7068 "U\"literal\";", 7069 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7070 EXPECT_EQ("L\"wide string \"\n" 7071 "L\"literal\";", 7072 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7073 EXPECT_EQ("@\"NSString \"\n" 7074 "@\"literal\";", 7075 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7076 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7077 7078 // This input makes clang-format try to split the incomplete unicode escape 7079 // sequence, which used to lead to a crasher. 7080 verifyNoCrash( 7081 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7082 getLLVMStyleWithColumns(60)); 7083 } 7084 7085 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7086 FormatStyle Style = getGoogleStyleWithColumns(15); 7087 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7088 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7089 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7090 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7091 EXPECT_EQ("u8R\"x(raw literal)x\";", 7092 format("u8R\"x(raw literal)x\";", Style)); 7093 } 7094 7095 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7096 FormatStyle Style = getLLVMStyleWithColumns(20); 7097 EXPECT_EQ( 7098 "_T(\"aaaaaaaaaaaaaa\")\n" 7099 "_T(\"aaaaaaaaaaaaaa\")\n" 7100 "_T(\"aaaaaaaaaaaa\")", 7101 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7102 EXPECT_EQ("f(x,\n" 7103 " _T(\"aaaaaaaaaaaa\")\n" 7104 " _T(\"aaa\"),\n" 7105 " z);", 7106 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7107 7108 // FIXME: Handle embedded spaces in one iteration. 7109 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7110 // "_T(\"aaaaaaaaaaaaa\")\n" 7111 // "_T(\"aaaaaaaaaaaaa\")\n" 7112 // "_T(\"a\")", 7113 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7114 // getLLVMStyleWithColumns(20))); 7115 EXPECT_EQ( 7116 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7117 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7118 EXPECT_EQ("f(\n" 7119 "#if !TEST\n" 7120 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7121 "#endif\n" 7122 ");", 7123 format("f(\n" 7124 "#if !TEST\n" 7125 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7126 "#endif\n" 7127 ");")); 7128 EXPECT_EQ("f(\n" 7129 "\n" 7130 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7131 format("f(\n" 7132 "\n" 7133 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7134 } 7135 7136 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7137 // In a function call with two operands, the second can be broken with no line 7138 // break before it. 7139 EXPECT_EQ("func(a, \"long long \"\n" 7140 " \"long long\");", 7141 format("func(a, \"long long long long\");", 7142 getLLVMStyleWithColumns(24))); 7143 // In a function call with three operands, the second must be broken with a 7144 // line break before it. 7145 EXPECT_EQ("func(a,\n" 7146 " \"long long long \"\n" 7147 " \"long\",\n" 7148 " c);", 7149 format("func(a, \"long long long long\", c);", 7150 getLLVMStyleWithColumns(24))); 7151 // In a function call with three operands, the third must be broken with a 7152 // line break before it. 7153 EXPECT_EQ("func(a, b,\n" 7154 " \"long long long \"\n" 7155 " \"long\");", 7156 format("func(a, b, \"long long long long\");", 7157 getLLVMStyleWithColumns(24))); 7158 // In a function call with three operands, both the second and the third must 7159 // be broken with a line break before them. 7160 EXPECT_EQ("func(a,\n" 7161 " \"long long long \"\n" 7162 " \"long\",\n" 7163 " \"long long long \"\n" 7164 " \"long\");", 7165 format("func(a, \"long long long long\", \"long long long long\");", 7166 getLLVMStyleWithColumns(24))); 7167 // In a chain of << with two operands, the second can be broken with no line 7168 // break before it. 7169 EXPECT_EQ("a << \"line line \"\n" 7170 " \"line\";", 7171 format("a << \"line line line\";", 7172 getLLVMStyleWithColumns(20))); 7173 // In a chain of << with three operands, the second can be broken with no line 7174 // break before it. 7175 EXPECT_EQ("abcde << \"line \"\n" 7176 " \"line line\"\n" 7177 " << c;", 7178 format("abcde << \"line line line\" << c;", 7179 getLLVMStyleWithColumns(20))); 7180 // In a chain of << with three operands, the third must be broken with a line 7181 // break before it. 7182 EXPECT_EQ("a << b\n" 7183 " << \"line line \"\n" 7184 " \"line\";", 7185 format("a << b << \"line line line\";", 7186 getLLVMStyleWithColumns(20))); 7187 // In a chain of << with three operands, the second can be broken with no line 7188 // break before it and the third must be broken with a line break before it. 7189 EXPECT_EQ("abcd << \"line line \"\n" 7190 " \"line\"\n" 7191 " << \"line line \"\n" 7192 " \"line\";", 7193 format("abcd << \"line line line\" << \"line line line\";", 7194 getLLVMStyleWithColumns(20))); 7195 // In a chain of binary operators with two operands, the second can be broken 7196 // with no line break before it. 7197 EXPECT_EQ("abcd + \"line line \"\n" 7198 " \"line line\";", 7199 format("abcd + \"line line line line\";", 7200 getLLVMStyleWithColumns(20))); 7201 // In a chain of binary operators with three operands, the second must be 7202 // broken with a line break before it. 7203 EXPECT_EQ("abcd +\n" 7204 " \"line line \"\n" 7205 " \"line line\" +\n" 7206 " e;", 7207 format("abcd + \"line line line line\" + e;", 7208 getLLVMStyleWithColumns(20))); 7209 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7210 // the first must be broken with a line break before it. 7211 FormatStyle Style = getLLVMStyleWithColumns(25); 7212 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7213 EXPECT_EQ("someFunction(\n" 7214 " \"long long long \"\n" 7215 " \"long\",\n" 7216 " a);", 7217 format("someFunction(\"long long long long\", a);", Style)); 7218 } 7219 7220 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7221 EXPECT_EQ( 7222 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7225 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7228 } 7229 7230 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7231 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7232 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7233 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7234 "multiline raw string literal xxxxxxxxxxxxxx\n" 7235 ")x\",\n" 7236 " a),\n" 7237 " b);", 7238 format("fffffffffff(g(R\"x(\n" 7239 "multiline raw string literal xxxxxxxxxxxxxx\n" 7240 ")x\", a), b);", 7241 getGoogleStyleWithColumns(20))); 7242 EXPECT_EQ("fffffffffff(\n" 7243 " g(R\"x(qqq\n" 7244 "multiline raw string literal xxxxxxxxxxxxxx\n" 7245 ")x\",\n" 7246 " a),\n" 7247 " b);", 7248 format("fffffffffff(g(R\"x(qqq\n" 7249 "multiline raw string literal xxxxxxxxxxxxxx\n" 7250 ")x\", a), b);", 7251 getGoogleStyleWithColumns(20))); 7252 7253 EXPECT_EQ("fffffffffff(R\"x(\n" 7254 "multiline raw string literal xxxxxxxxxxxxxx\n" 7255 ")x\");", 7256 format("fffffffffff(R\"x(\n" 7257 "multiline raw string literal xxxxxxxxxxxxxx\n" 7258 ")x\");", 7259 getGoogleStyleWithColumns(20))); 7260 EXPECT_EQ("fffffffffff(R\"x(\n" 7261 "multiline raw string literal xxxxxxxxxxxxxx\n" 7262 ")x\" + bbbbbb);", 7263 format("fffffffffff(R\"x(\n" 7264 "multiline raw string literal xxxxxxxxxxxxxx\n" 7265 ")x\" + bbbbbb);", 7266 getGoogleStyleWithColumns(20))); 7267 EXPECT_EQ("fffffffffff(\n" 7268 " R\"x(\n" 7269 "multiline raw string literal xxxxxxxxxxxxxx\n" 7270 ")x\" +\n" 7271 " bbbbbb);", 7272 format("fffffffffff(\n" 7273 " R\"x(\n" 7274 "multiline raw string literal xxxxxxxxxxxxxx\n" 7275 ")x\" + bbbbbb);", 7276 getGoogleStyleWithColumns(20))); 7277 } 7278 7279 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7280 verifyFormat("string a = \"unterminated;"); 7281 EXPECT_EQ("function(\"unterminated,\n" 7282 " OtherParameter);", 7283 format("function( \"unterminated,\n" 7284 " OtherParameter);")); 7285 } 7286 7287 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7288 FormatStyle Style = getLLVMStyle(); 7289 Style.Standard = FormatStyle::LS_Cpp03; 7290 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7291 format("#define x(_a) printf(\"foo\"_a);", Style)); 7292 } 7293 7294 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7295 7296 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7297 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7298 " \"ddeeefff\");", 7299 format("someFunction(\"aaabbbcccdddeeefff\");", 7300 getLLVMStyleWithColumns(25))); 7301 EXPECT_EQ("someFunction1234567890(\n" 7302 " \"aaabbbcccdddeeefff\");", 7303 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7304 getLLVMStyleWithColumns(26))); 7305 EXPECT_EQ("someFunction1234567890(\n" 7306 " \"aaabbbcccdddeeeff\"\n" 7307 " \"f\");", 7308 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7309 getLLVMStyleWithColumns(25))); 7310 EXPECT_EQ("someFunction1234567890(\n" 7311 " \"aaabbbcccdddeeeff\"\n" 7312 " \"f\");", 7313 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7314 getLLVMStyleWithColumns(24))); 7315 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7316 " \"ddde \"\n" 7317 " \"efff\");", 7318 format("someFunction(\"aaabbbcc ddde efff\");", 7319 getLLVMStyleWithColumns(25))); 7320 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7321 " \"ddeeefff\");", 7322 format("someFunction(\"aaabbbccc ddeeefff\");", 7323 getLLVMStyleWithColumns(25))); 7324 EXPECT_EQ("someFunction1234567890(\n" 7325 " \"aaabb \"\n" 7326 " \"cccdddeeefff\");", 7327 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7328 getLLVMStyleWithColumns(25))); 7329 EXPECT_EQ("#define A \\\n" 7330 " string s = \\\n" 7331 " \"123456789\" \\\n" 7332 " \"0\"; \\\n" 7333 " int i;", 7334 format("#define A string s = \"1234567890\"; int i;", 7335 getLLVMStyleWithColumns(20))); 7336 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7337 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7338 " \"dddeeeff\"\n" 7339 " \"f\");", 7340 format("someFunction(\"aaabbbcc dddeeefff\");", 7341 getLLVMStyleWithColumns(25))); 7342 } 7343 7344 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7345 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7346 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7347 EXPECT_EQ("\"test\"\n" 7348 "\"\\n\"", 7349 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7350 EXPECT_EQ("\"tes\\\\\"\n" 7351 "\"n\"", 7352 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7353 EXPECT_EQ("\"\\\\\\\\\"\n" 7354 "\"\\n\"", 7355 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7356 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7357 EXPECT_EQ("\"\\uff01\"\n" 7358 "\"test\"", 7359 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7360 EXPECT_EQ("\"\\Uff01ff02\"", 7361 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7362 EXPECT_EQ("\"\\x000000000001\"\n" 7363 "\"next\"", 7364 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7365 EXPECT_EQ("\"\\x000000000001next\"", 7366 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7367 EXPECT_EQ("\"\\x000000000001\"", 7368 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7369 EXPECT_EQ("\"test\"\n" 7370 "\"\\000000\"\n" 7371 "\"000001\"", 7372 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7373 EXPECT_EQ("\"test\\000\"\n" 7374 "\"00000000\"\n" 7375 "\"1\"", 7376 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7377 } 7378 7379 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7380 verifyFormat("void f() {\n" 7381 " return g() {}\n" 7382 " void h() {}"); 7383 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7384 "g();\n" 7385 "}"); 7386 } 7387 7388 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7389 verifyFormat( 7390 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7391 } 7392 7393 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7394 verifyFormat("class X {\n" 7395 " void f() {\n" 7396 " }\n" 7397 "};", 7398 getLLVMStyleWithColumns(12)); 7399 } 7400 7401 TEST_F(FormatTest, ConfigurableIndentWidth) { 7402 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7403 EightIndent.IndentWidth = 8; 7404 EightIndent.ContinuationIndentWidth = 8; 7405 verifyFormat("void f() {\n" 7406 " someFunction();\n" 7407 " if (true) {\n" 7408 " f();\n" 7409 " }\n" 7410 "}", 7411 EightIndent); 7412 verifyFormat("class X {\n" 7413 " void f() {\n" 7414 " }\n" 7415 "};", 7416 EightIndent); 7417 verifyFormat("int x[] = {\n" 7418 " call(),\n" 7419 " call()};", 7420 EightIndent); 7421 } 7422 7423 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7424 verifyFormat("double\n" 7425 "f();", 7426 getLLVMStyleWithColumns(8)); 7427 } 7428 7429 TEST_F(FormatTest, ConfigurableUseOfTab) { 7430 FormatStyle Tab = getLLVMStyleWithColumns(42); 7431 Tab.IndentWidth = 8; 7432 Tab.UseTab = FormatStyle::UT_Always; 7433 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7434 7435 EXPECT_EQ("if (aaaaaaaa && // q\n" 7436 " bb)\t\t// w\n" 7437 "\t;", 7438 format("if (aaaaaaaa &&// q\n" 7439 "bb)// w\n" 7440 ";", 7441 Tab)); 7442 EXPECT_EQ("if (aaa && bbb) // w\n" 7443 "\t;", 7444 format("if(aaa&&bbb)// w\n" 7445 ";", 7446 Tab)); 7447 7448 verifyFormat("class X {\n" 7449 "\tvoid f() {\n" 7450 "\t\tsomeFunction(parameter1,\n" 7451 "\t\t\t parameter2);\n" 7452 "\t}\n" 7453 "};", 7454 Tab); 7455 verifyFormat("#define A \\\n" 7456 "\tvoid f() { \\\n" 7457 "\t\tsomeFunction( \\\n" 7458 "\t\t parameter1, \\\n" 7459 "\t\t parameter2); \\\n" 7460 "\t}", 7461 Tab); 7462 7463 Tab.TabWidth = 4; 7464 Tab.IndentWidth = 8; 7465 verifyFormat("class TabWidth4Indent8 {\n" 7466 "\t\tvoid f() {\n" 7467 "\t\t\t\tsomeFunction(parameter1,\n" 7468 "\t\t\t\t\t\t\t parameter2);\n" 7469 "\t\t}\n" 7470 "};", 7471 Tab); 7472 7473 Tab.TabWidth = 4; 7474 Tab.IndentWidth = 4; 7475 verifyFormat("class TabWidth4Indent4 {\n" 7476 "\tvoid f() {\n" 7477 "\t\tsomeFunction(parameter1,\n" 7478 "\t\t\t\t\t parameter2);\n" 7479 "\t}\n" 7480 "};", 7481 Tab); 7482 7483 Tab.TabWidth = 8; 7484 Tab.IndentWidth = 4; 7485 verifyFormat("class TabWidth8Indent4 {\n" 7486 " void f() {\n" 7487 "\tsomeFunction(parameter1,\n" 7488 "\t\t parameter2);\n" 7489 " }\n" 7490 "};", 7491 Tab); 7492 7493 Tab.TabWidth = 8; 7494 Tab.IndentWidth = 8; 7495 EXPECT_EQ("/*\n" 7496 "\t a\t\tcomment\n" 7497 "\t in multiple lines\n" 7498 " */", 7499 format(" /*\t \t \n" 7500 " \t \t a\t\tcomment\t \t\n" 7501 " \t \t in multiple lines\t\n" 7502 " \t */", 7503 Tab)); 7504 7505 Tab.UseTab = FormatStyle::UT_ForIndentation; 7506 verifyFormat("{\n" 7507 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7508 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7509 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7510 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7511 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7512 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7513 "};", 7514 Tab); 7515 verifyFormat("enum AA {\n" 7516 "\ta1, // Force multiple lines\n" 7517 "\ta2,\n" 7518 "\ta3\n" 7519 "};", 7520 Tab); 7521 EXPECT_EQ("if (aaaaaaaa && // q\n" 7522 " bb) // w\n" 7523 "\t;", 7524 format("if (aaaaaaaa &&// q\n" 7525 "bb)// w\n" 7526 ";", 7527 Tab)); 7528 verifyFormat("class X {\n" 7529 "\tvoid f() {\n" 7530 "\t\tsomeFunction(parameter1,\n" 7531 "\t\t parameter2);\n" 7532 "\t}\n" 7533 "};", 7534 Tab); 7535 verifyFormat("{\n" 7536 "\tQ(\n" 7537 "\t {\n" 7538 "\t\t int a;\n" 7539 "\t\t someFunction(aaaaaaaa,\n" 7540 "\t\t bbbbbbb);\n" 7541 "\t },\n" 7542 "\t p);\n" 7543 "}", 7544 Tab); 7545 EXPECT_EQ("{\n" 7546 "\t/* aaaa\n" 7547 "\t bbbb */\n" 7548 "}", 7549 format("{\n" 7550 "/* aaaa\n" 7551 " bbbb */\n" 7552 "}", 7553 Tab)); 7554 EXPECT_EQ("{\n" 7555 "\t/*\n" 7556 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7557 "\t bbbbbbbbbbbbb\n" 7558 "\t*/\n" 7559 "}", 7560 format("{\n" 7561 "/*\n" 7562 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7563 "*/\n" 7564 "}", 7565 Tab)); 7566 EXPECT_EQ("{\n" 7567 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7568 "\t// bbbbbbbbbbbbb\n" 7569 "}", 7570 format("{\n" 7571 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7572 "}", 7573 Tab)); 7574 EXPECT_EQ("{\n" 7575 "\t/*\n" 7576 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7577 "\t bbbbbbbbbbbbb\n" 7578 "\t*/\n" 7579 "}", 7580 format("{\n" 7581 "\t/*\n" 7582 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7583 "\t*/\n" 7584 "}", 7585 Tab)); 7586 EXPECT_EQ("{\n" 7587 "\t/*\n" 7588 "\n" 7589 "\t*/\n" 7590 "}", 7591 format("{\n" 7592 "\t/*\n" 7593 "\n" 7594 "\t*/\n" 7595 "}", 7596 Tab)); 7597 EXPECT_EQ("{\n" 7598 "\t/*\n" 7599 " asdf\n" 7600 "\t*/\n" 7601 "}", 7602 format("{\n" 7603 "\t/*\n" 7604 " asdf\n" 7605 "\t*/\n" 7606 "}", 7607 Tab)); 7608 7609 Tab.UseTab = FormatStyle::UT_Never; 7610 EXPECT_EQ("/*\n" 7611 " a\t\tcomment\n" 7612 " in multiple lines\n" 7613 " */", 7614 format(" /*\t \t \n" 7615 " \t \t a\t\tcomment\t \t\n" 7616 " \t \t in multiple lines\t\n" 7617 " \t */", 7618 Tab)); 7619 EXPECT_EQ("/* some\n" 7620 " comment */", 7621 format(" \t \t /* some\n" 7622 " \t \t comment */", 7623 Tab)); 7624 EXPECT_EQ("int a; /* some\n" 7625 " comment */", 7626 format(" \t \t int a; /* some\n" 7627 " \t \t comment */", 7628 Tab)); 7629 7630 EXPECT_EQ("int a; /* some\n" 7631 "comment */", 7632 format(" \t \t int\ta; /* some\n" 7633 " \t \t comment */", 7634 Tab)); 7635 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7636 " comment */", 7637 format(" \t \t f(\"\t\t\"); /* some\n" 7638 " \t \t comment */", 7639 Tab)); 7640 EXPECT_EQ("{\n" 7641 " /*\n" 7642 " * Comment\n" 7643 " */\n" 7644 " int i;\n" 7645 "}", 7646 format("{\n" 7647 "\t/*\n" 7648 "\t * Comment\n" 7649 "\t */\n" 7650 "\t int i;\n" 7651 "}")); 7652 7653 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 7654 Tab.TabWidth = 8; 7655 Tab.IndentWidth = 8; 7656 EXPECT_EQ("if (aaaaaaaa && // q\n" 7657 " bb) // w\n" 7658 "\t;", 7659 format("if (aaaaaaaa &&// q\n" 7660 "bb)// w\n" 7661 ";", 7662 Tab)); 7663 EXPECT_EQ("if (aaa && bbb) // w\n" 7664 "\t;", 7665 format("if(aaa&&bbb)// w\n" 7666 ";", 7667 Tab)); 7668 verifyFormat("class X {\n" 7669 "\tvoid f() {\n" 7670 "\t\tsomeFunction(parameter1,\n" 7671 "\t\t\t parameter2);\n" 7672 "\t}\n" 7673 "};", 7674 Tab); 7675 verifyFormat("#define A \\\n" 7676 "\tvoid f() { \\\n" 7677 "\t\tsomeFunction( \\\n" 7678 "\t\t parameter1, \\\n" 7679 "\t\t parameter2); \\\n" 7680 "\t}", 7681 Tab); 7682 Tab.TabWidth = 4; 7683 Tab.IndentWidth = 8; 7684 verifyFormat("class TabWidth4Indent8 {\n" 7685 "\t\tvoid f() {\n" 7686 "\t\t\t\tsomeFunction(parameter1,\n" 7687 "\t\t\t\t\t\t\t parameter2);\n" 7688 "\t\t}\n" 7689 "};", 7690 Tab); 7691 Tab.TabWidth = 4; 7692 Tab.IndentWidth = 4; 7693 verifyFormat("class TabWidth4Indent4 {\n" 7694 "\tvoid f() {\n" 7695 "\t\tsomeFunction(parameter1,\n" 7696 "\t\t\t\t\t parameter2);\n" 7697 "\t}\n" 7698 "};", 7699 Tab); 7700 Tab.TabWidth = 8; 7701 Tab.IndentWidth = 4; 7702 verifyFormat("class TabWidth8Indent4 {\n" 7703 " void f() {\n" 7704 "\tsomeFunction(parameter1,\n" 7705 "\t\t parameter2);\n" 7706 " }\n" 7707 "};", 7708 Tab); 7709 Tab.TabWidth = 8; 7710 Tab.IndentWidth = 8; 7711 EXPECT_EQ("/*\n" 7712 "\t a\t\tcomment\n" 7713 "\t in multiple lines\n" 7714 " */", 7715 format(" /*\t \t \n" 7716 " \t \t a\t\tcomment\t \t\n" 7717 " \t \t in multiple lines\t\n" 7718 " \t */", 7719 Tab)); 7720 verifyFormat("{\n" 7721 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7722 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7723 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7724 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7725 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7726 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7727 "};", 7728 Tab); 7729 verifyFormat("enum AA {\n" 7730 "\ta1, // Force multiple lines\n" 7731 "\ta2,\n" 7732 "\ta3\n" 7733 "};", 7734 Tab); 7735 EXPECT_EQ("if (aaaaaaaa && // q\n" 7736 " bb) // w\n" 7737 "\t;", 7738 format("if (aaaaaaaa &&// q\n" 7739 "bb)// w\n" 7740 ";", 7741 Tab)); 7742 verifyFormat("class X {\n" 7743 "\tvoid f() {\n" 7744 "\t\tsomeFunction(parameter1,\n" 7745 "\t\t\t parameter2);\n" 7746 "\t}\n" 7747 "};", 7748 Tab); 7749 verifyFormat("{\n" 7750 "\tQ(\n" 7751 "\t {\n" 7752 "\t\t int a;\n" 7753 "\t\t someFunction(aaaaaaaa,\n" 7754 "\t\t\t\t bbbbbbb);\n" 7755 "\t },\n" 7756 "\t p);\n" 7757 "}", 7758 Tab); 7759 EXPECT_EQ("{\n" 7760 "\t/* aaaa\n" 7761 "\t bbbb */\n" 7762 "}", 7763 format("{\n" 7764 "/* aaaa\n" 7765 " bbbb */\n" 7766 "}", 7767 Tab)); 7768 EXPECT_EQ("{\n" 7769 "\t/*\n" 7770 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7771 "\t bbbbbbbbbbbbb\n" 7772 "\t*/\n" 7773 "}", 7774 format("{\n" 7775 "/*\n" 7776 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7777 "*/\n" 7778 "}", 7779 Tab)); 7780 EXPECT_EQ("{\n" 7781 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7782 "\t// bbbbbbbbbbbbb\n" 7783 "}", 7784 format("{\n" 7785 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7786 "}", 7787 Tab)); 7788 EXPECT_EQ("{\n" 7789 "\t/*\n" 7790 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7791 "\t bbbbbbbbbbbbb\n" 7792 "\t*/\n" 7793 "}", 7794 format("{\n" 7795 "\t/*\n" 7796 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7797 "\t*/\n" 7798 "}", 7799 Tab)); 7800 EXPECT_EQ("{\n" 7801 "\t/*\n" 7802 "\n" 7803 "\t*/\n" 7804 "}", 7805 format("{\n" 7806 "\t/*\n" 7807 "\n" 7808 "\t*/\n" 7809 "}", 7810 Tab)); 7811 EXPECT_EQ("{\n" 7812 "\t/*\n" 7813 " asdf\n" 7814 "\t*/\n" 7815 "}", 7816 format("{\n" 7817 "\t/*\n" 7818 " asdf\n" 7819 "\t*/\n" 7820 "}", 7821 Tab)); 7822 EXPECT_EQ("/*\n" 7823 "\t a\t\tcomment\n" 7824 "\t in multiple lines\n" 7825 " */", 7826 format(" /*\t \t \n" 7827 " \t \t a\t\tcomment\t \t\n" 7828 " \t \t in multiple lines\t\n" 7829 " \t */", 7830 Tab)); 7831 EXPECT_EQ("/* some\n" 7832 " comment */", 7833 format(" \t \t /* some\n" 7834 " \t \t comment */", 7835 Tab)); 7836 EXPECT_EQ("int a; /* some\n" 7837 " comment */", 7838 format(" \t \t int a; /* some\n" 7839 " \t \t comment */", 7840 Tab)); 7841 EXPECT_EQ("int a; /* some\n" 7842 "comment */", 7843 format(" \t \t int\ta; /* some\n" 7844 " \t \t comment */", 7845 Tab)); 7846 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7847 " comment */", 7848 format(" \t \t f(\"\t\t\"); /* some\n" 7849 " \t \t comment */", 7850 Tab)); 7851 EXPECT_EQ("{\n" 7852 " /*\n" 7853 " * Comment\n" 7854 " */\n" 7855 " int i;\n" 7856 "}", 7857 format("{\n" 7858 "\t/*\n" 7859 "\t * Comment\n" 7860 "\t */\n" 7861 "\t int i;\n" 7862 "}")); 7863 Tab.AlignConsecutiveAssignments = true; 7864 Tab.AlignConsecutiveDeclarations = true; 7865 Tab.TabWidth = 4; 7866 Tab.IndentWidth = 4; 7867 verifyFormat("class Assign {\n" 7868 "\tvoid f() {\n" 7869 "\t\tint x = 123;\n" 7870 "\t\tint random = 4;\n" 7871 "\t\tstd::string alphabet =\n" 7872 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 7873 "\t}\n" 7874 "};", 7875 Tab); 7876 } 7877 7878 TEST_F(FormatTest, CalculatesOriginalColumn) { 7879 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7880 "q\"; /* some\n" 7881 " comment */", 7882 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7883 "q\"; /* some\n" 7884 " comment */", 7885 getLLVMStyle())); 7886 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7887 "/* some\n" 7888 " comment */", 7889 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7890 " /* some\n" 7891 " comment */", 7892 getLLVMStyle())); 7893 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7894 "qqq\n" 7895 "/* some\n" 7896 " comment */", 7897 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7898 "qqq\n" 7899 " /* some\n" 7900 " comment */", 7901 getLLVMStyle())); 7902 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7903 "wwww; /* some\n" 7904 " comment */", 7905 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7906 "wwww; /* some\n" 7907 " comment */", 7908 getLLVMStyle())); 7909 } 7910 7911 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 7912 FormatStyle NoSpace = getLLVMStyle(); 7913 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 7914 7915 verifyFormat("while(true)\n" 7916 " continue;", 7917 NoSpace); 7918 verifyFormat("for(;;)\n" 7919 " continue;", 7920 NoSpace); 7921 verifyFormat("if(true)\n" 7922 " f();\n" 7923 "else if(true)\n" 7924 " f();", 7925 NoSpace); 7926 verifyFormat("do {\n" 7927 " do_something();\n" 7928 "} while(something());", 7929 NoSpace); 7930 verifyFormat("switch(x) {\n" 7931 "default:\n" 7932 " break;\n" 7933 "}", 7934 NoSpace); 7935 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 7936 verifyFormat("size_t x = sizeof(x);", NoSpace); 7937 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 7938 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 7939 verifyFormat("alignas(128) char a[128];", NoSpace); 7940 verifyFormat("size_t x = alignof(MyType);", NoSpace); 7941 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 7942 verifyFormat("int f() throw(Deprecated);", NoSpace); 7943 verifyFormat("typedef void (*cb)(int);", NoSpace); 7944 verifyFormat("T A::operator()();", NoSpace); 7945 verifyFormat("X A::operator++(T);", NoSpace); 7946 7947 FormatStyle Space = getLLVMStyle(); 7948 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 7949 7950 verifyFormat("int f ();", Space); 7951 verifyFormat("void f (int a, T b) {\n" 7952 " while (true)\n" 7953 " continue;\n" 7954 "}", 7955 Space); 7956 verifyFormat("if (true)\n" 7957 " f ();\n" 7958 "else if (true)\n" 7959 " f ();", 7960 Space); 7961 verifyFormat("do {\n" 7962 " do_something ();\n" 7963 "} while (something ());", 7964 Space); 7965 verifyFormat("switch (x) {\n" 7966 "default:\n" 7967 " break;\n" 7968 "}", 7969 Space); 7970 verifyFormat("A::A () : a (1) {}", Space); 7971 verifyFormat("void f () __attribute__ ((asdf));", Space); 7972 verifyFormat("*(&a + 1);\n" 7973 "&((&a)[1]);\n" 7974 "a[(b + c) * d];\n" 7975 "(((a + 1) * 2) + 3) * 4;", 7976 Space); 7977 verifyFormat("#define A(x) x", Space); 7978 verifyFormat("#define A (x) x", Space); 7979 verifyFormat("#if defined(x)\n" 7980 "#endif", 7981 Space); 7982 verifyFormat("auto i = std::make_unique<int> (5);", Space); 7983 verifyFormat("size_t x = sizeof (x);", Space); 7984 verifyFormat("auto f (int x) -> decltype (x);", Space); 7985 verifyFormat("int f (T x) noexcept (x.create ());", Space); 7986 verifyFormat("alignas (128) char a[128];", Space); 7987 verifyFormat("size_t x = alignof (MyType);", Space); 7988 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 7989 verifyFormat("int f () throw (Deprecated);", Space); 7990 verifyFormat("typedef void (*cb) (int);", Space); 7991 verifyFormat("T A::operator() ();", Space); 7992 verifyFormat("X A::operator++ (T);", Space); 7993 } 7994 7995 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 7996 FormatStyle Spaces = getLLVMStyle(); 7997 7998 Spaces.SpacesInParentheses = true; 7999 verifyFormat("call( x, y, z );", Spaces); 8000 verifyFormat("call();", Spaces); 8001 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8002 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8003 Spaces); 8004 verifyFormat("while ( (bool)1 )\n" 8005 " continue;", 8006 Spaces); 8007 verifyFormat("for ( ;; )\n" 8008 " continue;", 8009 Spaces); 8010 verifyFormat("if ( true )\n" 8011 " f();\n" 8012 "else if ( true )\n" 8013 " f();", 8014 Spaces); 8015 verifyFormat("do {\n" 8016 " do_something( (int)i );\n" 8017 "} while ( something() );", 8018 Spaces); 8019 verifyFormat("switch ( x ) {\n" 8020 "default:\n" 8021 " break;\n" 8022 "}", 8023 Spaces); 8024 8025 Spaces.SpacesInParentheses = false; 8026 Spaces.SpacesInCStyleCastParentheses = true; 8027 verifyFormat("Type *A = ( Type * )P;", Spaces); 8028 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8029 verifyFormat("x = ( int32 )y;", Spaces); 8030 verifyFormat("int a = ( int )(2.0f);", Spaces); 8031 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8032 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8033 verifyFormat("#define x (( int )-1)", Spaces); 8034 8035 // Run the first set of tests again with: 8036 Spaces.SpacesInParentheses = false; 8037 Spaces.SpaceInEmptyParentheses = true; 8038 Spaces.SpacesInCStyleCastParentheses = true; 8039 verifyFormat("call(x, y, z);", Spaces); 8040 verifyFormat("call( );", Spaces); 8041 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8042 verifyFormat("while (( bool )1)\n" 8043 " continue;", 8044 Spaces); 8045 verifyFormat("for (;;)\n" 8046 " continue;", 8047 Spaces); 8048 verifyFormat("if (true)\n" 8049 " f( );\n" 8050 "else if (true)\n" 8051 " f( );", 8052 Spaces); 8053 verifyFormat("do {\n" 8054 " do_something(( int )i);\n" 8055 "} while (something( ));", 8056 Spaces); 8057 verifyFormat("switch (x) {\n" 8058 "default:\n" 8059 " break;\n" 8060 "}", 8061 Spaces); 8062 8063 // Run the first set of tests again with: 8064 Spaces.SpaceAfterCStyleCast = true; 8065 verifyFormat("call(x, y, z);", Spaces); 8066 verifyFormat("call( );", Spaces); 8067 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8068 verifyFormat("while (( bool ) 1)\n" 8069 " continue;", 8070 Spaces); 8071 verifyFormat("for (;;)\n" 8072 " continue;", 8073 Spaces); 8074 verifyFormat("if (true)\n" 8075 " f( );\n" 8076 "else if (true)\n" 8077 " f( );", 8078 Spaces); 8079 verifyFormat("do {\n" 8080 " do_something(( int ) i);\n" 8081 "} while (something( ));", 8082 Spaces); 8083 verifyFormat("switch (x) {\n" 8084 "default:\n" 8085 " break;\n" 8086 "}", 8087 Spaces); 8088 8089 // Run subset of tests again with: 8090 Spaces.SpacesInCStyleCastParentheses = false; 8091 Spaces.SpaceAfterCStyleCast = true; 8092 verifyFormat("while ((bool) 1)\n" 8093 " continue;", 8094 Spaces); 8095 verifyFormat("do {\n" 8096 " do_something((int) i);\n" 8097 "} while (something( ));", 8098 Spaces); 8099 } 8100 8101 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8102 verifyFormat("int a[5];"); 8103 verifyFormat("a[3] += 42;"); 8104 8105 FormatStyle Spaces = getLLVMStyle(); 8106 Spaces.SpacesInSquareBrackets = true; 8107 // Lambdas unchanged. 8108 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8109 verifyFormat("return [i, args...] {};", Spaces); 8110 8111 // Not lambdas. 8112 verifyFormat("int a[ 5 ];", Spaces); 8113 verifyFormat("a[ 3 ] += 42;", Spaces); 8114 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8115 verifyFormat("double &operator[](int i) { return 0; }\n" 8116 "int i;", 8117 Spaces); 8118 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8119 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8120 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8121 } 8122 8123 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8124 verifyFormat("int a = 5;"); 8125 verifyFormat("a += 42;"); 8126 verifyFormat("a or_eq 8;"); 8127 8128 FormatStyle Spaces = getLLVMStyle(); 8129 Spaces.SpaceBeforeAssignmentOperators = false; 8130 verifyFormat("int a= 5;", Spaces); 8131 verifyFormat("a+= 42;", Spaces); 8132 verifyFormat("a or_eq 8;", Spaces); 8133 } 8134 8135 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8136 FormatStyle Alignment = getLLVMStyle(); 8137 Alignment.AlignConsecutiveAssignments = false; 8138 verifyFormat("int a = 5;\n" 8139 "int oneTwoThree = 123;", 8140 Alignment); 8141 verifyFormat("int a = 5;\n" 8142 "int oneTwoThree = 123;", 8143 Alignment); 8144 8145 Alignment.AlignConsecutiveAssignments = true; 8146 verifyFormat("int a = 5;\n" 8147 "int oneTwoThree = 123;", 8148 Alignment); 8149 verifyFormat("int a = method();\n" 8150 "int oneTwoThree = 133;", 8151 Alignment); 8152 verifyFormat("a &= 5;\n" 8153 "bcd *= 5;\n" 8154 "ghtyf += 5;\n" 8155 "dvfvdb -= 5;\n" 8156 "a /= 5;\n" 8157 "vdsvsv %= 5;\n" 8158 "sfdbddfbdfbb ^= 5;\n" 8159 "dvsdsv |= 5;\n" 8160 "int dsvvdvsdvvv = 123;", 8161 Alignment); 8162 verifyFormat("int i = 1, j = 10;\n" 8163 "something = 2000;", 8164 Alignment); 8165 verifyFormat("something = 2000;\n" 8166 "int i = 1, j = 10;\n", 8167 Alignment); 8168 verifyFormat("something = 2000;\n" 8169 "another = 911;\n" 8170 "int i = 1, j = 10;\n" 8171 "oneMore = 1;\n" 8172 "i = 2;", 8173 Alignment); 8174 verifyFormat("int a = 5;\n" 8175 "int one = 1;\n" 8176 "method();\n" 8177 "int oneTwoThree = 123;\n" 8178 "int oneTwo = 12;", 8179 Alignment); 8180 verifyFormat("int oneTwoThree = 123;\n" 8181 "int oneTwo = 12;\n" 8182 "method();\n", 8183 Alignment); 8184 verifyFormat("int oneTwoThree = 123; // comment\n" 8185 "int oneTwo = 12; // comment", 8186 Alignment); 8187 EXPECT_EQ("int a = 5;\n" 8188 "\n" 8189 "int oneTwoThree = 123;", 8190 format("int a = 5;\n" 8191 "\n" 8192 "int oneTwoThree= 123;", 8193 Alignment)); 8194 EXPECT_EQ("int a = 5;\n" 8195 "int one = 1;\n" 8196 "\n" 8197 "int oneTwoThree = 123;", 8198 format("int a = 5;\n" 8199 "int one = 1;\n" 8200 "\n" 8201 "int oneTwoThree = 123;", 8202 Alignment)); 8203 EXPECT_EQ("int a = 5;\n" 8204 "int one = 1;\n" 8205 "\n" 8206 "int oneTwoThree = 123;\n" 8207 "int oneTwo = 12;", 8208 format("int a = 5;\n" 8209 "int one = 1;\n" 8210 "\n" 8211 "int oneTwoThree = 123;\n" 8212 "int oneTwo = 12;", 8213 Alignment)); 8214 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8215 verifyFormat("#define A \\\n" 8216 " int aaaa = 12; \\\n" 8217 " int b = 23; \\\n" 8218 " int ccc = 234; \\\n" 8219 " int dddddddddd = 2345;", 8220 Alignment); 8221 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8222 verifyFormat("#define A \\\n" 8223 " int aaaa = 12; \\\n" 8224 " int b = 23; \\\n" 8225 " int ccc = 234; \\\n" 8226 " int dddddddddd = 2345;", 8227 Alignment); 8228 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8229 verifyFormat("#define A " 8230 " \\\n" 8231 " int aaaa = 12; " 8232 " \\\n" 8233 " int b = 23; " 8234 " \\\n" 8235 " int ccc = 234; " 8236 " \\\n" 8237 " int dddddddddd = 2345;", 8238 Alignment); 8239 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8240 "k = 4, int l = 5,\n" 8241 " int m = 6) {\n" 8242 " int j = 10;\n" 8243 " otherThing = 1;\n" 8244 "}", 8245 Alignment); 8246 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8247 " int i = 1;\n" 8248 " int j = 2;\n" 8249 " int big = 10000;\n" 8250 "}", 8251 Alignment); 8252 verifyFormat("class C {\n" 8253 "public:\n" 8254 " int i = 1;\n" 8255 " virtual void f() = 0;\n" 8256 "};", 8257 Alignment); 8258 verifyFormat("int i = 1;\n" 8259 "if (SomeType t = getSomething()) {\n" 8260 "}\n" 8261 "int j = 2;\n" 8262 "int big = 10000;", 8263 Alignment); 8264 verifyFormat("int j = 7;\n" 8265 "for (int k = 0; k < N; ++k) {\n" 8266 "}\n" 8267 "int j = 2;\n" 8268 "int big = 10000;\n" 8269 "}", 8270 Alignment); 8271 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8272 verifyFormat("int i = 1;\n" 8273 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8274 " = someLooooooooooooooooongFunction();\n" 8275 "int j = 2;", 8276 Alignment); 8277 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8278 verifyFormat("int i = 1;\n" 8279 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8280 " someLooooooooooooooooongFunction();\n" 8281 "int j = 2;", 8282 Alignment); 8283 8284 verifyFormat("auto lambda = []() {\n" 8285 " auto i = 0;\n" 8286 " return 0;\n" 8287 "};\n" 8288 "int i = 0;\n" 8289 "auto v = type{\n" 8290 " i = 1, //\n" 8291 " (i = 2), //\n" 8292 " i = 3 //\n" 8293 "};", 8294 Alignment); 8295 8296 verifyFormat( 8297 "int i = 1;\n" 8298 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8299 " loooooooooooooooooooooongParameterB);\n" 8300 "int j = 2;", 8301 Alignment); 8302 8303 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8304 " typename B = very_long_type_name_1,\n" 8305 " typename T_2 = very_long_type_name_2>\n" 8306 "auto foo() {}\n", 8307 Alignment); 8308 verifyFormat("int a, b = 1;\n" 8309 "int c = 2;\n" 8310 "int dd = 3;\n", 8311 Alignment); 8312 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8313 "float b[1][] = {{3.f}};\n", 8314 Alignment); 8315 verifyFormat("for (int i = 0; i < 1; i++)\n" 8316 " int x = 1;\n", 8317 Alignment); 8318 verifyFormat("for (i = 0; i < 1; i++)\n" 8319 " x = 1;\n" 8320 "y = 1;\n", 8321 Alignment); 8322 } 8323 8324 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8325 FormatStyle Alignment = getLLVMStyle(); 8326 Alignment.AlignConsecutiveDeclarations = false; 8327 verifyFormat("float const a = 5;\n" 8328 "int oneTwoThree = 123;", 8329 Alignment); 8330 verifyFormat("int a = 5;\n" 8331 "float const oneTwoThree = 123;", 8332 Alignment); 8333 8334 Alignment.AlignConsecutiveDeclarations = true; 8335 verifyFormat("float const a = 5;\n" 8336 "int oneTwoThree = 123;", 8337 Alignment); 8338 verifyFormat("int a = method();\n" 8339 "float const oneTwoThree = 133;", 8340 Alignment); 8341 verifyFormat("int i = 1, j = 10;\n" 8342 "something = 2000;", 8343 Alignment); 8344 verifyFormat("something = 2000;\n" 8345 "int i = 1, j = 10;\n", 8346 Alignment); 8347 verifyFormat("float something = 2000;\n" 8348 "double another = 911;\n" 8349 "int i = 1, j = 10;\n" 8350 "const int *oneMore = 1;\n" 8351 "unsigned i = 2;", 8352 Alignment); 8353 verifyFormat("float a = 5;\n" 8354 "int one = 1;\n" 8355 "method();\n" 8356 "const double oneTwoThree = 123;\n" 8357 "const unsigned int oneTwo = 12;", 8358 Alignment); 8359 verifyFormat("int oneTwoThree{0}; // comment\n" 8360 "unsigned oneTwo; // comment", 8361 Alignment); 8362 EXPECT_EQ("float const a = 5;\n" 8363 "\n" 8364 "int oneTwoThree = 123;", 8365 format("float const a = 5;\n" 8366 "\n" 8367 "int oneTwoThree= 123;", 8368 Alignment)); 8369 EXPECT_EQ("float a = 5;\n" 8370 "int one = 1;\n" 8371 "\n" 8372 "unsigned oneTwoThree = 123;", 8373 format("float a = 5;\n" 8374 "int one = 1;\n" 8375 "\n" 8376 "unsigned oneTwoThree = 123;", 8377 Alignment)); 8378 EXPECT_EQ("float a = 5;\n" 8379 "int one = 1;\n" 8380 "\n" 8381 "unsigned oneTwoThree = 123;\n" 8382 "int oneTwo = 12;", 8383 format("float a = 5;\n" 8384 "int one = 1;\n" 8385 "\n" 8386 "unsigned oneTwoThree = 123;\n" 8387 "int oneTwo = 12;", 8388 Alignment)); 8389 // Function prototype alignment 8390 verifyFormat("int a();\n" 8391 "double b();", 8392 Alignment); 8393 verifyFormat("int a(int x);\n" 8394 "double b();", 8395 Alignment); 8396 unsigned OldColumnLimit = Alignment.ColumnLimit; 8397 // We need to set ColumnLimit to zero, in order to stress nested alignments, 8398 // otherwise the function parameters will be re-flowed onto a single line. 8399 Alignment.ColumnLimit = 0; 8400 EXPECT_EQ("int a(int x,\n" 8401 " float y);\n" 8402 "double b(int x,\n" 8403 " double y);", 8404 format("int a(int x,\n" 8405 " float y);\n" 8406 "double b(int x,\n" 8407 " double y);", 8408 Alignment)); 8409 // This ensures that function parameters of function declarations are 8410 // correctly indented when their owning functions are indented. 8411 // The failure case here is for 'double y' to not be indented enough. 8412 EXPECT_EQ("double a(int x);\n" 8413 "int b(int y,\n" 8414 " double z);", 8415 format("double a(int x);\n" 8416 "int b(int y,\n" 8417 " double z);", 8418 Alignment)); 8419 // Set ColumnLimit low so that we induce wrapping immediately after 8420 // the function name and opening paren. 8421 Alignment.ColumnLimit = 13; 8422 verifyFormat("int function(\n" 8423 " int x,\n" 8424 " bool y);", 8425 Alignment); 8426 Alignment.ColumnLimit = OldColumnLimit; 8427 // Ensure function pointers don't screw up recursive alignment 8428 verifyFormat("int a(int x, void (*fp)(int y));\n" 8429 "double b();", 8430 Alignment); 8431 Alignment.AlignConsecutiveAssignments = true; 8432 // Ensure recursive alignment is broken by function braces, so that the 8433 // "a = 1" does not align with subsequent assignments inside the function 8434 // body. 8435 verifyFormat("int func(int a = 1) {\n" 8436 " int b = 2;\n" 8437 " int cc = 3;\n" 8438 "}", 8439 Alignment); 8440 verifyFormat("float something = 2000;\n" 8441 "double another = 911;\n" 8442 "int i = 1, j = 10;\n" 8443 "const int *oneMore = 1;\n" 8444 "unsigned i = 2;", 8445 Alignment); 8446 verifyFormat("int oneTwoThree = {0}; // comment\n" 8447 "unsigned oneTwo = 0; // comment", 8448 Alignment); 8449 // Make sure that scope is correctly tracked, in the absence of braces 8450 verifyFormat("for (int i = 0; i < n; i++)\n" 8451 " j = i;\n" 8452 "double x = 1;\n", 8453 Alignment); 8454 verifyFormat("if (int i = 0)\n" 8455 " j = i;\n" 8456 "double x = 1;\n", 8457 Alignment); 8458 // Ensure operator[] and operator() are comprehended 8459 verifyFormat("struct test {\n" 8460 " long long int foo();\n" 8461 " int operator[](int a);\n" 8462 " double bar();\n" 8463 "};\n", 8464 Alignment); 8465 verifyFormat("struct test {\n" 8466 " long long int foo();\n" 8467 " int operator()(int a);\n" 8468 " double bar();\n" 8469 "};\n", 8470 Alignment); 8471 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8472 " int const i = 1;\n" 8473 " int * j = 2;\n" 8474 " int big = 10000;\n" 8475 "\n" 8476 " unsigned oneTwoThree = 123;\n" 8477 " int oneTwo = 12;\n" 8478 " method();\n" 8479 " float k = 2;\n" 8480 " int ll = 10000;\n" 8481 "}", 8482 format("void SomeFunction(int parameter= 0) {\n" 8483 " int const i= 1;\n" 8484 " int *j=2;\n" 8485 " int big = 10000;\n" 8486 "\n" 8487 "unsigned oneTwoThree =123;\n" 8488 "int oneTwo = 12;\n" 8489 " method();\n" 8490 "float k= 2;\n" 8491 "int ll=10000;\n" 8492 "}", 8493 Alignment)); 8494 Alignment.AlignConsecutiveAssignments = false; 8495 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8496 verifyFormat("#define A \\\n" 8497 " int aaaa = 12; \\\n" 8498 " float b = 23; \\\n" 8499 " const int ccc = 234; \\\n" 8500 " unsigned dddddddddd = 2345;", 8501 Alignment); 8502 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8503 verifyFormat("#define A \\\n" 8504 " int aaaa = 12; \\\n" 8505 " float b = 23; \\\n" 8506 " const int ccc = 234; \\\n" 8507 " unsigned dddddddddd = 2345;", 8508 Alignment); 8509 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8510 Alignment.ColumnLimit = 30; 8511 verifyFormat("#define A \\\n" 8512 " int aaaa = 12; \\\n" 8513 " float b = 23; \\\n" 8514 " const int ccc = 234; \\\n" 8515 " int dddddddddd = 2345;", 8516 Alignment); 8517 Alignment.ColumnLimit = 80; 8518 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8519 "k = 4, int l = 5,\n" 8520 " int m = 6) {\n" 8521 " const int j = 10;\n" 8522 " otherThing = 1;\n" 8523 "}", 8524 Alignment); 8525 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8526 " int const i = 1;\n" 8527 " int * j = 2;\n" 8528 " int big = 10000;\n" 8529 "}", 8530 Alignment); 8531 verifyFormat("class C {\n" 8532 "public:\n" 8533 " int i = 1;\n" 8534 " virtual void f() = 0;\n" 8535 "};", 8536 Alignment); 8537 verifyFormat("float i = 1;\n" 8538 "if (SomeType t = getSomething()) {\n" 8539 "}\n" 8540 "const unsigned j = 2;\n" 8541 "int big = 10000;", 8542 Alignment); 8543 verifyFormat("float j = 7;\n" 8544 "for (int k = 0; k < N; ++k) {\n" 8545 "}\n" 8546 "unsigned j = 2;\n" 8547 "int big = 10000;\n" 8548 "}", 8549 Alignment); 8550 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8551 verifyFormat("float i = 1;\n" 8552 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8553 " = someLooooooooooooooooongFunction();\n" 8554 "int j = 2;", 8555 Alignment); 8556 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8557 verifyFormat("int i = 1;\n" 8558 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8559 " someLooooooooooooooooongFunction();\n" 8560 "int j = 2;", 8561 Alignment); 8562 8563 Alignment.AlignConsecutiveAssignments = true; 8564 verifyFormat("auto lambda = []() {\n" 8565 " auto ii = 0;\n" 8566 " float j = 0;\n" 8567 " return 0;\n" 8568 "};\n" 8569 "int i = 0;\n" 8570 "float i2 = 0;\n" 8571 "auto v = type{\n" 8572 " i = 1, //\n" 8573 " (i = 2), //\n" 8574 " i = 3 //\n" 8575 "};", 8576 Alignment); 8577 Alignment.AlignConsecutiveAssignments = false; 8578 8579 verifyFormat( 8580 "int i = 1;\n" 8581 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8582 " loooooooooooooooooooooongParameterB);\n" 8583 "int j = 2;", 8584 Alignment); 8585 8586 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8587 // We expect declarations and assignments to align, as long as it doesn't 8588 // exceed the column limit, starting a new alignment sequence whenever it 8589 // happens. 8590 Alignment.AlignConsecutiveAssignments = true; 8591 Alignment.ColumnLimit = 30; 8592 verifyFormat("float ii = 1;\n" 8593 "unsigned j = 2;\n" 8594 "int someVerylongVariable = 1;\n" 8595 "AnotherLongType ll = 123456;\n" 8596 "VeryVeryLongType k = 2;\n" 8597 "int myvar = 1;", 8598 Alignment); 8599 Alignment.ColumnLimit = 80; 8600 Alignment.AlignConsecutiveAssignments = false; 8601 8602 verifyFormat( 8603 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8604 " typename LongType, typename B>\n" 8605 "auto foo() {}\n", 8606 Alignment); 8607 verifyFormat("float a, b = 1;\n" 8608 "int c = 2;\n" 8609 "int dd = 3;\n", 8610 Alignment); 8611 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8612 "float b[1][] = {{3.f}};\n", 8613 Alignment); 8614 Alignment.AlignConsecutiveAssignments = true; 8615 verifyFormat("float a, b = 1;\n" 8616 "int c = 2;\n" 8617 "int dd = 3;\n", 8618 Alignment); 8619 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8620 "float b[1][] = {{3.f}};\n", 8621 Alignment); 8622 Alignment.AlignConsecutiveAssignments = false; 8623 8624 Alignment.ColumnLimit = 30; 8625 Alignment.BinPackParameters = false; 8626 verifyFormat("void foo(float a,\n" 8627 " float b,\n" 8628 " int c,\n" 8629 " uint32_t *d) {\n" 8630 " int * e = 0;\n" 8631 " float f = 0;\n" 8632 " double g = 0;\n" 8633 "}\n" 8634 "void bar(ino_t a,\n" 8635 " int b,\n" 8636 " uint32_t *c,\n" 8637 " bool d) {}\n", 8638 Alignment); 8639 Alignment.BinPackParameters = true; 8640 Alignment.ColumnLimit = 80; 8641 } 8642 8643 TEST_F(FormatTest, LinuxBraceBreaking) { 8644 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8645 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8646 verifyFormat("namespace a\n" 8647 "{\n" 8648 "class A\n" 8649 "{\n" 8650 " void f()\n" 8651 " {\n" 8652 " if (true) {\n" 8653 " a();\n" 8654 " b();\n" 8655 " } else {\n" 8656 " a();\n" 8657 " }\n" 8658 " }\n" 8659 " void g() { return; }\n" 8660 "};\n" 8661 "struct B {\n" 8662 " int x;\n" 8663 "};\n" 8664 "}\n", 8665 LinuxBraceStyle); 8666 verifyFormat("enum X {\n" 8667 " Y = 0,\n" 8668 "}\n", 8669 LinuxBraceStyle); 8670 verifyFormat("struct S {\n" 8671 " int Type;\n" 8672 " union {\n" 8673 " int x;\n" 8674 " double y;\n" 8675 " } Value;\n" 8676 " class C\n" 8677 " {\n" 8678 " MyFavoriteType Value;\n" 8679 " } Class;\n" 8680 "}\n", 8681 LinuxBraceStyle); 8682 } 8683 8684 TEST_F(FormatTest, MozillaBraceBreaking) { 8685 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8686 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8687 MozillaBraceStyle.FixNamespaceComments = false; 8688 verifyFormat("namespace a {\n" 8689 "class A\n" 8690 "{\n" 8691 " void f()\n" 8692 " {\n" 8693 " if (true) {\n" 8694 " a();\n" 8695 " b();\n" 8696 " }\n" 8697 " }\n" 8698 " void g() { return; }\n" 8699 "};\n" 8700 "enum E\n" 8701 "{\n" 8702 " A,\n" 8703 " // foo\n" 8704 " B,\n" 8705 " C\n" 8706 "};\n" 8707 "struct B\n" 8708 "{\n" 8709 " int x;\n" 8710 "};\n" 8711 "}\n", 8712 MozillaBraceStyle); 8713 verifyFormat("struct S\n" 8714 "{\n" 8715 " int Type;\n" 8716 " union\n" 8717 " {\n" 8718 " int x;\n" 8719 " double y;\n" 8720 " } Value;\n" 8721 " class C\n" 8722 " {\n" 8723 " MyFavoriteType Value;\n" 8724 " } Class;\n" 8725 "}\n", 8726 MozillaBraceStyle); 8727 } 8728 8729 TEST_F(FormatTest, StroustrupBraceBreaking) { 8730 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8731 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8732 verifyFormat("namespace a {\n" 8733 "class A {\n" 8734 " void f()\n" 8735 " {\n" 8736 " if (true) {\n" 8737 " a();\n" 8738 " b();\n" 8739 " }\n" 8740 " }\n" 8741 " void g() { return; }\n" 8742 "};\n" 8743 "struct B {\n" 8744 " int x;\n" 8745 "};\n" 8746 "} // namespace a\n", 8747 StroustrupBraceStyle); 8748 8749 verifyFormat("void foo()\n" 8750 "{\n" 8751 " if (a) {\n" 8752 " a();\n" 8753 " }\n" 8754 " else {\n" 8755 " b();\n" 8756 " }\n" 8757 "}\n", 8758 StroustrupBraceStyle); 8759 8760 verifyFormat("#ifdef _DEBUG\n" 8761 "int foo(int i = 0)\n" 8762 "#else\n" 8763 "int foo(int i = 5)\n" 8764 "#endif\n" 8765 "{\n" 8766 " return i;\n" 8767 "}", 8768 StroustrupBraceStyle); 8769 8770 verifyFormat("void foo() {}\n" 8771 "void bar()\n" 8772 "#ifdef _DEBUG\n" 8773 "{\n" 8774 " foo();\n" 8775 "}\n" 8776 "#else\n" 8777 "{\n" 8778 "}\n" 8779 "#endif", 8780 StroustrupBraceStyle); 8781 8782 verifyFormat("void foobar() { int i = 5; }\n" 8783 "#ifdef _DEBUG\n" 8784 "void bar() {}\n" 8785 "#else\n" 8786 "void bar() { foobar(); }\n" 8787 "#endif", 8788 StroustrupBraceStyle); 8789 } 8790 8791 TEST_F(FormatTest, AllmanBraceBreaking) { 8792 FormatStyle AllmanBraceStyle = getLLVMStyle(); 8793 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 8794 verifyFormat("namespace a\n" 8795 "{\n" 8796 "class A\n" 8797 "{\n" 8798 " void f()\n" 8799 " {\n" 8800 " if (true)\n" 8801 " {\n" 8802 " a();\n" 8803 " b();\n" 8804 " }\n" 8805 " }\n" 8806 " void g() { return; }\n" 8807 "};\n" 8808 "struct B\n" 8809 "{\n" 8810 " int x;\n" 8811 "};\n" 8812 "}", 8813 AllmanBraceStyle); 8814 8815 verifyFormat("void f()\n" 8816 "{\n" 8817 " if (true)\n" 8818 " {\n" 8819 " a();\n" 8820 " }\n" 8821 " else if (false)\n" 8822 " {\n" 8823 " b();\n" 8824 " }\n" 8825 " else\n" 8826 " {\n" 8827 " c();\n" 8828 " }\n" 8829 "}\n", 8830 AllmanBraceStyle); 8831 8832 verifyFormat("void f()\n" 8833 "{\n" 8834 " for (int i = 0; i < 10; ++i)\n" 8835 " {\n" 8836 " a();\n" 8837 " }\n" 8838 " while (false)\n" 8839 " {\n" 8840 " b();\n" 8841 " }\n" 8842 " do\n" 8843 " {\n" 8844 " c();\n" 8845 " } while (false)\n" 8846 "}\n", 8847 AllmanBraceStyle); 8848 8849 verifyFormat("void f(int a)\n" 8850 "{\n" 8851 " switch (a)\n" 8852 " {\n" 8853 " case 0:\n" 8854 " break;\n" 8855 " case 1:\n" 8856 " {\n" 8857 " break;\n" 8858 " }\n" 8859 " case 2:\n" 8860 " {\n" 8861 " }\n" 8862 " break;\n" 8863 " default:\n" 8864 " break;\n" 8865 " }\n" 8866 "}\n", 8867 AllmanBraceStyle); 8868 8869 verifyFormat("enum X\n" 8870 "{\n" 8871 " Y = 0,\n" 8872 "}\n", 8873 AllmanBraceStyle); 8874 verifyFormat("enum X\n" 8875 "{\n" 8876 " Y = 0\n" 8877 "}\n", 8878 AllmanBraceStyle); 8879 8880 verifyFormat("@interface BSApplicationController ()\n" 8881 "{\n" 8882 "@private\n" 8883 " id _extraIvar;\n" 8884 "}\n" 8885 "@end\n", 8886 AllmanBraceStyle); 8887 8888 verifyFormat("#ifdef _DEBUG\n" 8889 "int foo(int i = 0)\n" 8890 "#else\n" 8891 "int foo(int i = 5)\n" 8892 "#endif\n" 8893 "{\n" 8894 " return i;\n" 8895 "}", 8896 AllmanBraceStyle); 8897 8898 verifyFormat("void foo() {}\n" 8899 "void bar()\n" 8900 "#ifdef _DEBUG\n" 8901 "{\n" 8902 " foo();\n" 8903 "}\n" 8904 "#else\n" 8905 "{\n" 8906 "}\n" 8907 "#endif", 8908 AllmanBraceStyle); 8909 8910 verifyFormat("void foobar() { int i = 5; }\n" 8911 "#ifdef _DEBUG\n" 8912 "void bar() {}\n" 8913 "#else\n" 8914 "void bar() { foobar(); }\n" 8915 "#endif", 8916 AllmanBraceStyle); 8917 8918 // This shouldn't affect ObjC blocks.. 8919 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 8920 " // ...\n" 8921 " int i;\n" 8922 "}];", 8923 AllmanBraceStyle); 8924 verifyFormat("void (^block)(void) = ^{\n" 8925 " // ...\n" 8926 " int i;\n" 8927 "};", 8928 AllmanBraceStyle); 8929 // .. or dict literals. 8930 verifyFormat("void f()\n" 8931 "{\n" 8932 " // ...\n" 8933 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 8934 "}", 8935 AllmanBraceStyle); 8936 verifyFormat("void f()\n" 8937 "{\n" 8938 " // ...\n" 8939 " [object someMethod:@{a : @\"b\"}];\n" 8940 "}", 8941 AllmanBraceStyle); 8942 verifyFormat("int f()\n" 8943 "{ // comment\n" 8944 " return 42;\n" 8945 "}", 8946 AllmanBraceStyle); 8947 8948 AllmanBraceStyle.ColumnLimit = 19; 8949 verifyFormat("void f() { int i; }", AllmanBraceStyle); 8950 AllmanBraceStyle.ColumnLimit = 18; 8951 verifyFormat("void f()\n" 8952 "{\n" 8953 " int i;\n" 8954 "}", 8955 AllmanBraceStyle); 8956 AllmanBraceStyle.ColumnLimit = 80; 8957 8958 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 8959 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 8960 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 8961 verifyFormat("void f(bool b)\n" 8962 "{\n" 8963 " if (b)\n" 8964 " {\n" 8965 " return;\n" 8966 " }\n" 8967 "}\n", 8968 BreakBeforeBraceShortIfs); 8969 verifyFormat("void f(bool b)\n" 8970 "{\n" 8971 " if constexpr (b)\n" 8972 " {\n" 8973 " return;\n" 8974 " }\n" 8975 "}\n", 8976 BreakBeforeBraceShortIfs); 8977 verifyFormat("void f(bool b)\n" 8978 "{\n" 8979 " if (b) return;\n" 8980 "}\n", 8981 BreakBeforeBraceShortIfs); 8982 verifyFormat("void f(bool b)\n" 8983 "{\n" 8984 " if constexpr (b) return;\n" 8985 "}\n", 8986 BreakBeforeBraceShortIfs); 8987 verifyFormat("void f(bool b)\n" 8988 "{\n" 8989 " while (b)\n" 8990 " {\n" 8991 " return;\n" 8992 " }\n" 8993 "}\n", 8994 BreakBeforeBraceShortIfs); 8995 } 8996 8997 TEST_F(FormatTest, GNUBraceBreaking) { 8998 FormatStyle GNUBraceStyle = getLLVMStyle(); 8999 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9000 verifyFormat("namespace a\n" 9001 "{\n" 9002 "class A\n" 9003 "{\n" 9004 " void f()\n" 9005 " {\n" 9006 " int a;\n" 9007 " {\n" 9008 " int b;\n" 9009 " }\n" 9010 " if (true)\n" 9011 " {\n" 9012 " a();\n" 9013 " b();\n" 9014 " }\n" 9015 " }\n" 9016 " void g() { return; }\n" 9017 "}\n" 9018 "}", 9019 GNUBraceStyle); 9020 9021 verifyFormat("void f()\n" 9022 "{\n" 9023 " if (true)\n" 9024 " {\n" 9025 " a();\n" 9026 " }\n" 9027 " else if (false)\n" 9028 " {\n" 9029 " b();\n" 9030 " }\n" 9031 " else\n" 9032 " {\n" 9033 " c();\n" 9034 " }\n" 9035 "}\n", 9036 GNUBraceStyle); 9037 9038 verifyFormat("void f()\n" 9039 "{\n" 9040 " for (int i = 0; i < 10; ++i)\n" 9041 " {\n" 9042 " a();\n" 9043 " }\n" 9044 " while (false)\n" 9045 " {\n" 9046 " b();\n" 9047 " }\n" 9048 " do\n" 9049 " {\n" 9050 " c();\n" 9051 " }\n" 9052 " while (false);\n" 9053 "}\n", 9054 GNUBraceStyle); 9055 9056 verifyFormat("void f(int a)\n" 9057 "{\n" 9058 " switch (a)\n" 9059 " {\n" 9060 " case 0:\n" 9061 " break;\n" 9062 " case 1:\n" 9063 " {\n" 9064 " break;\n" 9065 " }\n" 9066 " case 2:\n" 9067 " {\n" 9068 " }\n" 9069 " break;\n" 9070 " default:\n" 9071 " break;\n" 9072 " }\n" 9073 "}\n", 9074 GNUBraceStyle); 9075 9076 verifyFormat("enum X\n" 9077 "{\n" 9078 " Y = 0,\n" 9079 "}\n", 9080 GNUBraceStyle); 9081 9082 verifyFormat("@interface BSApplicationController ()\n" 9083 "{\n" 9084 "@private\n" 9085 " id _extraIvar;\n" 9086 "}\n" 9087 "@end\n", 9088 GNUBraceStyle); 9089 9090 verifyFormat("#ifdef _DEBUG\n" 9091 "int foo(int i = 0)\n" 9092 "#else\n" 9093 "int foo(int i = 5)\n" 9094 "#endif\n" 9095 "{\n" 9096 " return i;\n" 9097 "}", 9098 GNUBraceStyle); 9099 9100 verifyFormat("void foo() {}\n" 9101 "void bar()\n" 9102 "#ifdef _DEBUG\n" 9103 "{\n" 9104 " foo();\n" 9105 "}\n" 9106 "#else\n" 9107 "{\n" 9108 "}\n" 9109 "#endif", 9110 GNUBraceStyle); 9111 9112 verifyFormat("void foobar() { int i = 5; }\n" 9113 "#ifdef _DEBUG\n" 9114 "void bar() {}\n" 9115 "#else\n" 9116 "void bar() { foobar(); }\n" 9117 "#endif", 9118 GNUBraceStyle); 9119 } 9120 9121 TEST_F(FormatTest, WebKitBraceBreaking) { 9122 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9123 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9124 WebKitBraceStyle.FixNamespaceComments = false; 9125 verifyFormat("namespace a {\n" 9126 "class A {\n" 9127 " void f()\n" 9128 " {\n" 9129 " if (true) {\n" 9130 " a();\n" 9131 " b();\n" 9132 " }\n" 9133 " }\n" 9134 " void g() { return; }\n" 9135 "};\n" 9136 "enum E {\n" 9137 " A,\n" 9138 " // foo\n" 9139 " B,\n" 9140 " C\n" 9141 "};\n" 9142 "struct B {\n" 9143 " int x;\n" 9144 "};\n" 9145 "}\n", 9146 WebKitBraceStyle); 9147 verifyFormat("struct S {\n" 9148 " int Type;\n" 9149 " union {\n" 9150 " int x;\n" 9151 " double y;\n" 9152 " } Value;\n" 9153 " class C {\n" 9154 " MyFavoriteType Value;\n" 9155 " } Class;\n" 9156 "};\n", 9157 WebKitBraceStyle); 9158 } 9159 9160 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9161 verifyFormat("void f() {\n" 9162 " try {\n" 9163 " } catch (const Exception &e) {\n" 9164 " }\n" 9165 "}\n", 9166 getLLVMStyle()); 9167 } 9168 9169 TEST_F(FormatTest, UnderstandsPragmas) { 9170 verifyFormat("#pragma omp reduction(| : var)"); 9171 verifyFormat("#pragma omp reduction(+ : var)"); 9172 9173 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9174 "(including parentheses).", 9175 format("#pragma mark Any non-hyphenated or hyphenated string " 9176 "(including parentheses).")); 9177 } 9178 9179 TEST_F(FormatTest, UnderstandPragmaOption) { 9180 verifyFormat("#pragma option -C -A"); 9181 9182 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9183 } 9184 9185 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9186 for (size_t i = 1; i < Styles.size(); ++i) \ 9187 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9188 << " differs from Style #0" 9189 9190 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9191 SmallVector<FormatStyle, 3> Styles; 9192 Styles.resize(3); 9193 9194 Styles[0] = getLLVMStyle(); 9195 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9196 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9197 EXPECT_ALL_STYLES_EQUAL(Styles); 9198 9199 Styles[0] = getGoogleStyle(); 9200 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9201 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9202 EXPECT_ALL_STYLES_EQUAL(Styles); 9203 9204 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9205 EXPECT_TRUE( 9206 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9207 EXPECT_TRUE( 9208 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9209 EXPECT_ALL_STYLES_EQUAL(Styles); 9210 9211 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9212 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9213 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9214 EXPECT_ALL_STYLES_EQUAL(Styles); 9215 9216 Styles[0] = getMozillaStyle(); 9217 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9218 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9219 EXPECT_ALL_STYLES_EQUAL(Styles); 9220 9221 Styles[0] = getWebKitStyle(); 9222 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9223 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9224 EXPECT_ALL_STYLES_EQUAL(Styles); 9225 9226 Styles[0] = getGNUStyle(); 9227 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9228 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9229 EXPECT_ALL_STYLES_EQUAL(Styles); 9230 9231 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9232 } 9233 9234 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9235 SmallVector<FormatStyle, 8> Styles; 9236 Styles.resize(2); 9237 9238 Styles[0] = getGoogleStyle(); 9239 Styles[1] = getLLVMStyle(); 9240 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9241 EXPECT_ALL_STYLES_EQUAL(Styles); 9242 9243 Styles.resize(5); 9244 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9245 Styles[1] = getLLVMStyle(); 9246 Styles[1].Language = FormatStyle::LK_JavaScript; 9247 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9248 9249 Styles[2] = getLLVMStyle(); 9250 Styles[2].Language = FormatStyle::LK_JavaScript; 9251 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9252 "BasedOnStyle: Google", 9253 &Styles[2]) 9254 .value()); 9255 9256 Styles[3] = getLLVMStyle(); 9257 Styles[3].Language = FormatStyle::LK_JavaScript; 9258 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9259 "Language: JavaScript", 9260 &Styles[3]) 9261 .value()); 9262 9263 Styles[4] = getLLVMStyle(); 9264 Styles[4].Language = FormatStyle::LK_JavaScript; 9265 EXPECT_EQ(0, parseConfiguration("---\n" 9266 "BasedOnStyle: LLVM\n" 9267 "IndentWidth: 123\n" 9268 "---\n" 9269 "BasedOnStyle: Google\n" 9270 "Language: JavaScript", 9271 &Styles[4]) 9272 .value()); 9273 EXPECT_ALL_STYLES_EQUAL(Styles); 9274 } 9275 9276 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9277 Style.FIELD = false; \ 9278 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9279 EXPECT_TRUE(Style.FIELD); \ 9280 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9281 EXPECT_FALSE(Style.FIELD); 9282 9283 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9284 9285 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9286 Style.STRUCT.FIELD = false; \ 9287 EXPECT_EQ(0, \ 9288 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9289 .value()); \ 9290 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9291 EXPECT_EQ(0, \ 9292 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9293 .value()); \ 9294 EXPECT_FALSE(Style.STRUCT.FIELD); 9295 9296 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9297 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9298 9299 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9300 EXPECT_NE(VALUE, Style.FIELD); \ 9301 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9302 EXPECT_EQ(VALUE, Style.FIELD) 9303 9304 TEST_F(FormatTest, ParsesConfigurationBools) { 9305 FormatStyle Style = {}; 9306 Style.Language = FormatStyle::LK_Cpp; 9307 CHECK_PARSE_BOOL(AlignOperands); 9308 CHECK_PARSE_BOOL(AlignTrailingComments); 9309 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9310 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9311 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9312 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9313 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9314 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9315 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9316 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9317 CHECK_PARSE_BOOL(BinPackArguments); 9318 CHECK_PARSE_BOOL(BinPackParameters); 9319 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9320 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9321 CHECK_PARSE_BOOL(BreakStringLiterals); 9322 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 9323 CHECK_PARSE_BOOL(CompactNamespaces); 9324 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9325 CHECK_PARSE_BOOL(DerivePointerAlignment); 9326 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9327 CHECK_PARSE_BOOL(DisableFormat); 9328 CHECK_PARSE_BOOL(IndentCaseLabels); 9329 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9330 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9331 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9332 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9333 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9334 CHECK_PARSE_BOOL(ReflowComments); 9335 CHECK_PARSE_BOOL(SortIncludes); 9336 CHECK_PARSE_BOOL(SortUsingDeclarations); 9337 CHECK_PARSE_BOOL(SpacesInParentheses); 9338 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9339 CHECK_PARSE_BOOL(SpacesInAngles); 9340 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9341 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9342 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9343 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9344 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9345 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9346 9347 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9348 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9349 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9350 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9351 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9352 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9353 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9354 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9355 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9356 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9357 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9358 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunctionBody); 9359 } 9360 9361 #undef CHECK_PARSE_BOOL 9362 9363 TEST_F(FormatTest, ParsesConfiguration) { 9364 FormatStyle Style = {}; 9365 Style.Language = FormatStyle::LK_Cpp; 9366 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9367 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9368 ConstructorInitializerIndentWidth, 1234u); 9369 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9370 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9371 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9372 CHECK_PARSE("PenaltyBreakAssignment: 1234", 9373 PenaltyBreakAssignment, 1234u); 9374 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9375 PenaltyBreakBeforeFirstCallParameter, 1234u); 9376 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9377 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9378 PenaltyReturnTypeOnItsOwnLine, 1234u); 9379 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9380 SpacesBeforeTrailingComments, 1234u); 9381 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9382 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9383 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9384 9385 Style.PointerAlignment = FormatStyle::PAS_Middle; 9386 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9387 FormatStyle::PAS_Left); 9388 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9389 FormatStyle::PAS_Right); 9390 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9391 FormatStyle::PAS_Middle); 9392 // For backward compatibility: 9393 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9394 FormatStyle::PAS_Left); 9395 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9396 FormatStyle::PAS_Right); 9397 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9398 FormatStyle::PAS_Middle); 9399 9400 Style.Standard = FormatStyle::LS_Auto; 9401 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9402 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9403 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9404 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9405 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9406 9407 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9408 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9409 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9410 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9411 FormatStyle::BOS_None); 9412 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9413 FormatStyle::BOS_All); 9414 // For backward compatibility: 9415 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9416 FormatStyle::BOS_None); 9417 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9418 FormatStyle::BOS_All); 9419 9420 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 9421 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 9422 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9423 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 9424 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 9425 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 9426 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 9427 // For backward compatibility: 9428 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 9429 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9430 9431 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9432 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9433 FormatStyle::BAS_Align); 9434 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9435 FormatStyle::BAS_DontAlign); 9436 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9437 FormatStyle::BAS_AlwaysBreak); 9438 // For backward compatibility: 9439 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9440 FormatStyle::BAS_DontAlign); 9441 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9442 FormatStyle::BAS_Align); 9443 9444 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9445 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 9446 FormatStyle::ENAS_DontAlign); 9447 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 9448 FormatStyle::ENAS_Left); 9449 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 9450 FormatStyle::ENAS_Right); 9451 // For backward compatibility: 9452 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 9453 FormatStyle::ENAS_Left); 9454 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 9455 FormatStyle::ENAS_Right); 9456 9457 Style.UseTab = FormatStyle::UT_ForIndentation; 9458 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9459 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9460 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9461 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9462 FormatStyle::UT_ForContinuationAndIndentation); 9463 // For backward compatibility: 9464 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9465 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9466 9467 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9468 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9469 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9470 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9471 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9472 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9473 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9474 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9475 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9476 // For backward compatibility: 9477 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9478 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9479 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9480 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9481 9482 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9483 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9484 FormatStyle::SBPO_Never); 9485 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9486 FormatStyle::SBPO_Always); 9487 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9488 FormatStyle::SBPO_ControlStatements); 9489 // For backward compatibility: 9490 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9491 FormatStyle::SBPO_Never); 9492 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9493 FormatStyle::SBPO_ControlStatements); 9494 9495 Style.ColumnLimit = 123; 9496 FormatStyle BaseStyle = getLLVMStyle(); 9497 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9498 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9499 9500 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9501 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9502 FormatStyle::BS_Attach); 9503 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9504 FormatStyle::BS_Linux); 9505 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9506 FormatStyle::BS_Mozilla); 9507 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9508 FormatStyle::BS_Stroustrup); 9509 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9510 FormatStyle::BS_Allman); 9511 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9512 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9513 FormatStyle::BS_WebKit); 9514 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9515 FormatStyle::BS_Custom); 9516 9517 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9518 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9519 FormatStyle::RTBS_None); 9520 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9521 FormatStyle::RTBS_All); 9522 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9523 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9524 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9525 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9526 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9527 AlwaysBreakAfterReturnType, 9528 FormatStyle::RTBS_TopLevelDefinitions); 9529 9530 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9531 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9532 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9533 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9534 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9535 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9536 AlwaysBreakAfterDefinitionReturnType, 9537 FormatStyle::DRTBS_TopLevel); 9538 9539 Style.NamespaceIndentation = FormatStyle::NI_All; 9540 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9541 FormatStyle::NI_None); 9542 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9543 FormatStyle::NI_Inner); 9544 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9545 FormatStyle::NI_All); 9546 9547 // FIXME: This is required because parsing a configuration simply overwrites 9548 // the first N elements of the list instead of resetting it. 9549 Style.ForEachMacros.clear(); 9550 std::vector<std::string> BoostForeach; 9551 BoostForeach.push_back("BOOST_FOREACH"); 9552 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9553 std::vector<std::string> BoostAndQForeach; 9554 BoostAndQForeach.push_back("BOOST_FOREACH"); 9555 BoostAndQForeach.push_back("Q_FOREACH"); 9556 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9557 BoostAndQForeach); 9558 9559 Style.IncludeCategories.clear(); 9560 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9561 {".*", 1}}; 9562 CHECK_PARSE("IncludeCategories:\n" 9563 " - Regex: abc/.*\n" 9564 " Priority: 2\n" 9565 " - Regex: .*\n" 9566 " Priority: 1", 9567 IncludeCategories, ExpectedCategories); 9568 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9569 } 9570 9571 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9572 FormatStyle Style = {}; 9573 Style.Language = FormatStyle::LK_Cpp; 9574 CHECK_PARSE("Language: Cpp\n" 9575 "IndentWidth: 12", 9576 IndentWidth, 12u); 9577 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9578 "IndentWidth: 34", 9579 &Style), 9580 ParseError::Unsuitable); 9581 EXPECT_EQ(12u, Style.IndentWidth); 9582 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9583 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9584 9585 Style.Language = FormatStyle::LK_JavaScript; 9586 CHECK_PARSE("Language: JavaScript\n" 9587 "IndentWidth: 12", 9588 IndentWidth, 12u); 9589 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9590 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9591 "IndentWidth: 34", 9592 &Style), 9593 ParseError::Unsuitable); 9594 EXPECT_EQ(23u, Style.IndentWidth); 9595 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9596 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9597 9598 CHECK_PARSE("BasedOnStyle: LLVM\n" 9599 "IndentWidth: 67", 9600 IndentWidth, 67u); 9601 9602 CHECK_PARSE("---\n" 9603 "Language: JavaScript\n" 9604 "IndentWidth: 12\n" 9605 "---\n" 9606 "Language: Cpp\n" 9607 "IndentWidth: 34\n" 9608 "...\n", 9609 IndentWidth, 12u); 9610 9611 Style.Language = FormatStyle::LK_Cpp; 9612 CHECK_PARSE("---\n" 9613 "Language: JavaScript\n" 9614 "IndentWidth: 12\n" 9615 "---\n" 9616 "Language: Cpp\n" 9617 "IndentWidth: 34\n" 9618 "...\n", 9619 IndentWidth, 34u); 9620 CHECK_PARSE("---\n" 9621 "IndentWidth: 78\n" 9622 "---\n" 9623 "Language: JavaScript\n" 9624 "IndentWidth: 56\n" 9625 "...\n", 9626 IndentWidth, 78u); 9627 9628 Style.ColumnLimit = 123; 9629 Style.IndentWidth = 234; 9630 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9631 Style.TabWidth = 345; 9632 EXPECT_FALSE(parseConfiguration("---\n" 9633 "IndentWidth: 456\n" 9634 "BreakBeforeBraces: Allman\n" 9635 "---\n" 9636 "Language: JavaScript\n" 9637 "IndentWidth: 111\n" 9638 "TabWidth: 111\n" 9639 "---\n" 9640 "Language: Cpp\n" 9641 "BreakBeforeBraces: Stroustrup\n" 9642 "TabWidth: 789\n" 9643 "...\n", 9644 &Style)); 9645 EXPECT_EQ(123u, Style.ColumnLimit); 9646 EXPECT_EQ(456u, Style.IndentWidth); 9647 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9648 EXPECT_EQ(789u, Style.TabWidth); 9649 9650 EXPECT_EQ(parseConfiguration("---\n" 9651 "Language: JavaScript\n" 9652 "IndentWidth: 56\n" 9653 "---\n" 9654 "IndentWidth: 78\n" 9655 "...\n", 9656 &Style), 9657 ParseError::Error); 9658 EXPECT_EQ(parseConfiguration("---\n" 9659 "Language: JavaScript\n" 9660 "IndentWidth: 56\n" 9661 "---\n" 9662 "Language: JavaScript\n" 9663 "IndentWidth: 78\n" 9664 "...\n", 9665 &Style), 9666 ParseError::Error); 9667 9668 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9669 } 9670 9671 #undef CHECK_PARSE 9672 9673 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9674 FormatStyle Style = {}; 9675 Style.Language = FormatStyle::LK_JavaScript; 9676 Style.BreakBeforeTernaryOperators = true; 9677 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9678 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9679 9680 Style.BreakBeforeTernaryOperators = true; 9681 EXPECT_EQ(0, parseConfiguration("---\n" 9682 "BasedOnStyle: Google\n" 9683 "---\n" 9684 "Language: JavaScript\n" 9685 "IndentWidth: 76\n" 9686 "...\n", 9687 &Style) 9688 .value()); 9689 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9690 EXPECT_EQ(76u, Style.IndentWidth); 9691 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9692 } 9693 9694 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9695 FormatStyle Style = getLLVMStyle(); 9696 std::string YAML = configurationAsText(Style); 9697 FormatStyle ParsedStyle = {}; 9698 ParsedStyle.Language = FormatStyle::LK_Cpp; 9699 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9700 EXPECT_EQ(Style, ParsedStyle); 9701 } 9702 9703 TEST_F(FormatTest, WorksFor8bitEncodings) { 9704 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9705 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9706 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9707 "\"\xef\xee\xf0\xf3...\"", 9708 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9709 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9710 "\xef\xee\xf0\xf3...\"", 9711 getLLVMStyleWithColumns(12))); 9712 } 9713 9714 TEST_F(FormatTest, HandlesUTF8BOM) { 9715 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9716 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9717 format("\xef\xbb\xbf#include <iostream>")); 9718 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9719 format("\xef\xbb\xbf\n#include <iostream>")); 9720 } 9721 9722 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9723 #if !defined(_MSC_VER) 9724 9725 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9726 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9727 getLLVMStyleWithColumns(35)); 9728 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9729 getLLVMStyleWithColumns(31)); 9730 verifyFormat("// Однажды в студёную зимнюю пору...", 9731 getLLVMStyleWithColumns(36)); 9732 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9733 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9734 getLLVMStyleWithColumns(39)); 9735 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9736 getLLVMStyleWithColumns(35)); 9737 } 9738 9739 TEST_F(FormatTest, SplitsUTF8Strings) { 9740 // Non-printable characters' width is currently considered to be the length in 9741 // bytes in UTF8. The characters can be displayed in very different manner 9742 // (zero-width, single width with a substitution glyph, expanded to their code 9743 // (e.g. "<8d>"), so there's no single correct way to handle them. 9744 EXPECT_EQ("\"aaaaÄ\"\n" 9745 "\"\xc2\x8d\";", 9746 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9747 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9748 "\"\xc2\x8d\";", 9749 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9750 EXPECT_EQ("\"Однажды, в \"\n" 9751 "\"студёную \"\n" 9752 "\"зимнюю \"\n" 9753 "\"пору,\"", 9754 format("\"Однажды, в студёную зимнюю пору,\"", 9755 getLLVMStyleWithColumns(13))); 9756 EXPECT_EQ( 9757 "\"一 二 三 \"\n" 9758 "\"四 五六 \"\n" 9759 "\"七 八 九 \"\n" 9760 "\"十\"", 9761 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9762 EXPECT_EQ("\"一\t二 \"\n" 9763 "\"\t三 \"\n" 9764 "\"四 五\t六 \"\n" 9765 "\"\t七 \"\n" 9766 "\"八九十\tqq\"", 9767 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9768 getLLVMStyleWithColumns(11))); 9769 9770 // UTF8 character in an escape sequence. 9771 EXPECT_EQ("\"aaaaaa\"\n" 9772 "\"\\\xC2\x8D\"", 9773 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 9774 } 9775 9776 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9777 EXPECT_EQ("const char *sssss =\n" 9778 " \"一二三四五六七八\\\n" 9779 " 九 十\";", 9780 format("const char *sssss = \"一二三四五六七八\\\n" 9781 " 九 十\";", 9782 getLLVMStyleWithColumns(30))); 9783 } 9784 9785 TEST_F(FormatTest, SplitsUTF8LineComments) { 9786 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9787 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9788 EXPECT_EQ("// Я из лесу\n" 9789 "// вышел; был\n" 9790 "// сильный\n" 9791 "// мороз.", 9792 format("// Я из лесу вышел; был сильный мороз.", 9793 getLLVMStyleWithColumns(13))); 9794 EXPECT_EQ("// 一二三\n" 9795 "// 四五六七\n" 9796 "// 八 九\n" 9797 "// 十", 9798 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9799 } 9800 9801 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9802 EXPECT_EQ("/* Гляжу,\n" 9803 " * поднимается\n" 9804 " * медленно в\n" 9805 " * гору\n" 9806 " * Лошадка,\n" 9807 " * везущая\n" 9808 " * хворосту\n" 9809 " * воз. */", 9810 format("/* Гляжу, поднимается медленно в гору\n" 9811 " * Лошадка, везущая хворосту воз. */", 9812 getLLVMStyleWithColumns(13))); 9813 EXPECT_EQ( 9814 "/* 一二三\n" 9815 " * 四五六七\n" 9816 " * 八 九\n" 9817 " * 十 */", 9818 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 9819 EXPECT_EQ("/* \n" 9820 " * \n" 9821 " * - */", 9822 format("/* - */", getLLVMStyleWithColumns(12))); 9823 } 9824 9825 #endif // _MSC_VER 9826 9827 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 9828 FormatStyle Style = getLLVMStyle(); 9829 9830 Style.ConstructorInitializerIndentWidth = 4; 9831 verifyFormat( 9832 "SomeClass::Constructor()\n" 9833 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9834 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9835 Style); 9836 9837 Style.ConstructorInitializerIndentWidth = 2; 9838 verifyFormat( 9839 "SomeClass::Constructor()\n" 9840 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9841 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9842 Style); 9843 9844 Style.ConstructorInitializerIndentWidth = 0; 9845 verifyFormat( 9846 "SomeClass::Constructor()\n" 9847 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9848 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9849 Style); 9850 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9851 verifyFormat( 9852 "SomeLongTemplateVariableName<\n" 9853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 9854 Style); 9855 verifyFormat( 9856 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 9857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9858 Style); 9859 } 9860 9861 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 9862 FormatStyle Style = getLLVMStyle(); 9863 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 9864 Style.ConstructorInitializerIndentWidth = 4; 9865 verifyFormat("SomeClass::Constructor()\n" 9866 " : a(a)\n" 9867 " , b(b)\n" 9868 " , c(c) {}", 9869 Style); 9870 verifyFormat("SomeClass::Constructor()\n" 9871 " : a(a) {}", 9872 Style); 9873 9874 Style.ColumnLimit = 0; 9875 verifyFormat("SomeClass::Constructor()\n" 9876 " : a(a) {}", 9877 Style); 9878 verifyFormat("SomeClass::Constructor() noexcept\n" 9879 " : a(a) {}", 9880 Style); 9881 verifyFormat("SomeClass::Constructor()\n" 9882 " : a(a)\n" 9883 " , b(b)\n" 9884 " , c(c) {}", 9885 Style); 9886 verifyFormat("SomeClass::Constructor()\n" 9887 " : a(a) {\n" 9888 " foo();\n" 9889 " bar();\n" 9890 "}", 9891 Style); 9892 9893 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9894 verifyFormat("SomeClass::Constructor()\n" 9895 " : a(a)\n" 9896 " , b(b)\n" 9897 " , c(c) {\n}", 9898 Style); 9899 verifyFormat("SomeClass::Constructor()\n" 9900 " : a(a) {\n}", 9901 Style); 9902 9903 Style.ColumnLimit = 80; 9904 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 9905 Style.ConstructorInitializerIndentWidth = 2; 9906 verifyFormat("SomeClass::Constructor()\n" 9907 " : a(a)\n" 9908 " , b(b)\n" 9909 " , c(c) {}", 9910 Style); 9911 9912 Style.ConstructorInitializerIndentWidth = 0; 9913 verifyFormat("SomeClass::Constructor()\n" 9914 ": a(a)\n" 9915 ", b(b)\n" 9916 ", c(c) {}", 9917 Style); 9918 9919 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 9920 Style.ConstructorInitializerIndentWidth = 4; 9921 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 9922 verifyFormat( 9923 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 9924 Style); 9925 verifyFormat( 9926 "SomeClass::Constructor()\n" 9927 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 9928 Style); 9929 Style.ConstructorInitializerIndentWidth = 4; 9930 Style.ColumnLimit = 60; 9931 verifyFormat("SomeClass::Constructor()\n" 9932 " : aaaaaaaa(aaaaaaaa)\n" 9933 " , aaaaaaaa(aaaaaaaa)\n" 9934 " , aaaaaaaa(aaaaaaaa) {}", 9935 Style); 9936 } 9937 9938 TEST_F(FormatTest, Destructors) { 9939 verifyFormat("void F(int &i) { i.~int(); }"); 9940 verifyFormat("void F(int &i) { i->~int(); }"); 9941 } 9942 9943 TEST_F(FormatTest, FormatsWithWebKitStyle) { 9944 FormatStyle Style = getWebKitStyle(); 9945 9946 // Don't indent in outer namespaces. 9947 verifyFormat("namespace outer {\n" 9948 "int i;\n" 9949 "namespace inner {\n" 9950 " int i;\n" 9951 "} // namespace inner\n" 9952 "} // namespace outer\n" 9953 "namespace other_outer {\n" 9954 "int i;\n" 9955 "}", 9956 Style); 9957 9958 // Don't indent case labels. 9959 verifyFormat("switch (variable) {\n" 9960 "case 1:\n" 9961 "case 2:\n" 9962 " doSomething();\n" 9963 " break;\n" 9964 "default:\n" 9965 " ++variable;\n" 9966 "}", 9967 Style); 9968 9969 // Wrap before binary operators. 9970 EXPECT_EQ("void f()\n" 9971 "{\n" 9972 " if (aaaaaaaaaaaaaaaa\n" 9973 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 9974 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9975 " return;\n" 9976 "}", 9977 format("void f() {\n" 9978 "if (aaaaaaaaaaaaaaaa\n" 9979 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 9980 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9981 "return;\n" 9982 "}", 9983 Style)); 9984 9985 // Allow functions on a single line. 9986 verifyFormat("void f() { return; }", Style); 9987 9988 // Constructor initializers are formatted one per line with the "," on the 9989 // new line. 9990 verifyFormat("Constructor()\n" 9991 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9992 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 9993 " aaaaaaaaaaaaaa)\n" 9994 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 9995 "{\n" 9996 "}", 9997 Style); 9998 verifyFormat("SomeClass::Constructor()\n" 9999 " : a(a)\n" 10000 "{\n" 10001 "}", 10002 Style); 10003 EXPECT_EQ("SomeClass::Constructor()\n" 10004 " : a(a)\n" 10005 "{\n" 10006 "}", 10007 format("SomeClass::Constructor():a(a){}", Style)); 10008 verifyFormat("SomeClass::Constructor()\n" 10009 " : a(a)\n" 10010 " , b(b)\n" 10011 " , c(c)\n" 10012 "{\n" 10013 "}", 10014 Style); 10015 verifyFormat("SomeClass::Constructor()\n" 10016 " : a(a)\n" 10017 "{\n" 10018 " foo();\n" 10019 " bar();\n" 10020 "}", 10021 Style); 10022 10023 // Access specifiers should be aligned left. 10024 verifyFormat("class C {\n" 10025 "public:\n" 10026 " int i;\n" 10027 "};", 10028 Style); 10029 10030 // Do not align comments. 10031 verifyFormat("int a; // Do not\n" 10032 "double b; // align comments.", 10033 Style); 10034 10035 // Do not align operands. 10036 EXPECT_EQ("ASSERT(aaaa\n" 10037 " || bbbb);", 10038 format("ASSERT ( aaaa\n||bbbb);", Style)); 10039 10040 // Accept input's line breaks. 10041 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10042 " || bbbbbbbbbbbbbbb) {\n" 10043 " i++;\n" 10044 "}", 10045 format("if (aaaaaaaaaaaaaaa\n" 10046 "|| bbbbbbbbbbbbbbb) { i++; }", 10047 Style)); 10048 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10049 " i++;\n" 10050 "}", 10051 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10052 10053 // Don't automatically break all macro definitions (llvm.org/PR17842). 10054 verifyFormat("#define aNumber 10", Style); 10055 // However, generally keep the line breaks that the user authored. 10056 EXPECT_EQ("#define aNumber \\\n" 10057 " 10", 10058 format("#define aNumber \\\n" 10059 " 10", 10060 Style)); 10061 10062 // Keep empty and one-element array literals on a single line. 10063 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10064 " copyItems:YES];", 10065 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10066 "copyItems:YES];", 10067 Style)); 10068 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10069 " copyItems:YES];", 10070 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10071 " copyItems:YES];", 10072 Style)); 10073 // FIXME: This does not seem right, there should be more indentation before 10074 // the array literal's entries. Nested blocks have the same problem. 10075 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10076 " @\"a\",\n" 10077 " @\"a\"\n" 10078 "]\n" 10079 " copyItems:YES];", 10080 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10081 " @\"a\",\n" 10082 " @\"a\"\n" 10083 " ]\n" 10084 " copyItems:YES];", 10085 Style)); 10086 EXPECT_EQ( 10087 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10088 " copyItems:YES];", 10089 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10090 " copyItems:YES];", 10091 Style)); 10092 10093 verifyFormat("[self.a b:c c:d];", Style); 10094 EXPECT_EQ("[self.a b:c\n" 10095 " c:d];", 10096 format("[self.a b:c\n" 10097 "c:d];", 10098 Style)); 10099 } 10100 10101 TEST_F(FormatTest, FormatsLambdas) { 10102 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10103 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10104 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10105 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10106 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10107 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10108 verifyFormat("int x = f(*+[] {});"); 10109 verifyFormat("void f() {\n" 10110 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10111 "}\n"); 10112 verifyFormat("void f() {\n" 10113 " other(x.begin(), //\n" 10114 " x.end(), //\n" 10115 " [&](int, int) { return 1; });\n" 10116 "}\n"); 10117 verifyFormat("SomeFunction([]() { // A cool function...\n" 10118 " return 43;\n" 10119 "});"); 10120 EXPECT_EQ("SomeFunction([]() {\n" 10121 "#define A a\n" 10122 " return 43;\n" 10123 "});", 10124 format("SomeFunction([](){\n" 10125 "#define A a\n" 10126 "return 43;\n" 10127 "});")); 10128 verifyFormat("void f() {\n" 10129 " SomeFunction([](decltype(x), A *a) {});\n" 10130 "}"); 10131 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10132 " [](const aaaaaaaaaa &a) { return a; });"); 10133 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10134 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10135 "});"); 10136 verifyFormat("Constructor()\n" 10137 " : Field([] { // comment\n" 10138 " int i;\n" 10139 " }) {}"); 10140 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10141 " return some_parameter.size();\n" 10142 "};"); 10143 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10144 " [](const string &s) { return s; };"); 10145 verifyFormat("int i = aaaaaa ? 1 //\n" 10146 " : [] {\n" 10147 " return 2; //\n" 10148 " }();"); 10149 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10150 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10151 " return x == 2; // force break\n" 10152 " });"); 10153 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10154 " [=](int iiiiiiiiiiii) {\n" 10155 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 10156 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 10157 " });", 10158 getLLVMStyleWithColumns(60)); 10159 verifyFormat("SomeFunction({[&] {\n" 10160 " // comment\n" 10161 " },\n" 10162 " [&] {\n" 10163 " // comment\n" 10164 " }});"); 10165 verifyFormat("SomeFunction({[&] {\n" 10166 " // comment\n" 10167 "}});"); 10168 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10169 " [&]() { return true; },\n" 10170 " aaaaa aaaaaaaaa);"); 10171 10172 // Lambdas with return types. 10173 verifyFormat("int c = []() -> int { return 2; }();\n"); 10174 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10175 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10176 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10177 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10178 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10179 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10180 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10181 verifyFormat("[a, a]() -> a<1> {};"); 10182 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10183 " int j) -> int {\n" 10184 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10185 "};"); 10186 verifyFormat( 10187 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10188 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10189 " return aaaaaaaaaaaaaaaaa;\n" 10190 " });", 10191 getLLVMStyleWithColumns(70)); 10192 verifyFormat("[]() //\n" 10193 " -> int {\n" 10194 " return 1; //\n" 10195 "};"); 10196 10197 // Multiple lambdas in the same parentheses change indentation rules. 10198 verifyFormat("SomeFunction(\n" 10199 " []() {\n" 10200 " int i = 42;\n" 10201 " return i;\n" 10202 " },\n" 10203 " []() {\n" 10204 " int j = 43;\n" 10205 " return j;\n" 10206 " });"); 10207 10208 // More complex introducers. 10209 verifyFormat("return [i, args...] {};"); 10210 10211 // Not lambdas. 10212 verifyFormat("constexpr char hello[]{\"hello\"};"); 10213 verifyFormat("double &operator[](int i) { return 0; }\n" 10214 "int i;"); 10215 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10216 verifyFormat("int i = a[a][a]->f();"); 10217 verifyFormat("int i = (*b)[a]->f();"); 10218 10219 // Other corner cases. 10220 verifyFormat("void f() {\n" 10221 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10222 " );\n" 10223 "}"); 10224 10225 // Lambdas created through weird macros. 10226 verifyFormat("void f() {\n" 10227 " MACRO((const AA &a) { return 1; });\n" 10228 " MACRO((AA &a) { return 1; });\n" 10229 "}"); 10230 10231 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10232 " doo_dah();\n" 10233 " doo_dah();\n" 10234 " })) {\n" 10235 "}"); 10236 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 10237 " doo_dah();\n" 10238 " doo_dah();\n" 10239 " })) {\n" 10240 "}"); 10241 verifyFormat("auto lambda = []() {\n" 10242 " int a = 2\n" 10243 "#if A\n" 10244 " + 2\n" 10245 "#endif\n" 10246 " ;\n" 10247 "};"); 10248 10249 // Lambdas with complex multiline introducers. 10250 verifyFormat( 10251 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10252 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 10253 " -> ::std::unordered_set<\n" 10254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 10255 " //\n" 10256 " });"); 10257 } 10258 10259 TEST_F(FormatTest, FormatsBlocks) { 10260 FormatStyle ShortBlocks = getLLVMStyle(); 10261 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10262 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10263 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10264 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10265 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10266 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10267 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10268 10269 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10270 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10271 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10272 10273 verifyFormat("[operation setCompletionBlock:^{\n" 10274 " [self onOperationDone];\n" 10275 "}];"); 10276 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10277 " [self onOperationDone];\n" 10278 "}]};"); 10279 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10280 " f();\n" 10281 "}];"); 10282 verifyFormat("int a = [operation block:^int(int *i) {\n" 10283 " return 1;\n" 10284 "}];"); 10285 verifyFormat("[myObject doSomethingWith:arg1\n" 10286 " aaa:^int(int *a) {\n" 10287 " return 1;\n" 10288 " }\n" 10289 " bbb:f(a * bbbbbbbb)];"); 10290 10291 verifyFormat("[operation setCompletionBlock:^{\n" 10292 " [self.delegate newDataAvailable];\n" 10293 "}];", 10294 getLLVMStyleWithColumns(60)); 10295 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10296 " NSString *path = [self sessionFilePath];\n" 10297 " if (path) {\n" 10298 " // ...\n" 10299 " }\n" 10300 "});"); 10301 verifyFormat("[[SessionService sharedService]\n" 10302 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10303 " if (window) {\n" 10304 " [self windowDidLoad:window];\n" 10305 " } else {\n" 10306 " [self errorLoadingWindow];\n" 10307 " }\n" 10308 " }];"); 10309 verifyFormat("void (^largeBlock)(void) = ^{\n" 10310 " // ...\n" 10311 "};\n", 10312 getLLVMStyleWithColumns(40)); 10313 verifyFormat("[[SessionService sharedService]\n" 10314 " loadWindowWithCompletionBlock: //\n" 10315 " ^(SessionWindow *window) {\n" 10316 " if (window) {\n" 10317 " [self windowDidLoad:window];\n" 10318 " } else {\n" 10319 " [self errorLoadingWindow];\n" 10320 " }\n" 10321 " }];", 10322 getLLVMStyleWithColumns(60)); 10323 verifyFormat("[myObject doSomethingWith:arg1\n" 10324 " firstBlock:^(Foo *a) {\n" 10325 " // ...\n" 10326 " int i;\n" 10327 " }\n" 10328 " secondBlock:^(Bar *b) {\n" 10329 " // ...\n" 10330 " int i;\n" 10331 " }\n" 10332 " thirdBlock:^Foo(Bar *b) {\n" 10333 " // ...\n" 10334 " int i;\n" 10335 " }];"); 10336 verifyFormat("[myObject doSomethingWith:arg1\n" 10337 " firstBlock:-1\n" 10338 " secondBlock:^(Bar *b) {\n" 10339 " // ...\n" 10340 " int i;\n" 10341 " }];"); 10342 10343 verifyFormat("f(^{\n" 10344 " @autoreleasepool {\n" 10345 " if (a) {\n" 10346 " g();\n" 10347 " }\n" 10348 " }\n" 10349 "});"); 10350 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10351 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10352 "};"); 10353 10354 FormatStyle FourIndent = getLLVMStyle(); 10355 FourIndent.ObjCBlockIndentWidth = 4; 10356 verifyFormat("[operation setCompletionBlock:^{\n" 10357 " [self onOperationDone];\n" 10358 "}];", 10359 FourIndent); 10360 } 10361 10362 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10363 FormatStyle ZeroColumn = getLLVMStyle(); 10364 ZeroColumn.ColumnLimit = 0; 10365 10366 verifyFormat("[[SessionService sharedService] " 10367 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10368 " if (window) {\n" 10369 " [self windowDidLoad:window];\n" 10370 " } else {\n" 10371 " [self errorLoadingWindow];\n" 10372 " }\n" 10373 "}];", 10374 ZeroColumn); 10375 EXPECT_EQ("[[SessionService sharedService]\n" 10376 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10377 " if (window) {\n" 10378 " [self windowDidLoad:window];\n" 10379 " } else {\n" 10380 " [self errorLoadingWindow];\n" 10381 " }\n" 10382 " }];", 10383 format("[[SessionService sharedService]\n" 10384 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10385 " if (window) {\n" 10386 " [self windowDidLoad:window];\n" 10387 " } else {\n" 10388 " [self errorLoadingWindow];\n" 10389 " }\n" 10390 "}];", 10391 ZeroColumn)); 10392 verifyFormat("[myObject doSomethingWith:arg1\n" 10393 " firstBlock:^(Foo *a) {\n" 10394 " // ...\n" 10395 " int i;\n" 10396 " }\n" 10397 " secondBlock:^(Bar *b) {\n" 10398 " // ...\n" 10399 " int i;\n" 10400 " }\n" 10401 " thirdBlock:^Foo(Bar *b) {\n" 10402 " // ...\n" 10403 " int i;\n" 10404 " }];", 10405 ZeroColumn); 10406 verifyFormat("f(^{\n" 10407 " @autoreleasepool {\n" 10408 " if (a) {\n" 10409 " g();\n" 10410 " }\n" 10411 " }\n" 10412 "});", 10413 ZeroColumn); 10414 verifyFormat("void (^largeBlock)(void) = ^{\n" 10415 " // ...\n" 10416 "};", 10417 ZeroColumn); 10418 10419 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10420 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10421 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10422 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10423 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10424 " int i;\n" 10425 "};", 10426 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10427 } 10428 10429 TEST_F(FormatTest, SupportsCRLF) { 10430 EXPECT_EQ("int a;\r\n" 10431 "int b;\r\n" 10432 "int c;\r\n", 10433 format("int a;\r\n" 10434 " int b;\r\n" 10435 " int c;\r\n", 10436 getLLVMStyle())); 10437 EXPECT_EQ("int a;\r\n" 10438 "int b;\r\n" 10439 "int c;\r\n", 10440 format("int a;\r\n" 10441 " int b;\n" 10442 " int c;\r\n", 10443 getLLVMStyle())); 10444 EXPECT_EQ("int a;\n" 10445 "int b;\n" 10446 "int c;\n", 10447 format("int a;\r\n" 10448 " int b;\n" 10449 " int c;\n", 10450 getLLVMStyle())); 10451 EXPECT_EQ("\"aaaaaaa \"\r\n" 10452 "\"bbbbbbb\";\r\n", 10453 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10454 EXPECT_EQ("#define A \\\r\n" 10455 " b; \\\r\n" 10456 " c; \\\r\n" 10457 " d;\r\n", 10458 format("#define A \\\r\n" 10459 " b; \\\r\n" 10460 " c; d; \r\n", 10461 getGoogleStyle())); 10462 10463 EXPECT_EQ("/*\r\n" 10464 "multi line block comments\r\n" 10465 "should not introduce\r\n" 10466 "an extra carriage return\r\n" 10467 "*/\r\n", 10468 format("/*\r\n" 10469 "multi line block comments\r\n" 10470 "should not introduce\r\n" 10471 "an extra carriage return\r\n" 10472 "*/\r\n")); 10473 } 10474 10475 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10476 verifyFormat("MY_CLASS(C) {\n" 10477 " int i;\n" 10478 " int j;\n" 10479 "};"); 10480 } 10481 10482 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10483 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10484 TwoIndent.ContinuationIndentWidth = 2; 10485 10486 EXPECT_EQ("int i =\n" 10487 " longFunction(\n" 10488 " arg);", 10489 format("int i = longFunction(arg);", TwoIndent)); 10490 10491 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10492 SixIndent.ContinuationIndentWidth = 6; 10493 10494 EXPECT_EQ("int i =\n" 10495 " longFunction(\n" 10496 " arg);", 10497 format("int i = longFunction(arg);", SixIndent)); 10498 } 10499 10500 TEST_F(FormatTest, SpacesInAngles) { 10501 FormatStyle Spaces = getLLVMStyle(); 10502 Spaces.SpacesInAngles = true; 10503 10504 verifyFormat("static_cast< int >(arg);", Spaces); 10505 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10506 verifyFormat("f< int, float >();", Spaces); 10507 verifyFormat("template <> g() {}", Spaces); 10508 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10509 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10510 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10511 Spaces); 10512 10513 Spaces.Standard = FormatStyle::LS_Cpp03; 10514 Spaces.SpacesInAngles = true; 10515 verifyFormat("A< A< int > >();", Spaces); 10516 10517 Spaces.SpacesInAngles = false; 10518 verifyFormat("A<A<int> >();", Spaces); 10519 10520 Spaces.Standard = FormatStyle::LS_Cpp11; 10521 Spaces.SpacesInAngles = true; 10522 verifyFormat("A< A< int > >();", Spaces); 10523 10524 Spaces.SpacesInAngles = false; 10525 verifyFormat("A<A<int>>();", Spaces); 10526 } 10527 10528 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10529 FormatStyle Style = getLLVMStyle(); 10530 Style.SpaceAfterTemplateKeyword = false; 10531 verifyFormat("template<int> void foo();", Style); 10532 } 10533 10534 TEST_F(FormatTest, TripleAngleBrackets) { 10535 verifyFormat("f<<<1, 1>>>();"); 10536 verifyFormat("f<<<1, 1, 1, s>>>();"); 10537 verifyFormat("f<<<a, b, c, d>>>();"); 10538 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10539 verifyFormat("f<param><<<1, 1>>>();"); 10540 verifyFormat("f<1><<<1, 1>>>();"); 10541 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10542 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10543 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10544 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10545 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10546 } 10547 10548 TEST_F(FormatTest, MergeLessLessAtEnd) { 10549 verifyFormat("<<"); 10550 EXPECT_EQ("< < <", format("\\\n<<<")); 10551 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10552 "aaallvm::outs() <<"); 10553 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10554 "aaaallvm::outs()\n <<"); 10555 } 10556 10557 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10558 std::string code = "#if A\n" 10559 "#if B\n" 10560 "a.\n" 10561 "#endif\n" 10562 " a = 1;\n" 10563 "#else\n" 10564 "#endif\n" 10565 "#if C\n" 10566 "#else\n" 10567 "#endif\n"; 10568 EXPECT_EQ(code, format(code)); 10569 } 10570 10571 TEST_F(FormatTest, HandleConflictMarkers) { 10572 // Git/SVN conflict markers. 10573 EXPECT_EQ("int a;\n" 10574 "void f() {\n" 10575 " callme(some(parameter1,\n" 10576 "<<<<<<< text by the vcs\n" 10577 " parameter2),\n" 10578 "||||||| text by the vcs\n" 10579 " parameter2),\n" 10580 " parameter3,\n" 10581 "======= text by the vcs\n" 10582 " parameter2, parameter3),\n" 10583 ">>>>>>> text by the vcs\n" 10584 " otherparameter);\n", 10585 format("int a;\n" 10586 "void f() {\n" 10587 " callme(some(parameter1,\n" 10588 "<<<<<<< text by the vcs\n" 10589 " parameter2),\n" 10590 "||||||| text by the vcs\n" 10591 " parameter2),\n" 10592 " parameter3,\n" 10593 "======= text by the vcs\n" 10594 " parameter2,\n" 10595 " parameter3),\n" 10596 ">>>>>>> text by the vcs\n" 10597 " otherparameter);\n")); 10598 10599 // Perforce markers. 10600 EXPECT_EQ("void f() {\n" 10601 " function(\n" 10602 ">>>> text by the vcs\n" 10603 " parameter,\n" 10604 "==== text by the vcs\n" 10605 " parameter,\n" 10606 "==== text by the vcs\n" 10607 " parameter,\n" 10608 "<<<< text by the vcs\n" 10609 " parameter);\n", 10610 format("void f() {\n" 10611 " function(\n" 10612 ">>>> text by the vcs\n" 10613 " parameter,\n" 10614 "==== text by the vcs\n" 10615 " parameter,\n" 10616 "==== text by the vcs\n" 10617 " parameter,\n" 10618 "<<<< text by the vcs\n" 10619 " parameter);\n")); 10620 10621 EXPECT_EQ("<<<<<<<\n" 10622 "|||||||\n" 10623 "=======\n" 10624 ">>>>>>>", 10625 format("<<<<<<<\n" 10626 "|||||||\n" 10627 "=======\n" 10628 ">>>>>>>")); 10629 10630 EXPECT_EQ("<<<<<<<\n" 10631 "|||||||\n" 10632 "int i;\n" 10633 "=======\n" 10634 ">>>>>>>", 10635 format("<<<<<<<\n" 10636 "|||||||\n" 10637 "int i;\n" 10638 "=======\n" 10639 ">>>>>>>")); 10640 10641 // FIXME: Handle parsing of macros around conflict markers correctly: 10642 EXPECT_EQ("#define Macro \\\n" 10643 "<<<<<<<\n" 10644 "Something \\\n" 10645 "|||||||\n" 10646 "Else \\\n" 10647 "=======\n" 10648 "Other \\\n" 10649 ">>>>>>>\n" 10650 " End int i;\n", 10651 format("#define Macro \\\n" 10652 "<<<<<<<\n" 10653 " Something \\\n" 10654 "|||||||\n" 10655 " Else \\\n" 10656 "=======\n" 10657 " Other \\\n" 10658 ">>>>>>>\n" 10659 " End\n" 10660 "int i;\n")); 10661 } 10662 10663 TEST_F(FormatTest, DisableRegions) { 10664 EXPECT_EQ("int i;\n" 10665 "// clang-format off\n" 10666 " int j;\n" 10667 "// clang-format on\n" 10668 "int k;", 10669 format(" int i;\n" 10670 " // clang-format off\n" 10671 " int j;\n" 10672 " // clang-format on\n" 10673 " int k;")); 10674 EXPECT_EQ("int i;\n" 10675 "/* clang-format off */\n" 10676 " int j;\n" 10677 "/* clang-format on */\n" 10678 "int k;", 10679 format(" int i;\n" 10680 " /* clang-format off */\n" 10681 " int j;\n" 10682 " /* clang-format on */\n" 10683 " int k;")); 10684 10685 // Don't reflow comments within disabled regions. 10686 EXPECT_EQ( 10687 "// clang-format off\n" 10688 "// long long long long long long line\n" 10689 "/* clang-format on */\n" 10690 "/* long long long\n" 10691 " * long long long\n" 10692 " * line */\n" 10693 "int i;\n" 10694 "/* clang-format off */\n" 10695 "/* long long long long long long line */\n", 10696 format("// clang-format off\n" 10697 "// long long long long long long line\n" 10698 "/* clang-format on */\n" 10699 "/* long long long long long long line */\n" 10700 "int i;\n" 10701 "/* clang-format off */\n" 10702 "/* long long long long long long line */\n", 10703 getLLVMStyleWithColumns(20))); 10704 } 10705 10706 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10707 format("? ) ="); 10708 verifyNoCrash("#define a\\\n /**/}"); 10709 } 10710 10711 TEST_F(FormatTest, FormatsTableGenCode) { 10712 FormatStyle Style = getLLVMStyle(); 10713 Style.Language = FormatStyle::LK_TableGen; 10714 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10715 } 10716 10717 TEST_F(FormatTest, ArrayOfTemplates) { 10718 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10719 format("auto a = new unique_ptr<int > [ 10];")); 10720 10721 FormatStyle Spaces = getLLVMStyle(); 10722 Spaces.SpacesInSquareBrackets = true; 10723 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10724 format("auto a = new unique_ptr<int > [10];", Spaces)); 10725 } 10726 10727 TEST_F(FormatTest, ArrayAsTemplateType) { 10728 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10729 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10730 10731 FormatStyle Spaces = getLLVMStyle(); 10732 Spaces.SpacesInSquareBrackets = true; 10733 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10734 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10735 } 10736 10737 TEST_F(FormatTest, NoSpaceAfterSuper) { 10738 verifyFormat("__super::FooBar();"); 10739 } 10740 10741 TEST(FormatStyle, GetStyleOfFile) { 10742 vfs::InMemoryFileSystem FS; 10743 // Test 1: format file in the same directory. 10744 ASSERT_TRUE( 10745 FS.addFile("/a/.clang-format", 0, 10746 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10747 ASSERT_TRUE( 10748 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10749 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10750 ASSERT_TRUE((bool)Style1); 10751 ASSERT_EQ(*Style1, getLLVMStyle()); 10752 10753 // Test 2.1: fallback to default. 10754 ASSERT_TRUE( 10755 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10756 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10757 ASSERT_TRUE((bool)Style2); 10758 ASSERT_EQ(*Style2, getMozillaStyle()); 10759 10760 // Test 2.2: no format on 'none' fallback style. 10761 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10762 ASSERT_TRUE((bool)Style2); 10763 ASSERT_EQ(*Style2, getNoStyle()); 10764 10765 // Test 2.3: format if config is found with no based style while fallback is 10766 // 'none'. 10767 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 10768 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 10769 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10770 ASSERT_TRUE((bool)Style2); 10771 ASSERT_EQ(*Style2, getLLVMStyle()); 10772 10773 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 10774 Style2 = getStyle("{}", "a.h", "none", "", &FS); 10775 ASSERT_TRUE((bool)Style2); 10776 ASSERT_EQ(*Style2, getLLVMStyle()); 10777 10778 // Test 3: format file in parent directory. 10779 ASSERT_TRUE( 10780 FS.addFile("/c/.clang-format", 0, 10781 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10782 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10783 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10784 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10785 ASSERT_TRUE((bool)Style3); 10786 ASSERT_EQ(*Style3, getGoogleStyle()); 10787 10788 // Test 4: error on invalid fallback style 10789 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 10790 ASSERT_FALSE((bool)Style4); 10791 llvm::consumeError(Style4.takeError()); 10792 10793 // Test 5: error on invalid yaml on command line 10794 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 10795 ASSERT_FALSE((bool)Style5); 10796 llvm::consumeError(Style5.takeError()); 10797 10798 // Test 6: error on invalid style 10799 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 10800 ASSERT_FALSE((bool)Style6); 10801 llvm::consumeError(Style6.takeError()); 10802 10803 // Test 7: found config file, error on parsing it 10804 ASSERT_TRUE( 10805 FS.addFile("/d/.clang-format", 0, 10806 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 10807 "InvalidKey: InvalidValue"))); 10808 ASSERT_TRUE( 10809 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10810 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 10811 ASSERT_FALSE((bool)Style7); 10812 llvm::consumeError(Style7.takeError()); 10813 } 10814 10815 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 10816 // Column limit is 20. 10817 std::string Code = "Type *a =\n" 10818 " new Type();\n" 10819 "g(iiiii, 0, jjjjj,\n" 10820 " 0, kkkkk, 0, mm);\n" 10821 "int bad = format ;"; 10822 std::string Expected = "auto a = new Type();\n" 10823 "g(iiiii, nullptr,\n" 10824 " jjjjj, nullptr,\n" 10825 " kkkkk, nullptr,\n" 10826 " mm);\n" 10827 "int bad = format ;"; 10828 FileID ID = Context.createInMemoryFile("format.cpp", Code); 10829 tooling::Replacements Replaces = toReplacements( 10830 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 10831 "auto "), 10832 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 10833 "nullptr"), 10834 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 10835 "nullptr"), 10836 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 10837 "nullptr")}); 10838 10839 format::FormatStyle Style = format::getLLVMStyle(); 10840 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 10841 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10842 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10843 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10844 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10845 EXPECT_TRUE(static_cast<bool>(Result)); 10846 EXPECT_EQ(Expected, *Result); 10847 } 10848 10849 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 10850 std::string Code = "#include \"a.h\"\n" 10851 "#include \"c.h\"\n" 10852 "\n" 10853 "int main() {\n" 10854 " return 0;\n" 10855 "}"; 10856 std::string Expected = "#include \"a.h\"\n" 10857 "#include \"b.h\"\n" 10858 "#include \"c.h\"\n" 10859 "\n" 10860 "int main() {\n" 10861 " return 0;\n" 10862 "}"; 10863 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 10864 tooling::Replacements Replaces = toReplacements( 10865 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 10866 "#include \"b.h\"\n")}); 10867 10868 format::FormatStyle Style = format::getLLVMStyle(); 10869 Style.SortIncludes = true; 10870 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10871 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10872 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10873 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10874 EXPECT_TRUE(static_cast<bool>(Result)); 10875 EXPECT_EQ(Expected, *Result); 10876 } 10877 10878 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 10879 EXPECT_EQ("using std::cin;\n" 10880 "using std::cout;", 10881 format("using std::cout;\n" 10882 "using std::cin;", getGoogleStyle())); 10883 } 10884 10885 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 10886 format::FormatStyle Style = format::getLLVMStyle(); 10887 Style.Standard = FormatStyle::LS_Cpp03; 10888 // cpp03 recognize this string as identifier u8 and literal character 'a' 10889 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 10890 } 10891 10892 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 10893 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 10894 // all modes, including C++11, C++14 and C++17 10895 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 10896 } 10897 10898 } // end namespace 10899 } // end namespace format 10900 } // end namespace clang 10901