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 IncompleteCheck { 34 IC_ExpectComplete, 35 IC_ExpectIncomplete, 36 IC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 IncompleteCheck CheckIncomplete = IC_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 bool IncompleteFormat = false; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 48 if (CheckIncomplete != IC_DoNotCheck) { 49 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 50 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 60 FormatStyle Style = getLLVMStyle(); 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 66 FormatStyle Style = getGoogleStyle(); 67 Style.ColumnLimit = ColumnLimit; 68 return Style; 69 } 70 71 void verifyFormat(llvm::StringRef Code, 72 const FormatStyle &Style = getLLVMStyle()) { 73 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 74 } 75 76 void verifyIncompleteFormat(llvm::StringRef Code, 77 const FormatStyle &Style = getLLVMStyle()) { 78 EXPECT_EQ(Code.str(), 79 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 80 } 81 82 void verifyGoogleFormat(llvm::StringRef Code) { 83 verifyFormat(Code, getGoogleStyle()); 84 } 85 86 void verifyIndependentOfContext(llvm::StringRef text) { 87 verifyFormat(text); 88 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 89 } 90 91 /// \brief Verify that clang-format does not crash on the given input. 92 void verifyNoCrash(llvm::StringRef Code, 93 const FormatStyle &Style = getLLVMStyle()) { 94 format(Code, Style, IC_DoNotCheck); 95 } 96 97 int ReplacementCount; 98 }; 99 100 TEST_F(FormatTest, MessUp) { 101 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 102 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 103 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 104 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 105 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 106 } 107 108 //===----------------------------------------------------------------------===// 109 // Basic function tests. 110 //===----------------------------------------------------------------------===// 111 112 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 113 EXPECT_EQ(";", format(";")); 114 } 115 116 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 117 EXPECT_EQ("int i;", format(" int i;")); 118 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 119 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 120 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 121 } 122 123 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 124 EXPECT_EQ("int i;", format("int\ni;")); 125 } 126 127 TEST_F(FormatTest, FormatsNestedBlockStatements) { 128 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 129 } 130 131 TEST_F(FormatTest, FormatsNestedCall) { 132 verifyFormat("Method(f1, f2(f3));"); 133 verifyFormat("Method(f1(f2, f3()));"); 134 verifyFormat("Method(f1(f2, (f3())));"); 135 } 136 137 TEST_F(FormatTest, NestedNameSpecifiers) { 138 verifyFormat("vector<::Type> v;"); 139 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 140 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 141 verifyFormat("bool a = 2 < ::SomeFunction();"); 142 } 143 144 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 145 EXPECT_EQ("if (a) {\n" 146 " f();\n" 147 "}", 148 format("if(a){f();}")); 149 EXPECT_EQ(4, ReplacementCount); 150 EXPECT_EQ("if (a) {\n" 151 " f();\n" 152 "}", 153 format("if (a) {\n" 154 " f();\n" 155 "}")); 156 EXPECT_EQ(0, ReplacementCount); 157 EXPECT_EQ("/*\r\n" 158 "\r\n" 159 "*/\r\n", 160 format("/*\r\n" 161 "\r\n" 162 "*/\r\n")); 163 EXPECT_EQ(0, ReplacementCount); 164 } 165 166 TEST_F(FormatTest, RemovesEmptyLines) { 167 EXPECT_EQ("class C {\n" 168 " int i;\n" 169 "};", 170 format("class C {\n" 171 " int i;\n" 172 "\n" 173 "};")); 174 175 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 176 EXPECT_EQ("namespace N {\n" 177 "\n" 178 "int i;\n" 179 "}", 180 format("namespace N {\n" 181 "\n" 182 "int i;\n" 183 "}", 184 getGoogleStyle())); 185 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 186 "\n" 187 "int i;\n" 188 "}", 189 format("extern /**/ \"C\" /**/ {\n" 190 "\n" 191 "int i;\n" 192 "}", 193 getGoogleStyle())); 194 195 // ...but do keep inlining and removing empty lines for non-block extern "C" 196 // functions. 197 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 198 EXPECT_EQ("extern \"C\" int f() {\n" 199 " int i = 42;\n" 200 " return i;\n" 201 "}", 202 format("extern \"C\" int f() {\n" 203 "\n" 204 " int i = 42;\n" 205 " return i;\n" 206 "}", 207 getGoogleStyle())); 208 209 // Remove empty lines at the beginning and end of blocks. 210 EXPECT_EQ("void f() {\n" 211 "\n" 212 " if (a) {\n" 213 "\n" 214 " f();\n" 215 " }\n" 216 "}", 217 format("void f() {\n" 218 "\n" 219 " if (a) {\n" 220 "\n" 221 " f();\n" 222 "\n" 223 " }\n" 224 "\n" 225 "}", 226 getLLVMStyle())); 227 EXPECT_EQ("void f() {\n" 228 " if (a) {\n" 229 " f();\n" 230 " }\n" 231 "}", 232 format("void f() {\n" 233 "\n" 234 " if (a) {\n" 235 "\n" 236 " f();\n" 237 "\n" 238 " }\n" 239 "\n" 240 "}", 241 getGoogleStyle())); 242 243 // Don't remove empty lines in more complex control statements. 244 EXPECT_EQ("void f() {\n" 245 " if (a) {\n" 246 " f();\n" 247 "\n" 248 " } else if (b) {\n" 249 " f();\n" 250 " }\n" 251 "}", 252 format("void f() {\n" 253 " if (a) {\n" 254 " f();\n" 255 "\n" 256 " } else if (b) {\n" 257 " f();\n" 258 "\n" 259 " }\n" 260 "\n" 261 "}")); 262 263 // FIXME: This is slightly inconsistent. 264 EXPECT_EQ("namespace {\n" 265 "int i;\n" 266 "}", 267 format("namespace {\n" 268 "int i;\n" 269 "\n" 270 "}")); 271 EXPECT_EQ("namespace {\n" 272 "int i;\n" 273 "\n" 274 "} // namespace", 275 format("namespace {\n" 276 "int i;\n" 277 "\n" 278 "} // namespace")); 279 280 FormatStyle Style = getLLVMStyle(); 281 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 282 Style.MaxEmptyLinesToKeep = 2; 283 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 284 Style.BraceWrapping.AfterClass = true; 285 Style.BraceWrapping.AfterFunction = true; 286 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 287 288 EXPECT_EQ("class Foo\n" 289 "{\n" 290 " Foo() {}\n" 291 "\n" 292 " void funk() {}\n" 293 "};", 294 format("class Foo\n" 295 "{\n" 296 " Foo()\n" 297 " {\n" 298 " }\n" 299 "\n" 300 " void funk() {}\n" 301 "};", 302 Style)); 303 } 304 305 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 306 verifyFormat("x = (a) and (b);"); 307 verifyFormat("x = (a) or (b);"); 308 verifyFormat("x = (a) bitand (b);"); 309 verifyFormat("x = (a) bitor (b);"); 310 verifyFormat("x = (a) not_eq (b);"); 311 verifyFormat("x = (a) and_eq (b);"); 312 verifyFormat("x = (a) or_eq (b);"); 313 verifyFormat("x = (a) xor (b);"); 314 } 315 316 //===----------------------------------------------------------------------===// 317 // Tests for control statements. 318 //===----------------------------------------------------------------------===// 319 320 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 321 verifyFormat("if (true)\n f();\ng();"); 322 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 323 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 324 325 FormatStyle AllowsMergedIf = getLLVMStyle(); 326 AllowsMergedIf.AlignEscapedNewlinesLeft = true; 327 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 328 verifyFormat("if (a)\n" 329 " // comment\n" 330 " f();", 331 AllowsMergedIf); 332 verifyFormat("{\n" 333 " if (a)\n" 334 " label:\n" 335 " f();\n" 336 "}", 337 AllowsMergedIf); 338 verifyFormat("#define A \\\n" 339 " if (a) \\\n" 340 " label: \\\n" 341 " f()", 342 AllowsMergedIf); 343 verifyFormat("if (a)\n" 344 " ;", 345 AllowsMergedIf); 346 verifyFormat("if (a)\n" 347 " if (b) return;", 348 AllowsMergedIf); 349 350 verifyFormat("if (a) // Can't merge this\n" 351 " f();\n", 352 AllowsMergedIf); 353 verifyFormat("if (a) /* still don't merge */\n" 354 " f();", 355 AllowsMergedIf); 356 verifyFormat("if (a) { // Never merge this\n" 357 " f();\n" 358 "}", 359 AllowsMergedIf); 360 verifyFormat("if (a) { /* Never merge this */\n" 361 " f();\n" 362 "}", 363 AllowsMergedIf); 364 365 AllowsMergedIf.ColumnLimit = 14; 366 verifyFormat("if (a) return;", AllowsMergedIf); 367 verifyFormat("if (aaaaaaaaa)\n" 368 " return;", 369 AllowsMergedIf); 370 371 AllowsMergedIf.ColumnLimit = 13; 372 verifyFormat("if (a)\n return;", AllowsMergedIf); 373 } 374 375 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 376 FormatStyle AllowsMergedLoops = getLLVMStyle(); 377 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 378 verifyFormat("while (true) continue;", AllowsMergedLoops); 379 verifyFormat("for (;;) continue;", AllowsMergedLoops); 380 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 381 verifyFormat("while (true)\n" 382 " ;", 383 AllowsMergedLoops); 384 verifyFormat("for (;;)\n" 385 " ;", 386 AllowsMergedLoops); 387 verifyFormat("for (;;)\n" 388 " for (;;) continue;", 389 AllowsMergedLoops); 390 verifyFormat("for (;;) // Can't merge this\n" 391 " continue;", 392 AllowsMergedLoops); 393 verifyFormat("for (;;) /* still don't merge */\n" 394 " continue;", 395 AllowsMergedLoops); 396 } 397 398 TEST_F(FormatTest, FormatShortBracedStatements) { 399 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 400 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 401 402 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 403 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 404 405 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 406 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 407 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 408 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 409 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 410 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 411 verifyFormat("if (true) { //\n" 412 " f();\n" 413 "}", 414 AllowSimpleBracedStatements); 415 verifyFormat("if (true) {\n" 416 " f();\n" 417 " f();\n" 418 "}", 419 AllowSimpleBracedStatements); 420 verifyFormat("if (true) {\n" 421 " f();\n" 422 "} else {\n" 423 " f();\n" 424 "}", 425 AllowSimpleBracedStatements); 426 427 verifyFormat("template <int> struct A2 {\n" 428 " struct B {};\n" 429 "};", 430 AllowSimpleBracedStatements); 431 432 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 433 verifyFormat("if (true) {\n" 434 " f();\n" 435 "}", 436 AllowSimpleBracedStatements); 437 verifyFormat("if (true) {\n" 438 " f();\n" 439 "} else {\n" 440 " f();\n" 441 "}", 442 AllowSimpleBracedStatements); 443 444 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 445 verifyFormat("while (true) {\n" 446 " f();\n" 447 "}", 448 AllowSimpleBracedStatements); 449 verifyFormat("for (;;) {\n" 450 " f();\n" 451 "}", 452 AllowSimpleBracedStatements); 453 } 454 455 TEST_F(FormatTest, ParseIfElse) { 456 verifyFormat("if (true)\n" 457 " if (true)\n" 458 " if (true)\n" 459 " f();\n" 460 " else\n" 461 " g();\n" 462 " else\n" 463 " h();\n" 464 "else\n" 465 " i();"); 466 verifyFormat("if (true)\n" 467 " if (true)\n" 468 " if (true) {\n" 469 " if (true)\n" 470 " f();\n" 471 " } else {\n" 472 " g();\n" 473 " }\n" 474 " else\n" 475 " h();\n" 476 "else {\n" 477 " i();\n" 478 "}"); 479 verifyFormat("void f() {\n" 480 " if (a) {\n" 481 " } else {\n" 482 " }\n" 483 "}"); 484 } 485 486 TEST_F(FormatTest, ElseIf) { 487 verifyFormat("if (a) {\n} else if (b) {\n}"); 488 verifyFormat("if (a)\n" 489 " f();\n" 490 "else if (b)\n" 491 " g();\n" 492 "else\n" 493 " h();"); 494 verifyFormat("if (a) {\n" 495 " f();\n" 496 "}\n" 497 "// or else ..\n" 498 "else {\n" 499 " g()\n" 500 "}"); 501 502 verifyFormat("if (a) {\n" 503 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 505 "}"); 506 verifyFormat("if (a) {\n" 507 "} else if (\n" 508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 509 "}", 510 getLLVMStyleWithColumns(62)); 511 } 512 513 TEST_F(FormatTest, FormatsForLoop) { 514 verifyFormat( 515 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 516 " ++VeryVeryLongLoopVariable)\n" 517 " ;"); 518 verifyFormat("for (;;)\n" 519 " f();"); 520 verifyFormat("for (;;) {\n}"); 521 verifyFormat("for (;;) {\n" 522 " f();\n" 523 "}"); 524 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 525 526 verifyFormat( 527 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 528 " E = UnwrappedLines.end();\n" 529 " I != E; ++I) {\n}"); 530 531 verifyFormat( 532 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 533 " ++IIIII) {\n}"); 534 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 535 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 536 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 537 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 538 " I = FD->getDeclsInPrototypeScope().begin(),\n" 539 " E = FD->getDeclsInPrototypeScope().end();\n" 540 " I != E; ++I) {\n}"); 541 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 542 " I = Container.begin(),\n" 543 " E = Container.end();\n" 544 " I != E; ++I) {\n}", 545 getLLVMStyleWithColumns(76)); 546 547 verifyFormat( 548 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 552 " ++aaaaaaaaaaa) {\n}"); 553 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 554 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 555 " ++i) {\n}"); 556 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 557 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 558 "}"); 559 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 560 " aaaaaaaaaa);\n" 561 " iter; ++iter) {\n" 562 "}"); 563 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 565 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 566 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 567 568 FormatStyle NoBinPacking = getLLVMStyle(); 569 NoBinPacking.BinPackParameters = false; 570 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 571 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 572 " aaaaaaaaaaaaaaaa,\n" 573 " aaaaaaaaaaaaaaaa,\n" 574 " aaaaaaaaaaaaaaaa);\n" 575 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 576 "}", 577 NoBinPacking); 578 verifyFormat( 579 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 580 " E = UnwrappedLines.end();\n" 581 " I != E;\n" 582 " ++I) {\n}", 583 NoBinPacking); 584 } 585 586 TEST_F(FormatTest, RangeBasedForLoops) { 587 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 589 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 590 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 591 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 593 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 594 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 595 } 596 597 TEST_F(FormatTest, ForEachLoops) { 598 verifyFormat("void f() {\n" 599 " foreach (Item *item, itemlist) {}\n" 600 " Q_FOREACH (Item *item, itemlist) {}\n" 601 " BOOST_FOREACH (Item *item, itemlist) {}\n" 602 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 603 "}"); 604 605 // As function-like macros. 606 verifyFormat("#define foreach(x, y)\n" 607 "#define Q_FOREACH(x, y)\n" 608 "#define BOOST_FOREACH(x, y)\n" 609 "#define UNKNOWN_FOREACH(x, y)\n"); 610 611 // Not as function-like macros. 612 verifyFormat("#define foreach (x, y)\n" 613 "#define Q_FOREACH (x, y)\n" 614 "#define BOOST_FOREACH (x, y)\n" 615 "#define UNKNOWN_FOREACH (x, y)\n"); 616 } 617 618 TEST_F(FormatTest, FormatsWhileLoop) { 619 verifyFormat("while (true) {\n}"); 620 verifyFormat("while (true)\n" 621 " f();"); 622 verifyFormat("while () {\n}"); 623 verifyFormat("while () {\n" 624 " f();\n" 625 "}"); 626 } 627 628 TEST_F(FormatTest, FormatsDoWhile) { 629 verifyFormat("do {\n" 630 " do_something();\n" 631 "} while (something());"); 632 verifyFormat("do\n" 633 " do_something();\n" 634 "while (something());"); 635 } 636 637 TEST_F(FormatTest, FormatsSwitchStatement) { 638 verifyFormat("switch (x) {\n" 639 "case 1:\n" 640 " f();\n" 641 " break;\n" 642 "case kFoo:\n" 643 "case ns::kBar:\n" 644 "case kBaz:\n" 645 " break;\n" 646 "default:\n" 647 " g();\n" 648 " break;\n" 649 "}"); 650 verifyFormat("switch (x) {\n" 651 "case 1: {\n" 652 " f();\n" 653 " break;\n" 654 "}\n" 655 "case 2: {\n" 656 " break;\n" 657 "}\n" 658 "}"); 659 verifyFormat("switch (x) {\n" 660 "case 1: {\n" 661 " f();\n" 662 " {\n" 663 " g();\n" 664 " h();\n" 665 " }\n" 666 " break;\n" 667 "}\n" 668 "}"); 669 verifyFormat("switch (x) {\n" 670 "case 1: {\n" 671 " f();\n" 672 " if (foo) {\n" 673 " g();\n" 674 " h();\n" 675 " }\n" 676 " break;\n" 677 "}\n" 678 "}"); 679 verifyFormat("switch (x) {\n" 680 "case 1: {\n" 681 " f();\n" 682 " g();\n" 683 "} break;\n" 684 "}"); 685 verifyFormat("switch (test)\n" 686 " ;"); 687 verifyFormat("switch (x) {\n" 688 "default: {\n" 689 " // Do nothing.\n" 690 "}\n" 691 "}"); 692 verifyFormat("switch (x) {\n" 693 "// comment\n" 694 "// if 1, do f()\n" 695 "case 1:\n" 696 " f();\n" 697 "}"); 698 verifyFormat("switch (x) {\n" 699 "case 1:\n" 700 " // Do amazing stuff\n" 701 " {\n" 702 " f();\n" 703 " g();\n" 704 " }\n" 705 " break;\n" 706 "}"); 707 verifyFormat("#define A \\\n" 708 " switch (x) { \\\n" 709 " case a: \\\n" 710 " foo = b; \\\n" 711 " }", 712 getLLVMStyleWithColumns(20)); 713 verifyFormat("#define OPERATION_CASE(name) \\\n" 714 " case OP_name: \\\n" 715 " return operations::Operation##name\n", 716 getLLVMStyleWithColumns(40)); 717 verifyFormat("switch (x) {\n" 718 "case 1:;\n" 719 "default:;\n" 720 " int i;\n" 721 "}"); 722 723 verifyGoogleFormat("switch (x) {\n" 724 " case 1:\n" 725 " f();\n" 726 " break;\n" 727 " case kFoo:\n" 728 " case ns::kBar:\n" 729 " case kBaz:\n" 730 " break;\n" 731 " default:\n" 732 " g();\n" 733 " break;\n" 734 "}"); 735 verifyGoogleFormat("switch (x) {\n" 736 " case 1: {\n" 737 " f();\n" 738 " break;\n" 739 " }\n" 740 "}"); 741 verifyGoogleFormat("switch (test)\n" 742 " ;"); 743 744 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 745 " case OP_name: \\\n" 746 " return operations::Operation##name\n"); 747 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 748 " // Get the correction operation class.\n" 749 " switch (OpCode) {\n" 750 " CASE(Add);\n" 751 " CASE(Subtract);\n" 752 " default:\n" 753 " return operations::Unknown;\n" 754 " }\n" 755 "#undef OPERATION_CASE\n" 756 "}"); 757 verifyFormat("DEBUG({\n" 758 " switch (x) {\n" 759 " case A:\n" 760 " f();\n" 761 " break;\n" 762 " // On B:\n" 763 " case B:\n" 764 " g();\n" 765 " break;\n" 766 " }\n" 767 "});"); 768 verifyFormat("switch (a) {\n" 769 "case (b):\n" 770 " return;\n" 771 "}"); 772 773 verifyFormat("switch (a) {\n" 774 "case some_namespace::\n" 775 " some_constant:\n" 776 " return;\n" 777 "}", 778 getLLVMStyleWithColumns(34)); 779 } 780 781 TEST_F(FormatTest, CaseRanges) { 782 verifyFormat("switch (x) {\n" 783 "case 'A' ... 'Z':\n" 784 "case 1 ... 5:\n" 785 "case a ... b:\n" 786 " break;\n" 787 "}"); 788 } 789 790 TEST_F(FormatTest, ShortCaseLabels) { 791 FormatStyle Style = getLLVMStyle(); 792 Style.AllowShortCaseLabelsOnASingleLine = true; 793 verifyFormat("switch (a) {\n" 794 "case 1: x = 1; break;\n" 795 "case 2: return;\n" 796 "case 3:\n" 797 "case 4:\n" 798 "case 5: return;\n" 799 "case 6: // comment\n" 800 " return;\n" 801 "case 7:\n" 802 " // comment\n" 803 " return;\n" 804 "case 8:\n" 805 " x = 8; // comment\n" 806 " break;\n" 807 "default: y = 1; break;\n" 808 "}", 809 Style); 810 verifyFormat("switch (a) {\n" 811 "#if FOO\n" 812 "case 0: return 0;\n" 813 "#endif\n" 814 "}", 815 Style); 816 verifyFormat("switch (a) {\n" 817 "case 1: {\n" 818 "}\n" 819 "case 2: {\n" 820 " return;\n" 821 "}\n" 822 "case 3: {\n" 823 " x = 1;\n" 824 " return;\n" 825 "}\n" 826 "case 4:\n" 827 " if (x)\n" 828 " return;\n" 829 "}", 830 Style); 831 Style.ColumnLimit = 21; 832 verifyFormat("switch (a) {\n" 833 "case 1: x = 1; break;\n" 834 "case 2: return;\n" 835 "case 3:\n" 836 "case 4:\n" 837 "case 5: return;\n" 838 "default:\n" 839 " y = 1;\n" 840 " break;\n" 841 "}", 842 Style); 843 } 844 845 TEST_F(FormatTest, FormatsLabels) { 846 verifyFormat("void f() {\n" 847 " some_code();\n" 848 "test_label:\n" 849 " some_other_code();\n" 850 " {\n" 851 " some_more_code();\n" 852 " another_label:\n" 853 " some_more_code();\n" 854 " }\n" 855 "}"); 856 verifyFormat("{\n" 857 " some_code();\n" 858 "test_label:\n" 859 " some_other_code();\n" 860 "}"); 861 verifyFormat("{\n" 862 " some_code();\n" 863 "test_label:;\n" 864 " int i = 0;\n" 865 "}"); 866 } 867 868 //===----------------------------------------------------------------------===// 869 // Tests for comments. 870 //===----------------------------------------------------------------------===// 871 872 TEST_F(FormatTest, UnderstandsSingleLineComments) { 873 verifyFormat("//* */"); 874 verifyFormat("// line 1\n" 875 "// line 2\n" 876 "void f() {}\n"); 877 878 verifyFormat("void f() {\n" 879 " // Doesn't do anything\n" 880 "}"); 881 verifyFormat("SomeObject\n" 882 " // Calling someFunction on SomeObject\n" 883 " .someFunction();"); 884 verifyFormat("auto result = SomeObject\n" 885 " // Calling someFunction on SomeObject\n" 886 " .someFunction();"); 887 verifyFormat("void f(int i, // some comment (probably for i)\n" 888 " int j, // some comment (probably for j)\n" 889 " int k); // some comment (probably for k)"); 890 verifyFormat("void f(int i,\n" 891 " // some comment (probably for j)\n" 892 " int j,\n" 893 " // some comment (probably for k)\n" 894 " int k);"); 895 896 verifyFormat("int i // This is a fancy variable\n" 897 " = 5; // with nicely aligned comment."); 898 899 verifyFormat("// Leading comment.\n" 900 "int a; // Trailing comment."); 901 verifyFormat("int a; // Trailing comment\n" 902 " // on 2\n" 903 " // or 3 lines.\n" 904 "int b;"); 905 verifyFormat("int a; // Trailing comment\n" 906 "\n" 907 "// Leading comment.\n" 908 "int b;"); 909 verifyFormat("int a; // Comment.\n" 910 " // More details.\n" 911 "int bbbb; // Another comment."); 912 verifyFormat( 913 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 914 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 915 "int cccccccccccccccccccccccccccccc; // comment\n" 916 "int ddd; // looooooooooooooooooooooooong comment\n" 917 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 918 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 919 "int ccccccccccccccccccc; // comment"); 920 921 verifyFormat("#include \"a\" // comment\n" 922 "#include \"a/b/c\" // comment"); 923 verifyFormat("#include <a> // comment\n" 924 "#include <a/b/c> // comment"); 925 EXPECT_EQ("#include \"a\" // comment\n" 926 "#include \"a/b/c\" // comment", 927 format("#include \\\n" 928 " \"a\" // comment\n" 929 "#include \"a/b/c\" // comment")); 930 931 verifyFormat("enum E {\n" 932 " // comment\n" 933 " VAL_A, // comment\n" 934 " VAL_B\n" 935 "};"); 936 937 EXPECT_EQ("enum A {\n" 938 " // line a\n" 939 " a,\n" 940 " b, // line b\n" 941 "\n" 942 " // line c\n" 943 " c\n" 944 "};", 945 format("enum A {\n" 946 " // line a\n" 947 " a,\n" 948 " b, // line b\n" 949 "\n" 950 " // line c\n" 951 " c\n" 952 "};", 953 getLLVMStyleWithColumns(20))); 954 EXPECT_EQ("enum A {\n" 955 " a, // line 1\n" 956 " // line 2\n" 957 "};", 958 format("enum A {\n" 959 " a, // line 1\n" 960 " // line 2\n" 961 "};", 962 getLLVMStyleWithColumns(20))); 963 EXPECT_EQ("enum A {\n" 964 " a, // line 1\n" 965 " // line 2\n" 966 "};", 967 format("enum A {\n" 968 " a, // line 1\n" 969 " // line 2\n" 970 "};", 971 getLLVMStyleWithColumns(20))); 972 EXPECT_EQ("enum A {\n" 973 " a, // line 1\n" 974 " // line 2\n" 975 " b\n" 976 "};", 977 format("enum A {\n" 978 " a, // line 1\n" 979 " // line 2\n" 980 " b\n" 981 "};", 982 getLLVMStyleWithColumns(20))); 983 EXPECT_EQ("enum A {\n" 984 " a, // line 1\n" 985 " // line 2\n" 986 " b\n" 987 "};", 988 format("enum A {\n" 989 " a, // line 1\n" 990 " // line 2\n" 991 " b\n" 992 "};", 993 getLLVMStyleWithColumns(20))); 994 verifyFormat( 995 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 996 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 997 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 998 " // Comment inside a statement.\n" 999 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 1000 verifyFormat("SomeFunction(a,\n" 1001 " // comment\n" 1002 " b + x);"); 1003 verifyFormat("SomeFunction(a, a,\n" 1004 " // comment\n" 1005 " b + x);"); 1006 verifyFormat( 1007 "bool aaaaaaaaaaaaa = // comment\n" 1008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1010 1011 verifyFormat("int aaaa; // aaaaa\n" 1012 "int aa; // aaaaaaa", 1013 getLLVMStyleWithColumns(20)); 1014 1015 EXPECT_EQ("void f() { // This does something ..\n" 1016 "}\n" 1017 "int a; // This is unrelated", 1018 format("void f() { // This does something ..\n" 1019 " }\n" 1020 "int a; // This is unrelated")); 1021 EXPECT_EQ("class C {\n" 1022 " void f() { // This does something ..\n" 1023 " } // awesome..\n" 1024 "\n" 1025 " int a; // This is unrelated\n" 1026 "};", 1027 format("class C{void f() { // This does something ..\n" 1028 " } // awesome..\n" 1029 " \n" 1030 "int a; // This is unrelated\n" 1031 "};")); 1032 1033 EXPECT_EQ("int i; // single line trailing comment", 1034 format("int i;\\\n// single line trailing comment")); 1035 1036 verifyGoogleFormat("int a; // Trailing comment."); 1037 1038 verifyFormat("someFunction(anotherFunction( // Force break.\n" 1039 " parameter));"); 1040 1041 verifyGoogleFormat("#endif // HEADER_GUARD"); 1042 1043 verifyFormat("const char *test[] = {\n" 1044 " // A\n" 1045 " \"aaaa\",\n" 1046 " // B\n" 1047 " \"aaaaa\"};"); 1048 verifyGoogleFormat( 1049 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1050 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 1051 EXPECT_EQ("D(a, {\n" 1052 " // test\n" 1053 " int a;\n" 1054 "});", 1055 format("D(a, {\n" 1056 "// test\n" 1057 "int a;\n" 1058 "});")); 1059 1060 EXPECT_EQ("lineWith(); // comment\n" 1061 "// at start\n" 1062 "otherLine();", 1063 format("lineWith(); // comment\n" 1064 "// at start\n" 1065 "otherLine();")); 1066 EXPECT_EQ("lineWith(); // comment\n" 1067 "/*\n" 1068 " * at start */\n" 1069 "otherLine();", 1070 format("lineWith(); // comment\n" 1071 "/*\n" 1072 " * at start */\n" 1073 "otherLine();")); 1074 EXPECT_EQ("lineWith(); // comment\n" 1075 " // at start\n" 1076 "otherLine();", 1077 format("lineWith(); // comment\n" 1078 " // at start\n" 1079 "otherLine();")); 1080 1081 EXPECT_EQ("lineWith(); // comment\n" 1082 "// at start\n" 1083 "otherLine(); // comment", 1084 format("lineWith(); // comment\n" 1085 "// at start\n" 1086 "otherLine(); // comment")); 1087 EXPECT_EQ("lineWith();\n" 1088 "// at start\n" 1089 "otherLine(); // comment", 1090 format("lineWith();\n" 1091 " // at start\n" 1092 "otherLine(); // comment")); 1093 EXPECT_EQ("// first\n" 1094 "// at start\n" 1095 "otherLine(); // comment", 1096 format("// first\n" 1097 " // at start\n" 1098 "otherLine(); // comment")); 1099 EXPECT_EQ("f();\n" 1100 "// first\n" 1101 "// at start\n" 1102 "otherLine(); // comment", 1103 format("f();\n" 1104 "// first\n" 1105 " // at start\n" 1106 "otherLine(); // comment")); 1107 verifyFormat("f(); // comment\n" 1108 "// first\n" 1109 "// at start\n" 1110 "otherLine();"); 1111 EXPECT_EQ("f(); // comment\n" 1112 "// first\n" 1113 "// at start\n" 1114 "otherLine();", 1115 format("f(); // comment\n" 1116 "// first\n" 1117 " // at start\n" 1118 "otherLine();")); 1119 EXPECT_EQ("f(); // comment\n" 1120 " // first\n" 1121 "// at start\n" 1122 "otherLine();", 1123 format("f(); // comment\n" 1124 " // first\n" 1125 "// at start\n" 1126 "otherLine();")); 1127 EXPECT_EQ("void f() {\n" 1128 " lineWith(); // comment\n" 1129 " // at start\n" 1130 "}", 1131 format("void f() {\n" 1132 " lineWith(); // comment\n" 1133 " // at start\n" 1134 "}")); 1135 EXPECT_EQ("int xy; // a\n" 1136 "int z; // b", 1137 format("int xy; // a\n" 1138 "int z; //b")); 1139 EXPECT_EQ("int xy; // a\n" 1140 "int z; // bb", 1141 format("int xy; // a\n" 1142 "int z; //bb", 1143 getLLVMStyleWithColumns(12))); 1144 1145 verifyFormat("#define A \\\n" 1146 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1147 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1148 getLLVMStyleWithColumns(60)); 1149 verifyFormat( 1150 "#define A \\\n" 1151 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1152 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1153 getLLVMStyleWithColumns(61)); 1154 1155 verifyFormat("if ( // This is some comment\n" 1156 " x + 3) {\n" 1157 "}"); 1158 EXPECT_EQ("if ( // This is some comment\n" 1159 " // spanning two lines\n" 1160 " x + 3) {\n" 1161 "}", 1162 format("if( // This is some comment\n" 1163 " // spanning two lines\n" 1164 " x + 3) {\n" 1165 "}")); 1166 1167 verifyNoCrash("/\\\n/"); 1168 verifyNoCrash("/\\\n* */"); 1169 // The 0-character somehow makes the lexer return a proper comment. 1170 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1171 } 1172 1173 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1174 EXPECT_EQ("SomeFunction(a,\n" 1175 " b, // comment\n" 1176 " c);", 1177 format("SomeFunction(a,\n" 1178 " b, // comment\n" 1179 " c);")); 1180 EXPECT_EQ("SomeFunction(a, b,\n" 1181 " // comment\n" 1182 " c);", 1183 format("SomeFunction(a,\n" 1184 " b,\n" 1185 " // comment\n" 1186 " c);")); 1187 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1188 " c);", 1189 format("SomeFunction(a, b, // comment (unclear relation)\n" 1190 " c);")); 1191 EXPECT_EQ("SomeFunction(a, // comment\n" 1192 " b,\n" 1193 " c); // comment", 1194 format("SomeFunction(a, // comment\n" 1195 " b,\n" 1196 " c); // comment")); 1197 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" 1198 " aaaa), //\n" 1199 " aaaa, bbbbb);", 1200 format("aaaaaaaaaa(aaaa(aaaa,\n" 1201 "aaaa), //\n" 1202 "aaaa, bbbbb);")); 1203 } 1204 1205 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1206 EXPECT_EQ("// comment", format("// comment ")); 1207 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1208 format("int aaaaaaa, bbbbbbb; // comment ", 1209 getLLVMStyleWithColumns(33))); 1210 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1211 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1212 } 1213 1214 TEST_F(FormatTest, UnderstandsBlockComments) { 1215 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1216 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 1217 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1218 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1219 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1220 "/* Trailing comment for aa... */\n" 1221 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1222 EXPECT_EQ( 1223 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1224 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1225 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1226 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1227 EXPECT_EQ( 1228 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1229 " aaaaaaaaaaaaaaaaaa,\n" 1230 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1231 "}", 1232 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1233 " aaaaaaaaaaaaaaaaaa ,\n" 1234 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1235 "}")); 1236 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 1237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1238 1239 FormatStyle NoBinPacking = getLLVMStyle(); 1240 NoBinPacking.BinPackParameters = false; 1241 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1242 " /* parameter 2 */ aaaaaa,\n" 1243 " /* parameter 3 */ aaaaaa,\n" 1244 " /* parameter 4 */ aaaaaa);", 1245 NoBinPacking); 1246 1247 // Aligning block comments in macros. 1248 verifyGoogleFormat("#define A \\\n" 1249 " int i; /*a*/ \\\n" 1250 " int jjj; /*b*/"); 1251 } 1252 1253 TEST_F(FormatTest, AlignsBlockComments) { 1254 EXPECT_EQ("/*\n" 1255 " * Really multi-line\n" 1256 " * comment.\n" 1257 " */\n" 1258 "void f() {}", 1259 format(" /*\n" 1260 " * Really multi-line\n" 1261 " * comment.\n" 1262 " */\n" 1263 " void f() {}")); 1264 EXPECT_EQ("class C {\n" 1265 " /*\n" 1266 " * Another multi-line\n" 1267 " * comment.\n" 1268 " */\n" 1269 " void f() {}\n" 1270 "};", 1271 format("class C {\n" 1272 "/*\n" 1273 " * Another multi-line\n" 1274 " * comment.\n" 1275 " */\n" 1276 "void f() {}\n" 1277 "};")); 1278 EXPECT_EQ("/*\n" 1279 " 1. This is a comment with non-trivial formatting.\n" 1280 " 1.1. We have to indent/outdent all lines equally\n" 1281 " 1.1.1. to keep the formatting.\n" 1282 " */", 1283 format(" /*\n" 1284 " 1. This is a comment with non-trivial formatting.\n" 1285 " 1.1. We have to indent/outdent all lines equally\n" 1286 " 1.1.1. to keep the formatting.\n" 1287 " */")); 1288 EXPECT_EQ("/*\n" 1289 "Don't try to outdent if there's not enough indentation.\n" 1290 "*/", 1291 format(" /*\n" 1292 " Don't try to outdent if there's not enough indentation.\n" 1293 " */")); 1294 1295 EXPECT_EQ("int i; /* Comment with empty...\n" 1296 " *\n" 1297 " * line. */", 1298 format("int i; /* Comment with empty...\n" 1299 " *\n" 1300 " * line. */")); 1301 EXPECT_EQ("int foobar = 0; /* comment */\n" 1302 "int bar = 0; /* multiline\n" 1303 " comment 1 */\n" 1304 "int baz = 0; /* multiline\n" 1305 " comment 2 */\n" 1306 "int bzz = 0; /* multiline\n" 1307 " comment 3 */", 1308 format("int foobar = 0; /* comment */\n" 1309 "int bar = 0; /* multiline\n" 1310 " comment 1 */\n" 1311 "int baz = 0; /* multiline\n" 1312 " comment 2 */\n" 1313 "int bzz = 0; /* multiline\n" 1314 " comment 3 */")); 1315 EXPECT_EQ("int foobar = 0; /* comment */\n" 1316 "int bar = 0; /* multiline\n" 1317 " comment */\n" 1318 "int baz = 0; /* multiline\n" 1319 "comment */", 1320 format("int foobar = 0; /* comment */\n" 1321 "int bar = 0; /* multiline\n" 1322 "comment */\n" 1323 "int baz = 0; /* multiline\n" 1324 "comment */")); 1325 } 1326 1327 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1328 FormatStyle Style = getLLVMStyleWithColumns(20); 1329 Style.ReflowComments = false; 1330 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1331 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1332 } 1333 1334 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1335 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1337 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1339 EXPECT_EQ( 1340 "void ffffffffffff(\n" 1341 " int aaaaaaaa, int bbbbbbbb,\n" 1342 " int cccccccccccc) { /*\n" 1343 " aaaaaaaaaa\n" 1344 " aaaaaaaaaaaaa\n" 1345 " bbbbbbbbbbbbbb\n" 1346 " bbbbbbbbbb\n" 1347 " */\n" 1348 "}", 1349 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1350 "{ /*\n" 1351 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1352 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1353 " */\n" 1354 "}", 1355 getLLVMStyleWithColumns(40))); 1356 } 1357 1358 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1359 EXPECT_EQ("void ffffffffff(\n" 1360 " int aaaaa /* test */);", 1361 format("void ffffffffff(int aaaaa /* test */);", 1362 getLLVMStyleWithColumns(35))); 1363 } 1364 1365 TEST_F(FormatTest, SplitsLongCxxComments) { 1366 EXPECT_EQ("// A comment that\n" 1367 "// doesn't fit on\n" 1368 "// one line", 1369 format("// A comment that doesn't fit on one line", 1370 getLLVMStyleWithColumns(20))); 1371 EXPECT_EQ("/// A comment that\n" 1372 "/// doesn't fit on\n" 1373 "/// one line", 1374 format("/// A comment that doesn't fit on one line", 1375 getLLVMStyleWithColumns(20))); 1376 EXPECT_EQ("//! A comment that\n" 1377 "//! doesn't fit on\n" 1378 "//! one line", 1379 format("//! A comment that doesn't fit on one line", 1380 getLLVMStyleWithColumns(20))); 1381 EXPECT_EQ("// a b c d\n" 1382 "// e f g\n" 1383 "// h i j k", 1384 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1385 EXPECT_EQ( 1386 "// a b c d\n" 1387 "// e f g\n" 1388 "// h i j k", 1389 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1390 EXPECT_EQ("if (true) // A comment that\n" 1391 " // doesn't fit on\n" 1392 " // one line", 1393 format("if (true) // A comment that doesn't fit on one line ", 1394 getLLVMStyleWithColumns(30))); 1395 EXPECT_EQ("// Don't_touch_leading_whitespace", 1396 format("// Don't_touch_leading_whitespace", 1397 getLLVMStyleWithColumns(20))); 1398 EXPECT_EQ("// Add leading\n" 1399 "// whitespace", 1400 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1401 EXPECT_EQ("/// Add leading\n" 1402 "/// whitespace", 1403 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1404 EXPECT_EQ("//! Add leading\n" 1405 "//! whitespace", 1406 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1407 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1408 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1409 "// limit", 1410 format("//Even if it makes the line exceed the column limit", 1411 getLLVMStyleWithColumns(51))); 1412 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1413 EXPECT_EQ("/// line 1\n" 1414 "// add leading whitespace", 1415 format("/// line 1\n" 1416 "//add leading whitespace", 1417 getLLVMStyleWithColumns(30))); 1418 EXPECT_EQ("/// line 1\n" 1419 "/// line 2\n" 1420 "//! line 3\n" 1421 "//! line 4\n" 1422 "//! line 5\n" 1423 "// line 6\n" 1424 "// line 7", 1425 format("///line 1\n" 1426 "///line 2\n" 1427 "//! line 3\n" 1428 "//!line 4\n" 1429 "//!line 5\n" 1430 "// line 6\n" 1431 "//line 7", getLLVMStyleWithColumns(20))); 1432 1433 EXPECT_EQ("// aa bb cc dd", 1434 format("// aa bb cc dd ", 1435 getLLVMStyleWithColumns(15))); 1436 1437 EXPECT_EQ("// A comment before\n" 1438 "// a macro\n" 1439 "// definition\n" 1440 "#define a b", 1441 format("// A comment before a macro definition\n" 1442 "#define a b", 1443 getLLVMStyleWithColumns(20))); 1444 EXPECT_EQ("void ffffff(\n" 1445 " int aaaaaaaaa, // wwww\n" 1446 " int bbbbbbbbbb, // xxxxxxx\n" 1447 " // yyyyyyyyyy\n" 1448 " int c, int d, int e) {}", 1449 format("void ffffff(\n" 1450 " int aaaaaaaaa, // wwww\n" 1451 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1452 " int c, int d, int e) {}", 1453 getLLVMStyleWithColumns(40))); 1454 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1455 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1456 getLLVMStyleWithColumns(20))); 1457 EXPECT_EQ( 1458 "#define XXX // a b c d\n" 1459 " // e f g h", 1460 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1461 EXPECT_EQ( 1462 "#define XXX // q w e r\n" 1463 " // t y u i", 1464 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1465 EXPECT_EQ("{\n" 1466 " //\n" 1467 " //\\\n" 1468 " // long 1 2 3 4\n" 1469 " // 5\n" 1470 "}", 1471 format("{\n" 1472 " //\n" 1473 " //\\\n" 1474 " // long 1 2 3 4 5\n" 1475 "}", 1476 getLLVMStyleWithColumns(20))); 1477 } 1478 1479 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1480 EXPECT_EQ("// A comment\n" 1481 "// that doesn't\n" 1482 "// fit on one\n" 1483 "// line", 1484 format("// A comment that doesn't fit on one line", 1485 getLLVMStyleWithColumns(20))); 1486 EXPECT_EQ("/// A comment\n" 1487 "/// that doesn't\n" 1488 "/// fit on one\n" 1489 "/// line", 1490 format("/// A comment that doesn't fit on one line", 1491 getLLVMStyleWithColumns(20))); 1492 } 1493 1494 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1495 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1496 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1497 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1498 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1499 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1500 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1501 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1502 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1503 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1504 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1505 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1506 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1507 getLLVMStyleWithColumns(50))); 1508 // FIXME: One day we might want to implement adjustment of leading whitespace 1509 // of the consecutive lines in this kind of comment: 1510 EXPECT_EQ("double\n" 1511 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1512 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1513 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1514 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1515 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1516 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1517 getLLVMStyleWithColumns(49))); 1518 } 1519 1520 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1521 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1522 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1523 EXPECT_EQ( 1524 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1525 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1526 EXPECT_EQ( 1527 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1528 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1529 } 1530 1531 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1532 EXPECT_EQ("if (xxx ==\n" 1533 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1534 " zzz)\n" 1535 " q();", 1536 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1537 " zzz) q();", 1538 getLLVMStyleWithColumns(40))); 1539 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1540 " yyy && // aaaaaa bbbbbbbb cccc\n" 1541 " zzz)\n" 1542 " q();", 1543 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1544 " zzz) q();", 1545 getLLVMStyleWithColumns(40))); 1546 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1547 " yyy || // aaaaaa bbbbbbbb cccc\n" 1548 " zzz)\n" 1549 " q();", 1550 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1551 " zzz) q();", 1552 getLLVMStyleWithColumns(40))); 1553 EXPECT_EQ("fffffffff(\n" 1554 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1555 " zzz);", 1556 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1557 " zzz);", 1558 getLLVMStyleWithColumns(40))); 1559 } 1560 1561 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1562 EXPECT_EQ("#define A(x) /* \\\n" 1563 " a comment \\\n" 1564 " inside */ \\\n" 1565 " f();", 1566 format("#define A(x) /* \\\n" 1567 " a comment \\\n" 1568 " inside */ \\\n" 1569 " f();", 1570 getLLVMStyleWithColumns(17))); 1571 EXPECT_EQ("#define A( \\\n" 1572 " x) /* \\\n" 1573 " a comment \\\n" 1574 " inside */ \\\n" 1575 " f();", 1576 format("#define A( \\\n" 1577 " x) /* \\\n" 1578 " a comment \\\n" 1579 " inside */ \\\n" 1580 " f();", 1581 getLLVMStyleWithColumns(17))); 1582 } 1583 1584 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1585 EXPECT_EQ("namespace {}\n// Test\n#define A", 1586 format("namespace {}\n // Test\n#define A")); 1587 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1588 format("namespace {}\n /* Test */\n#define A")); 1589 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1590 format("namespace {}\n /* Test */ #define A")); 1591 } 1592 1593 TEST_F(FormatTest, SplitsLongLinesInComments) { 1594 EXPECT_EQ("/* This is a long\n" 1595 " * comment that\n" 1596 " * doesn't\n" 1597 " * fit on one line.\n" 1598 " */", 1599 format("/* " 1600 "This is a long " 1601 "comment that " 1602 "doesn't " 1603 "fit on one line. */", 1604 getLLVMStyleWithColumns(20))); 1605 EXPECT_EQ( 1606 "/* a b c d\n" 1607 " * e f g\n" 1608 " * h i j k\n" 1609 " */", 1610 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1611 EXPECT_EQ( 1612 "/* a b c d\n" 1613 " * e f g\n" 1614 " * h i j k\n" 1615 " */", 1616 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1617 EXPECT_EQ("/*\n" 1618 "This is a long\n" 1619 "comment that doesn't\n" 1620 "fit on one line.\n" 1621 "*/", 1622 format("/*\n" 1623 "This is a long " 1624 "comment that doesn't " 1625 "fit on one line. \n" 1626 "*/", 1627 getLLVMStyleWithColumns(20))); 1628 EXPECT_EQ("/*\n" 1629 " * This is a long\n" 1630 " * comment that\n" 1631 " * doesn't fit on\n" 1632 " * one line.\n" 1633 " */", 1634 format("/* \n" 1635 " * This is a long " 1636 " comment that " 1637 " doesn't fit on " 1638 " one line. \n" 1639 " */", 1640 getLLVMStyleWithColumns(20))); 1641 EXPECT_EQ("/*\n" 1642 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1643 " * so_it_should_be_broken\n" 1644 " * wherever_a_space_occurs\n" 1645 " */", 1646 format("/*\n" 1647 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1648 " so_it_should_be_broken " 1649 " wherever_a_space_occurs \n" 1650 " */", 1651 getLLVMStyleWithColumns(20))); 1652 EXPECT_EQ("/*\n" 1653 " * This_comment_can_not_be_broken_into_lines\n" 1654 " */", 1655 format("/*\n" 1656 " * This_comment_can_not_be_broken_into_lines\n" 1657 " */", 1658 getLLVMStyleWithColumns(20))); 1659 EXPECT_EQ("{\n" 1660 " /*\n" 1661 " This is another\n" 1662 " long comment that\n" 1663 " doesn't fit on one\n" 1664 " line 1234567890\n" 1665 " */\n" 1666 "}", 1667 format("{\n" 1668 "/*\n" 1669 "This is another " 1670 " long comment that " 1671 " doesn't fit on one" 1672 " line 1234567890\n" 1673 "*/\n" 1674 "}", 1675 getLLVMStyleWithColumns(20))); 1676 EXPECT_EQ("{\n" 1677 " /*\n" 1678 " * This i s\n" 1679 " * another comment\n" 1680 " * t hat doesn' t\n" 1681 " * fit on one l i\n" 1682 " * n e\n" 1683 " */\n" 1684 "}", 1685 format("{\n" 1686 "/*\n" 1687 " * This i s" 1688 " another comment" 1689 " t hat doesn' t" 1690 " fit on one l i" 1691 " n e\n" 1692 " */\n" 1693 "}", 1694 getLLVMStyleWithColumns(20))); 1695 EXPECT_EQ("/*\n" 1696 " * This is a long\n" 1697 " * comment that\n" 1698 " * doesn't fit on\n" 1699 " * one line\n" 1700 " */", 1701 format(" /*\n" 1702 " * This is a long comment that doesn't fit on one line\n" 1703 " */", 1704 getLLVMStyleWithColumns(20))); 1705 EXPECT_EQ("{\n" 1706 " if (something) /* This is a\n" 1707 " long\n" 1708 " comment */\n" 1709 " ;\n" 1710 "}", 1711 format("{\n" 1712 " if (something) /* This is a long comment */\n" 1713 " ;\n" 1714 "}", 1715 getLLVMStyleWithColumns(30))); 1716 1717 EXPECT_EQ("/* A comment before\n" 1718 " * a macro\n" 1719 " * definition */\n" 1720 "#define a b", 1721 format("/* A comment before a macro definition */\n" 1722 "#define a b", 1723 getLLVMStyleWithColumns(20))); 1724 1725 EXPECT_EQ("/* some comment\n" 1726 " * a comment\n" 1727 "* that we break\n" 1728 " * another comment\n" 1729 "* we have to break\n" 1730 "* a left comment\n" 1731 " */", 1732 format(" /* some comment\n" 1733 " * a comment that we break\n" 1734 " * another comment we have to break\n" 1735 "* a left comment\n" 1736 " */", 1737 getLLVMStyleWithColumns(20))); 1738 1739 EXPECT_EQ("/**\n" 1740 " * multiline block\n" 1741 " * comment\n" 1742 " *\n" 1743 " */", 1744 format("/**\n" 1745 " * multiline block comment\n" 1746 " *\n" 1747 " */", 1748 getLLVMStyleWithColumns(20))); 1749 1750 EXPECT_EQ("/*\n" 1751 "\n" 1752 "\n" 1753 " */\n", 1754 format(" /* \n" 1755 " \n" 1756 " \n" 1757 " */\n")); 1758 1759 EXPECT_EQ("/* a a */", 1760 format("/* a a */", getLLVMStyleWithColumns(15))); 1761 EXPECT_EQ("/* a a bc */", 1762 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1763 EXPECT_EQ("/* aaa aaa\n" 1764 " * aaaaa */", 1765 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1766 EXPECT_EQ("/* aaa aaa\n" 1767 " * aaaaa */", 1768 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1769 } 1770 1771 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1772 EXPECT_EQ("#define X \\\n" 1773 " /* \\\n" 1774 " Test \\\n" 1775 " Macro comment \\\n" 1776 " with a long \\\n" 1777 " line \\\n" 1778 " */ \\\n" 1779 " A + B", 1780 format("#define X \\\n" 1781 " /*\n" 1782 " Test\n" 1783 " Macro comment with a long line\n" 1784 " */ \\\n" 1785 " A + B", 1786 getLLVMStyleWithColumns(20))); 1787 EXPECT_EQ("#define X \\\n" 1788 " /* Macro comment \\\n" 1789 " with a long \\\n" 1790 " line */ \\\n" 1791 " A + B", 1792 format("#define X \\\n" 1793 " /* Macro comment with a long\n" 1794 " line */ \\\n" 1795 " A + B", 1796 getLLVMStyleWithColumns(20))); 1797 EXPECT_EQ("#define X \\\n" 1798 " /* Macro comment \\\n" 1799 " * with a long \\\n" 1800 " * line */ \\\n" 1801 " A + B", 1802 format("#define X \\\n" 1803 " /* Macro comment with a long line */ \\\n" 1804 " A + B", 1805 getLLVMStyleWithColumns(20))); 1806 } 1807 1808 TEST_F(FormatTest, CommentsInStaticInitializers) { 1809 EXPECT_EQ( 1810 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1811 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1812 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1813 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1814 " aaaaaaaaaaaaaaaaaaaa};", 1815 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1816 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1817 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1818 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1819 " aaaaaaaaaaaaaaaaaaaa };")); 1820 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1821 " bbbbbbbbbbb, ccccccccccc};"); 1822 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1823 " // comment for bb....\n" 1824 " bbbbbbbbbbb, ccccccccccc};"); 1825 verifyGoogleFormat( 1826 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1827 " bbbbbbbbbbb, ccccccccccc};"); 1828 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1829 " // comment for bb....\n" 1830 " bbbbbbbbbbb, ccccccccccc};"); 1831 1832 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1833 " {d, e, f}, // Group #2\n" 1834 " {g, h, i}}; // Group #3"); 1835 verifyFormat("S s = {{// Group #1\n" 1836 " a, b, c},\n" 1837 " {// Group #2\n" 1838 " d, e, f},\n" 1839 " {// Group #3\n" 1840 " g, h, i}};"); 1841 1842 EXPECT_EQ("S s = {\n" 1843 " // Some comment\n" 1844 " a,\n" 1845 "\n" 1846 " // Comment after empty line\n" 1847 " b}", 1848 format("S s = {\n" 1849 " // Some comment\n" 1850 " a,\n" 1851 " \n" 1852 " // Comment after empty line\n" 1853 " b\n" 1854 "}")); 1855 EXPECT_EQ("S s = {\n" 1856 " /* Some comment */\n" 1857 " a,\n" 1858 "\n" 1859 " /* Comment after empty line */\n" 1860 " b}", 1861 format("S s = {\n" 1862 " /* Some comment */\n" 1863 " a,\n" 1864 " \n" 1865 " /* Comment after empty line */\n" 1866 " b\n" 1867 "}")); 1868 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1869 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1870 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1871 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1872 } 1873 1874 TEST_F(FormatTest, LineCommentsAfterRightBrace) { 1875 EXPECT_EQ("if (true) { // comment about branch\n" 1876 " // comment about f\n" 1877 " f();\n" 1878 "}", 1879 format("if (true) { // comment about branch\n" 1880 " // comment about f\n" 1881 " f();\n" 1882 "}", 1883 getLLVMStyleWithColumns(80))); 1884 EXPECT_EQ("if (1) { // if line 1\n" 1885 " // if line 2\n" 1886 " // if line 3\n" 1887 " // f line 1\n" 1888 " // f line 2\n" 1889 " f();\n" 1890 "} else { // else line 1\n" 1891 " // else line 2\n" 1892 " // else line 3\n" 1893 " // g line 1\n" 1894 " g();\n" 1895 "}", 1896 format("if (1) { // if line 1\n" 1897 " // if line 2\n" 1898 " // if line 3\n" 1899 " // f line 1\n" 1900 " // f line 2\n" 1901 " f();\n" 1902 "} else { // else line 1\n" 1903 " // else line 2\n" 1904 " // else line 3\n" 1905 " // g line 1\n" 1906 " g();\n" 1907 "}")); 1908 EXPECT_EQ("do { // line 1\n" 1909 " // line 2\n" 1910 " // line 3\n" 1911 " f();\n" 1912 "} while (true);", 1913 format("do { // line 1\n" 1914 " // line 2\n" 1915 " // line 3\n" 1916 " f();\n" 1917 "} while (true);", 1918 getLLVMStyleWithColumns(80))); 1919 EXPECT_EQ("while (a < b) { // line 1\n" 1920 " // line 2\n" 1921 " // line 3\n" 1922 " f();\n" 1923 "}", 1924 format("while (a < b) {// line 1\n" 1925 " // line 2\n" 1926 " // line 3\n" 1927 " f();\n" 1928 "}", 1929 getLLVMStyleWithColumns(80))); 1930 } 1931 1932 TEST_F(FormatTest, ReflowsComments) { 1933 // Break a long line and reflow with the full next line. 1934 EXPECT_EQ("// long long long\n" 1935 "// long long", 1936 format("// long long long long\n" 1937 "// long", 1938 getLLVMStyleWithColumns(20))); 1939 1940 // Keep the trailing newline while reflowing. 1941 EXPECT_EQ("// long long long\n" 1942 "// long long\n", 1943 format("// long long long long\n" 1944 "// long\n", 1945 getLLVMStyleWithColumns(20))); 1946 1947 // Break a long line and reflow with a part of the next line. 1948 EXPECT_EQ("// long long long\n" 1949 "// long long\n" 1950 "// long_long", 1951 format("// long long long long\n" 1952 "// long long_long", 1953 getLLVMStyleWithColumns(20))); 1954 1955 // Break but do not reflow if the first word from the next line is too long. 1956 EXPECT_EQ("// long long long\n" 1957 "// long\n" 1958 "// long_long_long\n", 1959 format("// long long long long\n" 1960 "// long_long_long\n", 1961 getLLVMStyleWithColumns(20))); 1962 1963 // Don't break or reflow short lines. 1964 verifyFormat("// long\n" 1965 "// long long long lo\n" 1966 "// long long long lo\n" 1967 "// long", 1968 getLLVMStyleWithColumns(20)); 1969 1970 // Keep prefixes and decorations while reflowing. 1971 EXPECT_EQ("/// long long long\n" 1972 "/// long long\n", 1973 format("/// long long long long\n" 1974 "/// long\n", 1975 getLLVMStyleWithColumns(20))); 1976 EXPECT_EQ("//! long long long\n" 1977 "//! long long\n", 1978 format("//! long long long long\n" 1979 "//! long\n", 1980 getLLVMStyleWithColumns(20))); 1981 EXPECT_EQ("/* long long long\n" 1982 " * long long */", 1983 format("/* long long long long\n" 1984 " * long */", 1985 getLLVMStyleWithColumns(20))); 1986 1987 // Don't bring leading whitespace up while reflowing. 1988 EXPECT_EQ("/* long long long\n" 1989 " * long long long\n" 1990 " */", 1991 format("/* long long long long\n" 1992 " * long long\n" 1993 " */", 1994 getLLVMStyleWithColumns(20))); 1995 1996 // Reflow the last line of a block comment with its trailing '*/'. 1997 EXPECT_EQ("/* long long long\n" 1998 " long long */", 1999 format("/* long long long long\n" 2000 " long */", 2001 getLLVMStyleWithColumns(20))); 2002 2003 // Reflow two short lines; keep the postfix of the last one. 2004 EXPECT_EQ("/* long long long\n" 2005 " * long long long */", 2006 format("/* long long long long\n" 2007 " * long\n" 2008 " * long */", 2009 getLLVMStyleWithColumns(20))); 2010 2011 // Put the postfix of the last short reflow line on a newline if it doesn't 2012 // fit. 2013 EXPECT_EQ("/* long long long\n" 2014 " * long long longg\n" 2015 " */", 2016 format("/* long long long long\n" 2017 " * long\n" 2018 " * longg */", 2019 getLLVMStyleWithColumns(20))); 2020 2021 // Reflow lines with leading whitespace. 2022 EXPECT_EQ("{\n" 2023 " /*\n" 2024 " * long long long\n" 2025 " * long long long\n" 2026 " * long long long\n" 2027 " */\n" 2028 "}", 2029 format("{\n" 2030 "/*\n" 2031 " * long long long long\n" 2032 " * long\n" 2033 " * long long long long\n" 2034 " */\n" 2035 "}", 2036 getLLVMStyleWithColumns(20))); 2037 2038 // Break single line block comments that are first in the line with ' *' 2039 // decoration. 2040 EXPECT_EQ("/* long long long\n" 2041 " * long */", 2042 format("/* long long long long */", getLLVMStyleWithColumns(20))); 2043 2044 // Break single line block comment that are not first in the line with ' ' 2045 // decoration. 2046 EXPECT_EQ("int i; /* long long\n" 2047 " long */", 2048 format("int i; /* long long long */", getLLVMStyleWithColumns(20))); 2049 2050 // Reflow a line that goes just over the column limit. 2051 EXPECT_EQ("// long long long\n" 2052 "// lon long", 2053 format("// long long long lon\n" 2054 "// long", 2055 getLLVMStyleWithColumns(20))); 2056 2057 // Stop reflowing if the next line has a different indentation than the 2058 // previous line. 2059 EXPECT_EQ("// long long long\n" 2060 "// long\n" 2061 "// long long\n" 2062 "// long", 2063 format("// long long long long\n" 2064 "// long long\n" 2065 "// long", 2066 getLLVMStyleWithColumns(20))); 2067 2068 // Reflow into the last part of a really long line that has been broken into 2069 // multiple lines. 2070 EXPECT_EQ("// long long long\n" 2071 "// long long long\n" 2072 "// long long long\n", 2073 format("// long long long long long long long long\n" 2074 "// long\n", 2075 getLLVMStyleWithColumns(20))); 2076 2077 // Break the first line, then reflow the beginning of the second and third 2078 // line up. 2079 EXPECT_EQ("// long long long\n" 2080 "// lon1 lon2 lon2\n" 2081 "// lon2 lon3 lon3", 2082 format("// long long long lon1\n" 2083 "// lon2 lon2 lon2\n" 2084 "// lon3 lon3", 2085 getLLVMStyleWithColumns(20))); 2086 2087 // Reflow the beginning of the second line, then break the rest. 2088 EXPECT_EQ("// long long long\n" 2089 "// lon1 lon2 lon2\n" 2090 "// lon2 lon2 lon2\n" 2091 "// lon3", 2092 format("// long long long lon1\n" 2093 "// lon2 lon2 lon2 lon2 lon2 lon3", 2094 getLLVMStyleWithColumns(20))); 2095 2096 // Shrink the first line, then reflow the second line up. 2097 EXPECT_EQ("// long long long", format("// long long\n" 2098 "// long", 2099 getLLVMStyleWithColumns(20))); 2100 2101 // Don't shrink leading whitespace. 2102 EXPECT_EQ("int i; /// a", 2103 format("int i; /// a", getLLVMStyleWithColumns(20))); 2104 2105 // Shrink trailing whitespace if there is no postfix and reflow. 2106 EXPECT_EQ("// long long long\n" 2107 "// long long", 2108 format("// long long long long \n" 2109 "// long", 2110 getLLVMStyleWithColumns(20))); 2111 2112 // Shrink trailing whitespace to a single one if there is postfix. 2113 EXPECT_EQ("/* long long long */", 2114 format("/* long long long */", getLLVMStyleWithColumns(20))); 2115 2116 // Break a block comment postfix if exceeding the line limit. 2117 EXPECT_EQ("/* long\n" 2118 " */", 2119 format("/* long */", getLLVMStyleWithColumns(20))); 2120 2121 // Reflow indented comments. 2122 EXPECT_EQ("{\n" 2123 " // long long long\n" 2124 " // long long\n" 2125 " int i; /* long lon\n" 2126 " g long\n" 2127 " */\n" 2128 "}", 2129 format("{\n" 2130 " // long long long long\n" 2131 " // long\n" 2132 " int i; /* long lon g\n" 2133 " long */\n" 2134 "}", 2135 getLLVMStyleWithColumns(20))); 2136 2137 // Don't realign trailing comments after reflow has happened. 2138 EXPECT_EQ("// long long long\n" 2139 "// long long\n" 2140 "long i; // long", 2141 format("// long long long long\n" 2142 "// long\n" 2143 "long i; // long", 2144 getLLVMStyleWithColumns(20))); 2145 EXPECT_EQ("// long long long\n" 2146 "// longng long long\n" 2147 "// long lo", 2148 format("// long long long longng\n" 2149 "// long long long\n" 2150 "// lo", 2151 getLLVMStyleWithColumns(20))); 2152 2153 // Reflow lines after a broken line. 2154 EXPECT_EQ("int a; // Trailing\n" 2155 " // comment on\n" 2156 " // 2 or 3\n" 2157 " // lines.\n", 2158 format("int a; // Trailing comment\n" 2159 " // on 2\n" 2160 " // or 3\n" 2161 " // lines.\n", 2162 getLLVMStyleWithColumns(20))); 2163 EXPECT_EQ("/// This long line\n" 2164 "/// gets reflown.\n", 2165 format("/// This long line gets\n" 2166 "/// reflown.\n", 2167 getLLVMStyleWithColumns(20))); 2168 EXPECT_EQ("//! This long line\n" 2169 "//! gets reflown.\n", 2170 format(" //! This long line gets\n" 2171 " //! reflown.\n", 2172 getLLVMStyleWithColumns(20))); 2173 EXPECT_EQ("/* This long line\n" 2174 " * gets reflown.\n" 2175 " */\n", 2176 format("/* This long line gets\n" 2177 " * reflown.\n" 2178 " */\n", 2179 getLLVMStyleWithColumns(20))); 2180 2181 // Reflow after indentation makes a line too long. 2182 EXPECT_EQ("{\n" 2183 " // long long long\n" 2184 " // lo long\n" 2185 "}\n", 2186 format("{\n" 2187 "// long long long lo\n" 2188 "// long\n" 2189 "}\n", 2190 getLLVMStyleWithColumns(20))); 2191 2192 // Break and reflow multiple lines. 2193 EXPECT_EQ("/*\n" 2194 " * Reflow the end of\n" 2195 " * line by 11 22 33\n" 2196 " * 4.\n" 2197 " */\n", 2198 format("/*\n" 2199 " * Reflow the end of line\n" 2200 " * by\n" 2201 " * 11\n" 2202 " * 22\n" 2203 " * 33\n" 2204 " * 4.\n" 2205 " */\n", 2206 getLLVMStyleWithColumns(20))); 2207 EXPECT_EQ("/// First line gets\n" 2208 "/// broken. Second\n" 2209 "/// line gets\n" 2210 "/// reflown and\n" 2211 "/// broken. Third\n" 2212 "/// gets reflown.\n", 2213 format("/// First line gets broken.\n" 2214 "/// Second line gets reflown and broken.\n" 2215 "/// Third gets reflown.\n", 2216 getLLVMStyleWithColumns(20))); 2217 EXPECT_EQ("int i; // first long\n" 2218 " // long snd\n" 2219 " // long.\n", 2220 format("int i; // first long long\n" 2221 " // snd long.\n", 2222 getLLVMStyleWithColumns(20))); 2223 EXPECT_EQ("{\n" 2224 " // first long line\n" 2225 " // line second\n" 2226 " // long line line\n" 2227 " // third long line\n" 2228 " // line\n" 2229 "}\n", 2230 format("{\n" 2231 " // first long line line\n" 2232 " // second long line line\n" 2233 " // third long line line\n" 2234 "}\n", 2235 getLLVMStyleWithColumns(20))); 2236 EXPECT_EQ("int i; /* first line\n" 2237 " * second\n" 2238 " * line third\n" 2239 " * line\n" 2240 " */", 2241 format("int i; /* first line\n" 2242 " * second line\n" 2243 " * third line\n" 2244 " */", 2245 getLLVMStyleWithColumns(20))); 2246 2247 // Reflow the last two lines of a section that starts with a line having 2248 // different indentation. 2249 EXPECT_EQ( 2250 "// long\n" 2251 "// long long long\n" 2252 "// long long", 2253 format("// long\n" 2254 "// long long long long\n" 2255 "// long", 2256 getLLVMStyleWithColumns(20))); 2257 2258 // Keep the block comment endling '*/' while reflowing. 2259 EXPECT_EQ("/* Long long long\n" 2260 " * line short */\n", 2261 format("/* Long long long line\n" 2262 " * short */\n", 2263 getLLVMStyleWithColumns(20))); 2264 2265 // Don't reflow between separate blocks of comments. 2266 EXPECT_EQ("/* First comment\n" 2267 " * block will */\n" 2268 "/* Snd\n" 2269 " */\n", 2270 format("/* First comment block\n" 2271 " * will */\n" 2272 "/* Snd\n" 2273 " */\n", 2274 getLLVMStyleWithColumns(20))); 2275 2276 // Don't reflow across blank comment lines. 2277 EXPECT_EQ("int i; // This long\n" 2278 " // line gets\n" 2279 " // broken.\n" 2280 " //\n" 2281 " // keep.\n", 2282 format("int i; // This long line gets broken.\n" 2283 " // \n" 2284 " // keep.\n", 2285 getLLVMStyleWithColumns(20))); 2286 EXPECT_EQ("{\n" 2287 " /// long long long\n" 2288 " /// long long\n" 2289 " ///\n" 2290 " /// long\n" 2291 "}", 2292 format("{\n" 2293 " /// long long long long\n" 2294 " /// long\n" 2295 " ///\n" 2296 " /// long\n" 2297 "}", 2298 getLLVMStyleWithColumns(20))); 2299 EXPECT_EQ("//! long long long\n" 2300 "//! long\n" 2301 "\n" 2302 "//! long", 2303 format("//! long long long long\n" 2304 "\n" 2305 "//! long", 2306 getLLVMStyleWithColumns(20))); 2307 EXPECT_EQ("/* long long long\n" 2308 " long\n" 2309 "\n" 2310 " long */", 2311 format("/* long long long long\n" 2312 "\n" 2313 " long */", 2314 getLLVMStyleWithColumns(20))); 2315 EXPECT_EQ("/* long long long\n" 2316 " * long\n" 2317 " *\n" 2318 " * long */", 2319 format("/* long long long long\n" 2320 " *\n" 2321 " * long */", 2322 getLLVMStyleWithColumns(20))); 2323 2324 // Don't reflow lines having content that is a single character. 2325 EXPECT_EQ("// long long long\n" 2326 "// long\n" 2327 "// l", 2328 format("// long long long long\n" 2329 "// l", 2330 getLLVMStyleWithColumns(20))); 2331 2332 // Don't reflow lines starting with two punctuation characters. 2333 EXPECT_EQ("// long long long\n" 2334 "// long\n" 2335 "// ... --- ...", 2336 format( 2337 "// long long long long\n" 2338 "// ... --- ...", 2339 getLLVMStyleWithColumns(20))); 2340 2341 // Don't reflow lines starting with '@'. 2342 EXPECT_EQ("// long long long\n" 2343 "// long\n" 2344 "// @param arg", 2345 format("// long long long long\n" 2346 "// @param arg", 2347 getLLVMStyleWithColumns(20))); 2348 2349 // Don't reflow lines starting with 'TODO'. 2350 EXPECT_EQ("// long long long\n" 2351 "// long\n" 2352 "// TODO: long", 2353 format("// long long long long\n" 2354 "// TODO: long", 2355 getLLVMStyleWithColumns(20))); 2356 2357 // Don't reflow lines starting with 'FIXME'. 2358 EXPECT_EQ("// long long long\n" 2359 "// long\n" 2360 "// FIXME: long", 2361 format("// long long long long\n" 2362 "// FIXME: long", 2363 getLLVMStyleWithColumns(20))); 2364 2365 // Don't reflow lines starting with 'XXX'. 2366 EXPECT_EQ("// long long long\n" 2367 "// long\n" 2368 "// XXX: long", 2369 format("// long long long long\n" 2370 "// XXX: long", 2371 getLLVMStyleWithColumns(20))); 2372 2373 // Don't reflow comment pragmas. 2374 EXPECT_EQ("// long long long\n" 2375 "// long\n" 2376 "// IWYU pragma:", 2377 format("// long long long long\n" 2378 "// IWYU pragma:", 2379 getLLVMStyleWithColumns(20))); 2380 EXPECT_EQ("/* long long long\n" 2381 " * long\n" 2382 " * IWYU pragma:\n" 2383 " */", 2384 format("/* long long long long\n" 2385 " * IWYU pragma:\n" 2386 " */", 2387 getLLVMStyleWithColumns(20))); 2388 2389 // Reflow lines that have a non-punctuation character among their first 2 2390 // characters. 2391 EXPECT_EQ("// long long long\n" 2392 "// long 'long'", 2393 format( 2394 "// long long long long\n" 2395 "// 'long'", 2396 getLLVMStyleWithColumns(20))); 2397 2398 // Don't reflow between separate blocks of comments. 2399 EXPECT_EQ("/* First comment\n" 2400 " * block will */\n" 2401 "/* Snd\n" 2402 " */\n", 2403 format("/* First comment block\n" 2404 " * will */\n" 2405 "/* Snd\n" 2406 " */\n", 2407 getLLVMStyleWithColumns(20))); 2408 2409 // Don't reflow lines having different indentation. 2410 EXPECT_EQ("// long long long\n" 2411 "// long\n" 2412 "// long", 2413 format("// long long long long\n" 2414 "// long", 2415 getLLVMStyleWithColumns(20))); 2416 2417 // Don't break or reflow after implicit string literals. 2418 verifyFormat("#include <t> // l l l\n" 2419 " // l", 2420 getLLVMStyleWithColumns(20)); 2421 2422 // Don't break or reflow comments on import lines. 2423 EXPECT_EQ("#include \"t\" /* l l l\n" 2424 " * l */", 2425 format("#include \"t\" /* l l l\n" 2426 " * l */", 2427 getLLVMStyleWithColumns(20))); 2428 2429 // Don't reflow between different trailing comment sections. 2430 EXPECT_EQ("int i; // long long\n" 2431 " // long\n" 2432 "int j; // long long\n" 2433 " // long\n", 2434 format("int i; // long long long\n" 2435 "int j; // long long long\n", 2436 getLLVMStyleWithColumns(20))); 2437 2438 // Don't reflow if the first word on the next line is longer than the 2439 // available space at current line. 2440 EXPECT_EQ("int i; // trigger\n" 2441 " // reflow\n" 2442 " // longsec\n", 2443 format("int i; // trigger reflow\n" 2444 " // longsec\n", 2445 getLLVMStyleWithColumns(20))); 2446 2447 // Keep empty comment lines. 2448 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20))); 2449 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2450 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2451 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20))); 2452 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20))); 2453 } 2454 2455 TEST_F(FormatTest, IgnoresIf0Contents) { 2456 EXPECT_EQ("#if 0\n" 2457 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2458 "#endif\n" 2459 "void f() {}", 2460 format("#if 0\n" 2461 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2462 "#endif\n" 2463 "void f( ) { }")); 2464 EXPECT_EQ("#if false\n" 2465 "void f( ) { }\n" 2466 "#endif\n" 2467 "void g() {}\n", 2468 format("#if false\n" 2469 "void f( ) { }\n" 2470 "#endif\n" 2471 "void g( ) { }\n")); 2472 EXPECT_EQ("enum E {\n" 2473 " One,\n" 2474 " Two,\n" 2475 "#if 0\n" 2476 "Three,\n" 2477 " Four,\n" 2478 "#endif\n" 2479 " Five\n" 2480 "};", 2481 format("enum E {\n" 2482 " One,Two,\n" 2483 "#if 0\n" 2484 "Three,\n" 2485 " Four,\n" 2486 "#endif\n" 2487 " Five};")); 2488 EXPECT_EQ("enum F {\n" 2489 " One,\n" 2490 "#if 1\n" 2491 " Two,\n" 2492 "#if 0\n" 2493 "Three,\n" 2494 " Four,\n" 2495 "#endif\n" 2496 " Five\n" 2497 "#endif\n" 2498 "};", 2499 format("enum F {\n" 2500 "One,\n" 2501 "#if 1\n" 2502 "Two,\n" 2503 "#if 0\n" 2504 "Three,\n" 2505 " Four,\n" 2506 "#endif\n" 2507 "Five\n" 2508 "#endif\n" 2509 "};")); 2510 EXPECT_EQ("enum G {\n" 2511 " One,\n" 2512 "#if 0\n" 2513 "Two,\n" 2514 "#else\n" 2515 " Three,\n" 2516 "#endif\n" 2517 " Four\n" 2518 "};", 2519 format("enum G {\n" 2520 "One,\n" 2521 "#if 0\n" 2522 "Two,\n" 2523 "#else\n" 2524 "Three,\n" 2525 "#endif\n" 2526 "Four\n" 2527 "};")); 2528 EXPECT_EQ("enum H {\n" 2529 " One,\n" 2530 "#if 0\n" 2531 "#ifdef Q\n" 2532 "Two,\n" 2533 "#else\n" 2534 "Three,\n" 2535 "#endif\n" 2536 "#endif\n" 2537 " Four\n" 2538 "};", 2539 format("enum H {\n" 2540 "One,\n" 2541 "#if 0\n" 2542 "#ifdef Q\n" 2543 "Two,\n" 2544 "#else\n" 2545 "Three,\n" 2546 "#endif\n" 2547 "#endif\n" 2548 "Four\n" 2549 "};")); 2550 EXPECT_EQ("enum I {\n" 2551 " One,\n" 2552 "#if /* test */ 0 || 1\n" 2553 "Two,\n" 2554 "Three,\n" 2555 "#endif\n" 2556 " Four\n" 2557 "};", 2558 format("enum I {\n" 2559 "One,\n" 2560 "#if /* test */ 0 || 1\n" 2561 "Two,\n" 2562 "Three,\n" 2563 "#endif\n" 2564 "Four\n" 2565 "};")); 2566 EXPECT_EQ("enum J {\n" 2567 " One,\n" 2568 "#if 0\n" 2569 "#if 0\n" 2570 "Two,\n" 2571 "#else\n" 2572 "Three,\n" 2573 "#endif\n" 2574 "Four,\n" 2575 "#endif\n" 2576 " Five\n" 2577 "};", 2578 format("enum J {\n" 2579 "One,\n" 2580 "#if 0\n" 2581 "#if 0\n" 2582 "Two,\n" 2583 "#else\n" 2584 "Three,\n" 2585 "#endif\n" 2586 "Four,\n" 2587 "#endif\n" 2588 "Five\n" 2589 "};")); 2590 } 2591 2592 //===----------------------------------------------------------------------===// 2593 // Tests for classes, namespaces, etc. 2594 //===----------------------------------------------------------------------===// 2595 2596 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2597 verifyFormat("class A {};"); 2598 } 2599 2600 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2601 verifyFormat("class A {\n" 2602 "public:\n" 2603 "public: // comment\n" 2604 "protected:\n" 2605 "private:\n" 2606 " void f() {}\n" 2607 "};"); 2608 verifyGoogleFormat("class A {\n" 2609 " public:\n" 2610 " protected:\n" 2611 " private:\n" 2612 " void f() {}\n" 2613 "};"); 2614 verifyFormat("class A {\n" 2615 "public slots:\n" 2616 " void f1() {}\n" 2617 "public Q_SLOTS:\n" 2618 " void f2() {}\n" 2619 "protected slots:\n" 2620 " void f3() {}\n" 2621 "protected Q_SLOTS:\n" 2622 " void f4() {}\n" 2623 "private slots:\n" 2624 " void f5() {}\n" 2625 "private Q_SLOTS:\n" 2626 " void f6() {}\n" 2627 "signals:\n" 2628 " void g1();\n" 2629 "Q_SIGNALS:\n" 2630 " void g2();\n" 2631 "};"); 2632 2633 // Don't interpret 'signals' the wrong way. 2634 verifyFormat("signals.set();"); 2635 verifyFormat("for (Signals signals : f()) {\n}"); 2636 verifyFormat("{\n" 2637 " signals.set(); // This needs indentation.\n" 2638 "}"); 2639 verifyFormat("void f() {\n" 2640 "label:\n" 2641 " signals.baz();\n" 2642 "}"); 2643 } 2644 2645 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2646 EXPECT_EQ("class A {\n" 2647 "public:\n" 2648 " void f();\n" 2649 "\n" 2650 "private:\n" 2651 " void g() {}\n" 2652 " // test\n" 2653 "protected:\n" 2654 " int h;\n" 2655 "};", 2656 format("class A {\n" 2657 "public:\n" 2658 "void f();\n" 2659 "private:\n" 2660 "void g() {}\n" 2661 "// test\n" 2662 "protected:\n" 2663 "int h;\n" 2664 "};")); 2665 EXPECT_EQ("class A {\n" 2666 "protected:\n" 2667 "public:\n" 2668 " void f();\n" 2669 "};", 2670 format("class A {\n" 2671 "protected:\n" 2672 "\n" 2673 "public:\n" 2674 "\n" 2675 " void f();\n" 2676 "};")); 2677 2678 // Even ensure proper spacing inside macros. 2679 EXPECT_EQ("#define B \\\n" 2680 " class A { \\\n" 2681 " protected: \\\n" 2682 " public: \\\n" 2683 " void f(); \\\n" 2684 " };", 2685 format("#define B \\\n" 2686 " class A { \\\n" 2687 " protected: \\\n" 2688 " \\\n" 2689 " public: \\\n" 2690 " \\\n" 2691 " void f(); \\\n" 2692 " };", 2693 getGoogleStyle())); 2694 // But don't remove empty lines after macros ending in access specifiers. 2695 EXPECT_EQ("#define A private:\n" 2696 "\n" 2697 "int i;", 2698 format("#define A private:\n" 2699 "\n" 2700 "int i;")); 2701 } 2702 2703 TEST_F(FormatTest, FormatsClasses) { 2704 verifyFormat("class A : public B {};"); 2705 verifyFormat("class A : public ::B {};"); 2706 2707 verifyFormat( 2708 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2709 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2710 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2711 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2712 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2713 verifyFormat( 2714 "class A : public B, public C, public D, public E, public F {};"); 2715 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2716 " public C,\n" 2717 " public D,\n" 2718 " public E,\n" 2719 " public F,\n" 2720 " public G {};"); 2721 2722 verifyFormat("class\n" 2723 " ReallyReallyLongClassName {\n" 2724 " int i;\n" 2725 "};", 2726 getLLVMStyleWithColumns(32)); 2727 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2728 " aaaaaaaaaaaaaaaa> {};"); 2729 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2730 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2731 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2732 verifyFormat("template <class R, class C>\n" 2733 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2734 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2735 verifyFormat("class ::A::B {};"); 2736 } 2737 2738 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2739 verifyFormat("class A {\n} a, b;"); 2740 verifyFormat("struct A {\n} a, b;"); 2741 verifyFormat("union A {\n} a;"); 2742 } 2743 2744 TEST_F(FormatTest, FormatsEnum) { 2745 verifyFormat("enum {\n" 2746 " Zero,\n" 2747 " One = 1,\n" 2748 " Two = One + 1,\n" 2749 " Three = (One + Two),\n" 2750 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2751 " Five = (One, Two, Three, Four, 5)\n" 2752 "};"); 2753 verifyGoogleFormat("enum {\n" 2754 " Zero,\n" 2755 " One = 1,\n" 2756 " Two = One + 1,\n" 2757 " Three = (One + Two),\n" 2758 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2759 " Five = (One, Two, Three, Four, 5)\n" 2760 "};"); 2761 verifyFormat("enum Enum {};"); 2762 verifyFormat("enum {};"); 2763 verifyFormat("enum X E {} d;"); 2764 verifyFormat("enum __attribute__((...)) E {} d;"); 2765 verifyFormat("enum __declspec__((...)) E {} d;"); 2766 verifyFormat("enum {\n" 2767 " Bar = Foo<int, int>::value\n" 2768 "};", 2769 getLLVMStyleWithColumns(30)); 2770 2771 verifyFormat("enum ShortEnum { A, B, C };"); 2772 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2773 2774 EXPECT_EQ("enum KeepEmptyLines {\n" 2775 " ONE,\n" 2776 "\n" 2777 " TWO,\n" 2778 "\n" 2779 " THREE\n" 2780 "}", 2781 format("enum KeepEmptyLines {\n" 2782 " ONE,\n" 2783 "\n" 2784 " TWO,\n" 2785 "\n" 2786 "\n" 2787 " THREE\n" 2788 "}")); 2789 verifyFormat("enum E { // comment\n" 2790 " ONE,\n" 2791 " TWO\n" 2792 "};\n" 2793 "int i;"); 2794 // Not enums. 2795 verifyFormat("enum X f() {\n" 2796 " a();\n" 2797 " return 42;\n" 2798 "}"); 2799 verifyFormat("enum X Type::f() {\n" 2800 " a();\n" 2801 " return 42;\n" 2802 "}"); 2803 verifyFormat("enum ::X f() {\n" 2804 " a();\n" 2805 " return 42;\n" 2806 "}"); 2807 verifyFormat("enum ns::X f() {\n" 2808 " a();\n" 2809 " return 42;\n" 2810 "}"); 2811 } 2812 2813 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2814 verifyFormat("enum Type {\n" 2815 " One = 0; // These semicolons should be commas.\n" 2816 " Two = 1;\n" 2817 "};"); 2818 verifyFormat("namespace n {\n" 2819 "enum Type {\n" 2820 " One,\n" 2821 " Two, // missing };\n" 2822 " int i;\n" 2823 "}\n" 2824 "void g() {}"); 2825 } 2826 2827 TEST_F(FormatTest, FormatsEnumStruct) { 2828 verifyFormat("enum struct {\n" 2829 " Zero,\n" 2830 " One = 1,\n" 2831 " Two = One + 1,\n" 2832 " Three = (One + Two),\n" 2833 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2834 " Five = (One, Two, Three, Four, 5)\n" 2835 "};"); 2836 verifyFormat("enum struct Enum {};"); 2837 verifyFormat("enum struct {};"); 2838 verifyFormat("enum struct X E {} d;"); 2839 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2840 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2841 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2842 } 2843 2844 TEST_F(FormatTest, FormatsEnumClass) { 2845 verifyFormat("enum class {\n" 2846 " Zero,\n" 2847 " One = 1,\n" 2848 " Two = One + 1,\n" 2849 " Three = (One + Two),\n" 2850 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2851 " Five = (One, Two, Three, Four, 5)\n" 2852 "};"); 2853 verifyFormat("enum class Enum {};"); 2854 verifyFormat("enum class {};"); 2855 verifyFormat("enum class X E {} d;"); 2856 verifyFormat("enum class __attribute__((...)) E {} d;"); 2857 verifyFormat("enum class __declspec__((...)) E {} d;"); 2858 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2859 } 2860 2861 TEST_F(FormatTest, FormatsEnumTypes) { 2862 verifyFormat("enum X : int {\n" 2863 " A, // Force multiple lines.\n" 2864 " B\n" 2865 "};"); 2866 verifyFormat("enum X : int { A, B };"); 2867 verifyFormat("enum X : std::uint32_t { A, B };"); 2868 } 2869 2870 TEST_F(FormatTest, FormatsNSEnums) { 2871 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2872 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2873 " // Information about someDecentlyLongValue.\n" 2874 " someDecentlyLongValue,\n" 2875 " // Information about anotherDecentlyLongValue.\n" 2876 " anotherDecentlyLongValue,\n" 2877 " // Information about aThirdDecentlyLongValue.\n" 2878 " aThirdDecentlyLongValue\n" 2879 "};"); 2880 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2881 " a = 1,\n" 2882 " b = 2,\n" 2883 " c = 3,\n" 2884 "};"); 2885 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2886 " a = 1,\n" 2887 " b = 2,\n" 2888 " c = 3,\n" 2889 "};"); 2890 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2891 " a = 1,\n" 2892 " b = 2,\n" 2893 " c = 3,\n" 2894 "};"); 2895 } 2896 2897 TEST_F(FormatTest, FormatsBitfields) { 2898 verifyFormat("struct Bitfields {\n" 2899 " unsigned sClass : 8;\n" 2900 " unsigned ValueKind : 2;\n" 2901 "};"); 2902 verifyFormat("struct A {\n" 2903 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2904 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2905 "};"); 2906 verifyFormat("struct MyStruct {\n" 2907 " uchar data;\n" 2908 " uchar : 8;\n" 2909 " uchar : 8;\n" 2910 " uchar other;\n" 2911 "};"); 2912 } 2913 2914 TEST_F(FormatTest, FormatsNamespaces) { 2915 verifyFormat("namespace some_namespace {\n" 2916 "class A {};\n" 2917 "void f() { f(); }\n" 2918 "}"); 2919 verifyFormat("namespace {\n" 2920 "class A {};\n" 2921 "void f() { f(); }\n" 2922 "}"); 2923 verifyFormat("inline namespace X {\n" 2924 "class A {};\n" 2925 "void f() { f(); }\n" 2926 "}"); 2927 verifyFormat("using namespace some_namespace;\n" 2928 "class A {};\n" 2929 "void f() { f(); }"); 2930 2931 // This code is more common than we thought; if we 2932 // layout this correctly the semicolon will go into 2933 // its own line, which is undesirable. 2934 verifyFormat("namespace {};"); 2935 verifyFormat("namespace {\n" 2936 "class A {};\n" 2937 "};"); 2938 2939 verifyFormat("namespace {\n" 2940 "int SomeVariable = 0; // comment\n" 2941 "} // namespace"); 2942 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2943 "#define HEADER_GUARD\n" 2944 "namespace my_namespace {\n" 2945 "int i;\n" 2946 "} // my_namespace\n" 2947 "#endif // HEADER_GUARD", 2948 format("#ifndef HEADER_GUARD\n" 2949 " #define HEADER_GUARD\n" 2950 " namespace my_namespace {\n" 2951 "int i;\n" 2952 "} // my_namespace\n" 2953 "#endif // HEADER_GUARD")); 2954 2955 EXPECT_EQ("namespace A::B {\n" 2956 "class C {};\n" 2957 "}", 2958 format("namespace A::B {\n" 2959 "class C {};\n" 2960 "}")); 2961 2962 FormatStyle Style = getLLVMStyle(); 2963 Style.NamespaceIndentation = FormatStyle::NI_All; 2964 EXPECT_EQ("namespace out {\n" 2965 " int i;\n" 2966 " namespace in {\n" 2967 " int i;\n" 2968 " } // namespace\n" 2969 "} // namespace", 2970 format("namespace out {\n" 2971 "int i;\n" 2972 "namespace in {\n" 2973 "int i;\n" 2974 "} // namespace\n" 2975 "} // namespace", 2976 Style)); 2977 2978 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2979 EXPECT_EQ("namespace out {\n" 2980 "int i;\n" 2981 "namespace in {\n" 2982 " int i;\n" 2983 "} // namespace\n" 2984 "} // namespace", 2985 format("namespace out {\n" 2986 "int i;\n" 2987 "namespace in {\n" 2988 "int i;\n" 2989 "} // namespace\n" 2990 "} // namespace", 2991 Style)); 2992 } 2993 2994 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2995 2996 TEST_F(FormatTest, FormatsInlineASM) { 2997 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2998 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2999 verifyFormat( 3000 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3001 " \"cpuid\\n\\t\"\n" 3002 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3003 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3004 " : \"a\"(value));"); 3005 EXPECT_EQ( 3006 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3007 " __asm {\n" 3008 " mov edx,[that] // vtable in edx\n" 3009 " mov eax,methodIndex\n" 3010 " call [edx][eax*4] // stdcall\n" 3011 " }\n" 3012 "}", 3013 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3014 " __asm {\n" 3015 " mov edx,[that] // vtable in edx\n" 3016 " mov eax,methodIndex\n" 3017 " call [edx][eax*4] // stdcall\n" 3018 " }\n" 3019 "}")); 3020 EXPECT_EQ("_asm {\n" 3021 " xor eax, eax;\n" 3022 " cpuid;\n" 3023 "}", 3024 format("_asm {\n" 3025 " xor eax, eax;\n" 3026 " cpuid;\n" 3027 "}")); 3028 verifyFormat("void function() {\n" 3029 " // comment\n" 3030 " asm(\"\");\n" 3031 "}"); 3032 EXPECT_EQ("__asm {\n" 3033 "}\n" 3034 "int i;", 3035 format("__asm {\n" 3036 "}\n" 3037 "int i;")); 3038 } 3039 3040 TEST_F(FormatTest, FormatTryCatch) { 3041 verifyFormat("try {\n" 3042 " throw a * b;\n" 3043 "} catch (int a) {\n" 3044 " // Do nothing.\n" 3045 "} catch (...) {\n" 3046 " exit(42);\n" 3047 "}"); 3048 3049 // Function-level try statements. 3050 verifyFormat("int f() try { return 4; } catch (...) {\n" 3051 " return 5;\n" 3052 "}"); 3053 verifyFormat("class A {\n" 3054 " int a;\n" 3055 " A() try : a(0) {\n" 3056 " } catch (...) {\n" 3057 " throw;\n" 3058 " }\n" 3059 "};\n"); 3060 3061 // Incomplete try-catch blocks. 3062 verifyIncompleteFormat("try {} catch ("); 3063 } 3064 3065 TEST_F(FormatTest, FormatSEHTryCatch) { 3066 verifyFormat("__try {\n" 3067 " int a = b * c;\n" 3068 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3069 " // Do nothing.\n" 3070 "}"); 3071 3072 verifyFormat("__try {\n" 3073 " int a = b * c;\n" 3074 "} __finally {\n" 3075 " // Do nothing.\n" 3076 "}"); 3077 3078 verifyFormat("DEBUG({\n" 3079 " __try {\n" 3080 " } __finally {\n" 3081 " }\n" 3082 "});\n"); 3083 } 3084 3085 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3086 verifyFormat("try {\n" 3087 " f();\n" 3088 "} catch {\n" 3089 " g();\n" 3090 "}"); 3091 verifyFormat("try {\n" 3092 " f();\n" 3093 "} catch (A a) MACRO(x) {\n" 3094 " g();\n" 3095 "} catch (B b) MACRO(x) {\n" 3096 " g();\n" 3097 "}"); 3098 } 3099 3100 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3101 FormatStyle Style = getLLVMStyle(); 3102 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3103 FormatStyle::BS_WebKit}) { 3104 Style.BreakBeforeBraces = BraceStyle; 3105 verifyFormat("try {\n" 3106 " // something\n" 3107 "} catch (...) {\n" 3108 " // something\n" 3109 "}", 3110 Style); 3111 } 3112 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3113 verifyFormat("try {\n" 3114 " // something\n" 3115 "}\n" 3116 "catch (...) {\n" 3117 " // something\n" 3118 "}", 3119 Style); 3120 verifyFormat("__try {\n" 3121 " // something\n" 3122 "}\n" 3123 "__finally {\n" 3124 " // something\n" 3125 "}", 3126 Style); 3127 verifyFormat("@try {\n" 3128 " // something\n" 3129 "}\n" 3130 "@finally {\n" 3131 " // something\n" 3132 "}", 3133 Style); 3134 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3135 verifyFormat("try\n" 3136 "{\n" 3137 " // something\n" 3138 "}\n" 3139 "catch (...)\n" 3140 "{\n" 3141 " // something\n" 3142 "}", 3143 Style); 3144 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 3145 verifyFormat("try\n" 3146 " {\n" 3147 " // something\n" 3148 " }\n" 3149 "catch (...)\n" 3150 " {\n" 3151 " // something\n" 3152 " }", 3153 Style); 3154 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3155 Style.BraceWrapping.BeforeCatch = true; 3156 verifyFormat("try {\n" 3157 " // something\n" 3158 "}\n" 3159 "catch (...) {\n" 3160 " // something\n" 3161 "}", 3162 Style); 3163 } 3164 3165 TEST_F(FormatTest, StaticInitializers) { 3166 verifyFormat("static SomeClass SC = {1, 'a'};"); 3167 3168 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 3169 " 100000000, " 3170 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 3171 3172 // Here, everything other than the "}" would fit on a line. 3173 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 3174 " 10000000000000000000000000};"); 3175 EXPECT_EQ("S s = {a,\n" 3176 "\n" 3177 " b};", 3178 format("S s = {\n" 3179 " a,\n" 3180 "\n" 3181 " b\n" 3182 "};")); 3183 3184 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 3185 // line. However, the formatting looks a bit off and this probably doesn't 3186 // happen often in practice. 3187 verifyFormat("static int Variable[1] = {\n" 3188 " {1000000000000000000000000000000000000}};", 3189 getLLVMStyleWithColumns(40)); 3190 } 3191 3192 TEST_F(FormatTest, DesignatedInitializers) { 3193 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 3194 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 3195 " .bbbbbbbbbb = 2,\n" 3196 " .cccccccccc = 3,\n" 3197 " .dddddddddd = 4,\n" 3198 " .eeeeeeeeee = 5};"); 3199 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3200 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 3201 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 3202 " .ccccccccccccccccccccccccccc = 3,\n" 3203 " .ddddddddddddddddddddddddddd = 4,\n" 3204 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 3205 3206 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 3207 } 3208 3209 TEST_F(FormatTest, NestedStaticInitializers) { 3210 verifyFormat("static A x = {{{}}};\n"); 3211 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 3212 " {init1, init2, init3, init4}}};", 3213 getLLVMStyleWithColumns(50)); 3214 3215 verifyFormat("somes Status::global_reps[3] = {\n" 3216 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3217 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3218 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 3219 getLLVMStyleWithColumns(60)); 3220 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 3221 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3222 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3223 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 3224 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 3225 " {rect.fRight - rect.fLeft, rect.fBottom - " 3226 "rect.fTop}};"); 3227 3228 verifyFormat( 3229 "SomeArrayOfSomeType a = {\n" 3230 " {{1, 2, 3},\n" 3231 " {1, 2, 3},\n" 3232 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 3233 " 333333333333333333333333333333},\n" 3234 " {1, 2, 3},\n" 3235 " {1, 2, 3}}};"); 3236 verifyFormat( 3237 "SomeArrayOfSomeType a = {\n" 3238 " {{1, 2, 3}},\n" 3239 " {{1, 2, 3}},\n" 3240 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 3241 " 333333333333333333333333333333}},\n" 3242 " {{1, 2, 3}},\n" 3243 " {{1, 2, 3}}};"); 3244 3245 verifyFormat("struct {\n" 3246 " unsigned bit;\n" 3247 " const char *const name;\n" 3248 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 3249 " {kOsWin, \"Windows\"},\n" 3250 " {kOsLinux, \"Linux\"},\n" 3251 " {kOsCrOS, \"Chrome OS\"}};"); 3252 verifyFormat("struct {\n" 3253 " unsigned bit;\n" 3254 " const char *const name;\n" 3255 "} kBitsToOs[] = {\n" 3256 " {kOsMac, \"Mac\"},\n" 3257 " {kOsWin, \"Windows\"},\n" 3258 " {kOsLinux, \"Linux\"},\n" 3259 " {kOsCrOS, \"Chrome OS\"},\n" 3260 "};"); 3261 } 3262 3263 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 3264 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3265 " \\\n" 3266 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 3267 } 3268 3269 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 3270 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 3271 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 3272 3273 // Do break defaulted and deleted functions. 3274 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3275 " default;", 3276 getLLVMStyleWithColumns(40)); 3277 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3278 " delete;", 3279 getLLVMStyleWithColumns(40)); 3280 } 3281 3282 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 3283 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 3284 getLLVMStyleWithColumns(40)); 3285 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3286 getLLVMStyleWithColumns(40)); 3287 EXPECT_EQ("#define Q \\\n" 3288 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 3289 " \"aaaaaaaa.cpp\"", 3290 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3291 getLLVMStyleWithColumns(40))); 3292 } 3293 3294 TEST_F(FormatTest, UnderstandsLinePPDirective) { 3295 EXPECT_EQ("# 123 \"A string literal\"", 3296 format(" # 123 \"A string literal\"")); 3297 } 3298 3299 TEST_F(FormatTest, LayoutUnknownPPDirective) { 3300 EXPECT_EQ("#;", format("#;")); 3301 verifyFormat("#\n;\n;\n;"); 3302 } 3303 3304 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 3305 EXPECT_EQ("#line 42 \"test\"\n", 3306 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 3307 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 3308 getLLVMStyleWithColumns(12))); 3309 } 3310 3311 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 3312 EXPECT_EQ("#line 42 \"test\"", 3313 format("# \\\n line \\\n 42 \\\n \"test\"")); 3314 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 3315 } 3316 3317 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 3318 verifyFormat("#define A \\x20"); 3319 verifyFormat("#define A \\ x20"); 3320 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 3321 verifyFormat("#define A ''"); 3322 verifyFormat("#define A ''qqq"); 3323 verifyFormat("#define A `qqq"); 3324 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 3325 EXPECT_EQ("const char *c = STRINGIFY(\n" 3326 "\\na : b);", 3327 format("const char * c = STRINGIFY(\n" 3328 "\\na : b);")); 3329 3330 verifyFormat("a\r\\"); 3331 verifyFormat("a\v\\"); 3332 verifyFormat("a\f\\"); 3333 } 3334 3335 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 3336 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 3337 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 3338 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 3339 // FIXME: We never break before the macro name. 3340 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 3341 3342 verifyFormat("#define A A\n#define A A"); 3343 verifyFormat("#define A(X) A\n#define A A"); 3344 3345 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 3346 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 3347 } 3348 3349 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 3350 EXPECT_EQ("// somecomment\n" 3351 "#include \"a.h\"\n" 3352 "#define A( \\\n" 3353 " A, B)\n" 3354 "#include \"b.h\"\n" 3355 "// somecomment\n", 3356 format(" // somecomment\n" 3357 " #include \"a.h\"\n" 3358 "#define A(A,\\\n" 3359 " B)\n" 3360 " #include \"b.h\"\n" 3361 " // somecomment\n", 3362 getLLVMStyleWithColumns(13))); 3363 } 3364 3365 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 3366 3367 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 3368 EXPECT_EQ("#define A \\\n" 3369 " c; \\\n" 3370 " e;\n" 3371 "f;", 3372 format("#define A c; e;\n" 3373 "f;", 3374 getLLVMStyleWithColumns(14))); 3375 } 3376 3377 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 3378 3379 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 3380 EXPECT_EQ("int x,\n" 3381 "#define A\n" 3382 " y;", 3383 format("int x,\n#define A\ny;")); 3384 } 3385 3386 TEST_F(FormatTest, HashInMacroDefinition) { 3387 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 3388 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 3389 verifyFormat("#define A \\\n" 3390 " { \\\n" 3391 " f(#c); \\\n" 3392 " }", 3393 getLLVMStyleWithColumns(11)); 3394 3395 verifyFormat("#define A(X) \\\n" 3396 " void function##X()", 3397 getLLVMStyleWithColumns(22)); 3398 3399 verifyFormat("#define A(a, b, c) \\\n" 3400 " void a##b##c()", 3401 getLLVMStyleWithColumns(22)); 3402 3403 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 3404 } 3405 3406 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 3407 EXPECT_EQ("#define A (x)", format("#define A (x)")); 3408 EXPECT_EQ("#define A(x)", format("#define A(x)")); 3409 } 3410 3411 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 3412 EXPECT_EQ("#define A b;", format("#define A \\\n" 3413 " \\\n" 3414 " b;", 3415 getLLVMStyleWithColumns(25))); 3416 EXPECT_EQ("#define A \\\n" 3417 " \\\n" 3418 " a; \\\n" 3419 " b;", 3420 format("#define A \\\n" 3421 " \\\n" 3422 " a; \\\n" 3423 " b;", 3424 getLLVMStyleWithColumns(11))); 3425 EXPECT_EQ("#define A \\\n" 3426 " a; \\\n" 3427 " \\\n" 3428 " b;", 3429 format("#define A \\\n" 3430 " a; \\\n" 3431 " \\\n" 3432 " b;", 3433 getLLVMStyleWithColumns(11))); 3434 } 3435 3436 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 3437 verifyIncompleteFormat("#define A :"); 3438 verifyFormat("#define SOMECASES \\\n" 3439 " case 1: \\\n" 3440 " case 2\n", 3441 getLLVMStyleWithColumns(20)); 3442 verifyFormat("#define MACRO(a) \\\n" 3443 " if (a) \\\n" 3444 " f(); \\\n" 3445 " else \\\n" 3446 " g()", 3447 getLLVMStyleWithColumns(18)); 3448 verifyFormat("#define A template <typename T>"); 3449 verifyIncompleteFormat("#define STR(x) #x\n" 3450 "f(STR(this_is_a_string_literal{));"); 3451 verifyFormat("#pragma omp threadprivate( \\\n" 3452 " y)), // expected-warning", 3453 getLLVMStyleWithColumns(28)); 3454 verifyFormat("#d, = };"); 3455 verifyFormat("#if \"a"); 3456 verifyIncompleteFormat("({\n" 3457 "#define b \\\n" 3458 " } \\\n" 3459 " a\n" 3460 "a", 3461 getLLVMStyleWithColumns(15)); 3462 verifyFormat("#define A \\\n" 3463 " { \\\n" 3464 " {\n" 3465 "#define B \\\n" 3466 " } \\\n" 3467 " }", 3468 getLLVMStyleWithColumns(15)); 3469 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 3470 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 3471 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 3472 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 3473 } 3474 3475 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 3476 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 3477 EXPECT_EQ("class A : public QObject {\n" 3478 " Q_OBJECT\n" 3479 "\n" 3480 " A() {}\n" 3481 "};", 3482 format("class A : public QObject {\n" 3483 " Q_OBJECT\n" 3484 "\n" 3485 " A() {\n}\n" 3486 "} ;")); 3487 EXPECT_EQ("MACRO\n" 3488 "/*static*/ int i;", 3489 format("MACRO\n" 3490 " /*static*/ int i;")); 3491 EXPECT_EQ("SOME_MACRO\n" 3492 "namespace {\n" 3493 "void f();\n" 3494 "}", 3495 format("SOME_MACRO\n" 3496 " namespace {\n" 3497 "void f( );\n" 3498 "}")); 3499 // Only if the identifier contains at least 5 characters. 3500 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 3501 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 3502 // Only if everything is upper case. 3503 EXPECT_EQ("class A : public QObject {\n" 3504 " Q_Object A() {}\n" 3505 "};", 3506 format("class A : public QObject {\n" 3507 " Q_Object\n" 3508 " A() {\n}\n" 3509 "} ;")); 3510 3511 // Only if the next line can actually start an unwrapped line. 3512 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 3513 format("SOME_WEIRD_LOG_MACRO\n" 3514 "<< SomeThing;")); 3515 3516 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 3517 "(n, buffers))\n", 3518 getChromiumStyle(FormatStyle::LK_Cpp)); 3519 } 3520 3521 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 3522 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3523 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3524 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3525 "class X {};\n" 3526 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3527 "int *createScopDetectionPass() { return 0; }", 3528 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3529 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3530 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3531 " class X {};\n" 3532 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3533 " int *createScopDetectionPass() { return 0; }")); 3534 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 3535 // braces, so that inner block is indented one level more. 3536 EXPECT_EQ("int q() {\n" 3537 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3538 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3539 " IPC_END_MESSAGE_MAP()\n" 3540 "}", 3541 format("int q() {\n" 3542 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3543 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3544 " IPC_END_MESSAGE_MAP()\n" 3545 "}")); 3546 3547 // Same inside macros. 3548 EXPECT_EQ("#define LIST(L) \\\n" 3549 " L(A) \\\n" 3550 " L(B) \\\n" 3551 " L(C)", 3552 format("#define LIST(L) \\\n" 3553 " L(A) \\\n" 3554 " L(B) \\\n" 3555 " L(C)", 3556 getGoogleStyle())); 3557 3558 // These must not be recognized as macros. 3559 EXPECT_EQ("int q() {\n" 3560 " f(x);\n" 3561 " f(x) {}\n" 3562 " f(x)->g();\n" 3563 " f(x)->*g();\n" 3564 " f(x).g();\n" 3565 " f(x) = x;\n" 3566 " f(x) += x;\n" 3567 " f(x) -= x;\n" 3568 " f(x) *= x;\n" 3569 " f(x) /= x;\n" 3570 " f(x) %= x;\n" 3571 " f(x) &= x;\n" 3572 " f(x) |= x;\n" 3573 " f(x) ^= x;\n" 3574 " f(x) >>= x;\n" 3575 " f(x) <<= x;\n" 3576 " f(x)[y].z();\n" 3577 " LOG(INFO) << x;\n" 3578 " ifstream(x) >> x;\n" 3579 "}\n", 3580 format("int q() {\n" 3581 " f(x)\n;\n" 3582 " f(x)\n {}\n" 3583 " f(x)\n->g();\n" 3584 " f(x)\n->*g();\n" 3585 " f(x)\n.g();\n" 3586 " f(x)\n = x;\n" 3587 " f(x)\n += x;\n" 3588 " f(x)\n -= x;\n" 3589 " f(x)\n *= x;\n" 3590 " f(x)\n /= x;\n" 3591 " f(x)\n %= x;\n" 3592 " f(x)\n &= x;\n" 3593 " f(x)\n |= x;\n" 3594 " f(x)\n ^= x;\n" 3595 " f(x)\n >>= x;\n" 3596 " f(x)\n <<= x;\n" 3597 " f(x)\n[y].z();\n" 3598 " LOG(INFO)\n << x;\n" 3599 " ifstream(x)\n >> x;\n" 3600 "}\n")); 3601 EXPECT_EQ("int q() {\n" 3602 " F(x)\n" 3603 " if (1) {\n" 3604 " }\n" 3605 " F(x)\n" 3606 " while (1) {\n" 3607 " }\n" 3608 " F(x)\n" 3609 " G(x);\n" 3610 " F(x)\n" 3611 " try {\n" 3612 " Q();\n" 3613 " } catch (...) {\n" 3614 " }\n" 3615 "}\n", 3616 format("int q() {\n" 3617 "F(x)\n" 3618 "if (1) {}\n" 3619 "F(x)\n" 3620 "while (1) {}\n" 3621 "F(x)\n" 3622 "G(x);\n" 3623 "F(x)\n" 3624 "try { Q(); } catch (...) {}\n" 3625 "}\n")); 3626 EXPECT_EQ("class A {\n" 3627 " A() : t(0) {}\n" 3628 " A(int i) noexcept() : {}\n" 3629 " A(X x)\n" // FIXME: function-level try blocks are broken. 3630 " try : t(0) {\n" 3631 " } catch (...) {\n" 3632 " }\n" 3633 "};", 3634 format("class A {\n" 3635 " A()\n : t(0) {}\n" 3636 " A(int i)\n noexcept() : {}\n" 3637 " A(X x)\n" 3638 " try : t(0) {} catch (...) {}\n" 3639 "};")); 3640 EXPECT_EQ("class SomeClass {\n" 3641 "public:\n" 3642 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3643 "};", 3644 format("class SomeClass {\n" 3645 "public:\n" 3646 " SomeClass()\n" 3647 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3648 "};")); 3649 EXPECT_EQ("class SomeClass {\n" 3650 "public:\n" 3651 " SomeClass()\n" 3652 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3653 "};", 3654 format("class SomeClass {\n" 3655 "public:\n" 3656 " SomeClass()\n" 3657 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3658 "};", 3659 getLLVMStyleWithColumns(40))); 3660 3661 verifyFormat("MACRO(>)"); 3662 } 3663 3664 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3665 verifyFormat("#define A \\\n" 3666 " f({ \\\n" 3667 " g(); \\\n" 3668 " });", 3669 getLLVMStyleWithColumns(11)); 3670 } 3671 3672 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 3673 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 3674 } 3675 3676 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3677 verifyFormat("{\n { a #c; }\n}"); 3678 } 3679 3680 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3681 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3682 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3683 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3684 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3685 } 3686 3687 TEST_F(FormatTest, EscapedNewlines) { 3688 EXPECT_EQ( 3689 "#define A \\\n int i; \\\n int j;", 3690 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3691 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3692 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3693 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3694 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3695 } 3696 3697 TEST_F(FormatTest, DontCrashOnBlockComments) { 3698 EXPECT_EQ( 3699 "int xxxxxxxxx; /* " 3700 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3701 "zzzzzz\n" 3702 "0*/", 3703 format("int xxxxxxxxx; /* " 3704 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3705 "0*/")); 3706 } 3707 3708 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3709 verifyFormat("#define A \\\n" 3710 " int v( \\\n" 3711 " a); \\\n" 3712 " int i;", 3713 getLLVMStyleWithColumns(11)); 3714 } 3715 3716 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3717 EXPECT_EQ( 3718 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3719 " \\\n" 3720 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3721 "\n" 3722 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3723 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3724 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3725 "\\\n" 3726 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3727 " \n" 3728 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3729 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3730 } 3731 3732 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3733 EXPECT_EQ("int\n" 3734 "#define A\n" 3735 " a;", 3736 format("int\n#define A\na;")); 3737 verifyFormat("functionCallTo(\n" 3738 " someOtherFunction(\n" 3739 " withSomeParameters, whichInSequence,\n" 3740 " areLongerThanALine(andAnotherCall,\n" 3741 "#define A B\n" 3742 " withMoreParamters,\n" 3743 " whichStronglyInfluenceTheLayout),\n" 3744 " andMoreParameters),\n" 3745 " trailing);", 3746 getLLVMStyleWithColumns(69)); 3747 verifyFormat("Foo::Foo()\n" 3748 "#ifdef BAR\n" 3749 " : baz(0)\n" 3750 "#endif\n" 3751 "{\n" 3752 "}"); 3753 verifyFormat("void f() {\n" 3754 " if (true)\n" 3755 "#ifdef A\n" 3756 " f(42);\n" 3757 " x();\n" 3758 "#else\n" 3759 " g();\n" 3760 " x();\n" 3761 "#endif\n" 3762 "}"); 3763 verifyFormat("void f(param1, param2,\n" 3764 " param3,\n" 3765 "#ifdef A\n" 3766 " param4(param5,\n" 3767 "#ifdef A1\n" 3768 " param6,\n" 3769 "#ifdef A2\n" 3770 " param7),\n" 3771 "#else\n" 3772 " param8),\n" 3773 " param9,\n" 3774 "#endif\n" 3775 " param10,\n" 3776 "#endif\n" 3777 " param11)\n" 3778 "#else\n" 3779 " param12)\n" 3780 "#endif\n" 3781 "{\n" 3782 " x();\n" 3783 "}", 3784 getLLVMStyleWithColumns(28)); 3785 verifyFormat("#if 1\n" 3786 "int i;"); 3787 verifyFormat("#if 1\n" 3788 "#endif\n" 3789 "#if 1\n" 3790 "#else\n" 3791 "#endif\n"); 3792 verifyFormat("DEBUG({\n" 3793 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3795 "});\n" 3796 "#if a\n" 3797 "#else\n" 3798 "#endif"); 3799 3800 verifyIncompleteFormat("void f(\n" 3801 "#if A\n" 3802 " );\n" 3803 "#else\n" 3804 "#endif"); 3805 } 3806 3807 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3808 verifyFormat("#endif\n" 3809 "#if B"); 3810 } 3811 3812 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3813 FormatStyle SingleLine = getLLVMStyle(); 3814 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3815 verifyFormat("#if 0\n" 3816 "#elif 1\n" 3817 "#endif\n" 3818 "void foo() {\n" 3819 " if (test) foo2();\n" 3820 "}", 3821 SingleLine); 3822 } 3823 3824 TEST_F(FormatTest, LayoutBlockInsideParens) { 3825 verifyFormat("functionCall({ int i; });"); 3826 verifyFormat("functionCall({\n" 3827 " int i;\n" 3828 " int j;\n" 3829 "});"); 3830 verifyFormat("functionCall(\n" 3831 " {\n" 3832 " int i;\n" 3833 " int j;\n" 3834 " },\n" 3835 " aaaa, bbbb, cccc);"); 3836 verifyFormat("functionA(functionB({\n" 3837 " int i;\n" 3838 " int j;\n" 3839 " }),\n" 3840 " aaaa, bbbb, cccc);"); 3841 verifyFormat("functionCall(\n" 3842 " {\n" 3843 " int i;\n" 3844 " int j;\n" 3845 " },\n" 3846 " aaaa, bbbb, // comment\n" 3847 " cccc);"); 3848 verifyFormat("functionA(functionB({\n" 3849 " int i;\n" 3850 " int j;\n" 3851 " }),\n" 3852 " aaaa, bbbb, // comment\n" 3853 " cccc);"); 3854 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3855 verifyFormat("functionCall(aaaa, bbbb, {\n" 3856 " int i;\n" 3857 " int j;\n" 3858 "});"); 3859 verifyFormat( 3860 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3861 " {\n" 3862 " int i; // break\n" 3863 " },\n" 3864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3865 " ccccccccccccccccc));"); 3866 verifyFormat("DEBUG({\n" 3867 " if (a)\n" 3868 " f();\n" 3869 "});"); 3870 } 3871 3872 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3873 EXPECT_EQ("SOME_MACRO { int i; }\n" 3874 "int i;", 3875 format(" SOME_MACRO {int i;} int i;")); 3876 } 3877 3878 TEST_F(FormatTest, LayoutNestedBlocks) { 3879 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3880 " struct s {\n" 3881 " int i;\n" 3882 " };\n" 3883 " s kBitsToOs[] = {{10}};\n" 3884 " for (int i = 0; i < 10; ++i)\n" 3885 " return;\n" 3886 "}"); 3887 verifyFormat("call(parameter, {\n" 3888 " something();\n" 3889 " // Comment using all columns.\n" 3890 " somethingelse();\n" 3891 "});", 3892 getLLVMStyleWithColumns(40)); 3893 verifyFormat("DEBUG( //\n" 3894 " { f(); }, a);"); 3895 verifyFormat("DEBUG( //\n" 3896 " {\n" 3897 " f(); //\n" 3898 " },\n" 3899 " a);"); 3900 3901 EXPECT_EQ("call(parameter, {\n" 3902 " something();\n" 3903 " // Comment too\n" 3904 " // looooooooooong.\n" 3905 " somethingElse();\n" 3906 "});", 3907 format("call(parameter, {\n" 3908 " something();\n" 3909 " // Comment too looooooooooong.\n" 3910 " somethingElse();\n" 3911 "});", 3912 getLLVMStyleWithColumns(29))); 3913 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3914 EXPECT_EQ("DEBUG({ // comment\n" 3915 " int i;\n" 3916 "});", 3917 format("DEBUG({ // comment\n" 3918 "int i;\n" 3919 "});")); 3920 EXPECT_EQ("DEBUG({\n" 3921 " int i;\n" 3922 "\n" 3923 " // comment\n" 3924 " int j;\n" 3925 "});", 3926 format("DEBUG({\n" 3927 " int i;\n" 3928 "\n" 3929 " // comment\n" 3930 " int j;\n" 3931 "});")); 3932 3933 verifyFormat("DEBUG({\n" 3934 " if (a)\n" 3935 " return;\n" 3936 "});"); 3937 verifyGoogleFormat("DEBUG({\n" 3938 " if (a) return;\n" 3939 "});"); 3940 FormatStyle Style = getGoogleStyle(); 3941 Style.ColumnLimit = 45; 3942 verifyFormat("Debug(aaaaa,\n" 3943 " {\n" 3944 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3945 " },\n" 3946 " a);", 3947 Style); 3948 3949 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3950 3951 verifyNoCrash("^{v^{a}}"); 3952 } 3953 3954 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3955 EXPECT_EQ("#define MACRO() \\\n" 3956 " Debug(aaa, /* force line break */ \\\n" 3957 " { \\\n" 3958 " int i; \\\n" 3959 " int j; \\\n" 3960 " })", 3961 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3962 " { int i; int j; })", 3963 getGoogleStyle())); 3964 3965 EXPECT_EQ("#define A \\\n" 3966 " [] { \\\n" 3967 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3968 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3969 " }", 3970 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3971 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3972 getGoogleStyle())); 3973 } 3974 3975 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3976 EXPECT_EQ("{}", format("{}")); 3977 verifyFormat("enum E {};"); 3978 verifyFormat("enum E {}"); 3979 } 3980 3981 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3982 FormatStyle Style = getLLVMStyle(); 3983 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3984 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3985 verifyFormat("FOO_BEGIN\n" 3986 " FOO_ENTRY\n" 3987 "FOO_END", Style); 3988 verifyFormat("FOO_BEGIN\n" 3989 " NESTED_FOO_BEGIN\n" 3990 " NESTED_FOO_ENTRY\n" 3991 " NESTED_FOO_END\n" 3992 "FOO_END", Style); 3993 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3994 " int x;\n" 3995 " x = 1;\n" 3996 "FOO_END(Baz)", Style); 3997 } 3998 3999 //===----------------------------------------------------------------------===// 4000 // Line break tests. 4001 //===----------------------------------------------------------------------===// 4002 4003 TEST_F(FormatTest, PreventConfusingIndents) { 4004 verifyFormat( 4005 "void f() {\n" 4006 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 4007 " parameter, parameter, parameter)),\n" 4008 " SecondLongCall(parameter));\n" 4009 "}"); 4010 verifyFormat( 4011 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4012 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4014 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 4015 verifyFormat( 4016 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4017 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 4018 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4019 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 4020 verifyFormat( 4021 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4022 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 4023 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 4024 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 4025 verifyFormat("int a = bbbb && ccc &&\n" 4026 " fffff(\n" 4027 "#define A Just forcing a new line\n" 4028 " ddd);"); 4029 } 4030 4031 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 4032 verifyFormat( 4033 "bool aaaaaaa =\n" 4034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 4035 " bbbbbbbb();"); 4036 verifyFormat( 4037 "bool aaaaaaa =\n" 4038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 4039 " bbbbbbbb();"); 4040 4041 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 4043 " ccccccccc == ddddddddddd;"); 4044 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 4046 " ccccccccc == ddddddddddd;"); 4047 verifyFormat( 4048 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 4050 " ccccccccc == ddddddddddd;"); 4051 4052 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4053 " aaaaaa) &&\n" 4054 " bbbbbb && cccccc;"); 4055 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4056 " aaaaaa) >>\n" 4057 " bbbbbb;"); 4058 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 4059 " SourceMgr.getSpellingColumnNumber(\n" 4060 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 4061 " 1);"); 4062 4063 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4064 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 4065 " cccccc) {\n}"); 4066 verifyFormat("b = a &&\n" 4067 " // Comment\n" 4068 " b.c && d;"); 4069 4070 // If the LHS of a comparison is not a binary expression itself, the 4071 // additional linebreak confuses many people. 4072 verifyFormat( 4073 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 4075 "}"); 4076 verifyFormat( 4077 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4079 "}"); 4080 verifyFormat( 4081 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 4082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4083 "}"); 4084 // Even explicit parentheses stress the precedence enough to make the 4085 // additional break unnecessary. 4086 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4088 "}"); 4089 // This cases is borderline, but with the indentation it is still readable. 4090 verifyFormat( 4091 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4092 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4094 "}", 4095 getLLVMStyleWithColumns(75)); 4096 4097 // If the LHS is a binary expression, we should still use the additional break 4098 // as otherwise the formatting hides the operator precedence. 4099 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4101 " 5) {\n" 4102 "}"); 4103 4104 FormatStyle OnePerLine = getLLVMStyle(); 4105 OnePerLine.BinPackParameters = false; 4106 verifyFormat( 4107 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 4110 OnePerLine); 4111 4112 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 4113 " .aaa(aaaaaaaaaaaaa) *\n" 4114 " aaaaaaa +\n" 4115 " aaaaaaa;", 4116 getLLVMStyleWithColumns(40)); 4117 } 4118 4119 TEST_F(FormatTest, ExpressionIndentation) { 4120 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4124 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4125 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 4126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 4128 " ccccccccccccccccccccccccccccccccccccccccc;"); 4129 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4132 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4133 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4136 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4137 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4140 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4141 verifyFormat("if () {\n" 4142 "} else if (aaaaa && bbbbb > // break\n" 4143 " ccccc) {\n" 4144 "}"); 4145 verifyFormat("if () {\n" 4146 "} else if (aaaaa &&\n" 4147 " bbbbb > // break\n" 4148 " ccccc &&\n" 4149 " ddddd) {\n" 4150 "}"); 4151 4152 // Presence of a trailing comment used to change indentation of b. 4153 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 4154 " b;\n" 4155 "return aaaaaaaaaaaaaaaaaaa +\n" 4156 " b; //", 4157 getLLVMStyleWithColumns(30)); 4158 } 4159 4160 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 4161 // Not sure what the best system is here. Like this, the LHS can be found 4162 // immediately above an operator (everything with the same or a higher 4163 // indent). The RHS is aligned right of the operator and so compasses 4164 // everything until something with the same indent as the operator is found. 4165 // FIXME: Is this a good system? 4166 FormatStyle Style = getLLVMStyle(); 4167 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4168 verifyFormat( 4169 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4170 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4171 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4172 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4173 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4174 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4175 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4176 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4177 " > ccccccccccccccccccccccccccccccccccccccccc;", 4178 Style); 4179 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4180 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4181 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4182 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4183 Style); 4184 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4185 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4186 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4187 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4188 Style); 4189 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4190 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4191 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4192 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4193 Style); 4194 verifyFormat("if () {\n" 4195 "} else if (aaaaa\n" 4196 " && bbbbb // break\n" 4197 " > ccccc) {\n" 4198 "}", 4199 Style); 4200 verifyFormat("return (a)\n" 4201 " // comment\n" 4202 " + b;", 4203 Style); 4204 verifyFormat( 4205 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4206 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4207 " + cc;", 4208 Style); 4209 4210 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4211 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4212 Style); 4213 4214 // Forced by comments. 4215 verifyFormat( 4216 "unsigned ContentSize =\n" 4217 " sizeof(int16_t) // DWARF ARange version number\n" 4218 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4219 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4220 " + sizeof(int8_t); // Segment Size (in bytes)"); 4221 4222 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4223 " == boost::fusion::at_c<1>(iiii).second;", 4224 Style); 4225 4226 Style.ColumnLimit = 60; 4227 verifyFormat("zzzzzzzzzz\n" 4228 " = bbbbbbbbbbbbbbbbb\n" 4229 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4230 Style); 4231 } 4232 4233 TEST_F(FormatTest, EnforcedOperatorWraps) { 4234 // Here we'd like to wrap after the || operators, but a comment is forcing an 4235 // earlier wrap. 4236 verifyFormat("bool x = aaaaa //\n" 4237 " || bbbbb\n" 4238 " //\n" 4239 " || cccc;"); 4240 } 4241 4242 TEST_F(FormatTest, NoOperandAlignment) { 4243 FormatStyle Style = getLLVMStyle(); 4244 Style.AlignOperands = false; 4245 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 4246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4247 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4248 Style); 4249 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4250 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4251 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4252 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4253 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4254 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4255 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4256 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4257 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4258 " > ccccccccccccccccccccccccccccccccccccccccc;", 4259 Style); 4260 4261 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4262 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4263 " + cc;", 4264 Style); 4265 verifyFormat("int a = aa\n" 4266 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4267 " * cccccccccccccccccccccccccccccccccccc;\n", 4268 Style); 4269 4270 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4271 verifyFormat("return (a > b\n" 4272 " // comment1\n" 4273 " // comment2\n" 4274 " || c);", 4275 Style); 4276 } 4277 4278 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 4279 FormatStyle Style = getLLVMStyle(); 4280 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4281 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4282 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4283 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4284 Style); 4285 } 4286 4287 TEST_F(FormatTest, ConstructorInitializers) { 4288 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 4289 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 4290 getLLVMStyleWithColumns(45)); 4291 verifyFormat("Constructor()\n" 4292 " : Inttializer(FitsOnTheLine) {}", 4293 getLLVMStyleWithColumns(44)); 4294 verifyFormat("Constructor()\n" 4295 " : Inttializer(FitsOnTheLine) {}", 4296 getLLVMStyleWithColumns(43)); 4297 4298 verifyFormat("template <typename T>\n" 4299 "Constructor() : Initializer(FitsOnTheLine) {}", 4300 getLLVMStyleWithColumns(45)); 4301 4302 verifyFormat( 4303 "SomeClass::Constructor()\n" 4304 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4305 4306 verifyFormat( 4307 "SomeClass::Constructor()\n" 4308 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4309 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 4310 verifyFormat( 4311 "SomeClass::Constructor()\n" 4312 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4313 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4314 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4315 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4316 " : aaaaaaaaaa(aaaaaa) {}"); 4317 4318 verifyFormat("Constructor()\n" 4319 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4320 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4321 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4322 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 4323 4324 verifyFormat("Constructor()\n" 4325 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4327 4328 verifyFormat("Constructor(int Parameter = 0)\n" 4329 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4330 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 4331 verifyFormat("Constructor()\n" 4332 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4333 "}", 4334 getLLVMStyleWithColumns(60)); 4335 verifyFormat("Constructor()\n" 4336 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4337 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 4338 4339 // Here a line could be saved by splitting the second initializer onto two 4340 // lines, but that is not desirable. 4341 verifyFormat("Constructor()\n" 4342 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4343 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4344 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4345 4346 FormatStyle OnePerLine = getLLVMStyle(); 4347 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4348 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 4349 verifyFormat("SomeClass::Constructor()\n" 4350 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4351 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4352 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4353 OnePerLine); 4354 verifyFormat("SomeClass::Constructor()\n" 4355 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4356 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4357 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4358 OnePerLine); 4359 verifyFormat("MyClass::MyClass(int var)\n" 4360 " : some_var_(var), // 4 space indent\n" 4361 " some_other_var_(var + 1) { // lined up\n" 4362 "}", 4363 OnePerLine); 4364 verifyFormat("Constructor()\n" 4365 " : aaaaa(aaaaaa),\n" 4366 " aaaaa(aaaaaa),\n" 4367 " aaaaa(aaaaaa),\n" 4368 " aaaaa(aaaaaa),\n" 4369 " aaaaa(aaaaaa) {}", 4370 OnePerLine); 4371 verifyFormat("Constructor()\n" 4372 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4373 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4374 OnePerLine); 4375 OnePerLine.BinPackParameters = false; 4376 verifyFormat( 4377 "Constructor()\n" 4378 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4379 " aaaaaaaaaaa().aaa(),\n" 4380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4381 OnePerLine); 4382 OnePerLine.ColumnLimit = 60; 4383 verifyFormat("Constructor()\n" 4384 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4385 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4386 OnePerLine); 4387 4388 EXPECT_EQ("Constructor()\n" 4389 " : // Comment forcing unwanted break.\n" 4390 " aaaa(aaaa) {}", 4391 format("Constructor() :\n" 4392 " // Comment forcing unwanted break.\n" 4393 " aaaa(aaaa) {}")); 4394 } 4395 4396 TEST_F(FormatTest, MemoizationTests) { 4397 // This breaks if the memoization lookup does not take \c Indent and 4398 // \c LastSpace into account. 4399 verifyFormat( 4400 "extern CFRunLoopTimerRef\n" 4401 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 4402 " CFTimeInterval interval, CFOptionFlags flags,\n" 4403 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 4404 " CFRunLoopTimerContext *context) {}"); 4405 4406 // Deep nesting somewhat works around our memoization. 4407 verifyFormat( 4408 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4409 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4410 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4411 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4412 " aaaaa())))))))))))))))))))))))))))))))))))))));", 4413 getLLVMStyleWithColumns(65)); 4414 verifyFormat( 4415 "aaaaa(\n" 4416 " aaaaa,\n" 4417 " aaaaa(\n" 4418 " aaaaa,\n" 4419 " aaaaa(\n" 4420 " aaaaa,\n" 4421 " aaaaa(\n" 4422 " aaaaa,\n" 4423 " aaaaa(\n" 4424 " aaaaa,\n" 4425 " aaaaa(\n" 4426 " aaaaa,\n" 4427 " aaaaa(\n" 4428 " aaaaa,\n" 4429 " aaaaa(\n" 4430 " aaaaa,\n" 4431 " aaaaa(\n" 4432 " aaaaa,\n" 4433 " aaaaa(\n" 4434 " aaaaa,\n" 4435 " aaaaa(\n" 4436 " aaaaa,\n" 4437 " aaaaa(\n" 4438 " aaaaa,\n" 4439 " aaaaa))))))))))));", 4440 getLLVMStyleWithColumns(65)); 4441 verifyFormat( 4442 "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" 4443 " a),\n" 4444 " a),\n" 4445 " a),\n" 4446 " a),\n" 4447 " a),\n" 4448 " a),\n" 4449 " a),\n" 4450 " a),\n" 4451 " a),\n" 4452 " a),\n" 4453 " a),\n" 4454 " a),\n" 4455 " a),\n" 4456 " a),\n" 4457 " a),\n" 4458 " a),\n" 4459 " a)", 4460 getLLVMStyleWithColumns(65)); 4461 4462 // This test takes VERY long when memoization is broken. 4463 FormatStyle OnePerLine = getLLVMStyle(); 4464 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4465 OnePerLine.BinPackParameters = false; 4466 std::string input = "Constructor()\n" 4467 " : aaaa(a,\n"; 4468 for (unsigned i = 0, e = 80; i != e; ++i) { 4469 input += " a,\n"; 4470 } 4471 input += " a) {}"; 4472 verifyFormat(input, OnePerLine); 4473 } 4474 4475 TEST_F(FormatTest, BreaksAsHighAsPossible) { 4476 verifyFormat( 4477 "void f() {\n" 4478 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 4479 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 4480 " f();\n" 4481 "}"); 4482 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 4483 " Intervals[i - 1].getRange().getLast()) {\n}"); 4484 } 4485 4486 TEST_F(FormatTest, BreaksFunctionDeclarations) { 4487 // Principially, we break function declarations in a certain order: 4488 // 1) break amongst arguments. 4489 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 4490 " Cccccccccccccc cccccccccccccc);"); 4491 verifyFormat("template <class TemplateIt>\n" 4492 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 4493 " TemplateIt *stop) {}"); 4494 4495 // 2) break after return type. 4496 verifyFormat( 4497 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4498 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 4499 getGoogleStyle()); 4500 4501 // 3) break after (. 4502 verifyFormat( 4503 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 4504 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 4505 getGoogleStyle()); 4506 4507 // 4) break before after nested name specifiers. 4508 verifyFormat( 4509 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4510 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 4511 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 4512 getGoogleStyle()); 4513 4514 // However, there are exceptions, if a sufficient amount of lines can be 4515 // saved. 4516 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 4517 // more adjusting. 4518 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4519 " Cccccccccccccc cccccccccc,\n" 4520 " Cccccccccccccc cccccccccc,\n" 4521 " Cccccccccccccc cccccccccc,\n" 4522 " Cccccccccccccc cccccccccc);"); 4523 verifyFormat( 4524 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4525 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4526 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4527 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 4528 getGoogleStyle()); 4529 verifyFormat( 4530 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4531 " Cccccccccccccc cccccccccc,\n" 4532 " Cccccccccccccc cccccccccc,\n" 4533 " Cccccccccccccc cccccccccc,\n" 4534 " Cccccccccccccc cccccccccc,\n" 4535 " Cccccccccccccc cccccccccc,\n" 4536 " Cccccccccccccc cccccccccc);"); 4537 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4538 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4539 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4540 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4541 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 4542 4543 // Break after multi-line parameters. 4544 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4547 " bbbb bbbb);"); 4548 verifyFormat("void SomeLoooooooooooongFunction(\n" 4549 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4550 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4551 " int bbbbbbbbbbbbb);"); 4552 4553 // Treat overloaded operators like other functions. 4554 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4555 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 4556 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4557 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 4558 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4559 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 4560 verifyGoogleFormat( 4561 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 4562 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4563 verifyGoogleFormat( 4564 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 4565 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4566 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4567 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4568 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 4569 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4570 verifyGoogleFormat( 4571 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 4572 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4573 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 4574 verifyGoogleFormat( 4575 "template <typename T>\n" 4576 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4577 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 4578 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 4579 4580 FormatStyle Style = getLLVMStyle(); 4581 Style.PointerAlignment = FormatStyle::PAS_Left; 4582 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4583 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 4584 Style); 4585 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 4586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4587 Style); 4588 } 4589 4590 TEST_F(FormatTest, TrailingReturnType) { 4591 verifyFormat("auto foo() -> int;\n"); 4592 verifyFormat("struct S {\n" 4593 " auto bar() const -> int;\n" 4594 "};"); 4595 verifyFormat("template <size_t Order, typename T>\n" 4596 "auto load_img(const std::string &filename)\n" 4597 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4598 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4599 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4600 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4601 verifyFormat("template <typename T>\n" 4602 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4603 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4604 4605 // Not trailing return types. 4606 verifyFormat("void f() { auto a = b->c(); }"); 4607 } 4608 4609 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4610 // Avoid breaking before trailing 'const' or other trailing annotations, if 4611 // they are not function-like. 4612 FormatStyle Style = getGoogleStyle(); 4613 Style.ColumnLimit = 47; 4614 verifyFormat("void someLongFunction(\n" 4615 " int someLoooooooooooooongParameter) const {\n}", 4616 getLLVMStyleWithColumns(47)); 4617 verifyFormat("LoooooongReturnType\n" 4618 "someLoooooooongFunction() const {}", 4619 getLLVMStyleWithColumns(47)); 4620 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4621 " const {}", 4622 Style); 4623 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4624 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4625 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4626 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4627 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4628 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4629 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4630 " aaaaaaaaaaa aaaaa) const override;"); 4631 verifyGoogleFormat( 4632 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4633 " const override;"); 4634 4635 // Even if the first parameter has to be wrapped. 4636 verifyFormat("void someLongFunction(\n" 4637 " int someLongParameter) const {}", 4638 getLLVMStyleWithColumns(46)); 4639 verifyFormat("void someLongFunction(\n" 4640 " int someLongParameter) const {}", 4641 Style); 4642 verifyFormat("void someLongFunction(\n" 4643 " int someLongParameter) override {}", 4644 Style); 4645 verifyFormat("void someLongFunction(\n" 4646 " int someLongParameter) OVERRIDE {}", 4647 Style); 4648 verifyFormat("void someLongFunction(\n" 4649 " int someLongParameter) final {}", 4650 Style); 4651 verifyFormat("void someLongFunction(\n" 4652 " int someLongParameter) FINAL {}", 4653 Style); 4654 verifyFormat("void someLongFunction(\n" 4655 " int parameter) const override {}", 4656 Style); 4657 4658 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4659 verifyFormat("void someLongFunction(\n" 4660 " int someLongParameter) const\n" 4661 "{\n" 4662 "}", 4663 Style); 4664 4665 // Unless these are unknown annotations. 4666 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4667 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4668 " LONG_AND_UGLY_ANNOTATION;"); 4669 4670 // Breaking before function-like trailing annotations is fine to keep them 4671 // close to their arguments. 4672 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4673 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4674 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4675 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4676 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4677 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4678 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4679 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4680 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4681 4682 verifyFormat( 4683 "void aaaaaaaaaaaaaaaaaa()\n" 4684 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4685 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4686 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4687 " __attribute__((unused));"); 4688 verifyGoogleFormat( 4689 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4690 " GUARDED_BY(aaaaaaaaaaaa);"); 4691 verifyGoogleFormat( 4692 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4693 " GUARDED_BY(aaaaaaaaaaaa);"); 4694 verifyGoogleFormat( 4695 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4696 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4697 verifyGoogleFormat( 4698 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4699 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4700 } 4701 4702 TEST_F(FormatTest, FunctionAnnotations) { 4703 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4704 "int OldFunction(const string ¶meter) {}"); 4705 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4706 "string OldFunction(const string ¶meter) {}"); 4707 verifyFormat("template <typename T>\n" 4708 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4709 "string OldFunction(const string ¶meter) {}"); 4710 4711 // Not function annotations. 4712 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4713 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4714 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4715 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4716 verifyFormat("MACRO(abc).function() // wrap\n" 4717 " << abc;"); 4718 verifyFormat("MACRO(abc)->function() // wrap\n" 4719 " << abc;"); 4720 verifyFormat("MACRO(abc)::function() // wrap\n" 4721 " << abc;"); 4722 } 4723 4724 TEST_F(FormatTest, BreaksDesireably) { 4725 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4726 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4727 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4728 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4730 "}"); 4731 4732 verifyFormat( 4733 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4735 4736 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4739 4740 verifyFormat( 4741 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4742 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4744 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4746 4747 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4748 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4749 4750 verifyFormat( 4751 "void f() {\n" 4752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4754 "}"); 4755 verifyFormat( 4756 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4758 verifyFormat( 4759 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4761 verifyFormat( 4762 "aaaaaa(aaa,\n" 4763 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4765 " aaaa);"); 4766 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4769 4770 // Indent consistently independent of call expression and unary operator. 4771 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4772 " dddddddddddddddddddddddddddddd));"); 4773 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4774 " dddddddddddddddddddddddddddddd));"); 4775 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4776 " dddddddddddddddddddddddddddddd));"); 4777 4778 // This test case breaks on an incorrect memoization, i.e. an optimization not 4779 // taking into account the StopAt value. 4780 verifyFormat( 4781 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4782 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4783 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4784 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4785 4786 verifyFormat("{\n {\n {\n" 4787 " Annotation.SpaceRequiredBefore =\n" 4788 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4789 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4790 " }\n }\n}"); 4791 4792 // Break on an outer level if there was a break on an inner level. 4793 EXPECT_EQ("f(g(h(a, // comment\n" 4794 " b, c),\n" 4795 " d, e),\n" 4796 " x, y);", 4797 format("f(g(h(a, // comment\n" 4798 " b, c), d, e), x, y);")); 4799 4800 // Prefer breaking similar line breaks. 4801 verifyFormat( 4802 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4803 " NSTrackingMouseEnteredAndExited |\n" 4804 " NSTrackingActiveAlways;"); 4805 } 4806 4807 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4808 FormatStyle NoBinPacking = getGoogleStyle(); 4809 NoBinPacking.BinPackParameters = false; 4810 NoBinPacking.BinPackArguments = true; 4811 verifyFormat("void f() {\n" 4812 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4814 "}", 4815 NoBinPacking); 4816 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4817 " int aaaaaaaaaaaaaaaaaaaa,\n" 4818 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4819 NoBinPacking); 4820 4821 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4822 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4823 " vector<int> bbbbbbbbbbbbbbb);", 4824 NoBinPacking); 4825 // FIXME: This behavior difference is probably not wanted. However, currently 4826 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4827 // template arguments from BreakBeforeParameter being set because of the 4828 // one-per-line formatting. 4829 verifyFormat( 4830 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4831 " aaaaaaaaaa> aaaaaaaaaa);", 4832 NoBinPacking); 4833 verifyFormat( 4834 "void fffffffffff(\n" 4835 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4836 " aaaaaaaaaa);"); 4837 } 4838 4839 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4840 FormatStyle NoBinPacking = getGoogleStyle(); 4841 NoBinPacking.BinPackParameters = false; 4842 NoBinPacking.BinPackArguments = false; 4843 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4844 " aaaaaaaaaaaaaaaaaaaa,\n" 4845 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4846 NoBinPacking); 4847 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4848 " aaaaaaaaaaaaa,\n" 4849 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4850 NoBinPacking); 4851 verifyFormat( 4852 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4853 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4855 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4857 NoBinPacking); 4858 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4859 " .aaaaaaaaaaaaaaaaaa();", 4860 NoBinPacking); 4861 verifyFormat("void f() {\n" 4862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4863 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4864 "}", 4865 NoBinPacking); 4866 4867 verifyFormat( 4868 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4869 " aaaaaaaaaaaa,\n" 4870 " aaaaaaaaaaaa);", 4871 NoBinPacking); 4872 verifyFormat( 4873 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4874 " ddddddddddddddddddddddddddddd),\n" 4875 " test);", 4876 NoBinPacking); 4877 4878 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4879 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4880 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4881 " aaaaaaaaaaaaaaaaaa;", 4882 NoBinPacking); 4883 verifyFormat("a(\"a\"\n" 4884 " \"a\",\n" 4885 " a);"); 4886 4887 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4888 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4889 " aaaaaaaaa,\n" 4890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4891 NoBinPacking); 4892 verifyFormat( 4893 "void f() {\n" 4894 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4895 " .aaaaaaa();\n" 4896 "}", 4897 NoBinPacking); 4898 verifyFormat( 4899 "template <class SomeType, class SomeOtherType>\n" 4900 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4901 NoBinPacking); 4902 } 4903 4904 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4905 FormatStyle Style = getLLVMStyleWithColumns(15); 4906 Style.ExperimentalAutoDetectBinPacking = true; 4907 EXPECT_EQ("aaa(aaaa,\n" 4908 " aaaa,\n" 4909 " aaaa);\n" 4910 "aaa(aaaa,\n" 4911 " aaaa,\n" 4912 " aaaa);", 4913 format("aaa(aaaa,\n" // one-per-line 4914 " aaaa,\n" 4915 " aaaa );\n" 4916 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4917 Style)); 4918 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4919 " aaaa);\n" 4920 "aaa(aaaa, aaaa,\n" 4921 " aaaa);", 4922 format("aaa(aaaa, aaaa,\n" // bin-packed 4923 " aaaa );\n" 4924 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4925 Style)); 4926 } 4927 4928 TEST_F(FormatTest, FormatsBuilderPattern) { 4929 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4930 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4931 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4932 " .StartsWith(\".init\", ORDER_INIT)\n" 4933 " .StartsWith(\".fini\", ORDER_FINI)\n" 4934 " .StartsWith(\".hash\", ORDER_HASH)\n" 4935 " .Default(ORDER_TEXT);\n"); 4936 4937 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4938 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4939 verifyFormat( 4940 "aaaaaaa->aaaaaaa\n" 4941 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4943 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4944 verifyFormat( 4945 "aaaaaaa->aaaaaaa\n" 4946 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4947 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4948 verifyFormat( 4949 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4950 " aaaaaaaaaaaaaa);"); 4951 verifyFormat( 4952 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4953 " aaaaaa->aaaaaaaaaaaa()\n" 4954 " ->aaaaaaaaaaaaaaaa(\n" 4955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4956 " ->aaaaaaaaaaaaaaaaa();"); 4957 verifyGoogleFormat( 4958 "void f() {\n" 4959 " someo->Add((new util::filetools::Handler(dir))\n" 4960 " ->OnEvent1(NewPermanentCallback(\n" 4961 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4962 " ->OnEvent2(NewPermanentCallback(\n" 4963 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4964 " ->OnEvent3(NewPermanentCallback(\n" 4965 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4966 " ->OnEvent5(NewPermanentCallback(\n" 4967 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4968 " ->OnEvent6(NewPermanentCallback(\n" 4969 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4970 "}"); 4971 4972 verifyFormat( 4973 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4974 verifyFormat("aaaaaaaaaaaaaaa()\n" 4975 " .aaaaaaaaaaaaaaa()\n" 4976 " .aaaaaaaaaaaaaaa()\n" 4977 " .aaaaaaaaaaaaaaa()\n" 4978 " .aaaaaaaaaaaaaaa();"); 4979 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4980 " .aaaaaaaaaaaaaaa()\n" 4981 " .aaaaaaaaaaaaaaa()\n" 4982 " .aaaaaaaaaaaaaaa();"); 4983 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4984 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4985 " .aaaaaaaaaaaaaaa();"); 4986 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4987 " ->aaaaaaaaaaaaaae(0)\n" 4988 " ->aaaaaaaaaaaaaaa();"); 4989 4990 // Don't linewrap after very short segments. 4991 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4992 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4993 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4994 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4995 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4996 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4997 verifyFormat("aaa()\n" 4998 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4999 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5000 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5001 5002 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5003 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5004 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 5005 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5006 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 5008 5009 // Prefer not to break after empty parentheses. 5010 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 5011 " First->LastNewlineOffset);"); 5012 5013 // Prefer not to create "hanging" indents. 5014 verifyFormat( 5015 "return !soooooooooooooome_map\n" 5016 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5017 " .second;"); 5018 verifyFormat( 5019 "return aaaaaaaaaaaaaaaa\n" 5020 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 5021 " .aaaa(aaaaaaaaaaaaaa);"); 5022 // No hanging indent here. 5023 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 5024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5025 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 5026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5027 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5028 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5029 getLLVMStyleWithColumns(60)); 5030 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 5031 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5032 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5033 getLLVMStyleWithColumns(59)); 5034 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5036 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5037 } 5038 5039 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 5040 verifyFormat( 5041 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5042 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 5043 verifyFormat( 5044 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 5045 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 5046 5047 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5048 " ccccccccccccccccccccccccc) {\n}"); 5049 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 5050 " ccccccccccccccccccccccccc) {\n}"); 5051 5052 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5053 " ccccccccccccccccccccccccc) {\n}"); 5054 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 5055 " ccccccccccccccccccccccccc) {\n}"); 5056 5057 verifyFormat( 5058 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 5059 " ccccccccccccccccccccccccc) {\n}"); 5060 verifyFormat( 5061 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 5062 " ccccccccccccccccccccccccc) {\n}"); 5063 5064 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 5065 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 5066 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 5067 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5068 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 5069 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 5070 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 5071 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5072 5073 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 5074 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 5075 " aaaaaaaaaaaaaaa != aa) {\n}"); 5076 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 5078 " aaaaaaaaaaaaaaa != aa) {\n}"); 5079 } 5080 5081 TEST_F(FormatTest, BreaksAfterAssignments) { 5082 verifyFormat( 5083 "unsigned Cost =\n" 5084 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 5085 " SI->getPointerAddressSpaceee());\n"); 5086 verifyFormat( 5087 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 5088 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 5089 5090 verifyFormat( 5091 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 5092 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 5093 verifyFormat("unsigned OriginalStartColumn =\n" 5094 " SourceMgr.getSpellingColumnNumber(\n" 5095 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 5096 " 1;"); 5097 } 5098 5099 TEST_F(FormatTest, AlignsAfterAssignments) { 5100 verifyFormat( 5101 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5102 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5103 verifyFormat( 5104 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5105 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5106 verifyFormat( 5107 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5108 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5109 verifyFormat( 5110 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5111 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5112 verifyFormat( 5113 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5114 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5115 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 5116 } 5117 5118 TEST_F(FormatTest, AlignsAfterReturn) { 5119 verifyFormat( 5120 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5121 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5122 verifyFormat( 5123 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5124 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5125 verifyFormat( 5126 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5127 " aaaaaaaaaaaaaaaaaaaaaa();"); 5128 verifyFormat( 5129 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5130 " aaaaaaaaaaaaaaaaaaaaaa());"); 5131 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5133 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 5135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5136 verifyFormat("return\n" 5137 " // true if code is one of a or b.\n" 5138 " code == a || code == b;"); 5139 } 5140 5141 TEST_F(FormatTest, AlignsAfterOpenBracket) { 5142 verifyFormat( 5143 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5144 " aaaaaaaaa aaaaaaa) {}"); 5145 verifyFormat( 5146 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5147 " aaaaaaaaaaa aaaaaaaaa);"); 5148 verifyFormat( 5149 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5150 " aaaaaaaaaaaaaaaaaaaaa));"); 5151 FormatStyle Style = getLLVMStyle(); 5152 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5153 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5154 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 5155 Style); 5156 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5157 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 5158 Style); 5159 verifyFormat("SomeLongVariableName->someFunction(\n" 5160 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 5161 Style); 5162 verifyFormat( 5163 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5164 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5165 Style); 5166 verifyFormat( 5167 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5168 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5169 Style); 5170 verifyFormat( 5171 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5172 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5173 Style); 5174 5175 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 5176 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 5177 " b));", 5178 Style); 5179 5180 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5181 Style.BinPackArguments = false; 5182 Style.BinPackParameters = false; 5183 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5184 " aaaaaaaaaaa aaaaaaaa,\n" 5185 " aaaaaaaaa aaaaaaa,\n" 5186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5187 Style); 5188 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5189 " aaaaaaaaaaa aaaaaaaaa,\n" 5190 " aaaaaaaaaaa aaaaaaaaa,\n" 5191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5192 Style); 5193 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 5194 " aaaaaaaaaaaaaaa,\n" 5195 " aaaaaaaaaaaaaaaaaaaaa,\n" 5196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5197 Style); 5198 verifyFormat( 5199 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 5200 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5201 Style); 5202 verifyFormat( 5203 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 5204 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5205 Style); 5206 verifyFormat( 5207 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5208 " aaaaaaaaaaaaaaaaaaaaa(\n" 5209 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 5210 " aaaaaaaaaaaaaaaa);", 5211 Style); 5212 verifyFormat( 5213 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5214 " aaaaaaaaaaaaaaaaaaaaa(\n" 5215 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 5216 " aaaaaaaaaaaaaaaa);", 5217 Style); 5218 } 5219 5220 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 5221 FormatStyle Style = getLLVMStyleWithColumns(40); 5222 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5223 " bbbbbbbbbbbbbbbbbbbbbb);", 5224 Style); 5225 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5226 Style.AlignOperands = false; 5227 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5228 " bbbbbbbbbbbbbbbbbbbbbb);", 5229 Style); 5230 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5231 Style.AlignOperands = true; 5232 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5233 " bbbbbbbbbbbbbbbbbbbbbb);", 5234 Style); 5235 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5236 Style.AlignOperands = false; 5237 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5238 " bbbbbbbbbbbbbbbbbbbbbb);", 5239 Style); 5240 } 5241 5242 TEST_F(FormatTest, BreaksConditionalExpressions) { 5243 verifyFormat( 5244 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5245 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5246 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5247 verifyFormat( 5248 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5249 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5250 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5251 verifyFormat( 5252 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5253 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5254 verifyFormat( 5255 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 5256 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5257 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5258 verifyFormat( 5259 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 5260 " : aaaaaaaaaaaaa);"); 5261 verifyFormat( 5262 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5263 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5264 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5265 " aaaaaaaaaaaaa);"); 5266 verifyFormat( 5267 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5268 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5269 " aaaaaaaaaaaaa);"); 5270 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5271 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5273 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5275 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5277 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5279 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5281 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5282 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5284 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5286 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5287 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5288 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5289 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5290 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5292 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5293 " : aaaaaaaaaaaaaaaa;"); 5294 verifyFormat( 5295 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5296 " ? aaaaaaaaaaaaaaa\n" 5297 " : aaaaaaaaaaaaaaa;"); 5298 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5299 " aaaaaaaaa\n" 5300 " ? b\n" 5301 " : c);"); 5302 verifyFormat("return aaaa == bbbb\n" 5303 " // comment\n" 5304 " ? aaaa\n" 5305 " : bbbb;"); 5306 verifyFormat("unsigned Indent =\n" 5307 " format(TheLine.First,\n" 5308 " IndentForLevel[TheLine.Level] >= 0\n" 5309 " ? IndentForLevel[TheLine.Level]\n" 5310 " : TheLine * 2,\n" 5311 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5312 getLLVMStyleWithColumns(60)); 5313 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5314 " ? aaaaaaaaaaaaaaa\n" 5315 " : bbbbbbbbbbbbbbb //\n" 5316 " ? ccccccccccccccc\n" 5317 " : ddddddddddddddd;"); 5318 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5319 " ? aaaaaaaaaaaaaaa\n" 5320 " : (bbbbbbbbbbbbbbb //\n" 5321 " ? ccccccccccccccc\n" 5322 " : ddddddddddddddd);"); 5323 verifyFormat( 5324 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5325 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5326 " aaaaaaaaaaaaaaaaaaaaa +\n" 5327 " aaaaaaaaaaaaaaaaaaaaa\n" 5328 " : aaaaaaaaaa;"); 5329 verifyFormat( 5330 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5331 " : aaaaaaaaaaaaaaaaaaaaaa\n" 5332 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5333 5334 FormatStyle NoBinPacking = getLLVMStyle(); 5335 NoBinPacking.BinPackArguments = false; 5336 verifyFormat( 5337 "void f() {\n" 5338 " g(aaa,\n" 5339 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5341 " ? aaaaaaaaaaaaaaa\n" 5342 " : aaaaaaaaaaaaaaa);\n" 5343 "}", 5344 NoBinPacking); 5345 verifyFormat( 5346 "void f() {\n" 5347 " g(aaa,\n" 5348 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5350 " ?: aaaaaaaaaaaaaaa);\n" 5351 "}", 5352 NoBinPacking); 5353 5354 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 5355 " // comment.\n" 5356 " ccccccccccccccccccccccccccccccccccccccc\n" 5357 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5358 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5359 5360 // Assignments in conditional expressions. Apparently not uncommon :-(. 5361 verifyFormat("return a != b\n" 5362 " // comment\n" 5363 " ? a = b\n" 5364 " : a = b;"); 5365 verifyFormat("return a != b\n" 5366 " // comment\n" 5367 " ? a = a != b\n" 5368 " // comment\n" 5369 " ? a = b\n" 5370 " : a\n" 5371 " : a;\n"); 5372 verifyFormat("return a != b\n" 5373 " // comment\n" 5374 " ? a\n" 5375 " : a = a != b\n" 5376 " // comment\n" 5377 " ? a = b\n" 5378 " : a;"); 5379 } 5380 5381 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5382 FormatStyle Style = getLLVMStyle(); 5383 Style.BreakBeforeTernaryOperators = false; 5384 Style.ColumnLimit = 70; 5385 verifyFormat( 5386 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5389 Style); 5390 verifyFormat( 5391 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5392 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5394 Style); 5395 verifyFormat( 5396 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5397 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5398 Style); 5399 verifyFormat( 5400 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5401 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5403 Style); 5404 verifyFormat( 5405 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5406 " aaaaaaaaaaaaa);", 5407 Style); 5408 verifyFormat( 5409 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5410 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5412 " aaaaaaaaaaaaa);", 5413 Style); 5414 verifyFormat( 5415 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5416 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5417 " aaaaaaaaaaaaa);", 5418 Style); 5419 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5424 Style); 5425 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5431 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5432 Style); 5433 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5437 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5438 Style); 5439 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5440 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5441 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5442 Style); 5443 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5447 Style); 5448 verifyFormat( 5449 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5450 " aaaaaaaaaaaaaaa :\n" 5451 " aaaaaaaaaaaaaaa;", 5452 Style); 5453 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5454 " aaaaaaaaa ?\n" 5455 " b :\n" 5456 " c);", 5457 Style); 5458 verifyFormat("unsigned Indent =\n" 5459 " format(TheLine.First,\n" 5460 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5461 " IndentForLevel[TheLine.Level] :\n" 5462 " TheLine * 2,\n" 5463 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5464 Style); 5465 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5466 " aaaaaaaaaaaaaaa :\n" 5467 " bbbbbbbbbbbbbbb ? //\n" 5468 " ccccccccccccccc :\n" 5469 " ddddddddddddddd;", 5470 Style); 5471 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5472 " aaaaaaaaaaaaaaa :\n" 5473 " (bbbbbbbbbbbbbbb ? //\n" 5474 " ccccccccccccccc :\n" 5475 " ddddddddddddddd);", 5476 Style); 5477 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5478 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5479 " ccccccccccccccccccccccccccc;", 5480 Style); 5481 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5482 " aaaaa :\n" 5483 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5484 Style); 5485 } 5486 5487 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5488 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5489 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5490 verifyFormat("bool a = true, b = false;"); 5491 5492 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5494 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5495 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5496 verifyFormat( 5497 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5498 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5499 " d = e && f;"); 5500 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5501 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5502 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5503 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5504 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5505 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5506 5507 FormatStyle Style = getGoogleStyle(); 5508 Style.PointerAlignment = FormatStyle::PAS_Left; 5509 Style.DerivePointerAlignment = false; 5510 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5511 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5512 " *b = bbbbbbbbbbbbbbbbbbb;", 5513 Style); 5514 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5515 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5516 Style); 5517 verifyFormat("vector<int*> a, b;", Style); 5518 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5519 } 5520 5521 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5522 verifyFormat("arr[foo ? bar : baz];"); 5523 verifyFormat("f()[foo ? bar : baz];"); 5524 verifyFormat("(a + b)[foo ? bar : baz];"); 5525 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5526 } 5527 5528 TEST_F(FormatTest, AlignsStringLiterals) { 5529 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5530 " \"short literal\");"); 5531 verifyFormat( 5532 "looooooooooooooooooooooooongFunction(\n" 5533 " \"short literal\"\n" 5534 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5535 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5536 " \" string literals\",\n" 5537 " and, other, parameters);"); 5538 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5539 " \"5678\";", 5540 format("fun + \"1243\" /* comment */\n" 5541 " \"5678\";", 5542 getLLVMStyleWithColumns(28))); 5543 EXPECT_EQ( 5544 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5545 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5546 " \"aaaaaaaaaaaaaaaa\";", 5547 format("aaaaaa =" 5548 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5549 "aaaaaaaaaaaaaaaaaaaaa\" " 5550 "\"aaaaaaaaaaaaaaaa\";")); 5551 verifyFormat("a = a + \"a\"\n" 5552 " \"a\"\n" 5553 " \"a\";"); 5554 verifyFormat("f(\"a\", \"b\"\n" 5555 " \"c\");"); 5556 5557 verifyFormat( 5558 "#define LL_FORMAT \"ll\"\n" 5559 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5560 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5561 5562 verifyFormat("#define A(X) \\\n" 5563 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5564 " \"ccccc\"", 5565 getLLVMStyleWithColumns(23)); 5566 verifyFormat("#define A \"def\"\n" 5567 "f(\"abc\" A \"ghi\"\n" 5568 " \"jkl\");"); 5569 5570 verifyFormat("f(L\"a\"\n" 5571 " L\"b\");"); 5572 verifyFormat("#define A(X) \\\n" 5573 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5574 " L\"ccccc\"", 5575 getLLVMStyleWithColumns(25)); 5576 5577 verifyFormat("f(@\"a\"\n" 5578 " @\"b\");"); 5579 verifyFormat("NSString s = @\"a\"\n" 5580 " @\"b\"\n" 5581 " @\"c\";"); 5582 verifyFormat("NSString s = @\"a\"\n" 5583 " \"b\"\n" 5584 " \"c\";"); 5585 } 5586 5587 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5588 FormatStyle Style = getLLVMStyle(); 5589 // No declarations or definitions should be moved to own line. 5590 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5591 verifyFormat("class A {\n" 5592 " int f() { return 1; }\n" 5593 " int g();\n" 5594 "};\n" 5595 "int f() { return 1; }\n" 5596 "int g();\n", 5597 Style); 5598 5599 // All declarations and definitions should have the return type moved to its 5600 // own 5601 // line. 5602 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5603 verifyFormat("class E {\n" 5604 " int\n" 5605 " f() {\n" 5606 " return 1;\n" 5607 " }\n" 5608 " int\n" 5609 " g();\n" 5610 "};\n" 5611 "int\n" 5612 "f() {\n" 5613 " return 1;\n" 5614 "}\n" 5615 "int\n" 5616 "g();\n", 5617 Style); 5618 5619 // Top-level definitions, and no kinds of declarations should have the 5620 // return type moved to its own line. 5621 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5622 verifyFormat("class B {\n" 5623 " int f() { return 1; }\n" 5624 " int g();\n" 5625 "};\n" 5626 "int\n" 5627 "f() {\n" 5628 " return 1;\n" 5629 "}\n" 5630 "int g();\n", 5631 Style); 5632 5633 // Top-level definitions and declarations should have the return type moved 5634 // to its own line. 5635 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5636 verifyFormat("class C {\n" 5637 " int f() { return 1; }\n" 5638 " int g();\n" 5639 "};\n" 5640 "int\n" 5641 "f() {\n" 5642 " return 1;\n" 5643 "}\n" 5644 "int\n" 5645 "g();\n", 5646 Style); 5647 5648 // All definitions should have the return type moved to its own line, but no 5649 // kinds of declarations. 5650 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5651 verifyFormat("class D {\n" 5652 " int\n" 5653 " f() {\n" 5654 " return 1;\n" 5655 " }\n" 5656 " int g();\n" 5657 "};\n" 5658 "int\n" 5659 "f() {\n" 5660 " return 1;\n" 5661 "}\n" 5662 "int g();\n", 5663 Style); 5664 verifyFormat("const char *\n" 5665 "f(void) {\n" // Break here. 5666 " return \"\";\n" 5667 "}\n" 5668 "const char *bar(void);\n", // No break here. 5669 Style); 5670 verifyFormat("template <class T>\n" 5671 "T *\n" 5672 "f(T &c) {\n" // Break here. 5673 " return NULL;\n" 5674 "}\n" 5675 "template <class T> T *f(T &c);\n", // No break here. 5676 Style); 5677 verifyFormat("class C {\n" 5678 " int\n" 5679 " operator+() {\n" 5680 " return 1;\n" 5681 " }\n" 5682 " int\n" 5683 " operator()() {\n" 5684 " return 1;\n" 5685 " }\n" 5686 "};\n", 5687 Style); 5688 verifyFormat("void\n" 5689 "A::operator()() {}\n" 5690 "void\n" 5691 "A::operator>>() {}\n" 5692 "void\n" 5693 "A::operator+() {}\n", 5694 Style); 5695 verifyFormat("void *operator new(std::size_t s);", // No break here. 5696 Style); 5697 verifyFormat("void *\n" 5698 "operator new(std::size_t s) {}", 5699 Style); 5700 verifyFormat("void *\n" 5701 "operator delete[](void *ptr) {}", 5702 Style); 5703 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5704 verifyFormat("const char *\n" 5705 "f(void)\n" // Break here. 5706 "{\n" 5707 " return \"\";\n" 5708 "}\n" 5709 "const char *bar(void);\n", // No break here. 5710 Style); 5711 verifyFormat("template <class T>\n" 5712 "T *\n" // Problem here: no line break 5713 "f(T &c)\n" // Break here. 5714 "{\n" 5715 " return NULL;\n" 5716 "}\n" 5717 "template <class T> T *f(T &c);\n", // No break here. 5718 Style); 5719 } 5720 5721 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5722 FormatStyle NoBreak = getLLVMStyle(); 5723 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5724 FormatStyle Break = getLLVMStyle(); 5725 Break.AlwaysBreakBeforeMultilineStrings = true; 5726 verifyFormat("aaaa = \"bbbb\"\n" 5727 " \"cccc\";", 5728 NoBreak); 5729 verifyFormat("aaaa =\n" 5730 " \"bbbb\"\n" 5731 " \"cccc\";", 5732 Break); 5733 verifyFormat("aaaa(\"bbbb\"\n" 5734 " \"cccc\");", 5735 NoBreak); 5736 verifyFormat("aaaa(\n" 5737 " \"bbbb\"\n" 5738 " \"cccc\");", 5739 Break); 5740 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5741 " \"cccc\");", 5742 NoBreak); 5743 verifyFormat("aaaa(qqq,\n" 5744 " \"bbbb\"\n" 5745 " \"cccc\");", 5746 Break); 5747 verifyFormat("aaaa(qqq,\n" 5748 " L\"bbbb\"\n" 5749 " L\"cccc\");", 5750 Break); 5751 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5752 " \"bbbb\"));", 5753 Break); 5754 verifyFormat("string s = someFunction(\n" 5755 " \"abc\"\n" 5756 " \"abc\");", 5757 Break); 5758 5759 // As we break before unary operators, breaking right after them is bad. 5760 verifyFormat("string foo = abc ? \"x\"\n" 5761 " \"blah blah blah blah blah blah\"\n" 5762 " : \"y\";", 5763 Break); 5764 5765 // Don't break if there is no column gain. 5766 verifyFormat("f(\"aaaa\"\n" 5767 " \"bbbb\");", 5768 Break); 5769 5770 // Treat literals with escaped newlines like multi-line string literals. 5771 EXPECT_EQ("x = \"a\\\n" 5772 "b\\\n" 5773 "c\";", 5774 format("x = \"a\\\n" 5775 "b\\\n" 5776 "c\";", 5777 NoBreak)); 5778 EXPECT_EQ("xxxx =\n" 5779 " \"a\\\n" 5780 "b\\\n" 5781 "c\";", 5782 format("xxxx = \"a\\\n" 5783 "b\\\n" 5784 "c\";", 5785 Break)); 5786 5787 // Exempt ObjC strings for now. 5788 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5789 " @\"bbbb\";", 5790 format("NSString *const kString = @\"aaaa\"\n" 5791 "@\"bbbb\";", 5792 Break)); 5793 5794 Break.ColumnLimit = 0; 5795 verifyFormat("const char *hello = \"hello llvm\";", Break); 5796 } 5797 5798 TEST_F(FormatTest, AlignsPipes) { 5799 verifyFormat( 5800 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5801 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5802 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5803 verifyFormat( 5804 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5805 " << aaaaaaaaaaaaaaaaaaaa;"); 5806 verifyFormat( 5807 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5808 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5809 verifyFormat( 5810 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5811 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5812 verifyFormat( 5813 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5814 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5815 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5816 verifyFormat( 5817 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5818 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5819 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5820 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5823 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5824 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5825 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5826 verifyFormat( 5827 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5829 5830 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5831 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5832 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5834 " aaaaaaaaaaaaaaaaaaaaa)\n" 5835 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5836 verifyFormat("LOG_IF(aaa == //\n" 5837 " bbb)\n" 5838 " << a << b;"); 5839 5840 // But sometimes, breaking before the first "<<" is desirable. 5841 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5842 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5843 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5844 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5845 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5846 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5847 " << BEF << IsTemplate << Description << E->getType();"); 5848 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5849 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5851 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5852 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5854 " << aaa;"); 5855 5856 verifyFormat( 5857 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5858 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5859 5860 // Incomplete string literal. 5861 EXPECT_EQ("llvm::errs() << \"\n" 5862 " << a;", 5863 format("llvm::errs() << \"\n<<a;")); 5864 5865 verifyFormat("void f() {\n" 5866 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5867 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5868 "}"); 5869 5870 // Handle 'endl'. 5871 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5872 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5873 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5874 5875 // Handle '\n'. 5876 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5877 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5878 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5879 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5880 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5881 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5882 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5883 } 5884 5885 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5886 verifyFormat("return out << \"somepacket = {\\n\"\n" 5887 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5888 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5889 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5890 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5891 " << \"}\";"); 5892 5893 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5894 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5895 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5896 verifyFormat( 5897 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5898 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5899 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5900 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5901 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5902 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5903 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5904 verifyFormat( 5905 "void f() {\n" 5906 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5907 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5908 "}"); 5909 5910 // Breaking before the first "<<" is generally not desirable. 5911 verifyFormat( 5912 "llvm::errs()\n" 5913 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5914 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5915 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5916 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5917 getLLVMStyleWithColumns(70)); 5918 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5919 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5920 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5921 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5922 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5923 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5924 getLLVMStyleWithColumns(70)); 5925 5926 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5927 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5928 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5929 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5930 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5931 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5932 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5933 " (aaaa + aaaa);", 5934 getLLVMStyleWithColumns(40)); 5935 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5936 " (aaaaaaa + aaaaa));", 5937 getLLVMStyleWithColumns(40)); 5938 verifyFormat( 5939 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5940 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5941 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5942 } 5943 5944 TEST_F(FormatTest, UnderstandsEquals) { 5945 verifyFormat( 5946 "aaaaaaaaaaaaaaaaa =\n" 5947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5948 verifyFormat( 5949 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5951 verifyFormat( 5952 "if (a) {\n" 5953 " f();\n" 5954 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5956 "}"); 5957 5958 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5959 " 100000000 + 10000000) {\n}"); 5960 } 5961 5962 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5963 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5964 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5965 5966 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5967 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5968 5969 verifyFormat( 5970 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5971 " Parameter2);"); 5972 5973 verifyFormat( 5974 "ShortObject->shortFunction(\n" 5975 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5976 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5977 5978 verifyFormat("loooooooooooooongFunction(\n" 5979 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5980 5981 verifyFormat( 5982 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5983 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5984 5985 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5986 " .WillRepeatedly(Return(SomeValue));"); 5987 verifyFormat("void f() {\n" 5988 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5989 " .Times(2)\n" 5990 " .WillRepeatedly(Return(SomeValue));\n" 5991 "}"); 5992 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5993 " ccccccccccccccccccccccc);"); 5994 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5996 " .aaaaa(aaaaa),\n" 5997 " aaaaaaaaaaaaaaaaaaaaa);"); 5998 verifyFormat("void f() {\n" 5999 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6000 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 6001 "}"); 6002 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6004 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6005 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6006 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6007 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6008 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6009 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6010 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 6011 "}"); 6012 6013 // Here, it is not necessary to wrap at "." or "->". 6014 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 6015 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 6016 verifyFormat( 6017 "aaaaaaaaaaa->aaaaaaaaa(\n" 6018 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6019 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 6020 6021 verifyFormat( 6022 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6023 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 6024 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 6025 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6026 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 6027 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6028 6029 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6031 " .a();"); 6032 6033 FormatStyle NoBinPacking = getLLVMStyle(); 6034 NoBinPacking.BinPackParameters = false; 6035 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6036 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6037 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 6038 " aaaaaaaaaaaaaaaaaaa,\n" 6039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6040 NoBinPacking); 6041 6042 // If there is a subsequent call, change to hanging indentation. 6043 verifyFormat( 6044 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6045 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 6046 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6047 verifyFormat( 6048 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6049 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 6050 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6052 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6053 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6055 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6056 } 6057 6058 TEST_F(FormatTest, WrapsTemplateDeclarations) { 6059 verifyFormat("template <typename T>\n" 6060 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6061 verifyFormat("template <typename T>\n" 6062 "// T should be one of {A, B}.\n" 6063 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6064 verifyFormat( 6065 "template <typename T>\n" 6066 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 6067 verifyFormat("template <typename T>\n" 6068 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 6069 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 6070 verifyFormat( 6071 "template <typename T>\n" 6072 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 6073 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 6074 verifyFormat( 6075 "template <typename T>\n" 6076 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 6077 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 6078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6079 verifyFormat("template <typename T>\n" 6080 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6081 " int aaaaaaaaaaaaaaaaaaaaaa);"); 6082 verifyFormat( 6083 "template <typename T1, typename T2 = char, typename T3 = char,\n" 6084 " typename T4 = char>\n" 6085 "void f();"); 6086 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 6087 " template <typename> class cccccccccccccccccccccc,\n" 6088 " typename ddddddddddddd>\n" 6089 "class C {};"); 6090 verifyFormat( 6091 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 6092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6093 6094 verifyFormat("void f() {\n" 6095 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 6096 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 6097 "}"); 6098 6099 verifyFormat("template <typename T> class C {};"); 6100 verifyFormat("template <typename T> void f();"); 6101 verifyFormat("template <typename T> void f() {}"); 6102 verifyFormat( 6103 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 6106 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 6109 " bbbbbbbbbbbbbbbbbbbbbbbb);", 6110 getLLVMStyleWithColumns(72)); 6111 EXPECT_EQ("static_cast<A< //\n" 6112 " B> *>(\n" 6113 "\n" 6114 " );", 6115 format("static_cast<A<//\n" 6116 " B>*>(\n" 6117 "\n" 6118 " );")); 6119 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6120 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 6121 6122 FormatStyle AlwaysBreak = getLLVMStyle(); 6123 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 6124 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 6125 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 6126 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 6127 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6128 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6129 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 6130 verifyFormat("template <template <typename> class Fooooooo,\n" 6131 " template <typename> class Baaaaaaar>\n" 6132 "struct C {};", 6133 AlwaysBreak); 6134 verifyFormat("template <typename T> // T can be A, B or C.\n" 6135 "struct C {};", 6136 AlwaysBreak); 6137 verifyFormat("template <enum E> class A {\n" 6138 "public:\n" 6139 " E *f();\n" 6140 "};"); 6141 } 6142 6143 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 6144 verifyFormat( 6145 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6147 verifyFormat( 6148 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6151 6152 // FIXME: Should we have the extra indent after the second break? 6153 verifyFormat( 6154 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6157 6158 verifyFormat( 6159 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 6160 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 6161 6162 // Breaking at nested name specifiers is generally not desirable. 6163 verifyFormat( 6164 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6165 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6166 6167 verifyFormat( 6168 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 6169 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6171 " aaaaaaaaaaaaaaaaaaaaa);", 6172 getLLVMStyleWithColumns(74)); 6173 6174 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6176 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6177 } 6178 6179 TEST_F(FormatTest, UnderstandsTemplateParameters) { 6180 verifyFormat("A<int> a;"); 6181 verifyFormat("A<A<A<int>>> a;"); 6182 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 6183 verifyFormat("bool x = a < 1 || 2 > a;"); 6184 verifyFormat("bool x = 5 < f<int>();"); 6185 verifyFormat("bool x = f<int>() > 5;"); 6186 verifyFormat("bool x = 5 < a<int>::x;"); 6187 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 6188 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 6189 6190 verifyGoogleFormat("A<A<int>> a;"); 6191 verifyGoogleFormat("A<A<A<int>>> a;"); 6192 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 6193 verifyGoogleFormat("A<A<int> > a;"); 6194 verifyGoogleFormat("A<A<A<int> > > a;"); 6195 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 6196 verifyGoogleFormat("A<::A<int>> a;"); 6197 verifyGoogleFormat("A<::A> a;"); 6198 verifyGoogleFormat("A< ::A> a;"); 6199 verifyGoogleFormat("A< ::A<int> > a;"); 6200 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 6201 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 6202 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 6203 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 6204 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 6205 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 6206 6207 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 6208 6209 verifyFormat("test >> a >> b;"); 6210 verifyFormat("test << a >> b;"); 6211 6212 verifyFormat("f<int>();"); 6213 verifyFormat("template <typename T> void f() {}"); 6214 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 6215 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 6216 "sizeof(char)>::type>;"); 6217 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 6218 verifyFormat("f(a.operator()<A>());"); 6219 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6220 " .template operator()<A>());", 6221 getLLVMStyleWithColumns(35)); 6222 6223 // Not template parameters. 6224 verifyFormat("return a < b && c > d;"); 6225 verifyFormat("void f() {\n" 6226 " while (a < b && c > d) {\n" 6227 " }\n" 6228 "}"); 6229 verifyFormat("template <typename... Types>\n" 6230 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 6231 6232 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 6234 getLLVMStyleWithColumns(60)); 6235 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 6236 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 6237 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 6238 } 6239 6240 TEST_F(FormatTest, BitshiftOperatorWidth) { 6241 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6242 " bar */", 6243 format("int a=1<<2; /* foo\n" 6244 " bar */")); 6245 6246 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6247 " bar */", 6248 format("int b =256>>1 ; /* foo\n" 6249 " bar */")); 6250 } 6251 6252 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6253 verifyFormat("COMPARE(a, ==, b);"); 6254 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6255 } 6256 6257 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6258 verifyFormat("int A::*x;"); 6259 verifyFormat("int (S::*func)(void *);"); 6260 verifyFormat("void f() { int (S::*func)(void *); }"); 6261 verifyFormat("typedef bool *(Class::*Member)() const;"); 6262 verifyFormat("void f() {\n" 6263 " (a->*f)();\n" 6264 " a->*x;\n" 6265 " (a.*f)();\n" 6266 " ((*a).*f)();\n" 6267 " a.*x;\n" 6268 "}"); 6269 verifyFormat("void f() {\n" 6270 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6271 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6272 "}"); 6273 verifyFormat( 6274 "(aaaaaaaaaa->*bbbbbbb)(\n" 6275 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6276 FormatStyle Style = getLLVMStyle(); 6277 Style.PointerAlignment = FormatStyle::PAS_Left; 6278 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6279 } 6280 6281 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6282 verifyFormat("int a = -2;"); 6283 verifyFormat("f(-1, -2, -3);"); 6284 verifyFormat("a[-1] = 5;"); 6285 verifyFormat("int a = 5 + -2;"); 6286 verifyFormat("if (i == -1) {\n}"); 6287 verifyFormat("if (i != -1) {\n}"); 6288 verifyFormat("if (i > -1) {\n}"); 6289 verifyFormat("if (i < -1) {\n}"); 6290 verifyFormat("++(a->f());"); 6291 verifyFormat("--(a->f());"); 6292 verifyFormat("(a->f())++;"); 6293 verifyFormat("a[42]++;"); 6294 verifyFormat("if (!(a->f())) {\n}"); 6295 6296 verifyFormat("a-- > b;"); 6297 verifyFormat("b ? -a : c;"); 6298 verifyFormat("n * sizeof char16;"); 6299 verifyFormat("n * alignof char16;", getGoogleStyle()); 6300 verifyFormat("sizeof(char);"); 6301 verifyFormat("alignof(char);", getGoogleStyle()); 6302 6303 verifyFormat("return -1;"); 6304 verifyFormat("switch (a) {\n" 6305 "case -1:\n" 6306 " break;\n" 6307 "}"); 6308 verifyFormat("#define X -1"); 6309 verifyFormat("#define X -kConstant"); 6310 6311 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6312 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6313 6314 verifyFormat("int a = /* confusing comment */ -1;"); 6315 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6316 verifyFormat("int a = i /* confusing comment */++;"); 6317 } 6318 6319 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6320 verifyFormat("if (!aaaaaaaaaa( // break\n" 6321 " aaaaa)) {\n" 6322 "}"); 6323 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6324 " aaaaa));"); 6325 verifyFormat("*aaa = aaaaaaa( // break\n" 6326 " bbbbbb);"); 6327 } 6328 6329 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6330 verifyFormat("bool operator<();"); 6331 verifyFormat("bool operator>();"); 6332 verifyFormat("bool operator=();"); 6333 verifyFormat("bool operator==();"); 6334 verifyFormat("bool operator!=();"); 6335 verifyFormat("int operator+();"); 6336 verifyFormat("int operator++();"); 6337 verifyFormat("bool operator,();"); 6338 verifyFormat("bool operator();"); 6339 verifyFormat("bool operator()();"); 6340 verifyFormat("bool operator[]();"); 6341 verifyFormat("operator bool();"); 6342 verifyFormat("operator int();"); 6343 verifyFormat("operator void *();"); 6344 verifyFormat("operator SomeType<int>();"); 6345 verifyFormat("operator SomeType<int, int>();"); 6346 verifyFormat("operator SomeType<SomeType<int>>();"); 6347 verifyFormat("void *operator new(std::size_t size);"); 6348 verifyFormat("void *operator new[](std::size_t size);"); 6349 verifyFormat("void operator delete(void *ptr);"); 6350 verifyFormat("void operator delete[](void *ptr);"); 6351 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6352 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6353 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6354 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6355 6356 verifyFormat( 6357 "ostream &operator<<(ostream &OutputStream,\n" 6358 " SomeReallyLongType WithSomeReallyLongValue);"); 6359 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6360 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6361 " return left.group < right.group;\n" 6362 "}"); 6363 verifyFormat("SomeType &operator=(const SomeType &S);"); 6364 verifyFormat("f.template operator()<int>();"); 6365 6366 verifyGoogleFormat("operator void*();"); 6367 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6368 verifyGoogleFormat("operator ::A();"); 6369 6370 verifyFormat("using A::operator+;"); 6371 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6372 "int i;"); 6373 } 6374 6375 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6376 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6377 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6378 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6379 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6380 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6381 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6382 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6383 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6384 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6385 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6386 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6387 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 6388 verifyFormat("template <typename T>\n" 6389 "void F(T) && = delete;", 6390 getGoogleStyle()); 6391 6392 FormatStyle AlignLeft = getLLVMStyle(); 6393 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6394 verifyFormat("void A::b() && {}", AlignLeft); 6395 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6396 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6397 AlignLeft); 6398 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6399 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6400 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6401 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6402 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6403 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6404 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 6405 6406 FormatStyle Spaces = getLLVMStyle(); 6407 Spaces.SpacesInCStyleCastParentheses = true; 6408 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6409 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6410 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6411 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6412 6413 Spaces.SpacesInCStyleCastParentheses = false; 6414 Spaces.SpacesInParentheses = true; 6415 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6416 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6417 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6418 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6419 } 6420 6421 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6422 verifyFormat("void f() {\n" 6423 " A *a = new A;\n" 6424 " A *a = new (placement) A;\n" 6425 " delete a;\n" 6426 " delete (A *)a;\n" 6427 "}"); 6428 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6429 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6430 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6431 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6432 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6433 verifyFormat("delete[] h->p;"); 6434 } 6435 6436 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6437 verifyFormat("int *f(int *a) {}"); 6438 verifyFormat("int main(int argc, char **argv) {}"); 6439 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6440 verifyIndependentOfContext("f(a, *a);"); 6441 verifyFormat("void g() { f(*a); }"); 6442 verifyIndependentOfContext("int a = b * 10;"); 6443 verifyIndependentOfContext("int a = 10 * b;"); 6444 verifyIndependentOfContext("int a = b * c;"); 6445 verifyIndependentOfContext("int a += b * c;"); 6446 verifyIndependentOfContext("int a -= b * c;"); 6447 verifyIndependentOfContext("int a *= b * c;"); 6448 verifyIndependentOfContext("int a /= b * c;"); 6449 verifyIndependentOfContext("int a = *b;"); 6450 verifyIndependentOfContext("int a = *b * c;"); 6451 verifyIndependentOfContext("int a = b * *c;"); 6452 verifyIndependentOfContext("int a = b * (10);"); 6453 verifyIndependentOfContext("S << b * (10);"); 6454 verifyIndependentOfContext("return 10 * b;"); 6455 verifyIndependentOfContext("return *b * *c;"); 6456 verifyIndependentOfContext("return a & ~b;"); 6457 verifyIndependentOfContext("f(b ? *c : *d);"); 6458 verifyIndependentOfContext("int a = b ? *c : *d;"); 6459 verifyIndependentOfContext("*b = a;"); 6460 verifyIndependentOfContext("a * ~b;"); 6461 verifyIndependentOfContext("a * !b;"); 6462 verifyIndependentOfContext("a * +b;"); 6463 verifyIndependentOfContext("a * -b;"); 6464 verifyIndependentOfContext("a * ++b;"); 6465 verifyIndependentOfContext("a * --b;"); 6466 verifyIndependentOfContext("a[4] * b;"); 6467 verifyIndependentOfContext("a[a * a] = 1;"); 6468 verifyIndependentOfContext("f() * b;"); 6469 verifyIndependentOfContext("a * [self dostuff];"); 6470 verifyIndependentOfContext("int x = a * (a + b);"); 6471 verifyIndependentOfContext("(a *)(a + b);"); 6472 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6473 verifyIndependentOfContext("int *pa = (int *)&a;"); 6474 verifyIndependentOfContext("return sizeof(int **);"); 6475 verifyIndependentOfContext("return sizeof(int ******);"); 6476 verifyIndependentOfContext("return (int **&)a;"); 6477 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6478 verifyFormat("void f(Type (*parameter)[10]) {}"); 6479 verifyFormat("void f(Type (¶meter)[10]) {}"); 6480 verifyGoogleFormat("return sizeof(int**);"); 6481 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6482 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6483 verifyFormat("auto a = [](int **&, int ***) {};"); 6484 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6485 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6486 verifyFormat("[](const decltype(*a) &value) {}"); 6487 verifyFormat("decltype(a * b) F();"); 6488 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6489 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6490 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6491 verifyIndependentOfContext("int i{a * b};"); 6492 verifyIndependentOfContext("aaa && aaa->f();"); 6493 verifyIndependentOfContext("int x = ~*p;"); 6494 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6495 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6496 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6497 verifyFormat("void f() { f(a, c * d); }"); 6498 verifyFormat("void f() { f(new a(), c * d); }"); 6499 verifyFormat("void f(const MyOverride &override);"); 6500 verifyFormat("void f(const MyFinal &final);"); 6501 verifyIndependentOfContext("bool a = f() && override.f();"); 6502 verifyIndependentOfContext("bool a = f() && final.f();"); 6503 6504 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6505 6506 verifyIndependentOfContext("A<int *> a;"); 6507 verifyIndependentOfContext("A<int **> a;"); 6508 verifyIndependentOfContext("A<int *, int *> a;"); 6509 verifyIndependentOfContext("A<int *[]> a;"); 6510 verifyIndependentOfContext( 6511 "const char *const p = reinterpret_cast<const char *const>(q);"); 6512 verifyIndependentOfContext("A<int **, int **> a;"); 6513 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6514 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6515 verifyFormat("for (; a && b;) {\n}"); 6516 verifyFormat("bool foo = true && [] { return false; }();"); 6517 6518 verifyFormat( 6519 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6521 6522 verifyGoogleFormat("int const* a = &b;"); 6523 verifyGoogleFormat("**outparam = 1;"); 6524 verifyGoogleFormat("*outparam = a * b;"); 6525 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6526 verifyGoogleFormat("A<int*> a;"); 6527 verifyGoogleFormat("A<int**> a;"); 6528 verifyGoogleFormat("A<int*, int*> a;"); 6529 verifyGoogleFormat("A<int**, int**> a;"); 6530 verifyGoogleFormat("f(b ? *c : *d);"); 6531 verifyGoogleFormat("int a = b ? *c : *d;"); 6532 verifyGoogleFormat("Type* t = **x;"); 6533 verifyGoogleFormat("Type* t = *++*x;"); 6534 verifyGoogleFormat("*++*x;"); 6535 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6536 verifyGoogleFormat("Type* t = x++ * y;"); 6537 verifyGoogleFormat( 6538 "const char* const p = reinterpret_cast<const char* const>(q);"); 6539 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6540 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6541 verifyGoogleFormat("template <typename T>\n" 6542 "void f(int i = 0, SomeType** temps = NULL);"); 6543 6544 FormatStyle Left = getLLVMStyle(); 6545 Left.PointerAlignment = FormatStyle::PAS_Left; 6546 verifyFormat("x = *a(x) = *a(y);", Left); 6547 verifyFormat("for (;; *a = b) {\n}", Left); 6548 verifyFormat("return *this += 1;", Left); 6549 6550 verifyIndependentOfContext("a = *(x + y);"); 6551 verifyIndependentOfContext("a = &(x + y);"); 6552 verifyIndependentOfContext("*(x + y).call();"); 6553 verifyIndependentOfContext("&(x + y)->call();"); 6554 verifyFormat("void f() { &(*I).first; }"); 6555 6556 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6557 verifyFormat( 6558 "int *MyValues = {\n" 6559 " *A, // Operator detection might be confused by the '{'\n" 6560 " *BB // Operator detection might be confused by previous comment\n" 6561 "};"); 6562 6563 verifyIndependentOfContext("if (int *a = &b)"); 6564 verifyIndependentOfContext("if (int &a = *b)"); 6565 verifyIndependentOfContext("if (a & b[i])"); 6566 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6567 verifyIndependentOfContext("if (*b[i])"); 6568 verifyIndependentOfContext("if (int *a = (&b))"); 6569 verifyIndependentOfContext("while (int *a = &b)"); 6570 verifyIndependentOfContext("size = sizeof *a;"); 6571 verifyIndependentOfContext("if (a && (b = c))"); 6572 verifyFormat("void f() {\n" 6573 " for (const int &v : Values) {\n" 6574 " }\n" 6575 "}"); 6576 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 6577 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 6578 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 6579 6580 verifyFormat("#define A (!a * b)"); 6581 verifyFormat("#define MACRO \\\n" 6582 " int *i = a * b; \\\n" 6583 " void f(a *b);", 6584 getLLVMStyleWithColumns(19)); 6585 6586 verifyIndependentOfContext("A = new SomeType *[Length];"); 6587 verifyIndependentOfContext("A = new SomeType *[Length]();"); 6588 verifyIndependentOfContext("T **t = new T *;"); 6589 verifyIndependentOfContext("T **t = new T *();"); 6590 verifyGoogleFormat("A = new SomeType*[Length]();"); 6591 verifyGoogleFormat("A = new SomeType*[Length];"); 6592 verifyGoogleFormat("T** t = new T*;"); 6593 verifyGoogleFormat("T** t = new T*();"); 6594 6595 FormatStyle PointerLeft = getLLVMStyle(); 6596 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 6597 verifyFormat("delete *x;", PointerLeft); 6598 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 6599 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 6600 verifyFormat("template <bool a, bool b> " 6601 "typename t::if<x && y>::type f() {}"); 6602 verifyFormat("template <int *y> f() {}"); 6603 verifyFormat("vector<int *> v;"); 6604 verifyFormat("vector<int *const> v;"); 6605 verifyFormat("vector<int *const **const *> v;"); 6606 verifyFormat("vector<int *volatile> v;"); 6607 verifyFormat("vector<a * b> v;"); 6608 verifyFormat("foo<b && false>();"); 6609 verifyFormat("foo<b & 1>();"); 6610 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 6611 verifyFormat( 6612 "template <class T, class = typename std::enable_if<\n" 6613 " std::is_integral<T>::value &&\n" 6614 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6615 "void F();", 6616 getLLVMStyleWithColumns(70)); 6617 verifyFormat( 6618 "template <class T,\n" 6619 " class = typename std::enable_if<\n" 6620 " std::is_integral<T>::value &&\n" 6621 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6622 " class U>\n" 6623 "void F();", 6624 getLLVMStyleWithColumns(70)); 6625 verifyFormat( 6626 "template <class T,\n" 6627 " class = typename ::std::enable_if<\n" 6628 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6629 "void F();", 6630 getGoogleStyleWithColumns(68)); 6631 6632 verifyIndependentOfContext("MACRO(int *i);"); 6633 verifyIndependentOfContext("MACRO(auto *a);"); 6634 verifyIndependentOfContext("MACRO(const A *a);"); 6635 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6636 verifyFormat("void f() { f(float{1}, a * a); }"); 6637 // FIXME: Is there a way to make this work? 6638 // verifyIndependentOfContext("MACRO(A *a);"); 6639 6640 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6641 verifyFormat("return options != nullptr && operator==(*options);"); 6642 6643 EXPECT_EQ("#define OP(x) \\\n" 6644 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6645 " return s << a.DebugString(); \\\n" 6646 " }", 6647 format("#define OP(x) \\\n" 6648 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6649 " return s << a.DebugString(); \\\n" 6650 " }", 6651 getLLVMStyleWithColumns(50))); 6652 6653 // FIXME: We cannot handle this case yet; we might be able to figure out that 6654 // foo<x> d > v; doesn't make sense. 6655 verifyFormat("foo<a<b && c> d> v;"); 6656 6657 FormatStyle PointerMiddle = getLLVMStyle(); 6658 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6659 verifyFormat("delete *x;", PointerMiddle); 6660 verifyFormat("int * x;", PointerMiddle); 6661 verifyFormat("template <int * y> f() {}", PointerMiddle); 6662 verifyFormat("int * f(int * a) {}", PointerMiddle); 6663 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6664 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6665 verifyFormat("A<int *> a;", PointerMiddle); 6666 verifyFormat("A<int **> a;", PointerMiddle); 6667 verifyFormat("A<int *, int *> a;", PointerMiddle); 6668 verifyFormat("A<int * []> a;", PointerMiddle); 6669 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6670 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6671 verifyFormat("T ** t = new T *;", PointerMiddle); 6672 6673 // Member function reference qualifiers aren't binary operators. 6674 verifyFormat("string // break\n" 6675 "operator()() & {}"); 6676 verifyFormat("string // break\n" 6677 "operator()() && {}"); 6678 verifyGoogleFormat("template <typename T>\n" 6679 "auto x() & -> int {}"); 6680 } 6681 6682 TEST_F(FormatTest, UnderstandsAttributes) { 6683 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6684 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6685 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6686 FormatStyle AfterType = getLLVMStyle(); 6687 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6688 verifyFormat("__attribute__((nodebug)) void\n" 6689 "foo() {}\n", 6690 AfterType); 6691 } 6692 6693 TEST_F(FormatTest, UnderstandsEllipsis) { 6694 verifyFormat("int printf(const char *fmt, ...);"); 6695 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6696 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6697 6698 FormatStyle PointersLeft = getLLVMStyle(); 6699 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6700 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6701 } 6702 6703 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6704 EXPECT_EQ("int *a;\n" 6705 "int *a;\n" 6706 "int *a;", 6707 format("int *a;\n" 6708 "int* a;\n" 6709 "int *a;", 6710 getGoogleStyle())); 6711 EXPECT_EQ("int* a;\n" 6712 "int* a;\n" 6713 "int* a;", 6714 format("int* a;\n" 6715 "int* a;\n" 6716 "int *a;", 6717 getGoogleStyle())); 6718 EXPECT_EQ("int *a;\n" 6719 "int *a;\n" 6720 "int *a;", 6721 format("int *a;\n" 6722 "int * a;\n" 6723 "int * a;", 6724 getGoogleStyle())); 6725 EXPECT_EQ("auto x = [] {\n" 6726 " int *a;\n" 6727 " int *a;\n" 6728 " int *a;\n" 6729 "};", 6730 format("auto x=[]{int *a;\n" 6731 "int * a;\n" 6732 "int * a;};", 6733 getGoogleStyle())); 6734 } 6735 6736 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6737 verifyFormat("int f(int &&a) {}"); 6738 verifyFormat("int f(int a, char &&b) {}"); 6739 verifyFormat("void f() { int &&a = b; }"); 6740 verifyGoogleFormat("int f(int a, char&& b) {}"); 6741 verifyGoogleFormat("void f() { int&& a = b; }"); 6742 6743 verifyIndependentOfContext("A<int &&> a;"); 6744 verifyIndependentOfContext("A<int &&, int &&> a;"); 6745 verifyGoogleFormat("A<int&&> a;"); 6746 verifyGoogleFormat("A<int&&, int&&> a;"); 6747 6748 // Not rvalue references: 6749 verifyFormat("template <bool B, bool C> class A {\n" 6750 " static_assert(B && C, \"Something is wrong\");\n" 6751 "};"); 6752 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6753 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6754 verifyFormat("#define A(a, b) (a && b)"); 6755 } 6756 6757 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6758 verifyFormat("void f() {\n" 6759 " x[aaaaaaaaa -\n" 6760 " b] = 23;\n" 6761 "}", 6762 getLLVMStyleWithColumns(15)); 6763 } 6764 6765 TEST_F(FormatTest, FormatsCasts) { 6766 verifyFormat("Type *A = static_cast<Type *>(P);"); 6767 verifyFormat("Type *A = (Type *)P;"); 6768 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6769 verifyFormat("int a = (int)(2.0f);"); 6770 verifyFormat("int a = (int)2.0f;"); 6771 verifyFormat("x[(int32)y];"); 6772 verifyFormat("x = (int32)y;"); 6773 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6774 verifyFormat("int a = (int)*b;"); 6775 verifyFormat("int a = (int)2.0f;"); 6776 verifyFormat("int a = (int)~0;"); 6777 verifyFormat("int a = (int)++a;"); 6778 verifyFormat("int a = (int)sizeof(int);"); 6779 verifyFormat("int a = (int)+2;"); 6780 verifyFormat("my_int a = (my_int)2.0f;"); 6781 verifyFormat("my_int a = (my_int)sizeof(int);"); 6782 verifyFormat("return (my_int)aaa;"); 6783 verifyFormat("#define x ((int)-1)"); 6784 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6785 verifyFormat("#define p(q) ((int *)&q)"); 6786 verifyFormat("fn(a)(b) + 1;"); 6787 6788 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6789 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6790 verifyFormat("my_int a = (my_int)~0;"); 6791 verifyFormat("my_int a = (my_int)++a;"); 6792 verifyFormat("my_int a = (my_int)-2;"); 6793 verifyFormat("my_int a = (my_int)1;"); 6794 verifyFormat("my_int a = (my_int *)1;"); 6795 verifyFormat("my_int a = (const my_int)-1;"); 6796 verifyFormat("my_int a = (const my_int *)-1;"); 6797 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6798 verifyFormat("my_int a = (ns::my_int)-2;"); 6799 verifyFormat("case (my_int)ONE:"); 6800 verifyFormat("auto x = (X)this;"); 6801 6802 // FIXME: single value wrapped with paren will be treated as cast. 6803 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6804 6805 verifyFormat("{ (void)F; }"); 6806 6807 // Don't break after a cast's 6808 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6809 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6810 " bbbbbbbbbbbbbbbbbbbbbb);"); 6811 6812 // These are not casts. 6813 verifyFormat("void f(int *) {}"); 6814 verifyFormat("f(foo)->b;"); 6815 verifyFormat("f(foo).b;"); 6816 verifyFormat("f(foo)(b);"); 6817 verifyFormat("f(foo)[b];"); 6818 verifyFormat("[](foo) { return 4; }(bar);"); 6819 verifyFormat("(*funptr)(foo)[4];"); 6820 verifyFormat("funptrs[4](foo)[4];"); 6821 verifyFormat("void f(int *);"); 6822 verifyFormat("void f(int *) = 0;"); 6823 verifyFormat("void f(SmallVector<int>) {}"); 6824 verifyFormat("void f(SmallVector<int>);"); 6825 verifyFormat("void f(SmallVector<int>) = 0;"); 6826 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6827 verifyFormat("int a = sizeof(int) * b;"); 6828 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6829 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6830 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6831 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6832 6833 // These are not casts, but at some point were confused with casts. 6834 verifyFormat("virtual void foo(int *) override;"); 6835 verifyFormat("virtual void foo(char &) const;"); 6836 verifyFormat("virtual void foo(int *a, char *) const;"); 6837 verifyFormat("int a = sizeof(int *) + b;"); 6838 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6839 verifyFormat("bool b = f(g<int>) && c;"); 6840 verifyFormat("typedef void (*f)(int i) func;"); 6841 6842 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6843 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6844 // FIXME: The indentation here is not ideal. 6845 verifyFormat( 6846 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6847 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6848 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6849 } 6850 6851 TEST_F(FormatTest, FormatsFunctionTypes) { 6852 verifyFormat("A<bool()> a;"); 6853 verifyFormat("A<SomeType()> a;"); 6854 verifyFormat("A<void (*)(int, std::string)> a;"); 6855 verifyFormat("A<void *(int)>;"); 6856 verifyFormat("void *(*a)(int *, SomeType *);"); 6857 verifyFormat("int (*func)(void *);"); 6858 verifyFormat("void f() { int (*func)(void *); }"); 6859 verifyFormat("template <class CallbackClass>\n" 6860 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6861 6862 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6863 verifyGoogleFormat("void* (*a)(int);"); 6864 verifyGoogleFormat( 6865 "template <class CallbackClass>\n" 6866 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6867 6868 // Other constructs can look somewhat like function types: 6869 verifyFormat("A<sizeof(*x)> a;"); 6870 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6871 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6872 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6873 verifyFormat("int x = f(&h)();"); 6874 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6875 } 6876 6877 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6878 verifyFormat("A (*foo_)[6];"); 6879 verifyFormat("vector<int> (*foo_)[6];"); 6880 } 6881 6882 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6883 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6884 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6885 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6886 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6887 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6888 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6889 6890 // Different ways of ()-initializiation. 6891 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6892 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6893 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6894 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6895 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6896 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6897 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6898 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6899 } 6900 6901 TEST_F(FormatTest, BreaksLongDeclarations) { 6902 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6903 " AnotherNameForTheLongType;"); 6904 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6906 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6907 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6908 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6909 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6910 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6911 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6912 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6913 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6914 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6915 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6916 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6917 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6918 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6919 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6920 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6921 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6922 FormatStyle Indented = getLLVMStyle(); 6923 Indented.IndentWrappedFunctionNames = true; 6924 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6925 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6926 Indented); 6927 verifyFormat( 6928 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6929 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6930 Indented); 6931 verifyFormat( 6932 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6933 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6934 Indented); 6935 verifyFormat( 6936 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6937 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6938 Indented); 6939 6940 // FIXME: Without the comment, this breaks after "(". 6941 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6942 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6943 getGoogleStyle()); 6944 6945 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6946 " int LoooooooooooooooooooongParam2) {}"); 6947 verifyFormat( 6948 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6949 " SourceLocation L, IdentifierIn *II,\n" 6950 " Type *T) {}"); 6951 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6952 "ReallyReaaallyLongFunctionName(\n" 6953 " const std::string &SomeParameter,\n" 6954 " const SomeType<string, SomeOtherTemplateParameter>\n" 6955 " &ReallyReallyLongParameterName,\n" 6956 " const SomeType<string, SomeOtherTemplateParameter>\n" 6957 " &AnotherLongParameterName) {}"); 6958 verifyFormat("template <typename A>\n" 6959 "SomeLoooooooooooooooooooooongType<\n" 6960 " typename some_namespace::SomeOtherType<A>::Type>\n" 6961 "Function() {}"); 6962 6963 verifyGoogleFormat( 6964 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6965 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6966 verifyGoogleFormat( 6967 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6968 " SourceLocation L) {}"); 6969 verifyGoogleFormat( 6970 "some_namespace::LongReturnType\n" 6971 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6972 " int first_long_parameter, int second_parameter) {}"); 6973 6974 verifyGoogleFormat("template <typename T>\n" 6975 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6976 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6977 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6978 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6979 6980 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6981 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6982 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6983 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6984 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6985 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6986 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6987 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6990 } 6991 6992 TEST_F(FormatTest, FormatsArrays) { 6993 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6994 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6995 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6996 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6997 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6998 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6999 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7000 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7001 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7002 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 7003 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7004 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7005 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7006 verifyFormat( 7007 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 7008 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7009 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7010 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 7011 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7012 7013 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 7014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 7015 verifyFormat( 7016 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 7017 " .aaaaaaa[0]\n" 7018 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7019 verifyFormat("a[::b::c];"); 7020 7021 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 7022 7023 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 7024 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 7025 } 7026 7027 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 7028 verifyFormat("(a)->b();"); 7029 verifyFormat("--a;"); 7030 } 7031 7032 TEST_F(FormatTest, HandlesIncludeDirectives) { 7033 verifyFormat("#include <string>\n" 7034 "#include <a/b/c.h>\n" 7035 "#include \"a/b/string\"\n" 7036 "#include \"string.h\"\n" 7037 "#include \"string.h\"\n" 7038 "#include <a-a>\n" 7039 "#include < path with space >\n" 7040 "#include_next <test.h>" 7041 "#include \"abc.h\" // this is included for ABC\n" 7042 "#include \"some long include\" // with a comment\n" 7043 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 7044 getLLVMStyleWithColumns(35)); 7045 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 7046 EXPECT_EQ("#include <a>", format("#include<a>")); 7047 7048 verifyFormat("#import <string>"); 7049 verifyFormat("#import <a/b/c.h>"); 7050 verifyFormat("#import \"a/b/string\""); 7051 verifyFormat("#import \"string.h\""); 7052 verifyFormat("#import \"string.h\""); 7053 verifyFormat("#if __has_include(<strstream>)\n" 7054 "#include <strstream>\n" 7055 "#endif"); 7056 7057 verifyFormat("#define MY_IMPORT <a/b>"); 7058 7059 // Protocol buffer definition or missing "#". 7060 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 7061 getLLVMStyleWithColumns(30)); 7062 7063 FormatStyle Style = getLLVMStyle(); 7064 Style.AlwaysBreakBeforeMultilineStrings = true; 7065 Style.ColumnLimit = 0; 7066 verifyFormat("#import \"abc.h\"", Style); 7067 7068 // But 'import' might also be a regular C++ namespace. 7069 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7071 } 7072 7073 //===----------------------------------------------------------------------===// 7074 // Error recovery tests. 7075 //===----------------------------------------------------------------------===// 7076 7077 TEST_F(FormatTest, IncompleteParameterLists) { 7078 FormatStyle NoBinPacking = getLLVMStyle(); 7079 NoBinPacking.BinPackParameters = false; 7080 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 7081 " double *min_x,\n" 7082 " double *max_x,\n" 7083 " double *min_y,\n" 7084 " double *max_y,\n" 7085 " double *min_z,\n" 7086 " double *max_z, ) {}", 7087 NoBinPacking); 7088 } 7089 7090 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 7091 verifyFormat("void f() { return; }\n42"); 7092 verifyFormat("void f() {\n" 7093 " if (0)\n" 7094 " return;\n" 7095 "}\n" 7096 "42"); 7097 verifyFormat("void f() { return }\n42"); 7098 verifyFormat("void f() {\n" 7099 " if (0)\n" 7100 " return\n" 7101 "}\n" 7102 "42"); 7103 } 7104 7105 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 7106 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 7107 EXPECT_EQ("void f() {\n" 7108 " if (a)\n" 7109 " return\n" 7110 "}", 7111 format("void f ( ) { if ( a ) return }")); 7112 EXPECT_EQ("namespace N {\n" 7113 "void f()\n" 7114 "}", 7115 format("namespace N { void f() }")); 7116 EXPECT_EQ("namespace N {\n" 7117 "void f() {}\n" 7118 "void g()\n" 7119 "}", 7120 format("namespace N { void f( ) { } void g( ) }")); 7121 } 7122 7123 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 7124 verifyFormat("int aaaaaaaa =\n" 7125 " // Overlylongcomment\n" 7126 " b;", 7127 getLLVMStyleWithColumns(20)); 7128 verifyFormat("function(\n" 7129 " ShortArgument,\n" 7130 " LoooooooooooongArgument);\n", 7131 getLLVMStyleWithColumns(20)); 7132 } 7133 7134 TEST_F(FormatTest, IncorrectAccessSpecifier) { 7135 verifyFormat("public:"); 7136 verifyFormat("class A {\n" 7137 "public\n" 7138 " void f() {}\n" 7139 "};"); 7140 verifyFormat("public\n" 7141 "int qwerty;"); 7142 verifyFormat("public\n" 7143 "B {}"); 7144 verifyFormat("public\n" 7145 "{}"); 7146 verifyFormat("public\n" 7147 "B { int x; }"); 7148 } 7149 7150 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 7151 verifyFormat("{"); 7152 verifyFormat("#})"); 7153 verifyNoCrash("(/**/[:!] ?[)."); 7154 } 7155 7156 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 7157 verifyFormat("do {\n}"); 7158 verifyFormat("do {\n}\n" 7159 "f();"); 7160 verifyFormat("do {\n}\n" 7161 "wheeee(fun);"); 7162 verifyFormat("do {\n" 7163 " f();\n" 7164 "}"); 7165 } 7166 7167 TEST_F(FormatTest, IncorrectCodeMissingParens) { 7168 verifyFormat("if {\n foo;\n foo();\n}"); 7169 verifyFormat("switch {\n foo;\n foo();\n}"); 7170 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 7171 verifyFormat("while {\n foo;\n foo();\n}"); 7172 verifyFormat("do {\n foo;\n foo();\n} while;"); 7173 } 7174 7175 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7176 verifyIncompleteFormat("namespace {\n" 7177 "class Foo { Foo (\n" 7178 "};\n" 7179 "} // comment"); 7180 } 7181 7182 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7183 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7184 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7185 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7186 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7187 7188 EXPECT_EQ("{\n" 7189 " {\n" 7190 " breakme(\n" 7191 " qwe);\n" 7192 " }\n", 7193 format("{\n" 7194 " {\n" 7195 " breakme(qwe);\n" 7196 "}\n", 7197 getLLVMStyleWithColumns(10))); 7198 } 7199 7200 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7201 verifyFormat("int x = {\n" 7202 " avariable,\n" 7203 " b(alongervariable)};", 7204 getLLVMStyleWithColumns(25)); 7205 } 7206 7207 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7208 verifyFormat("return (a)(b){1, 2, 3};"); 7209 } 7210 7211 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7212 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7213 verifyFormat("vector<int> x{\n" 7214 " 1, 2, 3, 4,\n" 7215 "};"); 7216 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7217 verifyFormat("f({1, 2});"); 7218 verifyFormat("auto v = Foo{-1};"); 7219 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7220 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7221 verifyFormat("new vector<int>{1, 2, 3};"); 7222 verifyFormat("new int[3]{1, 2, 3};"); 7223 verifyFormat("new int{1};"); 7224 verifyFormat("return {arg1, arg2};"); 7225 verifyFormat("return {arg1, SomeType{parameter}};"); 7226 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7227 verifyFormat("new T{arg1, arg2};"); 7228 verifyFormat("f(MyMap[{composite, key}]);"); 7229 verifyFormat("class Class {\n" 7230 " T member = {arg1, arg2};\n" 7231 "};"); 7232 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7233 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7234 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7235 7236 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7237 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7238 verifyFormat("auto i = decltype(x){};"); 7239 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7240 verifyFormat("Node n{1, Node{1000}, //\n" 7241 " 2};"); 7242 verifyFormat("Aaaa aaaaaaa{\n" 7243 " {\n" 7244 " aaaa,\n" 7245 " },\n" 7246 "};"); 7247 verifyFormat("class C : public D {\n" 7248 " SomeClass SC{2};\n" 7249 "};"); 7250 verifyFormat("class C : public A {\n" 7251 " class D : public B {\n" 7252 " void f() { int i{2}; }\n" 7253 " };\n" 7254 "};"); 7255 verifyFormat("#define A {a, a},"); 7256 7257 // Cases where distinguising braced lists and blocks is hard. 7258 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7259 verifyFormat("void f() {\n" 7260 " return; // comment\n" 7261 "}\n" 7262 "SomeType t;"); 7263 verifyFormat("void f() {\n" 7264 " if (a) {\n" 7265 " f();\n" 7266 " }\n" 7267 "}\n" 7268 "SomeType t;"); 7269 7270 // In combination with BinPackArguments = false. 7271 FormatStyle NoBinPacking = getLLVMStyle(); 7272 NoBinPacking.BinPackArguments = false; 7273 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7274 " bbbbb,\n" 7275 " ccccc,\n" 7276 " ddddd,\n" 7277 " eeeee,\n" 7278 " ffffff,\n" 7279 " ggggg,\n" 7280 " hhhhhh,\n" 7281 " iiiiii,\n" 7282 " jjjjjj,\n" 7283 " kkkkkk};", 7284 NoBinPacking); 7285 verifyFormat("const Aaaaaa aaaaa = {\n" 7286 " aaaaa,\n" 7287 " bbbbb,\n" 7288 " ccccc,\n" 7289 " ddddd,\n" 7290 " eeeee,\n" 7291 " ffffff,\n" 7292 " ggggg,\n" 7293 " hhhhhh,\n" 7294 " iiiiii,\n" 7295 " jjjjjj,\n" 7296 " kkkkkk,\n" 7297 "};", 7298 NoBinPacking); 7299 verifyFormat( 7300 "const Aaaaaa aaaaa = {\n" 7301 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7302 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7303 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7304 "};", 7305 NoBinPacking); 7306 7307 // FIXME: The alignment of these trailing comments might be bad. Then again, 7308 // this might be utterly useless in real code. 7309 verifyFormat("Constructor::Constructor()\n" 7310 " : some_value{ //\n" 7311 " aaaaaaa, //\n" 7312 " bbbbbbb} {}"); 7313 7314 // In braced lists, the first comment is always assumed to belong to the 7315 // first element. Thus, it can be moved to the next or previous line as 7316 // appropriate. 7317 EXPECT_EQ("function({// First element:\n" 7318 " 1,\n" 7319 " // Second element:\n" 7320 " 2});", 7321 format("function({\n" 7322 " // First element:\n" 7323 " 1,\n" 7324 " // Second element:\n" 7325 " 2});")); 7326 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7327 " // First element:\n" 7328 " 1,\n" 7329 " // Second element:\n" 7330 " 2};", 7331 format("std::vector<int> MyNumbers{// First element:\n" 7332 " 1,\n" 7333 " // Second element:\n" 7334 " 2};", 7335 getLLVMStyleWithColumns(30))); 7336 // A trailing comma should still lead to an enforced line break. 7337 EXPECT_EQ("vector<int> SomeVector = {\n" 7338 " // aaa\n" 7339 " 1, 2,\n" 7340 "};", 7341 format("vector<int> SomeVector = { // aaa\n" 7342 " 1, 2, };")); 7343 7344 FormatStyle ExtraSpaces = getLLVMStyle(); 7345 ExtraSpaces.Cpp11BracedListStyle = false; 7346 ExtraSpaces.ColumnLimit = 75; 7347 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7348 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7349 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7350 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7351 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7352 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7353 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7354 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7355 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7356 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7357 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7358 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7359 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7360 verifyFormat("class Class {\n" 7361 " T member = { arg1, arg2 };\n" 7362 "};", 7363 ExtraSpaces); 7364 verifyFormat( 7365 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7366 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7367 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7368 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7369 ExtraSpaces); 7370 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7371 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7372 ExtraSpaces); 7373 verifyFormat( 7374 "someFunction(OtherParam,\n" 7375 " BracedList{ // comment 1 (Forcing interesting break)\n" 7376 " param1, param2,\n" 7377 " // comment 2\n" 7378 " param3, param4 });", 7379 ExtraSpaces); 7380 verifyFormat( 7381 "std::this_thread::sleep_for(\n" 7382 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7383 ExtraSpaces); 7384 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7385 " aaaaaaa,\n" 7386 " aaaaaaaaaa,\n" 7387 " aaaaa,\n" 7388 " aaaaaaaaaaaaaaa,\n" 7389 " aaa,\n" 7390 " aaaaaaaaaa,\n" 7391 " a,\n" 7392 " aaaaaaaaaaaaaaaaaaaaa,\n" 7393 " aaaaaaaaaaaa,\n" 7394 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7395 " aaaaaaa,\n" 7396 " a};"); 7397 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7398 } 7399 7400 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7401 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7402 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7403 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7404 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7405 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7406 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7407 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7408 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7409 " 1, 22, 333, 4444, 55555, //\n" 7410 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7411 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7412 verifyFormat( 7413 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7414 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7415 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7416 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7417 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7418 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7419 " 7777777};"); 7420 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7421 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7422 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7423 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7424 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7425 " // Separating comment.\n" 7426 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7427 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7428 " // Leading comment\n" 7429 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7430 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7431 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7432 " 1, 1, 1, 1};", 7433 getLLVMStyleWithColumns(39)); 7434 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7435 " 1, 1, 1, 1};", 7436 getLLVMStyleWithColumns(38)); 7437 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7438 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7439 getLLVMStyleWithColumns(43)); 7440 verifyFormat( 7441 "static unsigned SomeValues[10][3] = {\n" 7442 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7443 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7444 verifyFormat("static auto fields = new vector<string>{\n" 7445 " \"aaaaaaaaaaaaa\",\n" 7446 " \"aaaaaaaaaaaaa\",\n" 7447 " \"aaaaaaaaaaaa\",\n" 7448 " \"aaaaaaaaaaaaaa\",\n" 7449 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7450 " \"aaaaaaaaaaaa\",\n" 7451 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7452 "};"); 7453 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7454 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7455 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7456 " 3, cccccccccccccccccccccc};", 7457 getLLVMStyleWithColumns(60)); 7458 7459 // Trailing commas. 7460 verifyFormat("vector<int> x = {\n" 7461 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 7462 "};", 7463 getLLVMStyleWithColumns(39)); 7464 verifyFormat("vector<int> x = {\n" 7465 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 7466 "};", 7467 getLLVMStyleWithColumns(39)); 7468 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7469 " 1, 1, 1, 1,\n" 7470 " /**/ /**/};", 7471 getLLVMStyleWithColumns(39)); 7472 7473 // Trailing comment in the first line. 7474 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 7475 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 7476 " 111111111, 222222222, 3333333333, 444444444, //\n" 7477 " 11111111, 22222222, 333333333, 44444444};"); 7478 // Trailing comment in the last line. 7479 verifyFormat("int aaaaa[] = {\n" 7480 " 1, 2, 3, // comment\n" 7481 " 4, 5, 6 // comment\n" 7482 "};"); 7483 7484 // With nested lists, we should either format one item per line or all nested 7485 // lists one on line. 7486 // FIXME: For some nested lists, we can do better. 7487 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 7488 " {aaaaaaaaaaaaaaaaaaa},\n" 7489 " {aaaaaaaaaaaaaaaaaaaaa},\n" 7490 " {aaaaaaaaaaaaaaaaa}};", 7491 getLLVMStyleWithColumns(60)); 7492 verifyFormat( 7493 "SomeStruct my_struct_array = {\n" 7494 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 7495 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 7496 " {aaa, aaa},\n" 7497 " {aaa, aaa},\n" 7498 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 7499 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7500 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 7501 7502 // No column layout should be used here. 7503 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 7504 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 7505 7506 verifyNoCrash("a<,"); 7507 7508 // No braced initializer here. 7509 verifyFormat("void f() {\n" 7510 " struct Dummy {};\n" 7511 " f(v);\n" 7512 "}"); 7513 7514 // Long lists should be formatted in columns even if they are nested. 7515 verifyFormat( 7516 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7517 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7518 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7519 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7520 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7521 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 7522 7523 // Allow "single-column" layout even if that violates the column limit. There 7524 // isn't going to be a better way. 7525 verifyFormat("std::vector<int> a = {\n" 7526 " aaaaaaaa,\n" 7527 " aaaaaaaa,\n" 7528 " aaaaaaaa,\n" 7529 " aaaaaaaa,\n" 7530 " aaaaaaaaaa,\n" 7531 " aaaaaaaa,\n" 7532 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 7533 getLLVMStyleWithColumns(30)); 7534 verifyFormat("vector<int> aaaa = {\n" 7535 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7536 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7537 " aaaaaa.aaaaaaa,\n" 7538 " aaaaaa.aaaaaaa,\n" 7539 " aaaaaa.aaaaaaa,\n" 7540 " aaaaaa.aaaaaaa,\n" 7541 "};"); 7542 7543 // Don't create hanging lists. 7544 verifyFormat("someFunction(Param, {List1, List2,\n" 7545 " List3});", 7546 getLLVMStyleWithColumns(35)); 7547 verifyFormat("someFunction(Param, Param,\n" 7548 " {List1, List2,\n" 7549 " List3});", 7550 getLLVMStyleWithColumns(35)); 7551 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 7552 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7553 } 7554 7555 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7556 FormatStyle DoNotMerge = getLLVMStyle(); 7557 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7558 7559 verifyFormat("void f() { return 42; }"); 7560 verifyFormat("void f() {\n" 7561 " return 42;\n" 7562 "}", 7563 DoNotMerge); 7564 verifyFormat("void f() {\n" 7565 " // Comment\n" 7566 "}"); 7567 verifyFormat("{\n" 7568 "#error {\n" 7569 " int a;\n" 7570 "}"); 7571 verifyFormat("{\n" 7572 " int a;\n" 7573 "#error {\n" 7574 "}"); 7575 verifyFormat("void f() {} // comment"); 7576 verifyFormat("void f() { int a; } // comment"); 7577 verifyFormat("void f() {\n" 7578 "} // comment", 7579 DoNotMerge); 7580 verifyFormat("void f() {\n" 7581 " int a;\n" 7582 "} // comment", 7583 DoNotMerge); 7584 verifyFormat("void f() {\n" 7585 "} // comment", 7586 getLLVMStyleWithColumns(15)); 7587 7588 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7589 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7590 7591 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7592 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7593 verifyFormat("class C {\n" 7594 " C()\n" 7595 " : iiiiiiii(nullptr),\n" 7596 " kkkkkkk(nullptr),\n" 7597 " mmmmmmm(nullptr),\n" 7598 " nnnnnnn(nullptr) {}\n" 7599 "};", 7600 getGoogleStyle()); 7601 7602 FormatStyle NoColumnLimit = getLLVMStyle(); 7603 NoColumnLimit.ColumnLimit = 0; 7604 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7605 EXPECT_EQ("class C {\n" 7606 " A() : b(0) {}\n" 7607 "};", 7608 format("class C{A():b(0){}};", NoColumnLimit)); 7609 EXPECT_EQ("A()\n" 7610 " : b(0) {\n" 7611 "}", 7612 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7613 7614 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7615 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7616 FormatStyle::SFS_None; 7617 EXPECT_EQ("A()\n" 7618 " : b(0) {\n" 7619 "}", 7620 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7621 EXPECT_EQ("A()\n" 7622 " : b(0) {\n" 7623 "}", 7624 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7625 7626 verifyFormat("#define A \\\n" 7627 " void f() { \\\n" 7628 " int i; \\\n" 7629 " }", 7630 getLLVMStyleWithColumns(20)); 7631 verifyFormat("#define A \\\n" 7632 " void f() { int i; }", 7633 getLLVMStyleWithColumns(21)); 7634 verifyFormat("#define A \\\n" 7635 " void f() { \\\n" 7636 " int i; \\\n" 7637 " } \\\n" 7638 " int j;", 7639 getLLVMStyleWithColumns(22)); 7640 verifyFormat("#define A \\\n" 7641 " void f() { int i; } \\\n" 7642 " int j;", 7643 getLLVMStyleWithColumns(23)); 7644 } 7645 7646 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7647 FormatStyle MergeInlineOnly = getLLVMStyle(); 7648 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7649 verifyFormat("class C {\n" 7650 " int f() { return 42; }\n" 7651 "};", 7652 MergeInlineOnly); 7653 verifyFormat("int f() {\n" 7654 " return 42;\n" 7655 "}", 7656 MergeInlineOnly); 7657 } 7658 7659 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7660 // Elaborate type variable declarations. 7661 verifyFormat("struct foo a = {bar};\nint n;"); 7662 verifyFormat("class foo a = {bar};\nint n;"); 7663 verifyFormat("union foo a = {bar};\nint n;"); 7664 7665 // Elaborate types inside function definitions. 7666 verifyFormat("struct foo f() {}\nint n;"); 7667 verifyFormat("class foo f() {}\nint n;"); 7668 verifyFormat("union foo f() {}\nint n;"); 7669 7670 // Templates. 7671 verifyFormat("template <class X> void f() {}\nint n;"); 7672 verifyFormat("template <struct X> void f() {}\nint n;"); 7673 verifyFormat("template <union X> void f() {}\nint n;"); 7674 7675 // Actual definitions... 7676 verifyFormat("struct {\n} n;"); 7677 verifyFormat( 7678 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7679 verifyFormat("union Z {\n int n;\n} x;"); 7680 verifyFormat("class MACRO Z {\n} n;"); 7681 verifyFormat("class MACRO(X) Z {\n} n;"); 7682 verifyFormat("class __attribute__(X) Z {\n} n;"); 7683 verifyFormat("class __declspec(X) Z {\n} n;"); 7684 verifyFormat("class A##B##C {\n} n;"); 7685 verifyFormat("class alignas(16) Z {\n} n;"); 7686 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7687 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7688 7689 // Redefinition from nested context: 7690 verifyFormat("class A::B::C {\n} n;"); 7691 7692 // Template definitions. 7693 verifyFormat( 7694 "template <typename F>\n" 7695 "Matcher(const Matcher<F> &Other,\n" 7696 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7697 " !is_same<F, T>::value>::type * = 0)\n" 7698 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7699 7700 // FIXME: This is still incorrectly handled at the formatter side. 7701 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7702 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7703 7704 // FIXME: 7705 // This now gets parsed incorrectly as class definition. 7706 // verifyFormat("class A<int> f() {\n}\nint n;"); 7707 7708 // Elaborate types where incorrectly parsing the structural element would 7709 // break the indent. 7710 verifyFormat("if (true)\n" 7711 " class X x;\n" 7712 "else\n" 7713 " f();\n"); 7714 7715 // This is simply incomplete. Formatting is not important, but must not crash. 7716 verifyFormat("class A:"); 7717 } 7718 7719 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7720 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7721 format("#error Leave all white!!!!! space* alone!\n")); 7722 EXPECT_EQ( 7723 "#warning Leave all white!!!!! space* alone!\n", 7724 format("#warning Leave all white!!!!! space* alone!\n")); 7725 EXPECT_EQ("#error 1", format(" # error 1")); 7726 EXPECT_EQ("#warning 1", format(" # warning 1")); 7727 } 7728 7729 TEST_F(FormatTest, FormatHashIfExpressions) { 7730 verifyFormat("#if AAAA && BBBB"); 7731 verifyFormat("#if (AAAA && BBBB)"); 7732 verifyFormat("#elif (AAAA && BBBB)"); 7733 // FIXME: Come up with a better indentation for #elif. 7734 verifyFormat( 7735 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7736 " defined(BBBBBBBB)\n" 7737 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7738 " defined(BBBBBBBB)\n" 7739 "#endif", 7740 getLLVMStyleWithColumns(65)); 7741 } 7742 7743 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7744 FormatStyle AllowsMergedIf = getGoogleStyle(); 7745 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7746 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7747 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7748 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7749 EXPECT_EQ("if (true) return 42;", 7750 format("if (true)\nreturn 42;", AllowsMergedIf)); 7751 FormatStyle ShortMergedIf = AllowsMergedIf; 7752 ShortMergedIf.ColumnLimit = 25; 7753 verifyFormat("#define A \\\n" 7754 " if (true) return 42;", 7755 ShortMergedIf); 7756 verifyFormat("#define A \\\n" 7757 " f(); \\\n" 7758 " if (true)\n" 7759 "#define B", 7760 ShortMergedIf); 7761 verifyFormat("#define A \\\n" 7762 " f(); \\\n" 7763 " if (true)\n" 7764 "g();", 7765 ShortMergedIf); 7766 verifyFormat("{\n" 7767 "#ifdef A\n" 7768 " // Comment\n" 7769 " if (true) continue;\n" 7770 "#endif\n" 7771 " // Comment\n" 7772 " if (true) continue;\n" 7773 "}", 7774 ShortMergedIf); 7775 ShortMergedIf.ColumnLimit = 29; 7776 verifyFormat("#define A \\\n" 7777 " if (aaaaaaaaaa) return 1; \\\n" 7778 " return 2;", 7779 ShortMergedIf); 7780 ShortMergedIf.ColumnLimit = 28; 7781 verifyFormat("#define A \\\n" 7782 " if (aaaaaaaaaa) \\\n" 7783 " return 1; \\\n" 7784 " return 2;", 7785 ShortMergedIf); 7786 } 7787 7788 TEST_F(FormatTest, BlockCommentsInControlLoops) { 7789 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7790 " f();\n" 7791 "}"); 7792 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7793 " f();\n" 7794 "} /* another comment */ else /* comment #3 */ {\n" 7795 " g();\n" 7796 "}"); 7797 verifyFormat("while (0) /* a comment in a strange place */ {\n" 7798 " f();\n" 7799 "}"); 7800 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 7801 " f();\n" 7802 "}"); 7803 verifyFormat("do /* a comment in a strange place */ {\n" 7804 " f();\n" 7805 "} /* another comment */ while (0);"); 7806 } 7807 7808 TEST_F(FormatTest, BlockComments) { 7809 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 7810 format("/* *//* */ /* */\n/* *//* */ /* */")); 7811 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 7812 EXPECT_EQ("#define A /*123*/ \\\n" 7813 " b\n" 7814 "/* */\n" 7815 "someCall(\n" 7816 " parameter);", 7817 format("#define A /*123*/ b\n" 7818 "/* */\n" 7819 "someCall(parameter);", 7820 getLLVMStyleWithColumns(15))); 7821 7822 EXPECT_EQ("#define A\n" 7823 "/* */ someCall(\n" 7824 " parameter);", 7825 format("#define A\n" 7826 "/* */someCall(parameter);", 7827 getLLVMStyleWithColumns(15))); 7828 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7829 EXPECT_EQ("/*\n" 7830 "*\n" 7831 " * aaaaaa\n" 7832 " * aaaaaa\n" 7833 "*/", 7834 format("/*\n" 7835 "*\n" 7836 " * aaaaaa aaaaaa\n" 7837 "*/", 7838 getLLVMStyleWithColumns(10))); 7839 EXPECT_EQ("/*\n" 7840 "**\n" 7841 "* aaaaaa\n" 7842 "*aaaaaa\n" 7843 "*/", 7844 format("/*\n" 7845 "**\n" 7846 "* aaaaaa aaaaaa\n" 7847 "*/", 7848 getLLVMStyleWithColumns(10))); 7849 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7850 " /* line 1\n" 7851 " bbbbbbbbbbbb */\n" 7852 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7853 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7854 " /* line 1\n" 7855 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7856 getLLVMStyleWithColumns(50))); 7857 7858 FormatStyle NoBinPacking = getLLVMStyle(); 7859 NoBinPacking.BinPackParameters = false; 7860 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7861 " 2, /* comment 2 */\n" 7862 " 3, /* comment 3 */\n" 7863 " aaaa,\n" 7864 " bbbb);", 7865 format("someFunction (1, /* comment 1 */\n" 7866 " 2, /* comment 2 */ \n" 7867 " 3, /* comment 3 */\n" 7868 "aaaa, bbbb );", 7869 NoBinPacking)); 7870 verifyFormat( 7871 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7873 EXPECT_EQ( 7874 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7875 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7877 format( 7878 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7879 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7881 EXPECT_EQ( 7882 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7883 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7884 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7885 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7886 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7887 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7888 7889 verifyFormat("void f(int * /* unused */) {}"); 7890 7891 EXPECT_EQ("/*\n" 7892 " **\n" 7893 " */", 7894 format("/*\n" 7895 " **\n" 7896 " */")); 7897 EXPECT_EQ("/*\n" 7898 " *q\n" 7899 " */", 7900 format("/*\n" 7901 " *q\n" 7902 " */")); 7903 EXPECT_EQ("/*\n" 7904 " * q\n" 7905 " */", 7906 format("/*\n" 7907 " * q\n" 7908 " */")); 7909 EXPECT_EQ("/*\n" 7910 " **/", 7911 format("/*\n" 7912 " **/")); 7913 EXPECT_EQ("/*\n" 7914 " ***/", 7915 format("/*\n" 7916 " ***/")); 7917 } 7918 7919 TEST_F(FormatTest, BlockCommentsInMacros) { 7920 EXPECT_EQ("#define A \\\n" 7921 " { \\\n" 7922 " /* one line */ \\\n" 7923 " someCall();", 7924 format("#define A { \\\n" 7925 " /* one line */ \\\n" 7926 " someCall();", 7927 getLLVMStyleWithColumns(20))); 7928 EXPECT_EQ("#define A \\\n" 7929 " { \\\n" 7930 " /* previous */ \\\n" 7931 " /* one line */ \\\n" 7932 " someCall();", 7933 format("#define A { \\\n" 7934 " /* previous */ \\\n" 7935 " /* one line */ \\\n" 7936 " someCall();", 7937 getLLVMStyleWithColumns(20))); 7938 } 7939 7940 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7941 EXPECT_EQ("a = {\n" 7942 " 1111 /* */\n" 7943 "};", 7944 format("a = {1111 /* */\n" 7945 "};", 7946 getLLVMStyleWithColumns(15))); 7947 EXPECT_EQ("a = {\n" 7948 " 1111 /* */\n" 7949 "};", 7950 format("a = {1111 /* */\n" 7951 "};", 7952 getLLVMStyleWithColumns(15))); 7953 EXPECT_EQ("a = {\n" 7954 " 1111 /* a\n" 7955 " */\n" 7956 "};", 7957 format("a = {1111 /* a */\n" 7958 "};", 7959 getLLVMStyleWithColumns(15))); 7960 } 7961 7962 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7963 verifyFormat("{\n" 7964 " // a\n" 7965 " // b"); 7966 } 7967 7968 TEST_F(FormatTest, FormatStarDependingOnContext) { 7969 verifyFormat("void f(int *a);"); 7970 verifyFormat("void f() { f(fint * b); }"); 7971 verifyFormat("class A {\n void f(int *a);\n};"); 7972 verifyFormat("class A {\n int *a;\n};"); 7973 verifyFormat("namespace a {\n" 7974 "namespace b {\n" 7975 "class A {\n" 7976 " void f() {}\n" 7977 " int *a;\n" 7978 "};\n" 7979 "}\n" 7980 "}"); 7981 } 7982 7983 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7984 verifyFormat("while"); 7985 verifyFormat("operator"); 7986 } 7987 7988 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7989 // This code would be painfully slow to format if we didn't skip it. 7990 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 7991 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7992 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7993 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7994 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7995 "A(1, 1)\n" 7996 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7997 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7998 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7999 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8000 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8001 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8002 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8003 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8004 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8005 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 8006 // Deeply nested part is untouched, rest is formatted. 8007 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8008 format(std::string("int i;\n") + Code + "int j;\n", 8009 getLLVMStyle(), IC_ExpectIncomplete)); 8010 } 8011 8012 //===----------------------------------------------------------------------===// 8013 // Objective-C tests. 8014 //===----------------------------------------------------------------------===// 8015 8016 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8017 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8018 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8019 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8020 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8021 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8022 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8023 format("-(NSInteger)Method3:(id)anObject;")); 8024 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8025 format("-(NSInteger)Method4:(id)anObject;")); 8026 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8027 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8028 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8029 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8030 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8031 "forAllCells:(BOOL)flag;", 8032 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8033 "forAllCells:(BOOL)flag;")); 8034 8035 // Very long objectiveC method declaration. 8036 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8037 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8038 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8039 " inRange:(NSRange)range\n" 8040 " outRange:(NSRange)out_range\n" 8041 " outRange1:(NSRange)out_range1\n" 8042 " outRange2:(NSRange)out_range2\n" 8043 " outRange3:(NSRange)out_range3\n" 8044 " outRange4:(NSRange)out_range4\n" 8045 " outRange5:(NSRange)out_range5\n" 8046 " outRange6:(NSRange)out_range6\n" 8047 " outRange7:(NSRange)out_range7\n" 8048 " outRange8:(NSRange)out_range8\n" 8049 " outRange9:(NSRange)out_range9;"); 8050 8051 // When the function name has to be wrapped. 8052 FormatStyle Style = getLLVMStyle(); 8053 Style.IndentWrappedFunctionNames = false; 8054 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8055 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8056 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8057 "}", 8058 Style); 8059 Style.IndentWrappedFunctionNames = true; 8060 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8061 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8062 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8063 "}", 8064 Style); 8065 8066 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8067 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8068 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8069 // protocol lists (but not for template classes): 8070 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8071 8072 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8073 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8074 8075 // If there's no return type (very rare in practice!), LLVM and Google style 8076 // agree. 8077 verifyFormat("- foo;"); 8078 verifyFormat("- foo:(int)f;"); 8079 verifyGoogleFormat("- foo:(int)foo;"); 8080 } 8081 8082 8083 TEST_F(FormatTest, BreaksStringLiterals) { 8084 EXPECT_EQ("\"some text \"\n" 8085 "\"other\";", 8086 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8087 EXPECT_EQ("\"some text \"\n" 8088 "\"other\";", 8089 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8090 EXPECT_EQ( 8091 "#define A \\\n" 8092 " \"some \" \\\n" 8093 " \"text \" \\\n" 8094 " \"other\";", 8095 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8096 EXPECT_EQ( 8097 "#define A \\\n" 8098 " \"so \" \\\n" 8099 " \"text \" \\\n" 8100 " \"other\";", 8101 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8102 8103 EXPECT_EQ("\"some text\"", 8104 format("\"some text\"", getLLVMStyleWithColumns(1))); 8105 EXPECT_EQ("\"some text\"", 8106 format("\"some text\"", getLLVMStyleWithColumns(11))); 8107 EXPECT_EQ("\"some \"\n" 8108 "\"text\"", 8109 format("\"some text\"", getLLVMStyleWithColumns(10))); 8110 EXPECT_EQ("\"some \"\n" 8111 "\"text\"", 8112 format("\"some text\"", getLLVMStyleWithColumns(7))); 8113 EXPECT_EQ("\"some\"\n" 8114 "\" tex\"\n" 8115 "\"t\"", 8116 format("\"some text\"", getLLVMStyleWithColumns(6))); 8117 EXPECT_EQ("\"some\"\n" 8118 "\" tex\"\n" 8119 "\" and\"", 8120 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8121 EXPECT_EQ("\"some\"\n" 8122 "\"/tex\"\n" 8123 "\"/and\"", 8124 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8125 8126 EXPECT_EQ("variable =\n" 8127 " \"long string \"\n" 8128 " \"literal\";", 8129 format("variable = \"long string literal\";", 8130 getLLVMStyleWithColumns(20))); 8131 8132 EXPECT_EQ("variable = f(\n" 8133 " \"long string \"\n" 8134 " \"literal\",\n" 8135 " short,\n" 8136 " loooooooooooooooooooong);", 8137 format("variable = f(\"long string literal\", short, " 8138 "loooooooooooooooooooong);", 8139 getLLVMStyleWithColumns(20))); 8140 8141 EXPECT_EQ( 8142 "f(g(\"long string \"\n" 8143 " \"literal\"),\n" 8144 " b);", 8145 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8146 EXPECT_EQ("f(g(\"long string \"\n" 8147 " \"literal\",\n" 8148 " a),\n" 8149 " b);", 8150 format("f(g(\"long string literal\", a), b);", 8151 getLLVMStyleWithColumns(20))); 8152 EXPECT_EQ( 8153 "f(\"one two\".split(\n" 8154 " variable));", 8155 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8156 EXPECT_EQ("f(\"one two three four five six \"\n" 8157 " \"seven\".split(\n" 8158 " really_looooong_variable));", 8159 format("f(\"one two three four five six seven\"." 8160 "split(really_looooong_variable));", 8161 getLLVMStyleWithColumns(33))); 8162 8163 EXPECT_EQ("f(\"some \"\n" 8164 " \"text\",\n" 8165 " other);", 8166 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8167 8168 // Only break as a last resort. 8169 verifyFormat( 8170 "aaaaaaaaaaaaaaaaaaaa(\n" 8171 " aaaaaaaaaaaaaaaaaaaa,\n" 8172 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8173 8174 EXPECT_EQ("\"splitmea\"\n" 8175 "\"trandomp\"\n" 8176 "\"oint\"", 8177 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8178 8179 EXPECT_EQ("\"split/\"\n" 8180 "\"pathat/\"\n" 8181 "\"slashes\"", 8182 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8183 8184 EXPECT_EQ("\"split/\"\n" 8185 "\"pathat/\"\n" 8186 "\"slashes\"", 8187 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8188 EXPECT_EQ("\"split at \"\n" 8189 "\"spaces/at/\"\n" 8190 "\"slashes.at.any$\"\n" 8191 "\"non-alphanumeric%\"\n" 8192 "\"1111111111characte\"\n" 8193 "\"rs\"", 8194 format("\"split at " 8195 "spaces/at/" 8196 "slashes.at." 8197 "any$non-" 8198 "alphanumeric%" 8199 "1111111111characte" 8200 "rs\"", 8201 getLLVMStyleWithColumns(20))); 8202 8203 // Verify that splitting the strings understands 8204 // Style::AlwaysBreakBeforeMultilineStrings. 8205 EXPECT_EQ( 8206 "aaaaaaaaaaaa(\n" 8207 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8208 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8209 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8210 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8211 "aaaaaaaaaaaaaaaaaaaaaa\");", 8212 getGoogleStyle())); 8213 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8214 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8215 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8216 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8217 "aaaaaaaaaaaaaaaaaaaaaa\";", 8218 getGoogleStyle())); 8219 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8220 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8221 format("llvm::outs() << " 8222 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8223 "aaaaaaaaaaaaaaaaaaa\";")); 8224 EXPECT_EQ("ffff(\n" 8225 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8226 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8227 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8228 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8229 getGoogleStyle())); 8230 8231 FormatStyle Style = getLLVMStyleWithColumns(12); 8232 Style.BreakStringLiterals = false; 8233 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8234 8235 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8236 AlignLeft.AlignEscapedNewlinesLeft = true; 8237 EXPECT_EQ("#define A \\\n" 8238 " \"some \" \\\n" 8239 " \"text \" \\\n" 8240 " \"other\";", 8241 format("#define A \"some text other\";", AlignLeft)); 8242 } 8243 8244 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8245 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8246 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8247 EXPECT_EQ("int i = a(b());", 8248 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8249 } 8250 8251 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8252 EXPECT_EQ( 8253 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8254 "(\n" 8255 " \"x\t\");", 8256 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8257 "aaaaaaa(" 8258 "\"x\t\");")); 8259 } 8260 8261 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8262 EXPECT_EQ( 8263 "u8\"utf8 string \"\n" 8264 "u8\"literal\";", 8265 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8266 EXPECT_EQ( 8267 "u\"utf16 string \"\n" 8268 "u\"literal\";", 8269 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8270 EXPECT_EQ( 8271 "U\"utf32 string \"\n" 8272 "U\"literal\";", 8273 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8274 EXPECT_EQ("L\"wide string \"\n" 8275 "L\"literal\";", 8276 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8277 EXPECT_EQ("@\"NSString \"\n" 8278 "@\"literal\";", 8279 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8280 8281 // This input makes clang-format try to split the incomplete unicode escape 8282 // sequence, which used to lead to a crasher. 8283 verifyNoCrash( 8284 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8285 getLLVMStyleWithColumns(60)); 8286 } 8287 8288 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8289 FormatStyle Style = getGoogleStyleWithColumns(15); 8290 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8291 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8292 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8293 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8294 EXPECT_EQ("u8R\"x(raw literal)x\";", 8295 format("u8R\"x(raw literal)x\";", Style)); 8296 } 8297 8298 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8299 FormatStyle Style = getLLVMStyleWithColumns(20); 8300 EXPECT_EQ( 8301 "_T(\"aaaaaaaaaaaaaa\")\n" 8302 "_T(\"aaaaaaaaaaaaaa\")\n" 8303 "_T(\"aaaaaaaaaaaa\")", 8304 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8305 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8306 " _T(\"aaaaaa\"),\n" 8307 " z);", 8308 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8309 8310 // FIXME: Handle embedded spaces in one iteration. 8311 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8312 // "_T(\"aaaaaaaaaaaaa\")\n" 8313 // "_T(\"aaaaaaaaaaaaa\")\n" 8314 // "_T(\"a\")", 8315 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8316 // getLLVMStyleWithColumns(20))); 8317 EXPECT_EQ( 8318 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8319 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8320 EXPECT_EQ("f(\n" 8321 "#if !TEST\n" 8322 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8323 "#endif\n" 8324 " );", 8325 format("f(\n" 8326 "#if !TEST\n" 8327 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8328 "#endif\n" 8329 ");")); 8330 EXPECT_EQ("f(\n" 8331 "\n" 8332 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8333 format("f(\n" 8334 "\n" 8335 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8336 } 8337 8338 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8339 EXPECT_EQ( 8340 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8343 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8346 } 8347 8348 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8349 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8350 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8351 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8352 "multiline raw string literal xxxxxxxxxxxxxx\n" 8353 ")x\",\n" 8354 " a),\n" 8355 " b);", 8356 format("fffffffffff(g(R\"x(\n" 8357 "multiline raw string literal xxxxxxxxxxxxxx\n" 8358 ")x\", a), b);", 8359 getGoogleStyleWithColumns(20))); 8360 EXPECT_EQ("fffffffffff(\n" 8361 " g(R\"x(qqq\n" 8362 "multiline raw string literal xxxxxxxxxxxxxx\n" 8363 ")x\",\n" 8364 " a),\n" 8365 " b);", 8366 format("fffffffffff(g(R\"x(qqq\n" 8367 "multiline raw string literal xxxxxxxxxxxxxx\n" 8368 ")x\", a), b);", 8369 getGoogleStyleWithColumns(20))); 8370 8371 EXPECT_EQ("fffffffffff(R\"x(\n" 8372 "multiline raw string literal xxxxxxxxxxxxxx\n" 8373 ")x\");", 8374 format("fffffffffff(R\"x(\n" 8375 "multiline raw string literal xxxxxxxxxxxxxx\n" 8376 ")x\");", 8377 getGoogleStyleWithColumns(20))); 8378 EXPECT_EQ("fffffffffff(R\"x(\n" 8379 "multiline raw string literal xxxxxxxxxxxxxx\n" 8380 ")x\" + bbbbbb);", 8381 format("fffffffffff(R\"x(\n" 8382 "multiline raw string literal xxxxxxxxxxxxxx\n" 8383 ")x\" + bbbbbb);", 8384 getGoogleStyleWithColumns(20))); 8385 EXPECT_EQ("fffffffffff(\n" 8386 " R\"x(\n" 8387 "multiline raw string literal xxxxxxxxxxxxxx\n" 8388 ")x\" +\n" 8389 " bbbbbb);", 8390 format("fffffffffff(\n" 8391 " R\"x(\n" 8392 "multiline raw string literal xxxxxxxxxxxxxx\n" 8393 ")x\" + bbbbbb);", 8394 getGoogleStyleWithColumns(20))); 8395 } 8396 8397 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8398 verifyFormat("string a = \"unterminated;"); 8399 EXPECT_EQ("function(\"unterminated,\n" 8400 " OtherParameter);", 8401 format("function( \"unterminated,\n" 8402 " OtherParameter);")); 8403 } 8404 8405 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8406 FormatStyle Style = getLLVMStyle(); 8407 Style.Standard = FormatStyle::LS_Cpp03; 8408 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8409 format("#define x(_a) printf(\"foo\"_a);", Style)); 8410 } 8411 8412 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8413 8414 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8415 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8416 " \"ddeeefff\");", 8417 format("someFunction(\"aaabbbcccdddeeefff\");", 8418 getLLVMStyleWithColumns(25))); 8419 EXPECT_EQ("someFunction1234567890(\n" 8420 " \"aaabbbcccdddeeefff\");", 8421 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8422 getLLVMStyleWithColumns(26))); 8423 EXPECT_EQ("someFunction1234567890(\n" 8424 " \"aaabbbcccdddeeeff\"\n" 8425 " \"f\");", 8426 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8427 getLLVMStyleWithColumns(25))); 8428 EXPECT_EQ("someFunction1234567890(\n" 8429 " \"aaabbbcccdddeeeff\"\n" 8430 " \"f\");", 8431 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8432 getLLVMStyleWithColumns(24))); 8433 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8434 " \"ddde \"\n" 8435 " \"efff\");", 8436 format("someFunction(\"aaabbbcc ddde efff\");", 8437 getLLVMStyleWithColumns(25))); 8438 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8439 " \"ddeeefff\");", 8440 format("someFunction(\"aaabbbccc ddeeefff\");", 8441 getLLVMStyleWithColumns(25))); 8442 EXPECT_EQ("someFunction1234567890(\n" 8443 " \"aaabb \"\n" 8444 " \"cccdddeeefff\");", 8445 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8446 getLLVMStyleWithColumns(25))); 8447 EXPECT_EQ("#define A \\\n" 8448 " string s = \\\n" 8449 " \"123456789\" \\\n" 8450 " \"0\"; \\\n" 8451 " int i;", 8452 format("#define A string s = \"1234567890\"; int i;", 8453 getLLVMStyleWithColumns(20))); 8454 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8455 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8456 " \"dddeeeff\"\n" 8457 " \"f\");", 8458 format("someFunction(\"aaabbbcc dddeeefff\");", 8459 getLLVMStyleWithColumns(25))); 8460 } 8461 8462 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8463 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8464 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8465 EXPECT_EQ("\"test\"\n" 8466 "\"\\n\"", 8467 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8468 EXPECT_EQ("\"tes\\\\\"\n" 8469 "\"n\"", 8470 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8471 EXPECT_EQ("\"\\\\\\\\\"\n" 8472 "\"\\n\"", 8473 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8474 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8475 EXPECT_EQ("\"\\uff01\"\n" 8476 "\"test\"", 8477 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8478 EXPECT_EQ("\"\\Uff01ff02\"", 8479 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8480 EXPECT_EQ("\"\\x000000000001\"\n" 8481 "\"next\"", 8482 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8483 EXPECT_EQ("\"\\x000000000001next\"", 8484 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8485 EXPECT_EQ("\"\\x000000000001\"", 8486 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8487 EXPECT_EQ("\"test\"\n" 8488 "\"\\000000\"\n" 8489 "\"000001\"", 8490 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8491 EXPECT_EQ("\"test\\000\"\n" 8492 "\"00000000\"\n" 8493 "\"1\"", 8494 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8495 } 8496 8497 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8498 verifyFormat("void f() {\n" 8499 " return g() {}\n" 8500 " void h() {}"); 8501 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8502 "g();\n" 8503 "}"); 8504 } 8505 8506 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8507 verifyFormat( 8508 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8509 } 8510 8511 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8512 verifyFormat("class X {\n" 8513 " void f() {\n" 8514 " }\n" 8515 "};", 8516 getLLVMStyleWithColumns(12)); 8517 } 8518 8519 TEST_F(FormatTest, ConfigurableIndentWidth) { 8520 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8521 EightIndent.IndentWidth = 8; 8522 EightIndent.ContinuationIndentWidth = 8; 8523 verifyFormat("void f() {\n" 8524 " someFunction();\n" 8525 " if (true) {\n" 8526 " f();\n" 8527 " }\n" 8528 "}", 8529 EightIndent); 8530 verifyFormat("class X {\n" 8531 " void f() {\n" 8532 " }\n" 8533 "};", 8534 EightIndent); 8535 verifyFormat("int x[] = {\n" 8536 " call(),\n" 8537 " call()};", 8538 EightIndent); 8539 } 8540 8541 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8542 verifyFormat("double\n" 8543 "f();", 8544 getLLVMStyleWithColumns(8)); 8545 } 8546 8547 TEST_F(FormatTest, ConfigurableUseOfTab) { 8548 FormatStyle Tab = getLLVMStyleWithColumns(42); 8549 Tab.IndentWidth = 8; 8550 Tab.UseTab = FormatStyle::UT_Always; 8551 Tab.AlignEscapedNewlinesLeft = true; 8552 8553 EXPECT_EQ("if (aaaaaaaa && // q\n" 8554 " bb)\t\t// w\n" 8555 "\t;", 8556 format("if (aaaaaaaa &&// q\n" 8557 "bb)// w\n" 8558 ";", 8559 Tab)); 8560 EXPECT_EQ("if (aaa && bbb) // w\n" 8561 "\t;", 8562 format("if(aaa&&bbb)// w\n" 8563 ";", 8564 Tab)); 8565 8566 verifyFormat("class X {\n" 8567 "\tvoid f() {\n" 8568 "\t\tsomeFunction(parameter1,\n" 8569 "\t\t\t parameter2);\n" 8570 "\t}\n" 8571 "};", 8572 Tab); 8573 verifyFormat("#define A \\\n" 8574 "\tvoid f() { \\\n" 8575 "\t\tsomeFunction( \\\n" 8576 "\t\t parameter1, \\\n" 8577 "\t\t parameter2); \\\n" 8578 "\t}", 8579 Tab); 8580 8581 Tab.TabWidth = 4; 8582 Tab.IndentWidth = 8; 8583 verifyFormat("class TabWidth4Indent8 {\n" 8584 "\t\tvoid f() {\n" 8585 "\t\t\t\tsomeFunction(parameter1,\n" 8586 "\t\t\t\t\t\t\t parameter2);\n" 8587 "\t\t}\n" 8588 "};", 8589 Tab); 8590 8591 Tab.TabWidth = 4; 8592 Tab.IndentWidth = 4; 8593 verifyFormat("class TabWidth4Indent4 {\n" 8594 "\tvoid f() {\n" 8595 "\t\tsomeFunction(parameter1,\n" 8596 "\t\t\t\t\t parameter2);\n" 8597 "\t}\n" 8598 "};", 8599 Tab); 8600 8601 Tab.TabWidth = 8; 8602 Tab.IndentWidth = 4; 8603 verifyFormat("class TabWidth8Indent4 {\n" 8604 " void f() {\n" 8605 "\tsomeFunction(parameter1,\n" 8606 "\t\t parameter2);\n" 8607 " }\n" 8608 "};", 8609 Tab); 8610 8611 Tab.TabWidth = 8; 8612 Tab.IndentWidth = 8; 8613 EXPECT_EQ("/*\n" 8614 "\t a\t\tcomment\n" 8615 "\t in multiple lines\n" 8616 " */", 8617 format(" /*\t \t \n" 8618 " \t \t a\t\tcomment\t \t\n" 8619 " \t \t in multiple lines\t\n" 8620 " \t */", 8621 Tab)); 8622 8623 Tab.UseTab = FormatStyle::UT_ForIndentation; 8624 verifyFormat("{\n" 8625 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8626 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8627 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8628 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8629 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8630 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8631 "};", 8632 Tab); 8633 verifyFormat("enum AA {\n" 8634 "\ta1, // Force multiple lines\n" 8635 "\ta2,\n" 8636 "\ta3\n" 8637 "};", 8638 Tab); 8639 EXPECT_EQ("if (aaaaaaaa && // q\n" 8640 " bb) // w\n" 8641 "\t;", 8642 format("if (aaaaaaaa &&// q\n" 8643 "bb)// w\n" 8644 ";", 8645 Tab)); 8646 verifyFormat("class X {\n" 8647 "\tvoid f() {\n" 8648 "\t\tsomeFunction(parameter1,\n" 8649 "\t\t parameter2);\n" 8650 "\t}\n" 8651 "};", 8652 Tab); 8653 verifyFormat("{\n" 8654 "\tQ(\n" 8655 "\t {\n" 8656 "\t\t int a;\n" 8657 "\t\t someFunction(aaaaaaaa,\n" 8658 "\t\t bbbbbbb);\n" 8659 "\t },\n" 8660 "\t p);\n" 8661 "}", 8662 Tab); 8663 EXPECT_EQ("{\n" 8664 "\t/* aaaa\n" 8665 "\t bbbb */\n" 8666 "}", 8667 format("{\n" 8668 "/* aaaa\n" 8669 " bbbb */\n" 8670 "}", 8671 Tab)); 8672 EXPECT_EQ("{\n" 8673 "\t/*\n" 8674 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8675 "\t bbbbbbbbbbbbb\n" 8676 "\t*/\n" 8677 "}", 8678 format("{\n" 8679 "/*\n" 8680 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8681 "*/\n" 8682 "}", 8683 Tab)); 8684 EXPECT_EQ("{\n" 8685 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8686 "\t// bbbbbbbbbbbbb\n" 8687 "}", 8688 format("{\n" 8689 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8690 "}", 8691 Tab)); 8692 EXPECT_EQ("{\n" 8693 "\t/*\n" 8694 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8695 "\t bbbbbbbbbbbbb\n" 8696 "\t*/\n" 8697 "}", 8698 format("{\n" 8699 "\t/*\n" 8700 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8701 "\t*/\n" 8702 "}", 8703 Tab)); 8704 EXPECT_EQ("{\n" 8705 "\t/*\n" 8706 "\n" 8707 "\t*/\n" 8708 "}", 8709 format("{\n" 8710 "\t/*\n" 8711 "\n" 8712 "\t*/\n" 8713 "}", 8714 Tab)); 8715 EXPECT_EQ("{\n" 8716 "\t/*\n" 8717 " asdf\n" 8718 "\t*/\n" 8719 "}", 8720 format("{\n" 8721 "\t/*\n" 8722 " asdf\n" 8723 "\t*/\n" 8724 "}", 8725 Tab)); 8726 8727 Tab.UseTab = FormatStyle::UT_Never; 8728 EXPECT_EQ("/*\n" 8729 " a\t\tcomment\n" 8730 " in multiple lines\n" 8731 " */", 8732 format(" /*\t \t \n" 8733 " \t \t a\t\tcomment\t \t\n" 8734 " \t \t in multiple lines\t\n" 8735 " \t */", 8736 Tab)); 8737 EXPECT_EQ("/* some\n" 8738 " comment */", 8739 format(" \t \t /* some\n" 8740 " \t \t comment */", 8741 Tab)); 8742 EXPECT_EQ("int a; /* some\n" 8743 " comment */", 8744 format(" \t \t int a; /* some\n" 8745 " \t \t comment */", 8746 Tab)); 8747 8748 EXPECT_EQ("int a; /* some\n" 8749 "comment */", 8750 format(" \t \t int\ta; /* some\n" 8751 " \t \t comment */", 8752 Tab)); 8753 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8754 " comment */", 8755 format(" \t \t f(\"\t\t\"); /* some\n" 8756 " \t \t comment */", 8757 Tab)); 8758 EXPECT_EQ("{\n" 8759 " /*\n" 8760 " * Comment\n" 8761 " */\n" 8762 " int i;\n" 8763 "}", 8764 format("{\n" 8765 "\t/*\n" 8766 "\t * Comment\n" 8767 "\t */\n" 8768 "\t int i;\n" 8769 "}")); 8770 8771 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8772 Tab.TabWidth = 8; 8773 Tab.IndentWidth = 8; 8774 EXPECT_EQ("if (aaaaaaaa && // q\n" 8775 " bb) // w\n" 8776 "\t;", 8777 format("if (aaaaaaaa &&// q\n" 8778 "bb)// w\n" 8779 ";", 8780 Tab)); 8781 EXPECT_EQ("if (aaa && bbb) // w\n" 8782 "\t;", 8783 format("if(aaa&&bbb)// w\n" 8784 ";", 8785 Tab)); 8786 verifyFormat("class X {\n" 8787 "\tvoid f() {\n" 8788 "\t\tsomeFunction(parameter1,\n" 8789 "\t\t\t parameter2);\n" 8790 "\t}\n" 8791 "};", 8792 Tab); 8793 verifyFormat("#define A \\\n" 8794 "\tvoid f() { \\\n" 8795 "\t\tsomeFunction( \\\n" 8796 "\t\t parameter1, \\\n" 8797 "\t\t parameter2); \\\n" 8798 "\t}", 8799 Tab); 8800 Tab.TabWidth = 4; 8801 Tab.IndentWidth = 8; 8802 verifyFormat("class TabWidth4Indent8 {\n" 8803 "\t\tvoid f() {\n" 8804 "\t\t\t\tsomeFunction(parameter1,\n" 8805 "\t\t\t\t\t\t\t parameter2);\n" 8806 "\t\t}\n" 8807 "};", 8808 Tab); 8809 Tab.TabWidth = 4; 8810 Tab.IndentWidth = 4; 8811 verifyFormat("class TabWidth4Indent4 {\n" 8812 "\tvoid f() {\n" 8813 "\t\tsomeFunction(parameter1,\n" 8814 "\t\t\t\t\t parameter2);\n" 8815 "\t}\n" 8816 "};", 8817 Tab); 8818 Tab.TabWidth = 8; 8819 Tab.IndentWidth = 4; 8820 verifyFormat("class TabWidth8Indent4 {\n" 8821 " void f() {\n" 8822 "\tsomeFunction(parameter1,\n" 8823 "\t\t parameter2);\n" 8824 " }\n" 8825 "};", 8826 Tab); 8827 Tab.TabWidth = 8; 8828 Tab.IndentWidth = 8; 8829 EXPECT_EQ("/*\n" 8830 "\t a\t\tcomment\n" 8831 "\t in multiple lines\n" 8832 " */", 8833 format(" /*\t \t \n" 8834 " \t \t a\t\tcomment\t \t\n" 8835 " \t \t in multiple lines\t\n" 8836 " \t */", 8837 Tab)); 8838 verifyFormat("{\n" 8839 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8840 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8841 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8842 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8843 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8844 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8845 "};", 8846 Tab); 8847 verifyFormat("enum AA {\n" 8848 "\ta1, // Force multiple lines\n" 8849 "\ta2,\n" 8850 "\ta3\n" 8851 "};", 8852 Tab); 8853 EXPECT_EQ("if (aaaaaaaa && // q\n" 8854 " bb) // w\n" 8855 "\t;", 8856 format("if (aaaaaaaa &&// q\n" 8857 "bb)// w\n" 8858 ";", 8859 Tab)); 8860 verifyFormat("class X {\n" 8861 "\tvoid f() {\n" 8862 "\t\tsomeFunction(parameter1,\n" 8863 "\t\t\t parameter2);\n" 8864 "\t}\n" 8865 "};", 8866 Tab); 8867 verifyFormat("{\n" 8868 "\tQ(\n" 8869 "\t {\n" 8870 "\t\t int a;\n" 8871 "\t\t someFunction(aaaaaaaa,\n" 8872 "\t\t\t\t bbbbbbb);\n" 8873 "\t },\n" 8874 "\t p);\n" 8875 "}", 8876 Tab); 8877 EXPECT_EQ("{\n" 8878 "\t/* aaaa\n" 8879 "\t bbbb */\n" 8880 "}", 8881 format("{\n" 8882 "/* aaaa\n" 8883 " bbbb */\n" 8884 "}", 8885 Tab)); 8886 EXPECT_EQ("{\n" 8887 "\t/*\n" 8888 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8889 "\t bbbbbbbbbbbbb\n" 8890 "\t*/\n" 8891 "}", 8892 format("{\n" 8893 "/*\n" 8894 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8895 "*/\n" 8896 "}", 8897 Tab)); 8898 EXPECT_EQ("{\n" 8899 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8900 "\t// bbbbbbbbbbbbb\n" 8901 "}", 8902 format("{\n" 8903 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8904 "}", 8905 Tab)); 8906 EXPECT_EQ("{\n" 8907 "\t/*\n" 8908 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8909 "\t bbbbbbbbbbbbb\n" 8910 "\t*/\n" 8911 "}", 8912 format("{\n" 8913 "\t/*\n" 8914 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8915 "\t*/\n" 8916 "}", 8917 Tab)); 8918 EXPECT_EQ("{\n" 8919 "\t/*\n" 8920 "\n" 8921 "\t*/\n" 8922 "}", 8923 format("{\n" 8924 "\t/*\n" 8925 "\n" 8926 "\t*/\n" 8927 "}", 8928 Tab)); 8929 EXPECT_EQ("{\n" 8930 "\t/*\n" 8931 " asdf\n" 8932 "\t*/\n" 8933 "}", 8934 format("{\n" 8935 "\t/*\n" 8936 " asdf\n" 8937 "\t*/\n" 8938 "}", 8939 Tab)); 8940 EXPECT_EQ("/*\n" 8941 "\t a\t\tcomment\n" 8942 "\t in multiple lines\n" 8943 " */", 8944 format(" /*\t \t \n" 8945 " \t \t a\t\tcomment\t \t\n" 8946 " \t \t in multiple lines\t\n" 8947 " \t */", 8948 Tab)); 8949 EXPECT_EQ("/* some\n" 8950 " comment */", 8951 format(" \t \t /* some\n" 8952 " \t \t comment */", 8953 Tab)); 8954 EXPECT_EQ("int a; /* some\n" 8955 " comment */", 8956 format(" \t \t int a; /* some\n" 8957 " \t \t comment */", 8958 Tab)); 8959 EXPECT_EQ("int a; /* some\n" 8960 "comment */", 8961 format(" \t \t int\ta; /* some\n" 8962 " \t \t comment */", 8963 Tab)); 8964 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8965 " comment */", 8966 format(" \t \t f(\"\t\t\"); /* some\n" 8967 " \t \t comment */", 8968 Tab)); 8969 EXPECT_EQ("{\n" 8970 " /*\n" 8971 " * Comment\n" 8972 " */\n" 8973 " int i;\n" 8974 "}", 8975 format("{\n" 8976 "\t/*\n" 8977 "\t * Comment\n" 8978 "\t */\n" 8979 "\t int i;\n" 8980 "}")); 8981 Tab.AlignConsecutiveAssignments = true; 8982 Tab.AlignConsecutiveDeclarations = true; 8983 Tab.TabWidth = 4; 8984 Tab.IndentWidth = 4; 8985 verifyFormat("class Assign {\n" 8986 "\tvoid f() {\n" 8987 "\t\tint x = 123;\n" 8988 "\t\tint random = 4;\n" 8989 "\t\tstd::string alphabet =\n" 8990 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8991 "\t}\n" 8992 "};", 8993 Tab); 8994 } 8995 8996 TEST_F(FormatTest, CalculatesOriginalColumn) { 8997 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8998 "q\"; /* some\n" 8999 " comment */", 9000 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9001 "q\"; /* some\n" 9002 " comment */", 9003 getLLVMStyle())); 9004 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9005 "/* some\n" 9006 " comment */", 9007 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9008 " /* some\n" 9009 " comment */", 9010 getLLVMStyle())); 9011 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9012 "qqq\n" 9013 "/* some\n" 9014 " comment */", 9015 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9016 "qqq\n" 9017 " /* some\n" 9018 " comment */", 9019 getLLVMStyle())); 9020 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9021 "wwww; /* some\n" 9022 " comment */", 9023 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9024 "wwww; /* some\n" 9025 " comment */", 9026 getLLVMStyle())); 9027 } 9028 9029 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9030 FormatStyle NoSpace = getLLVMStyle(); 9031 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9032 9033 verifyFormat("while(true)\n" 9034 " continue;", 9035 NoSpace); 9036 verifyFormat("for(;;)\n" 9037 " continue;", 9038 NoSpace); 9039 verifyFormat("if(true)\n" 9040 " f();\n" 9041 "else if(true)\n" 9042 " f();", 9043 NoSpace); 9044 verifyFormat("do {\n" 9045 " do_something();\n" 9046 "} while(something());", 9047 NoSpace); 9048 verifyFormat("switch(x) {\n" 9049 "default:\n" 9050 " break;\n" 9051 "}", 9052 NoSpace); 9053 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9054 verifyFormat("size_t x = sizeof(x);", NoSpace); 9055 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9056 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9057 verifyFormat("alignas(128) char a[128];", NoSpace); 9058 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9059 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9060 verifyFormat("int f() throw(Deprecated);", NoSpace); 9061 verifyFormat("typedef void (*cb)(int);", NoSpace); 9062 verifyFormat("T A::operator()();", NoSpace); 9063 verifyFormat("X A::operator++(T);", NoSpace); 9064 9065 FormatStyle Space = getLLVMStyle(); 9066 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9067 9068 verifyFormat("int f ();", Space); 9069 verifyFormat("void f (int a, T b) {\n" 9070 " while (true)\n" 9071 " continue;\n" 9072 "}", 9073 Space); 9074 verifyFormat("if (true)\n" 9075 " f ();\n" 9076 "else if (true)\n" 9077 " f ();", 9078 Space); 9079 verifyFormat("do {\n" 9080 " do_something ();\n" 9081 "} while (something ());", 9082 Space); 9083 verifyFormat("switch (x) {\n" 9084 "default:\n" 9085 " break;\n" 9086 "}", 9087 Space); 9088 verifyFormat("A::A () : a (1) {}", Space); 9089 verifyFormat("void f () __attribute__ ((asdf));", Space); 9090 verifyFormat("*(&a + 1);\n" 9091 "&((&a)[1]);\n" 9092 "a[(b + c) * d];\n" 9093 "(((a + 1) * 2) + 3) * 4;", 9094 Space); 9095 verifyFormat("#define A(x) x", Space); 9096 verifyFormat("#define A (x) x", Space); 9097 verifyFormat("#if defined(x)\n" 9098 "#endif", 9099 Space); 9100 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9101 verifyFormat("size_t x = sizeof (x);", Space); 9102 verifyFormat("auto f (int x) -> decltype (x);", Space); 9103 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9104 verifyFormat("alignas (128) char a[128];", Space); 9105 verifyFormat("size_t x = alignof (MyType);", Space); 9106 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9107 verifyFormat("int f () throw (Deprecated);", Space); 9108 verifyFormat("typedef void (*cb) (int);", Space); 9109 verifyFormat("T A::operator() ();", Space); 9110 verifyFormat("X A::operator++ (T);", Space); 9111 } 9112 9113 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9114 FormatStyle Spaces = getLLVMStyle(); 9115 9116 Spaces.SpacesInParentheses = true; 9117 verifyFormat("call( x, y, z );", Spaces); 9118 verifyFormat("call();", Spaces); 9119 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9120 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9121 Spaces); 9122 verifyFormat("while ( (bool)1 )\n" 9123 " continue;", 9124 Spaces); 9125 verifyFormat("for ( ;; )\n" 9126 " continue;", 9127 Spaces); 9128 verifyFormat("if ( true )\n" 9129 " f();\n" 9130 "else if ( true )\n" 9131 " f();", 9132 Spaces); 9133 verifyFormat("do {\n" 9134 " do_something( (int)i );\n" 9135 "} while ( something() );", 9136 Spaces); 9137 verifyFormat("switch ( x ) {\n" 9138 "default:\n" 9139 " break;\n" 9140 "}", 9141 Spaces); 9142 9143 Spaces.SpacesInParentheses = false; 9144 Spaces.SpacesInCStyleCastParentheses = true; 9145 verifyFormat("Type *A = ( Type * )P;", Spaces); 9146 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9147 verifyFormat("x = ( int32 )y;", Spaces); 9148 verifyFormat("int a = ( int )(2.0f);", Spaces); 9149 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9150 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9151 verifyFormat("#define x (( int )-1)", Spaces); 9152 9153 // Run the first set of tests again with: 9154 Spaces.SpacesInParentheses = false; 9155 Spaces.SpaceInEmptyParentheses = true; 9156 Spaces.SpacesInCStyleCastParentheses = true; 9157 verifyFormat("call(x, y, z);", Spaces); 9158 verifyFormat("call( );", Spaces); 9159 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9160 verifyFormat("while (( bool )1)\n" 9161 " continue;", 9162 Spaces); 9163 verifyFormat("for (;;)\n" 9164 " continue;", 9165 Spaces); 9166 verifyFormat("if (true)\n" 9167 " f( );\n" 9168 "else if (true)\n" 9169 " f( );", 9170 Spaces); 9171 verifyFormat("do {\n" 9172 " do_something(( int )i);\n" 9173 "} while (something( ));", 9174 Spaces); 9175 verifyFormat("switch (x) {\n" 9176 "default:\n" 9177 " break;\n" 9178 "}", 9179 Spaces); 9180 9181 // Run the first set of tests again with: 9182 Spaces.SpaceAfterCStyleCast = true; 9183 verifyFormat("call(x, y, z);", Spaces); 9184 verifyFormat("call( );", Spaces); 9185 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9186 verifyFormat("while (( bool ) 1)\n" 9187 " continue;", 9188 Spaces); 9189 verifyFormat("for (;;)\n" 9190 " continue;", 9191 Spaces); 9192 verifyFormat("if (true)\n" 9193 " f( );\n" 9194 "else if (true)\n" 9195 " f( );", 9196 Spaces); 9197 verifyFormat("do {\n" 9198 " do_something(( int ) i);\n" 9199 "} while (something( ));", 9200 Spaces); 9201 verifyFormat("switch (x) {\n" 9202 "default:\n" 9203 " break;\n" 9204 "}", 9205 Spaces); 9206 9207 // Run subset of tests again with: 9208 Spaces.SpacesInCStyleCastParentheses = false; 9209 Spaces.SpaceAfterCStyleCast = true; 9210 verifyFormat("while ((bool) 1)\n" 9211 " continue;", 9212 Spaces); 9213 verifyFormat("do {\n" 9214 " do_something((int) i);\n" 9215 "} while (something( ));", 9216 Spaces); 9217 } 9218 9219 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9220 verifyFormat("int a[5];"); 9221 verifyFormat("a[3] += 42;"); 9222 9223 FormatStyle Spaces = getLLVMStyle(); 9224 Spaces.SpacesInSquareBrackets = true; 9225 // Lambdas unchanged. 9226 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9227 verifyFormat("return [i, args...] {};", Spaces); 9228 9229 // Not lambdas. 9230 verifyFormat("int a[ 5 ];", Spaces); 9231 verifyFormat("a[ 3 ] += 42;", Spaces); 9232 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9233 verifyFormat("double &operator[](int i) { return 0; }\n" 9234 "int i;", 9235 Spaces); 9236 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9237 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9238 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9239 } 9240 9241 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9242 verifyFormat("int a = 5;"); 9243 verifyFormat("a += 42;"); 9244 verifyFormat("a or_eq 8;"); 9245 9246 FormatStyle Spaces = getLLVMStyle(); 9247 Spaces.SpaceBeforeAssignmentOperators = false; 9248 verifyFormat("int a= 5;", Spaces); 9249 verifyFormat("a+= 42;", Spaces); 9250 verifyFormat("a or_eq 8;", Spaces); 9251 } 9252 9253 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9254 FormatStyle Alignment = getLLVMStyle(); 9255 Alignment.AlignConsecutiveAssignments = false; 9256 verifyFormat("int a = 5;\n" 9257 "int oneTwoThree = 123;", 9258 Alignment); 9259 verifyFormat("int a = 5;\n" 9260 "int oneTwoThree = 123;", 9261 Alignment); 9262 9263 Alignment.AlignConsecutiveAssignments = true; 9264 verifyFormat("int a = 5;\n" 9265 "int oneTwoThree = 123;", 9266 Alignment); 9267 verifyFormat("int a = method();\n" 9268 "int oneTwoThree = 133;", 9269 Alignment); 9270 verifyFormat("a &= 5;\n" 9271 "bcd *= 5;\n" 9272 "ghtyf += 5;\n" 9273 "dvfvdb -= 5;\n" 9274 "a /= 5;\n" 9275 "vdsvsv %= 5;\n" 9276 "sfdbddfbdfbb ^= 5;\n" 9277 "dvsdsv |= 5;\n" 9278 "int dsvvdvsdvvv = 123;", 9279 Alignment); 9280 verifyFormat("int i = 1, j = 10;\n" 9281 "something = 2000;", 9282 Alignment); 9283 verifyFormat("something = 2000;\n" 9284 "int i = 1, j = 10;\n", 9285 Alignment); 9286 verifyFormat("something = 2000;\n" 9287 "another = 911;\n" 9288 "int i = 1, j = 10;\n" 9289 "oneMore = 1;\n" 9290 "i = 2;", 9291 Alignment); 9292 verifyFormat("int a = 5;\n" 9293 "int one = 1;\n" 9294 "method();\n" 9295 "int oneTwoThree = 123;\n" 9296 "int oneTwo = 12;", 9297 Alignment); 9298 verifyFormat("int oneTwoThree = 123;\n" 9299 "int oneTwo = 12;\n" 9300 "method();\n", 9301 Alignment); 9302 verifyFormat("int oneTwoThree = 123; // comment\n" 9303 "int oneTwo = 12; // comment", 9304 Alignment); 9305 EXPECT_EQ("int a = 5;\n" 9306 "\n" 9307 "int oneTwoThree = 123;", 9308 format("int a = 5;\n" 9309 "\n" 9310 "int oneTwoThree= 123;", 9311 Alignment)); 9312 EXPECT_EQ("int a = 5;\n" 9313 "int one = 1;\n" 9314 "\n" 9315 "int oneTwoThree = 123;", 9316 format("int a = 5;\n" 9317 "int one = 1;\n" 9318 "\n" 9319 "int oneTwoThree = 123;", 9320 Alignment)); 9321 EXPECT_EQ("int a = 5;\n" 9322 "int one = 1;\n" 9323 "\n" 9324 "int oneTwoThree = 123;\n" 9325 "int oneTwo = 12;", 9326 format("int a = 5;\n" 9327 "int one = 1;\n" 9328 "\n" 9329 "int oneTwoThree = 123;\n" 9330 "int oneTwo = 12;", 9331 Alignment)); 9332 Alignment.AlignEscapedNewlinesLeft = true; 9333 verifyFormat("#define A \\\n" 9334 " int aaaa = 12; \\\n" 9335 " int b = 23; \\\n" 9336 " int ccc = 234; \\\n" 9337 " int dddddddddd = 2345;", 9338 Alignment); 9339 Alignment.AlignEscapedNewlinesLeft = false; 9340 verifyFormat("#define A " 9341 " \\\n" 9342 " int aaaa = 12; " 9343 " \\\n" 9344 " int b = 23; " 9345 " \\\n" 9346 " int ccc = 234; " 9347 " \\\n" 9348 " int dddddddddd = 2345;", 9349 Alignment); 9350 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9351 "k = 4, int l = 5,\n" 9352 " int m = 6) {\n" 9353 " int j = 10;\n" 9354 " otherThing = 1;\n" 9355 "}", 9356 Alignment); 9357 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9358 " int i = 1;\n" 9359 " int j = 2;\n" 9360 " int big = 10000;\n" 9361 "}", 9362 Alignment); 9363 verifyFormat("class C {\n" 9364 "public:\n" 9365 " int i = 1;\n" 9366 " virtual void f() = 0;\n" 9367 "};", 9368 Alignment); 9369 verifyFormat("int i = 1;\n" 9370 "if (SomeType t = getSomething()) {\n" 9371 "}\n" 9372 "int j = 2;\n" 9373 "int big = 10000;", 9374 Alignment); 9375 verifyFormat("int j = 7;\n" 9376 "for (int k = 0; k < N; ++k) {\n" 9377 "}\n" 9378 "int j = 2;\n" 9379 "int big = 10000;\n" 9380 "}", 9381 Alignment); 9382 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9383 verifyFormat("int i = 1;\n" 9384 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9385 " = someLooooooooooooooooongFunction();\n" 9386 "int j = 2;", 9387 Alignment); 9388 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9389 verifyFormat("int i = 1;\n" 9390 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9391 " someLooooooooooooooooongFunction();\n" 9392 "int j = 2;", 9393 Alignment); 9394 9395 verifyFormat("auto lambda = []() {\n" 9396 " auto i = 0;\n" 9397 " return 0;\n" 9398 "};\n" 9399 "int i = 0;\n" 9400 "auto v = type{\n" 9401 " i = 1, //\n" 9402 " (i = 2), //\n" 9403 " i = 3 //\n" 9404 "};", 9405 Alignment); 9406 9407 // FIXME: Should align all three assignments 9408 verifyFormat( 9409 "int i = 1;\n" 9410 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9411 " loooooooooooooooooooooongParameterB);\n" 9412 "int j = 2;", 9413 Alignment); 9414 9415 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9416 " typename B = very_long_type_name_1,\n" 9417 " typename T_2 = very_long_type_name_2>\n" 9418 "auto foo() {}\n", 9419 Alignment); 9420 verifyFormat("int a, b = 1;\n" 9421 "int c = 2;\n" 9422 "int dd = 3;\n", 9423 Alignment); 9424 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9425 "float b[1][] = {{3.f}};\n", 9426 Alignment); 9427 } 9428 9429 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9430 FormatStyle Alignment = getLLVMStyle(); 9431 Alignment.AlignConsecutiveDeclarations = false; 9432 verifyFormat("float const a = 5;\n" 9433 "int oneTwoThree = 123;", 9434 Alignment); 9435 verifyFormat("int a = 5;\n" 9436 "float const oneTwoThree = 123;", 9437 Alignment); 9438 9439 Alignment.AlignConsecutiveDeclarations = true; 9440 verifyFormat("float const a = 5;\n" 9441 "int oneTwoThree = 123;", 9442 Alignment); 9443 verifyFormat("int a = method();\n" 9444 "float const oneTwoThree = 133;", 9445 Alignment); 9446 verifyFormat("int i = 1, j = 10;\n" 9447 "something = 2000;", 9448 Alignment); 9449 verifyFormat("something = 2000;\n" 9450 "int i = 1, j = 10;\n", 9451 Alignment); 9452 verifyFormat("float something = 2000;\n" 9453 "double another = 911;\n" 9454 "int i = 1, j = 10;\n" 9455 "const int *oneMore = 1;\n" 9456 "unsigned i = 2;", 9457 Alignment); 9458 verifyFormat("float a = 5;\n" 9459 "int one = 1;\n" 9460 "method();\n" 9461 "const double oneTwoThree = 123;\n" 9462 "const unsigned int oneTwo = 12;", 9463 Alignment); 9464 verifyFormat("int oneTwoThree{0}; // comment\n" 9465 "unsigned oneTwo; // comment", 9466 Alignment); 9467 EXPECT_EQ("float const a = 5;\n" 9468 "\n" 9469 "int oneTwoThree = 123;", 9470 format("float const a = 5;\n" 9471 "\n" 9472 "int oneTwoThree= 123;", 9473 Alignment)); 9474 EXPECT_EQ("float a = 5;\n" 9475 "int one = 1;\n" 9476 "\n" 9477 "unsigned oneTwoThree = 123;", 9478 format("float a = 5;\n" 9479 "int one = 1;\n" 9480 "\n" 9481 "unsigned oneTwoThree = 123;", 9482 Alignment)); 9483 EXPECT_EQ("float a = 5;\n" 9484 "int one = 1;\n" 9485 "\n" 9486 "unsigned oneTwoThree = 123;\n" 9487 "int oneTwo = 12;", 9488 format("float a = 5;\n" 9489 "int one = 1;\n" 9490 "\n" 9491 "unsigned oneTwoThree = 123;\n" 9492 "int oneTwo = 12;", 9493 Alignment)); 9494 Alignment.AlignConsecutiveAssignments = true; 9495 verifyFormat("float something = 2000;\n" 9496 "double another = 911;\n" 9497 "int i = 1, j = 10;\n" 9498 "const int *oneMore = 1;\n" 9499 "unsigned i = 2;", 9500 Alignment); 9501 verifyFormat("int oneTwoThree = {0}; // comment\n" 9502 "unsigned oneTwo = 0; // comment", 9503 Alignment); 9504 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9505 " int const i = 1;\n" 9506 " int * j = 2;\n" 9507 " int big = 10000;\n" 9508 "\n" 9509 " unsigned oneTwoThree = 123;\n" 9510 " int oneTwo = 12;\n" 9511 " method();\n" 9512 " float k = 2;\n" 9513 " int ll = 10000;\n" 9514 "}", 9515 format("void SomeFunction(int parameter= 0) {\n" 9516 " int const i= 1;\n" 9517 " int *j=2;\n" 9518 " int big = 10000;\n" 9519 "\n" 9520 "unsigned oneTwoThree =123;\n" 9521 "int oneTwo = 12;\n" 9522 " method();\n" 9523 "float k= 2;\n" 9524 "int ll=10000;\n" 9525 "}", 9526 Alignment)); 9527 Alignment.AlignConsecutiveAssignments = false; 9528 Alignment.AlignEscapedNewlinesLeft = true; 9529 verifyFormat("#define A \\\n" 9530 " int aaaa = 12; \\\n" 9531 " float b = 23; \\\n" 9532 " const int ccc = 234; \\\n" 9533 " unsigned dddddddddd = 2345;", 9534 Alignment); 9535 Alignment.AlignEscapedNewlinesLeft = false; 9536 Alignment.ColumnLimit = 30; 9537 verifyFormat("#define A \\\n" 9538 " int aaaa = 12; \\\n" 9539 " float b = 23; \\\n" 9540 " const int ccc = 234; \\\n" 9541 " int dddddddddd = 2345;", 9542 Alignment); 9543 Alignment.ColumnLimit = 80; 9544 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9545 "k = 4, int l = 5,\n" 9546 " int m = 6) {\n" 9547 " const int j = 10;\n" 9548 " otherThing = 1;\n" 9549 "}", 9550 Alignment); 9551 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9552 " int const i = 1;\n" 9553 " int * j = 2;\n" 9554 " int big = 10000;\n" 9555 "}", 9556 Alignment); 9557 verifyFormat("class C {\n" 9558 "public:\n" 9559 " int i = 1;\n" 9560 " virtual void f() = 0;\n" 9561 "};", 9562 Alignment); 9563 verifyFormat("float i = 1;\n" 9564 "if (SomeType t = getSomething()) {\n" 9565 "}\n" 9566 "const unsigned j = 2;\n" 9567 "int big = 10000;", 9568 Alignment); 9569 verifyFormat("float j = 7;\n" 9570 "for (int k = 0; k < N; ++k) {\n" 9571 "}\n" 9572 "unsigned j = 2;\n" 9573 "int big = 10000;\n" 9574 "}", 9575 Alignment); 9576 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9577 verifyFormat("float i = 1;\n" 9578 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9579 " = someLooooooooooooooooongFunction();\n" 9580 "int j = 2;", 9581 Alignment); 9582 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9583 verifyFormat("int i = 1;\n" 9584 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9585 " someLooooooooooooooooongFunction();\n" 9586 "int j = 2;", 9587 Alignment); 9588 9589 Alignment.AlignConsecutiveAssignments = true; 9590 verifyFormat("auto lambda = []() {\n" 9591 " auto ii = 0;\n" 9592 " float j = 0;\n" 9593 " return 0;\n" 9594 "};\n" 9595 "int i = 0;\n" 9596 "float i2 = 0;\n" 9597 "auto v = type{\n" 9598 " i = 1, //\n" 9599 " (i = 2), //\n" 9600 " i = 3 //\n" 9601 "};", 9602 Alignment); 9603 Alignment.AlignConsecutiveAssignments = false; 9604 9605 // FIXME: Should align all three declarations 9606 verifyFormat( 9607 "int i = 1;\n" 9608 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9609 " loooooooooooooooooooooongParameterB);\n" 9610 "int j = 2;", 9611 Alignment); 9612 9613 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9614 // We expect declarations and assignments to align, as long as it doesn't 9615 // exceed the column limit, starting a new alignemnt sequence whenever it 9616 // happens. 9617 Alignment.AlignConsecutiveAssignments = true; 9618 Alignment.ColumnLimit = 30; 9619 verifyFormat("float ii = 1;\n" 9620 "unsigned j = 2;\n" 9621 "int someVerylongVariable = 1;\n" 9622 "AnotherLongType ll = 123456;\n" 9623 "VeryVeryLongType k = 2;\n" 9624 "int myvar = 1;", 9625 Alignment); 9626 Alignment.ColumnLimit = 80; 9627 Alignment.AlignConsecutiveAssignments = false; 9628 9629 verifyFormat( 9630 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9631 " typename LongType, typename B>\n" 9632 "auto foo() {}\n", 9633 Alignment); 9634 verifyFormat("float a, b = 1;\n" 9635 "int c = 2;\n" 9636 "int dd = 3;\n", 9637 Alignment); 9638 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9639 "float b[1][] = {{3.f}};\n", 9640 Alignment); 9641 Alignment.AlignConsecutiveAssignments = true; 9642 verifyFormat("float a, b = 1;\n" 9643 "int c = 2;\n" 9644 "int dd = 3;\n", 9645 Alignment); 9646 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9647 "float b[1][] = {{3.f}};\n", 9648 Alignment); 9649 Alignment.AlignConsecutiveAssignments = false; 9650 9651 Alignment.ColumnLimit = 30; 9652 Alignment.BinPackParameters = false; 9653 verifyFormat("void foo(float a,\n" 9654 " float b,\n" 9655 " int c,\n" 9656 " uint32_t *d) {\n" 9657 " int * e = 0;\n" 9658 " float f = 0;\n" 9659 " double g = 0;\n" 9660 "}\n" 9661 "void bar(ino_t a,\n" 9662 " int b,\n" 9663 " uint32_t *c,\n" 9664 " bool d) {}\n", 9665 Alignment); 9666 Alignment.BinPackParameters = true; 9667 Alignment.ColumnLimit = 80; 9668 } 9669 9670 TEST_F(FormatTest, LinuxBraceBreaking) { 9671 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9672 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9673 verifyFormat("namespace a\n" 9674 "{\n" 9675 "class A\n" 9676 "{\n" 9677 " void f()\n" 9678 " {\n" 9679 " if (true) {\n" 9680 " a();\n" 9681 " b();\n" 9682 " } else {\n" 9683 " a();\n" 9684 " }\n" 9685 " }\n" 9686 " void g() { return; }\n" 9687 "};\n" 9688 "struct B {\n" 9689 " int x;\n" 9690 "};\n" 9691 "}\n", 9692 LinuxBraceStyle); 9693 verifyFormat("enum X {\n" 9694 " Y = 0,\n" 9695 "}\n", 9696 LinuxBraceStyle); 9697 verifyFormat("struct S {\n" 9698 " int Type;\n" 9699 " union {\n" 9700 " int x;\n" 9701 " double y;\n" 9702 " } Value;\n" 9703 " class C\n" 9704 " {\n" 9705 " MyFavoriteType Value;\n" 9706 " } Class;\n" 9707 "}\n", 9708 LinuxBraceStyle); 9709 } 9710 9711 TEST_F(FormatTest, MozillaBraceBreaking) { 9712 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9713 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9714 verifyFormat("namespace a {\n" 9715 "class A\n" 9716 "{\n" 9717 " void f()\n" 9718 " {\n" 9719 " if (true) {\n" 9720 " a();\n" 9721 " b();\n" 9722 " }\n" 9723 " }\n" 9724 " void g() { return; }\n" 9725 "};\n" 9726 "enum E\n" 9727 "{\n" 9728 " A,\n" 9729 " // foo\n" 9730 " B,\n" 9731 " C\n" 9732 "};\n" 9733 "struct B\n" 9734 "{\n" 9735 " int x;\n" 9736 "};\n" 9737 "}\n", 9738 MozillaBraceStyle); 9739 verifyFormat("struct S\n" 9740 "{\n" 9741 " int Type;\n" 9742 " union\n" 9743 " {\n" 9744 " int x;\n" 9745 " double y;\n" 9746 " } Value;\n" 9747 " class C\n" 9748 " {\n" 9749 " MyFavoriteType Value;\n" 9750 " } Class;\n" 9751 "}\n", 9752 MozillaBraceStyle); 9753 } 9754 9755 TEST_F(FormatTest, StroustrupBraceBreaking) { 9756 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9757 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9758 verifyFormat("namespace a {\n" 9759 "class A {\n" 9760 " void f()\n" 9761 " {\n" 9762 " if (true) {\n" 9763 " a();\n" 9764 " b();\n" 9765 " }\n" 9766 " }\n" 9767 " void g() { return; }\n" 9768 "};\n" 9769 "struct B {\n" 9770 " int x;\n" 9771 "};\n" 9772 "}\n", 9773 StroustrupBraceStyle); 9774 9775 verifyFormat("void foo()\n" 9776 "{\n" 9777 " if (a) {\n" 9778 " a();\n" 9779 " }\n" 9780 " else {\n" 9781 " b();\n" 9782 " }\n" 9783 "}\n", 9784 StroustrupBraceStyle); 9785 9786 verifyFormat("#ifdef _DEBUG\n" 9787 "int foo(int i = 0)\n" 9788 "#else\n" 9789 "int foo(int i = 5)\n" 9790 "#endif\n" 9791 "{\n" 9792 " return i;\n" 9793 "}", 9794 StroustrupBraceStyle); 9795 9796 verifyFormat("void foo() {}\n" 9797 "void bar()\n" 9798 "#ifdef _DEBUG\n" 9799 "{\n" 9800 " foo();\n" 9801 "}\n" 9802 "#else\n" 9803 "{\n" 9804 "}\n" 9805 "#endif", 9806 StroustrupBraceStyle); 9807 9808 verifyFormat("void foobar() { int i = 5; }\n" 9809 "#ifdef _DEBUG\n" 9810 "void bar() {}\n" 9811 "#else\n" 9812 "void bar() { foobar(); }\n" 9813 "#endif", 9814 StroustrupBraceStyle); 9815 } 9816 9817 TEST_F(FormatTest, AllmanBraceBreaking) { 9818 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9819 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9820 verifyFormat("namespace a\n" 9821 "{\n" 9822 "class A\n" 9823 "{\n" 9824 " void f()\n" 9825 " {\n" 9826 " if (true)\n" 9827 " {\n" 9828 " a();\n" 9829 " b();\n" 9830 " }\n" 9831 " }\n" 9832 " void g() { return; }\n" 9833 "};\n" 9834 "struct B\n" 9835 "{\n" 9836 " int x;\n" 9837 "};\n" 9838 "}", 9839 AllmanBraceStyle); 9840 9841 verifyFormat("void f()\n" 9842 "{\n" 9843 " if (true)\n" 9844 " {\n" 9845 " a();\n" 9846 " }\n" 9847 " else if (false)\n" 9848 " {\n" 9849 " b();\n" 9850 " }\n" 9851 " else\n" 9852 " {\n" 9853 " c();\n" 9854 " }\n" 9855 "}\n", 9856 AllmanBraceStyle); 9857 9858 verifyFormat("void f()\n" 9859 "{\n" 9860 " for (int i = 0; i < 10; ++i)\n" 9861 " {\n" 9862 " a();\n" 9863 " }\n" 9864 " while (false)\n" 9865 " {\n" 9866 " b();\n" 9867 " }\n" 9868 " do\n" 9869 " {\n" 9870 " c();\n" 9871 " } while (false)\n" 9872 "}\n", 9873 AllmanBraceStyle); 9874 9875 verifyFormat("void f(int a)\n" 9876 "{\n" 9877 " switch (a)\n" 9878 " {\n" 9879 " case 0:\n" 9880 " break;\n" 9881 " case 1:\n" 9882 " {\n" 9883 " break;\n" 9884 " }\n" 9885 " case 2:\n" 9886 " {\n" 9887 " }\n" 9888 " break;\n" 9889 " default:\n" 9890 " break;\n" 9891 " }\n" 9892 "}\n", 9893 AllmanBraceStyle); 9894 9895 verifyFormat("enum X\n" 9896 "{\n" 9897 " Y = 0,\n" 9898 "}\n", 9899 AllmanBraceStyle); 9900 verifyFormat("enum X\n" 9901 "{\n" 9902 " Y = 0\n" 9903 "}\n", 9904 AllmanBraceStyle); 9905 9906 verifyFormat("@interface BSApplicationController ()\n" 9907 "{\n" 9908 "@private\n" 9909 " id _extraIvar;\n" 9910 "}\n" 9911 "@end\n", 9912 AllmanBraceStyle); 9913 9914 verifyFormat("#ifdef _DEBUG\n" 9915 "int foo(int i = 0)\n" 9916 "#else\n" 9917 "int foo(int i = 5)\n" 9918 "#endif\n" 9919 "{\n" 9920 " return i;\n" 9921 "}", 9922 AllmanBraceStyle); 9923 9924 verifyFormat("void foo() {}\n" 9925 "void bar()\n" 9926 "#ifdef _DEBUG\n" 9927 "{\n" 9928 " foo();\n" 9929 "}\n" 9930 "#else\n" 9931 "{\n" 9932 "}\n" 9933 "#endif", 9934 AllmanBraceStyle); 9935 9936 verifyFormat("void foobar() { int i = 5; }\n" 9937 "#ifdef _DEBUG\n" 9938 "void bar() {}\n" 9939 "#else\n" 9940 "void bar() { foobar(); }\n" 9941 "#endif", 9942 AllmanBraceStyle); 9943 9944 // This shouldn't affect ObjC blocks.. 9945 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9946 " // ...\n" 9947 " int i;\n" 9948 "}];", 9949 AllmanBraceStyle); 9950 verifyFormat("void (^block)(void) = ^{\n" 9951 " // ...\n" 9952 " int i;\n" 9953 "};", 9954 AllmanBraceStyle); 9955 // .. or dict literals. 9956 verifyFormat("void f()\n" 9957 "{\n" 9958 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9959 "}", 9960 AllmanBraceStyle); 9961 verifyFormat("int f()\n" 9962 "{ // comment\n" 9963 " return 42;\n" 9964 "}", 9965 AllmanBraceStyle); 9966 9967 AllmanBraceStyle.ColumnLimit = 19; 9968 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9969 AllmanBraceStyle.ColumnLimit = 18; 9970 verifyFormat("void f()\n" 9971 "{\n" 9972 " int i;\n" 9973 "}", 9974 AllmanBraceStyle); 9975 AllmanBraceStyle.ColumnLimit = 80; 9976 9977 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9978 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9979 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9980 verifyFormat("void f(bool b)\n" 9981 "{\n" 9982 " if (b)\n" 9983 " {\n" 9984 " return;\n" 9985 " }\n" 9986 "}\n", 9987 BreakBeforeBraceShortIfs); 9988 verifyFormat("void f(bool b)\n" 9989 "{\n" 9990 " if (b) return;\n" 9991 "}\n", 9992 BreakBeforeBraceShortIfs); 9993 verifyFormat("void f(bool b)\n" 9994 "{\n" 9995 " while (b)\n" 9996 " {\n" 9997 " return;\n" 9998 " }\n" 9999 "}\n", 10000 BreakBeforeBraceShortIfs); 10001 } 10002 10003 TEST_F(FormatTest, GNUBraceBreaking) { 10004 FormatStyle GNUBraceStyle = getLLVMStyle(); 10005 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 10006 verifyFormat("namespace a\n" 10007 "{\n" 10008 "class A\n" 10009 "{\n" 10010 " void f()\n" 10011 " {\n" 10012 " int a;\n" 10013 " {\n" 10014 " int b;\n" 10015 " }\n" 10016 " if (true)\n" 10017 " {\n" 10018 " a();\n" 10019 " b();\n" 10020 " }\n" 10021 " }\n" 10022 " void g() { return; }\n" 10023 "}\n" 10024 "}", 10025 GNUBraceStyle); 10026 10027 verifyFormat("void f()\n" 10028 "{\n" 10029 " if (true)\n" 10030 " {\n" 10031 " a();\n" 10032 " }\n" 10033 " else if (false)\n" 10034 " {\n" 10035 " b();\n" 10036 " }\n" 10037 " else\n" 10038 " {\n" 10039 " c();\n" 10040 " }\n" 10041 "}\n", 10042 GNUBraceStyle); 10043 10044 verifyFormat("void f()\n" 10045 "{\n" 10046 " for (int i = 0; i < 10; ++i)\n" 10047 " {\n" 10048 " a();\n" 10049 " }\n" 10050 " while (false)\n" 10051 " {\n" 10052 " b();\n" 10053 " }\n" 10054 " do\n" 10055 " {\n" 10056 " c();\n" 10057 " }\n" 10058 " while (false);\n" 10059 "}\n", 10060 GNUBraceStyle); 10061 10062 verifyFormat("void f(int a)\n" 10063 "{\n" 10064 " switch (a)\n" 10065 " {\n" 10066 " case 0:\n" 10067 " break;\n" 10068 " case 1:\n" 10069 " {\n" 10070 " break;\n" 10071 " }\n" 10072 " case 2:\n" 10073 " {\n" 10074 " }\n" 10075 " break;\n" 10076 " default:\n" 10077 " break;\n" 10078 " }\n" 10079 "}\n", 10080 GNUBraceStyle); 10081 10082 verifyFormat("enum X\n" 10083 "{\n" 10084 " Y = 0,\n" 10085 "}\n", 10086 GNUBraceStyle); 10087 10088 verifyFormat("@interface BSApplicationController ()\n" 10089 "{\n" 10090 "@private\n" 10091 " id _extraIvar;\n" 10092 "}\n" 10093 "@end\n", 10094 GNUBraceStyle); 10095 10096 verifyFormat("#ifdef _DEBUG\n" 10097 "int foo(int i = 0)\n" 10098 "#else\n" 10099 "int foo(int i = 5)\n" 10100 "#endif\n" 10101 "{\n" 10102 " return i;\n" 10103 "}", 10104 GNUBraceStyle); 10105 10106 verifyFormat("void foo() {}\n" 10107 "void bar()\n" 10108 "#ifdef _DEBUG\n" 10109 "{\n" 10110 " foo();\n" 10111 "}\n" 10112 "#else\n" 10113 "{\n" 10114 "}\n" 10115 "#endif", 10116 GNUBraceStyle); 10117 10118 verifyFormat("void foobar() { int i = 5; }\n" 10119 "#ifdef _DEBUG\n" 10120 "void bar() {}\n" 10121 "#else\n" 10122 "void bar() { foobar(); }\n" 10123 "#endif", 10124 GNUBraceStyle); 10125 } 10126 10127 TEST_F(FormatTest, WebKitBraceBreaking) { 10128 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10129 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10130 verifyFormat("namespace a {\n" 10131 "class A {\n" 10132 " void f()\n" 10133 " {\n" 10134 " if (true) {\n" 10135 " a();\n" 10136 " b();\n" 10137 " }\n" 10138 " }\n" 10139 " void g() { return; }\n" 10140 "};\n" 10141 "enum E {\n" 10142 " A,\n" 10143 " // foo\n" 10144 " B,\n" 10145 " C\n" 10146 "};\n" 10147 "struct B {\n" 10148 " int x;\n" 10149 "};\n" 10150 "}\n", 10151 WebKitBraceStyle); 10152 verifyFormat("struct S {\n" 10153 " int Type;\n" 10154 " union {\n" 10155 " int x;\n" 10156 " double y;\n" 10157 " } Value;\n" 10158 " class C {\n" 10159 " MyFavoriteType Value;\n" 10160 " } Class;\n" 10161 "};\n", 10162 WebKitBraceStyle); 10163 } 10164 10165 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10166 verifyFormat("void f() {\n" 10167 " try {\n" 10168 " } catch (const Exception &e) {\n" 10169 " }\n" 10170 "}\n", 10171 getLLVMStyle()); 10172 } 10173 10174 TEST_F(FormatTest, UnderstandsPragmas) { 10175 verifyFormat("#pragma omp reduction(| : var)"); 10176 verifyFormat("#pragma omp reduction(+ : var)"); 10177 10178 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10179 "(including parentheses).", 10180 format("#pragma mark Any non-hyphenated or hyphenated string " 10181 "(including parentheses).")); 10182 } 10183 10184 TEST_F(FormatTest, UnderstandPragmaOption) { 10185 verifyFormat("#pragma option -C -A"); 10186 10187 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10188 } 10189 10190 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10191 for (size_t i = 1; i < Styles.size(); ++i) \ 10192 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10193 << " differs from Style #0" 10194 10195 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10196 SmallVector<FormatStyle, 3> Styles; 10197 Styles.resize(3); 10198 10199 Styles[0] = getLLVMStyle(); 10200 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10201 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10202 EXPECT_ALL_STYLES_EQUAL(Styles); 10203 10204 Styles[0] = getGoogleStyle(); 10205 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10206 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10207 EXPECT_ALL_STYLES_EQUAL(Styles); 10208 10209 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10210 EXPECT_TRUE( 10211 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10212 EXPECT_TRUE( 10213 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10214 EXPECT_ALL_STYLES_EQUAL(Styles); 10215 10216 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10217 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10218 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10219 EXPECT_ALL_STYLES_EQUAL(Styles); 10220 10221 Styles[0] = getMozillaStyle(); 10222 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10223 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10224 EXPECT_ALL_STYLES_EQUAL(Styles); 10225 10226 Styles[0] = getWebKitStyle(); 10227 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10228 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10229 EXPECT_ALL_STYLES_EQUAL(Styles); 10230 10231 Styles[0] = getGNUStyle(); 10232 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10233 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10234 EXPECT_ALL_STYLES_EQUAL(Styles); 10235 10236 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10237 } 10238 10239 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10240 SmallVector<FormatStyle, 8> Styles; 10241 Styles.resize(2); 10242 10243 Styles[0] = getGoogleStyle(); 10244 Styles[1] = getLLVMStyle(); 10245 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10246 EXPECT_ALL_STYLES_EQUAL(Styles); 10247 10248 Styles.resize(5); 10249 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10250 Styles[1] = getLLVMStyle(); 10251 Styles[1].Language = FormatStyle::LK_JavaScript; 10252 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10253 10254 Styles[2] = getLLVMStyle(); 10255 Styles[2].Language = FormatStyle::LK_JavaScript; 10256 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10257 "BasedOnStyle: Google", 10258 &Styles[2]) 10259 .value()); 10260 10261 Styles[3] = getLLVMStyle(); 10262 Styles[3].Language = FormatStyle::LK_JavaScript; 10263 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10264 "Language: JavaScript", 10265 &Styles[3]) 10266 .value()); 10267 10268 Styles[4] = getLLVMStyle(); 10269 Styles[4].Language = FormatStyle::LK_JavaScript; 10270 EXPECT_EQ(0, parseConfiguration("---\n" 10271 "BasedOnStyle: LLVM\n" 10272 "IndentWidth: 123\n" 10273 "---\n" 10274 "BasedOnStyle: Google\n" 10275 "Language: JavaScript", 10276 &Styles[4]) 10277 .value()); 10278 EXPECT_ALL_STYLES_EQUAL(Styles); 10279 } 10280 10281 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10282 Style.FIELD = false; \ 10283 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10284 EXPECT_TRUE(Style.FIELD); \ 10285 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10286 EXPECT_FALSE(Style.FIELD); 10287 10288 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10289 10290 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10291 Style.STRUCT.FIELD = false; \ 10292 EXPECT_EQ(0, \ 10293 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10294 .value()); \ 10295 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10296 EXPECT_EQ(0, \ 10297 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10298 .value()); \ 10299 EXPECT_FALSE(Style.STRUCT.FIELD); 10300 10301 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10302 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10303 10304 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10305 EXPECT_NE(VALUE, Style.FIELD); \ 10306 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10307 EXPECT_EQ(VALUE, Style.FIELD) 10308 10309 TEST_F(FormatTest, ParsesConfigurationBools) { 10310 FormatStyle Style = {}; 10311 Style.Language = FormatStyle::LK_Cpp; 10312 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 10313 CHECK_PARSE_BOOL(AlignOperands); 10314 CHECK_PARSE_BOOL(AlignTrailingComments); 10315 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10316 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10317 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10318 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10319 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10320 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10321 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10322 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10323 CHECK_PARSE_BOOL(BinPackArguments); 10324 CHECK_PARSE_BOOL(BinPackParameters); 10325 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10326 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10327 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 10328 CHECK_PARSE_BOOL(BreakStringLiterals); 10329 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10330 CHECK_PARSE_BOOL(DerivePointerAlignment); 10331 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10332 CHECK_PARSE_BOOL(DisableFormat); 10333 CHECK_PARSE_BOOL(IndentCaseLabels); 10334 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10335 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10336 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10337 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10338 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10339 CHECK_PARSE_BOOL(ReflowComments); 10340 CHECK_PARSE_BOOL(SortIncludes); 10341 CHECK_PARSE_BOOL(SpacesInParentheses); 10342 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10343 CHECK_PARSE_BOOL(SpacesInAngles); 10344 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10345 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10346 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10347 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10348 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10349 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10350 10351 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10352 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10353 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10354 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10355 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10356 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10357 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10358 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10359 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10360 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10361 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10362 } 10363 10364 #undef CHECK_PARSE_BOOL 10365 10366 TEST_F(FormatTest, ParsesConfiguration) { 10367 FormatStyle Style = {}; 10368 Style.Language = FormatStyle::LK_Cpp; 10369 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10370 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10371 ConstructorInitializerIndentWidth, 1234u); 10372 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10373 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10374 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10375 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10376 PenaltyBreakBeforeFirstCallParameter, 1234u); 10377 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10378 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10379 PenaltyReturnTypeOnItsOwnLine, 1234u); 10380 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10381 SpacesBeforeTrailingComments, 1234u); 10382 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10383 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10384 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10385 10386 Style.PointerAlignment = FormatStyle::PAS_Middle; 10387 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10388 FormatStyle::PAS_Left); 10389 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10390 FormatStyle::PAS_Right); 10391 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10392 FormatStyle::PAS_Middle); 10393 // For backward compatibility: 10394 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10395 FormatStyle::PAS_Left); 10396 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10397 FormatStyle::PAS_Right); 10398 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10399 FormatStyle::PAS_Middle); 10400 10401 Style.Standard = FormatStyle::LS_Auto; 10402 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10403 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10404 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10405 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10406 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10407 10408 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10409 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10410 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10411 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10412 FormatStyle::BOS_None); 10413 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10414 FormatStyle::BOS_All); 10415 // For backward compatibility: 10416 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10417 FormatStyle::BOS_None); 10418 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10419 FormatStyle::BOS_All); 10420 10421 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10422 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10423 FormatStyle::BAS_Align); 10424 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10425 FormatStyle::BAS_DontAlign); 10426 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10427 FormatStyle::BAS_AlwaysBreak); 10428 // For backward compatibility: 10429 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10430 FormatStyle::BAS_DontAlign); 10431 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10432 FormatStyle::BAS_Align); 10433 10434 Style.UseTab = FormatStyle::UT_ForIndentation; 10435 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10436 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10437 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10438 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10439 FormatStyle::UT_ForContinuationAndIndentation); 10440 // For backward compatibility: 10441 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10442 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10443 10444 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10445 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10446 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10447 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10448 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10449 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10450 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10451 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10452 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10453 // For backward compatibility: 10454 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10455 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10456 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10457 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10458 10459 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10460 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10461 FormatStyle::SBPO_Never); 10462 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10463 FormatStyle::SBPO_Always); 10464 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10465 FormatStyle::SBPO_ControlStatements); 10466 // For backward compatibility: 10467 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10468 FormatStyle::SBPO_Never); 10469 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10470 FormatStyle::SBPO_ControlStatements); 10471 10472 Style.ColumnLimit = 123; 10473 FormatStyle BaseStyle = getLLVMStyle(); 10474 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10475 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10476 10477 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10478 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10479 FormatStyle::BS_Attach); 10480 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10481 FormatStyle::BS_Linux); 10482 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10483 FormatStyle::BS_Mozilla); 10484 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10485 FormatStyle::BS_Stroustrup); 10486 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10487 FormatStyle::BS_Allman); 10488 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10489 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10490 FormatStyle::BS_WebKit); 10491 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10492 FormatStyle::BS_Custom); 10493 10494 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10495 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10496 FormatStyle::RTBS_None); 10497 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10498 FormatStyle::RTBS_All); 10499 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10500 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10501 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10502 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10503 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10504 AlwaysBreakAfterReturnType, 10505 FormatStyle::RTBS_TopLevelDefinitions); 10506 10507 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10508 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10509 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10510 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10511 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10512 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10513 AlwaysBreakAfterDefinitionReturnType, 10514 FormatStyle::DRTBS_TopLevel); 10515 10516 Style.NamespaceIndentation = FormatStyle::NI_All; 10517 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10518 FormatStyle::NI_None); 10519 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10520 FormatStyle::NI_Inner); 10521 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10522 FormatStyle::NI_All); 10523 10524 // FIXME: This is required because parsing a configuration simply overwrites 10525 // the first N elements of the list instead of resetting it. 10526 Style.ForEachMacros.clear(); 10527 std::vector<std::string> BoostForeach; 10528 BoostForeach.push_back("BOOST_FOREACH"); 10529 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10530 std::vector<std::string> BoostAndQForeach; 10531 BoostAndQForeach.push_back("BOOST_FOREACH"); 10532 BoostAndQForeach.push_back("Q_FOREACH"); 10533 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10534 BoostAndQForeach); 10535 10536 Style.IncludeCategories.clear(); 10537 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10538 {".*", 1}}; 10539 CHECK_PARSE("IncludeCategories:\n" 10540 " - Regex: abc/.*\n" 10541 " Priority: 2\n" 10542 " - Regex: .*\n" 10543 " Priority: 1", 10544 IncludeCategories, ExpectedCategories); 10545 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10546 } 10547 10548 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10549 FormatStyle Style = {}; 10550 Style.Language = FormatStyle::LK_Cpp; 10551 CHECK_PARSE("Language: Cpp\n" 10552 "IndentWidth: 12", 10553 IndentWidth, 12u); 10554 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10555 "IndentWidth: 34", 10556 &Style), 10557 ParseError::Unsuitable); 10558 EXPECT_EQ(12u, Style.IndentWidth); 10559 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10560 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10561 10562 Style.Language = FormatStyle::LK_JavaScript; 10563 CHECK_PARSE("Language: JavaScript\n" 10564 "IndentWidth: 12", 10565 IndentWidth, 12u); 10566 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10567 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10568 "IndentWidth: 34", 10569 &Style), 10570 ParseError::Unsuitable); 10571 EXPECT_EQ(23u, Style.IndentWidth); 10572 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10573 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10574 10575 CHECK_PARSE("BasedOnStyle: LLVM\n" 10576 "IndentWidth: 67", 10577 IndentWidth, 67u); 10578 10579 CHECK_PARSE("---\n" 10580 "Language: JavaScript\n" 10581 "IndentWidth: 12\n" 10582 "---\n" 10583 "Language: Cpp\n" 10584 "IndentWidth: 34\n" 10585 "...\n", 10586 IndentWidth, 12u); 10587 10588 Style.Language = FormatStyle::LK_Cpp; 10589 CHECK_PARSE("---\n" 10590 "Language: JavaScript\n" 10591 "IndentWidth: 12\n" 10592 "---\n" 10593 "Language: Cpp\n" 10594 "IndentWidth: 34\n" 10595 "...\n", 10596 IndentWidth, 34u); 10597 CHECK_PARSE("---\n" 10598 "IndentWidth: 78\n" 10599 "---\n" 10600 "Language: JavaScript\n" 10601 "IndentWidth: 56\n" 10602 "...\n", 10603 IndentWidth, 78u); 10604 10605 Style.ColumnLimit = 123; 10606 Style.IndentWidth = 234; 10607 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10608 Style.TabWidth = 345; 10609 EXPECT_FALSE(parseConfiguration("---\n" 10610 "IndentWidth: 456\n" 10611 "BreakBeforeBraces: Allman\n" 10612 "---\n" 10613 "Language: JavaScript\n" 10614 "IndentWidth: 111\n" 10615 "TabWidth: 111\n" 10616 "---\n" 10617 "Language: Cpp\n" 10618 "BreakBeforeBraces: Stroustrup\n" 10619 "TabWidth: 789\n" 10620 "...\n", 10621 &Style)); 10622 EXPECT_EQ(123u, Style.ColumnLimit); 10623 EXPECT_EQ(456u, Style.IndentWidth); 10624 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10625 EXPECT_EQ(789u, Style.TabWidth); 10626 10627 EXPECT_EQ(parseConfiguration("---\n" 10628 "Language: JavaScript\n" 10629 "IndentWidth: 56\n" 10630 "---\n" 10631 "IndentWidth: 78\n" 10632 "...\n", 10633 &Style), 10634 ParseError::Error); 10635 EXPECT_EQ(parseConfiguration("---\n" 10636 "Language: JavaScript\n" 10637 "IndentWidth: 56\n" 10638 "---\n" 10639 "Language: JavaScript\n" 10640 "IndentWidth: 78\n" 10641 "...\n", 10642 &Style), 10643 ParseError::Error); 10644 10645 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10646 } 10647 10648 #undef CHECK_PARSE 10649 10650 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10651 FormatStyle Style = {}; 10652 Style.Language = FormatStyle::LK_JavaScript; 10653 Style.BreakBeforeTernaryOperators = true; 10654 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10655 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10656 10657 Style.BreakBeforeTernaryOperators = true; 10658 EXPECT_EQ(0, parseConfiguration("---\n" 10659 "BasedOnStyle: Google\n" 10660 "---\n" 10661 "Language: JavaScript\n" 10662 "IndentWidth: 76\n" 10663 "...\n", 10664 &Style) 10665 .value()); 10666 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10667 EXPECT_EQ(76u, Style.IndentWidth); 10668 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10669 } 10670 10671 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10672 FormatStyle Style = getLLVMStyle(); 10673 std::string YAML = configurationAsText(Style); 10674 FormatStyle ParsedStyle = {}; 10675 ParsedStyle.Language = FormatStyle::LK_Cpp; 10676 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10677 EXPECT_EQ(Style, ParsedStyle); 10678 } 10679 10680 TEST_F(FormatTest, WorksFor8bitEncodings) { 10681 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10682 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10683 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10684 "\"\xef\xee\xf0\xf3...\"", 10685 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10686 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10687 "\xef\xee\xf0\xf3...\"", 10688 getLLVMStyleWithColumns(12))); 10689 } 10690 10691 TEST_F(FormatTest, HandlesUTF8BOM) { 10692 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10693 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10694 format("\xef\xbb\xbf#include <iostream>")); 10695 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10696 format("\xef\xbb\xbf\n#include <iostream>")); 10697 } 10698 10699 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10700 #if !defined(_MSC_VER) 10701 10702 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10703 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10704 getLLVMStyleWithColumns(35)); 10705 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10706 getLLVMStyleWithColumns(31)); 10707 verifyFormat("// Однажды в студёную зимнюю пору...", 10708 getLLVMStyleWithColumns(36)); 10709 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10710 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10711 getLLVMStyleWithColumns(39)); 10712 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10713 getLLVMStyleWithColumns(35)); 10714 } 10715 10716 TEST_F(FormatTest, SplitsUTF8Strings) { 10717 // Non-printable characters' width is currently considered to be the length in 10718 // bytes in UTF8. The characters can be displayed in very different manner 10719 // (zero-width, single width with a substitution glyph, expanded to their code 10720 // (e.g. "<8d>"), so there's no single correct way to handle them. 10721 EXPECT_EQ("\"aaaaÄ\"\n" 10722 "\"\xc2\x8d\";", 10723 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10724 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10725 "\"\xc2\x8d\";", 10726 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10727 EXPECT_EQ("\"Однажды, в \"\n" 10728 "\"студёную \"\n" 10729 "\"зимнюю \"\n" 10730 "\"пору,\"", 10731 format("\"Однажды, в студёную зимнюю пору,\"", 10732 getLLVMStyleWithColumns(13))); 10733 EXPECT_EQ( 10734 "\"一 二 三 \"\n" 10735 "\"四 五六 \"\n" 10736 "\"七 八 九 \"\n" 10737 "\"十\"", 10738 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10739 EXPECT_EQ("\"一\t二 \"\n" 10740 "\"\t三 \"\n" 10741 "\"四 五\t六 \"\n" 10742 "\"\t七 \"\n" 10743 "\"八九十\tqq\"", 10744 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10745 getLLVMStyleWithColumns(11))); 10746 10747 // UTF8 character in an escape sequence. 10748 EXPECT_EQ("\"aaaaaa\"\n" 10749 "\"\\\xC2\x8D\"", 10750 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10751 } 10752 10753 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10754 EXPECT_EQ("const char *sssss =\n" 10755 " \"一二三四五六七八\\\n" 10756 " 九 十\";", 10757 format("const char *sssss = \"一二三四五六七八\\\n" 10758 " 九 十\";", 10759 getLLVMStyleWithColumns(30))); 10760 } 10761 10762 TEST_F(FormatTest, SplitsUTF8LineComments) { 10763 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10764 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10765 EXPECT_EQ("// Я из лесу\n" 10766 "// вышел; был\n" 10767 "// сильный\n" 10768 "// мороз.", 10769 format("// Я из лесу вышел; был сильный мороз.", 10770 getLLVMStyleWithColumns(13))); 10771 EXPECT_EQ("// 一二三\n" 10772 "// 四五六七\n" 10773 "// 八 九\n" 10774 "// 十", 10775 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10776 } 10777 10778 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10779 EXPECT_EQ("/* Гляжу,\n" 10780 " * поднимается\n" 10781 " * медленно в\n" 10782 " * гору\n" 10783 " * Лошадка,\n" 10784 " * везущая\n" 10785 " * хворосту\n" 10786 " * воз. */", 10787 format("/* Гляжу, поднимается медленно в гору\n" 10788 " * Лошадка, везущая хворосту воз. */", 10789 getLLVMStyleWithColumns(13))); 10790 EXPECT_EQ( 10791 "/* 一二三\n" 10792 " * 四五六七\n" 10793 " * 八 九\n" 10794 " * 十 */", 10795 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10796 EXPECT_EQ("/* \n" 10797 " * \n" 10798 " * - */", 10799 format("/* - */", getLLVMStyleWithColumns(12))); 10800 } 10801 10802 #endif // _MSC_VER 10803 10804 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10805 FormatStyle Style = getLLVMStyle(); 10806 10807 Style.ConstructorInitializerIndentWidth = 4; 10808 verifyFormat( 10809 "SomeClass::Constructor()\n" 10810 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10811 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10812 Style); 10813 10814 Style.ConstructorInitializerIndentWidth = 2; 10815 verifyFormat( 10816 "SomeClass::Constructor()\n" 10817 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10818 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10819 Style); 10820 10821 Style.ConstructorInitializerIndentWidth = 0; 10822 verifyFormat( 10823 "SomeClass::Constructor()\n" 10824 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10825 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10826 Style); 10827 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10828 verifyFormat( 10829 "SomeLongTemplateVariableName<\n" 10830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10831 Style); 10832 verifyFormat( 10833 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10835 Style); 10836 } 10837 10838 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10839 FormatStyle Style = getLLVMStyle(); 10840 Style.BreakConstructorInitializersBeforeComma = true; 10841 Style.ConstructorInitializerIndentWidth = 4; 10842 verifyFormat("SomeClass::Constructor()\n" 10843 " : a(a)\n" 10844 " , b(b)\n" 10845 " , c(c) {}", 10846 Style); 10847 verifyFormat("SomeClass::Constructor()\n" 10848 " : a(a) {}", 10849 Style); 10850 10851 Style.ColumnLimit = 0; 10852 verifyFormat("SomeClass::Constructor()\n" 10853 " : a(a) {}", 10854 Style); 10855 verifyFormat("SomeClass::Constructor() noexcept\n" 10856 " : a(a) {}", 10857 Style); 10858 verifyFormat("SomeClass::Constructor()\n" 10859 " : a(a)\n" 10860 " , b(b)\n" 10861 " , c(c) {}", 10862 Style); 10863 verifyFormat("SomeClass::Constructor()\n" 10864 " : a(a) {\n" 10865 " foo();\n" 10866 " bar();\n" 10867 "}", 10868 Style); 10869 10870 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10871 verifyFormat("SomeClass::Constructor()\n" 10872 " : a(a)\n" 10873 " , b(b)\n" 10874 " , c(c) {\n}", 10875 Style); 10876 verifyFormat("SomeClass::Constructor()\n" 10877 " : a(a) {\n}", 10878 Style); 10879 10880 Style.ColumnLimit = 80; 10881 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10882 Style.ConstructorInitializerIndentWidth = 2; 10883 verifyFormat("SomeClass::Constructor()\n" 10884 " : a(a)\n" 10885 " , b(b)\n" 10886 " , c(c) {}", 10887 Style); 10888 10889 Style.ConstructorInitializerIndentWidth = 0; 10890 verifyFormat("SomeClass::Constructor()\n" 10891 ": a(a)\n" 10892 ", b(b)\n" 10893 ", c(c) {}", 10894 Style); 10895 10896 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10897 Style.ConstructorInitializerIndentWidth = 4; 10898 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10899 verifyFormat( 10900 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10901 Style); 10902 verifyFormat( 10903 "SomeClass::Constructor()\n" 10904 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10905 Style); 10906 Style.ConstructorInitializerIndentWidth = 4; 10907 Style.ColumnLimit = 60; 10908 verifyFormat("SomeClass::Constructor()\n" 10909 " : aaaaaaaa(aaaaaaaa)\n" 10910 " , aaaaaaaa(aaaaaaaa)\n" 10911 " , aaaaaaaa(aaaaaaaa) {}", 10912 Style); 10913 } 10914 10915 TEST_F(FormatTest, Destructors) { 10916 verifyFormat("void F(int &i) { i.~int(); }"); 10917 verifyFormat("void F(int &i) { i->~int(); }"); 10918 } 10919 10920 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10921 FormatStyle Style = getWebKitStyle(); 10922 10923 // Don't indent in outer namespaces. 10924 verifyFormat("namespace outer {\n" 10925 "int i;\n" 10926 "namespace inner {\n" 10927 " int i;\n" 10928 "} // namespace inner\n" 10929 "} // namespace outer\n" 10930 "namespace other_outer {\n" 10931 "int i;\n" 10932 "}", 10933 Style); 10934 10935 // Don't indent case labels. 10936 verifyFormat("switch (variable) {\n" 10937 "case 1:\n" 10938 "case 2:\n" 10939 " doSomething();\n" 10940 " break;\n" 10941 "default:\n" 10942 " ++variable;\n" 10943 "}", 10944 Style); 10945 10946 // Wrap before binary operators. 10947 EXPECT_EQ("void f()\n" 10948 "{\n" 10949 " if (aaaaaaaaaaaaaaaa\n" 10950 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10951 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10952 " return;\n" 10953 "}", 10954 format("void f() {\n" 10955 "if (aaaaaaaaaaaaaaaa\n" 10956 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10957 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10958 "return;\n" 10959 "}", 10960 Style)); 10961 10962 // Allow functions on a single line. 10963 verifyFormat("void f() { return; }", Style); 10964 10965 // Constructor initializers are formatted one per line with the "," on the 10966 // new line. 10967 verifyFormat("Constructor()\n" 10968 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10969 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10970 " aaaaaaaaaaaaaa)\n" 10971 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10972 "{\n" 10973 "}", 10974 Style); 10975 verifyFormat("SomeClass::Constructor()\n" 10976 " : a(a)\n" 10977 "{\n" 10978 "}", 10979 Style); 10980 EXPECT_EQ("SomeClass::Constructor()\n" 10981 " : a(a)\n" 10982 "{\n" 10983 "}", 10984 format("SomeClass::Constructor():a(a){}", Style)); 10985 verifyFormat("SomeClass::Constructor()\n" 10986 " : a(a)\n" 10987 " , b(b)\n" 10988 " , c(c)\n" 10989 "{\n" 10990 "}", 10991 Style); 10992 verifyFormat("SomeClass::Constructor()\n" 10993 " : a(a)\n" 10994 "{\n" 10995 " foo();\n" 10996 " bar();\n" 10997 "}", 10998 Style); 10999 11000 // Access specifiers should be aligned left. 11001 verifyFormat("class C {\n" 11002 "public:\n" 11003 " int i;\n" 11004 "};", 11005 Style); 11006 11007 // Do not align comments. 11008 verifyFormat("int a; // Do not\n" 11009 "double b; // align comments.", 11010 Style); 11011 11012 // Do not align operands. 11013 EXPECT_EQ("ASSERT(aaaa\n" 11014 " || bbbb);", 11015 format("ASSERT ( aaaa\n||bbbb);", Style)); 11016 11017 // Accept input's line breaks. 11018 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11019 " || bbbbbbbbbbbbbbb) {\n" 11020 " i++;\n" 11021 "}", 11022 format("if (aaaaaaaaaaaaaaa\n" 11023 "|| bbbbbbbbbbbbbbb) { i++; }", 11024 Style)); 11025 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11026 " i++;\n" 11027 "}", 11028 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11029 11030 // Don't automatically break all macro definitions (llvm.org/PR17842). 11031 verifyFormat("#define aNumber 10", Style); 11032 // However, generally keep the line breaks that the user authored. 11033 EXPECT_EQ("#define aNumber \\\n" 11034 " 10", 11035 format("#define aNumber \\\n" 11036 " 10", 11037 Style)); 11038 11039 // Keep empty and one-element array literals on a single line. 11040 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11041 " copyItems:YES];", 11042 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11043 "copyItems:YES];", 11044 Style)); 11045 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11046 " copyItems:YES];", 11047 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11048 " copyItems:YES];", 11049 Style)); 11050 // FIXME: This does not seem right, there should be more indentation before 11051 // the array literal's entries. Nested blocks have the same problem. 11052 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11053 " @\"a\",\n" 11054 " @\"a\"\n" 11055 "]\n" 11056 " copyItems:YES];", 11057 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11058 " @\"a\",\n" 11059 " @\"a\"\n" 11060 " ]\n" 11061 " copyItems:YES];", 11062 Style)); 11063 EXPECT_EQ( 11064 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11065 " copyItems:YES];", 11066 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11067 " copyItems:YES];", 11068 Style)); 11069 11070 verifyFormat("[self.a b:c c:d];", Style); 11071 EXPECT_EQ("[self.a b:c\n" 11072 " c:d];", 11073 format("[self.a b:c\n" 11074 "c:d];", 11075 Style)); 11076 } 11077 11078 TEST_F(FormatTest, FormatsLambdas) { 11079 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11080 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11081 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11082 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11083 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11084 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11085 verifyFormat("int x = f(*+[] {});"); 11086 verifyFormat("void f() {\n" 11087 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11088 "}\n"); 11089 verifyFormat("void f() {\n" 11090 " other(x.begin(), //\n" 11091 " x.end(), //\n" 11092 " [&](int, int) { return 1; });\n" 11093 "}\n"); 11094 verifyFormat("SomeFunction([]() { // A cool function...\n" 11095 " return 43;\n" 11096 "});"); 11097 EXPECT_EQ("SomeFunction([]() {\n" 11098 "#define A a\n" 11099 " return 43;\n" 11100 "});", 11101 format("SomeFunction([](){\n" 11102 "#define A a\n" 11103 "return 43;\n" 11104 "});")); 11105 verifyFormat("void f() {\n" 11106 " SomeFunction([](decltype(x), A *a) {});\n" 11107 "}"); 11108 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11109 " [](const aaaaaaaaaa &a) { return a; });"); 11110 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11111 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11112 "});"); 11113 verifyFormat("Constructor()\n" 11114 " : Field([] { // comment\n" 11115 " int i;\n" 11116 " }) {}"); 11117 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11118 " return some_parameter.size();\n" 11119 "};"); 11120 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11121 " [](const string &s) { return s; };"); 11122 verifyFormat("int i = aaaaaa ? 1 //\n" 11123 " : [] {\n" 11124 " return 2; //\n" 11125 " }();"); 11126 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11127 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11128 " return x == 2; // force break\n" 11129 " });"); 11130 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 11131 " int iiiiiiiiiiii) {\n" 11132 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 11133 "});", 11134 getLLVMStyleWithColumns(60)); 11135 verifyFormat("SomeFunction({[&] {\n" 11136 " // comment\n" 11137 " },\n" 11138 " [&] {\n" 11139 " // comment\n" 11140 " }});"); 11141 verifyFormat("SomeFunction({[&] {\n" 11142 " // comment\n" 11143 "}});"); 11144 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11145 " [&]() { return true; },\n" 11146 " aaaaa aaaaaaaaa);"); 11147 11148 // Lambdas with return types. 11149 verifyFormat("int c = []() -> int { return 2; }();\n"); 11150 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11151 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11152 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11153 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11154 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11155 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11156 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11157 verifyFormat("[a, a]() -> a<1> {};"); 11158 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11159 " int j) -> int {\n" 11160 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11161 "};"); 11162 verifyFormat( 11163 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11164 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11165 " return aaaaaaaaaaaaaaaaa;\n" 11166 " });", 11167 getLLVMStyleWithColumns(70)); 11168 verifyFormat("[]() //\n" 11169 " -> int {\n" 11170 " return 1; //\n" 11171 "};"); 11172 11173 // Multiple lambdas in the same parentheses change indentation rules. 11174 verifyFormat("SomeFunction(\n" 11175 " []() {\n" 11176 " int i = 42;\n" 11177 " return i;\n" 11178 " },\n" 11179 " []() {\n" 11180 " int j = 43;\n" 11181 " return j;\n" 11182 " });"); 11183 11184 // More complex introducers. 11185 verifyFormat("return [i, args...] {};"); 11186 11187 // Not lambdas. 11188 verifyFormat("constexpr char hello[]{\"hello\"};"); 11189 verifyFormat("double &operator[](int i) { return 0; }\n" 11190 "int i;"); 11191 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11192 verifyFormat("int i = a[a][a]->f();"); 11193 verifyFormat("int i = (*b)[a]->f();"); 11194 11195 // Other corner cases. 11196 verifyFormat("void f() {\n" 11197 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11198 " );\n" 11199 "}"); 11200 11201 // Lambdas created through weird macros. 11202 verifyFormat("void f() {\n" 11203 " MACRO((const AA &a) { return 1; });\n" 11204 " MACRO((AA &a) { return 1; });\n" 11205 "}"); 11206 11207 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11208 " doo_dah();\n" 11209 " doo_dah();\n" 11210 " })) {\n" 11211 "}"); 11212 verifyFormat("auto lambda = []() {\n" 11213 " int a = 2\n" 11214 "#if A\n" 11215 " + 2\n" 11216 "#endif\n" 11217 " ;\n" 11218 "};"); 11219 } 11220 11221 TEST_F(FormatTest, FormatsBlocks) { 11222 FormatStyle ShortBlocks = getLLVMStyle(); 11223 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11224 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11225 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11226 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11227 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11228 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11229 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11230 11231 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11232 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11233 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11234 11235 verifyFormat("[operation setCompletionBlock:^{\n" 11236 " [self onOperationDone];\n" 11237 "}];"); 11238 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11239 " [self onOperationDone];\n" 11240 "}]};"); 11241 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11242 " f();\n" 11243 "}];"); 11244 verifyFormat("int a = [operation block:^int(int *i) {\n" 11245 " return 1;\n" 11246 "}];"); 11247 verifyFormat("[myObject doSomethingWith:arg1\n" 11248 " aaa:^int(int *a) {\n" 11249 " return 1;\n" 11250 " }\n" 11251 " bbb:f(a * bbbbbbbb)];"); 11252 11253 verifyFormat("[operation setCompletionBlock:^{\n" 11254 " [self.delegate newDataAvailable];\n" 11255 "}];", 11256 getLLVMStyleWithColumns(60)); 11257 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11258 " NSString *path = [self sessionFilePath];\n" 11259 " if (path) {\n" 11260 " // ...\n" 11261 " }\n" 11262 "});"); 11263 verifyFormat("[[SessionService sharedService]\n" 11264 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11265 " if (window) {\n" 11266 " [self windowDidLoad:window];\n" 11267 " } else {\n" 11268 " [self errorLoadingWindow];\n" 11269 " }\n" 11270 " }];"); 11271 verifyFormat("void (^largeBlock)(void) = ^{\n" 11272 " // ...\n" 11273 "};\n", 11274 getLLVMStyleWithColumns(40)); 11275 verifyFormat("[[SessionService sharedService]\n" 11276 " loadWindowWithCompletionBlock: //\n" 11277 " ^(SessionWindow *window) {\n" 11278 " if (window) {\n" 11279 " [self windowDidLoad:window];\n" 11280 " } else {\n" 11281 " [self errorLoadingWindow];\n" 11282 " }\n" 11283 " }];", 11284 getLLVMStyleWithColumns(60)); 11285 verifyFormat("[myObject doSomethingWith:arg1\n" 11286 " firstBlock:^(Foo *a) {\n" 11287 " // ...\n" 11288 " int i;\n" 11289 " }\n" 11290 " secondBlock:^(Bar *b) {\n" 11291 " // ...\n" 11292 " int i;\n" 11293 " }\n" 11294 " thirdBlock:^Foo(Bar *b) {\n" 11295 " // ...\n" 11296 " int i;\n" 11297 " }];"); 11298 verifyFormat("[myObject doSomethingWith:arg1\n" 11299 " firstBlock:-1\n" 11300 " secondBlock:^(Bar *b) {\n" 11301 " // ...\n" 11302 " int i;\n" 11303 " }];"); 11304 11305 verifyFormat("f(^{\n" 11306 " @autoreleasepool {\n" 11307 " if (a) {\n" 11308 " g();\n" 11309 " }\n" 11310 " }\n" 11311 "});"); 11312 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11313 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11314 "};"); 11315 11316 FormatStyle FourIndent = getLLVMStyle(); 11317 FourIndent.ObjCBlockIndentWidth = 4; 11318 verifyFormat("[operation setCompletionBlock:^{\n" 11319 " [self onOperationDone];\n" 11320 "}];", 11321 FourIndent); 11322 } 11323 11324 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11325 FormatStyle ZeroColumn = getLLVMStyle(); 11326 ZeroColumn.ColumnLimit = 0; 11327 11328 verifyFormat("[[SessionService sharedService] " 11329 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11330 " if (window) {\n" 11331 " [self windowDidLoad:window];\n" 11332 " } else {\n" 11333 " [self errorLoadingWindow];\n" 11334 " }\n" 11335 "}];", 11336 ZeroColumn); 11337 EXPECT_EQ("[[SessionService sharedService]\n" 11338 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11339 " if (window) {\n" 11340 " [self windowDidLoad:window];\n" 11341 " } else {\n" 11342 " [self errorLoadingWindow];\n" 11343 " }\n" 11344 " }];", 11345 format("[[SessionService sharedService]\n" 11346 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11347 " if (window) {\n" 11348 " [self windowDidLoad:window];\n" 11349 " } else {\n" 11350 " [self errorLoadingWindow];\n" 11351 " }\n" 11352 "}];", 11353 ZeroColumn)); 11354 verifyFormat("[myObject doSomethingWith:arg1\n" 11355 " firstBlock:^(Foo *a) {\n" 11356 " // ...\n" 11357 " int i;\n" 11358 " }\n" 11359 " secondBlock:^(Bar *b) {\n" 11360 " // ...\n" 11361 " int i;\n" 11362 " }\n" 11363 " thirdBlock:^Foo(Bar *b) {\n" 11364 " // ...\n" 11365 " int i;\n" 11366 " }];", 11367 ZeroColumn); 11368 verifyFormat("f(^{\n" 11369 " @autoreleasepool {\n" 11370 " if (a) {\n" 11371 " g();\n" 11372 " }\n" 11373 " }\n" 11374 "});", 11375 ZeroColumn); 11376 verifyFormat("void (^largeBlock)(void) = ^{\n" 11377 " // ...\n" 11378 "};", 11379 ZeroColumn); 11380 11381 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11382 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11383 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11384 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11385 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11386 " int i;\n" 11387 "};", 11388 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11389 } 11390 11391 TEST_F(FormatTest, SupportsCRLF) { 11392 EXPECT_EQ("int a;\r\n" 11393 "int b;\r\n" 11394 "int c;\r\n", 11395 format("int a;\r\n" 11396 " int b;\r\n" 11397 " int c;\r\n", 11398 getLLVMStyle())); 11399 EXPECT_EQ("int a;\r\n" 11400 "int b;\r\n" 11401 "int c;\r\n", 11402 format("int a;\r\n" 11403 " int b;\n" 11404 " int c;\r\n", 11405 getLLVMStyle())); 11406 EXPECT_EQ("int a;\n" 11407 "int b;\n" 11408 "int c;\n", 11409 format("int a;\r\n" 11410 " int b;\n" 11411 " int c;\n", 11412 getLLVMStyle())); 11413 EXPECT_EQ("\"aaaaaaa \"\r\n" 11414 "\"bbbbbbb\";\r\n", 11415 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11416 EXPECT_EQ("#define A \\\r\n" 11417 " b; \\\r\n" 11418 " c; \\\r\n" 11419 " d;\r\n", 11420 format("#define A \\\r\n" 11421 " b; \\\r\n" 11422 " c; d; \r\n", 11423 getGoogleStyle())); 11424 11425 EXPECT_EQ("/*\r\n" 11426 "multi line block comments\r\n" 11427 "should not introduce\r\n" 11428 "an extra carriage return\r\n" 11429 "*/\r\n", 11430 format("/*\r\n" 11431 "multi line block comments\r\n" 11432 "should not introduce\r\n" 11433 "an extra carriage return\r\n" 11434 "*/\r\n")); 11435 } 11436 11437 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11438 verifyFormat("MY_CLASS(C) {\n" 11439 " int i;\n" 11440 " int j;\n" 11441 "};"); 11442 } 11443 11444 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11445 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11446 TwoIndent.ContinuationIndentWidth = 2; 11447 11448 EXPECT_EQ("int i =\n" 11449 " longFunction(\n" 11450 " arg);", 11451 format("int i = longFunction(arg);", TwoIndent)); 11452 11453 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11454 SixIndent.ContinuationIndentWidth = 6; 11455 11456 EXPECT_EQ("int i =\n" 11457 " longFunction(\n" 11458 " arg);", 11459 format("int i = longFunction(arg);", SixIndent)); 11460 } 11461 11462 TEST_F(FormatTest, SpacesInAngles) { 11463 FormatStyle Spaces = getLLVMStyle(); 11464 Spaces.SpacesInAngles = true; 11465 11466 verifyFormat("static_cast< int >(arg);", Spaces); 11467 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11468 verifyFormat("f< int, float >();", Spaces); 11469 verifyFormat("template <> g() {}", Spaces); 11470 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11471 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11472 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11473 Spaces); 11474 11475 Spaces.Standard = FormatStyle::LS_Cpp03; 11476 Spaces.SpacesInAngles = true; 11477 verifyFormat("A< A< int > >();", Spaces); 11478 11479 Spaces.SpacesInAngles = false; 11480 verifyFormat("A<A<int> >();", Spaces); 11481 11482 Spaces.Standard = FormatStyle::LS_Cpp11; 11483 Spaces.SpacesInAngles = true; 11484 verifyFormat("A< A< int > >();", Spaces); 11485 11486 Spaces.SpacesInAngles = false; 11487 verifyFormat("A<A<int>>();", Spaces); 11488 } 11489 11490 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11491 FormatStyle Style = getLLVMStyle(); 11492 Style.SpaceAfterTemplateKeyword = false; 11493 verifyFormat("template<int> void foo();", Style); 11494 } 11495 11496 TEST_F(FormatTest, TripleAngleBrackets) { 11497 verifyFormat("f<<<1, 1>>>();"); 11498 verifyFormat("f<<<1, 1, 1, s>>>();"); 11499 verifyFormat("f<<<a, b, c, d>>>();"); 11500 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11501 verifyFormat("f<param><<<1, 1>>>();"); 11502 verifyFormat("f<1><<<1, 1>>>();"); 11503 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11504 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11505 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11506 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11507 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11508 } 11509 11510 TEST_F(FormatTest, MergeLessLessAtEnd) { 11511 verifyFormat("<<"); 11512 EXPECT_EQ("< < <", format("\\\n<<<")); 11513 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11514 "aaallvm::outs() <<"); 11515 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11516 "aaaallvm::outs()\n <<"); 11517 } 11518 11519 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11520 std::string code = "#if A\n" 11521 "#if B\n" 11522 "a.\n" 11523 "#endif\n" 11524 " a = 1;\n" 11525 "#else\n" 11526 "#endif\n" 11527 "#if C\n" 11528 "#else\n" 11529 "#endif\n"; 11530 EXPECT_EQ(code, format(code)); 11531 } 11532 11533 TEST_F(FormatTest, HandleConflictMarkers) { 11534 // Git/SVN conflict markers. 11535 EXPECT_EQ("int a;\n" 11536 "void f() {\n" 11537 " callme(some(parameter1,\n" 11538 "<<<<<<< text by the vcs\n" 11539 " parameter2),\n" 11540 "||||||| text by the vcs\n" 11541 " parameter2),\n" 11542 " parameter3,\n" 11543 "======= text by the vcs\n" 11544 " parameter2, parameter3),\n" 11545 ">>>>>>> text by the vcs\n" 11546 " otherparameter);\n", 11547 format("int a;\n" 11548 "void f() {\n" 11549 " callme(some(parameter1,\n" 11550 "<<<<<<< text by the vcs\n" 11551 " parameter2),\n" 11552 "||||||| text by the vcs\n" 11553 " parameter2),\n" 11554 " parameter3,\n" 11555 "======= text by the vcs\n" 11556 " parameter2,\n" 11557 " parameter3),\n" 11558 ">>>>>>> text by the vcs\n" 11559 " otherparameter);\n")); 11560 11561 // Perforce markers. 11562 EXPECT_EQ("void f() {\n" 11563 " function(\n" 11564 ">>>> text by the vcs\n" 11565 " parameter,\n" 11566 "==== text by the vcs\n" 11567 " parameter,\n" 11568 "==== text by the vcs\n" 11569 " parameter,\n" 11570 "<<<< text by the vcs\n" 11571 " parameter);\n", 11572 format("void f() {\n" 11573 " function(\n" 11574 ">>>> text by the vcs\n" 11575 " parameter,\n" 11576 "==== text by the vcs\n" 11577 " parameter,\n" 11578 "==== text by the vcs\n" 11579 " parameter,\n" 11580 "<<<< text by the vcs\n" 11581 " parameter);\n")); 11582 11583 EXPECT_EQ("<<<<<<<\n" 11584 "|||||||\n" 11585 "=======\n" 11586 ">>>>>>>", 11587 format("<<<<<<<\n" 11588 "|||||||\n" 11589 "=======\n" 11590 ">>>>>>>")); 11591 11592 EXPECT_EQ("<<<<<<<\n" 11593 "|||||||\n" 11594 "int i;\n" 11595 "=======\n" 11596 ">>>>>>>", 11597 format("<<<<<<<\n" 11598 "|||||||\n" 11599 "int i;\n" 11600 "=======\n" 11601 ">>>>>>>")); 11602 11603 // FIXME: Handle parsing of macros around conflict markers correctly: 11604 EXPECT_EQ("#define Macro \\\n" 11605 "<<<<<<<\n" 11606 "Something \\\n" 11607 "|||||||\n" 11608 "Else \\\n" 11609 "=======\n" 11610 "Other \\\n" 11611 ">>>>>>>\n" 11612 " End int i;\n", 11613 format("#define Macro \\\n" 11614 "<<<<<<<\n" 11615 " Something \\\n" 11616 "|||||||\n" 11617 " Else \\\n" 11618 "=======\n" 11619 " Other \\\n" 11620 ">>>>>>>\n" 11621 " End\n" 11622 "int i;\n")); 11623 } 11624 11625 TEST_F(FormatTest, DisableRegions) { 11626 EXPECT_EQ("int i;\n" 11627 "// clang-format off\n" 11628 " int j;\n" 11629 "// clang-format on\n" 11630 "int k;", 11631 format(" int i;\n" 11632 " // clang-format off\n" 11633 " int j;\n" 11634 " // clang-format on\n" 11635 " int k;")); 11636 EXPECT_EQ("int i;\n" 11637 "/* clang-format off */\n" 11638 " int j;\n" 11639 "/* clang-format on */\n" 11640 "int k;", 11641 format(" int i;\n" 11642 " /* clang-format off */\n" 11643 " int j;\n" 11644 " /* clang-format on */\n" 11645 " int k;")); 11646 11647 // Don't reflow comments within disabled regions. 11648 EXPECT_EQ( 11649 "// clang-format off\n" 11650 "// long long long long long long line\n" 11651 "/* clang-format on */\n" 11652 "/* long long long\n" 11653 " * long long long\n" 11654 " * line */\n" 11655 "int i;\n" 11656 "/* clang-format off */\n" 11657 "/* long long long long long long line */\n", 11658 format("// clang-format off\n" 11659 "// long long long long long long line\n" 11660 "/* clang-format on */\n" 11661 "/* long long long long long long line */\n" 11662 "int i;\n" 11663 "/* clang-format off */\n" 11664 "/* long long long long long long line */\n", 11665 getLLVMStyleWithColumns(20))); 11666 } 11667 11668 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11669 format("? ) ="); 11670 verifyNoCrash("#define a\\\n /**/}"); 11671 } 11672 11673 TEST_F(FormatTest, FormatsTableGenCode) { 11674 FormatStyle Style = getLLVMStyle(); 11675 Style.Language = FormatStyle::LK_TableGen; 11676 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11677 } 11678 11679 TEST_F(FormatTest, ArrayOfTemplates) { 11680 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11681 format("auto a = new unique_ptr<int > [ 10];")); 11682 11683 FormatStyle Spaces = getLLVMStyle(); 11684 Spaces.SpacesInSquareBrackets = true; 11685 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11686 format("auto a = new unique_ptr<int > [10];", Spaces)); 11687 } 11688 11689 TEST_F(FormatTest, ArrayAsTemplateType) { 11690 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11691 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11692 11693 FormatStyle Spaces = getLLVMStyle(); 11694 Spaces.SpacesInSquareBrackets = true; 11695 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11696 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11697 } 11698 11699 TEST(FormatStyle, GetStyleOfFile) { 11700 vfs::InMemoryFileSystem FS; 11701 // Test 1: format file in the same directory. 11702 ASSERT_TRUE( 11703 FS.addFile("/a/.clang-format", 0, 11704 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11705 ASSERT_TRUE( 11706 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11707 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11708 ASSERT_TRUE((bool)Style1); 11709 ASSERT_EQ(*Style1, getLLVMStyle()); 11710 11711 // Test 2.1: fallback to default. 11712 ASSERT_TRUE( 11713 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11714 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11715 ASSERT_TRUE((bool)Style2); 11716 ASSERT_EQ(*Style2, getMozillaStyle()); 11717 11718 // Test 2.2: no format on 'none' fallback style. 11719 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11720 ASSERT_TRUE((bool)Style2); 11721 ASSERT_EQ(*Style2, getNoStyle()); 11722 11723 // Test 2.3: format if config is found with no based style while fallback is 11724 // 'none'. 11725 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11726 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11727 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11728 ASSERT_TRUE((bool)Style2); 11729 ASSERT_EQ(*Style2, getLLVMStyle()); 11730 11731 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11732 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11733 ASSERT_TRUE((bool)Style2); 11734 ASSERT_EQ(*Style2, getLLVMStyle()); 11735 11736 // Test 3: format file in parent directory. 11737 ASSERT_TRUE( 11738 FS.addFile("/c/.clang-format", 0, 11739 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11740 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11741 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11742 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11743 ASSERT_TRUE((bool)Style3); 11744 ASSERT_EQ(*Style3, getGoogleStyle()); 11745 11746 // Test 4: error on invalid fallback style 11747 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11748 ASSERT_FALSE((bool)Style4); 11749 llvm::consumeError(Style4.takeError()); 11750 11751 // Test 5: error on invalid yaml on command line 11752 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11753 ASSERT_FALSE((bool)Style5); 11754 llvm::consumeError(Style5.takeError()); 11755 11756 // Test 6: error on invalid style 11757 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11758 ASSERT_FALSE((bool)Style6); 11759 llvm::consumeError(Style6.takeError()); 11760 11761 // Test 7: found config file, error on parsing it 11762 ASSERT_TRUE( 11763 FS.addFile("/d/.clang-format", 0, 11764 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11765 "InvalidKey: InvalidValue"))); 11766 ASSERT_TRUE( 11767 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11768 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11769 ASSERT_FALSE((bool)Style7); 11770 llvm::consumeError(Style7.takeError()); 11771 } 11772 11773 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11774 // Column limit is 20. 11775 std::string Code = "Type *a =\n" 11776 " new Type();\n" 11777 "g(iiiii, 0, jjjjj,\n" 11778 " 0, kkkkk, 0, mm);\n" 11779 "int bad = format ;"; 11780 std::string Expected = "auto a = new Type();\n" 11781 "g(iiiii, nullptr,\n" 11782 " jjjjj, nullptr,\n" 11783 " kkkkk, nullptr,\n" 11784 " mm);\n" 11785 "int bad = format ;"; 11786 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11787 tooling::Replacements Replaces = toReplacements( 11788 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11789 "auto "), 11790 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11791 "nullptr"), 11792 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11793 "nullptr"), 11794 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11795 "nullptr")}); 11796 11797 format::FormatStyle Style = format::getLLVMStyle(); 11798 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11799 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11800 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11801 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11802 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11803 EXPECT_TRUE(static_cast<bool>(Result)); 11804 EXPECT_EQ(Expected, *Result); 11805 } 11806 11807 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11808 std::string Code = "#include \"a.h\"\n" 11809 "#include \"c.h\"\n" 11810 "\n" 11811 "int main() {\n" 11812 " return 0;\n" 11813 "}"; 11814 std::string Expected = "#include \"a.h\"\n" 11815 "#include \"b.h\"\n" 11816 "#include \"c.h\"\n" 11817 "\n" 11818 "int main() {\n" 11819 " return 0;\n" 11820 "}"; 11821 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11822 tooling::Replacements Replaces = toReplacements( 11823 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11824 "#include \"b.h\"\n")}); 11825 11826 format::FormatStyle Style = format::getLLVMStyle(); 11827 Style.SortIncludes = true; 11828 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11829 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11830 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11831 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11832 EXPECT_TRUE(static_cast<bool>(Result)); 11833 EXPECT_EQ(Expected, *Result); 11834 } 11835 11836 TEST_F(FormatTest, AlignTrailingComments) { 11837 EXPECT_EQ("#define MACRO(V) \\\n" 11838 " V(Rt2) /* one more char */ \\\n" 11839 " V(Rs) /* than here */ \\\n" 11840 "/* comment 3 */\n", 11841 format("#define MACRO(V)\\\n" 11842 "V(Rt2) /* one more char */ \\\n" 11843 "V(Rs) /* than here */ \\\n" 11844 "/* comment 3 */ \\\n", 11845 getLLVMStyleWithColumns(40))); 11846 EXPECT_EQ("int i = f(abc, // line 1\n" 11847 " d, // line 2\n" 11848 " // line 3\n" 11849 " b);", 11850 format("int i = f(abc, // line 1\n" 11851 " d, // line 2\n" 11852 " // line 3\n" 11853 " b);", 11854 getLLVMStyleWithColumns(40))); 11855 11856 // Align newly broken trailing comments. 11857 EXPECT_EQ("int ab; // line\n" 11858 "int a; // long\n" 11859 " // long\n", 11860 format("int ab; // line\n" 11861 "int a; // long long\n", 11862 getLLVMStyleWithColumns(15))); 11863 EXPECT_EQ("int ab; // line\n" 11864 "int a; // long\n" 11865 " // long\n" 11866 " // long", 11867 format("int ab; // line\n" 11868 "int a; // long long\n" 11869 " // long", 11870 getLLVMStyleWithColumns(15))); 11871 EXPECT_EQ("int ab; // line\n" 11872 "int a; // long\n" 11873 " // long\n" 11874 "pt c; // long", 11875 format("int ab; // line\n" 11876 "int a; // long long\n" 11877 "pt c; // long", 11878 getLLVMStyleWithColumns(15))); 11879 EXPECT_EQ("int ab; // line\n" 11880 "int a; // long\n" 11881 " // long\n" 11882 "\n" 11883 "// long", 11884 format("int ab; // line\n" 11885 "int a; // long long\n" 11886 "\n" 11887 "// long", 11888 getLLVMStyleWithColumns(15))); 11889 } 11890 } // end namespace 11891 } // end namespace format 11892 } // end namespace clang 11893