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 verifyFormat( 938 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 939 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 940 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 941 " // Comment inside a statement.\n" 942 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 943 verifyFormat("SomeFunction(a,\n" 944 " // comment\n" 945 " b + x);"); 946 verifyFormat("SomeFunction(a, a,\n" 947 " // comment\n" 948 " b + x);"); 949 verifyFormat( 950 "bool aaaaaaaaaaaaa = // comment\n" 951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 953 954 verifyFormat("int aaaa; // aaaaa\n" 955 "int aa; // aaaaaaa", 956 getLLVMStyleWithColumns(20)); 957 958 EXPECT_EQ("void f() { // This does something ..\n" 959 "}\n" 960 "int a; // This is unrelated", 961 format("void f() { // This does something ..\n" 962 " }\n" 963 "int a; // This is unrelated")); 964 EXPECT_EQ("class C {\n" 965 " void f() { // This does something ..\n" 966 " } // awesome..\n" 967 "\n" 968 " int a; // This is unrelated\n" 969 "};", 970 format("class C{void f() { // This does something ..\n" 971 " } // awesome..\n" 972 " \n" 973 "int a; // This is unrelated\n" 974 "};")); 975 976 EXPECT_EQ("int i; // single line trailing comment", 977 format("int i;\\\n// single line trailing comment")); 978 979 verifyGoogleFormat("int a; // Trailing comment."); 980 981 verifyFormat("someFunction(anotherFunction( // Force break.\n" 982 " parameter));"); 983 984 verifyGoogleFormat("#endif // HEADER_GUARD"); 985 986 verifyFormat("const char *test[] = {\n" 987 " // A\n" 988 " \"aaaa\",\n" 989 " // B\n" 990 " \"aaaaa\"};"); 991 verifyGoogleFormat( 992 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 993 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 994 EXPECT_EQ("D(a, {\n" 995 " // test\n" 996 " int a;\n" 997 "});", 998 format("D(a, {\n" 999 "// test\n" 1000 "int a;\n" 1001 "});")); 1002 1003 EXPECT_EQ("lineWith(); // comment\n" 1004 "// at start\n" 1005 "otherLine();", 1006 format("lineWith(); // comment\n" 1007 "// at start\n" 1008 "otherLine();")); 1009 EXPECT_EQ("lineWith(); // comment\n" 1010 "/*\n" 1011 " * at start */\n" 1012 "otherLine();", 1013 format("lineWith(); // comment\n" 1014 "/*\n" 1015 " * at start */\n" 1016 "otherLine();")); 1017 EXPECT_EQ("lineWith(); // comment\n" 1018 " // at start\n" 1019 "otherLine();", 1020 format("lineWith(); // comment\n" 1021 " // at start\n" 1022 "otherLine();")); 1023 1024 EXPECT_EQ("lineWith(); // comment\n" 1025 "// at start\n" 1026 "otherLine(); // comment", 1027 format("lineWith(); // comment\n" 1028 "// at start\n" 1029 "otherLine(); // comment")); 1030 EXPECT_EQ("lineWith();\n" 1031 "// at start\n" 1032 "otherLine(); // comment", 1033 format("lineWith();\n" 1034 " // at start\n" 1035 "otherLine(); // comment")); 1036 EXPECT_EQ("// first\n" 1037 "// at start\n" 1038 "otherLine(); // comment", 1039 format("// first\n" 1040 " // at start\n" 1041 "otherLine(); // comment")); 1042 EXPECT_EQ("f();\n" 1043 "// first\n" 1044 "// at start\n" 1045 "otherLine(); // comment", 1046 format("f();\n" 1047 "// first\n" 1048 " // at start\n" 1049 "otherLine(); // comment")); 1050 verifyFormat("f(); // comment\n" 1051 "// first\n" 1052 "// at start\n" 1053 "otherLine();"); 1054 EXPECT_EQ("f(); // comment\n" 1055 "// first\n" 1056 "// at start\n" 1057 "otherLine();", 1058 format("f(); // comment\n" 1059 "// first\n" 1060 " // at start\n" 1061 "otherLine();")); 1062 EXPECT_EQ("f(); // comment\n" 1063 " // first\n" 1064 "// at start\n" 1065 "otherLine();", 1066 format("f(); // comment\n" 1067 " // first\n" 1068 "// at start\n" 1069 "otherLine();")); 1070 EXPECT_EQ("void f() {\n" 1071 " lineWith(); // comment\n" 1072 " // at start\n" 1073 "}", 1074 format("void f() {\n" 1075 " lineWith(); // comment\n" 1076 " // at start\n" 1077 "}")); 1078 EXPECT_EQ("int xy; // a\n" 1079 "int z; // b", 1080 format("int xy; // a\n" 1081 "int z; //b")); 1082 EXPECT_EQ("int xy; // a\n" 1083 "int z; // bb", 1084 format("int xy; // a\n" 1085 "int z; //bb", 1086 getLLVMStyleWithColumns(12))); 1087 1088 verifyFormat("#define A \\\n" 1089 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1090 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1091 getLLVMStyleWithColumns(60)); 1092 verifyFormat( 1093 "#define A \\\n" 1094 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1095 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1096 getLLVMStyleWithColumns(61)); 1097 1098 verifyFormat("if ( // This is some comment\n" 1099 " x + 3) {\n" 1100 "}"); 1101 EXPECT_EQ("if ( // This is some comment\n" 1102 " // spanning two lines\n" 1103 " x + 3) {\n" 1104 "}", 1105 format("if( // This is some comment\n" 1106 " // spanning two lines\n" 1107 " x + 3) {\n" 1108 "}")); 1109 1110 verifyNoCrash("/\\\n/"); 1111 verifyNoCrash("/\\\n* */"); 1112 // The 0-character somehow makes the lexer return a proper comment. 1113 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1114 } 1115 1116 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1117 EXPECT_EQ("SomeFunction(a,\n" 1118 " b, // comment\n" 1119 " c);", 1120 format("SomeFunction(a,\n" 1121 " b, // comment\n" 1122 " c);")); 1123 EXPECT_EQ("SomeFunction(a, b,\n" 1124 " // comment\n" 1125 " c);", 1126 format("SomeFunction(a,\n" 1127 " b,\n" 1128 " // comment\n" 1129 " c);")); 1130 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1131 " c);", 1132 format("SomeFunction(a, b, // comment (unclear relation)\n" 1133 " c);")); 1134 EXPECT_EQ("SomeFunction(a, // comment\n" 1135 " b,\n" 1136 " c); // comment", 1137 format("SomeFunction(a, // comment\n" 1138 " b,\n" 1139 " c); // comment")); 1140 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" 1141 " aaaa), //\n" 1142 " aaaa, bbbbb);", 1143 format("aaaaaaaaaa(aaaa(aaaa,\n" 1144 "aaaa), //\n" 1145 "aaaa, bbbbb);")); 1146 } 1147 1148 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1149 EXPECT_EQ("// comment", format("// comment ")); 1150 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1151 format("int aaaaaaa, bbbbbbb; // comment ", 1152 getLLVMStyleWithColumns(33))); 1153 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1154 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1155 } 1156 1157 TEST_F(FormatTest, UnderstandsBlockComments) { 1158 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1159 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 1160 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1161 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1162 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1163 "/* Trailing comment for aa... */\n" 1164 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1165 EXPECT_EQ( 1166 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1167 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1168 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1169 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1170 EXPECT_EQ( 1171 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1172 " aaaaaaaaaaaaaaaaaa,\n" 1173 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1174 "}", 1175 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1176 " aaaaaaaaaaaaaaaaaa ,\n" 1177 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1178 "}")); 1179 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 1180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1181 1182 FormatStyle NoBinPacking = getLLVMStyle(); 1183 NoBinPacking.BinPackParameters = false; 1184 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1185 " /* parameter 2 */ aaaaaa,\n" 1186 " /* parameter 3 */ aaaaaa,\n" 1187 " /* parameter 4 */ aaaaaa);", 1188 NoBinPacking); 1189 1190 // Aligning block comments in macros. 1191 verifyGoogleFormat("#define A \\\n" 1192 " int i; /*a*/ \\\n" 1193 " int jjj; /*b*/"); 1194 } 1195 1196 TEST_F(FormatTest, AlignsBlockComments) { 1197 EXPECT_EQ("/*\n" 1198 " * Really multi-line\n" 1199 " * comment.\n" 1200 " */\n" 1201 "void f() {}", 1202 format(" /*\n" 1203 " * Really multi-line\n" 1204 " * comment.\n" 1205 " */\n" 1206 " void f() {}")); 1207 EXPECT_EQ("class C {\n" 1208 " /*\n" 1209 " * Another multi-line\n" 1210 " * comment.\n" 1211 " */\n" 1212 " void f() {}\n" 1213 "};", 1214 format("class C {\n" 1215 "/*\n" 1216 " * Another multi-line\n" 1217 " * comment.\n" 1218 " */\n" 1219 "void f() {}\n" 1220 "};")); 1221 EXPECT_EQ("/*\n" 1222 " 1. This is a comment with non-trivial formatting.\n" 1223 " 1.1. We have to indent/outdent all lines equally\n" 1224 " 1.1.1. to keep the formatting.\n" 1225 " */", 1226 format(" /*\n" 1227 " 1. This is a comment with non-trivial formatting.\n" 1228 " 1.1. We have to indent/outdent all lines equally\n" 1229 " 1.1.1. to keep the formatting.\n" 1230 " */")); 1231 EXPECT_EQ("/*\n" 1232 "Don't try to outdent if there's not enough indentation.\n" 1233 "*/", 1234 format(" /*\n" 1235 " Don't try to outdent if there's not enough indentation.\n" 1236 " */")); 1237 1238 EXPECT_EQ("int i; /* Comment with empty...\n" 1239 " *\n" 1240 " * line. */", 1241 format("int i; /* Comment with empty...\n" 1242 " *\n" 1243 " * line. */")); 1244 EXPECT_EQ("int foobar = 0; /* comment */\n" 1245 "int bar = 0; /* multiline\n" 1246 " comment 1 */\n" 1247 "int baz = 0; /* multiline\n" 1248 " comment 2 */\n" 1249 "int bzz = 0; /* multiline\n" 1250 " comment 3 */", 1251 format("int foobar = 0; /* comment */\n" 1252 "int bar = 0; /* multiline\n" 1253 " comment 1 */\n" 1254 "int baz = 0; /* multiline\n" 1255 " comment 2 */\n" 1256 "int bzz = 0; /* multiline\n" 1257 " comment 3 */")); 1258 EXPECT_EQ("int foobar = 0; /* comment */\n" 1259 "int bar = 0; /* multiline\n" 1260 " comment */\n" 1261 "int baz = 0; /* multiline\n" 1262 "comment */", 1263 format("int foobar = 0; /* comment */\n" 1264 "int bar = 0; /* multiline\n" 1265 "comment */\n" 1266 "int baz = 0; /* multiline\n" 1267 "comment */")); 1268 } 1269 1270 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1271 FormatStyle Style = getLLVMStyleWithColumns(20); 1272 Style.ReflowComments = false; 1273 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1274 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1275 } 1276 1277 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1278 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1280 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1282 EXPECT_EQ( 1283 "void ffffffffffff(\n" 1284 " int aaaaaaaa, int bbbbbbbb,\n" 1285 " int cccccccccccc) { /*\n" 1286 " aaaaaaaaaa\n" 1287 " aaaaaaaaaaaaa\n" 1288 " bbbbbbbbbbbbbb\n" 1289 " bbbbbbbbbb\n" 1290 " */\n" 1291 "}", 1292 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1293 "{ /*\n" 1294 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1295 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1296 " */\n" 1297 "}", 1298 getLLVMStyleWithColumns(40))); 1299 } 1300 1301 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1302 EXPECT_EQ("void ffffffffff(\n" 1303 " int aaaaa /* test */);", 1304 format("void ffffffffff(int aaaaa /* test */);", 1305 getLLVMStyleWithColumns(35))); 1306 } 1307 1308 TEST_F(FormatTest, SplitsLongCxxComments) { 1309 EXPECT_EQ("// A comment that\n" 1310 "// doesn't fit on\n" 1311 "// one line", 1312 format("// A comment that doesn't fit on one line", 1313 getLLVMStyleWithColumns(20))); 1314 EXPECT_EQ("/// A comment that\n" 1315 "/// doesn't fit on\n" 1316 "/// one line", 1317 format("/// A comment that doesn't fit on one line", 1318 getLLVMStyleWithColumns(20))); 1319 EXPECT_EQ("//! A comment that\n" 1320 "//! doesn't fit on\n" 1321 "//! one line", 1322 format("//! A comment that doesn't fit on one line", 1323 getLLVMStyleWithColumns(20))); 1324 EXPECT_EQ("// a b c d\n" 1325 "// e f g\n" 1326 "// h i j k", 1327 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1328 EXPECT_EQ( 1329 "// a b c d\n" 1330 "// e f g\n" 1331 "// h i j k", 1332 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1333 EXPECT_EQ("if (true) // A comment that\n" 1334 " // doesn't fit on\n" 1335 " // one line", 1336 format("if (true) // A comment that doesn't fit on one line ", 1337 getLLVMStyleWithColumns(30))); 1338 EXPECT_EQ("// Don't_touch_leading_whitespace", 1339 format("// Don't_touch_leading_whitespace", 1340 getLLVMStyleWithColumns(20))); 1341 EXPECT_EQ("// Add leading\n" 1342 "// whitespace", 1343 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1344 EXPECT_EQ("/// Add leading\n" 1345 "/// whitespace", 1346 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1347 EXPECT_EQ("//! Add leading\n" 1348 "//! whitespace", 1349 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1350 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1351 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1352 "// limit", 1353 format("//Even if it makes the line exceed the column limit", 1354 getLLVMStyleWithColumns(51))); 1355 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1356 1357 EXPECT_EQ("// aa bb cc dd", 1358 format("// aa bb cc dd ", 1359 getLLVMStyleWithColumns(15))); 1360 1361 EXPECT_EQ("// A comment before\n" 1362 "// a macro\n" 1363 "// definition\n" 1364 "#define a b", 1365 format("// A comment before a macro definition\n" 1366 "#define a b", 1367 getLLVMStyleWithColumns(20))); 1368 EXPECT_EQ("void ffffff(\n" 1369 " int aaaaaaaaa, // wwww\n" 1370 " int bbbbbbbbbb, // xxxxxxx\n" 1371 " // yyyyyyyyyy\n" 1372 " int c, int d, int e) {}", 1373 format("void ffffff(\n" 1374 " int aaaaaaaaa, // wwww\n" 1375 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1376 " int c, int d, int e) {}", 1377 getLLVMStyleWithColumns(40))); 1378 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1379 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1380 getLLVMStyleWithColumns(20))); 1381 EXPECT_EQ( 1382 "#define XXX // a b c d\n" 1383 " // e f g h", 1384 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1385 EXPECT_EQ( 1386 "#define XXX // q w e r\n" 1387 " // t y u i", 1388 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1389 } 1390 1391 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1392 EXPECT_EQ("// A comment\n" 1393 "// that doesn't\n" 1394 "// fit on one\n" 1395 "// line", 1396 format("// A comment that doesn't fit on one line", 1397 getLLVMStyleWithColumns(20))); 1398 EXPECT_EQ("/// A comment\n" 1399 "/// that doesn't\n" 1400 "/// fit on one\n" 1401 "/// line", 1402 format("/// A comment that doesn't fit on one line", 1403 getLLVMStyleWithColumns(20))); 1404 } 1405 1406 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1407 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1408 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1409 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1410 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1411 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1412 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1413 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1414 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1415 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1416 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1417 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1418 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1419 getLLVMStyleWithColumns(50))); 1420 // FIXME: One day we might want to implement adjustment of leading whitespace 1421 // of the consecutive lines in this kind of comment: 1422 EXPECT_EQ("double\n" 1423 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1424 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1425 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1426 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1427 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1428 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1429 getLLVMStyleWithColumns(49))); 1430 } 1431 1432 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1433 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1434 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1435 EXPECT_EQ( 1436 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1437 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1438 EXPECT_EQ( 1439 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1440 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1441 } 1442 1443 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1444 EXPECT_EQ("if (xxx ==\n" 1445 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1446 " zzz)\n" 1447 " q();", 1448 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1449 " zzz) q();", 1450 getLLVMStyleWithColumns(40))); 1451 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1452 " yyy && // aaaaaa bbbbbbbb cccc\n" 1453 " zzz)\n" 1454 " q();", 1455 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1456 " zzz) q();", 1457 getLLVMStyleWithColumns(40))); 1458 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1459 " yyy || // aaaaaa bbbbbbbb cccc\n" 1460 " zzz)\n" 1461 " q();", 1462 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1463 " zzz) q();", 1464 getLLVMStyleWithColumns(40))); 1465 EXPECT_EQ("fffffffff(\n" 1466 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1467 " zzz);", 1468 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1469 " zzz);", 1470 getLLVMStyleWithColumns(40))); 1471 } 1472 1473 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1474 EXPECT_EQ("#define A(x) /* \\\n" 1475 " a comment \\\n" 1476 " inside */ \\\n" 1477 " f();", 1478 format("#define A(x) /* \\\n" 1479 " a comment \\\n" 1480 " inside */ \\\n" 1481 " f();", 1482 getLLVMStyleWithColumns(17))); 1483 EXPECT_EQ("#define A( \\\n" 1484 " x) /* \\\n" 1485 " a comment \\\n" 1486 " inside */ \\\n" 1487 " f();", 1488 format("#define A( \\\n" 1489 " x) /* \\\n" 1490 " a comment \\\n" 1491 " inside */ \\\n" 1492 " f();", 1493 getLLVMStyleWithColumns(17))); 1494 } 1495 1496 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1497 EXPECT_EQ("namespace {}\n// Test\n#define A", 1498 format("namespace {}\n // Test\n#define A")); 1499 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1500 format("namespace {}\n /* Test */\n#define A")); 1501 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1502 format("namespace {}\n /* Test */ #define A")); 1503 } 1504 1505 TEST_F(FormatTest, SplitsLongLinesInComments) { 1506 EXPECT_EQ("/* This is a long\n" 1507 " * comment that\n" 1508 " * doesn't\n" 1509 " * fit on one line.\n" 1510 " */", 1511 format("/* " 1512 "This is a long " 1513 "comment that " 1514 "doesn't " 1515 "fit on one line. */", 1516 getLLVMStyleWithColumns(20))); 1517 EXPECT_EQ( 1518 "/* a b c d\n" 1519 " * e f g\n" 1520 " * h i j k\n" 1521 " */", 1522 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1523 EXPECT_EQ( 1524 "/* a b c d\n" 1525 " * e f g\n" 1526 " * h i j k\n" 1527 " */", 1528 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1529 EXPECT_EQ("/*\n" 1530 "This is a long\n" 1531 "comment that doesn't\n" 1532 "fit on one line.\n" 1533 "*/", 1534 format("/*\n" 1535 "This is a long " 1536 "comment that doesn't " 1537 "fit on one line. \n" 1538 "*/", 1539 getLLVMStyleWithColumns(20))); 1540 EXPECT_EQ("/*\n" 1541 " * This is a long\n" 1542 " * comment that\n" 1543 " * doesn't fit on\n" 1544 " * one line.\n" 1545 " */", 1546 format("/* \n" 1547 " * This is a long " 1548 " comment that " 1549 " doesn't fit on " 1550 " one line. \n" 1551 " */", 1552 getLLVMStyleWithColumns(20))); 1553 EXPECT_EQ("/*\n" 1554 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1555 " * so_it_should_be_broken\n" 1556 " * wherever_a_space_occurs\n" 1557 " */", 1558 format("/*\n" 1559 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1560 " so_it_should_be_broken " 1561 " wherever_a_space_occurs \n" 1562 " */", 1563 getLLVMStyleWithColumns(20))); 1564 EXPECT_EQ("/*\n" 1565 " * This_comment_can_not_be_broken_into_lines\n" 1566 " */", 1567 format("/*\n" 1568 " * This_comment_can_not_be_broken_into_lines\n" 1569 " */", 1570 getLLVMStyleWithColumns(20))); 1571 EXPECT_EQ("{\n" 1572 " /*\n" 1573 " This is another\n" 1574 " long comment that\n" 1575 " doesn't fit on one\n" 1576 " line 1234567890\n" 1577 " */\n" 1578 "}", 1579 format("{\n" 1580 "/*\n" 1581 "This is another " 1582 " long comment that " 1583 " doesn't fit on one" 1584 " line 1234567890\n" 1585 "*/\n" 1586 "}", 1587 getLLVMStyleWithColumns(20))); 1588 EXPECT_EQ("{\n" 1589 " /*\n" 1590 " * This i s\n" 1591 " * another comment\n" 1592 " * t hat doesn' t\n" 1593 " * fit on one l i\n" 1594 " * n e\n" 1595 " */\n" 1596 "}", 1597 format("{\n" 1598 "/*\n" 1599 " * This i s" 1600 " another comment" 1601 " t hat doesn' t" 1602 " fit on one l i" 1603 " n e\n" 1604 " */\n" 1605 "}", 1606 getLLVMStyleWithColumns(20))); 1607 EXPECT_EQ("/*\n" 1608 " * This is a long\n" 1609 " * comment that\n" 1610 " * doesn't fit on\n" 1611 " * one line\n" 1612 " */", 1613 format(" /*\n" 1614 " * This is a long comment that doesn't fit on one line\n" 1615 " */", 1616 getLLVMStyleWithColumns(20))); 1617 EXPECT_EQ("{\n" 1618 " if (something) /* This is a\n" 1619 " long\n" 1620 " comment */\n" 1621 " ;\n" 1622 "}", 1623 format("{\n" 1624 " if (something) /* This is a long comment */\n" 1625 " ;\n" 1626 "}", 1627 getLLVMStyleWithColumns(30))); 1628 1629 EXPECT_EQ("/* A comment before\n" 1630 " * a macro\n" 1631 " * definition */\n" 1632 "#define a b", 1633 format("/* A comment before a macro definition */\n" 1634 "#define a b", 1635 getLLVMStyleWithColumns(20))); 1636 1637 EXPECT_EQ("/* some comment\n" 1638 " * a comment\n" 1639 "* that we break\n" 1640 " * another comment\n" 1641 "* we have to break\n" 1642 "* a left comment\n" 1643 " */", 1644 format(" /* some comment\n" 1645 " * a comment that we break\n" 1646 " * another comment we have to break\n" 1647 "* a left comment\n" 1648 " */", 1649 getLLVMStyleWithColumns(20))); 1650 1651 EXPECT_EQ("/**\n" 1652 " * multiline block\n" 1653 " * comment\n" 1654 " *\n" 1655 " */", 1656 format("/**\n" 1657 " * multiline block comment\n" 1658 " *\n" 1659 " */", 1660 getLLVMStyleWithColumns(20))); 1661 1662 EXPECT_EQ("/*\n" 1663 "\n" 1664 "\n" 1665 " */\n", 1666 format(" /* \n" 1667 " \n" 1668 " \n" 1669 " */\n")); 1670 1671 EXPECT_EQ("/* a a */", 1672 format("/* a a */", getLLVMStyleWithColumns(15))); 1673 EXPECT_EQ("/* a a bc */", 1674 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1675 EXPECT_EQ("/* aaa aaa\n" 1676 " * aaaaa */", 1677 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1678 EXPECT_EQ("/* aaa aaa\n" 1679 " * aaaaa */", 1680 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1681 } 1682 1683 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1684 EXPECT_EQ("#define X \\\n" 1685 " /* \\\n" 1686 " Test \\\n" 1687 " Macro comment \\\n" 1688 " with a long \\\n" 1689 " line \\\n" 1690 " */ \\\n" 1691 " A + B", 1692 format("#define X \\\n" 1693 " /*\n" 1694 " Test\n" 1695 " Macro comment with a long line\n" 1696 " */ \\\n" 1697 " A + B", 1698 getLLVMStyleWithColumns(20))); 1699 EXPECT_EQ("#define X \\\n" 1700 " /* Macro comment \\\n" 1701 " with a long \\\n" 1702 " line */ \\\n" 1703 " A + B", 1704 format("#define X \\\n" 1705 " /* Macro comment with a long\n" 1706 " line */ \\\n" 1707 " A + B", 1708 getLLVMStyleWithColumns(20))); 1709 EXPECT_EQ("#define X \\\n" 1710 " /* Macro comment \\\n" 1711 " * with a long \\\n" 1712 " * line */ \\\n" 1713 " A + B", 1714 format("#define X \\\n" 1715 " /* Macro comment with a long line */ \\\n" 1716 " A + B", 1717 getLLVMStyleWithColumns(20))); 1718 } 1719 1720 TEST_F(FormatTest, CommentsInStaticInitializers) { 1721 EXPECT_EQ( 1722 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1723 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1724 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1725 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1726 " aaaaaaaaaaaaaaaaaaaa};", 1727 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1728 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1729 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1730 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1731 " aaaaaaaaaaaaaaaaaaaa };")); 1732 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1733 " bbbbbbbbbbb, ccccccccccc};"); 1734 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1735 " // comment for bb....\n" 1736 " bbbbbbbbbbb, ccccccccccc};"); 1737 verifyGoogleFormat( 1738 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1739 " bbbbbbbbbbb, ccccccccccc};"); 1740 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1741 " // comment for bb....\n" 1742 " bbbbbbbbbbb, ccccccccccc};"); 1743 1744 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1745 " {d, e, f}, // Group #2\n" 1746 " {g, h, i}}; // Group #3"); 1747 verifyFormat("S s = {{// Group #1\n" 1748 " a, b, c},\n" 1749 " {// Group #2\n" 1750 " d, e, f},\n" 1751 " {// Group #3\n" 1752 " g, h, i}};"); 1753 1754 EXPECT_EQ("S s = {\n" 1755 " // Some comment\n" 1756 " a,\n" 1757 "\n" 1758 " // Comment after empty line\n" 1759 " b}", 1760 format("S s = {\n" 1761 " // Some comment\n" 1762 " a,\n" 1763 " \n" 1764 " // Comment after empty line\n" 1765 " b\n" 1766 "}")); 1767 EXPECT_EQ("S s = {\n" 1768 " /* Some comment */\n" 1769 " a,\n" 1770 "\n" 1771 " /* Comment after empty line */\n" 1772 " b}", 1773 format("S s = {\n" 1774 " /* Some comment */\n" 1775 " a,\n" 1776 " \n" 1777 " /* Comment after empty line */\n" 1778 " b\n" 1779 "}")); 1780 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1781 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1782 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1783 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1784 } 1785 1786 TEST_F(FormatTest, IgnoresIf0Contents) { 1787 EXPECT_EQ("#if 0\n" 1788 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1789 "#endif\n" 1790 "void f() {}", 1791 format("#if 0\n" 1792 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1793 "#endif\n" 1794 "void f( ) { }")); 1795 EXPECT_EQ("#if false\n" 1796 "void f( ) { }\n" 1797 "#endif\n" 1798 "void g() {}\n", 1799 format("#if false\n" 1800 "void f( ) { }\n" 1801 "#endif\n" 1802 "void g( ) { }\n")); 1803 EXPECT_EQ("enum E {\n" 1804 " One,\n" 1805 " Two,\n" 1806 "#if 0\n" 1807 "Three,\n" 1808 " Four,\n" 1809 "#endif\n" 1810 " Five\n" 1811 "};", 1812 format("enum E {\n" 1813 " One,Two,\n" 1814 "#if 0\n" 1815 "Three,\n" 1816 " Four,\n" 1817 "#endif\n" 1818 " Five};")); 1819 EXPECT_EQ("enum F {\n" 1820 " One,\n" 1821 "#if 1\n" 1822 " Two,\n" 1823 "#if 0\n" 1824 "Three,\n" 1825 " Four,\n" 1826 "#endif\n" 1827 " Five\n" 1828 "#endif\n" 1829 "};", 1830 format("enum F {\n" 1831 "One,\n" 1832 "#if 1\n" 1833 "Two,\n" 1834 "#if 0\n" 1835 "Three,\n" 1836 " Four,\n" 1837 "#endif\n" 1838 "Five\n" 1839 "#endif\n" 1840 "};")); 1841 EXPECT_EQ("enum G {\n" 1842 " One,\n" 1843 "#if 0\n" 1844 "Two,\n" 1845 "#else\n" 1846 " Three,\n" 1847 "#endif\n" 1848 " Four\n" 1849 "};", 1850 format("enum G {\n" 1851 "One,\n" 1852 "#if 0\n" 1853 "Two,\n" 1854 "#else\n" 1855 "Three,\n" 1856 "#endif\n" 1857 "Four\n" 1858 "};")); 1859 EXPECT_EQ("enum H {\n" 1860 " One,\n" 1861 "#if 0\n" 1862 "#ifdef Q\n" 1863 "Two,\n" 1864 "#else\n" 1865 "Three,\n" 1866 "#endif\n" 1867 "#endif\n" 1868 " Four\n" 1869 "};", 1870 format("enum H {\n" 1871 "One,\n" 1872 "#if 0\n" 1873 "#ifdef Q\n" 1874 "Two,\n" 1875 "#else\n" 1876 "Three,\n" 1877 "#endif\n" 1878 "#endif\n" 1879 "Four\n" 1880 "};")); 1881 EXPECT_EQ("enum I {\n" 1882 " One,\n" 1883 "#if /* test */ 0 || 1\n" 1884 "Two,\n" 1885 "Three,\n" 1886 "#endif\n" 1887 " Four\n" 1888 "};", 1889 format("enum I {\n" 1890 "One,\n" 1891 "#if /* test */ 0 || 1\n" 1892 "Two,\n" 1893 "Three,\n" 1894 "#endif\n" 1895 "Four\n" 1896 "};")); 1897 EXPECT_EQ("enum J {\n" 1898 " One,\n" 1899 "#if 0\n" 1900 "#if 0\n" 1901 "Two,\n" 1902 "#else\n" 1903 "Three,\n" 1904 "#endif\n" 1905 "Four,\n" 1906 "#endif\n" 1907 " Five\n" 1908 "};", 1909 format("enum J {\n" 1910 "One,\n" 1911 "#if 0\n" 1912 "#if 0\n" 1913 "Two,\n" 1914 "#else\n" 1915 "Three,\n" 1916 "#endif\n" 1917 "Four,\n" 1918 "#endif\n" 1919 "Five\n" 1920 "};")); 1921 } 1922 1923 //===----------------------------------------------------------------------===// 1924 // Tests for classes, namespaces, etc. 1925 //===----------------------------------------------------------------------===// 1926 1927 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1928 verifyFormat("class A {};"); 1929 } 1930 1931 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1932 verifyFormat("class A {\n" 1933 "public:\n" 1934 "public: // comment\n" 1935 "protected:\n" 1936 "private:\n" 1937 " void f() {}\n" 1938 "};"); 1939 verifyGoogleFormat("class A {\n" 1940 " public:\n" 1941 " protected:\n" 1942 " private:\n" 1943 " void f() {}\n" 1944 "};"); 1945 verifyFormat("class A {\n" 1946 "public slots:\n" 1947 " void f1() {}\n" 1948 "public Q_SLOTS:\n" 1949 " void f2() {}\n" 1950 "protected slots:\n" 1951 " void f3() {}\n" 1952 "protected Q_SLOTS:\n" 1953 " void f4() {}\n" 1954 "private slots:\n" 1955 " void f5() {}\n" 1956 "private Q_SLOTS:\n" 1957 " void f6() {}\n" 1958 "signals:\n" 1959 " void g1();\n" 1960 "Q_SIGNALS:\n" 1961 " void g2();\n" 1962 "};"); 1963 1964 // Don't interpret 'signals' the wrong way. 1965 verifyFormat("signals.set();"); 1966 verifyFormat("for (Signals signals : f()) {\n}"); 1967 verifyFormat("{\n" 1968 " signals.set(); // This needs indentation.\n" 1969 "}"); 1970 verifyFormat("void f() {\n" 1971 "label:\n" 1972 " signals.baz();\n" 1973 "}"); 1974 } 1975 1976 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1977 EXPECT_EQ("class A {\n" 1978 "public:\n" 1979 " void f();\n" 1980 "\n" 1981 "private:\n" 1982 " void g() {}\n" 1983 " // test\n" 1984 "protected:\n" 1985 " int h;\n" 1986 "};", 1987 format("class A {\n" 1988 "public:\n" 1989 "void f();\n" 1990 "private:\n" 1991 "void g() {}\n" 1992 "// test\n" 1993 "protected:\n" 1994 "int h;\n" 1995 "};")); 1996 EXPECT_EQ("class A {\n" 1997 "protected:\n" 1998 "public:\n" 1999 " void f();\n" 2000 "};", 2001 format("class A {\n" 2002 "protected:\n" 2003 "\n" 2004 "public:\n" 2005 "\n" 2006 " void f();\n" 2007 "};")); 2008 2009 // Even ensure proper spacing inside macros. 2010 EXPECT_EQ("#define B \\\n" 2011 " class A { \\\n" 2012 " protected: \\\n" 2013 " public: \\\n" 2014 " void f(); \\\n" 2015 " };", 2016 format("#define B \\\n" 2017 " class A { \\\n" 2018 " protected: \\\n" 2019 " \\\n" 2020 " public: \\\n" 2021 " \\\n" 2022 " void f(); \\\n" 2023 " };", 2024 getGoogleStyle())); 2025 // But don't remove empty lines after macros ending in access specifiers. 2026 EXPECT_EQ("#define A private:\n" 2027 "\n" 2028 "int i;", 2029 format("#define A private:\n" 2030 "\n" 2031 "int i;")); 2032 } 2033 2034 TEST_F(FormatTest, FormatsClasses) { 2035 verifyFormat("class A : public B {};"); 2036 verifyFormat("class A : public ::B {};"); 2037 2038 verifyFormat( 2039 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2040 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2041 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2042 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2043 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2044 verifyFormat( 2045 "class A : public B, public C, public D, public E, public F {};"); 2046 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2047 " public C,\n" 2048 " public D,\n" 2049 " public E,\n" 2050 " public F,\n" 2051 " public G {};"); 2052 2053 verifyFormat("class\n" 2054 " ReallyReallyLongClassName {\n" 2055 " int i;\n" 2056 "};", 2057 getLLVMStyleWithColumns(32)); 2058 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2059 " aaaaaaaaaaaaaaaa> {};"); 2060 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2061 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2062 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2063 verifyFormat("template <class R, class C>\n" 2064 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2065 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2066 verifyFormat("class ::A::B {};"); 2067 } 2068 2069 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2070 verifyFormat("class A {\n} a, b;"); 2071 verifyFormat("struct A {\n} a, b;"); 2072 verifyFormat("union A {\n} a;"); 2073 } 2074 2075 TEST_F(FormatTest, FormatsEnum) { 2076 verifyFormat("enum {\n" 2077 " Zero,\n" 2078 " One = 1,\n" 2079 " Two = One + 1,\n" 2080 " Three = (One + Two),\n" 2081 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2082 " Five = (One, Two, Three, Four, 5)\n" 2083 "};"); 2084 verifyGoogleFormat("enum {\n" 2085 " Zero,\n" 2086 " One = 1,\n" 2087 " Two = One + 1,\n" 2088 " Three = (One + Two),\n" 2089 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2090 " Five = (One, Two, Three, Four, 5)\n" 2091 "};"); 2092 verifyFormat("enum Enum {};"); 2093 verifyFormat("enum {};"); 2094 verifyFormat("enum X E {} d;"); 2095 verifyFormat("enum __attribute__((...)) E {} d;"); 2096 verifyFormat("enum __declspec__((...)) E {} d;"); 2097 verifyFormat("enum {\n" 2098 " Bar = Foo<int, int>::value\n" 2099 "};", 2100 getLLVMStyleWithColumns(30)); 2101 2102 verifyFormat("enum ShortEnum { A, B, C };"); 2103 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2104 2105 EXPECT_EQ("enum KeepEmptyLines {\n" 2106 " ONE,\n" 2107 "\n" 2108 " TWO,\n" 2109 "\n" 2110 " THREE\n" 2111 "}", 2112 format("enum KeepEmptyLines {\n" 2113 " ONE,\n" 2114 "\n" 2115 " TWO,\n" 2116 "\n" 2117 "\n" 2118 " THREE\n" 2119 "}")); 2120 verifyFormat("enum E { // comment\n" 2121 " ONE,\n" 2122 " TWO\n" 2123 "};\n" 2124 "int i;"); 2125 // Not enums. 2126 verifyFormat("enum X f() {\n" 2127 " a();\n" 2128 " return 42;\n" 2129 "}"); 2130 verifyFormat("enum X Type::f() {\n" 2131 " a();\n" 2132 " return 42;\n" 2133 "}"); 2134 verifyFormat("enum ::X f() {\n" 2135 " a();\n" 2136 " return 42;\n" 2137 "}"); 2138 verifyFormat("enum ns::X f() {\n" 2139 " a();\n" 2140 " return 42;\n" 2141 "}"); 2142 } 2143 2144 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2145 verifyFormat("enum Type {\n" 2146 " One = 0; // These semicolons should be commas.\n" 2147 " Two = 1;\n" 2148 "};"); 2149 verifyFormat("namespace n {\n" 2150 "enum Type {\n" 2151 " One,\n" 2152 " Two, // missing };\n" 2153 " int i;\n" 2154 "}\n" 2155 "void g() {}"); 2156 } 2157 2158 TEST_F(FormatTest, FormatsEnumStruct) { 2159 verifyFormat("enum struct {\n" 2160 " Zero,\n" 2161 " One = 1,\n" 2162 " Two = One + 1,\n" 2163 " Three = (One + Two),\n" 2164 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2165 " Five = (One, Two, Three, Four, 5)\n" 2166 "};"); 2167 verifyFormat("enum struct Enum {};"); 2168 verifyFormat("enum struct {};"); 2169 verifyFormat("enum struct X E {} d;"); 2170 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2171 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2172 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2173 } 2174 2175 TEST_F(FormatTest, FormatsEnumClass) { 2176 verifyFormat("enum class {\n" 2177 " Zero,\n" 2178 " One = 1,\n" 2179 " Two = One + 1,\n" 2180 " Three = (One + Two),\n" 2181 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2182 " Five = (One, Two, Three, Four, 5)\n" 2183 "};"); 2184 verifyFormat("enum class Enum {};"); 2185 verifyFormat("enum class {};"); 2186 verifyFormat("enum class X E {} d;"); 2187 verifyFormat("enum class __attribute__((...)) E {} d;"); 2188 verifyFormat("enum class __declspec__((...)) E {} d;"); 2189 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2190 } 2191 2192 TEST_F(FormatTest, FormatsEnumTypes) { 2193 verifyFormat("enum X : int {\n" 2194 " A, // Force multiple lines.\n" 2195 " B\n" 2196 "};"); 2197 verifyFormat("enum X : int { A, B };"); 2198 verifyFormat("enum X : std::uint32_t { A, B };"); 2199 } 2200 2201 TEST_F(FormatTest, FormatsNSEnums) { 2202 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2203 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2204 " // Information about someDecentlyLongValue.\n" 2205 " someDecentlyLongValue,\n" 2206 " // Information about anotherDecentlyLongValue.\n" 2207 " anotherDecentlyLongValue,\n" 2208 " // Information about aThirdDecentlyLongValue.\n" 2209 " aThirdDecentlyLongValue\n" 2210 "};"); 2211 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2212 " a = 1,\n" 2213 " b = 2,\n" 2214 " c = 3,\n" 2215 "};"); 2216 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2217 " a = 1,\n" 2218 " b = 2,\n" 2219 " c = 3,\n" 2220 "};"); 2221 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2222 " a = 1,\n" 2223 " b = 2,\n" 2224 " c = 3,\n" 2225 "};"); 2226 } 2227 2228 TEST_F(FormatTest, FormatsBitfields) { 2229 verifyFormat("struct Bitfields {\n" 2230 " unsigned sClass : 8;\n" 2231 " unsigned ValueKind : 2;\n" 2232 "};"); 2233 verifyFormat("struct A {\n" 2234 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2235 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2236 "};"); 2237 verifyFormat("struct MyStruct {\n" 2238 " uchar data;\n" 2239 " uchar : 8;\n" 2240 " uchar : 8;\n" 2241 " uchar other;\n" 2242 "};"); 2243 } 2244 2245 TEST_F(FormatTest, FormatsNamespaces) { 2246 verifyFormat("namespace some_namespace {\n" 2247 "class A {};\n" 2248 "void f() { f(); }\n" 2249 "}"); 2250 verifyFormat("namespace {\n" 2251 "class A {};\n" 2252 "void f() { f(); }\n" 2253 "}"); 2254 verifyFormat("inline namespace X {\n" 2255 "class A {};\n" 2256 "void f() { f(); }\n" 2257 "}"); 2258 verifyFormat("using namespace some_namespace;\n" 2259 "class A {};\n" 2260 "void f() { f(); }"); 2261 2262 // This code is more common than we thought; if we 2263 // layout this correctly the semicolon will go into 2264 // its own line, which is undesirable. 2265 verifyFormat("namespace {};"); 2266 verifyFormat("namespace {\n" 2267 "class A {};\n" 2268 "};"); 2269 2270 verifyFormat("namespace {\n" 2271 "int SomeVariable = 0; // comment\n" 2272 "} // namespace"); 2273 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2274 "#define HEADER_GUARD\n" 2275 "namespace my_namespace {\n" 2276 "int i;\n" 2277 "} // my_namespace\n" 2278 "#endif // HEADER_GUARD", 2279 format("#ifndef HEADER_GUARD\n" 2280 " #define HEADER_GUARD\n" 2281 " namespace my_namespace {\n" 2282 "int i;\n" 2283 "} // my_namespace\n" 2284 "#endif // HEADER_GUARD")); 2285 2286 EXPECT_EQ("namespace A::B {\n" 2287 "class C {};\n" 2288 "}", 2289 format("namespace A::B {\n" 2290 "class C {};\n" 2291 "}")); 2292 2293 FormatStyle Style = getLLVMStyle(); 2294 Style.NamespaceIndentation = FormatStyle::NI_All; 2295 EXPECT_EQ("namespace out {\n" 2296 " int i;\n" 2297 " namespace in {\n" 2298 " int i;\n" 2299 " } // namespace\n" 2300 "} // namespace", 2301 format("namespace out {\n" 2302 "int i;\n" 2303 "namespace in {\n" 2304 "int i;\n" 2305 "} // namespace\n" 2306 "} // namespace", 2307 Style)); 2308 2309 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2310 EXPECT_EQ("namespace out {\n" 2311 "int i;\n" 2312 "namespace in {\n" 2313 " int i;\n" 2314 "} // namespace\n" 2315 "} // namespace", 2316 format("namespace out {\n" 2317 "int i;\n" 2318 "namespace in {\n" 2319 "int i;\n" 2320 "} // namespace\n" 2321 "} // namespace", 2322 Style)); 2323 } 2324 2325 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2326 2327 TEST_F(FormatTest, FormatsInlineASM) { 2328 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2329 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2330 verifyFormat( 2331 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2332 " \"cpuid\\n\\t\"\n" 2333 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2334 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2335 " : \"a\"(value));"); 2336 EXPECT_EQ( 2337 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2338 " __asm {\n" 2339 " mov edx,[that] // vtable in edx\n" 2340 " mov eax,methodIndex\n" 2341 " call [edx][eax*4] // stdcall\n" 2342 " }\n" 2343 "}", 2344 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2345 " __asm {\n" 2346 " mov edx,[that] // vtable in edx\n" 2347 " mov eax,methodIndex\n" 2348 " call [edx][eax*4] // stdcall\n" 2349 " }\n" 2350 "}")); 2351 EXPECT_EQ("_asm {\n" 2352 " xor eax, eax;\n" 2353 " cpuid;\n" 2354 "}", 2355 format("_asm {\n" 2356 " xor eax, eax;\n" 2357 " cpuid;\n" 2358 "}")); 2359 verifyFormat("void function() {\n" 2360 " // comment\n" 2361 " asm(\"\");\n" 2362 "}"); 2363 EXPECT_EQ("__asm {\n" 2364 "}\n" 2365 "int i;", 2366 format("__asm {\n" 2367 "}\n" 2368 "int i;")); 2369 } 2370 2371 TEST_F(FormatTest, FormatTryCatch) { 2372 verifyFormat("try {\n" 2373 " throw a * b;\n" 2374 "} catch (int a) {\n" 2375 " // Do nothing.\n" 2376 "} catch (...) {\n" 2377 " exit(42);\n" 2378 "}"); 2379 2380 // Function-level try statements. 2381 verifyFormat("int f() try { return 4; } catch (...) {\n" 2382 " return 5;\n" 2383 "}"); 2384 verifyFormat("class A {\n" 2385 " int a;\n" 2386 " A() try : a(0) {\n" 2387 " } catch (...) {\n" 2388 " throw;\n" 2389 " }\n" 2390 "};\n"); 2391 2392 // Incomplete try-catch blocks. 2393 verifyIncompleteFormat("try {} catch ("); 2394 } 2395 2396 TEST_F(FormatTest, FormatSEHTryCatch) { 2397 verifyFormat("__try {\n" 2398 " int a = b * c;\n" 2399 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2400 " // Do nothing.\n" 2401 "}"); 2402 2403 verifyFormat("__try {\n" 2404 " int a = b * c;\n" 2405 "} __finally {\n" 2406 " // Do nothing.\n" 2407 "}"); 2408 2409 verifyFormat("DEBUG({\n" 2410 " __try {\n" 2411 " } __finally {\n" 2412 " }\n" 2413 "});\n"); 2414 } 2415 2416 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2417 verifyFormat("try {\n" 2418 " f();\n" 2419 "} catch {\n" 2420 " g();\n" 2421 "}"); 2422 verifyFormat("try {\n" 2423 " f();\n" 2424 "} catch (A a) MACRO(x) {\n" 2425 " g();\n" 2426 "} catch (B b) MACRO(x) {\n" 2427 " g();\n" 2428 "}"); 2429 } 2430 2431 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2432 FormatStyle Style = getLLVMStyle(); 2433 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2434 FormatStyle::BS_WebKit}) { 2435 Style.BreakBeforeBraces = BraceStyle; 2436 verifyFormat("try {\n" 2437 " // something\n" 2438 "} catch (...) {\n" 2439 " // something\n" 2440 "}", 2441 Style); 2442 } 2443 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2444 verifyFormat("try {\n" 2445 " // something\n" 2446 "}\n" 2447 "catch (...) {\n" 2448 " // something\n" 2449 "}", 2450 Style); 2451 verifyFormat("__try {\n" 2452 " // something\n" 2453 "}\n" 2454 "__finally {\n" 2455 " // something\n" 2456 "}", 2457 Style); 2458 verifyFormat("@try {\n" 2459 " // something\n" 2460 "}\n" 2461 "@finally {\n" 2462 " // something\n" 2463 "}", 2464 Style); 2465 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2466 verifyFormat("try\n" 2467 "{\n" 2468 " // something\n" 2469 "}\n" 2470 "catch (...)\n" 2471 "{\n" 2472 " // something\n" 2473 "}", 2474 Style); 2475 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2476 verifyFormat("try\n" 2477 " {\n" 2478 " // something\n" 2479 " }\n" 2480 "catch (...)\n" 2481 " {\n" 2482 " // something\n" 2483 " }", 2484 Style); 2485 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2486 Style.BraceWrapping.BeforeCatch = true; 2487 verifyFormat("try {\n" 2488 " // something\n" 2489 "}\n" 2490 "catch (...) {\n" 2491 " // something\n" 2492 "}", 2493 Style); 2494 } 2495 2496 TEST_F(FormatTest, FormatObjCTryCatch) { 2497 verifyFormat("@try {\n" 2498 " f();\n" 2499 "} @catch (NSException e) {\n" 2500 " @throw;\n" 2501 "} @finally {\n" 2502 " exit(42);\n" 2503 "}"); 2504 verifyFormat("DEBUG({\n" 2505 " @try {\n" 2506 " } @finally {\n" 2507 " }\n" 2508 "});\n"); 2509 } 2510 2511 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2512 FormatStyle Style = getLLVMStyle(); 2513 verifyFormat("@autoreleasepool {\n" 2514 " f();\n" 2515 "}\n" 2516 "@autoreleasepool {\n" 2517 " f();\n" 2518 "}\n", 2519 Style); 2520 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2521 verifyFormat("@autoreleasepool\n" 2522 "{\n" 2523 " f();\n" 2524 "}\n" 2525 "@autoreleasepool\n" 2526 "{\n" 2527 " f();\n" 2528 "}\n", 2529 Style); 2530 } 2531 2532 TEST_F(FormatTest, StaticInitializers) { 2533 verifyFormat("static SomeClass SC = {1, 'a'};"); 2534 2535 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2536 " 100000000, " 2537 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2538 2539 // Here, everything other than the "}" would fit on a line. 2540 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2541 " 10000000000000000000000000};"); 2542 EXPECT_EQ("S s = {a,\n" 2543 "\n" 2544 " b};", 2545 format("S s = {\n" 2546 " a,\n" 2547 "\n" 2548 " b\n" 2549 "};")); 2550 2551 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2552 // line. However, the formatting looks a bit off and this probably doesn't 2553 // happen often in practice. 2554 verifyFormat("static int Variable[1] = {\n" 2555 " {1000000000000000000000000000000000000}};", 2556 getLLVMStyleWithColumns(40)); 2557 } 2558 2559 TEST_F(FormatTest, DesignatedInitializers) { 2560 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2561 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2562 " .bbbbbbbbbb = 2,\n" 2563 " .cccccccccc = 3,\n" 2564 " .dddddddddd = 4,\n" 2565 " .eeeeeeeeee = 5};"); 2566 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2567 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2568 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2569 " .ccccccccccccccccccccccccccc = 3,\n" 2570 " .ddddddddddddddddddddddddddd = 4,\n" 2571 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2572 2573 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2574 } 2575 2576 TEST_F(FormatTest, NestedStaticInitializers) { 2577 verifyFormat("static A x = {{{}}};\n"); 2578 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2579 " {init1, init2, init3, init4}}};", 2580 getLLVMStyleWithColumns(50)); 2581 2582 verifyFormat("somes Status::global_reps[3] = {\n" 2583 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2584 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2585 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2586 getLLVMStyleWithColumns(60)); 2587 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2588 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2589 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2590 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2591 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2592 " {rect.fRight - rect.fLeft, rect.fBottom - " 2593 "rect.fTop}};"); 2594 2595 verifyFormat( 2596 "SomeArrayOfSomeType a = {\n" 2597 " {{1, 2, 3},\n" 2598 " {1, 2, 3},\n" 2599 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2600 " 333333333333333333333333333333},\n" 2601 " {1, 2, 3},\n" 2602 " {1, 2, 3}}};"); 2603 verifyFormat( 2604 "SomeArrayOfSomeType a = {\n" 2605 " {{1, 2, 3}},\n" 2606 " {{1, 2, 3}},\n" 2607 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2608 " 333333333333333333333333333333}},\n" 2609 " {{1, 2, 3}},\n" 2610 " {{1, 2, 3}}};"); 2611 2612 verifyFormat("struct {\n" 2613 " unsigned bit;\n" 2614 " const char *const name;\n" 2615 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2616 " {kOsWin, \"Windows\"},\n" 2617 " {kOsLinux, \"Linux\"},\n" 2618 " {kOsCrOS, \"Chrome OS\"}};"); 2619 verifyFormat("struct {\n" 2620 " unsigned bit;\n" 2621 " const char *const name;\n" 2622 "} kBitsToOs[] = {\n" 2623 " {kOsMac, \"Mac\"},\n" 2624 " {kOsWin, \"Windows\"},\n" 2625 " {kOsLinux, \"Linux\"},\n" 2626 " {kOsCrOS, \"Chrome OS\"},\n" 2627 "};"); 2628 } 2629 2630 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2631 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2632 " \\\n" 2633 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2634 } 2635 2636 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2637 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2638 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2639 2640 // Do break defaulted and deleted functions. 2641 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2642 " default;", 2643 getLLVMStyleWithColumns(40)); 2644 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2645 " delete;", 2646 getLLVMStyleWithColumns(40)); 2647 } 2648 2649 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2650 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2651 getLLVMStyleWithColumns(40)); 2652 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2653 getLLVMStyleWithColumns(40)); 2654 EXPECT_EQ("#define Q \\\n" 2655 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2656 " \"aaaaaaaa.cpp\"", 2657 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2658 getLLVMStyleWithColumns(40))); 2659 } 2660 2661 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2662 EXPECT_EQ("# 123 \"A string literal\"", 2663 format(" # 123 \"A string literal\"")); 2664 } 2665 2666 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2667 EXPECT_EQ("#;", format("#;")); 2668 verifyFormat("#\n;\n;\n;"); 2669 } 2670 2671 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2672 EXPECT_EQ("#line 42 \"test\"\n", 2673 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2674 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2675 getLLVMStyleWithColumns(12))); 2676 } 2677 2678 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2679 EXPECT_EQ("#line 42 \"test\"", 2680 format("# \\\n line \\\n 42 \\\n \"test\"")); 2681 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2682 } 2683 2684 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2685 verifyFormat("#define A \\x20"); 2686 verifyFormat("#define A \\ x20"); 2687 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2688 verifyFormat("#define A ''"); 2689 verifyFormat("#define A ''qqq"); 2690 verifyFormat("#define A `qqq"); 2691 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2692 EXPECT_EQ("const char *c = STRINGIFY(\n" 2693 "\\na : b);", 2694 format("const char * c = STRINGIFY(\n" 2695 "\\na : b);")); 2696 2697 verifyFormat("a\r\\"); 2698 verifyFormat("a\v\\"); 2699 verifyFormat("a\f\\"); 2700 } 2701 2702 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2703 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2704 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2705 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2706 // FIXME: We never break before the macro name. 2707 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2708 2709 verifyFormat("#define A A\n#define A A"); 2710 verifyFormat("#define A(X) A\n#define A A"); 2711 2712 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2713 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2714 } 2715 2716 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2717 EXPECT_EQ("// somecomment\n" 2718 "#include \"a.h\"\n" 2719 "#define A( \\\n" 2720 " A, B)\n" 2721 "#include \"b.h\"\n" 2722 "// somecomment\n", 2723 format(" // somecomment\n" 2724 " #include \"a.h\"\n" 2725 "#define A(A,\\\n" 2726 " B)\n" 2727 " #include \"b.h\"\n" 2728 " // somecomment\n", 2729 getLLVMStyleWithColumns(13))); 2730 } 2731 2732 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2733 2734 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2735 EXPECT_EQ("#define A \\\n" 2736 " c; \\\n" 2737 " e;\n" 2738 "f;", 2739 format("#define A c; e;\n" 2740 "f;", 2741 getLLVMStyleWithColumns(14))); 2742 } 2743 2744 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2745 2746 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2747 EXPECT_EQ("int x,\n" 2748 "#define A\n" 2749 " y;", 2750 format("int x,\n#define A\ny;")); 2751 } 2752 2753 TEST_F(FormatTest, HashInMacroDefinition) { 2754 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2755 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2756 verifyFormat("#define A \\\n" 2757 " { \\\n" 2758 " f(#c); \\\n" 2759 " }", 2760 getLLVMStyleWithColumns(11)); 2761 2762 verifyFormat("#define A(X) \\\n" 2763 " void function##X()", 2764 getLLVMStyleWithColumns(22)); 2765 2766 verifyFormat("#define A(a, b, c) \\\n" 2767 " void a##b##c()", 2768 getLLVMStyleWithColumns(22)); 2769 2770 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2771 } 2772 2773 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2774 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2775 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2776 } 2777 2778 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2779 EXPECT_EQ("#define A b;", format("#define A \\\n" 2780 " \\\n" 2781 " b;", 2782 getLLVMStyleWithColumns(25))); 2783 EXPECT_EQ("#define A \\\n" 2784 " \\\n" 2785 " a; \\\n" 2786 " b;", 2787 format("#define A \\\n" 2788 " \\\n" 2789 " a; \\\n" 2790 " b;", 2791 getLLVMStyleWithColumns(11))); 2792 EXPECT_EQ("#define A \\\n" 2793 " a; \\\n" 2794 " \\\n" 2795 " b;", 2796 format("#define A \\\n" 2797 " a; \\\n" 2798 " \\\n" 2799 " b;", 2800 getLLVMStyleWithColumns(11))); 2801 } 2802 2803 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2804 verifyIncompleteFormat("#define A :"); 2805 verifyFormat("#define SOMECASES \\\n" 2806 " case 1: \\\n" 2807 " case 2\n", 2808 getLLVMStyleWithColumns(20)); 2809 verifyFormat("#define MACRO(a) \\\n" 2810 " if (a) \\\n" 2811 " f(); \\\n" 2812 " else \\\n" 2813 " g()", 2814 getLLVMStyleWithColumns(18)); 2815 verifyFormat("#define A template <typename T>"); 2816 verifyIncompleteFormat("#define STR(x) #x\n" 2817 "f(STR(this_is_a_string_literal{));"); 2818 verifyFormat("#pragma omp threadprivate( \\\n" 2819 " y)), // expected-warning", 2820 getLLVMStyleWithColumns(28)); 2821 verifyFormat("#d, = };"); 2822 verifyFormat("#if \"a"); 2823 verifyIncompleteFormat("({\n" 2824 "#define b \\\n" 2825 " } \\\n" 2826 " a\n" 2827 "a", 2828 getLLVMStyleWithColumns(15)); 2829 verifyFormat("#define A \\\n" 2830 " { \\\n" 2831 " {\n" 2832 "#define B \\\n" 2833 " } \\\n" 2834 " }", 2835 getLLVMStyleWithColumns(15)); 2836 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2837 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2838 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2839 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2840 } 2841 2842 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2843 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2844 EXPECT_EQ("class A : public QObject {\n" 2845 " Q_OBJECT\n" 2846 "\n" 2847 " A() {}\n" 2848 "};", 2849 format("class A : public QObject {\n" 2850 " Q_OBJECT\n" 2851 "\n" 2852 " A() {\n}\n" 2853 "} ;")); 2854 EXPECT_EQ("MACRO\n" 2855 "/*static*/ int i;", 2856 format("MACRO\n" 2857 " /*static*/ int i;")); 2858 EXPECT_EQ("SOME_MACRO\n" 2859 "namespace {\n" 2860 "void f();\n" 2861 "}", 2862 format("SOME_MACRO\n" 2863 " namespace {\n" 2864 "void f( );\n" 2865 "}")); 2866 // Only if the identifier contains at least 5 characters. 2867 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2868 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2869 // Only if everything is upper case. 2870 EXPECT_EQ("class A : public QObject {\n" 2871 " Q_Object A() {}\n" 2872 "};", 2873 format("class A : public QObject {\n" 2874 " Q_Object\n" 2875 " A() {\n}\n" 2876 "} ;")); 2877 2878 // Only if the next line can actually start an unwrapped line. 2879 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2880 format("SOME_WEIRD_LOG_MACRO\n" 2881 "<< SomeThing;")); 2882 2883 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2884 "(n, buffers))\n", 2885 getChromiumStyle(FormatStyle::LK_Cpp)); 2886 } 2887 2888 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2889 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2890 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2891 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2892 "class X {};\n" 2893 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2894 "int *createScopDetectionPass() { return 0; }", 2895 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2896 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2897 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2898 " class X {};\n" 2899 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2900 " int *createScopDetectionPass() { return 0; }")); 2901 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2902 // braces, so that inner block is indented one level more. 2903 EXPECT_EQ("int q() {\n" 2904 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2905 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2906 " IPC_END_MESSAGE_MAP()\n" 2907 "}", 2908 format("int q() {\n" 2909 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2910 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2911 " IPC_END_MESSAGE_MAP()\n" 2912 "}")); 2913 2914 // Same inside macros. 2915 EXPECT_EQ("#define LIST(L) \\\n" 2916 " L(A) \\\n" 2917 " L(B) \\\n" 2918 " L(C)", 2919 format("#define LIST(L) \\\n" 2920 " L(A) \\\n" 2921 " L(B) \\\n" 2922 " L(C)", 2923 getGoogleStyle())); 2924 2925 // These must not be recognized as macros. 2926 EXPECT_EQ("int q() {\n" 2927 " f(x);\n" 2928 " f(x) {}\n" 2929 " f(x)->g();\n" 2930 " f(x)->*g();\n" 2931 " f(x).g();\n" 2932 " f(x) = x;\n" 2933 " f(x) += x;\n" 2934 " f(x) -= x;\n" 2935 " f(x) *= x;\n" 2936 " f(x) /= x;\n" 2937 " f(x) %= x;\n" 2938 " f(x) &= x;\n" 2939 " f(x) |= x;\n" 2940 " f(x) ^= x;\n" 2941 " f(x) >>= x;\n" 2942 " f(x) <<= x;\n" 2943 " f(x)[y].z();\n" 2944 " LOG(INFO) << x;\n" 2945 " ifstream(x) >> x;\n" 2946 "}\n", 2947 format("int q() {\n" 2948 " f(x)\n;\n" 2949 " f(x)\n {}\n" 2950 " f(x)\n->g();\n" 2951 " f(x)\n->*g();\n" 2952 " f(x)\n.g();\n" 2953 " f(x)\n = x;\n" 2954 " f(x)\n += x;\n" 2955 " f(x)\n -= x;\n" 2956 " f(x)\n *= x;\n" 2957 " f(x)\n /= x;\n" 2958 " f(x)\n %= x;\n" 2959 " f(x)\n &= x;\n" 2960 " f(x)\n |= x;\n" 2961 " f(x)\n ^= x;\n" 2962 " f(x)\n >>= x;\n" 2963 " f(x)\n <<= x;\n" 2964 " f(x)\n[y].z();\n" 2965 " LOG(INFO)\n << x;\n" 2966 " ifstream(x)\n >> x;\n" 2967 "}\n")); 2968 EXPECT_EQ("int q() {\n" 2969 " F(x)\n" 2970 " if (1) {\n" 2971 " }\n" 2972 " F(x)\n" 2973 " while (1) {\n" 2974 " }\n" 2975 " F(x)\n" 2976 " G(x);\n" 2977 " F(x)\n" 2978 " try {\n" 2979 " Q();\n" 2980 " } catch (...) {\n" 2981 " }\n" 2982 "}\n", 2983 format("int q() {\n" 2984 "F(x)\n" 2985 "if (1) {}\n" 2986 "F(x)\n" 2987 "while (1) {}\n" 2988 "F(x)\n" 2989 "G(x);\n" 2990 "F(x)\n" 2991 "try { Q(); } catch (...) {}\n" 2992 "}\n")); 2993 EXPECT_EQ("class A {\n" 2994 " A() : t(0) {}\n" 2995 " A(int i) noexcept() : {}\n" 2996 " A(X x)\n" // FIXME: function-level try blocks are broken. 2997 " try : t(0) {\n" 2998 " } catch (...) {\n" 2999 " }\n" 3000 "};", 3001 format("class A {\n" 3002 " A()\n : t(0) {}\n" 3003 " A(int i)\n noexcept() : {}\n" 3004 " A(X x)\n" 3005 " try : t(0) {} catch (...) {}\n" 3006 "};")); 3007 EXPECT_EQ("class SomeClass {\n" 3008 "public:\n" 3009 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3010 "};", 3011 format("class SomeClass {\n" 3012 "public:\n" 3013 " SomeClass()\n" 3014 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3015 "};")); 3016 EXPECT_EQ("class SomeClass {\n" 3017 "public:\n" 3018 " SomeClass()\n" 3019 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3020 "};", 3021 format("class SomeClass {\n" 3022 "public:\n" 3023 " SomeClass()\n" 3024 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3025 "};", 3026 getLLVMStyleWithColumns(40))); 3027 3028 verifyFormat("MACRO(>)"); 3029 } 3030 3031 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3032 verifyFormat("#define A \\\n" 3033 " f({ \\\n" 3034 " g(); \\\n" 3035 " });", 3036 getLLVMStyleWithColumns(11)); 3037 } 3038 3039 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 3040 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 3041 } 3042 3043 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3044 verifyFormat("{\n { a #c; }\n}"); 3045 } 3046 3047 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3048 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3049 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3050 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3051 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3052 } 3053 3054 TEST_F(FormatTest, EscapedNewlines) { 3055 EXPECT_EQ( 3056 "#define A \\\n int i; \\\n int j;", 3057 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3058 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3059 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3060 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3061 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3062 } 3063 3064 TEST_F(FormatTest, DontCrashOnBlockComments) { 3065 EXPECT_EQ( 3066 "int xxxxxxxxx; /* " 3067 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3068 "zzzzzz\n" 3069 "0*/", 3070 format("int xxxxxxxxx; /* " 3071 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3072 "0*/")); 3073 } 3074 3075 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3076 verifyFormat("#define A \\\n" 3077 " int v( \\\n" 3078 " a); \\\n" 3079 " int i;", 3080 getLLVMStyleWithColumns(11)); 3081 } 3082 3083 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3084 EXPECT_EQ( 3085 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3086 " \\\n" 3087 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3088 "\n" 3089 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3090 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3091 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3092 "\\\n" 3093 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3094 " \n" 3095 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3096 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3097 } 3098 3099 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3100 EXPECT_EQ("int\n" 3101 "#define A\n" 3102 " a;", 3103 format("int\n#define A\na;")); 3104 verifyFormat("functionCallTo(\n" 3105 " someOtherFunction(\n" 3106 " withSomeParameters, whichInSequence,\n" 3107 " areLongerThanALine(andAnotherCall,\n" 3108 "#define A B\n" 3109 " withMoreParamters,\n" 3110 " whichStronglyInfluenceTheLayout),\n" 3111 " andMoreParameters),\n" 3112 " trailing);", 3113 getLLVMStyleWithColumns(69)); 3114 verifyFormat("Foo::Foo()\n" 3115 "#ifdef BAR\n" 3116 " : baz(0)\n" 3117 "#endif\n" 3118 "{\n" 3119 "}"); 3120 verifyFormat("void f() {\n" 3121 " if (true)\n" 3122 "#ifdef A\n" 3123 " f(42);\n" 3124 " x();\n" 3125 "#else\n" 3126 " g();\n" 3127 " x();\n" 3128 "#endif\n" 3129 "}"); 3130 verifyFormat("void f(param1, param2,\n" 3131 " param3,\n" 3132 "#ifdef A\n" 3133 " param4(param5,\n" 3134 "#ifdef A1\n" 3135 " param6,\n" 3136 "#ifdef A2\n" 3137 " param7),\n" 3138 "#else\n" 3139 " param8),\n" 3140 " param9,\n" 3141 "#endif\n" 3142 " param10,\n" 3143 "#endif\n" 3144 " param11)\n" 3145 "#else\n" 3146 " param12)\n" 3147 "#endif\n" 3148 "{\n" 3149 " x();\n" 3150 "}", 3151 getLLVMStyleWithColumns(28)); 3152 verifyFormat("#if 1\n" 3153 "int i;"); 3154 verifyFormat("#if 1\n" 3155 "#endif\n" 3156 "#if 1\n" 3157 "#else\n" 3158 "#endif\n"); 3159 verifyFormat("DEBUG({\n" 3160 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3162 "});\n" 3163 "#if a\n" 3164 "#else\n" 3165 "#endif"); 3166 3167 verifyIncompleteFormat("void f(\n" 3168 "#if A\n" 3169 " );\n" 3170 "#else\n" 3171 "#endif"); 3172 } 3173 3174 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3175 verifyFormat("#endif\n" 3176 "#if B"); 3177 } 3178 3179 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3180 FormatStyle SingleLine = getLLVMStyle(); 3181 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3182 verifyFormat("#if 0\n" 3183 "#elif 1\n" 3184 "#endif\n" 3185 "void foo() {\n" 3186 " if (test) foo2();\n" 3187 "}", 3188 SingleLine); 3189 } 3190 3191 TEST_F(FormatTest, LayoutBlockInsideParens) { 3192 verifyFormat("functionCall({ int i; });"); 3193 verifyFormat("functionCall({\n" 3194 " int i;\n" 3195 " int j;\n" 3196 "});"); 3197 verifyFormat("functionCall(\n" 3198 " {\n" 3199 " int i;\n" 3200 " int j;\n" 3201 " },\n" 3202 " aaaa, bbbb, cccc);"); 3203 verifyFormat("functionA(functionB({\n" 3204 " int i;\n" 3205 " int j;\n" 3206 " }),\n" 3207 " aaaa, bbbb, cccc);"); 3208 verifyFormat("functionCall(\n" 3209 " {\n" 3210 " int i;\n" 3211 " int j;\n" 3212 " },\n" 3213 " aaaa, bbbb, // comment\n" 3214 " cccc);"); 3215 verifyFormat("functionA(functionB({\n" 3216 " int i;\n" 3217 " int j;\n" 3218 " }),\n" 3219 " aaaa, bbbb, // comment\n" 3220 " cccc);"); 3221 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3222 verifyFormat("functionCall(aaaa, bbbb, {\n" 3223 " int i;\n" 3224 " int j;\n" 3225 "});"); 3226 verifyFormat( 3227 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3228 " {\n" 3229 " int i; // break\n" 3230 " },\n" 3231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3232 " ccccccccccccccccc));"); 3233 verifyFormat("DEBUG({\n" 3234 " if (a)\n" 3235 " f();\n" 3236 "});"); 3237 } 3238 3239 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3240 EXPECT_EQ("SOME_MACRO { int i; }\n" 3241 "int i;", 3242 format(" SOME_MACRO {int i;} int i;")); 3243 } 3244 3245 TEST_F(FormatTest, LayoutNestedBlocks) { 3246 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3247 " struct s {\n" 3248 " int i;\n" 3249 " };\n" 3250 " s kBitsToOs[] = {{10}};\n" 3251 " for (int i = 0; i < 10; ++i)\n" 3252 " return;\n" 3253 "}"); 3254 verifyFormat("call(parameter, {\n" 3255 " something();\n" 3256 " // Comment using all columns.\n" 3257 " somethingelse();\n" 3258 "});", 3259 getLLVMStyleWithColumns(40)); 3260 verifyFormat("DEBUG( //\n" 3261 " { f(); }, a);"); 3262 verifyFormat("DEBUG( //\n" 3263 " {\n" 3264 " f(); //\n" 3265 " },\n" 3266 " a);"); 3267 3268 EXPECT_EQ("call(parameter, {\n" 3269 " something();\n" 3270 " // Comment too\n" 3271 " // looooooooooong.\n" 3272 " somethingElse();\n" 3273 "});", 3274 format("call(parameter, {\n" 3275 " something();\n" 3276 " // Comment too looooooooooong.\n" 3277 " somethingElse();\n" 3278 "});", 3279 getLLVMStyleWithColumns(29))); 3280 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3281 EXPECT_EQ("DEBUG({ // comment\n" 3282 " int i;\n" 3283 "});", 3284 format("DEBUG({ // comment\n" 3285 "int i;\n" 3286 "});")); 3287 EXPECT_EQ("DEBUG({\n" 3288 " int i;\n" 3289 "\n" 3290 " // comment\n" 3291 " int j;\n" 3292 "});", 3293 format("DEBUG({\n" 3294 " int i;\n" 3295 "\n" 3296 " // comment\n" 3297 " int j;\n" 3298 "});")); 3299 3300 verifyFormat("DEBUG({\n" 3301 " if (a)\n" 3302 " return;\n" 3303 "});"); 3304 verifyGoogleFormat("DEBUG({\n" 3305 " if (a) return;\n" 3306 "});"); 3307 FormatStyle Style = getGoogleStyle(); 3308 Style.ColumnLimit = 45; 3309 verifyFormat("Debug(aaaaa,\n" 3310 " {\n" 3311 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3312 " },\n" 3313 " a);", 3314 Style); 3315 3316 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3317 3318 verifyNoCrash("^{v^{a}}"); 3319 } 3320 3321 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3322 EXPECT_EQ("#define MACRO() \\\n" 3323 " Debug(aaa, /* force line break */ \\\n" 3324 " { \\\n" 3325 " int i; \\\n" 3326 " int j; \\\n" 3327 " })", 3328 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3329 " { int i; int j; })", 3330 getGoogleStyle())); 3331 3332 EXPECT_EQ("#define A \\\n" 3333 " [] { \\\n" 3334 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3335 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3336 " }", 3337 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3338 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3339 getGoogleStyle())); 3340 } 3341 3342 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3343 EXPECT_EQ("{}", format("{}")); 3344 verifyFormat("enum E {};"); 3345 verifyFormat("enum E {}"); 3346 } 3347 3348 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3349 FormatStyle Style = getLLVMStyle(); 3350 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3351 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3352 verifyFormat("FOO_BEGIN\n" 3353 " FOO_ENTRY\n" 3354 "FOO_END", Style); 3355 verifyFormat("FOO_BEGIN\n" 3356 " NESTED_FOO_BEGIN\n" 3357 " NESTED_FOO_ENTRY\n" 3358 " NESTED_FOO_END\n" 3359 "FOO_END", Style); 3360 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3361 " int x;\n" 3362 " x = 1;\n" 3363 "FOO_END(Baz)", Style); 3364 } 3365 3366 //===----------------------------------------------------------------------===// 3367 // Line break tests. 3368 //===----------------------------------------------------------------------===// 3369 3370 TEST_F(FormatTest, PreventConfusingIndents) { 3371 verifyFormat( 3372 "void f() {\n" 3373 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3374 " parameter, parameter, parameter)),\n" 3375 " SecondLongCall(parameter));\n" 3376 "}"); 3377 verifyFormat( 3378 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3379 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3381 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3382 verifyFormat( 3383 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3384 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3385 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3386 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3387 verifyFormat( 3388 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3389 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3391 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3392 verifyFormat("int a = bbbb && ccc && fffff(\n" 3393 "#define A Just forcing a new line\n" 3394 " ddd);"); 3395 } 3396 3397 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3398 verifyFormat( 3399 "bool aaaaaaa =\n" 3400 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3401 " bbbbbbbb();"); 3402 verifyFormat( 3403 "bool aaaaaaa =\n" 3404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3405 " bbbbbbbb();"); 3406 3407 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3409 " ccccccccc == ddddddddddd;"); 3410 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3412 " ccccccccc == ddddddddddd;"); 3413 verifyFormat( 3414 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3416 " ccccccccc == ddddddddddd;"); 3417 3418 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3419 " aaaaaa) &&\n" 3420 " bbbbbb && cccccc;"); 3421 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3422 " aaaaaa) >>\n" 3423 " bbbbbb;"); 3424 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3425 " SourceMgr.getSpellingColumnNumber(\n" 3426 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3427 " 1);"); 3428 3429 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3430 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3431 " cccccc) {\n}"); 3432 verifyFormat("b = a &&\n" 3433 " // Comment\n" 3434 " b.c && d;"); 3435 3436 // If the LHS of a comparison is not a binary expression itself, the 3437 // additional linebreak confuses many people. 3438 verifyFormat( 3439 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3441 "}"); 3442 verifyFormat( 3443 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3445 "}"); 3446 verifyFormat( 3447 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3449 "}"); 3450 // Even explicit parentheses stress the precedence enough to make the 3451 // additional break unnecessary. 3452 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3454 "}"); 3455 // This cases is borderline, but with the indentation it is still readable. 3456 verifyFormat( 3457 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3458 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3460 "}", 3461 getLLVMStyleWithColumns(75)); 3462 3463 // If the LHS is a binary expression, we should still use the additional break 3464 // as otherwise the formatting hides the operator precedence. 3465 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3467 " 5) {\n" 3468 "}"); 3469 3470 FormatStyle OnePerLine = getLLVMStyle(); 3471 OnePerLine.BinPackParameters = false; 3472 verifyFormat( 3473 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3474 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3476 OnePerLine); 3477 } 3478 3479 TEST_F(FormatTest, ExpressionIndentation) { 3480 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3484 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3485 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3487 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3488 " ccccccccccccccccccccccccccccccccccccccccc;"); 3489 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3492 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3493 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3496 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3497 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3500 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3501 verifyFormat("if () {\n" 3502 "} else if (aaaaa &&\n" 3503 " bbbbb > // break\n" 3504 " ccccc) {\n" 3505 "}"); 3506 3507 // Presence of a trailing comment used to change indentation of b. 3508 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3509 " b;\n" 3510 "return aaaaaaaaaaaaaaaaaaa +\n" 3511 " b; //", 3512 getLLVMStyleWithColumns(30)); 3513 } 3514 3515 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3516 // Not sure what the best system is here. Like this, the LHS can be found 3517 // immediately above an operator (everything with the same or a higher 3518 // indent). The RHS is aligned right of the operator and so compasses 3519 // everything until something with the same indent as the operator is found. 3520 // FIXME: Is this a good system? 3521 FormatStyle Style = getLLVMStyle(); 3522 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3523 verifyFormat( 3524 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3525 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3526 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3527 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3528 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3529 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3530 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3531 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3532 " > ccccccccccccccccccccccccccccccccccccccccc;", 3533 Style); 3534 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3535 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3537 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3538 Style); 3539 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3540 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3541 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3542 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3543 Style); 3544 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3545 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3546 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3547 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3548 Style); 3549 verifyFormat("if () {\n" 3550 "} else if (aaaaa\n" 3551 " && bbbbb // break\n" 3552 " > ccccc) {\n" 3553 "}", 3554 Style); 3555 verifyFormat("return (a)\n" 3556 " // comment\n" 3557 " + b;", 3558 Style); 3559 verifyFormat( 3560 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3561 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3562 " + cc;", 3563 Style); 3564 3565 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3566 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3567 Style); 3568 3569 // Forced by comments. 3570 verifyFormat( 3571 "unsigned ContentSize =\n" 3572 " sizeof(int16_t) // DWARF ARange version number\n" 3573 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3574 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3575 " + sizeof(int8_t); // Segment Size (in bytes)"); 3576 3577 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3578 " == boost::fusion::at_c<1>(iiii).second;", 3579 Style); 3580 3581 Style.ColumnLimit = 60; 3582 verifyFormat("zzzzzzzzzz\n" 3583 " = bbbbbbbbbbbbbbbbb\n" 3584 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3585 Style); 3586 } 3587 3588 TEST_F(FormatTest, NoOperandAlignment) { 3589 FormatStyle Style = getLLVMStyle(); 3590 Style.AlignOperands = false; 3591 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3592 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3593 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3594 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3595 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3596 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3597 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3598 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3599 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3600 " > ccccccccccccccccccccccccccccccccccccccccc;", 3601 Style); 3602 3603 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3604 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3605 " + cc;", 3606 Style); 3607 verifyFormat("int a = aa\n" 3608 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3609 " * cccccccccccccccccccccccccccccccccccc;", 3610 Style); 3611 3612 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3613 verifyFormat("return (a > b\n" 3614 " // comment1\n" 3615 " // comment2\n" 3616 " || c);", 3617 Style); 3618 } 3619 3620 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3621 FormatStyle Style = getLLVMStyle(); 3622 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3623 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3625 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3626 Style); 3627 } 3628 3629 TEST_F(FormatTest, ConstructorInitializers) { 3630 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3631 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3632 getLLVMStyleWithColumns(45)); 3633 verifyFormat("Constructor()\n" 3634 " : Inttializer(FitsOnTheLine) {}", 3635 getLLVMStyleWithColumns(44)); 3636 verifyFormat("Constructor()\n" 3637 " : Inttializer(FitsOnTheLine) {}", 3638 getLLVMStyleWithColumns(43)); 3639 3640 verifyFormat("template <typename T>\n" 3641 "Constructor() : Initializer(FitsOnTheLine) {}", 3642 getLLVMStyleWithColumns(45)); 3643 3644 verifyFormat( 3645 "SomeClass::Constructor()\n" 3646 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3647 3648 verifyFormat( 3649 "SomeClass::Constructor()\n" 3650 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3651 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3652 verifyFormat( 3653 "SomeClass::Constructor()\n" 3654 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3655 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3656 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3657 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3658 " : aaaaaaaaaa(aaaaaa) {}"); 3659 3660 verifyFormat("Constructor()\n" 3661 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3662 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3663 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3664 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3665 3666 verifyFormat("Constructor()\n" 3667 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3669 3670 verifyFormat("Constructor(int Parameter = 0)\n" 3671 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3672 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3673 verifyFormat("Constructor()\n" 3674 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3675 "}", 3676 getLLVMStyleWithColumns(60)); 3677 verifyFormat("Constructor()\n" 3678 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3679 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3680 3681 // Here a line could be saved by splitting the second initializer onto two 3682 // lines, but that is not desirable. 3683 verifyFormat("Constructor()\n" 3684 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3685 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3686 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3687 3688 FormatStyle OnePerLine = getLLVMStyle(); 3689 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3690 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3691 verifyFormat("SomeClass::Constructor()\n" 3692 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3693 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3694 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3695 OnePerLine); 3696 verifyFormat("SomeClass::Constructor()\n" 3697 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3698 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3699 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3700 OnePerLine); 3701 verifyFormat("MyClass::MyClass(int var)\n" 3702 " : some_var_(var), // 4 space indent\n" 3703 " some_other_var_(var + 1) { // lined up\n" 3704 "}", 3705 OnePerLine); 3706 verifyFormat("Constructor()\n" 3707 " : aaaaa(aaaaaa),\n" 3708 " aaaaa(aaaaaa),\n" 3709 " aaaaa(aaaaaa),\n" 3710 " aaaaa(aaaaaa),\n" 3711 " aaaaa(aaaaaa) {}", 3712 OnePerLine); 3713 verifyFormat("Constructor()\n" 3714 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3715 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3716 OnePerLine); 3717 OnePerLine.BinPackParameters = false; 3718 verifyFormat( 3719 "Constructor()\n" 3720 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3721 " aaaaaaaaaaa().aaa(),\n" 3722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3723 OnePerLine); 3724 OnePerLine.ColumnLimit = 60; 3725 verifyFormat("Constructor()\n" 3726 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3727 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3728 OnePerLine); 3729 3730 EXPECT_EQ("Constructor()\n" 3731 " : // Comment forcing unwanted break.\n" 3732 " aaaa(aaaa) {}", 3733 format("Constructor() :\n" 3734 " // Comment forcing unwanted break.\n" 3735 " aaaa(aaaa) {}")); 3736 } 3737 3738 TEST_F(FormatTest, MemoizationTests) { 3739 // This breaks if the memoization lookup does not take \c Indent and 3740 // \c LastSpace into account. 3741 verifyFormat( 3742 "extern CFRunLoopTimerRef\n" 3743 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3744 " CFTimeInterval interval, CFOptionFlags flags,\n" 3745 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3746 " CFRunLoopTimerContext *context) {}"); 3747 3748 // Deep nesting somewhat works around our memoization. 3749 verifyFormat( 3750 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3751 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3752 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3753 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3754 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3755 getLLVMStyleWithColumns(65)); 3756 verifyFormat( 3757 "aaaaa(\n" 3758 " aaaaa,\n" 3759 " aaaaa(\n" 3760 " aaaaa,\n" 3761 " aaaaa(\n" 3762 " aaaaa,\n" 3763 " aaaaa(\n" 3764 " aaaaa,\n" 3765 " aaaaa(\n" 3766 " aaaaa,\n" 3767 " aaaaa(\n" 3768 " aaaaa,\n" 3769 " aaaaa(\n" 3770 " aaaaa,\n" 3771 " aaaaa(\n" 3772 " aaaaa,\n" 3773 " aaaaa(\n" 3774 " aaaaa,\n" 3775 " aaaaa(\n" 3776 " aaaaa,\n" 3777 " aaaaa(\n" 3778 " aaaaa,\n" 3779 " aaaaa(\n" 3780 " aaaaa,\n" 3781 " aaaaa))))))))))));", 3782 getLLVMStyleWithColumns(65)); 3783 verifyFormat( 3784 "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" 3785 " a),\n" 3786 " a),\n" 3787 " a),\n" 3788 " a),\n" 3789 " a),\n" 3790 " a),\n" 3791 " a),\n" 3792 " a),\n" 3793 " a),\n" 3794 " a),\n" 3795 " a),\n" 3796 " a),\n" 3797 " a),\n" 3798 " a),\n" 3799 " a),\n" 3800 " a),\n" 3801 " a)", 3802 getLLVMStyleWithColumns(65)); 3803 3804 // This test takes VERY long when memoization is broken. 3805 FormatStyle OnePerLine = getLLVMStyle(); 3806 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3807 OnePerLine.BinPackParameters = false; 3808 std::string input = "Constructor()\n" 3809 " : aaaa(a,\n"; 3810 for (unsigned i = 0, e = 80; i != e; ++i) { 3811 input += " a,\n"; 3812 } 3813 input += " a) {}"; 3814 verifyFormat(input, OnePerLine); 3815 } 3816 3817 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3818 verifyFormat( 3819 "void f() {\n" 3820 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3821 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3822 " f();\n" 3823 "}"); 3824 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3825 " Intervals[i - 1].getRange().getLast()) {\n}"); 3826 } 3827 3828 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3829 // Principially, we break function declarations in a certain order: 3830 // 1) break amongst arguments. 3831 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3832 " Cccccccccccccc cccccccccccccc);"); 3833 verifyFormat("template <class TemplateIt>\n" 3834 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3835 " TemplateIt *stop) {}"); 3836 3837 // 2) break after return type. 3838 verifyFormat( 3839 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3840 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3841 getGoogleStyle()); 3842 3843 // 3) break after (. 3844 verifyFormat( 3845 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3846 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3847 getGoogleStyle()); 3848 3849 // 4) break before after nested name specifiers. 3850 verifyFormat( 3851 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3852 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3853 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3854 getGoogleStyle()); 3855 3856 // However, there are exceptions, if a sufficient amount of lines can be 3857 // saved. 3858 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3859 // more adjusting. 3860 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3861 " Cccccccccccccc cccccccccc,\n" 3862 " Cccccccccccccc cccccccccc,\n" 3863 " Cccccccccccccc cccccccccc,\n" 3864 " Cccccccccccccc cccccccccc);"); 3865 verifyFormat( 3866 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3867 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3868 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3869 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3870 getGoogleStyle()); 3871 verifyFormat( 3872 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3873 " Cccccccccccccc cccccccccc,\n" 3874 " Cccccccccccccc cccccccccc,\n" 3875 " Cccccccccccccc cccccccccc,\n" 3876 " Cccccccccccccc cccccccccc,\n" 3877 " Cccccccccccccc cccccccccc,\n" 3878 " Cccccccccccccc cccccccccc);"); 3879 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3880 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3881 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3882 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3883 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3884 3885 // Break after multi-line parameters. 3886 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3889 " bbbb bbbb);"); 3890 verifyFormat("void SomeLoooooooooooongFunction(\n" 3891 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3892 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3893 " int bbbbbbbbbbbbb);"); 3894 3895 // Treat overloaded operators like other functions. 3896 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3897 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3898 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3899 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3900 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3901 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3902 verifyGoogleFormat( 3903 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3904 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3905 verifyGoogleFormat( 3906 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3907 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3908 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3909 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3910 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3911 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3912 verifyGoogleFormat( 3913 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3914 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3915 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3916 verifyGoogleFormat( 3917 "template <typename T>\n" 3918 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3919 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3920 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3921 3922 FormatStyle Style = getLLVMStyle(); 3923 Style.PointerAlignment = FormatStyle::PAS_Left; 3924 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3925 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3926 Style); 3927 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3929 Style); 3930 } 3931 3932 TEST_F(FormatTest, TrailingReturnType) { 3933 verifyFormat("auto foo() -> int;\n"); 3934 verifyFormat("struct S {\n" 3935 " auto bar() const -> int;\n" 3936 "};"); 3937 verifyFormat("template <size_t Order, typename T>\n" 3938 "auto load_img(const std::string &filename)\n" 3939 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3940 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3941 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3942 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3943 verifyFormat("template <typename T>\n" 3944 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3945 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3946 3947 // Not trailing return types. 3948 verifyFormat("void f() { auto a = b->c(); }"); 3949 } 3950 3951 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3952 // Avoid breaking before trailing 'const' or other trailing annotations, if 3953 // they are not function-like. 3954 FormatStyle Style = getGoogleStyle(); 3955 Style.ColumnLimit = 47; 3956 verifyFormat("void someLongFunction(\n" 3957 " int someLoooooooooooooongParameter) const {\n}", 3958 getLLVMStyleWithColumns(47)); 3959 verifyFormat("LoooooongReturnType\n" 3960 "someLoooooooongFunction() const {}", 3961 getLLVMStyleWithColumns(47)); 3962 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3963 " const {}", 3964 Style); 3965 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3966 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3967 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3968 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3969 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3970 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3971 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3972 " aaaaaaaaaaa aaaaa) const override;"); 3973 verifyGoogleFormat( 3974 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3975 " const override;"); 3976 3977 // Even if the first parameter has to be wrapped. 3978 verifyFormat("void someLongFunction(\n" 3979 " int someLongParameter) const {}", 3980 getLLVMStyleWithColumns(46)); 3981 verifyFormat("void someLongFunction(\n" 3982 " int someLongParameter) const {}", 3983 Style); 3984 verifyFormat("void someLongFunction(\n" 3985 " int someLongParameter) override {}", 3986 Style); 3987 verifyFormat("void someLongFunction(\n" 3988 " int someLongParameter) OVERRIDE {}", 3989 Style); 3990 verifyFormat("void someLongFunction(\n" 3991 " int someLongParameter) final {}", 3992 Style); 3993 verifyFormat("void someLongFunction(\n" 3994 " int someLongParameter) FINAL {}", 3995 Style); 3996 verifyFormat("void someLongFunction(\n" 3997 " int parameter) const override {}", 3998 Style); 3999 4000 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4001 verifyFormat("void someLongFunction(\n" 4002 " int someLongParameter) const\n" 4003 "{\n" 4004 "}", 4005 Style); 4006 4007 // Unless these are unknown annotations. 4008 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4009 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4010 " LONG_AND_UGLY_ANNOTATION;"); 4011 4012 // Breaking before function-like trailing annotations is fine to keep them 4013 // close to their arguments. 4014 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4015 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4016 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4017 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4018 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4019 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4020 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4021 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4022 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4023 4024 verifyFormat( 4025 "void aaaaaaaaaaaaaaaaaa()\n" 4026 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4027 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4028 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4029 " __attribute__((unused));"); 4030 verifyGoogleFormat( 4031 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4032 " GUARDED_BY(aaaaaaaaaaaa);"); 4033 verifyGoogleFormat( 4034 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4035 " GUARDED_BY(aaaaaaaaaaaa);"); 4036 verifyGoogleFormat( 4037 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4038 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4039 verifyGoogleFormat( 4040 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4041 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4042 } 4043 4044 TEST_F(FormatTest, FunctionAnnotations) { 4045 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4046 "int OldFunction(const string ¶meter) {}"); 4047 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4048 "string OldFunction(const string ¶meter) {}"); 4049 verifyFormat("template <typename T>\n" 4050 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4051 "string OldFunction(const string ¶meter) {}"); 4052 4053 // Not function annotations. 4054 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4055 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4056 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4057 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4058 verifyFormat("MACRO(abc).function() // wrap\n" 4059 " << abc;"); 4060 verifyFormat("MACRO(abc)->function() // wrap\n" 4061 " << abc;"); 4062 verifyFormat("MACRO(abc)::function() // wrap\n" 4063 " << abc;"); 4064 } 4065 4066 TEST_F(FormatTest, BreaksDesireably) { 4067 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4068 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4069 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4070 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4072 "}"); 4073 4074 verifyFormat( 4075 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4077 4078 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4081 4082 verifyFormat( 4083 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4085 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4087 4088 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4089 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4090 4091 verifyFormat( 4092 "void f() {\n" 4093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4095 "}"); 4096 verifyFormat( 4097 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4099 verifyFormat( 4100 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4102 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4105 4106 // Indent consistently independent of call expression and unary operator. 4107 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4108 " dddddddddddddddddddddddddddddd));"); 4109 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4110 " dddddddddddddddddddddddddddddd));"); 4111 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4112 " dddddddddddddddddddddddddddddd));"); 4113 4114 // This test case breaks on an incorrect memoization, i.e. an optimization not 4115 // taking into account the StopAt value. 4116 verifyFormat( 4117 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4118 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4119 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4120 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4121 4122 verifyFormat("{\n {\n {\n" 4123 " Annotation.SpaceRequiredBefore =\n" 4124 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4125 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4126 " }\n }\n}"); 4127 4128 // Break on an outer level if there was a break on an inner level. 4129 EXPECT_EQ("f(g(h(a, // comment\n" 4130 " b, c),\n" 4131 " d, e),\n" 4132 " x, y);", 4133 format("f(g(h(a, // comment\n" 4134 " b, c), d, e), x, y);")); 4135 4136 // Prefer breaking similar line breaks. 4137 verifyFormat( 4138 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4139 " NSTrackingMouseEnteredAndExited |\n" 4140 " NSTrackingActiveAlways;"); 4141 } 4142 4143 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4144 FormatStyle NoBinPacking = getGoogleStyle(); 4145 NoBinPacking.BinPackParameters = false; 4146 NoBinPacking.BinPackArguments = true; 4147 verifyFormat("void f() {\n" 4148 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4150 "}", 4151 NoBinPacking); 4152 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4153 " int aaaaaaaaaaaaaaaaaaaa,\n" 4154 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4155 NoBinPacking); 4156 4157 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4158 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4159 " vector<int> bbbbbbbbbbbbbbb);", 4160 NoBinPacking); 4161 // FIXME: This behavior difference is probably not wanted. However, currently 4162 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4163 // template arguments from BreakBeforeParameter being set because of the 4164 // one-per-line formatting. 4165 verifyFormat( 4166 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4167 " aaaaaaaaaa> aaaaaaaaaa);", 4168 NoBinPacking); 4169 verifyFormat( 4170 "void fffffffffff(\n" 4171 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4172 " aaaaaaaaaa);"); 4173 } 4174 4175 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4176 FormatStyle NoBinPacking = getGoogleStyle(); 4177 NoBinPacking.BinPackParameters = false; 4178 NoBinPacking.BinPackArguments = false; 4179 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4180 " aaaaaaaaaaaaaaaaaaaa,\n" 4181 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4182 NoBinPacking); 4183 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4184 " aaaaaaaaaaaaa,\n" 4185 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4186 NoBinPacking); 4187 verifyFormat( 4188 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4189 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4191 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4193 NoBinPacking); 4194 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4195 " .aaaaaaaaaaaaaaaaaa();", 4196 NoBinPacking); 4197 verifyFormat("void f() {\n" 4198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4199 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4200 "}", 4201 NoBinPacking); 4202 4203 verifyFormat( 4204 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4205 " aaaaaaaaaaaa,\n" 4206 " aaaaaaaaaaaa);", 4207 NoBinPacking); 4208 verifyFormat( 4209 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4210 " ddddddddddddddddddddddddddddd),\n" 4211 " test);", 4212 NoBinPacking); 4213 4214 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4215 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4216 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4217 " aaaaaaaaaaaaaaaaaa;", 4218 NoBinPacking); 4219 verifyFormat("a(\"a\"\n" 4220 " \"a\",\n" 4221 " a);"); 4222 4223 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4224 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4225 " aaaaaaaaa,\n" 4226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4227 NoBinPacking); 4228 verifyFormat( 4229 "void f() {\n" 4230 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4231 " .aaaaaaa();\n" 4232 "}", 4233 NoBinPacking); 4234 verifyFormat( 4235 "template <class SomeType, class SomeOtherType>\n" 4236 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4237 NoBinPacking); 4238 } 4239 4240 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4241 FormatStyle Style = getLLVMStyleWithColumns(15); 4242 Style.ExperimentalAutoDetectBinPacking = true; 4243 EXPECT_EQ("aaa(aaaa,\n" 4244 " aaaa,\n" 4245 " aaaa);\n" 4246 "aaa(aaaa,\n" 4247 " aaaa,\n" 4248 " aaaa);", 4249 format("aaa(aaaa,\n" // one-per-line 4250 " aaaa,\n" 4251 " aaaa );\n" 4252 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4253 Style)); 4254 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4255 " aaaa);\n" 4256 "aaa(aaaa, aaaa,\n" 4257 " aaaa);", 4258 format("aaa(aaaa, aaaa,\n" // bin-packed 4259 " aaaa );\n" 4260 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4261 Style)); 4262 } 4263 4264 TEST_F(FormatTest, FormatsBuilderPattern) { 4265 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4266 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4267 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4268 " .StartsWith(\".init\", ORDER_INIT)\n" 4269 " .StartsWith(\".fini\", ORDER_FINI)\n" 4270 " .StartsWith(\".hash\", ORDER_HASH)\n" 4271 " .Default(ORDER_TEXT);\n"); 4272 4273 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4274 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4275 verifyFormat( 4276 "aaaaaaa->aaaaaaa\n" 4277 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4279 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4280 verifyFormat( 4281 "aaaaaaa->aaaaaaa\n" 4282 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4283 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4284 verifyFormat( 4285 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4286 " aaaaaaaaaaaaaa);"); 4287 verifyFormat( 4288 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4289 " aaaaaa->aaaaaaaaaaaa()\n" 4290 " ->aaaaaaaaaaaaaaaa(\n" 4291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4292 " ->aaaaaaaaaaaaaaaaa();"); 4293 verifyGoogleFormat( 4294 "void f() {\n" 4295 " someo->Add((new util::filetools::Handler(dir))\n" 4296 " ->OnEvent1(NewPermanentCallback(\n" 4297 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4298 " ->OnEvent2(NewPermanentCallback(\n" 4299 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4300 " ->OnEvent3(NewPermanentCallback(\n" 4301 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4302 " ->OnEvent5(NewPermanentCallback(\n" 4303 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4304 " ->OnEvent6(NewPermanentCallback(\n" 4305 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4306 "}"); 4307 4308 verifyFormat( 4309 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4310 verifyFormat("aaaaaaaaaaaaaaa()\n" 4311 " .aaaaaaaaaaaaaaa()\n" 4312 " .aaaaaaaaaaaaaaa()\n" 4313 " .aaaaaaaaaaaaaaa()\n" 4314 " .aaaaaaaaaaaaaaa();"); 4315 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4316 " .aaaaaaaaaaaaaaa()\n" 4317 " .aaaaaaaaaaaaaaa()\n" 4318 " .aaaaaaaaaaaaaaa();"); 4319 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4320 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4321 " .aaaaaaaaaaaaaaa();"); 4322 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4323 " ->aaaaaaaaaaaaaae(0)\n" 4324 " ->aaaaaaaaaaaaaaa();"); 4325 4326 // Don't linewrap after very short segments. 4327 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4328 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4329 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4330 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4331 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4332 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4333 verifyFormat("aaa()\n" 4334 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4335 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4336 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4337 4338 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4339 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4340 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4341 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4342 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4344 4345 // Prefer not to break after empty parentheses. 4346 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4347 " First->LastNewlineOffset);"); 4348 4349 // Prefer not to create "hanging" indents. 4350 verifyFormat( 4351 "return !soooooooooooooome_map\n" 4352 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4353 " .second;"); 4354 verifyFormat( 4355 "return aaaaaaaaaaaaaaaa\n" 4356 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4357 " .aaaa(aaaaaaaaaaaaaa);"); 4358 // No hanging indent here. 4359 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4361 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4362 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4363 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4364 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4365 getLLVMStyleWithColumns(60)); 4366 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4367 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4368 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4369 getLLVMStyleWithColumns(59)); 4370 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4372 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4373 } 4374 4375 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4376 verifyFormat( 4377 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4378 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4379 verifyFormat( 4380 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4381 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4382 4383 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4384 " ccccccccccccccccccccccccc) {\n}"); 4385 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4386 " ccccccccccccccccccccccccc) {\n}"); 4387 4388 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4389 " ccccccccccccccccccccccccc) {\n}"); 4390 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4391 " ccccccccccccccccccccccccc) {\n}"); 4392 4393 verifyFormat( 4394 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4395 " ccccccccccccccccccccccccc) {\n}"); 4396 verifyFormat( 4397 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4398 " ccccccccccccccccccccccccc) {\n}"); 4399 4400 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4401 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4402 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4403 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4404 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4405 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4406 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4407 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4408 4409 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4410 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4411 " aaaaaaaaaaaaaaa != aa) {\n}"); 4412 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4413 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4414 " aaaaaaaaaaaaaaa != aa) {\n}"); 4415 } 4416 4417 TEST_F(FormatTest, BreaksAfterAssignments) { 4418 verifyFormat( 4419 "unsigned Cost =\n" 4420 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4421 " SI->getPointerAddressSpaceee());\n"); 4422 verifyFormat( 4423 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4424 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4425 4426 verifyFormat( 4427 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4428 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4429 verifyFormat("unsigned OriginalStartColumn =\n" 4430 " SourceMgr.getSpellingColumnNumber(\n" 4431 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4432 " 1;"); 4433 } 4434 4435 TEST_F(FormatTest, AlignsAfterAssignments) { 4436 verifyFormat( 4437 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4438 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4439 verifyFormat( 4440 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4441 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4442 verifyFormat( 4443 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4444 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4445 verifyFormat( 4446 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4447 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4448 verifyFormat( 4449 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4450 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4451 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4452 } 4453 4454 TEST_F(FormatTest, AlignsAfterReturn) { 4455 verifyFormat( 4456 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4457 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4458 verifyFormat( 4459 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4460 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4461 verifyFormat( 4462 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4463 " aaaaaaaaaaaaaaaaaaaaaa();"); 4464 verifyFormat( 4465 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4466 " aaaaaaaaaaaaaaaaaaaaaa());"); 4467 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4469 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4472 verifyFormat("return\n" 4473 " // true if code is one of a or b.\n" 4474 " code == a || code == b;"); 4475 } 4476 4477 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4478 verifyFormat( 4479 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4480 " aaaaaaaaa aaaaaaa) {}"); 4481 verifyFormat( 4482 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4483 " aaaaaaaaaaa aaaaaaaaa);"); 4484 verifyFormat( 4485 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4486 " aaaaaaaaaaaaaaaaaaaaa));"); 4487 FormatStyle Style = getLLVMStyle(); 4488 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4489 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4490 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4491 Style); 4492 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4493 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4494 Style); 4495 verifyFormat("SomeLongVariableName->someFunction(\n" 4496 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4497 Style); 4498 verifyFormat( 4499 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4500 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4501 Style); 4502 verifyFormat( 4503 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4504 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4505 Style); 4506 verifyFormat( 4507 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4508 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4509 Style); 4510 4511 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4512 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4513 " b));", 4514 Style); 4515 4516 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4517 Style.BinPackArguments = false; 4518 Style.BinPackParameters = false; 4519 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4520 " aaaaaaaaaaa aaaaaaaa,\n" 4521 " aaaaaaaaa aaaaaaa,\n" 4522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4523 Style); 4524 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4525 " aaaaaaaaaaa aaaaaaaaa,\n" 4526 " aaaaaaaaaaa aaaaaaaaa,\n" 4527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4528 Style); 4529 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4530 " aaaaaaaaaaaaaaa,\n" 4531 " aaaaaaaaaaaaaaaaaaaaa,\n" 4532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4533 Style); 4534 verifyFormat( 4535 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4536 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4537 Style); 4538 verifyFormat( 4539 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4540 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4541 Style); 4542 verifyFormat( 4543 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4544 " aaaaaaaaaaaaaaaaaaaaa(\n" 4545 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4546 " aaaaaaaaaaaaaaaa);", 4547 Style); 4548 verifyFormat( 4549 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4550 " aaaaaaaaaaaaaaaaaaaaa(\n" 4551 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4552 " aaaaaaaaaaaaaaaa);", 4553 Style); 4554 } 4555 4556 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4557 FormatStyle Style = getLLVMStyleWithColumns(40); 4558 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4559 " bbbbbbbbbbbbbbbbbbbbbb);", 4560 Style); 4561 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4562 Style.AlignOperands = false; 4563 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4564 " bbbbbbbbbbbbbbbbbbbbbb);", 4565 Style); 4566 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4567 Style.AlignOperands = true; 4568 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4569 " bbbbbbbbbbbbbbbbbbbbbb);", 4570 Style); 4571 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4572 Style.AlignOperands = false; 4573 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4574 " bbbbbbbbbbbbbbbbbbbbbb);", 4575 Style); 4576 } 4577 4578 TEST_F(FormatTest, BreaksConditionalExpressions) { 4579 verifyFormat( 4580 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4581 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4582 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4583 verifyFormat( 4584 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4585 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4587 verifyFormat( 4588 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4589 " : aaaaaaaaaaaaa);"); 4590 verifyFormat( 4591 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4592 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4593 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4594 " aaaaaaaaaaaaa);"); 4595 verifyFormat( 4596 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4597 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4598 " aaaaaaaaaaaaa);"); 4599 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4600 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4602 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4604 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4606 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4608 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4610 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4611 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4613 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4615 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4616 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4617 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4618 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4619 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4621 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4622 " : aaaaaaaaaaaaaaaa;"); 4623 verifyFormat( 4624 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4625 " ? aaaaaaaaaaaaaaa\n" 4626 " : aaaaaaaaaaaaaaa;"); 4627 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4628 " aaaaaaaaa\n" 4629 " ? b\n" 4630 " : c);"); 4631 verifyFormat("return aaaa == bbbb\n" 4632 " // comment\n" 4633 " ? aaaa\n" 4634 " : bbbb;"); 4635 verifyFormat("unsigned Indent =\n" 4636 " format(TheLine.First,\n" 4637 " IndentForLevel[TheLine.Level] >= 0\n" 4638 " ? IndentForLevel[TheLine.Level]\n" 4639 " : TheLine * 2,\n" 4640 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4641 getLLVMStyleWithColumns(60)); 4642 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4643 " ? aaaaaaaaaaaaaaa\n" 4644 " : bbbbbbbbbbbbbbb //\n" 4645 " ? ccccccccccccccc\n" 4646 " : ddddddddddddddd;"); 4647 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4648 " ? aaaaaaaaaaaaaaa\n" 4649 " : (bbbbbbbbbbbbbbb //\n" 4650 " ? ccccccccccccccc\n" 4651 " : ddddddddddddddd);"); 4652 verifyFormat( 4653 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4654 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4655 " aaaaaaaaaaaaaaaaaaaaa +\n" 4656 " aaaaaaaaaaaaaaaaaaaaa\n" 4657 " : aaaaaaaaaa;"); 4658 verifyFormat( 4659 "aaaaaa = aaaaaaaaaaaa\n" 4660 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4661 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4662 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4663 4664 FormatStyle NoBinPacking = getLLVMStyle(); 4665 NoBinPacking.BinPackArguments = false; 4666 verifyFormat( 4667 "void f() {\n" 4668 " g(aaa,\n" 4669 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4671 " ? aaaaaaaaaaaaaaa\n" 4672 " : aaaaaaaaaaaaaaa);\n" 4673 "}", 4674 NoBinPacking); 4675 verifyFormat( 4676 "void f() {\n" 4677 " g(aaa,\n" 4678 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4680 " ?: aaaaaaaaaaaaaaa);\n" 4681 "}", 4682 NoBinPacking); 4683 4684 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4685 " // comment.\n" 4686 " ccccccccccccccccccccccccccccccccccccccc\n" 4687 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4688 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4689 4690 // Assignments in conditional expressions. Apparently not uncommon :-(. 4691 verifyFormat("return a != b\n" 4692 " // comment\n" 4693 " ? a = b\n" 4694 " : a = b;"); 4695 verifyFormat("return a != b\n" 4696 " // comment\n" 4697 " ? a = a != b\n" 4698 " // comment\n" 4699 " ? a = b\n" 4700 " : a\n" 4701 " : a;\n"); 4702 verifyFormat("return a != b\n" 4703 " // comment\n" 4704 " ? a\n" 4705 " : a = a != b\n" 4706 " // comment\n" 4707 " ? a = b\n" 4708 " : a;"); 4709 } 4710 4711 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4712 FormatStyle Style = getLLVMStyle(); 4713 Style.BreakBeforeTernaryOperators = false; 4714 Style.ColumnLimit = 70; 4715 verifyFormat( 4716 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4717 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4719 Style); 4720 verifyFormat( 4721 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4722 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4724 Style); 4725 verifyFormat( 4726 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4727 " aaaaaaaaaaaaa);", 4728 Style); 4729 verifyFormat( 4730 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4731 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4733 " aaaaaaaaaaaaa);", 4734 Style); 4735 verifyFormat( 4736 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4737 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4738 " aaaaaaaaaaaaa);", 4739 Style); 4740 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4745 Style); 4746 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4752 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4753 Style); 4754 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4755 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4758 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4759 Style); 4760 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4761 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4762 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4763 Style); 4764 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4768 Style); 4769 verifyFormat( 4770 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4771 " aaaaaaaaaaaaaaa :\n" 4772 " aaaaaaaaaaaaaaa;", 4773 Style); 4774 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4775 " aaaaaaaaa ?\n" 4776 " b :\n" 4777 " c);", 4778 Style); 4779 verifyFormat("unsigned Indent =\n" 4780 " format(TheLine.First,\n" 4781 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4782 " IndentForLevel[TheLine.Level] :\n" 4783 " TheLine * 2,\n" 4784 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4785 Style); 4786 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4787 " aaaaaaaaaaaaaaa :\n" 4788 " bbbbbbbbbbbbbbb ? //\n" 4789 " ccccccccccccccc :\n" 4790 " ddddddddddddddd;", 4791 Style); 4792 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4793 " aaaaaaaaaaaaaaa :\n" 4794 " (bbbbbbbbbbbbbbb ? //\n" 4795 " ccccccccccccccc :\n" 4796 " ddddddddddddddd);", 4797 Style); 4798 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4799 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4800 " ccccccccccccccccccccccccccc;", 4801 Style); 4802 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4803 " aaaaa :\n" 4804 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4805 Style); 4806 } 4807 4808 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4809 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4810 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4811 verifyFormat("bool a = true, b = false;"); 4812 4813 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4815 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4816 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4817 verifyFormat( 4818 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4819 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4820 " d = e && f;"); 4821 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4822 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4823 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4824 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4825 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4826 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4827 4828 FormatStyle Style = getGoogleStyle(); 4829 Style.PointerAlignment = FormatStyle::PAS_Left; 4830 Style.DerivePointerAlignment = false; 4831 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4832 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4833 " *b = bbbbbbbbbbbbbbbbbbb;", 4834 Style); 4835 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4836 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4837 Style); 4838 verifyFormat("vector<int*> a, b;", Style); 4839 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4840 } 4841 4842 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4843 verifyFormat("arr[foo ? bar : baz];"); 4844 verifyFormat("f()[foo ? bar : baz];"); 4845 verifyFormat("(a + b)[foo ? bar : baz];"); 4846 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4847 } 4848 4849 TEST_F(FormatTest, AlignsStringLiterals) { 4850 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4851 " \"short literal\");"); 4852 verifyFormat( 4853 "looooooooooooooooooooooooongFunction(\n" 4854 " \"short literal\"\n" 4855 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4856 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4857 " \" string literals\",\n" 4858 " and, other, parameters);"); 4859 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4860 " \"5678\";", 4861 format("fun + \"1243\" /* comment */\n" 4862 " \"5678\";", 4863 getLLVMStyleWithColumns(28))); 4864 EXPECT_EQ( 4865 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4866 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4867 " \"aaaaaaaaaaaaaaaa\";", 4868 format("aaaaaa =" 4869 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4870 "aaaaaaaaaaaaaaaaaaaaa\" " 4871 "\"aaaaaaaaaaaaaaaa\";")); 4872 verifyFormat("a = a + \"a\"\n" 4873 " \"a\"\n" 4874 " \"a\";"); 4875 verifyFormat("f(\"a\", \"b\"\n" 4876 " \"c\");"); 4877 4878 verifyFormat( 4879 "#define LL_FORMAT \"ll\"\n" 4880 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4881 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4882 4883 verifyFormat("#define A(X) \\\n" 4884 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4885 " \"ccccc\"", 4886 getLLVMStyleWithColumns(23)); 4887 verifyFormat("#define A \"def\"\n" 4888 "f(\"abc\" A \"ghi\"\n" 4889 " \"jkl\");"); 4890 4891 verifyFormat("f(L\"a\"\n" 4892 " L\"b\");"); 4893 verifyFormat("#define A(X) \\\n" 4894 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4895 " L\"ccccc\"", 4896 getLLVMStyleWithColumns(25)); 4897 4898 verifyFormat("f(@\"a\"\n" 4899 " @\"b\");"); 4900 verifyFormat("NSString s = @\"a\"\n" 4901 " @\"b\"\n" 4902 " @\"c\";"); 4903 verifyFormat("NSString s = @\"a\"\n" 4904 " \"b\"\n" 4905 " \"c\";"); 4906 } 4907 4908 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4909 FormatStyle Style = getLLVMStyle(); 4910 // No declarations or definitions should be moved to own line. 4911 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4912 verifyFormat("class A {\n" 4913 " int f() { return 1; }\n" 4914 " int g();\n" 4915 "};\n" 4916 "int f() { return 1; }\n" 4917 "int g();\n", 4918 Style); 4919 4920 // All declarations and definitions should have the return type moved to its 4921 // own 4922 // line. 4923 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4924 verifyFormat("class E {\n" 4925 " int\n" 4926 " f() {\n" 4927 " return 1;\n" 4928 " }\n" 4929 " int\n" 4930 " g();\n" 4931 "};\n" 4932 "int\n" 4933 "f() {\n" 4934 " return 1;\n" 4935 "}\n" 4936 "int\n" 4937 "g();\n", 4938 Style); 4939 4940 // Top-level definitions, and no kinds of declarations should have the 4941 // return type moved to its own line. 4942 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4943 verifyFormat("class B {\n" 4944 " int f() { return 1; }\n" 4945 " int g();\n" 4946 "};\n" 4947 "int\n" 4948 "f() {\n" 4949 " return 1;\n" 4950 "}\n" 4951 "int g();\n", 4952 Style); 4953 4954 // Top-level definitions and declarations should have the return type moved 4955 // to its own line. 4956 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4957 verifyFormat("class C {\n" 4958 " int f() { return 1; }\n" 4959 " int g();\n" 4960 "};\n" 4961 "int\n" 4962 "f() {\n" 4963 " return 1;\n" 4964 "}\n" 4965 "int\n" 4966 "g();\n", 4967 Style); 4968 4969 // All definitions should have the return type moved to its own line, but no 4970 // kinds of declarations. 4971 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4972 verifyFormat("class D {\n" 4973 " int\n" 4974 " f() {\n" 4975 " return 1;\n" 4976 " }\n" 4977 " int g();\n" 4978 "};\n" 4979 "int\n" 4980 "f() {\n" 4981 " return 1;\n" 4982 "}\n" 4983 "int g();\n", 4984 Style); 4985 verifyFormat("const char *\n" 4986 "f(void) {\n" // Break here. 4987 " return \"\";\n" 4988 "}\n" 4989 "const char *bar(void);\n", // No break here. 4990 Style); 4991 verifyFormat("template <class T>\n" 4992 "T *\n" 4993 "f(T &c) {\n" // Break here. 4994 " return NULL;\n" 4995 "}\n" 4996 "template <class T> T *f(T &c);\n", // No break here. 4997 Style); 4998 verifyFormat("class C {\n" 4999 " int\n" 5000 " operator+() {\n" 5001 " return 1;\n" 5002 " }\n" 5003 " int\n" 5004 " operator()() {\n" 5005 " return 1;\n" 5006 " }\n" 5007 "};\n", 5008 Style); 5009 verifyFormat("void\n" 5010 "A::operator()() {}\n" 5011 "void\n" 5012 "A::operator>>() {}\n" 5013 "void\n" 5014 "A::operator+() {}\n", 5015 Style); 5016 verifyFormat("void *operator new(std::size_t s);", // No break here. 5017 Style); 5018 verifyFormat("void *\n" 5019 "operator new(std::size_t s) {}", 5020 Style); 5021 verifyFormat("void *\n" 5022 "operator delete[](void *ptr) {}", 5023 Style); 5024 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5025 verifyFormat("const char *\n" 5026 "f(void)\n" // Break here. 5027 "{\n" 5028 " return \"\";\n" 5029 "}\n" 5030 "const char *bar(void);\n", // No break here. 5031 Style); 5032 verifyFormat("template <class T>\n" 5033 "T *\n" // Problem here: no line break 5034 "f(T &c)\n" // Break here. 5035 "{\n" 5036 " return NULL;\n" 5037 "}\n" 5038 "template <class T> T *f(T &c);\n", // No break here. 5039 Style); 5040 } 5041 5042 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5043 FormatStyle NoBreak = getLLVMStyle(); 5044 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5045 FormatStyle Break = getLLVMStyle(); 5046 Break.AlwaysBreakBeforeMultilineStrings = true; 5047 verifyFormat("aaaa = \"bbbb\"\n" 5048 " \"cccc\";", 5049 NoBreak); 5050 verifyFormat("aaaa =\n" 5051 " \"bbbb\"\n" 5052 " \"cccc\";", 5053 Break); 5054 verifyFormat("aaaa(\"bbbb\"\n" 5055 " \"cccc\");", 5056 NoBreak); 5057 verifyFormat("aaaa(\n" 5058 " \"bbbb\"\n" 5059 " \"cccc\");", 5060 Break); 5061 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5062 " \"cccc\");", 5063 NoBreak); 5064 verifyFormat("aaaa(qqq,\n" 5065 " \"bbbb\"\n" 5066 " \"cccc\");", 5067 Break); 5068 verifyFormat("aaaa(qqq,\n" 5069 " L\"bbbb\"\n" 5070 " L\"cccc\");", 5071 Break); 5072 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5073 " \"bbbb\"));", 5074 Break); 5075 verifyFormat("string s = someFunction(\n" 5076 " \"abc\"\n" 5077 " \"abc\");", 5078 Break); 5079 5080 // As we break before unary operators, breaking right after them is bad. 5081 verifyFormat("string foo = abc ? \"x\"\n" 5082 " \"blah blah blah blah blah blah\"\n" 5083 " : \"y\";", 5084 Break); 5085 5086 // Don't break if there is no column gain. 5087 verifyFormat("f(\"aaaa\"\n" 5088 " \"bbbb\");", 5089 Break); 5090 5091 // Treat literals with escaped newlines like multi-line string literals. 5092 EXPECT_EQ("x = \"a\\\n" 5093 "b\\\n" 5094 "c\";", 5095 format("x = \"a\\\n" 5096 "b\\\n" 5097 "c\";", 5098 NoBreak)); 5099 EXPECT_EQ("xxxx =\n" 5100 " \"a\\\n" 5101 "b\\\n" 5102 "c\";", 5103 format("xxxx = \"a\\\n" 5104 "b\\\n" 5105 "c\";", 5106 Break)); 5107 5108 // Exempt ObjC strings for now. 5109 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5110 " @\"bbbb\";", 5111 format("NSString *const kString = @\"aaaa\"\n" 5112 "@\"bbbb\";", 5113 Break)); 5114 5115 Break.ColumnLimit = 0; 5116 verifyFormat("const char *hello = \"hello llvm\";", Break); 5117 } 5118 5119 TEST_F(FormatTest, AlignsPipes) { 5120 verifyFormat( 5121 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5122 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5123 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5124 verifyFormat( 5125 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5126 " << aaaaaaaaaaaaaaaaaaaa;"); 5127 verifyFormat( 5128 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5129 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5130 verifyFormat( 5131 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5132 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5133 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5134 verifyFormat( 5135 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5136 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5137 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5138 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5141 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5142 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5143 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5144 verifyFormat( 5145 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5147 5148 verifyFormat("return out << \"somepacket = {\\n\"\n" 5149 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5150 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5151 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5152 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5153 " << \"}\";"); 5154 5155 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5156 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5157 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5158 verifyFormat( 5159 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5160 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5161 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5162 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5163 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5164 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5165 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5166 verifyFormat( 5167 "void f() {\n" 5168 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5169 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5170 "}"); 5171 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5172 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5173 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5175 " aaaaaaaaaaaaaaaaaaaaa)\n" 5176 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5177 verifyFormat("LOG_IF(aaa == //\n" 5178 " bbb)\n" 5179 " << a << b;"); 5180 5181 // Breaking before the first "<<" is generally not desirable. 5182 verifyFormat( 5183 "llvm::errs()\n" 5184 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5185 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5186 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5187 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5188 getLLVMStyleWithColumns(70)); 5189 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5190 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5191 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5192 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5193 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5194 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5195 getLLVMStyleWithColumns(70)); 5196 5197 // But sometimes, breaking before the first "<<" is desirable. 5198 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5199 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5200 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5201 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5202 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5203 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5204 " << BEF << IsTemplate << Description << E->getType();"); 5205 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5206 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5208 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5209 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5211 " << aaa;"); 5212 5213 verifyFormat( 5214 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5215 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5216 5217 // Incomplete string literal. 5218 EXPECT_EQ("llvm::errs() << \"\n" 5219 " << a;", 5220 format("llvm::errs() << \"\n<<a;")); 5221 5222 verifyFormat("void f() {\n" 5223 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5224 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5225 "}"); 5226 5227 // Handle 'endl'. 5228 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5229 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5230 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5231 5232 // Handle '\n'. 5233 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5234 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5235 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5236 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5237 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5238 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5239 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5240 } 5241 5242 TEST_F(FormatTest, UnderstandsEquals) { 5243 verifyFormat( 5244 "aaaaaaaaaaaaaaaaa =\n" 5245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5246 verifyFormat( 5247 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5249 verifyFormat( 5250 "if (a) {\n" 5251 " f();\n" 5252 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5254 "}"); 5255 5256 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5257 " 100000000 + 10000000) {\n}"); 5258 } 5259 5260 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5261 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5262 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5263 5264 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5265 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5266 5267 verifyFormat( 5268 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5269 " Parameter2);"); 5270 5271 verifyFormat( 5272 "ShortObject->shortFunction(\n" 5273 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5274 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5275 5276 verifyFormat("loooooooooooooongFunction(\n" 5277 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5278 5279 verifyFormat( 5280 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5281 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5282 5283 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5284 " .WillRepeatedly(Return(SomeValue));"); 5285 verifyFormat("void f() {\n" 5286 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5287 " .Times(2)\n" 5288 " .WillRepeatedly(Return(SomeValue));\n" 5289 "}"); 5290 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5291 " ccccccccccccccccccccccc);"); 5292 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5294 " .aaaaa(aaaaa),\n" 5295 " aaaaaaaaaaaaaaaaaaaaa);"); 5296 verifyFormat("void f() {\n" 5297 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5298 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5299 "}"); 5300 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5301 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5302 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5303 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5304 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5305 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5307 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5309 "}"); 5310 5311 // Here, it is not necessary to wrap at "." or "->". 5312 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5313 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5314 verifyFormat( 5315 "aaaaaaaaaaa->aaaaaaaaa(\n" 5316 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5317 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5318 5319 verifyFormat( 5320 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5322 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5323 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5324 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5325 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5326 5327 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5329 " .a();"); 5330 5331 FormatStyle NoBinPacking = getLLVMStyle(); 5332 NoBinPacking.BinPackParameters = false; 5333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5334 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5335 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5336 " aaaaaaaaaaaaaaaaaaa,\n" 5337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5338 NoBinPacking); 5339 5340 // If there is a subsequent call, change to hanging indentation. 5341 verifyFormat( 5342 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5343 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5344 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5345 verifyFormat( 5346 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5347 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5348 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5350 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5351 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5353 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5354 } 5355 5356 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5357 verifyFormat("template <typename T>\n" 5358 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5359 verifyFormat("template <typename T>\n" 5360 "// T should be one of {A, B}.\n" 5361 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5362 verifyFormat( 5363 "template <typename T>\n" 5364 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5365 verifyFormat("template <typename T>\n" 5366 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5367 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5368 verifyFormat( 5369 "template <typename T>\n" 5370 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5371 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5372 verifyFormat( 5373 "template <typename T>\n" 5374 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5375 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5377 verifyFormat("template <typename T>\n" 5378 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5379 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5380 verifyFormat( 5381 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5382 " typename T4 = char>\n" 5383 "void f();"); 5384 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5385 " template <typename> class cccccccccccccccccccccc,\n" 5386 " typename ddddddddddddd>\n" 5387 "class C {};"); 5388 verifyFormat( 5389 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5391 5392 verifyFormat("void f() {\n" 5393 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5394 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5395 "}"); 5396 5397 verifyFormat("template <typename T> class C {};"); 5398 verifyFormat("template <typename T> void f();"); 5399 verifyFormat("template <typename T> void f() {}"); 5400 verifyFormat( 5401 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5404 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5407 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5408 getLLVMStyleWithColumns(72)); 5409 EXPECT_EQ("static_cast<A< //\n" 5410 " B> *>(\n" 5411 "\n" 5412 " );", 5413 format("static_cast<A<//\n" 5414 " B>*>(\n" 5415 "\n" 5416 " );")); 5417 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5418 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5419 5420 FormatStyle AlwaysBreak = getLLVMStyle(); 5421 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5422 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5423 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5424 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5425 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5426 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5427 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5428 verifyFormat("template <template <typename> class Fooooooo,\n" 5429 " template <typename> class Baaaaaaar>\n" 5430 "struct C {};", 5431 AlwaysBreak); 5432 verifyFormat("template <typename T> // T can be A, B or C.\n" 5433 "struct C {};", 5434 AlwaysBreak); 5435 verifyFormat("template <enum E> class A {\n" 5436 "public:\n" 5437 " E *f();\n" 5438 "};"); 5439 } 5440 5441 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5442 verifyFormat( 5443 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5445 verifyFormat( 5446 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5449 5450 // FIXME: Should we have the extra indent after the second break? 5451 verifyFormat( 5452 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5455 5456 verifyFormat( 5457 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5458 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5459 5460 // Breaking at nested name specifiers is generally not desirable. 5461 verifyFormat( 5462 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5463 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5464 5465 verifyFormat( 5466 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5467 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5468 " aaaaaaaaaaaaaaaaaaaaa);", 5469 getLLVMStyleWithColumns(74)); 5470 5471 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5473 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5474 } 5475 5476 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5477 verifyFormat("A<int> a;"); 5478 verifyFormat("A<A<A<int>>> a;"); 5479 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5480 verifyFormat("bool x = a < 1 || 2 > a;"); 5481 verifyFormat("bool x = 5 < f<int>();"); 5482 verifyFormat("bool x = f<int>() > 5;"); 5483 verifyFormat("bool x = 5 < a<int>::x;"); 5484 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5485 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5486 5487 verifyGoogleFormat("A<A<int>> a;"); 5488 verifyGoogleFormat("A<A<A<int>>> a;"); 5489 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5490 verifyGoogleFormat("A<A<int> > a;"); 5491 verifyGoogleFormat("A<A<A<int> > > a;"); 5492 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5493 verifyGoogleFormat("A<::A<int>> a;"); 5494 verifyGoogleFormat("A<::A> a;"); 5495 verifyGoogleFormat("A< ::A> a;"); 5496 verifyGoogleFormat("A< ::A<int> > a;"); 5497 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5498 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5499 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5500 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5501 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5502 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5503 5504 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5505 5506 verifyFormat("test >> a >> b;"); 5507 verifyFormat("test << a >> b;"); 5508 5509 verifyFormat("f<int>();"); 5510 verifyFormat("template <typename T> void f() {}"); 5511 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5512 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5513 "sizeof(char)>::type>;"); 5514 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5515 verifyFormat("f(a.operator()<A>());"); 5516 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5517 " .template operator()<A>());", 5518 getLLVMStyleWithColumns(35)); 5519 5520 // Not template parameters. 5521 verifyFormat("return a < b && c > d;"); 5522 verifyFormat("void f() {\n" 5523 " while (a < b && c > d) {\n" 5524 " }\n" 5525 "}"); 5526 verifyFormat("template <typename... Types>\n" 5527 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5528 5529 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5531 getLLVMStyleWithColumns(60)); 5532 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5533 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5534 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5535 } 5536 5537 TEST_F(FormatTest, BitshiftOperatorWidth) { 5538 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5539 " bar */", 5540 format("int a=1<<2; /* foo\n" 5541 " bar */")); 5542 5543 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5544 " bar */", 5545 format("int b =256>>1 ; /* foo\n" 5546 " bar */")); 5547 } 5548 5549 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5550 verifyFormat("COMPARE(a, ==, b);"); 5551 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5552 } 5553 5554 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5555 verifyFormat("int A::*x;"); 5556 verifyFormat("int (S::*func)(void *);"); 5557 verifyFormat("void f() { int (S::*func)(void *); }"); 5558 verifyFormat("typedef bool *(Class::*Member)() const;"); 5559 verifyFormat("void f() {\n" 5560 " (a->*f)();\n" 5561 " a->*x;\n" 5562 " (a.*f)();\n" 5563 " ((*a).*f)();\n" 5564 " a.*x;\n" 5565 "}"); 5566 verifyFormat("void f() {\n" 5567 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5568 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5569 "}"); 5570 verifyFormat( 5571 "(aaaaaaaaaa->*bbbbbbb)(\n" 5572 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5573 FormatStyle Style = getLLVMStyle(); 5574 Style.PointerAlignment = FormatStyle::PAS_Left; 5575 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5576 } 5577 5578 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5579 verifyFormat("int a = -2;"); 5580 verifyFormat("f(-1, -2, -3);"); 5581 verifyFormat("a[-1] = 5;"); 5582 verifyFormat("int a = 5 + -2;"); 5583 verifyFormat("if (i == -1) {\n}"); 5584 verifyFormat("if (i != -1) {\n}"); 5585 verifyFormat("if (i > -1) {\n}"); 5586 verifyFormat("if (i < -1) {\n}"); 5587 verifyFormat("++(a->f());"); 5588 verifyFormat("--(a->f());"); 5589 verifyFormat("(a->f())++;"); 5590 verifyFormat("a[42]++;"); 5591 verifyFormat("if (!(a->f())) {\n}"); 5592 5593 verifyFormat("a-- > b;"); 5594 verifyFormat("b ? -a : c;"); 5595 verifyFormat("n * sizeof char16;"); 5596 verifyFormat("n * alignof char16;", getGoogleStyle()); 5597 verifyFormat("sizeof(char);"); 5598 verifyFormat("alignof(char);", getGoogleStyle()); 5599 5600 verifyFormat("return -1;"); 5601 verifyFormat("switch (a) {\n" 5602 "case -1:\n" 5603 " break;\n" 5604 "}"); 5605 verifyFormat("#define X -1"); 5606 verifyFormat("#define X -kConstant"); 5607 5608 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5609 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5610 5611 verifyFormat("int a = /* confusing comment */ -1;"); 5612 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5613 verifyFormat("int a = i /* confusing comment */++;"); 5614 } 5615 5616 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5617 verifyFormat("if (!aaaaaaaaaa( // break\n" 5618 " aaaaa)) {\n" 5619 "}"); 5620 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5621 " aaaaa));"); 5622 verifyFormat("*aaa = aaaaaaa( // break\n" 5623 " bbbbbb);"); 5624 } 5625 5626 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5627 verifyFormat("bool operator<();"); 5628 verifyFormat("bool operator>();"); 5629 verifyFormat("bool operator=();"); 5630 verifyFormat("bool operator==();"); 5631 verifyFormat("bool operator!=();"); 5632 verifyFormat("int operator+();"); 5633 verifyFormat("int operator++();"); 5634 verifyFormat("bool operator,();"); 5635 verifyFormat("bool operator();"); 5636 verifyFormat("bool operator()();"); 5637 verifyFormat("bool operator[]();"); 5638 verifyFormat("operator bool();"); 5639 verifyFormat("operator int();"); 5640 verifyFormat("operator void *();"); 5641 verifyFormat("operator SomeType<int>();"); 5642 verifyFormat("operator SomeType<int, int>();"); 5643 verifyFormat("operator SomeType<SomeType<int>>();"); 5644 verifyFormat("void *operator new(std::size_t size);"); 5645 verifyFormat("void *operator new[](std::size_t size);"); 5646 verifyFormat("void operator delete(void *ptr);"); 5647 verifyFormat("void operator delete[](void *ptr);"); 5648 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5649 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5650 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5651 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5652 5653 verifyFormat( 5654 "ostream &operator<<(ostream &OutputStream,\n" 5655 " SomeReallyLongType WithSomeReallyLongValue);"); 5656 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5657 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5658 " return left.group < right.group;\n" 5659 "}"); 5660 verifyFormat("SomeType &operator=(const SomeType &S);"); 5661 verifyFormat("f.template operator()<int>();"); 5662 5663 verifyGoogleFormat("operator void*();"); 5664 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5665 verifyGoogleFormat("operator ::A();"); 5666 5667 verifyFormat("using A::operator+;"); 5668 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5669 "int i;"); 5670 } 5671 5672 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5673 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5674 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5675 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5676 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5677 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5678 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5679 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5680 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5681 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5682 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5683 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5684 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5685 verifyFormat("template <typename T>\n" 5686 "void F(T) && = delete;", 5687 getGoogleStyle()); 5688 5689 FormatStyle AlignLeft = getLLVMStyle(); 5690 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5691 verifyFormat("void A::b() && {}", AlignLeft); 5692 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5693 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5694 AlignLeft); 5695 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5696 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5697 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5698 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5699 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5700 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5701 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5702 5703 FormatStyle Spaces = getLLVMStyle(); 5704 Spaces.SpacesInCStyleCastParentheses = true; 5705 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5706 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5707 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5708 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5709 5710 Spaces.SpacesInCStyleCastParentheses = false; 5711 Spaces.SpacesInParentheses = true; 5712 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5713 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5714 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5715 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5716 } 5717 5718 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5719 verifyFormat("void f() {\n" 5720 " A *a = new A;\n" 5721 " A *a = new (placement) A;\n" 5722 " delete a;\n" 5723 " delete (A *)a;\n" 5724 "}"); 5725 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5726 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5727 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5728 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5729 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5730 verifyFormat("delete[] h->p;"); 5731 } 5732 5733 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5734 verifyFormat("int *f(int *a) {}"); 5735 verifyFormat("int main(int argc, char **argv) {}"); 5736 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5737 verifyIndependentOfContext("f(a, *a);"); 5738 verifyFormat("void g() { f(*a); }"); 5739 verifyIndependentOfContext("int a = b * 10;"); 5740 verifyIndependentOfContext("int a = 10 * b;"); 5741 verifyIndependentOfContext("int a = b * c;"); 5742 verifyIndependentOfContext("int a += b * c;"); 5743 verifyIndependentOfContext("int a -= b * c;"); 5744 verifyIndependentOfContext("int a *= b * c;"); 5745 verifyIndependentOfContext("int a /= b * c;"); 5746 verifyIndependentOfContext("int a = *b;"); 5747 verifyIndependentOfContext("int a = *b * c;"); 5748 verifyIndependentOfContext("int a = b * *c;"); 5749 verifyIndependentOfContext("int a = b * (10);"); 5750 verifyIndependentOfContext("S << b * (10);"); 5751 verifyIndependentOfContext("return 10 * b;"); 5752 verifyIndependentOfContext("return *b * *c;"); 5753 verifyIndependentOfContext("return a & ~b;"); 5754 verifyIndependentOfContext("f(b ? *c : *d);"); 5755 verifyIndependentOfContext("int a = b ? *c : *d;"); 5756 verifyIndependentOfContext("*b = a;"); 5757 verifyIndependentOfContext("a * ~b;"); 5758 verifyIndependentOfContext("a * !b;"); 5759 verifyIndependentOfContext("a * +b;"); 5760 verifyIndependentOfContext("a * -b;"); 5761 verifyIndependentOfContext("a * ++b;"); 5762 verifyIndependentOfContext("a * --b;"); 5763 verifyIndependentOfContext("a[4] * b;"); 5764 verifyIndependentOfContext("a[a * a] = 1;"); 5765 verifyIndependentOfContext("f() * b;"); 5766 verifyIndependentOfContext("a * [self dostuff];"); 5767 verifyIndependentOfContext("int x = a * (a + b);"); 5768 verifyIndependentOfContext("(a *)(a + b);"); 5769 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5770 verifyIndependentOfContext("int *pa = (int *)&a;"); 5771 verifyIndependentOfContext("return sizeof(int **);"); 5772 verifyIndependentOfContext("return sizeof(int ******);"); 5773 verifyIndependentOfContext("return (int **&)a;"); 5774 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5775 verifyFormat("void f(Type (*parameter)[10]) {}"); 5776 verifyFormat("void f(Type (¶meter)[10]) {}"); 5777 verifyGoogleFormat("return sizeof(int**);"); 5778 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5779 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5780 verifyFormat("auto a = [](int **&, int ***) {};"); 5781 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5782 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5783 verifyFormat("[](const decltype(*a) &value) {}"); 5784 verifyFormat("decltype(a * b) F();"); 5785 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5786 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5787 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5788 verifyIndependentOfContext("int i{a * b};"); 5789 verifyIndependentOfContext("aaa && aaa->f();"); 5790 verifyIndependentOfContext("int x = ~*p;"); 5791 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5792 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5793 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5794 verifyFormat("void f() { f(a, c * d); }"); 5795 verifyFormat("void f() { f(new a(), c * d); }"); 5796 5797 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5798 5799 verifyIndependentOfContext("A<int *> a;"); 5800 verifyIndependentOfContext("A<int **> a;"); 5801 verifyIndependentOfContext("A<int *, int *> a;"); 5802 verifyIndependentOfContext("A<int *[]> a;"); 5803 verifyIndependentOfContext( 5804 "const char *const p = reinterpret_cast<const char *const>(q);"); 5805 verifyIndependentOfContext("A<int **, int **> a;"); 5806 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5807 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5808 verifyFormat("for (; a && b;) {\n}"); 5809 verifyFormat("bool foo = true && [] { return false; }();"); 5810 5811 verifyFormat( 5812 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5814 5815 verifyGoogleFormat("int const* a = &b;"); 5816 verifyGoogleFormat("**outparam = 1;"); 5817 verifyGoogleFormat("*outparam = a * b;"); 5818 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5819 verifyGoogleFormat("A<int*> a;"); 5820 verifyGoogleFormat("A<int**> a;"); 5821 verifyGoogleFormat("A<int*, int*> a;"); 5822 verifyGoogleFormat("A<int**, int**> a;"); 5823 verifyGoogleFormat("f(b ? *c : *d);"); 5824 verifyGoogleFormat("int a = b ? *c : *d;"); 5825 verifyGoogleFormat("Type* t = **x;"); 5826 verifyGoogleFormat("Type* t = *++*x;"); 5827 verifyGoogleFormat("*++*x;"); 5828 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5829 verifyGoogleFormat("Type* t = x++ * y;"); 5830 verifyGoogleFormat( 5831 "const char* const p = reinterpret_cast<const char* const>(q);"); 5832 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5833 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5834 verifyGoogleFormat("template <typename T>\n" 5835 "void f(int i = 0, SomeType** temps = NULL);"); 5836 5837 FormatStyle Left = getLLVMStyle(); 5838 Left.PointerAlignment = FormatStyle::PAS_Left; 5839 verifyFormat("x = *a(x) = *a(y);", Left); 5840 verifyFormat("for (;; *a = b) {\n}", Left); 5841 verifyFormat("return *this += 1;", Left); 5842 5843 verifyIndependentOfContext("a = *(x + y);"); 5844 verifyIndependentOfContext("a = &(x + y);"); 5845 verifyIndependentOfContext("*(x + y).call();"); 5846 verifyIndependentOfContext("&(x + y)->call();"); 5847 verifyFormat("void f() { &(*I).first; }"); 5848 5849 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5850 verifyFormat( 5851 "int *MyValues = {\n" 5852 " *A, // Operator detection might be confused by the '{'\n" 5853 " *BB // Operator detection might be confused by previous comment\n" 5854 "};"); 5855 5856 verifyIndependentOfContext("if (int *a = &b)"); 5857 verifyIndependentOfContext("if (int &a = *b)"); 5858 verifyIndependentOfContext("if (a & b[i])"); 5859 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5860 verifyIndependentOfContext("if (*b[i])"); 5861 verifyIndependentOfContext("if (int *a = (&b))"); 5862 verifyIndependentOfContext("while (int *a = &b)"); 5863 verifyIndependentOfContext("size = sizeof *a;"); 5864 verifyIndependentOfContext("if (a && (b = c))"); 5865 verifyFormat("void f() {\n" 5866 " for (const int &v : Values) {\n" 5867 " }\n" 5868 "}"); 5869 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5870 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5871 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5872 5873 verifyFormat("#define A (!a * b)"); 5874 verifyFormat("#define MACRO \\\n" 5875 " int *i = a * b; \\\n" 5876 " void f(a *b);", 5877 getLLVMStyleWithColumns(19)); 5878 5879 verifyIndependentOfContext("A = new SomeType *[Length];"); 5880 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5881 verifyIndependentOfContext("T **t = new T *;"); 5882 verifyIndependentOfContext("T **t = new T *();"); 5883 verifyGoogleFormat("A = new SomeType*[Length]();"); 5884 verifyGoogleFormat("A = new SomeType*[Length];"); 5885 verifyGoogleFormat("T** t = new T*;"); 5886 verifyGoogleFormat("T** t = new T*();"); 5887 5888 FormatStyle PointerLeft = getLLVMStyle(); 5889 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5890 verifyFormat("delete *x;", PointerLeft); 5891 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5892 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5893 verifyFormat("template <bool a, bool b> " 5894 "typename t::if<x && y>::type f() {}"); 5895 verifyFormat("template <int *y> f() {}"); 5896 verifyFormat("vector<int *> v;"); 5897 verifyFormat("vector<int *const> v;"); 5898 verifyFormat("vector<int *const **const *> v;"); 5899 verifyFormat("vector<int *volatile> v;"); 5900 verifyFormat("vector<a * b> v;"); 5901 verifyFormat("foo<b && false>();"); 5902 verifyFormat("foo<b & 1>();"); 5903 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5904 verifyFormat( 5905 "template <class T,\n" 5906 " class = typename std::enable_if<\n" 5907 " std::is_integral<T>::value &&\n" 5908 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5909 "void F();", 5910 getLLVMStyleWithColumns(70)); 5911 verifyFormat( 5912 "template <class T,\n" 5913 " class = typename ::std::enable_if<\n" 5914 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5915 "void F();", 5916 getGoogleStyleWithColumns(68)); 5917 5918 verifyIndependentOfContext("MACRO(int *i);"); 5919 verifyIndependentOfContext("MACRO(auto *a);"); 5920 verifyIndependentOfContext("MACRO(const A *a);"); 5921 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5922 verifyFormat("void f() { f(float{1}, a * a); }"); 5923 // FIXME: Is there a way to make this work? 5924 // verifyIndependentOfContext("MACRO(A *a);"); 5925 5926 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5927 verifyFormat("return options != nullptr && operator==(*options);"); 5928 5929 EXPECT_EQ("#define OP(x) \\\n" 5930 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5931 " return s << a.DebugString(); \\\n" 5932 " }", 5933 format("#define OP(x) \\\n" 5934 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5935 " return s << a.DebugString(); \\\n" 5936 " }", 5937 getLLVMStyleWithColumns(50))); 5938 5939 // FIXME: We cannot handle this case yet; we might be able to figure out that 5940 // foo<x> d > v; doesn't make sense. 5941 verifyFormat("foo<a<b && c> d> v;"); 5942 5943 FormatStyle PointerMiddle = getLLVMStyle(); 5944 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5945 verifyFormat("delete *x;", PointerMiddle); 5946 verifyFormat("int * x;", PointerMiddle); 5947 verifyFormat("template <int * y> f() {}", PointerMiddle); 5948 verifyFormat("int * f(int * a) {}", PointerMiddle); 5949 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5950 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5951 verifyFormat("A<int *> a;", PointerMiddle); 5952 verifyFormat("A<int **> a;", PointerMiddle); 5953 verifyFormat("A<int *, int *> a;", PointerMiddle); 5954 verifyFormat("A<int * []> a;", PointerMiddle); 5955 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5956 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5957 verifyFormat("T ** t = new T *;", PointerMiddle); 5958 5959 // Member function reference qualifiers aren't binary operators. 5960 verifyFormat("string // break\n" 5961 "operator()() & {}"); 5962 verifyFormat("string // break\n" 5963 "operator()() && {}"); 5964 verifyGoogleFormat("template <typename T>\n" 5965 "auto x() & -> int {}"); 5966 } 5967 5968 TEST_F(FormatTest, UnderstandsAttributes) { 5969 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5970 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5971 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5972 FormatStyle AfterType = getLLVMStyle(); 5973 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5974 verifyFormat("__attribute__((nodebug)) void\n" 5975 "foo() {}\n", 5976 AfterType); 5977 } 5978 5979 TEST_F(FormatTest, UnderstandsEllipsis) { 5980 verifyFormat("int printf(const char *fmt, ...);"); 5981 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5982 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5983 5984 FormatStyle PointersLeft = getLLVMStyle(); 5985 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5986 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5987 } 5988 5989 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5990 EXPECT_EQ("int *a;\n" 5991 "int *a;\n" 5992 "int *a;", 5993 format("int *a;\n" 5994 "int* a;\n" 5995 "int *a;", 5996 getGoogleStyle())); 5997 EXPECT_EQ("int* a;\n" 5998 "int* a;\n" 5999 "int* a;", 6000 format("int* a;\n" 6001 "int* a;\n" 6002 "int *a;", 6003 getGoogleStyle())); 6004 EXPECT_EQ("int *a;\n" 6005 "int *a;\n" 6006 "int *a;", 6007 format("int *a;\n" 6008 "int * a;\n" 6009 "int * a;", 6010 getGoogleStyle())); 6011 EXPECT_EQ("auto x = [] {\n" 6012 " int *a;\n" 6013 " int *a;\n" 6014 " int *a;\n" 6015 "};", 6016 format("auto x=[]{int *a;\n" 6017 "int * a;\n" 6018 "int * a;};", 6019 getGoogleStyle())); 6020 } 6021 6022 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6023 verifyFormat("int f(int &&a) {}"); 6024 verifyFormat("int f(int a, char &&b) {}"); 6025 verifyFormat("void f() { int &&a = b; }"); 6026 verifyGoogleFormat("int f(int a, char&& b) {}"); 6027 verifyGoogleFormat("void f() { int&& a = b; }"); 6028 6029 verifyIndependentOfContext("A<int &&> a;"); 6030 verifyIndependentOfContext("A<int &&, int &&> a;"); 6031 verifyGoogleFormat("A<int&&> a;"); 6032 verifyGoogleFormat("A<int&&, int&&> a;"); 6033 6034 // Not rvalue references: 6035 verifyFormat("template <bool B, bool C> class A {\n" 6036 " static_assert(B && C, \"Something is wrong\");\n" 6037 "};"); 6038 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6039 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6040 verifyFormat("#define A(a, b) (a && b)"); 6041 } 6042 6043 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6044 verifyFormat("void f() {\n" 6045 " x[aaaaaaaaa -\n" 6046 " b] = 23;\n" 6047 "}", 6048 getLLVMStyleWithColumns(15)); 6049 } 6050 6051 TEST_F(FormatTest, FormatsCasts) { 6052 verifyFormat("Type *A = static_cast<Type *>(P);"); 6053 verifyFormat("Type *A = (Type *)P;"); 6054 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6055 verifyFormat("int a = (int)(2.0f);"); 6056 verifyFormat("int a = (int)2.0f;"); 6057 verifyFormat("x[(int32)y];"); 6058 verifyFormat("x = (int32)y;"); 6059 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6060 verifyFormat("int a = (int)*b;"); 6061 verifyFormat("int a = (int)2.0f;"); 6062 verifyFormat("int a = (int)~0;"); 6063 verifyFormat("int a = (int)++a;"); 6064 verifyFormat("int a = (int)sizeof(int);"); 6065 verifyFormat("int a = (int)+2;"); 6066 verifyFormat("my_int a = (my_int)2.0f;"); 6067 verifyFormat("my_int a = (my_int)sizeof(int);"); 6068 verifyFormat("return (my_int)aaa;"); 6069 verifyFormat("#define x ((int)-1)"); 6070 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6071 verifyFormat("#define p(q) ((int *)&q)"); 6072 verifyFormat("fn(a)(b) + 1;"); 6073 6074 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6075 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6076 verifyFormat("my_int a = (my_int)~0;"); 6077 verifyFormat("my_int a = (my_int)++a;"); 6078 verifyFormat("my_int a = (my_int)-2;"); 6079 verifyFormat("my_int a = (my_int)1;"); 6080 verifyFormat("my_int a = (my_int *)1;"); 6081 verifyFormat("my_int a = (const my_int)-1;"); 6082 verifyFormat("my_int a = (const my_int *)-1;"); 6083 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6084 verifyFormat("my_int a = (ns::my_int)-2;"); 6085 verifyFormat("case (my_int)ONE:"); 6086 verifyFormat("auto x = (X)this;"); 6087 6088 // FIXME: single value wrapped with paren will be treated as cast. 6089 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6090 6091 verifyFormat("{ (void)F; }"); 6092 6093 // Don't break after a cast's 6094 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6095 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6096 " bbbbbbbbbbbbbbbbbbbbbb);"); 6097 6098 // These are not casts. 6099 verifyFormat("void f(int *) {}"); 6100 verifyFormat("f(foo)->b;"); 6101 verifyFormat("f(foo).b;"); 6102 verifyFormat("f(foo)(b);"); 6103 verifyFormat("f(foo)[b];"); 6104 verifyFormat("[](foo) { return 4; }(bar);"); 6105 verifyFormat("(*funptr)(foo)[4];"); 6106 verifyFormat("funptrs[4](foo)[4];"); 6107 verifyFormat("void f(int *);"); 6108 verifyFormat("void f(int *) = 0;"); 6109 verifyFormat("void f(SmallVector<int>) {}"); 6110 verifyFormat("void f(SmallVector<int>);"); 6111 verifyFormat("void f(SmallVector<int>) = 0;"); 6112 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6113 verifyFormat("int a = sizeof(int) * b;"); 6114 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6115 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6116 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6117 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6118 6119 // These are not casts, but at some point were confused with casts. 6120 verifyFormat("virtual void foo(int *) override;"); 6121 verifyFormat("virtual void foo(char &) const;"); 6122 verifyFormat("virtual void foo(int *a, char *) const;"); 6123 verifyFormat("int a = sizeof(int *) + b;"); 6124 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6125 verifyFormat("bool b = f(g<int>) && c;"); 6126 verifyFormat("typedef void (*f)(int i) func;"); 6127 6128 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6129 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6130 // FIXME: The indentation here is not ideal. 6131 verifyFormat( 6132 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6133 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6134 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6135 } 6136 6137 TEST_F(FormatTest, FormatsFunctionTypes) { 6138 verifyFormat("A<bool()> a;"); 6139 verifyFormat("A<SomeType()> a;"); 6140 verifyFormat("A<void (*)(int, std::string)> a;"); 6141 verifyFormat("A<void *(int)>;"); 6142 verifyFormat("void *(*a)(int *, SomeType *);"); 6143 verifyFormat("int (*func)(void *);"); 6144 verifyFormat("void f() { int (*func)(void *); }"); 6145 verifyFormat("template <class CallbackClass>\n" 6146 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6147 6148 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6149 verifyGoogleFormat("void* (*a)(int);"); 6150 verifyGoogleFormat( 6151 "template <class CallbackClass>\n" 6152 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6153 6154 // Other constructs can look somewhat like function types: 6155 verifyFormat("A<sizeof(*x)> a;"); 6156 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6157 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6158 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6159 verifyFormat("int x = f(&h)();"); 6160 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6161 } 6162 6163 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6164 verifyFormat("A (*foo_)[6];"); 6165 verifyFormat("vector<int> (*foo_)[6];"); 6166 } 6167 6168 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6169 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6170 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6171 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6172 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6173 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6174 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6175 6176 // Different ways of ()-initializiation. 6177 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6178 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6179 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6180 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6181 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6182 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6183 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6184 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6185 } 6186 6187 TEST_F(FormatTest, BreaksLongDeclarations) { 6188 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6189 " AnotherNameForTheLongType;"); 6190 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6192 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6193 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6194 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6195 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6196 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6197 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6198 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6199 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6200 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6201 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6202 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6203 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6204 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6205 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6206 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6207 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6208 FormatStyle Indented = getLLVMStyle(); 6209 Indented.IndentWrappedFunctionNames = true; 6210 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6211 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6212 Indented); 6213 verifyFormat( 6214 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6215 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6216 Indented); 6217 verifyFormat( 6218 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6219 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6220 Indented); 6221 verifyFormat( 6222 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6223 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6224 Indented); 6225 6226 // FIXME: Without the comment, this breaks after "(". 6227 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6228 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6229 getGoogleStyle()); 6230 6231 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6232 " int LoooooooooooooooooooongParam2) {}"); 6233 verifyFormat( 6234 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6235 " SourceLocation L, IdentifierIn *II,\n" 6236 " Type *T) {}"); 6237 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6238 "ReallyReaaallyLongFunctionName(\n" 6239 " const std::string &SomeParameter,\n" 6240 " const SomeType<string, SomeOtherTemplateParameter>\n" 6241 " &ReallyReallyLongParameterName,\n" 6242 " const SomeType<string, SomeOtherTemplateParameter>\n" 6243 " &AnotherLongParameterName) {}"); 6244 verifyFormat("template <typename A>\n" 6245 "SomeLoooooooooooooooooooooongType<\n" 6246 " typename some_namespace::SomeOtherType<A>::Type>\n" 6247 "Function() {}"); 6248 6249 verifyGoogleFormat( 6250 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6251 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6252 verifyGoogleFormat( 6253 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6254 " SourceLocation L) {}"); 6255 verifyGoogleFormat( 6256 "some_namespace::LongReturnType\n" 6257 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6258 " int first_long_parameter, int second_parameter) {}"); 6259 6260 verifyGoogleFormat("template <typename T>\n" 6261 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6262 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6263 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6264 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6265 6266 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6267 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6268 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6269 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6270 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6271 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6272 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6273 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6276 } 6277 6278 TEST_F(FormatTest, FormatsArrays) { 6279 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6280 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6281 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6282 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6283 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6284 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6285 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6286 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6287 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6288 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6289 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6290 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6291 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6292 verifyFormat( 6293 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6294 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6295 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6296 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6297 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6298 6299 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6301 verifyFormat( 6302 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6303 " .aaaaaaa[0]\n" 6304 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6305 verifyFormat("a[::b::c];"); 6306 6307 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6308 6309 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6310 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6311 } 6312 6313 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6314 verifyFormat("(a)->b();"); 6315 verifyFormat("--a;"); 6316 } 6317 6318 TEST_F(FormatTest, HandlesIncludeDirectives) { 6319 verifyFormat("#include <string>\n" 6320 "#include <a/b/c.h>\n" 6321 "#include \"a/b/string\"\n" 6322 "#include \"string.h\"\n" 6323 "#include \"string.h\"\n" 6324 "#include <a-a>\n" 6325 "#include < path with space >\n" 6326 "#include_next <test.h>" 6327 "#include \"abc.h\" // this is included for ABC\n" 6328 "#include \"some long include\" // with a comment\n" 6329 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6330 getLLVMStyleWithColumns(35)); 6331 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6332 EXPECT_EQ("#include <a>", format("#include<a>")); 6333 6334 verifyFormat("#import <string>"); 6335 verifyFormat("#import <a/b/c.h>"); 6336 verifyFormat("#import \"a/b/string\""); 6337 verifyFormat("#import \"string.h\""); 6338 verifyFormat("#import \"string.h\""); 6339 verifyFormat("#if __has_include(<strstream>)\n" 6340 "#include <strstream>\n" 6341 "#endif"); 6342 6343 verifyFormat("#define MY_IMPORT <a/b>"); 6344 6345 // Protocol buffer definition or missing "#". 6346 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6347 getLLVMStyleWithColumns(30)); 6348 6349 FormatStyle Style = getLLVMStyle(); 6350 Style.AlwaysBreakBeforeMultilineStrings = true; 6351 Style.ColumnLimit = 0; 6352 verifyFormat("#import \"abc.h\"", Style); 6353 6354 // But 'import' might also be a regular C++ namespace. 6355 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6357 } 6358 6359 //===----------------------------------------------------------------------===// 6360 // Error recovery tests. 6361 //===----------------------------------------------------------------------===// 6362 6363 TEST_F(FormatTest, IncompleteParameterLists) { 6364 FormatStyle NoBinPacking = getLLVMStyle(); 6365 NoBinPacking.BinPackParameters = false; 6366 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6367 " double *min_x,\n" 6368 " double *max_x,\n" 6369 " double *min_y,\n" 6370 " double *max_y,\n" 6371 " double *min_z,\n" 6372 " double *max_z, ) {}", 6373 NoBinPacking); 6374 } 6375 6376 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6377 verifyFormat("void f() { return; }\n42"); 6378 verifyFormat("void f() {\n" 6379 " if (0)\n" 6380 " return;\n" 6381 "}\n" 6382 "42"); 6383 verifyFormat("void f() { return }\n42"); 6384 verifyFormat("void f() {\n" 6385 " if (0)\n" 6386 " return\n" 6387 "}\n" 6388 "42"); 6389 } 6390 6391 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6392 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6393 EXPECT_EQ("void f() {\n" 6394 " if (a)\n" 6395 " return\n" 6396 "}", 6397 format("void f ( ) { if ( a ) return }")); 6398 EXPECT_EQ("namespace N {\n" 6399 "void f()\n" 6400 "}", 6401 format("namespace N { void f() }")); 6402 EXPECT_EQ("namespace N {\n" 6403 "void f() {}\n" 6404 "void g()\n" 6405 "}", 6406 format("namespace N { void f( ) { } void g( ) }")); 6407 } 6408 6409 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6410 verifyFormat("int aaaaaaaa =\n" 6411 " // Overlylongcomment\n" 6412 " b;", 6413 getLLVMStyleWithColumns(20)); 6414 verifyFormat("function(\n" 6415 " ShortArgument,\n" 6416 " LoooooooooooongArgument);\n", 6417 getLLVMStyleWithColumns(20)); 6418 } 6419 6420 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6421 verifyFormat("public:"); 6422 verifyFormat("class A {\n" 6423 "public\n" 6424 " void f() {}\n" 6425 "};"); 6426 verifyFormat("public\n" 6427 "int qwerty;"); 6428 verifyFormat("public\n" 6429 "B {}"); 6430 verifyFormat("public\n" 6431 "{}"); 6432 verifyFormat("public\n" 6433 "B { int x; }"); 6434 } 6435 6436 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6437 verifyFormat("{"); 6438 verifyFormat("#})"); 6439 verifyNoCrash("(/**/[:!] ?[)."); 6440 } 6441 6442 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6443 verifyFormat("do {\n}"); 6444 verifyFormat("do {\n}\n" 6445 "f();"); 6446 verifyFormat("do {\n}\n" 6447 "wheeee(fun);"); 6448 verifyFormat("do {\n" 6449 " f();\n" 6450 "}"); 6451 } 6452 6453 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6454 verifyFormat("if {\n foo;\n foo();\n}"); 6455 verifyFormat("switch {\n foo;\n foo();\n}"); 6456 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6457 verifyFormat("while {\n foo;\n foo();\n}"); 6458 verifyFormat("do {\n foo;\n foo();\n} while;"); 6459 } 6460 6461 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6462 verifyIncompleteFormat("namespace {\n" 6463 "class Foo { Foo (\n" 6464 "};\n" 6465 "} // comment"); 6466 } 6467 6468 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6469 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6470 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6471 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6472 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6473 6474 EXPECT_EQ("{\n" 6475 " {\n" 6476 " breakme(\n" 6477 " qwe);\n" 6478 " }\n", 6479 format("{\n" 6480 " {\n" 6481 " breakme(qwe);\n" 6482 "}\n", 6483 getLLVMStyleWithColumns(10))); 6484 } 6485 6486 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6487 verifyFormat("int x = {\n" 6488 " avariable,\n" 6489 " b(alongervariable)};", 6490 getLLVMStyleWithColumns(25)); 6491 } 6492 6493 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6494 verifyFormat("return (a)(b){1, 2, 3};"); 6495 } 6496 6497 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6498 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6499 verifyFormat("vector<int> x{\n" 6500 " 1, 2, 3, 4,\n" 6501 "};"); 6502 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6503 verifyFormat("f({1, 2});"); 6504 verifyFormat("auto v = Foo{-1};"); 6505 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6506 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6507 verifyFormat("new vector<int>{1, 2, 3};"); 6508 verifyFormat("new int[3]{1, 2, 3};"); 6509 verifyFormat("new int{1};"); 6510 verifyFormat("return {arg1, arg2};"); 6511 verifyFormat("return {arg1, SomeType{parameter}};"); 6512 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6513 verifyFormat("new T{arg1, arg2};"); 6514 verifyFormat("f(MyMap[{composite, key}]);"); 6515 verifyFormat("class Class {\n" 6516 " T member = {arg1, arg2};\n" 6517 "};"); 6518 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6519 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6520 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6521 6522 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6523 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6524 verifyFormat("auto i = decltype(x){};"); 6525 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6526 verifyFormat("Node n{1, Node{1000}, //\n" 6527 " 2};"); 6528 verifyFormat("Aaaa aaaaaaa{\n" 6529 " {\n" 6530 " aaaa,\n" 6531 " },\n" 6532 "};"); 6533 verifyFormat("class C : public D {\n" 6534 " SomeClass SC{2};\n" 6535 "};"); 6536 verifyFormat("class C : public A {\n" 6537 " class D : public B {\n" 6538 " void f() { int i{2}; }\n" 6539 " };\n" 6540 "};"); 6541 verifyFormat("#define A {a, a},"); 6542 6543 // In combination with BinPackArguments = false. 6544 FormatStyle NoBinPacking = getLLVMStyle(); 6545 NoBinPacking.BinPackArguments = false; 6546 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6547 " bbbbb,\n" 6548 " ccccc,\n" 6549 " ddddd,\n" 6550 " eeeee,\n" 6551 " ffffff,\n" 6552 " ggggg,\n" 6553 " hhhhhh,\n" 6554 " iiiiii,\n" 6555 " jjjjjj,\n" 6556 " kkkkkk};", 6557 NoBinPacking); 6558 verifyFormat("const Aaaaaa aaaaa = {\n" 6559 " aaaaa,\n" 6560 " bbbbb,\n" 6561 " ccccc,\n" 6562 " ddddd,\n" 6563 " eeeee,\n" 6564 " ffffff,\n" 6565 " ggggg,\n" 6566 " hhhhhh,\n" 6567 " iiiiii,\n" 6568 " jjjjjj,\n" 6569 " kkkkkk,\n" 6570 "};", 6571 NoBinPacking); 6572 verifyFormat( 6573 "const Aaaaaa aaaaa = {\n" 6574 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6575 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6576 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6577 "};", 6578 NoBinPacking); 6579 6580 // FIXME: The alignment of these trailing comments might be bad. Then again, 6581 // this might be utterly useless in real code. 6582 verifyFormat("Constructor::Constructor()\n" 6583 " : some_value{ //\n" 6584 " aaaaaaa, //\n" 6585 " bbbbbbb} {}"); 6586 6587 // In braced lists, the first comment is always assumed to belong to the 6588 // first element. Thus, it can be moved to the next or previous line as 6589 // appropriate. 6590 EXPECT_EQ("function({// First element:\n" 6591 " 1,\n" 6592 " // Second element:\n" 6593 " 2});", 6594 format("function({\n" 6595 " // First element:\n" 6596 " 1,\n" 6597 " // Second element:\n" 6598 " 2});")); 6599 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6600 " // First element:\n" 6601 " 1,\n" 6602 " // Second element:\n" 6603 " 2};", 6604 format("std::vector<int> MyNumbers{// First element:\n" 6605 " 1,\n" 6606 " // Second element:\n" 6607 " 2};", 6608 getLLVMStyleWithColumns(30))); 6609 // A trailing comma should still lead to an enforced line break. 6610 EXPECT_EQ("vector<int> SomeVector = {\n" 6611 " // aaa\n" 6612 " 1, 2,\n" 6613 "};", 6614 format("vector<int> SomeVector = { // aaa\n" 6615 " 1, 2, };")); 6616 6617 FormatStyle ExtraSpaces = getLLVMStyle(); 6618 ExtraSpaces.Cpp11BracedListStyle = false; 6619 ExtraSpaces.ColumnLimit = 75; 6620 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6621 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6622 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6623 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6624 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6625 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6626 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6627 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6628 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6629 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6630 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6631 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6632 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6633 verifyFormat("class Class {\n" 6634 " T member = { arg1, arg2 };\n" 6635 "};", 6636 ExtraSpaces); 6637 verifyFormat( 6638 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6639 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6640 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6641 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6642 ExtraSpaces); 6643 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6644 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6645 ExtraSpaces); 6646 verifyFormat( 6647 "someFunction(OtherParam,\n" 6648 " BracedList{ // comment 1 (Forcing interesting break)\n" 6649 " param1, param2,\n" 6650 " // comment 2\n" 6651 " param3, param4 });", 6652 ExtraSpaces); 6653 verifyFormat( 6654 "std::this_thread::sleep_for(\n" 6655 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6656 ExtraSpaces); 6657 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6658 " aaaaaaa,\n" 6659 " aaaaaaaaaa,\n" 6660 " aaaaa,\n" 6661 " aaaaaaaaaaaaaaa,\n" 6662 " aaa,\n" 6663 " aaaaaaaaaa,\n" 6664 " a,\n" 6665 " aaaaaaaaaaaaaaaaaaaaa,\n" 6666 " aaaaaaaaaaaa,\n" 6667 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6668 " aaaaaaa,\n" 6669 " a};"); 6670 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6671 } 6672 6673 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6674 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6675 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6676 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6677 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6678 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6679 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6680 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6681 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6682 " 1, 22, 333, 4444, 55555, //\n" 6683 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6684 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6685 verifyFormat( 6686 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6687 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6688 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6689 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6690 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6691 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6692 " 7777777};"); 6693 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6694 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6695 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6696 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6697 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6698 " // Separating comment.\n" 6699 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6700 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6701 " // Leading comment\n" 6702 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6703 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6704 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6705 " 1, 1, 1, 1};", 6706 getLLVMStyleWithColumns(39)); 6707 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6708 " 1, 1, 1, 1};", 6709 getLLVMStyleWithColumns(38)); 6710 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6711 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6712 getLLVMStyleWithColumns(43)); 6713 verifyFormat( 6714 "static unsigned SomeValues[10][3] = {\n" 6715 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6716 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6717 verifyFormat("static auto fields = new vector<string>{\n" 6718 " \"aaaaaaaaaaaaa\",\n" 6719 " \"aaaaaaaaaaaaa\",\n" 6720 " \"aaaaaaaaaaaa\",\n" 6721 " \"aaaaaaaaaaaaaa\",\n" 6722 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6723 " \"aaaaaaaaaaaa\",\n" 6724 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6725 "};"); 6726 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6727 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6728 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6729 " 3, cccccccccccccccccccccc};", 6730 getLLVMStyleWithColumns(60)); 6731 6732 // Trailing commas. 6733 verifyFormat("vector<int> x = {\n" 6734 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6735 "};", 6736 getLLVMStyleWithColumns(39)); 6737 verifyFormat("vector<int> x = {\n" 6738 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6739 "};", 6740 getLLVMStyleWithColumns(39)); 6741 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6742 " 1, 1, 1, 1,\n" 6743 " /**/ /**/};", 6744 getLLVMStyleWithColumns(39)); 6745 6746 // Trailing comment in the first line. 6747 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6748 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6749 " 111111111, 222222222, 3333333333, 444444444, //\n" 6750 " 11111111, 22222222, 333333333, 44444444};"); 6751 // Trailing comment in the last line. 6752 verifyFormat("int aaaaa[] = {\n" 6753 " 1, 2, 3, // comment\n" 6754 " 4, 5, 6 // comment\n" 6755 "};"); 6756 6757 // With nested lists, we should either format one item per line or all nested 6758 // lists one on line. 6759 // FIXME: For some nested lists, we can do better. 6760 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6761 " {aaaaaaaaaaaaaaaaaaa},\n" 6762 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6763 " {aaaaaaaaaaaaaaaaa}};", 6764 getLLVMStyleWithColumns(60)); 6765 verifyFormat( 6766 "SomeStruct my_struct_array = {\n" 6767 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6768 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6769 " {aaa, aaa},\n" 6770 " {aaa, aaa},\n" 6771 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6772 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6773 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6774 6775 // No column layout should be used here. 6776 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6777 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6778 6779 verifyNoCrash("a<,"); 6780 6781 // No braced initializer here. 6782 verifyFormat("void f() {\n" 6783 " struct Dummy {};\n" 6784 " f(v);\n" 6785 "}"); 6786 6787 // Long lists should be formatted in columns even if they are nested. 6788 verifyFormat( 6789 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6790 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6791 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6792 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6793 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6794 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6795 } 6796 6797 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6798 FormatStyle DoNotMerge = getLLVMStyle(); 6799 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6800 6801 verifyFormat("void f() { return 42; }"); 6802 verifyFormat("void f() {\n" 6803 " return 42;\n" 6804 "}", 6805 DoNotMerge); 6806 verifyFormat("void f() {\n" 6807 " // Comment\n" 6808 "}"); 6809 verifyFormat("{\n" 6810 "#error {\n" 6811 " int a;\n" 6812 "}"); 6813 verifyFormat("{\n" 6814 " int a;\n" 6815 "#error {\n" 6816 "}"); 6817 verifyFormat("void f() {} // comment"); 6818 verifyFormat("void f() { int a; } // comment"); 6819 verifyFormat("void f() {\n" 6820 "} // comment", 6821 DoNotMerge); 6822 verifyFormat("void f() {\n" 6823 " int a;\n" 6824 "} // comment", 6825 DoNotMerge); 6826 verifyFormat("void f() {\n" 6827 "} // comment", 6828 getLLVMStyleWithColumns(15)); 6829 6830 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6831 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6832 6833 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6834 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6835 verifyFormat("class C {\n" 6836 " C()\n" 6837 " : iiiiiiii(nullptr),\n" 6838 " kkkkkkk(nullptr),\n" 6839 " mmmmmmm(nullptr),\n" 6840 " nnnnnnn(nullptr) {}\n" 6841 "};", 6842 getGoogleStyle()); 6843 6844 FormatStyle NoColumnLimit = getLLVMStyle(); 6845 NoColumnLimit.ColumnLimit = 0; 6846 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6847 EXPECT_EQ("class C {\n" 6848 " A() : b(0) {}\n" 6849 "};", 6850 format("class C{A():b(0){}};", NoColumnLimit)); 6851 EXPECT_EQ("A()\n" 6852 " : b(0) {\n" 6853 "}", 6854 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6855 6856 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6857 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6858 FormatStyle::SFS_None; 6859 EXPECT_EQ("A()\n" 6860 " : b(0) {\n" 6861 "}", 6862 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6863 EXPECT_EQ("A()\n" 6864 " : b(0) {\n" 6865 "}", 6866 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6867 6868 verifyFormat("#define A \\\n" 6869 " void f() { \\\n" 6870 " int i; \\\n" 6871 " }", 6872 getLLVMStyleWithColumns(20)); 6873 verifyFormat("#define A \\\n" 6874 " void f() { int i; }", 6875 getLLVMStyleWithColumns(21)); 6876 verifyFormat("#define A \\\n" 6877 " void f() { \\\n" 6878 " int i; \\\n" 6879 " } \\\n" 6880 " int j;", 6881 getLLVMStyleWithColumns(22)); 6882 verifyFormat("#define A \\\n" 6883 " void f() { int i; } \\\n" 6884 " int j;", 6885 getLLVMStyleWithColumns(23)); 6886 } 6887 6888 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6889 FormatStyle MergeInlineOnly = getLLVMStyle(); 6890 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6891 verifyFormat("class C {\n" 6892 " int f() { return 42; }\n" 6893 "};", 6894 MergeInlineOnly); 6895 verifyFormat("int f() {\n" 6896 " return 42;\n" 6897 "}", 6898 MergeInlineOnly); 6899 } 6900 6901 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6902 // Elaborate type variable declarations. 6903 verifyFormat("struct foo a = {bar};\nint n;"); 6904 verifyFormat("class foo a = {bar};\nint n;"); 6905 verifyFormat("union foo a = {bar};\nint n;"); 6906 6907 // Elaborate types inside function definitions. 6908 verifyFormat("struct foo f() {}\nint n;"); 6909 verifyFormat("class foo f() {}\nint n;"); 6910 verifyFormat("union foo f() {}\nint n;"); 6911 6912 // Templates. 6913 verifyFormat("template <class X> void f() {}\nint n;"); 6914 verifyFormat("template <struct X> void f() {}\nint n;"); 6915 verifyFormat("template <union X> void f() {}\nint n;"); 6916 6917 // Actual definitions... 6918 verifyFormat("struct {\n} n;"); 6919 verifyFormat( 6920 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6921 verifyFormat("union Z {\n int n;\n} x;"); 6922 verifyFormat("class MACRO Z {\n} n;"); 6923 verifyFormat("class MACRO(X) Z {\n} n;"); 6924 verifyFormat("class __attribute__(X) Z {\n} n;"); 6925 verifyFormat("class __declspec(X) Z {\n} n;"); 6926 verifyFormat("class A##B##C {\n} n;"); 6927 verifyFormat("class alignas(16) Z {\n} n;"); 6928 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6929 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6930 6931 // Redefinition from nested context: 6932 verifyFormat("class A::B::C {\n} n;"); 6933 6934 // Template definitions. 6935 verifyFormat( 6936 "template <typename F>\n" 6937 "Matcher(const Matcher<F> &Other,\n" 6938 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6939 " !is_same<F, T>::value>::type * = 0)\n" 6940 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6941 6942 // FIXME: This is still incorrectly handled at the formatter side. 6943 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6944 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6945 6946 // FIXME: 6947 // This now gets parsed incorrectly as class definition. 6948 // verifyFormat("class A<int> f() {\n}\nint n;"); 6949 6950 // Elaborate types where incorrectly parsing the structural element would 6951 // break the indent. 6952 verifyFormat("if (true)\n" 6953 " class X x;\n" 6954 "else\n" 6955 " f();\n"); 6956 6957 // This is simply incomplete. Formatting is not important, but must not crash. 6958 verifyFormat("class A:"); 6959 } 6960 6961 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6962 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6963 format("#error Leave all white!!!!! space* alone!\n")); 6964 EXPECT_EQ( 6965 "#warning Leave all white!!!!! space* alone!\n", 6966 format("#warning Leave all white!!!!! space* alone!\n")); 6967 EXPECT_EQ("#error 1", format(" # error 1")); 6968 EXPECT_EQ("#warning 1", format(" # warning 1")); 6969 } 6970 6971 TEST_F(FormatTest, FormatHashIfExpressions) { 6972 verifyFormat("#if AAAA && BBBB"); 6973 verifyFormat("#if (AAAA && BBBB)"); 6974 verifyFormat("#elif (AAAA && BBBB)"); 6975 // FIXME: Come up with a better indentation for #elif. 6976 verifyFormat( 6977 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6978 " defined(BBBBBBBB)\n" 6979 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6980 " defined(BBBBBBBB)\n" 6981 "#endif", 6982 getLLVMStyleWithColumns(65)); 6983 } 6984 6985 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6986 FormatStyle AllowsMergedIf = getGoogleStyle(); 6987 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6988 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6989 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6990 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6991 EXPECT_EQ("if (true) return 42;", 6992 format("if (true)\nreturn 42;", AllowsMergedIf)); 6993 FormatStyle ShortMergedIf = AllowsMergedIf; 6994 ShortMergedIf.ColumnLimit = 25; 6995 verifyFormat("#define A \\\n" 6996 " if (true) return 42;", 6997 ShortMergedIf); 6998 verifyFormat("#define A \\\n" 6999 " f(); \\\n" 7000 " if (true)\n" 7001 "#define B", 7002 ShortMergedIf); 7003 verifyFormat("#define A \\\n" 7004 " f(); \\\n" 7005 " if (true)\n" 7006 "g();", 7007 ShortMergedIf); 7008 verifyFormat("{\n" 7009 "#ifdef A\n" 7010 " // Comment\n" 7011 " if (true) continue;\n" 7012 "#endif\n" 7013 " // Comment\n" 7014 " if (true) continue;\n" 7015 "}", 7016 ShortMergedIf); 7017 ShortMergedIf.ColumnLimit = 29; 7018 verifyFormat("#define A \\\n" 7019 " if (aaaaaaaaaa) return 1; \\\n" 7020 " return 2;", 7021 ShortMergedIf); 7022 ShortMergedIf.ColumnLimit = 28; 7023 verifyFormat("#define A \\\n" 7024 " if (aaaaaaaaaa) \\\n" 7025 " return 1; \\\n" 7026 " return 2;", 7027 ShortMergedIf); 7028 } 7029 7030 TEST_F(FormatTest, BlockCommentsInControlLoops) { 7031 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7032 " f();\n" 7033 "}"); 7034 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7035 " f();\n" 7036 "} /* another comment */ else /* comment #3 */ {\n" 7037 " g();\n" 7038 "}"); 7039 verifyFormat("while (0) /* a comment in a strange place */ {\n" 7040 " f();\n" 7041 "}"); 7042 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 7043 " f();\n" 7044 "}"); 7045 verifyFormat("do /* a comment in a strange place */ {\n" 7046 " f();\n" 7047 "} /* another comment */ while (0);"); 7048 } 7049 7050 TEST_F(FormatTest, BlockComments) { 7051 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 7052 format("/* *//* */ /* */\n/* *//* */ /* */")); 7053 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 7054 EXPECT_EQ("#define A /*123*/ \\\n" 7055 " b\n" 7056 "/* */\n" 7057 "someCall(\n" 7058 " parameter);", 7059 format("#define A /*123*/ b\n" 7060 "/* */\n" 7061 "someCall(parameter);", 7062 getLLVMStyleWithColumns(15))); 7063 7064 EXPECT_EQ("#define A\n" 7065 "/* */ someCall(\n" 7066 " parameter);", 7067 format("#define A\n" 7068 "/* */someCall(parameter);", 7069 getLLVMStyleWithColumns(15))); 7070 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7071 EXPECT_EQ("/*\n" 7072 "*\n" 7073 " * aaaaaa\n" 7074 " * aaaaaa\n" 7075 "*/", 7076 format("/*\n" 7077 "*\n" 7078 " * aaaaaa aaaaaa\n" 7079 "*/", 7080 getLLVMStyleWithColumns(10))); 7081 EXPECT_EQ("/*\n" 7082 "**\n" 7083 "* aaaaaa\n" 7084 "*aaaaaa\n" 7085 "*/", 7086 format("/*\n" 7087 "**\n" 7088 "* aaaaaa aaaaaa\n" 7089 "*/", 7090 getLLVMStyleWithColumns(10))); 7091 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7092 " /* line 1\n" 7093 " bbbbbbbbbbbb */\n" 7094 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7095 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7096 " /* line 1\n" 7097 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7098 getLLVMStyleWithColumns(50))); 7099 7100 FormatStyle NoBinPacking = getLLVMStyle(); 7101 NoBinPacking.BinPackParameters = false; 7102 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7103 " 2, /* comment 2 */\n" 7104 " 3, /* comment 3 */\n" 7105 " aaaa,\n" 7106 " bbbb);", 7107 format("someFunction (1, /* comment 1 */\n" 7108 " 2, /* comment 2 */ \n" 7109 " 3, /* comment 3 */\n" 7110 "aaaa, bbbb );", 7111 NoBinPacking)); 7112 verifyFormat( 7113 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7115 EXPECT_EQ( 7116 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7117 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7119 format( 7120 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7121 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7123 EXPECT_EQ( 7124 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7125 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7126 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7127 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7128 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7129 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7130 7131 verifyFormat("void f(int * /* unused */) {}"); 7132 7133 EXPECT_EQ("/*\n" 7134 " **\n" 7135 " */", 7136 format("/*\n" 7137 " **\n" 7138 " */")); 7139 EXPECT_EQ("/*\n" 7140 " *q\n" 7141 " */", 7142 format("/*\n" 7143 " *q\n" 7144 " */")); 7145 EXPECT_EQ("/*\n" 7146 " * q\n" 7147 " */", 7148 format("/*\n" 7149 " * q\n" 7150 " */")); 7151 EXPECT_EQ("/*\n" 7152 " **/", 7153 format("/*\n" 7154 " **/")); 7155 EXPECT_EQ("/*\n" 7156 " ***/", 7157 format("/*\n" 7158 " ***/")); 7159 } 7160 7161 TEST_F(FormatTest, BlockCommentsInMacros) { 7162 EXPECT_EQ("#define A \\\n" 7163 " { \\\n" 7164 " /* one line */ \\\n" 7165 " someCall();", 7166 format("#define A { \\\n" 7167 " /* one line */ \\\n" 7168 " someCall();", 7169 getLLVMStyleWithColumns(20))); 7170 EXPECT_EQ("#define A \\\n" 7171 " { \\\n" 7172 " /* previous */ \\\n" 7173 " /* one line */ \\\n" 7174 " someCall();", 7175 format("#define A { \\\n" 7176 " /* previous */ \\\n" 7177 " /* one line */ \\\n" 7178 " someCall();", 7179 getLLVMStyleWithColumns(20))); 7180 } 7181 7182 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7183 EXPECT_EQ("a = {\n" 7184 " 1111 /* */\n" 7185 "};", 7186 format("a = {1111 /* */\n" 7187 "};", 7188 getLLVMStyleWithColumns(15))); 7189 EXPECT_EQ("a = {\n" 7190 " 1111 /* */\n" 7191 "};", 7192 format("a = {1111 /* */\n" 7193 "};", 7194 getLLVMStyleWithColumns(15))); 7195 7196 // FIXME: The formatting is still wrong here. 7197 EXPECT_EQ("a = {\n" 7198 " 1111 /* a\n" 7199 " */\n" 7200 "};", 7201 format("a = {1111 /* a */\n" 7202 "};", 7203 getLLVMStyleWithColumns(15))); 7204 } 7205 7206 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7207 verifyFormat("{\n" 7208 " // a\n" 7209 " // b"); 7210 } 7211 7212 TEST_F(FormatTest, FormatStarDependingOnContext) { 7213 verifyFormat("void f(int *a);"); 7214 verifyFormat("void f() { f(fint * b); }"); 7215 verifyFormat("class A {\n void f(int *a);\n};"); 7216 verifyFormat("class A {\n int *a;\n};"); 7217 verifyFormat("namespace a {\n" 7218 "namespace b {\n" 7219 "class A {\n" 7220 " void f() {}\n" 7221 " int *a;\n" 7222 "};\n" 7223 "}\n" 7224 "}"); 7225 } 7226 7227 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7228 verifyFormat("while"); 7229 verifyFormat("operator"); 7230 } 7231 7232 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7233 // This code would be painfully slow to format if we didn't skip it. 7234 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 7235 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7236 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7237 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7238 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7239 "A(1, 1)\n" 7240 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7241 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7242 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7243 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7244 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7245 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7246 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7247 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7248 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7249 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7250 // Deeply nested part is untouched, rest is formatted. 7251 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7252 format(std::string("int i;\n") + Code + "int j;\n", 7253 getLLVMStyle(), IC_ExpectIncomplete)); 7254 } 7255 7256 //===----------------------------------------------------------------------===// 7257 // Objective-C tests. 7258 //===----------------------------------------------------------------------===// 7259 7260 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7261 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7262 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7263 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7264 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7265 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7266 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7267 format("-(NSInteger)Method3:(id)anObject;")); 7268 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7269 format("-(NSInteger)Method4:(id)anObject;")); 7270 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7271 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7272 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7273 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7274 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7275 "forAllCells:(BOOL)flag;", 7276 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7277 "forAllCells:(BOOL)flag;")); 7278 7279 // Very long objectiveC method declaration. 7280 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7281 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7282 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7283 " inRange:(NSRange)range\n" 7284 " outRange:(NSRange)out_range\n" 7285 " outRange1:(NSRange)out_range1\n" 7286 " outRange2:(NSRange)out_range2\n" 7287 " outRange3:(NSRange)out_range3\n" 7288 " outRange4:(NSRange)out_range4\n" 7289 " outRange5:(NSRange)out_range5\n" 7290 " outRange6:(NSRange)out_range6\n" 7291 " outRange7:(NSRange)out_range7\n" 7292 " outRange8:(NSRange)out_range8\n" 7293 " outRange9:(NSRange)out_range9;"); 7294 7295 // When the function name has to be wrapped. 7296 FormatStyle Style = getLLVMStyle(); 7297 Style.IndentWrappedFunctionNames = false; 7298 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7299 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7300 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7301 "}", 7302 Style); 7303 Style.IndentWrappedFunctionNames = true; 7304 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7305 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7306 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7307 "}", 7308 Style); 7309 7310 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7311 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7312 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7313 // protocol lists (but not for template classes): 7314 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7315 7316 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7317 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7318 7319 // If there's no return type (very rare in practice!), LLVM and Google style 7320 // agree. 7321 verifyFormat("- foo;"); 7322 verifyFormat("- foo:(int)f;"); 7323 verifyGoogleFormat("- foo:(int)foo;"); 7324 } 7325 7326 TEST_F(FormatTest, FormatObjCInterface) { 7327 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7328 "@public\n" 7329 " int field1;\n" 7330 "@protected\n" 7331 " int field2;\n" 7332 "@private\n" 7333 " int field3;\n" 7334 "@package\n" 7335 " int field4;\n" 7336 "}\n" 7337 "+ (id)init;\n" 7338 "@end"); 7339 7340 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7341 " @public\n" 7342 " int field1;\n" 7343 " @protected\n" 7344 " int field2;\n" 7345 " @private\n" 7346 " int field3;\n" 7347 " @package\n" 7348 " int field4;\n" 7349 "}\n" 7350 "+ (id)init;\n" 7351 "@end"); 7352 7353 verifyFormat("@interface /* wait for it */ Foo\n" 7354 "+ (id)init;\n" 7355 "// Look, a comment!\n" 7356 "- (int)answerWith:(int)i;\n" 7357 "@end"); 7358 7359 verifyFormat("@interface Foo\n" 7360 "@end\n" 7361 "@interface Bar\n" 7362 "@end"); 7363 7364 verifyFormat("@interface Foo : Bar\n" 7365 "+ (id)init;\n" 7366 "@end"); 7367 7368 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7369 "+ (id)init;\n" 7370 "@end"); 7371 7372 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7373 "+ (id)init;\n" 7374 "@end"); 7375 7376 verifyFormat("@interface Foo (HackStuff)\n" 7377 "+ (id)init;\n" 7378 "@end"); 7379 7380 verifyFormat("@interface Foo ()\n" 7381 "+ (id)init;\n" 7382 "@end"); 7383 7384 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7385 "+ (id)init;\n" 7386 "@end"); 7387 7388 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7389 "+ (id)init;\n" 7390 "@end"); 7391 7392 verifyFormat("@interface Foo {\n" 7393 " int _i;\n" 7394 "}\n" 7395 "+ (id)init;\n" 7396 "@end"); 7397 7398 verifyFormat("@interface Foo : Bar {\n" 7399 " int _i;\n" 7400 "}\n" 7401 "+ (id)init;\n" 7402 "@end"); 7403 7404 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7405 " int _i;\n" 7406 "}\n" 7407 "+ (id)init;\n" 7408 "@end"); 7409 7410 verifyFormat("@interface Foo (HackStuff) {\n" 7411 " int _i;\n" 7412 "}\n" 7413 "+ (id)init;\n" 7414 "@end"); 7415 7416 verifyFormat("@interface Foo () {\n" 7417 " int _i;\n" 7418 "}\n" 7419 "+ (id)init;\n" 7420 "@end"); 7421 7422 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7423 " int _i;\n" 7424 "}\n" 7425 "+ (id)init;\n" 7426 "@end"); 7427 7428 FormatStyle OnePerLine = getGoogleStyle(); 7429 OnePerLine.BinPackParameters = false; 7430 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7435 "}", 7436 OnePerLine); 7437 } 7438 7439 TEST_F(FormatTest, FormatObjCImplementation) { 7440 verifyFormat("@implementation Foo : NSObject {\n" 7441 "@public\n" 7442 " int field1;\n" 7443 "@protected\n" 7444 " int field2;\n" 7445 "@private\n" 7446 " int field3;\n" 7447 "@package\n" 7448 " int field4;\n" 7449 "}\n" 7450 "+ (id)init {\n}\n" 7451 "@end"); 7452 7453 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7454 " @public\n" 7455 " int field1;\n" 7456 " @protected\n" 7457 " int field2;\n" 7458 " @private\n" 7459 " int field3;\n" 7460 " @package\n" 7461 " int field4;\n" 7462 "}\n" 7463 "+ (id)init {\n}\n" 7464 "@end"); 7465 7466 verifyFormat("@implementation Foo\n" 7467 "+ (id)init {\n" 7468 " if (true)\n" 7469 " return nil;\n" 7470 "}\n" 7471 "// Look, a comment!\n" 7472 "- (int)answerWith:(int)i {\n" 7473 " return i;\n" 7474 "}\n" 7475 "+ (int)answerWith:(int)i {\n" 7476 " return i;\n" 7477 "}\n" 7478 "@end"); 7479 7480 verifyFormat("@implementation Foo\n" 7481 "@end\n" 7482 "@implementation Bar\n" 7483 "@end"); 7484 7485 EXPECT_EQ("@implementation Foo : Bar\n" 7486 "+ (id)init {\n}\n" 7487 "- (void)foo {\n}\n" 7488 "@end", 7489 format("@implementation Foo : Bar\n" 7490 "+(id)init{}\n" 7491 "-(void)foo{}\n" 7492 "@end")); 7493 7494 verifyFormat("@implementation Foo {\n" 7495 " int _i;\n" 7496 "}\n" 7497 "+ (id)init {\n}\n" 7498 "@end"); 7499 7500 verifyFormat("@implementation Foo : Bar {\n" 7501 " int _i;\n" 7502 "}\n" 7503 "+ (id)init {\n}\n" 7504 "@end"); 7505 7506 verifyFormat("@implementation Foo (HackStuff)\n" 7507 "+ (id)init {\n}\n" 7508 "@end"); 7509 verifyFormat("@implementation ObjcClass\n" 7510 "- (void)method;\n" 7511 "{}\n" 7512 "@end"); 7513 } 7514 7515 TEST_F(FormatTest, FormatObjCProtocol) { 7516 verifyFormat("@protocol Foo\n" 7517 "@property(weak) id delegate;\n" 7518 "- (NSUInteger)numberOfThings;\n" 7519 "@end"); 7520 7521 verifyFormat("@protocol MyProtocol <NSObject>\n" 7522 "- (NSUInteger)numberOfThings;\n" 7523 "@end"); 7524 7525 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7526 "- (NSUInteger)numberOfThings;\n" 7527 "@end"); 7528 7529 verifyFormat("@protocol Foo;\n" 7530 "@protocol Bar;\n"); 7531 7532 verifyFormat("@protocol Foo\n" 7533 "@end\n" 7534 "@protocol Bar\n" 7535 "@end"); 7536 7537 verifyFormat("@protocol myProtocol\n" 7538 "- (void)mandatoryWithInt:(int)i;\n" 7539 "@optional\n" 7540 "- (void)optional;\n" 7541 "@required\n" 7542 "- (void)required;\n" 7543 "@optional\n" 7544 "@property(assign) int madProp;\n" 7545 "@end\n"); 7546 7547 verifyFormat("@property(nonatomic, assign, readonly)\n" 7548 " int *looooooooooooooooooooooooooooongNumber;\n" 7549 "@property(nonatomic, assign, readonly)\n" 7550 " NSString *looooooooooooooooooooooooooooongName;"); 7551 7552 verifyFormat("@implementation PR18406\n" 7553 "}\n" 7554 "@end"); 7555 } 7556 7557 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7558 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7559 " rect:(NSRect)theRect\n" 7560 " interval:(float)theInterval {\n" 7561 "}"); 7562 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7563 " longKeyword:(NSRect)theRect\n" 7564 " longerKeyword:(float)theInterval\n" 7565 " error:(NSError **)theError {\n" 7566 "}"); 7567 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7568 " longKeyword:(NSRect)theRect\n" 7569 " evenLongerKeyword:(float)theInterval\n" 7570 " error:(NSError **)theError {\n" 7571 "}"); 7572 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7573 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7574 " NS_DESIGNATED_INITIALIZER;", 7575 getLLVMStyleWithColumns(60)); 7576 7577 // Continuation indent width should win over aligning colons if the function 7578 // name is long. 7579 FormatStyle continuationStyle = getGoogleStyle(); 7580 continuationStyle.ColumnLimit = 40; 7581 continuationStyle.IndentWrappedFunctionNames = true; 7582 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7583 " dontAlignNamef:(NSRect)theRect {\n" 7584 "}", 7585 continuationStyle); 7586 7587 // Make sure we don't break aligning for short parameter names. 7588 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7589 " aShortf:(NSRect)theRect {\n" 7590 "}", 7591 continuationStyle); 7592 7593 // Format pairs correctly. 7594 verifyFormat("- (void)drawRectOn:(id)surface\n" 7595 " ofSize:(aaaaaaaa)height\n" 7596 " :(size_t)width\n" 7597 " atOrigin:(size_t)x\n" 7598 " :(size_t)y\n" 7599 " aaaaa:(a)yyy\n" 7600 " bbb:(d)cccc;"); 7601 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;"); 7602 verifyFormat("- (void)drawRectOn:(id)surface\n" 7603 " ofSize:(size_t)height\n" 7604 " :(size_t)width;", 7605 getLLVMStyleWithColumns(60)); 7606 } 7607 7608 TEST_F(FormatTest, FormatObjCMethodExpr) { 7609 verifyFormat("[foo bar:baz];"); 7610 verifyFormat("return [foo bar:baz];"); 7611 verifyFormat("return (a)[foo bar:baz];"); 7612 verifyFormat("f([foo bar:baz]);"); 7613 verifyFormat("f(2, [foo bar:baz]);"); 7614 verifyFormat("f(2, a ? b : c);"); 7615 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7616 7617 // Unary operators. 7618 verifyFormat("int a = +[foo bar:baz];"); 7619 verifyFormat("int a = -[foo bar:baz];"); 7620 verifyFormat("int a = ![foo bar:baz];"); 7621 verifyFormat("int a = ~[foo bar:baz];"); 7622 verifyFormat("int a = ++[foo bar:baz];"); 7623 verifyFormat("int a = --[foo bar:baz];"); 7624 verifyFormat("int a = sizeof [foo bar:baz];"); 7625 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7626 verifyFormat("int a = &[foo bar:baz];"); 7627 verifyFormat("int a = *[foo bar:baz];"); 7628 // FIXME: Make casts work, without breaking f()[4]. 7629 // verifyFormat("int a = (int)[foo bar:baz];"); 7630 // verifyFormat("return (int)[foo bar:baz];"); 7631 // verifyFormat("(void)[foo bar:baz];"); 7632 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7633 7634 // Binary operators. 7635 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7636 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7637 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7638 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7639 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7640 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7641 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7642 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7643 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7644 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7645 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7646 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7647 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7648 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7649 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7650 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7651 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7652 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7653 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7654 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7655 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7656 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7657 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7658 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7659 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7660 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7661 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7662 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7663 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7664 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7665 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7666 // Whew! 7667 7668 verifyFormat("return in[42];"); 7669 verifyFormat("for (auto v : in[1]) {\n}"); 7670 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7671 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7672 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7673 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7674 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7675 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7676 "}"); 7677 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7678 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7679 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7680 7681 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7682 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7683 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7684 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7685 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7686 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7687 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7688 7689 verifyFormat("arr[[self indexForFoo:a]];"); 7690 verifyFormat("throw [self errorFor:a];"); 7691 verifyFormat("@throw [self errorFor:a];"); 7692 7693 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7694 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7695 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7696 7697 // This tests that the formatter doesn't break after "backing" but before ":", 7698 // which would be at 80 columns. 7699 verifyFormat( 7700 "void f() {\n" 7701 " if ((self = [super initWithContentRect:contentRect\n" 7702 " styleMask:styleMask ?: otherMask\n" 7703 " backing:NSBackingStoreBuffered\n" 7704 " defer:YES]))"); 7705 7706 verifyFormat( 7707 "[foo checkThatBreakingAfterColonWorksOk:\n" 7708 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7709 7710 verifyFormat("[myObj short:arg1 // Force line break\n" 7711 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7712 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7713 " error:arg4];"); 7714 verifyFormat( 7715 "void f() {\n" 7716 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7717 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7718 " pos.width(), pos.height())\n" 7719 " styleMask:NSBorderlessWindowMask\n" 7720 " backing:NSBackingStoreBuffered\n" 7721 " defer:NO]);\n" 7722 "}"); 7723 verifyFormat( 7724 "void f() {\n" 7725 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7726 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7727 " pos.width(), pos.height())\n" 7728 " syeMask:NSBorderlessWindowMask\n" 7729 " bking:NSBackingStoreBuffered\n" 7730 " der:NO]);\n" 7731 "}", 7732 getLLVMStyleWithColumns(70)); 7733 verifyFormat( 7734 "void f() {\n" 7735 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7736 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7737 " pos.width(), pos.height())\n" 7738 " styleMask:NSBorderlessWindowMask\n" 7739 " backing:NSBackingStoreBuffered\n" 7740 " defer:NO]);\n" 7741 "}", 7742 getChromiumStyle(FormatStyle::LK_Cpp)); 7743 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7744 " with:contentsNativeView];"); 7745 7746 verifyFormat( 7747 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7748 " owner:nillllll];"); 7749 7750 verifyFormat( 7751 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7752 " forType:kBookmarkButtonDragType];"); 7753 7754 verifyFormat("[defaultCenter addObserver:self\n" 7755 " selector:@selector(willEnterFullscreen)\n" 7756 " name:kWillEnterFullscreenNotification\n" 7757 " object:nil];"); 7758 verifyFormat("[image_rep drawInRect:drawRect\n" 7759 " fromRect:NSZeroRect\n" 7760 " operation:NSCompositeCopy\n" 7761 " fraction:1.0\n" 7762 " respectFlipped:NO\n" 7763 " hints:nil];"); 7764 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7766 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7768 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7769 " aaaaaaaaaaaaaaaaaaaaaa];"); 7770 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7771 " .aaaaaaaa];", // FIXME: Indentation seems off. 7772 getLLVMStyleWithColumns(60)); 7773 7774 verifyFormat( 7775 "scoped_nsobject<NSTextField> message(\n" 7776 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7777 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7778 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7779 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7780 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7781 " aaaa:bbb];"); 7782 verifyFormat("[self param:function( //\n" 7783 " parameter)]"); 7784 verifyFormat( 7785 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7786 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7787 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7788 7789 // FIXME: This violates the column limit. 7790 verifyFormat( 7791 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7792 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7793 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7794 getLLVMStyleWithColumns(60)); 7795 7796 // Variadic parameters. 7797 verifyFormat( 7798 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7799 verifyFormat( 7800 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7801 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7802 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7803 verifyFormat("[self // break\n" 7804 " a:a\n" 7805 " aaa:aaa];"); 7806 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7807 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7808 7809 // Formats pair-parameters. 7810 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];"); 7811 verifyFormat("[I drawRectOn:surface //\n" 7812 " ofSize:aa:bbb\n" 7813 " atOrigin:cc:dd];"); 7814 } 7815 7816 TEST_F(FormatTest, ObjCAt) { 7817 verifyFormat("@autoreleasepool"); 7818 verifyFormat("@catch"); 7819 verifyFormat("@class"); 7820 verifyFormat("@compatibility_alias"); 7821 verifyFormat("@defs"); 7822 verifyFormat("@dynamic"); 7823 verifyFormat("@encode"); 7824 verifyFormat("@end"); 7825 verifyFormat("@finally"); 7826 verifyFormat("@implementation"); 7827 verifyFormat("@import"); 7828 verifyFormat("@interface"); 7829 verifyFormat("@optional"); 7830 verifyFormat("@package"); 7831 verifyFormat("@private"); 7832 verifyFormat("@property"); 7833 verifyFormat("@protected"); 7834 verifyFormat("@protocol"); 7835 verifyFormat("@public"); 7836 verifyFormat("@required"); 7837 verifyFormat("@selector"); 7838 verifyFormat("@synchronized"); 7839 verifyFormat("@synthesize"); 7840 verifyFormat("@throw"); 7841 verifyFormat("@try"); 7842 7843 EXPECT_EQ("@interface", format("@ interface")); 7844 7845 // The precise formatting of this doesn't matter, nobody writes code like 7846 // this. 7847 verifyFormat("@ /*foo*/ interface"); 7848 } 7849 7850 TEST_F(FormatTest, ObjCSnippets) { 7851 verifyFormat("@autoreleasepool {\n" 7852 " foo();\n" 7853 "}"); 7854 verifyFormat("@class Foo, Bar;"); 7855 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7856 verifyFormat("@dynamic textColor;"); 7857 verifyFormat("char *buf1 = @encode(int *);"); 7858 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7859 verifyFormat("char *buf1 = @encode(int **);"); 7860 verifyFormat("Protocol *proto = @protocol(p1);"); 7861 verifyFormat("SEL s = @selector(foo:);"); 7862 verifyFormat("@synchronized(self) {\n" 7863 " f();\n" 7864 "}"); 7865 7866 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7867 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7868 7869 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7870 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7871 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7872 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7873 getMozillaStyle()); 7874 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7875 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7876 getWebKitStyle()); 7877 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7878 7879 verifyFormat("@import foo.bar;\n" 7880 "@import baz;"); 7881 } 7882 7883 TEST_F(FormatTest, ObjCForIn) { 7884 verifyFormat("- (void)test {\n" 7885 " for (NSString *n in arrayOfStrings) {\n" 7886 " foo(n);\n" 7887 " }\n" 7888 "}"); 7889 verifyFormat("- (void)test {\n" 7890 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7891 " foo(n);\n" 7892 " }\n" 7893 "}"); 7894 } 7895 7896 TEST_F(FormatTest, ObjCLiterals) { 7897 verifyFormat("@\"String\""); 7898 verifyFormat("@1"); 7899 verifyFormat("@+4.8"); 7900 verifyFormat("@-4"); 7901 verifyFormat("@1LL"); 7902 verifyFormat("@.5"); 7903 verifyFormat("@'c'"); 7904 verifyFormat("@true"); 7905 7906 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7907 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7908 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7909 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7910 7911 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7912 } 7913 7914 TEST_F(FormatTest, ObjCDictLiterals) { 7915 verifyFormat("@{"); 7916 verifyFormat("@{}"); 7917 verifyFormat("@{@\"one\" : @1}"); 7918 verifyFormat("return @{@\"one\" : @1;"); 7919 verifyFormat("@{@\"one\" : @1}"); 7920 7921 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7922 verifyFormat("@{\n" 7923 " @\"one\" : @{@2 : @1},\n" 7924 "}"); 7925 7926 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7927 verifyIncompleteFormat("[self setDict:@{}"); 7928 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7929 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7930 verifyFormat( 7931 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7932 verifyFormat( 7933 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7934 7935 verifyFormat("NSDictionary *d = @{\n" 7936 " @\"nam\" : NSUserNam(),\n" 7937 " @\"dte\" : [NSDate date],\n" 7938 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7939 "};"); 7940 verifyFormat( 7941 "@{\n" 7942 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7943 "regularFont,\n" 7944 "};"); 7945 verifyGoogleFormat( 7946 "@{\n" 7947 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7948 "regularFont,\n" 7949 "};"); 7950 verifyFormat( 7951 "@{\n" 7952 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7953 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7954 "};"); 7955 7956 // We should try to be robust in case someone forgets the "@". 7957 verifyFormat("NSDictionary *d = {\n" 7958 " @\"nam\" : NSUserNam(),\n" 7959 " @\"dte\" : [NSDate date],\n" 7960 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7961 "};"); 7962 verifyFormat("NSMutableDictionary *dictionary =\n" 7963 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7964 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7965 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7966 " cccccccccccccccc : ccccccccccccccc\n" 7967 " }];"); 7968 7969 // Ensure that casts before the key are kept on the same line as the key. 7970 verifyFormat( 7971 "NSDictionary *d = @{\n" 7972 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7973 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7974 "};"); 7975 } 7976 7977 TEST_F(FormatTest, ObjCArrayLiterals) { 7978 verifyIncompleteFormat("@["); 7979 verifyFormat("@[]"); 7980 verifyFormat( 7981 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7982 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7983 verifyFormat("NSArray *array = @[ [foo description] ];"); 7984 7985 verifyFormat( 7986 "NSArray *some_variable = @[\n" 7987 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7988 " @\"aaaaaaaaaaaaaaaaa\",\n" 7989 " @\"aaaaaaaaaaaaaaaaa\",\n" 7990 " @\"aaaaaaaaaaaaaaaaa\",\n" 7991 "];"); 7992 verifyFormat( 7993 "NSArray *some_variable = @[\n" 7994 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7995 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7996 "];"); 7997 verifyFormat("NSArray *some_variable = @[\n" 7998 " @\"aaaaaaaaaaaaaaaaa\",\n" 7999 " @\"aaaaaaaaaaaaaaaaa\",\n" 8000 " @\"aaaaaaaaaaaaaaaaa\",\n" 8001 " @\"aaaaaaaaaaaaaaaaa\",\n" 8002 "];"); 8003 verifyFormat("NSArray *array = @[\n" 8004 " @\"a\",\n" 8005 " @\"a\",\n" // Trailing comma -> one per line. 8006 "];"); 8007 8008 // We should try to be robust in case someone forgets the "@". 8009 verifyFormat("NSArray *some_variable = [\n" 8010 " @\"aaaaaaaaaaaaaaaaa\",\n" 8011 " @\"aaaaaaaaaaaaaaaaa\",\n" 8012 " @\"aaaaaaaaaaaaaaaaa\",\n" 8013 " @\"aaaaaaaaaaaaaaaaa\",\n" 8014 "];"); 8015 verifyFormat( 8016 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 8017 " index:(NSUInteger)index\n" 8018 " nonDigitAttributes:\n" 8019 " (NSDictionary *)noDigitAttributes;"); 8020 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 8021 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 8022 "]];"); 8023 } 8024 8025 TEST_F(FormatTest, BreaksStringLiterals) { 8026 EXPECT_EQ("\"some text \"\n" 8027 "\"other\";", 8028 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8029 EXPECT_EQ("\"some text \"\n" 8030 "\"other\";", 8031 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8032 EXPECT_EQ( 8033 "#define A \\\n" 8034 " \"some \" \\\n" 8035 " \"text \" \\\n" 8036 " \"other\";", 8037 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8038 EXPECT_EQ( 8039 "#define A \\\n" 8040 " \"so \" \\\n" 8041 " \"text \" \\\n" 8042 " \"other\";", 8043 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8044 8045 EXPECT_EQ("\"some text\"", 8046 format("\"some text\"", getLLVMStyleWithColumns(1))); 8047 EXPECT_EQ("\"some text\"", 8048 format("\"some text\"", getLLVMStyleWithColumns(11))); 8049 EXPECT_EQ("\"some \"\n" 8050 "\"text\"", 8051 format("\"some text\"", getLLVMStyleWithColumns(10))); 8052 EXPECT_EQ("\"some \"\n" 8053 "\"text\"", 8054 format("\"some text\"", getLLVMStyleWithColumns(7))); 8055 EXPECT_EQ("\"some\"\n" 8056 "\" tex\"\n" 8057 "\"t\"", 8058 format("\"some text\"", getLLVMStyleWithColumns(6))); 8059 EXPECT_EQ("\"some\"\n" 8060 "\" tex\"\n" 8061 "\" and\"", 8062 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8063 EXPECT_EQ("\"some\"\n" 8064 "\"/tex\"\n" 8065 "\"/and\"", 8066 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8067 8068 EXPECT_EQ("variable =\n" 8069 " \"long string \"\n" 8070 " \"literal\";", 8071 format("variable = \"long string literal\";", 8072 getLLVMStyleWithColumns(20))); 8073 8074 EXPECT_EQ("variable = f(\n" 8075 " \"long string \"\n" 8076 " \"literal\",\n" 8077 " short,\n" 8078 " loooooooooooooooooooong);", 8079 format("variable = f(\"long string literal\", short, " 8080 "loooooooooooooooooooong);", 8081 getLLVMStyleWithColumns(20))); 8082 8083 EXPECT_EQ( 8084 "f(g(\"long string \"\n" 8085 " \"literal\"),\n" 8086 " b);", 8087 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8088 EXPECT_EQ("f(g(\"long string \"\n" 8089 " \"literal\",\n" 8090 " a),\n" 8091 " b);", 8092 format("f(g(\"long string literal\", a), b);", 8093 getLLVMStyleWithColumns(20))); 8094 EXPECT_EQ( 8095 "f(\"one two\".split(\n" 8096 " variable));", 8097 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8098 EXPECT_EQ("f(\"one two three four five six \"\n" 8099 " \"seven\".split(\n" 8100 " really_looooong_variable));", 8101 format("f(\"one two three four five six seven\"." 8102 "split(really_looooong_variable));", 8103 getLLVMStyleWithColumns(33))); 8104 8105 EXPECT_EQ("f(\"some \"\n" 8106 " \"text\",\n" 8107 " other);", 8108 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8109 8110 // Only break as a last resort. 8111 verifyFormat( 8112 "aaaaaaaaaaaaaaaaaaaa(\n" 8113 " aaaaaaaaaaaaaaaaaaaa,\n" 8114 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8115 8116 EXPECT_EQ("\"splitmea\"\n" 8117 "\"trandomp\"\n" 8118 "\"oint\"", 8119 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8120 8121 EXPECT_EQ("\"split/\"\n" 8122 "\"pathat/\"\n" 8123 "\"slashes\"", 8124 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8125 8126 EXPECT_EQ("\"split/\"\n" 8127 "\"pathat/\"\n" 8128 "\"slashes\"", 8129 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8130 EXPECT_EQ("\"split at \"\n" 8131 "\"spaces/at/\"\n" 8132 "\"slashes.at.any$\"\n" 8133 "\"non-alphanumeric%\"\n" 8134 "\"1111111111characte\"\n" 8135 "\"rs\"", 8136 format("\"split at " 8137 "spaces/at/" 8138 "slashes.at." 8139 "any$non-" 8140 "alphanumeric%" 8141 "1111111111characte" 8142 "rs\"", 8143 getLLVMStyleWithColumns(20))); 8144 8145 // Verify that splitting the strings understands 8146 // Style::AlwaysBreakBeforeMultilineStrings. 8147 EXPECT_EQ( 8148 "aaaaaaaaaaaa(\n" 8149 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8150 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8151 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8152 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8153 "aaaaaaaaaaaaaaaaaaaaaa\");", 8154 getGoogleStyle())); 8155 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8156 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8157 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8158 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8159 "aaaaaaaaaaaaaaaaaaaaaa\";", 8160 getGoogleStyle())); 8161 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8162 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8163 format("llvm::outs() << " 8164 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8165 "aaaaaaaaaaaaaaaaaaa\";")); 8166 EXPECT_EQ("ffff(\n" 8167 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8168 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8169 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8170 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8171 getGoogleStyle())); 8172 8173 FormatStyle Style = getLLVMStyleWithColumns(12); 8174 Style.BreakStringLiterals = false; 8175 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8176 8177 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8178 AlignLeft.AlignEscapedNewlinesLeft = true; 8179 EXPECT_EQ("#define A \\\n" 8180 " \"some \" \\\n" 8181 " \"text \" \\\n" 8182 " \"other\";", 8183 format("#define A \"some text other\";", AlignLeft)); 8184 } 8185 8186 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8187 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8188 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8189 EXPECT_EQ("int i = a(b());", 8190 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8191 } 8192 8193 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8194 EXPECT_EQ( 8195 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8196 "(\n" 8197 " \"x\t\");", 8198 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8199 "aaaaaaa(" 8200 "\"x\t\");")); 8201 } 8202 8203 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8204 EXPECT_EQ( 8205 "u8\"utf8 string \"\n" 8206 "u8\"literal\";", 8207 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8208 EXPECT_EQ( 8209 "u\"utf16 string \"\n" 8210 "u\"literal\";", 8211 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8212 EXPECT_EQ( 8213 "U\"utf32 string \"\n" 8214 "U\"literal\";", 8215 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8216 EXPECT_EQ("L\"wide string \"\n" 8217 "L\"literal\";", 8218 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8219 EXPECT_EQ("@\"NSString \"\n" 8220 "@\"literal\";", 8221 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8222 8223 // This input makes clang-format try to split the incomplete unicode escape 8224 // sequence, which used to lead to a crasher. 8225 verifyNoCrash( 8226 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8227 getLLVMStyleWithColumns(60)); 8228 } 8229 8230 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8231 FormatStyle Style = getGoogleStyleWithColumns(15); 8232 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8233 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8234 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8235 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8236 EXPECT_EQ("u8R\"x(raw literal)x\";", 8237 format("u8R\"x(raw literal)x\";", Style)); 8238 } 8239 8240 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8241 FormatStyle Style = getLLVMStyleWithColumns(20); 8242 EXPECT_EQ( 8243 "_T(\"aaaaaaaaaaaaaa\")\n" 8244 "_T(\"aaaaaaaaaaaaaa\")\n" 8245 "_T(\"aaaaaaaaaaaa\")", 8246 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8247 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8248 " _T(\"aaaaaa\"),\n" 8249 " z);", 8250 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8251 8252 // FIXME: Handle embedded spaces in one iteration. 8253 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8254 // "_T(\"aaaaaaaaaaaaa\")\n" 8255 // "_T(\"aaaaaaaaaaaaa\")\n" 8256 // "_T(\"a\")", 8257 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8258 // getLLVMStyleWithColumns(20))); 8259 EXPECT_EQ( 8260 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8261 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8262 EXPECT_EQ("f(\n" 8263 "#if !TEST\n" 8264 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8265 "#endif\n" 8266 " );", 8267 format("f(\n" 8268 "#if !TEST\n" 8269 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8270 "#endif\n" 8271 ");")); 8272 EXPECT_EQ("f(\n" 8273 "\n" 8274 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8275 format("f(\n" 8276 "\n" 8277 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8278 } 8279 8280 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8281 EXPECT_EQ( 8282 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8285 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8288 } 8289 8290 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8291 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8292 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8293 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8294 "multiline raw string literal xxxxxxxxxxxxxx\n" 8295 ")x\",\n" 8296 " a),\n" 8297 " b);", 8298 format("fffffffffff(g(R\"x(\n" 8299 "multiline raw string literal xxxxxxxxxxxxxx\n" 8300 ")x\", a), b);", 8301 getGoogleStyleWithColumns(20))); 8302 EXPECT_EQ("fffffffffff(\n" 8303 " g(R\"x(qqq\n" 8304 "multiline raw string literal xxxxxxxxxxxxxx\n" 8305 ")x\",\n" 8306 " a),\n" 8307 " b);", 8308 format("fffffffffff(g(R\"x(qqq\n" 8309 "multiline raw string literal xxxxxxxxxxxxxx\n" 8310 ")x\", a), b);", 8311 getGoogleStyleWithColumns(20))); 8312 8313 EXPECT_EQ("fffffffffff(R\"x(\n" 8314 "multiline raw string literal xxxxxxxxxxxxxx\n" 8315 ")x\");", 8316 format("fffffffffff(R\"x(\n" 8317 "multiline raw string literal xxxxxxxxxxxxxx\n" 8318 ")x\");", 8319 getGoogleStyleWithColumns(20))); 8320 EXPECT_EQ("fffffffffff(R\"x(\n" 8321 "multiline raw string literal xxxxxxxxxxxxxx\n" 8322 ")x\" + bbbbbb);", 8323 format("fffffffffff(R\"x(\n" 8324 "multiline raw string literal xxxxxxxxxxxxxx\n" 8325 ")x\" + bbbbbb);", 8326 getGoogleStyleWithColumns(20))); 8327 EXPECT_EQ("fffffffffff(\n" 8328 " R\"x(\n" 8329 "multiline raw string literal xxxxxxxxxxxxxx\n" 8330 ")x\" +\n" 8331 " bbbbbb);", 8332 format("fffffffffff(\n" 8333 " R\"x(\n" 8334 "multiline raw string literal xxxxxxxxxxxxxx\n" 8335 ")x\" + bbbbbb);", 8336 getGoogleStyleWithColumns(20))); 8337 } 8338 8339 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8340 verifyFormat("string a = \"unterminated;"); 8341 EXPECT_EQ("function(\"unterminated,\n" 8342 " OtherParameter);", 8343 format("function( \"unterminated,\n" 8344 " OtherParameter);")); 8345 } 8346 8347 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8348 FormatStyle Style = getLLVMStyle(); 8349 Style.Standard = FormatStyle::LS_Cpp03; 8350 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8351 format("#define x(_a) printf(\"foo\"_a);", Style)); 8352 } 8353 8354 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8355 8356 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8357 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8358 " \"ddeeefff\");", 8359 format("someFunction(\"aaabbbcccdddeeefff\");", 8360 getLLVMStyleWithColumns(25))); 8361 EXPECT_EQ("someFunction1234567890(\n" 8362 " \"aaabbbcccdddeeefff\");", 8363 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8364 getLLVMStyleWithColumns(26))); 8365 EXPECT_EQ("someFunction1234567890(\n" 8366 " \"aaabbbcccdddeeeff\"\n" 8367 " \"f\");", 8368 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8369 getLLVMStyleWithColumns(25))); 8370 EXPECT_EQ("someFunction1234567890(\n" 8371 " \"aaabbbcccdddeeeff\"\n" 8372 " \"f\");", 8373 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8374 getLLVMStyleWithColumns(24))); 8375 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8376 " \"ddde \"\n" 8377 " \"efff\");", 8378 format("someFunction(\"aaabbbcc ddde efff\");", 8379 getLLVMStyleWithColumns(25))); 8380 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8381 " \"ddeeefff\");", 8382 format("someFunction(\"aaabbbccc ddeeefff\");", 8383 getLLVMStyleWithColumns(25))); 8384 EXPECT_EQ("someFunction1234567890(\n" 8385 " \"aaabb \"\n" 8386 " \"cccdddeeefff\");", 8387 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8388 getLLVMStyleWithColumns(25))); 8389 EXPECT_EQ("#define A \\\n" 8390 " string s = \\\n" 8391 " \"123456789\" \\\n" 8392 " \"0\"; \\\n" 8393 " int i;", 8394 format("#define A string s = \"1234567890\"; int i;", 8395 getLLVMStyleWithColumns(20))); 8396 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8397 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8398 " \"dddeeeff\"\n" 8399 " \"f\");", 8400 format("someFunction(\"aaabbbcc dddeeefff\");", 8401 getLLVMStyleWithColumns(25))); 8402 } 8403 8404 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8405 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8406 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8407 EXPECT_EQ("\"test\"\n" 8408 "\"\\n\"", 8409 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8410 EXPECT_EQ("\"tes\\\\\"\n" 8411 "\"n\"", 8412 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8413 EXPECT_EQ("\"\\\\\\\\\"\n" 8414 "\"\\n\"", 8415 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8416 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8417 EXPECT_EQ("\"\\uff01\"\n" 8418 "\"test\"", 8419 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8420 EXPECT_EQ("\"\\Uff01ff02\"", 8421 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8422 EXPECT_EQ("\"\\x000000000001\"\n" 8423 "\"next\"", 8424 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8425 EXPECT_EQ("\"\\x000000000001next\"", 8426 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8427 EXPECT_EQ("\"\\x000000000001\"", 8428 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8429 EXPECT_EQ("\"test\"\n" 8430 "\"\\000000\"\n" 8431 "\"000001\"", 8432 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8433 EXPECT_EQ("\"test\\000\"\n" 8434 "\"00000000\"\n" 8435 "\"1\"", 8436 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8437 } 8438 8439 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8440 verifyFormat("void f() {\n" 8441 " return g() {}\n" 8442 " void h() {}"); 8443 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8444 "g();\n" 8445 "}"); 8446 } 8447 8448 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8449 verifyFormat( 8450 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8451 } 8452 8453 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8454 verifyFormat("class X {\n" 8455 " void f() {\n" 8456 " }\n" 8457 "};", 8458 getLLVMStyleWithColumns(12)); 8459 } 8460 8461 TEST_F(FormatTest, ConfigurableIndentWidth) { 8462 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8463 EightIndent.IndentWidth = 8; 8464 EightIndent.ContinuationIndentWidth = 8; 8465 verifyFormat("void f() {\n" 8466 " someFunction();\n" 8467 " if (true) {\n" 8468 " f();\n" 8469 " }\n" 8470 "}", 8471 EightIndent); 8472 verifyFormat("class X {\n" 8473 " void f() {\n" 8474 " }\n" 8475 "};", 8476 EightIndent); 8477 verifyFormat("int x[] = {\n" 8478 " call(),\n" 8479 " call()};", 8480 EightIndent); 8481 } 8482 8483 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8484 verifyFormat("double\n" 8485 "f();", 8486 getLLVMStyleWithColumns(8)); 8487 } 8488 8489 TEST_F(FormatTest, ConfigurableUseOfTab) { 8490 FormatStyle Tab = getLLVMStyleWithColumns(42); 8491 Tab.IndentWidth = 8; 8492 Tab.UseTab = FormatStyle::UT_Always; 8493 Tab.AlignEscapedNewlinesLeft = true; 8494 8495 EXPECT_EQ("if (aaaaaaaa && // q\n" 8496 " bb)\t\t// w\n" 8497 "\t;", 8498 format("if (aaaaaaaa &&// q\n" 8499 "bb)// w\n" 8500 ";", 8501 Tab)); 8502 EXPECT_EQ("if (aaa && bbb) // w\n" 8503 "\t;", 8504 format("if(aaa&&bbb)// w\n" 8505 ";", 8506 Tab)); 8507 8508 verifyFormat("class X {\n" 8509 "\tvoid f() {\n" 8510 "\t\tsomeFunction(parameter1,\n" 8511 "\t\t\t parameter2);\n" 8512 "\t}\n" 8513 "};", 8514 Tab); 8515 verifyFormat("#define A \\\n" 8516 "\tvoid f() { \\\n" 8517 "\t\tsomeFunction( \\\n" 8518 "\t\t parameter1, \\\n" 8519 "\t\t parameter2); \\\n" 8520 "\t}", 8521 Tab); 8522 8523 Tab.TabWidth = 4; 8524 Tab.IndentWidth = 8; 8525 verifyFormat("class TabWidth4Indent8 {\n" 8526 "\t\tvoid f() {\n" 8527 "\t\t\t\tsomeFunction(parameter1,\n" 8528 "\t\t\t\t\t\t\t parameter2);\n" 8529 "\t\t}\n" 8530 "};", 8531 Tab); 8532 8533 Tab.TabWidth = 4; 8534 Tab.IndentWidth = 4; 8535 verifyFormat("class TabWidth4Indent4 {\n" 8536 "\tvoid f() {\n" 8537 "\t\tsomeFunction(parameter1,\n" 8538 "\t\t\t\t\t parameter2);\n" 8539 "\t}\n" 8540 "};", 8541 Tab); 8542 8543 Tab.TabWidth = 8; 8544 Tab.IndentWidth = 4; 8545 verifyFormat("class TabWidth8Indent4 {\n" 8546 " void f() {\n" 8547 "\tsomeFunction(parameter1,\n" 8548 "\t\t parameter2);\n" 8549 " }\n" 8550 "};", 8551 Tab); 8552 8553 Tab.TabWidth = 8; 8554 Tab.IndentWidth = 8; 8555 EXPECT_EQ("/*\n" 8556 "\t a\t\tcomment\n" 8557 "\t in multiple lines\n" 8558 " */", 8559 format(" /*\t \t \n" 8560 " \t \t a\t\tcomment\t \t\n" 8561 " \t \t in multiple lines\t\n" 8562 " \t */", 8563 Tab)); 8564 8565 Tab.UseTab = FormatStyle::UT_ForIndentation; 8566 verifyFormat("{\n" 8567 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8568 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8569 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8570 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8571 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8572 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8573 "};", 8574 Tab); 8575 verifyFormat("enum AA {\n" 8576 "\ta1, // Force multiple lines\n" 8577 "\ta2,\n" 8578 "\ta3\n" 8579 "};", 8580 Tab); 8581 EXPECT_EQ("if (aaaaaaaa && // q\n" 8582 " bb) // w\n" 8583 "\t;", 8584 format("if (aaaaaaaa &&// q\n" 8585 "bb)// w\n" 8586 ";", 8587 Tab)); 8588 verifyFormat("class X {\n" 8589 "\tvoid f() {\n" 8590 "\t\tsomeFunction(parameter1,\n" 8591 "\t\t parameter2);\n" 8592 "\t}\n" 8593 "};", 8594 Tab); 8595 verifyFormat("{\n" 8596 "\tQ(\n" 8597 "\t {\n" 8598 "\t\t int a;\n" 8599 "\t\t someFunction(aaaaaaaa,\n" 8600 "\t\t bbbbbbb);\n" 8601 "\t },\n" 8602 "\t p);\n" 8603 "}", 8604 Tab); 8605 EXPECT_EQ("{\n" 8606 "\t/* aaaa\n" 8607 "\t bbbb */\n" 8608 "}", 8609 format("{\n" 8610 "/* aaaa\n" 8611 " bbbb */\n" 8612 "}", 8613 Tab)); 8614 EXPECT_EQ("{\n" 8615 "\t/*\n" 8616 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8617 "\t bbbbbbbbbbbbb\n" 8618 "\t*/\n" 8619 "}", 8620 format("{\n" 8621 "/*\n" 8622 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8623 "*/\n" 8624 "}", 8625 Tab)); 8626 EXPECT_EQ("{\n" 8627 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8628 "\t// bbbbbbbbbbbbb\n" 8629 "}", 8630 format("{\n" 8631 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8632 "}", 8633 Tab)); 8634 EXPECT_EQ("{\n" 8635 "\t/*\n" 8636 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8637 "\t bbbbbbbbbbbbb\n" 8638 "\t*/\n" 8639 "}", 8640 format("{\n" 8641 "\t/*\n" 8642 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8643 "\t*/\n" 8644 "}", 8645 Tab)); 8646 EXPECT_EQ("{\n" 8647 "\t/*\n" 8648 "\n" 8649 "\t*/\n" 8650 "}", 8651 format("{\n" 8652 "\t/*\n" 8653 "\n" 8654 "\t*/\n" 8655 "}", 8656 Tab)); 8657 EXPECT_EQ("{\n" 8658 "\t/*\n" 8659 " asdf\n" 8660 "\t*/\n" 8661 "}", 8662 format("{\n" 8663 "\t/*\n" 8664 " asdf\n" 8665 "\t*/\n" 8666 "}", 8667 Tab)); 8668 8669 Tab.UseTab = FormatStyle::UT_Never; 8670 EXPECT_EQ("/*\n" 8671 " a\t\tcomment\n" 8672 " in multiple lines\n" 8673 " */", 8674 format(" /*\t \t \n" 8675 " \t \t a\t\tcomment\t \t\n" 8676 " \t \t in multiple lines\t\n" 8677 " \t */", 8678 Tab)); 8679 EXPECT_EQ("/* some\n" 8680 " comment */", 8681 format(" \t \t /* some\n" 8682 " \t \t comment */", 8683 Tab)); 8684 EXPECT_EQ("int a; /* some\n" 8685 " comment */", 8686 format(" \t \t int a; /* some\n" 8687 " \t \t comment */", 8688 Tab)); 8689 8690 EXPECT_EQ("int a; /* some\n" 8691 "comment */", 8692 format(" \t \t int\ta; /* some\n" 8693 " \t \t comment */", 8694 Tab)); 8695 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8696 " comment */", 8697 format(" \t \t f(\"\t\t\"); /* some\n" 8698 " \t \t comment */", 8699 Tab)); 8700 EXPECT_EQ("{\n" 8701 " /*\n" 8702 " * Comment\n" 8703 " */\n" 8704 " int i;\n" 8705 "}", 8706 format("{\n" 8707 "\t/*\n" 8708 "\t * Comment\n" 8709 "\t */\n" 8710 "\t int i;\n" 8711 "}")); 8712 8713 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8714 Tab.TabWidth = 8; 8715 Tab.IndentWidth = 8; 8716 EXPECT_EQ("if (aaaaaaaa && // q\n" 8717 " bb) // w\n" 8718 "\t;", 8719 format("if (aaaaaaaa &&// q\n" 8720 "bb)// w\n" 8721 ";", 8722 Tab)); 8723 EXPECT_EQ("if (aaa && bbb) // w\n" 8724 "\t;", 8725 format("if(aaa&&bbb)// w\n" 8726 ";", 8727 Tab)); 8728 verifyFormat("class X {\n" 8729 "\tvoid f() {\n" 8730 "\t\tsomeFunction(parameter1,\n" 8731 "\t\t\t parameter2);\n" 8732 "\t}\n" 8733 "};", 8734 Tab); 8735 verifyFormat("#define A \\\n" 8736 "\tvoid f() { \\\n" 8737 "\t\tsomeFunction( \\\n" 8738 "\t\t parameter1, \\\n" 8739 "\t\t parameter2); \\\n" 8740 "\t}", 8741 Tab); 8742 Tab.TabWidth = 4; 8743 Tab.IndentWidth = 8; 8744 verifyFormat("class TabWidth4Indent8 {\n" 8745 "\t\tvoid f() {\n" 8746 "\t\t\t\tsomeFunction(parameter1,\n" 8747 "\t\t\t\t\t\t\t parameter2);\n" 8748 "\t\t}\n" 8749 "};", 8750 Tab); 8751 Tab.TabWidth = 4; 8752 Tab.IndentWidth = 4; 8753 verifyFormat("class TabWidth4Indent4 {\n" 8754 "\tvoid f() {\n" 8755 "\t\tsomeFunction(parameter1,\n" 8756 "\t\t\t\t\t parameter2);\n" 8757 "\t}\n" 8758 "};", 8759 Tab); 8760 Tab.TabWidth = 8; 8761 Tab.IndentWidth = 4; 8762 verifyFormat("class TabWidth8Indent4 {\n" 8763 " void f() {\n" 8764 "\tsomeFunction(parameter1,\n" 8765 "\t\t parameter2);\n" 8766 " }\n" 8767 "};", 8768 Tab); 8769 Tab.TabWidth = 8; 8770 Tab.IndentWidth = 8; 8771 EXPECT_EQ("/*\n" 8772 "\t a\t\tcomment\n" 8773 "\t in multiple lines\n" 8774 " */", 8775 format(" /*\t \t \n" 8776 " \t \t a\t\tcomment\t \t\n" 8777 " \t \t in multiple lines\t\n" 8778 " \t */", 8779 Tab)); 8780 verifyFormat("{\n" 8781 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8782 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8783 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8784 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8785 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8786 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8787 "};", 8788 Tab); 8789 verifyFormat("enum AA {\n" 8790 "\ta1, // Force multiple lines\n" 8791 "\ta2,\n" 8792 "\ta3\n" 8793 "};", 8794 Tab); 8795 EXPECT_EQ("if (aaaaaaaa && // q\n" 8796 " bb) // w\n" 8797 "\t;", 8798 format("if (aaaaaaaa &&// q\n" 8799 "bb)// w\n" 8800 ";", 8801 Tab)); 8802 verifyFormat("class X {\n" 8803 "\tvoid f() {\n" 8804 "\t\tsomeFunction(parameter1,\n" 8805 "\t\t\t parameter2);\n" 8806 "\t}\n" 8807 "};", 8808 Tab); 8809 verifyFormat("{\n" 8810 "\tQ(\n" 8811 "\t {\n" 8812 "\t\t int a;\n" 8813 "\t\t someFunction(aaaaaaaa,\n" 8814 "\t\t\t\t bbbbbbb);\n" 8815 "\t },\n" 8816 "\t p);\n" 8817 "}", 8818 Tab); 8819 EXPECT_EQ("{\n" 8820 "\t/* aaaa\n" 8821 "\t bbbb */\n" 8822 "}", 8823 format("{\n" 8824 "/* aaaa\n" 8825 " bbbb */\n" 8826 "}", 8827 Tab)); 8828 EXPECT_EQ("{\n" 8829 "\t/*\n" 8830 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8831 "\t bbbbbbbbbbbbb\n" 8832 "\t*/\n" 8833 "}", 8834 format("{\n" 8835 "/*\n" 8836 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8837 "*/\n" 8838 "}", 8839 Tab)); 8840 EXPECT_EQ("{\n" 8841 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8842 "\t// bbbbbbbbbbbbb\n" 8843 "}", 8844 format("{\n" 8845 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8846 "}", 8847 Tab)); 8848 EXPECT_EQ("{\n" 8849 "\t/*\n" 8850 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8851 "\t bbbbbbbbbbbbb\n" 8852 "\t*/\n" 8853 "}", 8854 format("{\n" 8855 "\t/*\n" 8856 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8857 "\t*/\n" 8858 "}", 8859 Tab)); 8860 EXPECT_EQ("{\n" 8861 "\t/*\n" 8862 "\n" 8863 "\t*/\n" 8864 "}", 8865 format("{\n" 8866 "\t/*\n" 8867 "\n" 8868 "\t*/\n" 8869 "}", 8870 Tab)); 8871 EXPECT_EQ("{\n" 8872 "\t/*\n" 8873 " asdf\n" 8874 "\t*/\n" 8875 "}", 8876 format("{\n" 8877 "\t/*\n" 8878 " asdf\n" 8879 "\t*/\n" 8880 "}", 8881 Tab)); 8882 EXPECT_EQ("/*\n" 8883 "\t a\t\tcomment\n" 8884 "\t in multiple lines\n" 8885 " */", 8886 format(" /*\t \t \n" 8887 " \t \t a\t\tcomment\t \t\n" 8888 " \t \t in multiple lines\t\n" 8889 " \t */", 8890 Tab)); 8891 EXPECT_EQ("/* some\n" 8892 " comment */", 8893 format(" \t \t /* some\n" 8894 " \t \t comment */", 8895 Tab)); 8896 EXPECT_EQ("int a; /* some\n" 8897 " comment */", 8898 format(" \t \t int a; /* some\n" 8899 " \t \t comment */", 8900 Tab)); 8901 EXPECT_EQ("int a; /* some\n" 8902 "comment */", 8903 format(" \t \t int\ta; /* some\n" 8904 " \t \t comment */", 8905 Tab)); 8906 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8907 " comment */", 8908 format(" \t \t f(\"\t\t\"); /* some\n" 8909 " \t \t comment */", 8910 Tab)); 8911 EXPECT_EQ("{\n" 8912 " /*\n" 8913 " * Comment\n" 8914 " */\n" 8915 " int i;\n" 8916 "}", 8917 format("{\n" 8918 "\t/*\n" 8919 "\t * Comment\n" 8920 "\t */\n" 8921 "\t int i;\n" 8922 "}")); 8923 Tab.AlignConsecutiveAssignments = true; 8924 Tab.AlignConsecutiveDeclarations = true; 8925 Tab.TabWidth = 4; 8926 Tab.IndentWidth = 4; 8927 verifyFormat("class Assign {\n" 8928 "\tvoid f() {\n" 8929 "\t\tint x = 123;\n" 8930 "\t\tint random = 4;\n" 8931 "\t\tstd::string alphabet =\n" 8932 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8933 "\t}\n" 8934 "};", 8935 Tab); 8936 } 8937 8938 TEST_F(FormatTest, CalculatesOriginalColumn) { 8939 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8940 "q\"; /* some\n" 8941 " comment */", 8942 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8943 "q\"; /* some\n" 8944 " comment */", 8945 getLLVMStyle())); 8946 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8947 "/* some\n" 8948 " comment */", 8949 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8950 " /* some\n" 8951 " comment */", 8952 getLLVMStyle())); 8953 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8954 "qqq\n" 8955 "/* some\n" 8956 " comment */", 8957 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8958 "qqq\n" 8959 " /* some\n" 8960 " comment */", 8961 getLLVMStyle())); 8962 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8963 "wwww; /* some\n" 8964 " comment */", 8965 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8966 "wwww; /* some\n" 8967 " comment */", 8968 getLLVMStyle())); 8969 } 8970 8971 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8972 FormatStyle NoSpace = getLLVMStyle(); 8973 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8974 8975 verifyFormat("while(true)\n" 8976 " continue;", 8977 NoSpace); 8978 verifyFormat("for(;;)\n" 8979 " continue;", 8980 NoSpace); 8981 verifyFormat("if(true)\n" 8982 " f();\n" 8983 "else if(true)\n" 8984 " f();", 8985 NoSpace); 8986 verifyFormat("do {\n" 8987 " do_something();\n" 8988 "} while(something());", 8989 NoSpace); 8990 verifyFormat("switch(x) {\n" 8991 "default:\n" 8992 " break;\n" 8993 "}", 8994 NoSpace); 8995 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8996 verifyFormat("size_t x = sizeof(x);", NoSpace); 8997 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8998 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8999 verifyFormat("alignas(128) char a[128];", NoSpace); 9000 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9001 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9002 verifyFormat("int f() throw(Deprecated);", NoSpace); 9003 verifyFormat("typedef void (*cb)(int);", NoSpace); 9004 verifyFormat("T A::operator()();", NoSpace); 9005 verifyFormat("X A::operator++(T);", NoSpace); 9006 9007 FormatStyle Space = getLLVMStyle(); 9008 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9009 9010 verifyFormat("int f ();", Space); 9011 verifyFormat("void f (int a, T b) {\n" 9012 " while (true)\n" 9013 " continue;\n" 9014 "}", 9015 Space); 9016 verifyFormat("if (true)\n" 9017 " f ();\n" 9018 "else if (true)\n" 9019 " f ();", 9020 Space); 9021 verifyFormat("do {\n" 9022 " do_something ();\n" 9023 "} while (something ());", 9024 Space); 9025 verifyFormat("switch (x) {\n" 9026 "default:\n" 9027 " break;\n" 9028 "}", 9029 Space); 9030 verifyFormat("A::A () : a (1) {}", Space); 9031 verifyFormat("void f () __attribute__ ((asdf));", Space); 9032 verifyFormat("*(&a + 1);\n" 9033 "&((&a)[1]);\n" 9034 "a[(b + c) * d];\n" 9035 "(((a + 1) * 2) + 3) * 4;", 9036 Space); 9037 verifyFormat("#define A(x) x", Space); 9038 verifyFormat("#define A (x) x", Space); 9039 verifyFormat("#if defined(x)\n" 9040 "#endif", 9041 Space); 9042 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9043 verifyFormat("size_t x = sizeof (x);", Space); 9044 verifyFormat("auto f (int x) -> decltype (x);", Space); 9045 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9046 verifyFormat("alignas (128) char a[128];", Space); 9047 verifyFormat("size_t x = alignof (MyType);", Space); 9048 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9049 verifyFormat("int f () throw (Deprecated);", Space); 9050 verifyFormat("typedef void (*cb) (int);", Space); 9051 verifyFormat("T A::operator() ();", Space); 9052 verifyFormat("X A::operator++ (T);", Space); 9053 } 9054 9055 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9056 FormatStyle Spaces = getLLVMStyle(); 9057 9058 Spaces.SpacesInParentheses = true; 9059 verifyFormat("call( x, y, z );", Spaces); 9060 verifyFormat("call();", Spaces); 9061 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9062 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9063 Spaces); 9064 verifyFormat("while ( (bool)1 )\n" 9065 " continue;", 9066 Spaces); 9067 verifyFormat("for ( ;; )\n" 9068 " continue;", 9069 Spaces); 9070 verifyFormat("if ( true )\n" 9071 " f();\n" 9072 "else if ( true )\n" 9073 " f();", 9074 Spaces); 9075 verifyFormat("do {\n" 9076 " do_something( (int)i );\n" 9077 "} while ( something() );", 9078 Spaces); 9079 verifyFormat("switch ( x ) {\n" 9080 "default:\n" 9081 " break;\n" 9082 "}", 9083 Spaces); 9084 9085 Spaces.SpacesInParentheses = false; 9086 Spaces.SpacesInCStyleCastParentheses = true; 9087 verifyFormat("Type *A = ( Type * )P;", Spaces); 9088 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9089 verifyFormat("x = ( int32 )y;", Spaces); 9090 verifyFormat("int a = ( int )(2.0f);", Spaces); 9091 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9092 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9093 verifyFormat("#define x (( int )-1)", Spaces); 9094 9095 // Run the first set of tests again with: 9096 Spaces.SpacesInParentheses = false; 9097 Spaces.SpaceInEmptyParentheses = true; 9098 Spaces.SpacesInCStyleCastParentheses = true; 9099 verifyFormat("call(x, y, z);", Spaces); 9100 verifyFormat("call( );", Spaces); 9101 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9102 verifyFormat("while (( bool )1)\n" 9103 " continue;", 9104 Spaces); 9105 verifyFormat("for (;;)\n" 9106 " continue;", 9107 Spaces); 9108 verifyFormat("if (true)\n" 9109 " f( );\n" 9110 "else if (true)\n" 9111 " f( );", 9112 Spaces); 9113 verifyFormat("do {\n" 9114 " do_something(( int )i);\n" 9115 "} while (something( ));", 9116 Spaces); 9117 verifyFormat("switch (x) {\n" 9118 "default:\n" 9119 " break;\n" 9120 "}", 9121 Spaces); 9122 9123 // Run the first set of tests again with: 9124 Spaces.SpaceAfterCStyleCast = true; 9125 verifyFormat("call(x, y, z);", Spaces); 9126 verifyFormat("call( );", Spaces); 9127 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9128 verifyFormat("while (( bool ) 1)\n" 9129 " continue;", 9130 Spaces); 9131 verifyFormat("for (;;)\n" 9132 " continue;", 9133 Spaces); 9134 verifyFormat("if (true)\n" 9135 " f( );\n" 9136 "else if (true)\n" 9137 " f( );", 9138 Spaces); 9139 verifyFormat("do {\n" 9140 " do_something(( int ) i);\n" 9141 "} while (something( ));", 9142 Spaces); 9143 verifyFormat("switch (x) {\n" 9144 "default:\n" 9145 " break;\n" 9146 "}", 9147 Spaces); 9148 9149 // Run subset of tests again with: 9150 Spaces.SpacesInCStyleCastParentheses = false; 9151 Spaces.SpaceAfterCStyleCast = true; 9152 verifyFormat("while ((bool) 1)\n" 9153 " continue;", 9154 Spaces); 9155 verifyFormat("do {\n" 9156 " do_something((int) i);\n" 9157 "} while (something( ));", 9158 Spaces); 9159 } 9160 9161 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9162 verifyFormat("int a[5];"); 9163 verifyFormat("a[3] += 42;"); 9164 9165 FormatStyle Spaces = getLLVMStyle(); 9166 Spaces.SpacesInSquareBrackets = true; 9167 // Lambdas unchanged. 9168 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9169 verifyFormat("return [i, args...] {};", Spaces); 9170 9171 // Not lambdas. 9172 verifyFormat("int a[ 5 ];", Spaces); 9173 verifyFormat("a[ 3 ] += 42;", Spaces); 9174 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9175 verifyFormat("double &operator[](int i) { return 0; }\n" 9176 "int i;", 9177 Spaces); 9178 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9179 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9180 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9181 } 9182 9183 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9184 verifyFormat("int a = 5;"); 9185 verifyFormat("a += 42;"); 9186 verifyFormat("a or_eq 8;"); 9187 9188 FormatStyle Spaces = getLLVMStyle(); 9189 Spaces.SpaceBeforeAssignmentOperators = false; 9190 verifyFormat("int a= 5;", Spaces); 9191 verifyFormat("a+= 42;", Spaces); 9192 verifyFormat("a or_eq 8;", Spaces); 9193 } 9194 9195 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9196 FormatStyle Alignment = getLLVMStyle(); 9197 Alignment.AlignConsecutiveAssignments = false; 9198 verifyFormat("int a = 5;\n" 9199 "int oneTwoThree = 123;", 9200 Alignment); 9201 verifyFormat("int a = 5;\n" 9202 "int oneTwoThree = 123;", 9203 Alignment); 9204 9205 Alignment.AlignConsecutiveAssignments = true; 9206 verifyFormat("int a = 5;\n" 9207 "int oneTwoThree = 123;", 9208 Alignment); 9209 verifyFormat("int a = method();\n" 9210 "int oneTwoThree = 133;", 9211 Alignment); 9212 verifyFormat("a &= 5;\n" 9213 "bcd *= 5;\n" 9214 "ghtyf += 5;\n" 9215 "dvfvdb -= 5;\n" 9216 "a /= 5;\n" 9217 "vdsvsv %= 5;\n" 9218 "sfdbddfbdfbb ^= 5;\n" 9219 "dvsdsv |= 5;\n" 9220 "int dsvvdvsdvvv = 123;", 9221 Alignment); 9222 verifyFormat("int i = 1, j = 10;\n" 9223 "something = 2000;", 9224 Alignment); 9225 verifyFormat("something = 2000;\n" 9226 "int i = 1, j = 10;\n", 9227 Alignment); 9228 verifyFormat("something = 2000;\n" 9229 "another = 911;\n" 9230 "int i = 1, j = 10;\n" 9231 "oneMore = 1;\n" 9232 "i = 2;", 9233 Alignment); 9234 verifyFormat("int a = 5;\n" 9235 "int one = 1;\n" 9236 "method();\n" 9237 "int oneTwoThree = 123;\n" 9238 "int oneTwo = 12;", 9239 Alignment); 9240 verifyFormat("int oneTwoThree = 123;\n" 9241 "int oneTwo = 12;\n" 9242 "method();\n", 9243 Alignment); 9244 verifyFormat("int oneTwoThree = 123; // comment\n" 9245 "int oneTwo = 12; // comment", 9246 Alignment); 9247 EXPECT_EQ("int a = 5;\n" 9248 "\n" 9249 "int oneTwoThree = 123;", 9250 format("int a = 5;\n" 9251 "\n" 9252 "int oneTwoThree= 123;", 9253 Alignment)); 9254 EXPECT_EQ("int a = 5;\n" 9255 "int one = 1;\n" 9256 "\n" 9257 "int oneTwoThree = 123;", 9258 format("int a = 5;\n" 9259 "int one = 1;\n" 9260 "\n" 9261 "int oneTwoThree = 123;", 9262 Alignment)); 9263 EXPECT_EQ("int a = 5;\n" 9264 "int one = 1;\n" 9265 "\n" 9266 "int oneTwoThree = 123;\n" 9267 "int oneTwo = 12;", 9268 format("int a = 5;\n" 9269 "int one = 1;\n" 9270 "\n" 9271 "int oneTwoThree = 123;\n" 9272 "int oneTwo = 12;", 9273 Alignment)); 9274 Alignment.AlignEscapedNewlinesLeft = true; 9275 verifyFormat("#define A \\\n" 9276 " int aaaa = 12; \\\n" 9277 " int b = 23; \\\n" 9278 " int ccc = 234; \\\n" 9279 " int dddddddddd = 2345;", 9280 Alignment); 9281 Alignment.AlignEscapedNewlinesLeft = false; 9282 verifyFormat("#define A " 9283 " \\\n" 9284 " int aaaa = 12; " 9285 " \\\n" 9286 " int b = 23; " 9287 " \\\n" 9288 " int ccc = 234; " 9289 " \\\n" 9290 " int dddddddddd = 2345;", 9291 Alignment); 9292 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9293 "k = 4, int l = 5,\n" 9294 " int m = 6) {\n" 9295 " int j = 10;\n" 9296 " otherThing = 1;\n" 9297 "}", 9298 Alignment); 9299 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9300 " int i = 1;\n" 9301 " int j = 2;\n" 9302 " int big = 10000;\n" 9303 "}", 9304 Alignment); 9305 verifyFormat("class C {\n" 9306 "public:\n" 9307 " int i = 1;\n" 9308 " virtual void f() = 0;\n" 9309 "};", 9310 Alignment); 9311 verifyFormat("int i = 1;\n" 9312 "if (SomeType t = getSomething()) {\n" 9313 "}\n" 9314 "int j = 2;\n" 9315 "int big = 10000;", 9316 Alignment); 9317 verifyFormat("int j = 7;\n" 9318 "for (int k = 0; k < N; ++k) {\n" 9319 "}\n" 9320 "int j = 2;\n" 9321 "int big = 10000;\n" 9322 "}", 9323 Alignment); 9324 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9325 verifyFormat("int i = 1;\n" 9326 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9327 " = someLooooooooooooooooongFunction();\n" 9328 "int j = 2;", 9329 Alignment); 9330 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9331 verifyFormat("int i = 1;\n" 9332 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9333 " someLooooooooooooooooongFunction();\n" 9334 "int j = 2;", 9335 Alignment); 9336 9337 verifyFormat("auto lambda = []() {\n" 9338 " auto i = 0;\n" 9339 " return 0;\n" 9340 "};\n" 9341 "int i = 0;\n" 9342 "auto v = type{\n" 9343 " i = 1, //\n" 9344 " (i = 2), //\n" 9345 " i = 3 //\n" 9346 "};", 9347 Alignment); 9348 9349 // FIXME: Should align all three assignments 9350 verifyFormat( 9351 "int i = 1;\n" 9352 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9353 " loooooooooooooooooooooongParameterB);\n" 9354 "int j = 2;", 9355 Alignment); 9356 9357 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9358 " typename B = very_long_type_name_1,\n" 9359 " typename T_2 = very_long_type_name_2>\n" 9360 "auto foo() {}\n", 9361 Alignment); 9362 verifyFormat("int a, b = 1;\n" 9363 "int c = 2;\n" 9364 "int dd = 3;\n", 9365 Alignment); 9366 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9367 "float b[1][] = {{3.f}};\n", 9368 Alignment); 9369 } 9370 9371 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9372 FormatStyle Alignment = getLLVMStyle(); 9373 Alignment.AlignConsecutiveDeclarations = false; 9374 verifyFormat("float const a = 5;\n" 9375 "int oneTwoThree = 123;", 9376 Alignment); 9377 verifyFormat("int a = 5;\n" 9378 "float const oneTwoThree = 123;", 9379 Alignment); 9380 9381 Alignment.AlignConsecutiveDeclarations = true; 9382 verifyFormat("float const a = 5;\n" 9383 "int oneTwoThree = 123;", 9384 Alignment); 9385 verifyFormat("int a = method();\n" 9386 "float const oneTwoThree = 133;", 9387 Alignment); 9388 verifyFormat("int i = 1, j = 10;\n" 9389 "something = 2000;", 9390 Alignment); 9391 verifyFormat("something = 2000;\n" 9392 "int i = 1, j = 10;\n", 9393 Alignment); 9394 verifyFormat("float something = 2000;\n" 9395 "double another = 911;\n" 9396 "int i = 1, j = 10;\n" 9397 "const int *oneMore = 1;\n" 9398 "unsigned i = 2;", 9399 Alignment); 9400 verifyFormat("float a = 5;\n" 9401 "int one = 1;\n" 9402 "method();\n" 9403 "const double oneTwoThree = 123;\n" 9404 "const unsigned int oneTwo = 12;", 9405 Alignment); 9406 verifyFormat("int oneTwoThree{0}; // comment\n" 9407 "unsigned oneTwo; // comment", 9408 Alignment); 9409 EXPECT_EQ("float const a = 5;\n" 9410 "\n" 9411 "int oneTwoThree = 123;", 9412 format("float const a = 5;\n" 9413 "\n" 9414 "int oneTwoThree= 123;", 9415 Alignment)); 9416 EXPECT_EQ("float a = 5;\n" 9417 "int one = 1;\n" 9418 "\n" 9419 "unsigned oneTwoThree = 123;", 9420 format("float a = 5;\n" 9421 "int one = 1;\n" 9422 "\n" 9423 "unsigned oneTwoThree = 123;", 9424 Alignment)); 9425 EXPECT_EQ("float a = 5;\n" 9426 "int one = 1;\n" 9427 "\n" 9428 "unsigned oneTwoThree = 123;\n" 9429 "int oneTwo = 12;", 9430 format("float a = 5;\n" 9431 "int one = 1;\n" 9432 "\n" 9433 "unsigned oneTwoThree = 123;\n" 9434 "int oneTwo = 12;", 9435 Alignment)); 9436 Alignment.AlignConsecutiveAssignments = true; 9437 verifyFormat("float something = 2000;\n" 9438 "double another = 911;\n" 9439 "int i = 1, j = 10;\n" 9440 "const int *oneMore = 1;\n" 9441 "unsigned i = 2;", 9442 Alignment); 9443 verifyFormat("int oneTwoThree = {0}; // comment\n" 9444 "unsigned oneTwo = 0; // comment", 9445 Alignment); 9446 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9447 " int const i = 1;\n" 9448 " int * j = 2;\n" 9449 " int big = 10000;\n" 9450 "\n" 9451 " unsigned oneTwoThree = 123;\n" 9452 " int oneTwo = 12;\n" 9453 " method();\n" 9454 " float k = 2;\n" 9455 " int ll = 10000;\n" 9456 "}", 9457 format("void SomeFunction(int parameter= 0) {\n" 9458 " int const i= 1;\n" 9459 " int *j=2;\n" 9460 " int big = 10000;\n" 9461 "\n" 9462 "unsigned oneTwoThree =123;\n" 9463 "int oneTwo = 12;\n" 9464 " method();\n" 9465 "float k= 2;\n" 9466 "int ll=10000;\n" 9467 "}", 9468 Alignment)); 9469 Alignment.AlignConsecutiveAssignments = false; 9470 Alignment.AlignEscapedNewlinesLeft = true; 9471 verifyFormat("#define A \\\n" 9472 " int aaaa = 12; \\\n" 9473 " float b = 23; \\\n" 9474 " const int ccc = 234; \\\n" 9475 " unsigned dddddddddd = 2345;", 9476 Alignment); 9477 Alignment.AlignEscapedNewlinesLeft = false; 9478 Alignment.ColumnLimit = 30; 9479 verifyFormat("#define A \\\n" 9480 " int aaaa = 12; \\\n" 9481 " float b = 23; \\\n" 9482 " const int ccc = 234; \\\n" 9483 " int dddddddddd = 2345;", 9484 Alignment); 9485 Alignment.ColumnLimit = 80; 9486 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9487 "k = 4, int l = 5,\n" 9488 " int m = 6) {\n" 9489 " const int j = 10;\n" 9490 " otherThing = 1;\n" 9491 "}", 9492 Alignment); 9493 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9494 " int const i = 1;\n" 9495 " int * j = 2;\n" 9496 " int big = 10000;\n" 9497 "}", 9498 Alignment); 9499 verifyFormat("class C {\n" 9500 "public:\n" 9501 " int i = 1;\n" 9502 " virtual void f() = 0;\n" 9503 "};", 9504 Alignment); 9505 verifyFormat("float i = 1;\n" 9506 "if (SomeType t = getSomething()) {\n" 9507 "}\n" 9508 "const unsigned j = 2;\n" 9509 "int big = 10000;", 9510 Alignment); 9511 verifyFormat("float j = 7;\n" 9512 "for (int k = 0; k < N; ++k) {\n" 9513 "}\n" 9514 "unsigned j = 2;\n" 9515 "int big = 10000;\n" 9516 "}", 9517 Alignment); 9518 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9519 verifyFormat("float i = 1;\n" 9520 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9521 " = someLooooooooooooooooongFunction();\n" 9522 "int j = 2;", 9523 Alignment); 9524 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9525 verifyFormat("int i = 1;\n" 9526 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9527 " someLooooooooooooooooongFunction();\n" 9528 "int j = 2;", 9529 Alignment); 9530 9531 Alignment.AlignConsecutiveAssignments = true; 9532 verifyFormat("auto lambda = []() {\n" 9533 " auto ii = 0;\n" 9534 " float j = 0;\n" 9535 " return 0;\n" 9536 "};\n" 9537 "int i = 0;\n" 9538 "float i2 = 0;\n" 9539 "auto v = type{\n" 9540 " i = 1, //\n" 9541 " (i = 2), //\n" 9542 " i = 3 //\n" 9543 "};", 9544 Alignment); 9545 Alignment.AlignConsecutiveAssignments = false; 9546 9547 // FIXME: Should align all three declarations 9548 verifyFormat( 9549 "int i = 1;\n" 9550 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9551 " loooooooooooooooooooooongParameterB);\n" 9552 "int j = 2;", 9553 Alignment); 9554 9555 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9556 // We expect declarations and assignments to align, as long as it doesn't 9557 // exceed the column limit, starting a new alignemnt sequence whenever it 9558 // happens. 9559 Alignment.AlignConsecutiveAssignments = true; 9560 Alignment.ColumnLimit = 30; 9561 verifyFormat("float ii = 1;\n" 9562 "unsigned j = 2;\n" 9563 "int someVerylongVariable = 1;\n" 9564 "AnotherLongType ll = 123456;\n" 9565 "VeryVeryLongType k = 2;\n" 9566 "int myvar = 1;", 9567 Alignment); 9568 Alignment.ColumnLimit = 80; 9569 Alignment.AlignConsecutiveAssignments = false; 9570 9571 verifyFormat( 9572 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9573 " typename LongType, typename B>\n" 9574 "auto foo() {}\n", 9575 Alignment); 9576 verifyFormat("float a, b = 1;\n" 9577 "int c = 2;\n" 9578 "int dd = 3;\n", 9579 Alignment); 9580 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9581 "float b[1][] = {{3.f}};\n", 9582 Alignment); 9583 Alignment.AlignConsecutiveAssignments = true; 9584 verifyFormat("float a, b = 1;\n" 9585 "int c = 2;\n" 9586 "int dd = 3;\n", 9587 Alignment); 9588 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9589 "float b[1][] = {{3.f}};\n", 9590 Alignment); 9591 Alignment.AlignConsecutiveAssignments = false; 9592 9593 Alignment.ColumnLimit = 30; 9594 Alignment.BinPackParameters = false; 9595 verifyFormat("void foo(float a,\n" 9596 " float b,\n" 9597 " int c,\n" 9598 " uint32_t *d) {\n" 9599 " int * e = 0;\n" 9600 " float f = 0;\n" 9601 " double g = 0;\n" 9602 "}\n" 9603 "void bar(ino_t a,\n" 9604 " int b,\n" 9605 " uint32_t *c,\n" 9606 " bool d) {}\n", 9607 Alignment); 9608 Alignment.BinPackParameters = true; 9609 Alignment.ColumnLimit = 80; 9610 } 9611 9612 TEST_F(FormatTest, LinuxBraceBreaking) { 9613 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9614 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9615 verifyFormat("namespace a\n" 9616 "{\n" 9617 "class A\n" 9618 "{\n" 9619 " void f()\n" 9620 " {\n" 9621 " if (true) {\n" 9622 " a();\n" 9623 " b();\n" 9624 " } else {\n" 9625 " a();\n" 9626 " }\n" 9627 " }\n" 9628 " void g() { return; }\n" 9629 "};\n" 9630 "struct B {\n" 9631 " int x;\n" 9632 "};\n" 9633 "}\n", 9634 LinuxBraceStyle); 9635 verifyFormat("enum X {\n" 9636 " Y = 0,\n" 9637 "}\n", 9638 LinuxBraceStyle); 9639 verifyFormat("struct S {\n" 9640 " int Type;\n" 9641 " union {\n" 9642 " int x;\n" 9643 " double y;\n" 9644 " } Value;\n" 9645 " class C\n" 9646 " {\n" 9647 " MyFavoriteType Value;\n" 9648 " } Class;\n" 9649 "}\n", 9650 LinuxBraceStyle); 9651 } 9652 9653 TEST_F(FormatTest, MozillaBraceBreaking) { 9654 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9655 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9656 verifyFormat("namespace a {\n" 9657 "class A\n" 9658 "{\n" 9659 " void f()\n" 9660 " {\n" 9661 " if (true) {\n" 9662 " a();\n" 9663 " b();\n" 9664 " }\n" 9665 " }\n" 9666 " void g() { return; }\n" 9667 "};\n" 9668 "enum E\n" 9669 "{\n" 9670 " A,\n" 9671 " // foo\n" 9672 " B,\n" 9673 " C\n" 9674 "};\n" 9675 "struct B\n" 9676 "{\n" 9677 " int x;\n" 9678 "};\n" 9679 "}\n", 9680 MozillaBraceStyle); 9681 verifyFormat("struct S\n" 9682 "{\n" 9683 " int Type;\n" 9684 " union\n" 9685 " {\n" 9686 " int x;\n" 9687 " double y;\n" 9688 " } Value;\n" 9689 " class C\n" 9690 " {\n" 9691 " MyFavoriteType Value;\n" 9692 " } Class;\n" 9693 "}\n", 9694 MozillaBraceStyle); 9695 } 9696 9697 TEST_F(FormatTest, StroustrupBraceBreaking) { 9698 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9699 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9700 verifyFormat("namespace a {\n" 9701 "class A {\n" 9702 " void f()\n" 9703 " {\n" 9704 " if (true) {\n" 9705 " a();\n" 9706 " b();\n" 9707 " }\n" 9708 " }\n" 9709 " void g() { return; }\n" 9710 "};\n" 9711 "struct B {\n" 9712 " int x;\n" 9713 "};\n" 9714 "}\n", 9715 StroustrupBraceStyle); 9716 9717 verifyFormat("void foo()\n" 9718 "{\n" 9719 " if (a) {\n" 9720 " a();\n" 9721 " }\n" 9722 " else {\n" 9723 " b();\n" 9724 " }\n" 9725 "}\n", 9726 StroustrupBraceStyle); 9727 9728 verifyFormat("#ifdef _DEBUG\n" 9729 "int foo(int i = 0)\n" 9730 "#else\n" 9731 "int foo(int i = 5)\n" 9732 "#endif\n" 9733 "{\n" 9734 " return i;\n" 9735 "}", 9736 StroustrupBraceStyle); 9737 9738 verifyFormat("void foo() {}\n" 9739 "void bar()\n" 9740 "#ifdef _DEBUG\n" 9741 "{\n" 9742 " foo();\n" 9743 "}\n" 9744 "#else\n" 9745 "{\n" 9746 "}\n" 9747 "#endif", 9748 StroustrupBraceStyle); 9749 9750 verifyFormat("void foobar() { int i = 5; }\n" 9751 "#ifdef _DEBUG\n" 9752 "void bar() {}\n" 9753 "#else\n" 9754 "void bar() { foobar(); }\n" 9755 "#endif", 9756 StroustrupBraceStyle); 9757 } 9758 9759 TEST_F(FormatTest, AllmanBraceBreaking) { 9760 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9761 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9762 verifyFormat("namespace a\n" 9763 "{\n" 9764 "class A\n" 9765 "{\n" 9766 " void f()\n" 9767 " {\n" 9768 " if (true)\n" 9769 " {\n" 9770 " a();\n" 9771 " b();\n" 9772 " }\n" 9773 " }\n" 9774 " void g() { return; }\n" 9775 "};\n" 9776 "struct B\n" 9777 "{\n" 9778 " int x;\n" 9779 "};\n" 9780 "}", 9781 AllmanBraceStyle); 9782 9783 verifyFormat("void f()\n" 9784 "{\n" 9785 " if (true)\n" 9786 " {\n" 9787 " a();\n" 9788 " }\n" 9789 " else if (false)\n" 9790 " {\n" 9791 " b();\n" 9792 " }\n" 9793 " else\n" 9794 " {\n" 9795 " c();\n" 9796 " }\n" 9797 "}\n", 9798 AllmanBraceStyle); 9799 9800 verifyFormat("void f()\n" 9801 "{\n" 9802 " for (int i = 0; i < 10; ++i)\n" 9803 " {\n" 9804 " a();\n" 9805 " }\n" 9806 " while (false)\n" 9807 " {\n" 9808 " b();\n" 9809 " }\n" 9810 " do\n" 9811 " {\n" 9812 " c();\n" 9813 " } while (false)\n" 9814 "}\n", 9815 AllmanBraceStyle); 9816 9817 verifyFormat("void f(int a)\n" 9818 "{\n" 9819 " switch (a)\n" 9820 " {\n" 9821 " case 0:\n" 9822 " break;\n" 9823 " case 1:\n" 9824 " {\n" 9825 " break;\n" 9826 " }\n" 9827 " case 2:\n" 9828 " {\n" 9829 " }\n" 9830 " break;\n" 9831 " default:\n" 9832 " break;\n" 9833 " }\n" 9834 "}\n", 9835 AllmanBraceStyle); 9836 9837 verifyFormat("enum X\n" 9838 "{\n" 9839 " Y = 0,\n" 9840 "}\n", 9841 AllmanBraceStyle); 9842 verifyFormat("enum X\n" 9843 "{\n" 9844 " Y = 0\n" 9845 "}\n", 9846 AllmanBraceStyle); 9847 9848 verifyFormat("@interface BSApplicationController ()\n" 9849 "{\n" 9850 "@private\n" 9851 " id _extraIvar;\n" 9852 "}\n" 9853 "@end\n", 9854 AllmanBraceStyle); 9855 9856 verifyFormat("#ifdef _DEBUG\n" 9857 "int foo(int i = 0)\n" 9858 "#else\n" 9859 "int foo(int i = 5)\n" 9860 "#endif\n" 9861 "{\n" 9862 " return i;\n" 9863 "}", 9864 AllmanBraceStyle); 9865 9866 verifyFormat("void foo() {}\n" 9867 "void bar()\n" 9868 "#ifdef _DEBUG\n" 9869 "{\n" 9870 " foo();\n" 9871 "}\n" 9872 "#else\n" 9873 "{\n" 9874 "}\n" 9875 "#endif", 9876 AllmanBraceStyle); 9877 9878 verifyFormat("void foobar() { int i = 5; }\n" 9879 "#ifdef _DEBUG\n" 9880 "void bar() {}\n" 9881 "#else\n" 9882 "void bar() { foobar(); }\n" 9883 "#endif", 9884 AllmanBraceStyle); 9885 9886 // This shouldn't affect ObjC blocks.. 9887 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9888 " // ...\n" 9889 " int i;\n" 9890 "}];", 9891 AllmanBraceStyle); 9892 verifyFormat("void (^block)(void) = ^{\n" 9893 " // ...\n" 9894 " int i;\n" 9895 "};", 9896 AllmanBraceStyle); 9897 // .. or dict literals. 9898 verifyFormat("void f()\n" 9899 "{\n" 9900 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9901 "}", 9902 AllmanBraceStyle); 9903 verifyFormat("int f()\n" 9904 "{ // comment\n" 9905 " return 42;\n" 9906 "}", 9907 AllmanBraceStyle); 9908 9909 AllmanBraceStyle.ColumnLimit = 19; 9910 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9911 AllmanBraceStyle.ColumnLimit = 18; 9912 verifyFormat("void f()\n" 9913 "{\n" 9914 " int i;\n" 9915 "}", 9916 AllmanBraceStyle); 9917 AllmanBraceStyle.ColumnLimit = 80; 9918 9919 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9920 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9921 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9922 verifyFormat("void f(bool b)\n" 9923 "{\n" 9924 " if (b)\n" 9925 " {\n" 9926 " return;\n" 9927 " }\n" 9928 "}\n", 9929 BreakBeforeBraceShortIfs); 9930 verifyFormat("void f(bool b)\n" 9931 "{\n" 9932 " if (b) return;\n" 9933 "}\n", 9934 BreakBeforeBraceShortIfs); 9935 verifyFormat("void f(bool b)\n" 9936 "{\n" 9937 " while (b)\n" 9938 " {\n" 9939 " return;\n" 9940 " }\n" 9941 "}\n", 9942 BreakBeforeBraceShortIfs); 9943 } 9944 9945 TEST_F(FormatTest, GNUBraceBreaking) { 9946 FormatStyle GNUBraceStyle = getLLVMStyle(); 9947 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9948 verifyFormat("namespace a\n" 9949 "{\n" 9950 "class A\n" 9951 "{\n" 9952 " void f()\n" 9953 " {\n" 9954 " int a;\n" 9955 " {\n" 9956 " int b;\n" 9957 " }\n" 9958 " if (true)\n" 9959 " {\n" 9960 " a();\n" 9961 " b();\n" 9962 " }\n" 9963 " }\n" 9964 " void g() { return; }\n" 9965 "}\n" 9966 "}", 9967 GNUBraceStyle); 9968 9969 verifyFormat("void f()\n" 9970 "{\n" 9971 " if (true)\n" 9972 " {\n" 9973 " a();\n" 9974 " }\n" 9975 " else if (false)\n" 9976 " {\n" 9977 " b();\n" 9978 " }\n" 9979 " else\n" 9980 " {\n" 9981 " c();\n" 9982 " }\n" 9983 "}\n", 9984 GNUBraceStyle); 9985 9986 verifyFormat("void f()\n" 9987 "{\n" 9988 " for (int i = 0; i < 10; ++i)\n" 9989 " {\n" 9990 " a();\n" 9991 " }\n" 9992 " while (false)\n" 9993 " {\n" 9994 " b();\n" 9995 " }\n" 9996 " do\n" 9997 " {\n" 9998 " c();\n" 9999 " }\n" 10000 " while (false);\n" 10001 "}\n", 10002 GNUBraceStyle); 10003 10004 verifyFormat("void f(int a)\n" 10005 "{\n" 10006 " switch (a)\n" 10007 " {\n" 10008 " case 0:\n" 10009 " break;\n" 10010 " case 1:\n" 10011 " {\n" 10012 " break;\n" 10013 " }\n" 10014 " case 2:\n" 10015 " {\n" 10016 " }\n" 10017 " break;\n" 10018 " default:\n" 10019 " break;\n" 10020 " }\n" 10021 "}\n", 10022 GNUBraceStyle); 10023 10024 verifyFormat("enum X\n" 10025 "{\n" 10026 " Y = 0,\n" 10027 "}\n", 10028 GNUBraceStyle); 10029 10030 verifyFormat("@interface BSApplicationController ()\n" 10031 "{\n" 10032 "@private\n" 10033 " id _extraIvar;\n" 10034 "}\n" 10035 "@end\n", 10036 GNUBraceStyle); 10037 10038 verifyFormat("#ifdef _DEBUG\n" 10039 "int foo(int i = 0)\n" 10040 "#else\n" 10041 "int foo(int i = 5)\n" 10042 "#endif\n" 10043 "{\n" 10044 " return i;\n" 10045 "}", 10046 GNUBraceStyle); 10047 10048 verifyFormat("void foo() {}\n" 10049 "void bar()\n" 10050 "#ifdef _DEBUG\n" 10051 "{\n" 10052 " foo();\n" 10053 "}\n" 10054 "#else\n" 10055 "{\n" 10056 "}\n" 10057 "#endif", 10058 GNUBraceStyle); 10059 10060 verifyFormat("void foobar() { int i = 5; }\n" 10061 "#ifdef _DEBUG\n" 10062 "void bar() {}\n" 10063 "#else\n" 10064 "void bar() { foobar(); }\n" 10065 "#endif", 10066 GNUBraceStyle); 10067 } 10068 10069 TEST_F(FormatTest, WebKitBraceBreaking) { 10070 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10071 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10072 verifyFormat("namespace a {\n" 10073 "class A {\n" 10074 " void f()\n" 10075 " {\n" 10076 " if (true) {\n" 10077 " a();\n" 10078 " b();\n" 10079 " }\n" 10080 " }\n" 10081 " void g() { return; }\n" 10082 "};\n" 10083 "enum E {\n" 10084 " A,\n" 10085 " // foo\n" 10086 " B,\n" 10087 " C\n" 10088 "};\n" 10089 "struct B {\n" 10090 " int x;\n" 10091 "};\n" 10092 "}\n", 10093 WebKitBraceStyle); 10094 verifyFormat("struct S {\n" 10095 " int Type;\n" 10096 " union {\n" 10097 " int x;\n" 10098 " double y;\n" 10099 " } Value;\n" 10100 " class C {\n" 10101 " MyFavoriteType Value;\n" 10102 " } Class;\n" 10103 "};\n", 10104 WebKitBraceStyle); 10105 } 10106 10107 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10108 verifyFormat("void f() {\n" 10109 " try {\n" 10110 " } catch (const Exception &e) {\n" 10111 " }\n" 10112 "}\n", 10113 getLLVMStyle()); 10114 } 10115 10116 TEST_F(FormatTest, UnderstandsPragmas) { 10117 verifyFormat("#pragma omp reduction(| : var)"); 10118 verifyFormat("#pragma omp reduction(+ : var)"); 10119 10120 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10121 "(including parentheses).", 10122 format("#pragma mark Any non-hyphenated or hyphenated string " 10123 "(including parentheses).")); 10124 } 10125 10126 TEST_F(FormatTest, UnderstandPragmaOption) { 10127 verifyFormat("#pragma option -C -A"); 10128 10129 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10130 } 10131 10132 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10133 for (size_t i = 1; i < Styles.size(); ++i) \ 10134 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10135 << " differs from Style #0" 10136 10137 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10138 SmallVector<FormatStyle, 3> Styles; 10139 Styles.resize(3); 10140 10141 Styles[0] = getLLVMStyle(); 10142 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10143 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10144 EXPECT_ALL_STYLES_EQUAL(Styles); 10145 10146 Styles[0] = getGoogleStyle(); 10147 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10148 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10149 EXPECT_ALL_STYLES_EQUAL(Styles); 10150 10151 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10152 EXPECT_TRUE( 10153 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10154 EXPECT_TRUE( 10155 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10156 EXPECT_ALL_STYLES_EQUAL(Styles); 10157 10158 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10159 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10160 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10161 EXPECT_ALL_STYLES_EQUAL(Styles); 10162 10163 Styles[0] = getMozillaStyle(); 10164 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10165 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10166 EXPECT_ALL_STYLES_EQUAL(Styles); 10167 10168 Styles[0] = getWebKitStyle(); 10169 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10170 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10171 EXPECT_ALL_STYLES_EQUAL(Styles); 10172 10173 Styles[0] = getGNUStyle(); 10174 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10175 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10176 EXPECT_ALL_STYLES_EQUAL(Styles); 10177 10178 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10179 } 10180 10181 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10182 SmallVector<FormatStyle, 8> Styles; 10183 Styles.resize(2); 10184 10185 Styles[0] = getGoogleStyle(); 10186 Styles[1] = getLLVMStyle(); 10187 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10188 EXPECT_ALL_STYLES_EQUAL(Styles); 10189 10190 Styles.resize(5); 10191 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10192 Styles[1] = getLLVMStyle(); 10193 Styles[1].Language = FormatStyle::LK_JavaScript; 10194 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10195 10196 Styles[2] = getLLVMStyle(); 10197 Styles[2].Language = FormatStyle::LK_JavaScript; 10198 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10199 "BasedOnStyle: Google", 10200 &Styles[2]) 10201 .value()); 10202 10203 Styles[3] = getLLVMStyle(); 10204 Styles[3].Language = FormatStyle::LK_JavaScript; 10205 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10206 "Language: JavaScript", 10207 &Styles[3]) 10208 .value()); 10209 10210 Styles[4] = getLLVMStyle(); 10211 Styles[4].Language = FormatStyle::LK_JavaScript; 10212 EXPECT_EQ(0, parseConfiguration("---\n" 10213 "BasedOnStyle: LLVM\n" 10214 "IndentWidth: 123\n" 10215 "---\n" 10216 "BasedOnStyle: Google\n" 10217 "Language: JavaScript", 10218 &Styles[4]) 10219 .value()); 10220 EXPECT_ALL_STYLES_EQUAL(Styles); 10221 } 10222 10223 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10224 Style.FIELD = false; \ 10225 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10226 EXPECT_TRUE(Style.FIELD); \ 10227 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10228 EXPECT_FALSE(Style.FIELD); 10229 10230 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10231 10232 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10233 Style.STRUCT.FIELD = false; \ 10234 EXPECT_EQ(0, \ 10235 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10236 .value()); \ 10237 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10238 EXPECT_EQ(0, \ 10239 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10240 .value()); \ 10241 EXPECT_FALSE(Style.STRUCT.FIELD); 10242 10243 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10244 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10245 10246 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10247 EXPECT_NE(VALUE, Style.FIELD); \ 10248 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10249 EXPECT_EQ(VALUE, Style.FIELD) 10250 10251 TEST_F(FormatTest, ParsesConfigurationBools) { 10252 FormatStyle Style = {}; 10253 Style.Language = FormatStyle::LK_Cpp; 10254 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 10255 CHECK_PARSE_BOOL(AlignOperands); 10256 CHECK_PARSE_BOOL(AlignTrailingComments); 10257 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10258 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10259 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10260 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10261 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10262 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10263 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10264 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10265 CHECK_PARSE_BOOL(BinPackArguments); 10266 CHECK_PARSE_BOOL(BinPackParameters); 10267 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10268 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10269 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 10270 CHECK_PARSE_BOOL(BreakStringLiterals); 10271 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10272 CHECK_PARSE_BOOL(DerivePointerAlignment); 10273 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10274 CHECK_PARSE_BOOL(DisableFormat); 10275 CHECK_PARSE_BOOL(IndentCaseLabels); 10276 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10277 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10278 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10279 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10280 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10281 CHECK_PARSE_BOOL(ReflowComments); 10282 CHECK_PARSE_BOOL(SortIncludes); 10283 CHECK_PARSE_BOOL(SpacesInParentheses); 10284 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10285 CHECK_PARSE_BOOL(SpacesInAngles); 10286 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10287 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10288 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10289 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10290 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10291 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10292 10293 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10294 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10295 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10296 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10297 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10298 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10299 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10300 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10301 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10302 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10303 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10304 } 10305 10306 #undef CHECK_PARSE_BOOL 10307 10308 TEST_F(FormatTest, ParsesConfiguration) { 10309 FormatStyle Style = {}; 10310 Style.Language = FormatStyle::LK_Cpp; 10311 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10312 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10313 ConstructorInitializerIndentWidth, 1234u); 10314 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10315 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10316 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10317 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10318 PenaltyBreakBeforeFirstCallParameter, 1234u); 10319 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10320 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10321 PenaltyReturnTypeOnItsOwnLine, 1234u); 10322 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10323 SpacesBeforeTrailingComments, 1234u); 10324 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10325 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10326 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10327 10328 Style.PointerAlignment = FormatStyle::PAS_Middle; 10329 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10330 FormatStyle::PAS_Left); 10331 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10332 FormatStyle::PAS_Right); 10333 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10334 FormatStyle::PAS_Middle); 10335 // For backward compatibility: 10336 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10337 FormatStyle::PAS_Left); 10338 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10339 FormatStyle::PAS_Right); 10340 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10341 FormatStyle::PAS_Middle); 10342 10343 Style.Standard = FormatStyle::LS_Auto; 10344 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10345 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10346 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10347 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10348 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10349 10350 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10351 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10352 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10353 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10354 FormatStyle::BOS_None); 10355 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10356 FormatStyle::BOS_All); 10357 // For backward compatibility: 10358 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10359 FormatStyle::BOS_None); 10360 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10361 FormatStyle::BOS_All); 10362 10363 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10364 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10365 FormatStyle::BAS_Align); 10366 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10367 FormatStyle::BAS_DontAlign); 10368 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10369 FormatStyle::BAS_AlwaysBreak); 10370 // For backward compatibility: 10371 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10372 FormatStyle::BAS_DontAlign); 10373 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10374 FormatStyle::BAS_Align); 10375 10376 Style.UseTab = FormatStyle::UT_ForIndentation; 10377 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10378 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10379 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10380 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10381 FormatStyle::UT_ForContinuationAndIndentation); 10382 // For backward compatibility: 10383 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10384 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10385 10386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10387 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10388 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10389 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10390 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10391 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10392 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10393 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10394 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10395 // For backward compatibility: 10396 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10397 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10398 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10399 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10400 10401 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10402 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10403 FormatStyle::SBPO_Never); 10404 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10405 FormatStyle::SBPO_Always); 10406 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10407 FormatStyle::SBPO_ControlStatements); 10408 // For backward compatibility: 10409 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10410 FormatStyle::SBPO_Never); 10411 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10412 FormatStyle::SBPO_ControlStatements); 10413 10414 Style.ColumnLimit = 123; 10415 FormatStyle BaseStyle = getLLVMStyle(); 10416 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10417 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10418 10419 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10420 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10421 FormatStyle::BS_Attach); 10422 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10423 FormatStyle::BS_Linux); 10424 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10425 FormatStyle::BS_Mozilla); 10426 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10427 FormatStyle::BS_Stroustrup); 10428 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10429 FormatStyle::BS_Allman); 10430 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10431 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10432 FormatStyle::BS_WebKit); 10433 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10434 FormatStyle::BS_Custom); 10435 10436 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10437 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10438 FormatStyle::RTBS_None); 10439 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10440 FormatStyle::RTBS_All); 10441 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10442 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10443 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10444 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10445 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10446 AlwaysBreakAfterReturnType, 10447 FormatStyle::RTBS_TopLevelDefinitions); 10448 10449 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10450 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10451 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10452 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10453 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10454 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10455 AlwaysBreakAfterDefinitionReturnType, 10456 FormatStyle::DRTBS_TopLevel); 10457 10458 Style.NamespaceIndentation = FormatStyle::NI_All; 10459 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10460 FormatStyle::NI_None); 10461 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10462 FormatStyle::NI_Inner); 10463 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10464 FormatStyle::NI_All); 10465 10466 // FIXME: This is required because parsing a configuration simply overwrites 10467 // the first N elements of the list instead of resetting it. 10468 Style.ForEachMacros.clear(); 10469 std::vector<std::string> BoostForeach; 10470 BoostForeach.push_back("BOOST_FOREACH"); 10471 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10472 std::vector<std::string> BoostAndQForeach; 10473 BoostAndQForeach.push_back("BOOST_FOREACH"); 10474 BoostAndQForeach.push_back("Q_FOREACH"); 10475 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10476 BoostAndQForeach); 10477 10478 Style.IncludeCategories.clear(); 10479 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10480 {".*", 1}}; 10481 CHECK_PARSE("IncludeCategories:\n" 10482 " - Regex: abc/.*\n" 10483 " Priority: 2\n" 10484 " - Regex: .*\n" 10485 " Priority: 1", 10486 IncludeCategories, ExpectedCategories); 10487 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10488 } 10489 10490 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10491 FormatStyle Style = {}; 10492 Style.Language = FormatStyle::LK_Cpp; 10493 CHECK_PARSE("Language: Cpp\n" 10494 "IndentWidth: 12", 10495 IndentWidth, 12u); 10496 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10497 "IndentWidth: 34", 10498 &Style), 10499 ParseError::Unsuitable); 10500 EXPECT_EQ(12u, Style.IndentWidth); 10501 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10502 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10503 10504 Style.Language = FormatStyle::LK_JavaScript; 10505 CHECK_PARSE("Language: JavaScript\n" 10506 "IndentWidth: 12", 10507 IndentWidth, 12u); 10508 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10509 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10510 "IndentWidth: 34", 10511 &Style), 10512 ParseError::Unsuitable); 10513 EXPECT_EQ(23u, Style.IndentWidth); 10514 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10515 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10516 10517 CHECK_PARSE("BasedOnStyle: LLVM\n" 10518 "IndentWidth: 67", 10519 IndentWidth, 67u); 10520 10521 CHECK_PARSE("---\n" 10522 "Language: JavaScript\n" 10523 "IndentWidth: 12\n" 10524 "---\n" 10525 "Language: Cpp\n" 10526 "IndentWidth: 34\n" 10527 "...\n", 10528 IndentWidth, 12u); 10529 10530 Style.Language = FormatStyle::LK_Cpp; 10531 CHECK_PARSE("---\n" 10532 "Language: JavaScript\n" 10533 "IndentWidth: 12\n" 10534 "---\n" 10535 "Language: Cpp\n" 10536 "IndentWidth: 34\n" 10537 "...\n", 10538 IndentWidth, 34u); 10539 CHECK_PARSE("---\n" 10540 "IndentWidth: 78\n" 10541 "---\n" 10542 "Language: JavaScript\n" 10543 "IndentWidth: 56\n" 10544 "...\n", 10545 IndentWidth, 78u); 10546 10547 Style.ColumnLimit = 123; 10548 Style.IndentWidth = 234; 10549 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10550 Style.TabWidth = 345; 10551 EXPECT_FALSE(parseConfiguration("---\n" 10552 "IndentWidth: 456\n" 10553 "BreakBeforeBraces: Allman\n" 10554 "---\n" 10555 "Language: JavaScript\n" 10556 "IndentWidth: 111\n" 10557 "TabWidth: 111\n" 10558 "---\n" 10559 "Language: Cpp\n" 10560 "BreakBeforeBraces: Stroustrup\n" 10561 "TabWidth: 789\n" 10562 "...\n", 10563 &Style)); 10564 EXPECT_EQ(123u, Style.ColumnLimit); 10565 EXPECT_EQ(456u, Style.IndentWidth); 10566 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10567 EXPECT_EQ(789u, Style.TabWidth); 10568 10569 EXPECT_EQ(parseConfiguration("---\n" 10570 "Language: JavaScript\n" 10571 "IndentWidth: 56\n" 10572 "---\n" 10573 "IndentWidth: 78\n" 10574 "...\n", 10575 &Style), 10576 ParseError::Error); 10577 EXPECT_EQ(parseConfiguration("---\n" 10578 "Language: JavaScript\n" 10579 "IndentWidth: 56\n" 10580 "---\n" 10581 "Language: JavaScript\n" 10582 "IndentWidth: 78\n" 10583 "...\n", 10584 &Style), 10585 ParseError::Error); 10586 10587 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10588 } 10589 10590 #undef CHECK_PARSE 10591 10592 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10593 FormatStyle Style = {}; 10594 Style.Language = FormatStyle::LK_JavaScript; 10595 Style.BreakBeforeTernaryOperators = true; 10596 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10597 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10598 10599 Style.BreakBeforeTernaryOperators = true; 10600 EXPECT_EQ(0, parseConfiguration("---\n" 10601 "BasedOnStyle: Google\n" 10602 "---\n" 10603 "Language: JavaScript\n" 10604 "IndentWidth: 76\n" 10605 "...\n", 10606 &Style) 10607 .value()); 10608 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10609 EXPECT_EQ(76u, Style.IndentWidth); 10610 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10611 } 10612 10613 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10614 FormatStyle Style = getLLVMStyle(); 10615 std::string YAML = configurationAsText(Style); 10616 FormatStyle ParsedStyle = {}; 10617 ParsedStyle.Language = FormatStyle::LK_Cpp; 10618 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10619 EXPECT_EQ(Style, ParsedStyle); 10620 } 10621 10622 TEST_F(FormatTest, WorksFor8bitEncodings) { 10623 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10624 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10625 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10626 "\"\xef\xee\xf0\xf3...\"", 10627 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10628 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10629 "\xef\xee\xf0\xf3...\"", 10630 getLLVMStyleWithColumns(12))); 10631 } 10632 10633 TEST_F(FormatTest, HandlesUTF8BOM) { 10634 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10635 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10636 format("\xef\xbb\xbf#include <iostream>")); 10637 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10638 format("\xef\xbb\xbf\n#include <iostream>")); 10639 } 10640 10641 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10642 #if !defined(_MSC_VER) 10643 10644 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10645 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10646 getLLVMStyleWithColumns(35)); 10647 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10648 getLLVMStyleWithColumns(31)); 10649 verifyFormat("// Однажды в студёную зимнюю пору...", 10650 getLLVMStyleWithColumns(36)); 10651 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10652 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10653 getLLVMStyleWithColumns(39)); 10654 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10655 getLLVMStyleWithColumns(35)); 10656 } 10657 10658 TEST_F(FormatTest, SplitsUTF8Strings) { 10659 // Non-printable characters' width is currently considered to be the length in 10660 // bytes in UTF8. The characters can be displayed in very different manner 10661 // (zero-width, single width with a substitution glyph, expanded to their code 10662 // (e.g. "<8d>"), so there's no single correct way to handle them. 10663 EXPECT_EQ("\"aaaaÄ\"\n" 10664 "\"\xc2\x8d\";", 10665 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10666 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10667 "\"\xc2\x8d\";", 10668 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10669 EXPECT_EQ("\"Однажды, в \"\n" 10670 "\"студёную \"\n" 10671 "\"зимнюю \"\n" 10672 "\"пору,\"", 10673 format("\"Однажды, в студёную зимнюю пору,\"", 10674 getLLVMStyleWithColumns(13))); 10675 EXPECT_EQ( 10676 "\"一 二 三 \"\n" 10677 "\"四 五六 \"\n" 10678 "\"七 八 九 \"\n" 10679 "\"十\"", 10680 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10681 EXPECT_EQ("\"一\t二 \"\n" 10682 "\"\t三 \"\n" 10683 "\"四 五\t六 \"\n" 10684 "\"\t七 \"\n" 10685 "\"八九十\tqq\"", 10686 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10687 getLLVMStyleWithColumns(11))); 10688 10689 // UTF8 character in an escape sequence. 10690 EXPECT_EQ("\"aaaaaa\"\n" 10691 "\"\\\xC2\x8D\"", 10692 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10693 } 10694 10695 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10696 EXPECT_EQ("const char *sssss =\n" 10697 " \"一二三四五六七八\\\n" 10698 " 九 十\";", 10699 format("const char *sssss = \"一二三四五六七八\\\n" 10700 " 九 十\";", 10701 getLLVMStyleWithColumns(30))); 10702 } 10703 10704 TEST_F(FormatTest, SplitsUTF8LineComments) { 10705 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10706 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10707 EXPECT_EQ("// Я из лесу\n" 10708 "// вышел; был\n" 10709 "// сильный\n" 10710 "// мороз.", 10711 format("// Я из лесу вышел; был сильный мороз.", 10712 getLLVMStyleWithColumns(13))); 10713 EXPECT_EQ("// 一二三\n" 10714 "// 四五六七\n" 10715 "// 八 九\n" 10716 "// 十", 10717 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10718 } 10719 10720 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10721 EXPECT_EQ("/* Гляжу,\n" 10722 " * поднимается\n" 10723 " * медленно в\n" 10724 " * гору\n" 10725 " * Лошадка,\n" 10726 " * везущая\n" 10727 " * хворосту\n" 10728 " * воз. */", 10729 format("/* Гляжу, поднимается медленно в гору\n" 10730 " * Лошадка, везущая хворосту воз. */", 10731 getLLVMStyleWithColumns(13))); 10732 EXPECT_EQ( 10733 "/* 一二三\n" 10734 " * 四五六七\n" 10735 " * 八 九\n" 10736 " * 十 */", 10737 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10738 EXPECT_EQ("/* \n" 10739 " * \n" 10740 " * - */", 10741 format("/* - */", getLLVMStyleWithColumns(12))); 10742 } 10743 10744 #endif // _MSC_VER 10745 10746 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10747 FormatStyle Style = getLLVMStyle(); 10748 10749 Style.ConstructorInitializerIndentWidth = 4; 10750 verifyFormat( 10751 "SomeClass::Constructor()\n" 10752 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10753 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10754 Style); 10755 10756 Style.ConstructorInitializerIndentWidth = 2; 10757 verifyFormat( 10758 "SomeClass::Constructor()\n" 10759 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10760 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10761 Style); 10762 10763 Style.ConstructorInitializerIndentWidth = 0; 10764 verifyFormat( 10765 "SomeClass::Constructor()\n" 10766 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10767 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10768 Style); 10769 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10770 verifyFormat( 10771 "SomeLongTemplateVariableName<\n" 10772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10773 Style); 10774 verifyFormat( 10775 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10777 Style); 10778 } 10779 10780 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10781 FormatStyle Style = getLLVMStyle(); 10782 Style.BreakConstructorInitializersBeforeComma = true; 10783 Style.ConstructorInitializerIndentWidth = 4; 10784 verifyFormat("SomeClass::Constructor()\n" 10785 " : a(a)\n" 10786 " , b(b)\n" 10787 " , c(c) {}", 10788 Style); 10789 verifyFormat("SomeClass::Constructor()\n" 10790 " : a(a) {}", 10791 Style); 10792 10793 Style.ColumnLimit = 0; 10794 verifyFormat("SomeClass::Constructor()\n" 10795 " : a(a) {}", 10796 Style); 10797 verifyFormat("SomeClass::Constructor() noexcept\n" 10798 " : a(a) {}", 10799 Style); 10800 verifyFormat("SomeClass::Constructor()\n" 10801 " : a(a)\n" 10802 " , b(b)\n" 10803 " , c(c) {}", 10804 Style); 10805 verifyFormat("SomeClass::Constructor()\n" 10806 " : a(a) {\n" 10807 " foo();\n" 10808 " bar();\n" 10809 "}", 10810 Style); 10811 10812 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10813 verifyFormat("SomeClass::Constructor()\n" 10814 " : a(a)\n" 10815 " , b(b)\n" 10816 " , c(c) {\n}", 10817 Style); 10818 verifyFormat("SomeClass::Constructor()\n" 10819 " : a(a) {\n}", 10820 Style); 10821 10822 Style.ColumnLimit = 80; 10823 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10824 Style.ConstructorInitializerIndentWidth = 2; 10825 verifyFormat("SomeClass::Constructor()\n" 10826 " : a(a)\n" 10827 " , b(b)\n" 10828 " , c(c) {}", 10829 Style); 10830 10831 Style.ConstructorInitializerIndentWidth = 0; 10832 verifyFormat("SomeClass::Constructor()\n" 10833 ": a(a)\n" 10834 ", b(b)\n" 10835 ", c(c) {}", 10836 Style); 10837 10838 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10839 Style.ConstructorInitializerIndentWidth = 4; 10840 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10841 verifyFormat( 10842 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10843 Style); 10844 verifyFormat( 10845 "SomeClass::Constructor()\n" 10846 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10847 Style); 10848 Style.ConstructorInitializerIndentWidth = 4; 10849 Style.ColumnLimit = 60; 10850 verifyFormat("SomeClass::Constructor()\n" 10851 " : aaaaaaaa(aaaaaaaa)\n" 10852 " , aaaaaaaa(aaaaaaaa)\n" 10853 " , aaaaaaaa(aaaaaaaa) {}", 10854 Style); 10855 } 10856 10857 TEST_F(FormatTest, Destructors) { 10858 verifyFormat("void F(int &i) { i.~int(); }"); 10859 verifyFormat("void F(int &i) { i->~int(); }"); 10860 } 10861 10862 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10863 FormatStyle Style = getWebKitStyle(); 10864 10865 // Don't indent in outer namespaces. 10866 verifyFormat("namespace outer {\n" 10867 "int i;\n" 10868 "namespace inner {\n" 10869 " int i;\n" 10870 "} // namespace inner\n" 10871 "} // namespace outer\n" 10872 "namespace other_outer {\n" 10873 "int i;\n" 10874 "}", 10875 Style); 10876 10877 // Don't indent case labels. 10878 verifyFormat("switch (variable) {\n" 10879 "case 1:\n" 10880 "case 2:\n" 10881 " doSomething();\n" 10882 " break;\n" 10883 "default:\n" 10884 " ++variable;\n" 10885 "}", 10886 Style); 10887 10888 // Wrap before binary operators. 10889 EXPECT_EQ("void f()\n" 10890 "{\n" 10891 " if (aaaaaaaaaaaaaaaa\n" 10892 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10893 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10894 " return;\n" 10895 "}", 10896 format("void f() {\n" 10897 "if (aaaaaaaaaaaaaaaa\n" 10898 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10899 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10900 "return;\n" 10901 "}", 10902 Style)); 10903 10904 // Allow functions on a single line. 10905 verifyFormat("void f() { return; }", Style); 10906 10907 // Constructor initializers are formatted one per line with the "," on the 10908 // new line. 10909 verifyFormat("Constructor()\n" 10910 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10911 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10912 " aaaaaaaaaaaaaa)\n" 10913 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10914 "{\n" 10915 "}", 10916 Style); 10917 verifyFormat("SomeClass::Constructor()\n" 10918 " : a(a)\n" 10919 "{\n" 10920 "}", 10921 Style); 10922 EXPECT_EQ("SomeClass::Constructor()\n" 10923 " : a(a)\n" 10924 "{\n" 10925 "}", 10926 format("SomeClass::Constructor():a(a){}", Style)); 10927 verifyFormat("SomeClass::Constructor()\n" 10928 " : a(a)\n" 10929 " , b(b)\n" 10930 " , c(c)\n" 10931 "{\n" 10932 "}", 10933 Style); 10934 verifyFormat("SomeClass::Constructor()\n" 10935 " : a(a)\n" 10936 "{\n" 10937 " foo();\n" 10938 " bar();\n" 10939 "}", 10940 Style); 10941 10942 // Access specifiers should be aligned left. 10943 verifyFormat("class C {\n" 10944 "public:\n" 10945 " int i;\n" 10946 "};", 10947 Style); 10948 10949 // Do not align comments. 10950 verifyFormat("int a; // Do not\n" 10951 "double b; // align comments.", 10952 Style); 10953 10954 // Do not align operands. 10955 EXPECT_EQ("ASSERT(aaaa\n" 10956 " || bbbb);", 10957 format("ASSERT ( aaaa\n||bbbb);", Style)); 10958 10959 // Accept input's line breaks. 10960 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10961 " || bbbbbbbbbbbbbbb) {\n" 10962 " i++;\n" 10963 "}", 10964 format("if (aaaaaaaaaaaaaaa\n" 10965 "|| bbbbbbbbbbbbbbb) { i++; }", 10966 Style)); 10967 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10968 " i++;\n" 10969 "}", 10970 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10971 10972 // Don't automatically break all macro definitions (llvm.org/PR17842). 10973 verifyFormat("#define aNumber 10", Style); 10974 // However, generally keep the line breaks that the user authored. 10975 EXPECT_EQ("#define aNumber \\\n" 10976 " 10", 10977 format("#define aNumber \\\n" 10978 " 10", 10979 Style)); 10980 10981 // Keep empty and one-element array literals on a single line. 10982 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10983 " copyItems:YES];", 10984 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10985 "copyItems:YES];", 10986 Style)); 10987 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10988 " copyItems:YES];", 10989 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10990 " copyItems:YES];", 10991 Style)); 10992 // FIXME: This does not seem right, there should be more indentation before 10993 // the array literal's entries. Nested blocks have the same problem. 10994 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10995 " @\"a\",\n" 10996 " @\"a\"\n" 10997 "]\n" 10998 " copyItems:YES];", 10999 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11000 " @\"a\",\n" 11001 " @\"a\"\n" 11002 " ]\n" 11003 " copyItems:YES];", 11004 Style)); 11005 EXPECT_EQ( 11006 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11007 " copyItems:YES];", 11008 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11009 " copyItems:YES];", 11010 Style)); 11011 11012 verifyFormat("[self.a b:c c:d];", Style); 11013 EXPECT_EQ("[self.a b:c\n" 11014 " c:d];", 11015 format("[self.a b:c\n" 11016 "c:d];", 11017 Style)); 11018 } 11019 11020 TEST_F(FormatTest, FormatsLambdas) { 11021 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11022 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11023 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11024 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11025 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11026 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11027 verifyFormat("int x = f(*+[] {});"); 11028 verifyFormat("void f() {\n" 11029 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11030 "}\n"); 11031 verifyFormat("void f() {\n" 11032 " other(x.begin(), //\n" 11033 " x.end(), //\n" 11034 " [&](int, int) { return 1; });\n" 11035 "}\n"); 11036 verifyFormat("SomeFunction([]() { // A cool function...\n" 11037 " return 43;\n" 11038 "});"); 11039 EXPECT_EQ("SomeFunction([]() {\n" 11040 "#define A a\n" 11041 " return 43;\n" 11042 "});", 11043 format("SomeFunction([](){\n" 11044 "#define A a\n" 11045 "return 43;\n" 11046 "});")); 11047 verifyFormat("void f() {\n" 11048 " SomeFunction([](decltype(x), A *a) {});\n" 11049 "}"); 11050 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11051 " [](const aaaaaaaaaa &a) { return a; });"); 11052 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11053 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11054 "});"); 11055 verifyFormat("Constructor()\n" 11056 " : Field([] { // comment\n" 11057 " int i;\n" 11058 " }) {}"); 11059 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11060 " return some_parameter.size();\n" 11061 "};"); 11062 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11063 " [](const string &s) { return s; };"); 11064 verifyFormat("int i = aaaaaa ? 1 //\n" 11065 " : [] {\n" 11066 " return 2; //\n" 11067 " }();"); 11068 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11069 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11070 " return x == 2; // force break\n" 11071 " });"); 11072 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 11073 " int iiiiiiiiiiii) {\n" 11074 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 11075 "});", 11076 getLLVMStyleWithColumns(60)); 11077 verifyFormat("SomeFunction({[&] {\n" 11078 " // comment\n" 11079 " },\n" 11080 " [&] {\n" 11081 " // comment\n" 11082 " }});"); 11083 verifyFormat("SomeFunction({[&] {\n" 11084 " // comment\n" 11085 "}});"); 11086 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11087 " [&]() { return true; },\n" 11088 " aaaaa aaaaaaaaa);"); 11089 11090 // Lambdas with return types. 11091 verifyFormat("int c = []() -> int { return 2; }();\n"); 11092 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11093 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11094 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11095 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11096 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11097 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11098 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11099 verifyFormat("[a, a]() -> a<1> {};"); 11100 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11101 " int j) -> int {\n" 11102 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11103 "};"); 11104 verifyFormat( 11105 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11106 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11107 " return aaaaaaaaaaaaaaaaa;\n" 11108 " });", 11109 getLLVMStyleWithColumns(70)); 11110 verifyFormat("[]() //\n" 11111 " -> int {\n" 11112 " return 1; //\n" 11113 "};"); 11114 11115 // Multiple lambdas in the same parentheses change indentation rules. 11116 verifyFormat("SomeFunction(\n" 11117 " []() {\n" 11118 " int i = 42;\n" 11119 " return i;\n" 11120 " },\n" 11121 " []() {\n" 11122 " int j = 43;\n" 11123 " return j;\n" 11124 " });"); 11125 11126 // More complex introducers. 11127 verifyFormat("return [i, args...] {};"); 11128 11129 // Not lambdas. 11130 verifyFormat("constexpr char hello[]{\"hello\"};"); 11131 verifyFormat("double &operator[](int i) { return 0; }\n" 11132 "int i;"); 11133 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11134 verifyFormat("int i = a[a][a]->f();"); 11135 verifyFormat("int i = (*b)[a]->f();"); 11136 11137 // Other corner cases. 11138 verifyFormat("void f() {\n" 11139 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11140 " );\n" 11141 "}"); 11142 11143 // Lambdas created through weird macros. 11144 verifyFormat("void f() {\n" 11145 " MACRO((const AA &a) { return 1; });\n" 11146 " MACRO((AA &a) { return 1; });\n" 11147 "}"); 11148 11149 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11150 " doo_dah();\n" 11151 " doo_dah();\n" 11152 " })) {\n" 11153 "}"); 11154 verifyFormat("auto lambda = []() {\n" 11155 " int a = 2\n" 11156 "#if A\n" 11157 " + 2\n" 11158 "#endif\n" 11159 " ;\n" 11160 "};"); 11161 } 11162 11163 TEST_F(FormatTest, FormatsBlocks) { 11164 FormatStyle ShortBlocks = getLLVMStyle(); 11165 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11166 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11167 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11168 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11169 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11170 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11171 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11172 11173 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11174 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11175 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11176 11177 verifyFormat("[operation setCompletionBlock:^{\n" 11178 " [self onOperationDone];\n" 11179 "}];"); 11180 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11181 " [self onOperationDone];\n" 11182 "}]};"); 11183 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11184 " f();\n" 11185 "}];"); 11186 verifyFormat("int a = [operation block:^int(int *i) {\n" 11187 " return 1;\n" 11188 "}];"); 11189 verifyFormat("[myObject doSomethingWith:arg1\n" 11190 " aaa:^int(int *a) {\n" 11191 " return 1;\n" 11192 " }\n" 11193 " bbb:f(a * bbbbbbbb)];"); 11194 11195 verifyFormat("[operation setCompletionBlock:^{\n" 11196 " [self.delegate newDataAvailable];\n" 11197 "}];", 11198 getLLVMStyleWithColumns(60)); 11199 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11200 " NSString *path = [self sessionFilePath];\n" 11201 " if (path) {\n" 11202 " // ...\n" 11203 " }\n" 11204 "});"); 11205 verifyFormat("[[SessionService sharedService]\n" 11206 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11207 " if (window) {\n" 11208 " [self windowDidLoad:window];\n" 11209 " } else {\n" 11210 " [self errorLoadingWindow];\n" 11211 " }\n" 11212 " }];"); 11213 verifyFormat("void (^largeBlock)(void) = ^{\n" 11214 " // ...\n" 11215 "};\n", 11216 getLLVMStyleWithColumns(40)); 11217 verifyFormat("[[SessionService sharedService]\n" 11218 " loadWindowWithCompletionBlock: //\n" 11219 " ^(SessionWindow *window) {\n" 11220 " if (window) {\n" 11221 " [self windowDidLoad:window];\n" 11222 " } else {\n" 11223 " [self errorLoadingWindow];\n" 11224 " }\n" 11225 " }];", 11226 getLLVMStyleWithColumns(60)); 11227 verifyFormat("[myObject doSomethingWith:arg1\n" 11228 " firstBlock:^(Foo *a) {\n" 11229 " // ...\n" 11230 " int i;\n" 11231 " }\n" 11232 " secondBlock:^(Bar *b) {\n" 11233 " // ...\n" 11234 " int i;\n" 11235 " }\n" 11236 " thirdBlock:^Foo(Bar *b) {\n" 11237 " // ...\n" 11238 " int i;\n" 11239 " }];"); 11240 verifyFormat("[myObject doSomethingWith:arg1\n" 11241 " firstBlock:-1\n" 11242 " secondBlock:^(Bar *b) {\n" 11243 " // ...\n" 11244 " int i;\n" 11245 " }];"); 11246 11247 verifyFormat("f(^{\n" 11248 " @autoreleasepool {\n" 11249 " if (a) {\n" 11250 " g();\n" 11251 " }\n" 11252 " }\n" 11253 "});"); 11254 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11255 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11256 "};"); 11257 11258 FormatStyle FourIndent = getLLVMStyle(); 11259 FourIndent.ObjCBlockIndentWidth = 4; 11260 verifyFormat("[operation setCompletionBlock:^{\n" 11261 " [self onOperationDone];\n" 11262 "}];", 11263 FourIndent); 11264 } 11265 11266 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11267 FormatStyle ZeroColumn = getLLVMStyle(); 11268 ZeroColumn.ColumnLimit = 0; 11269 11270 verifyFormat("[[SessionService sharedService] " 11271 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11272 " if (window) {\n" 11273 " [self windowDidLoad:window];\n" 11274 " } else {\n" 11275 " [self errorLoadingWindow];\n" 11276 " }\n" 11277 "}];", 11278 ZeroColumn); 11279 EXPECT_EQ("[[SessionService sharedService]\n" 11280 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11281 " if (window) {\n" 11282 " [self windowDidLoad:window];\n" 11283 " } else {\n" 11284 " [self errorLoadingWindow];\n" 11285 " }\n" 11286 " }];", 11287 format("[[SessionService sharedService]\n" 11288 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11289 " if (window) {\n" 11290 " [self windowDidLoad:window];\n" 11291 " } else {\n" 11292 " [self errorLoadingWindow];\n" 11293 " }\n" 11294 "}];", 11295 ZeroColumn)); 11296 verifyFormat("[myObject doSomethingWith:arg1\n" 11297 " firstBlock:^(Foo *a) {\n" 11298 " // ...\n" 11299 " int i;\n" 11300 " }\n" 11301 " secondBlock:^(Bar *b) {\n" 11302 " // ...\n" 11303 " int i;\n" 11304 " }\n" 11305 " thirdBlock:^Foo(Bar *b) {\n" 11306 " // ...\n" 11307 " int i;\n" 11308 " }];", 11309 ZeroColumn); 11310 verifyFormat("f(^{\n" 11311 " @autoreleasepool {\n" 11312 " if (a) {\n" 11313 " g();\n" 11314 " }\n" 11315 " }\n" 11316 "});", 11317 ZeroColumn); 11318 verifyFormat("void (^largeBlock)(void) = ^{\n" 11319 " // ...\n" 11320 "};", 11321 ZeroColumn); 11322 11323 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11324 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11325 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11326 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11327 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11328 " int i;\n" 11329 "};", 11330 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11331 } 11332 11333 TEST_F(FormatTest, SupportsCRLF) { 11334 EXPECT_EQ("int a;\r\n" 11335 "int b;\r\n" 11336 "int c;\r\n", 11337 format("int a;\r\n" 11338 " int b;\r\n" 11339 " int c;\r\n", 11340 getLLVMStyle())); 11341 EXPECT_EQ("int a;\r\n" 11342 "int b;\r\n" 11343 "int c;\r\n", 11344 format("int a;\r\n" 11345 " int b;\n" 11346 " int c;\r\n", 11347 getLLVMStyle())); 11348 EXPECT_EQ("int a;\n" 11349 "int b;\n" 11350 "int c;\n", 11351 format("int a;\r\n" 11352 " int b;\n" 11353 " int c;\n", 11354 getLLVMStyle())); 11355 EXPECT_EQ("\"aaaaaaa \"\r\n" 11356 "\"bbbbbbb\";\r\n", 11357 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11358 EXPECT_EQ("#define A \\\r\n" 11359 " b; \\\r\n" 11360 " c; \\\r\n" 11361 " d;\r\n", 11362 format("#define A \\\r\n" 11363 " b; \\\r\n" 11364 " c; d; \r\n", 11365 getGoogleStyle())); 11366 11367 EXPECT_EQ("/*\r\n" 11368 "multi line block comments\r\n" 11369 "should not introduce\r\n" 11370 "an extra carriage return\r\n" 11371 "*/\r\n", 11372 format("/*\r\n" 11373 "multi line block comments\r\n" 11374 "should not introduce\r\n" 11375 "an extra carriage return\r\n" 11376 "*/\r\n")); 11377 } 11378 11379 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11380 verifyFormat("MY_CLASS(C) {\n" 11381 " int i;\n" 11382 " int j;\n" 11383 "};"); 11384 } 11385 11386 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11387 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11388 TwoIndent.ContinuationIndentWidth = 2; 11389 11390 EXPECT_EQ("int i =\n" 11391 " longFunction(\n" 11392 " arg);", 11393 format("int i = longFunction(arg);", TwoIndent)); 11394 11395 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11396 SixIndent.ContinuationIndentWidth = 6; 11397 11398 EXPECT_EQ("int i =\n" 11399 " longFunction(\n" 11400 " arg);", 11401 format("int i = longFunction(arg);", SixIndent)); 11402 } 11403 11404 TEST_F(FormatTest, SpacesInAngles) { 11405 FormatStyle Spaces = getLLVMStyle(); 11406 Spaces.SpacesInAngles = true; 11407 11408 verifyFormat("static_cast< int >(arg);", Spaces); 11409 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11410 verifyFormat("f< int, float >();", Spaces); 11411 verifyFormat("template <> g() {}", Spaces); 11412 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11413 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11414 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11415 Spaces); 11416 11417 Spaces.Standard = FormatStyle::LS_Cpp03; 11418 Spaces.SpacesInAngles = true; 11419 verifyFormat("A< A< int > >();", Spaces); 11420 11421 Spaces.SpacesInAngles = false; 11422 verifyFormat("A<A<int> >();", Spaces); 11423 11424 Spaces.Standard = FormatStyle::LS_Cpp11; 11425 Spaces.SpacesInAngles = true; 11426 verifyFormat("A< A< int > >();", Spaces); 11427 11428 Spaces.SpacesInAngles = false; 11429 verifyFormat("A<A<int>>();", Spaces); 11430 } 11431 11432 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11433 FormatStyle Style = getLLVMStyle(); 11434 Style.SpaceAfterTemplateKeyword = false; 11435 verifyFormat("template<int> void foo();", Style); 11436 } 11437 11438 TEST_F(FormatTest, TripleAngleBrackets) { 11439 verifyFormat("f<<<1, 1>>>();"); 11440 verifyFormat("f<<<1, 1, 1, s>>>();"); 11441 verifyFormat("f<<<a, b, c, d>>>();"); 11442 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11443 verifyFormat("f<param><<<1, 1>>>();"); 11444 verifyFormat("f<1><<<1, 1>>>();"); 11445 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11446 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11447 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11448 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11449 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11450 } 11451 11452 TEST_F(FormatTest, MergeLessLessAtEnd) { 11453 verifyFormat("<<"); 11454 EXPECT_EQ("< < <", format("\\\n<<<")); 11455 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11456 "aaallvm::outs() <<"); 11457 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11458 "aaaallvm::outs()\n <<"); 11459 } 11460 11461 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11462 std::string code = "#if A\n" 11463 "#if B\n" 11464 "a.\n" 11465 "#endif\n" 11466 " a = 1;\n" 11467 "#else\n" 11468 "#endif\n" 11469 "#if C\n" 11470 "#else\n" 11471 "#endif\n"; 11472 EXPECT_EQ(code, format(code)); 11473 } 11474 11475 TEST_F(FormatTest, HandleConflictMarkers) { 11476 // Git/SVN conflict markers. 11477 EXPECT_EQ("int a;\n" 11478 "void f() {\n" 11479 " callme(some(parameter1,\n" 11480 "<<<<<<< text by the vcs\n" 11481 " parameter2),\n" 11482 "||||||| text by the vcs\n" 11483 " parameter2),\n" 11484 " parameter3,\n" 11485 "======= text by the vcs\n" 11486 " parameter2, parameter3),\n" 11487 ">>>>>>> text by the vcs\n" 11488 " otherparameter);\n", 11489 format("int a;\n" 11490 "void f() {\n" 11491 " callme(some(parameter1,\n" 11492 "<<<<<<< text by the vcs\n" 11493 " parameter2),\n" 11494 "||||||| text by the vcs\n" 11495 " parameter2),\n" 11496 " parameter3,\n" 11497 "======= text by the vcs\n" 11498 " parameter2,\n" 11499 " parameter3),\n" 11500 ">>>>>>> text by the vcs\n" 11501 " otherparameter);\n")); 11502 11503 // Perforce markers. 11504 EXPECT_EQ("void f() {\n" 11505 " function(\n" 11506 ">>>> text by the vcs\n" 11507 " parameter,\n" 11508 "==== text by the vcs\n" 11509 " parameter,\n" 11510 "==== text by the vcs\n" 11511 " parameter,\n" 11512 "<<<< text by the vcs\n" 11513 " parameter);\n", 11514 format("void f() {\n" 11515 " function(\n" 11516 ">>>> text by the vcs\n" 11517 " parameter,\n" 11518 "==== text by the vcs\n" 11519 " parameter,\n" 11520 "==== text by the vcs\n" 11521 " parameter,\n" 11522 "<<<< text by the vcs\n" 11523 " parameter);\n")); 11524 11525 EXPECT_EQ("<<<<<<<\n" 11526 "|||||||\n" 11527 "=======\n" 11528 ">>>>>>>", 11529 format("<<<<<<<\n" 11530 "|||||||\n" 11531 "=======\n" 11532 ">>>>>>>")); 11533 11534 EXPECT_EQ("<<<<<<<\n" 11535 "|||||||\n" 11536 "int i;\n" 11537 "=======\n" 11538 ">>>>>>>", 11539 format("<<<<<<<\n" 11540 "|||||||\n" 11541 "int i;\n" 11542 "=======\n" 11543 ">>>>>>>")); 11544 11545 // FIXME: Handle parsing of macros around conflict markers correctly: 11546 EXPECT_EQ("#define Macro \\\n" 11547 "<<<<<<<\n" 11548 "Something \\\n" 11549 "|||||||\n" 11550 "Else \\\n" 11551 "=======\n" 11552 "Other \\\n" 11553 ">>>>>>>\n" 11554 " End int i;\n", 11555 format("#define Macro \\\n" 11556 "<<<<<<<\n" 11557 " Something \\\n" 11558 "|||||||\n" 11559 " Else \\\n" 11560 "=======\n" 11561 " Other \\\n" 11562 ">>>>>>>\n" 11563 " End\n" 11564 "int i;\n")); 11565 } 11566 11567 TEST_F(FormatTest, DisableRegions) { 11568 EXPECT_EQ("int i;\n" 11569 "// clang-format off\n" 11570 " int j;\n" 11571 "// clang-format on\n" 11572 "int k;", 11573 format(" int i;\n" 11574 " // clang-format off\n" 11575 " int j;\n" 11576 " // clang-format on\n" 11577 " int k;")); 11578 EXPECT_EQ("int i;\n" 11579 "/* clang-format off */\n" 11580 " int j;\n" 11581 "/* clang-format on */\n" 11582 "int k;", 11583 format(" int i;\n" 11584 " /* clang-format off */\n" 11585 " int j;\n" 11586 " /* clang-format on */\n" 11587 " int k;")); 11588 } 11589 11590 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11591 format("? ) ="); 11592 verifyNoCrash("#define a\\\n /**/}"); 11593 } 11594 11595 TEST_F(FormatTest, FormatsTableGenCode) { 11596 FormatStyle Style = getLLVMStyle(); 11597 Style.Language = FormatStyle::LK_TableGen; 11598 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11599 } 11600 11601 TEST_F(FormatTest, ArrayOfTemplates) { 11602 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11603 format("auto a = new unique_ptr<int > [ 10];")); 11604 11605 FormatStyle Spaces = getLLVMStyle(); 11606 Spaces.SpacesInSquareBrackets = true; 11607 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11608 format("auto a = new unique_ptr<int > [10];", Spaces)); 11609 } 11610 11611 TEST_F(FormatTest, ArrayAsTemplateType) { 11612 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11613 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11614 11615 FormatStyle Spaces = getLLVMStyle(); 11616 Spaces.SpacesInSquareBrackets = true; 11617 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11618 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11619 } 11620 11621 // Since this test case uses UNIX-style file path. We disable it for MS 11622 // compiler. 11623 #if !defined(_MSC_VER) && !defined(__MINGW32__) 11624 11625 TEST(FormatStyle, GetStyleOfFile) { 11626 vfs::InMemoryFileSystem FS; 11627 // Test 1: format file in the same directory. 11628 ASSERT_TRUE( 11629 FS.addFile("/a/.clang-format", 0, 11630 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11631 ASSERT_TRUE( 11632 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11633 auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS); 11634 ASSERT_EQ(Style1, getLLVMStyle()); 11635 11636 // Test 2: fallback to default. 11637 ASSERT_TRUE( 11638 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11639 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS); 11640 ASSERT_EQ(Style2, getMozillaStyle()); 11641 11642 // Test 3: format file in parent directory. 11643 ASSERT_TRUE( 11644 FS.addFile("/c/.clang-format", 0, 11645 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11646 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11647 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11648 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS); 11649 ASSERT_EQ(Style3, getGoogleStyle()); 11650 } 11651 11652 #endif // _MSC_VER 11653 11654 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11655 // Column limit is 20. 11656 std::string Code = "Type *a =\n" 11657 " new Type();\n" 11658 "g(iiiii, 0, jjjjj,\n" 11659 " 0, kkkkk, 0, mm);\n" 11660 "int bad = format ;"; 11661 std::string Expected = "auto a = new Type();\n" 11662 "g(iiiii, nullptr,\n" 11663 " jjjjj, nullptr,\n" 11664 " kkkkk, nullptr,\n" 11665 " mm);\n" 11666 "int bad = format ;"; 11667 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11668 tooling::Replacements Replaces = toReplacements( 11669 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11670 "auto "), 11671 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11672 "nullptr"), 11673 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11674 "nullptr"), 11675 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11676 "nullptr")}); 11677 11678 format::FormatStyle Style = format::getLLVMStyle(); 11679 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11680 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11681 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11682 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11683 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11684 EXPECT_TRUE(static_cast<bool>(Result)); 11685 EXPECT_EQ(Expected, *Result); 11686 } 11687 11688 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11689 std::string Code = "#include \"a.h\"\n" 11690 "#include \"c.h\"\n" 11691 "\n" 11692 "int main() {\n" 11693 " return 0;\n" 11694 "}"; 11695 std::string Expected = "#include \"a.h\"\n" 11696 "#include \"b.h\"\n" 11697 "#include \"c.h\"\n" 11698 "\n" 11699 "int main() {\n" 11700 " return 0;\n" 11701 "}"; 11702 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11703 tooling::Replacements Replaces = toReplacements( 11704 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11705 "#include \"b.h\"\n")}); 11706 11707 format::FormatStyle Style = format::getLLVMStyle(); 11708 Style.SortIncludes = true; 11709 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11710 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11711 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11712 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11713 EXPECT_TRUE(static_cast<bool>(Result)); 11714 EXPECT_EQ(Expected, *Result); 11715 } 11716 11717 TEST_F(FormatTest, AllignTrailingComments) { 11718 EXPECT_EQ("#define MACRO(V) \\\n" 11719 " V(Rt2) /* one more char */ \\\n" 11720 " V(Rs) /* than here */ \\\n" 11721 "/* comment 3 */\n", 11722 format("#define MACRO(V)\\\n" 11723 "V(Rt2) /* one more char */ \\\n" 11724 "V(Rs) /* than here */ \\\n" 11725 "/* comment 3 */ \\\n", 11726 getLLVMStyleWithColumns(40))); 11727 } 11728 } // end namespace 11729 } // end namespace format 11730 } // end namespace clang 11731