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 #define DEBUG_TYPE "format-test" 11 12 #include "FormatTestUtils.h" 13 #include "clang/Format/Format.h" 14 #include "llvm/Support/Debug.h" 15 #include "gtest/gtest.h" 16 17 namespace clang { 18 namespace format { 19 20 FormatStyle getGoogleStyle() { 21 return getGoogleStyle(FormatStyle::LK_Cpp); 22 } 23 24 class FormatTest : public ::testing::Test { 25 protected: 26 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 27 const FormatStyle &Style) { 28 DEBUG(llvm::errs() << "---\n"); 29 DEBUG(llvm::errs() << Code << "\n\n"); 30 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 31 tooling::Replacements Replaces = reformat(Style, Code, Ranges); 32 ReplacementCount = Replaces.size(); 33 std::string Result = applyAllReplacements(Code, Replaces); 34 EXPECT_NE("", Result); 35 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 36 return Result; 37 } 38 39 std::string 40 format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { 41 return format(Code, 0, Code.size(), Style); 42 } 43 44 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 45 FormatStyle Style = getLLVMStyle(); 46 Style.ColumnLimit = ColumnLimit; 47 return Style; 48 } 49 50 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 51 FormatStyle Style = getGoogleStyle(); 52 Style.ColumnLimit = ColumnLimit; 53 return Style; 54 } 55 56 void verifyFormat(llvm::StringRef Code, 57 const FormatStyle &Style = getLLVMStyle()) { 58 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 59 } 60 61 void verifyGoogleFormat(llvm::StringRef Code) { 62 verifyFormat(Code, getGoogleStyle()); 63 } 64 65 void verifyIndependentOfContext(llvm::StringRef text) { 66 verifyFormat(text); 67 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 68 } 69 70 int ReplacementCount; 71 }; 72 73 TEST_F(FormatTest, MessUp) { 74 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 75 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 76 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 77 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 78 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 79 } 80 81 //===----------------------------------------------------------------------===// 82 // Basic function tests. 83 //===----------------------------------------------------------------------===// 84 85 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 86 EXPECT_EQ(";", format(";")); 87 } 88 89 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 90 EXPECT_EQ("int i;", format(" int i;")); 91 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 92 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 93 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 94 } 95 96 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 97 EXPECT_EQ("int i;", format("int\ni;")); 98 } 99 100 TEST_F(FormatTest, FormatsNestedBlockStatements) { 101 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 102 } 103 104 TEST_F(FormatTest, FormatsNestedCall) { 105 verifyFormat("Method(f1, f2(f3));"); 106 verifyFormat("Method(f1(f2, f3()));"); 107 verifyFormat("Method(f1(f2, (f3())));"); 108 } 109 110 TEST_F(FormatTest, NestedNameSpecifiers) { 111 verifyFormat("vector<::Type> v;"); 112 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 113 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 114 } 115 116 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 117 EXPECT_EQ("if (a) {\n" 118 " f();\n" 119 "}", 120 format("if(a){f();}")); 121 EXPECT_EQ(4, ReplacementCount); 122 EXPECT_EQ("if (a) {\n" 123 " f();\n" 124 "}", 125 format("if (a) {\n" 126 " f();\n" 127 "}")); 128 EXPECT_EQ(0, ReplacementCount); 129 } 130 131 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { 132 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); 133 EXPECT_EQ("int a;", format("int a; ")); 134 EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); 135 EXPECT_EQ("int a;\nint b; ", 136 format("int a; \nint b; ", 0, 0, getLLVMStyle())); 137 } 138 139 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { 140 EXPECT_EQ("int b;\nint a;", 141 format("int b;\n int a;", 7, 0, getLLVMStyle())); 142 EXPECT_EQ("int b;\n int a;", 143 format("int b;\n int a;", 6, 0, getLLVMStyle())); 144 145 EXPECT_EQ("#define A \\\n" 146 " int a; \\\n" 147 " int b;", 148 format("#define A \\\n" 149 " int a; \\\n" 150 " int b;", 151 26, 0, getLLVMStyleWithColumns(12))); 152 EXPECT_EQ("#define A \\\n" 153 " int a; \\\n" 154 " int b;", 155 format("#define A \\\n" 156 " int a; \\\n" 157 " int b;", 158 25, 0, getLLVMStyleWithColumns(12))); 159 } 160 161 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 162 EXPECT_EQ("int a;\n\n int b;", 163 format("int a;\n \n\n int b;", 7, 0, getLLVMStyle())); 164 EXPECT_EQ("int a;\n\n int b;", 165 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); 166 } 167 168 TEST_F(FormatTest, RemovesEmptyLines) { 169 EXPECT_EQ("class C {\n" 170 " int i;\n" 171 "};", 172 format("class C {\n" 173 " int i;\n" 174 "\n" 175 "};")); 176 177 // Don't remove empty lines at the start of namespaces. 178 EXPECT_EQ("namespace N {\n" 179 "\n" 180 "int i;\n" 181 "}", 182 format("namespace N {\n" 183 "\n" 184 "int i;\n" 185 "}", 186 getGoogleStyle())); 187 188 // Remove empty lines at the beginning and end of blocks. 189 EXPECT_EQ("void f() {\n" 190 "\n" 191 " if (a) {\n" 192 "\n" 193 " f();\n" 194 " }\n" 195 "}", 196 format("void f() {\n" 197 "\n" 198 " if (a) {\n" 199 "\n" 200 " f();\n" 201 "\n" 202 " }\n" 203 "\n" 204 "}", 205 getLLVMStyle())); 206 EXPECT_EQ("void f() {\n" 207 " if (a) {\n" 208 " f();\n" 209 " }\n" 210 "}", 211 format("void f() {\n" 212 "\n" 213 " if (a) {\n" 214 "\n" 215 " f();\n" 216 "\n" 217 " }\n" 218 "\n" 219 "}", 220 getGoogleStyle())); 221 222 // Don't remove empty lines in more complex control statements. 223 EXPECT_EQ("void f() {\n" 224 " if (a) {\n" 225 " f();\n" 226 "\n" 227 " } else if (b) {\n" 228 " f();\n" 229 " }\n" 230 "}", 231 format("void f() {\n" 232 " if (a) {\n" 233 " f();\n" 234 "\n" 235 " } else if (b) {\n" 236 " f();\n" 237 "\n" 238 " }\n" 239 "\n" 240 "}")); 241 242 // FIXME: This is slightly inconsistent. 243 EXPECT_EQ("namespace {\n" 244 "int i;\n" 245 "}", 246 format("namespace {\n" 247 "int i;\n" 248 "\n" 249 "}")); 250 EXPECT_EQ("namespace {\n" 251 "int i;\n" 252 "\n" 253 "} // namespace", 254 format("namespace {\n" 255 "int i;\n" 256 "\n" 257 "} // namespace")); 258 } 259 260 TEST_F(FormatTest, ReformatsMovedLines) { 261 EXPECT_EQ( 262 "template <typename T> T *getFETokenInfo() const {\n" 263 " return static_cast<T *>(FETokenInfo);\n" 264 "}\n" 265 " int a; // <- Should not be formatted", 266 format( 267 "template<typename T>\n" 268 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 269 " int a; // <- Should not be formatted", 270 9, 5, getLLVMStyle())); 271 } 272 273 //===----------------------------------------------------------------------===// 274 // Tests for control statements. 275 //===----------------------------------------------------------------------===// 276 277 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 278 verifyFormat("if (true)\n f();\ng();"); 279 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 280 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 281 282 FormatStyle AllowsMergedIf = getLLVMStyle(); 283 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 284 verifyFormat("if (a)\n" 285 " // comment\n" 286 " f();", 287 AllowsMergedIf); 288 verifyFormat("if (a)\n" 289 " ;", 290 AllowsMergedIf); 291 verifyFormat("if (a)\n" 292 " if (b) return;", 293 AllowsMergedIf); 294 295 verifyFormat("if (a) // Can't merge this\n" 296 " f();\n", 297 AllowsMergedIf); 298 verifyFormat("if (a) /* still don't merge */\n" 299 " f();", 300 AllowsMergedIf); 301 verifyFormat("if (a) { // Never merge this\n" 302 " f();\n" 303 "}", 304 AllowsMergedIf); 305 verifyFormat("if (a) {/* Never merge this */\n" 306 " f();\n" 307 "}", 308 AllowsMergedIf); 309 310 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); 311 EXPECT_EQ("if (a) return; // comment", 312 format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); 313 314 AllowsMergedIf.ColumnLimit = 14; 315 verifyFormat("if (a) return;", AllowsMergedIf); 316 verifyFormat("if (aaaaaaaaa)\n" 317 " return;", 318 AllowsMergedIf); 319 320 AllowsMergedIf.ColumnLimit = 13; 321 verifyFormat("if (a)\n return;", AllowsMergedIf); 322 } 323 324 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 325 FormatStyle AllowsMergedLoops = getLLVMStyle(); 326 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 327 verifyFormat("while (true) continue;", AllowsMergedLoops); 328 verifyFormat("for (;;) continue;", AllowsMergedLoops); 329 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 330 verifyFormat("while (true)\n" 331 " ;", 332 AllowsMergedLoops); 333 verifyFormat("for (;;)\n" 334 " ;", 335 AllowsMergedLoops); 336 verifyFormat("for (;;)\n" 337 " for (;;) continue;", 338 AllowsMergedLoops); 339 verifyFormat("for (;;) // Can't merge this\n" 340 " continue;", 341 AllowsMergedLoops); 342 verifyFormat("for (;;) /* still don't merge */\n" 343 " continue;", 344 AllowsMergedLoops); 345 } 346 347 TEST_F(FormatTest, ParseIfElse) { 348 verifyFormat("if (true)\n" 349 " if (true)\n" 350 " if (true)\n" 351 " f();\n" 352 " else\n" 353 " g();\n" 354 " else\n" 355 " h();\n" 356 "else\n" 357 " i();"); 358 verifyFormat("if (true)\n" 359 " if (true)\n" 360 " if (true) {\n" 361 " if (true)\n" 362 " f();\n" 363 " } else {\n" 364 " g();\n" 365 " }\n" 366 " else\n" 367 " h();\n" 368 "else {\n" 369 " i();\n" 370 "}"); 371 verifyFormat("void f() {\n" 372 " if (a) {\n" 373 " } else {\n" 374 " }\n" 375 "}"); 376 } 377 378 TEST_F(FormatTest, ElseIf) { 379 verifyFormat("if (a) {\n} else if (b) {\n}"); 380 verifyFormat("if (a)\n" 381 " f();\n" 382 "else if (b)\n" 383 " g();\n" 384 "else\n" 385 " h();"); 386 verifyFormat("if (a) {\n" 387 " f();\n" 388 "}\n" 389 "// or else ..\n" 390 "else {\n" 391 " g()\n" 392 "}"); 393 } 394 395 TEST_F(FormatTest, FormatsForLoop) { 396 verifyFormat( 397 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 398 " ++VeryVeryLongLoopVariable)\n" 399 " ;"); 400 verifyFormat("for (;;)\n" 401 " f();"); 402 verifyFormat("for (;;) {\n}"); 403 verifyFormat("for (;;) {\n" 404 " f();\n" 405 "}"); 406 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 407 408 verifyFormat( 409 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 410 " E = UnwrappedLines.end();\n" 411 " I != E; ++I) {\n}"); 412 413 verifyFormat( 414 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 415 " ++IIIII) {\n}"); 416 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 417 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 418 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 419 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 420 " I = FD->getDeclsInPrototypeScope().begin(),\n" 421 " E = FD->getDeclsInPrototypeScope().end();\n" 422 " I != E; ++I) {\n}"); 423 424 // FIXME: Not sure whether we want extra identation in line 3 here: 425 verifyFormat( 426 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 430 " ++aaaaaaaaaaa) {\n}"); 431 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 432 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 433 "}"); 434 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 435 " aaaaaaaaaa);\n" 436 " iter; ++iter) {\n" 437 "}"); 438 439 FormatStyle NoBinPacking = getLLVMStyle(); 440 NoBinPacking.BinPackParameters = false; 441 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 442 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 443 " aaaaaaaaaaaaaaaa,\n" 444 " aaaaaaaaaaaaaaaa,\n" 445 " aaaaaaaaaaaaaaaa);\n" 446 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 447 "}", 448 NoBinPacking); 449 verifyFormat( 450 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 451 " E = UnwrappedLines.end();\n" 452 " I != E;\n" 453 " ++I) {\n}", 454 NoBinPacking); 455 } 456 457 TEST_F(FormatTest, RangeBasedForLoops) { 458 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 460 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 461 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 462 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 464 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 465 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 466 } 467 468 TEST_F(FormatTest, ForEachLoops) { 469 verifyFormat("void f() {\n" 470 " foreach (Item *item, itemlist) {}\n" 471 " Q_FOREACH (Item *item, itemlist) {}\n" 472 " BOOST_FOREACH (Item *item, itemlist) {}\n" 473 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 474 "}"); 475 } 476 477 TEST_F(FormatTest, FormatsWhileLoop) { 478 verifyFormat("while (true) {\n}"); 479 verifyFormat("while (true)\n" 480 " f();"); 481 verifyFormat("while () {\n}"); 482 verifyFormat("while () {\n" 483 " f();\n" 484 "}"); 485 } 486 487 TEST_F(FormatTest, FormatsDoWhile) { 488 verifyFormat("do {\n" 489 " do_something();\n" 490 "} while (something());"); 491 verifyFormat("do\n" 492 " do_something();\n" 493 "while (something());"); 494 } 495 496 TEST_F(FormatTest, FormatsSwitchStatement) { 497 verifyFormat("switch (x) {\n" 498 "case 1:\n" 499 " f();\n" 500 " break;\n" 501 "case kFoo:\n" 502 "case ns::kBar:\n" 503 "case kBaz:\n" 504 " break;\n" 505 "default:\n" 506 " g();\n" 507 " break;\n" 508 "}"); 509 verifyFormat("switch (x) {\n" 510 "case 1: {\n" 511 " f();\n" 512 " break;\n" 513 "}\n" 514 "case 2: {\n" 515 " break;\n" 516 "}\n" 517 "}"); 518 verifyFormat("switch (x) {\n" 519 "case 1: {\n" 520 " f();\n" 521 " {\n" 522 " g();\n" 523 " h();\n" 524 " }\n" 525 " break;\n" 526 "}\n" 527 "}"); 528 verifyFormat("switch (x) {\n" 529 "case 1: {\n" 530 " f();\n" 531 " if (foo) {\n" 532 " g();\n" 533 " h();\n" 534 " }\n" 535 " break;\n" 536 "}\n" 537 "}"); 538 verifyFormat("switch (x) {\n" 539 "case 1: {\n" 540 " f();\n" 541 " g();\n" 542 "} break;\n" 543 "}"); 544 verifyFormat("switch (test)\n" 545 " ;"); 546 verifyFormat("switch (x) {\n" 547 "default: {\n" 548 " // Do nothing.\n" 549 "}\n" 550 "}"); 551 verifyFormat("switch (x) {\n" 552 "// comment\n" 553 "// if 1, do f()\n" 554 "case 1:\n" 555 " f();\n" 556 "}"); 557 verifyFormat("switch (x) {\n" 558 "case 1:\n" 559 " // Do amazing stuff\n" 560 " {\n" 561 " f();\n" 562 " g();\n" 563 " }\n" 564 " break;\n" 565 "}"); 566 verifyFormat("#define A \\\n" 567 " switch (x) { \\\n" 568 " case a: \\\n" 569 " foo = b; \\\n" 570 " }", getLLVMStyleWithColumns(20)); 571 verifyFormat("#define OPERATION_CASE(name) \\\n" 572 " case OP_name: \\\n" 573 " return operations::Operation##name\n", 574 getLLVMStyleWithColumns(40)); 575 576 verifyGoogleFormat("switch (x) {\n" 577 " case 1:\n" 578 " f();\n" 579 " break;\n" 580 " case kFoo:\n" 581 " case ns::kBar:\n" 582 " case kBaz:\n" 583 " break;\n" 584 " default:\n" 585 " g();\n" 586 " break;\n" 587 "}"); 588 verifyGoogleFormat("switch (x) {\n" 589 " case 1: {\n" 590 " f();\n" 591 " break;\n" 592 " }\n" 593 "}"); 594 verifyGoogleFormat("switch (test)\n" 595 " ;"); 596 597 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 598 " case OP_name: \\\n" 599 " return operations::Operation##name\n"); 600 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 601 " // Get the correction operation class.\n" 602 " switch (OpCode) {\n" 603 " CASE(Add);\n" 604 " CASE(Subtract);\n" 605 " default:\n" 606 " return operations::Unknown;\n" 607 " }\n" 608 "#undef OPERATION_CASE\n" 609 "}"); 610 verifyFormat("DEBUG({\n" 611 " switch (x) {\n" 612 " case A:\n" 613 " f();\n" 614 " break;\n" 615 " // On B:\n" 616 " case B:\n" 617 " g();\n" 618 " break;\n" 619 " }\n" 620 "});"); 621 } 622 623 TEST_F(FormatTest, CaseRanges) { 624 verifyFormat("switch (x) {\n" 625 "case 'A' ... 'Z':\n" 626 "case 1 ... 5:\n" 627 " break;\n" 628 "}"); 629 } 630 631 TEST_F(FormatTest, FormatsLabels) { 632 verifyFormat("void f() {\n" 633 " some_code();\n" 634 "test_label:\n" 635 " some_other_code();\n" 636 " {\n" 637 " some_more_code();\n" 638 " another_label:\n" 639 " some_more_code();\n" 640 " }\n" 641 "}"); 642 verifyFormat("some_code();\n" 643 "test_label:\n" 644 "some_other_code();"); 645 } 646 647 //===----------------------------------------------------------------------===// 648 // Tests for comments. 649 //===----------------------------------------------------------------------===// 650 651 TEST_F(FormatTest, UnderstandsSingleLineComments) { 652 verifyFormat("//* */"); 653 verifyFormat("// line 1\n" 654 "// line 2\n" 655 "void f() {}\n"); 656 657 verifyFormat("void f() {\n" 658 " // Doesn't do anything\n" 659 "}"); 660 verifyFormat("SomeObject\n" 661 " // Calling someFunction on SomeObject\n" 662 " .someFunction();"); 663 verifyFormat("auto result = SomeObject\n" 664 " // Calling someFunction on SomeObject\n" 665 " .someFunction();"); 666 verifyFormat("void f(int i, // some comment (probably for i)\n" 667 " int j, // some comment (probably for j)\n" 668 " int k); // some comment (probably for k)"); 669 verifyFormat("void f(int i,\n" 670 " // some comment (probably for j)\n" 671 " int j,\n" 672 " // some comment (probably for k)\n" 673 " int k);"); 674 675 verifyFormat("int i // This is a fancy variable\n" 676 " = 5; // with nicely aligned comment."); 677 678 verifyFormat("// Leading comment.\n" 679 "int a; // Trailing comment."); 680 verifyFormat("int a; // Trailing comment\n" 681 " // on 2\n" 682 " // or 3 lines.\n" 683 "int b;"); 684 verifyFormat("int a; // Trailing comment\n" 685 "\n" 686 "// Leading comment.\n" 687 "int b;"); 688 verifyFormat("int a; // Comment.\n" 689 " // More details.\n" 690 "int bbbb; // Another comment."); 691 verifyFormat( 692 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 693 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 694 "int cccccccccccccccccccccccccccccc; // comment\n" 695 "int ddd; // looooooooooooooooooooooooong comment\n" 696 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 697 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 698 "int ccccccccccccccccccc; // comment"); 699 700 verifyFormat("#include \"a\" // comment\n" 701 "#include \"a/b/c\" // comment"); 702 verifyFormat("#include <a> // comment\n" 703 "#include <a/b/c> // comment"); 704 EXPECT_EQ("#include \"a\" // comment\n" 705 "#include \"a/b/c\" // comment", 706 format("#include \\\n" 707 " \"a\" // comment\n" 708 "#include \"a/b/c\" // comment")); 709 710 verifyFormat("enum E {\n" 711 " // comment\n" 712 " VAL_A, // comment\n" 713 " VAL_B\n" 714 "};"); 715 716 verifyFormat( 717 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 718 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 719 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 720 " // Comment inside a statement.\n" 721 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 722 verifyFormat( 723 "bool aaaaaaaaaaaaa = // comment\n" 724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 726 727 verifyFormat("int aaaa; // aaaaa\n" 728 "int aa; // aaaaaaa", 729 getLLVMStyleWithColumns(20)); 730 731 EXPECT_EQ("void f() { // This does something ..\n" 732 "}\n" 733 "int a; // This is unrelated", 734 format("void f() { // This does something ..\n" 735 " }\n" 736 "int a; // This is unrelated")); 737 EXPECT_EQ("class C {\n" 738 " void f() { // This does something ..\n" 739 " } // awesome..\n" 740 "\n" 741 " int a; // This is unrelated\n" 742 "};", 743 format("class C{void f() { // This does something ..\n" 744 " } // awesome..\n" 745 " \n" 746 "int a; // This is unrelated\n" 747 "};")); 748 749 EXPECT_EQ("int i; // single line trailing comment", 750 format("int i;\\\n// single line trailing comment")); 751 752 verifyGoogleFormat("int a; // Trailing comment."); 753 754 verifyFormat("someFunction(anotherFunction( // Force break.\n" 755 " parameter));"); 756 757 verifyGoogleFormat("#endif // HEADER_GUARD"); 758 759 verifyFormat("const char *test[] = {\n" 760 " // A\n" 761 " \"aaaa\",\n" 762 " // B\n" 763 " \"aaaaa\"};"); 764 verifyGoogleFormat( 765 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 766 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 767 EXPECT_EQ("D(a, {\n" 768 " // test\n" 769 " int a;\n" 770 "});", 771 format("D(a, {\n" 772 "// test\n" 773 "int a;\n" 774 "});")); 775 776 EXPECT_EQ("lineWith(); // comment\n" 777 "// at start\n" 778 "otherLine();", 779 format("lineWith(); // comment\n" 780 "// at start\n" 781 "otherLine();")); 782 EXPECT_EQ("lineWith(); // comment\n" 783 " // at start\n" 784 "otherLine();", 785 format("lineWith(); // comment\n" 786 " // at start\n" 787 "otherLine();")); 788 789 EXPECT_EQ("lineWith(); // comment\n" 790 "// at start\n" 791 "otherLine(); // comment", 792 format("lineWith(); // comment\n" 793 "// at start\n" 794 "otherLine(); // comment")); 795 EXPECT_EQ("lineWith();\n" 796 "// at start\n" 797 "otherLine(); // comment", 798 format("lineWith();\n" 799 " // at start\n" 800 "otherLine(); // comment")); 801 EXPECT_EQ("// first\n" 802 "// at start\n" 803 "otherLine(); // comment", 804 format("// first\n" 805 " // at start\n" 806 "otherLine(); // comment")); 807 EXPECT_EQ("f();\n" 808 "// first\n" 809 "// at start\n" 810 "otherLine(); // comment", 811 format("f();\n" 812 "// first\n" 813 " // at start\n" 814 "otherLine(); // comment")); 815 verifyFormat("f(); // comment\n" 816 "// first\n" 817 "// at start\n" 818 "otherLine();"); 819 EXPECT_EQ("f(); // comment\n" 820 "// first\n" 821 "// at start\n" 822 "otherLine();", 823 format("f(); // comment\n" 824 "// first\n" 825 " // at start\n" 826 "otherLine();")); 827 EXPECT_EQ("f(); // comment\n" 828 " // first\n" 829 "// at start\n" 830 "otherLine();", 831 format("f(); // comment\n" 832 " // first\n" 833 "// at start\n" 834 "otherLine();")); 835 } 836 837 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 838 EXPECT_EQ("SomeFunction(a,\n" 839 " b, // comment\n" 840 " c);", 841 format("SomeFunction(a,\n" 842 " b, // comment\n" 843 " c);")); 844 EXPECT_EQ("SomeFunction(a, b,\n" 845 " // comment\n" 846 " c);", 847 format("SomeFunction(a,\n" 848 " b,\n" 849 " // comment\n" 850 " c);")); 851 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 852 " c);", 853 format("SomeFunction(a, b, // comment (unclear relation)\n" 854 " c);")); 855 EXPECT_EQ("SomeFunction(a, // comment\n" 856 " b,\n" 857 " c); // comment", 858 format("SomeFunction(a, // comment\n" 859 " b,\n" 860 " c); // comment")); 861 } 862 863 TEST_F(FormatTest, CanFormatCommentsLocally) { 864 EXPECT_EQ("int a; // comment\n" 865 "int b; // comment", 866 format("int a; // comment\n" 867 "int b; // comment", 868 0, 0, getLLVMStyle())); 869 EXPECT_EQ("int a; // comment\n" 870 " // line 2\n" 871 "int b;", 872 format("int a; // comment\n" 873 " // line 2\n" 874 "int b;", 875 28, 0, getLLVMStyle())); 876 EXPECT_EQ("int aaaaaa; // comment\n" 877 "int b;\n" 878 "int c; // unrelated comment", 879 format("int aaaaaa; // comment\n" 880 "int b;\n" 881 "int c; // unrelated comment", 882 31, 0, getLLVMStyle())); 883 884 EXPECT_EQ("int a; // This\n" 885 " // is\n" 886 " // a", 887 format("int a; // This\n" 888 " // is\n" 889 " // a", 890 0, 0, getLLVMStyle())); 891 EXPECT_EQ("int a; // This\n" 892 " // is\n" 893 " // a\n" 894 "// This is b\n" 895 "int b;", 896 format("int a; // This\n" 897 " // is\n" 898 " // a\n" 899 "// This is b\n" 900 "int b;", 901 0, 0, getLLVMStyle())); 902 EXPECT_EQ("int a; // This\n" 903 " // is\n" 904 " // a\n" 905 "\n" 906 " // This is unrelated", 907 format("int a; // This\n" 908 " // is\n" 909 " // a\n" 910 "\n" 911 " // This is unrelated", 912 0, 0, getLLVMStyle())); 913 EXPECT_EQ("int a;\n" 914 "// This is\n" 915 "// not formatted. ", 916 format("int a;\n" 917 "// This is\n" 918 "// not formatted. ", 919 0, 0, getLLVMStyle())); 920 } 921 922 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 923 EXPECT_EQ("// comment", format("// comment ")); 924 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 925 format("int aaaaaaa, bbbbbbb; // comment ", 926 getLLVMStyleWithColumns(33))); 927 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 928 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 929 } 930 931 TEST_F(FormatTest, UnderstandsBlockComments) { 932 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 933 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 934 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 935 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 936 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 937 "/* Trailing comment for aa... */\n" 938 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 939 EXPECT_EQ( 940 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 941 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 942 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 943 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 944 EXPECT_EQ( 945 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 946 " aaaaaaaaaaaaaaaaaa,\n" 947 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 948 "}", 949 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 950 " aaaaaaaaaaaaaaaaaa ,\n" 951 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 952 "}")); 953 954 FormatStyle NoBinPacking = getLLVMStyle(); 955 NoBinPacking.BinPackParameters = false; 956 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 957 " /* parameter 2 */ aaaaaa,\n" 958 " /* parameter 3 */ aaaaaa,\n" 959 " /* parameter 4 */ aaaaaa);", 960 NoBinPacking); 961 962 // Aligning block comments in macros. 963 verifyGoogleFormat("#define A \\\n" 964 " int i; /*a*/ \\\n" 965 " int jjj; /*b*/"); 966 } 967 968 TEST_F(FormatTest, AlignsBlockComments) { 969 EXPECT_EQ("/*\n" 970 " * Really multi-line\n" 971 " * comment.\n" 972 " */\n" 973 "void f() {}", 974 format(" /*\n" 975 " * Really multi-line\n" 976 " * comment.\n" 977 " */\n" 978 " void f() {}")); 979 EXPECT_EQ("class C {\n" 980 " /*\n" 981 " * Another multi-line\n" 982 " * comment.\n" 983 " */\n" 984 " void f() {}\n" 985 "};", 986 format("class C {\n" 987 "/*\n" 988 " * Another multi-line\n" 989 " * comment.\n" 990 " */\n" 991 "void f() {}\n" 992 "};")); 993 EXPECT_EQ("/*\n" 994 " 1. This is a comment with non-trivial formatting.\n" 995 " 1.1. We have to indent/outdent all lines equally\n" 996 " 1.1.1. to keep the formatting.\n" 997 " */", 998 format(" /*\n" 999 " 1. This is a comment with non-trivial formatting.\n" 1000 " 1.1. We have to indent/outdent all lines equally\n" 1001 " 1.1.1. to keep the formatting.\n" 1002 " */")); 1003 EXPECT_EQ("/*\n" 1004 "Don't try to outdent if there's not enough indentation.\n" 1005 "*/", 1006 format(" /*\n" 1007 " Don't try to outdent if there's not enough indentation.\n" 1008 " */")); 1009 1010 EXPECT_EQ("int i; /* Comment with empty...\n" 1011 " *\n" 1012 " * line. */", 1013 format("int i; /* Comment with empty...\n" 1014 " *\n" 1015 " * line. */")); 1016 } 1017 1018 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1019 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1021 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1023 EXPECT_EQ( 1024 "void ffffffffffff(\n" 1025 " int aaaaaaaa, int bbbbbbbb,\n" 1026 " int cccccccccccc) { /*\n" 1027 " aaaaaaaaaa\n" 1028 " aaaaaaaaaaaaa\n" 1029 " bbbbbbbbbbbbbb\n" 1030 " bbbbbbbbbb\n" 1031 " */\n" 1032 "}", 1033 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1034 "{ /*\n" 1035 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1036 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1037 " */\n" 1038 "}", 1039 getLLVMStyleWithColumns(40))); 1040 } 1041 1042 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1043 EXPECT_EQ("void\n" 1044 "ffffffffff(int aaaaa /* test */);", 1045 format("void ffffffffff(int aaaaa /* test */);", 1046 getLLVMStyleWithColumns(35))); 1047 } 1048 1049 TEST_F(FormatTest, SplitsLongCxxComments) { 1050 EXPECT_EQ("// A comment that\n" 1051 "// doesn't fit on\n" 1052 "// one line", 1053 format("// A comment that doesn't fit on one line", 1054 getLLVMStyleWithColumns(20))); 1055 EXPECT_EQ("// a b c d\n" 1056 "// e f g\n" 1057 "// h i j k", 1058 format("// a b c d e f g h i j k", 1059 getLLVMStyleWithColumns(10))); 1060 EXPECT_EQ("// a b c d\n" 1061 "// e f g\n" 1062 "// h i j k", 1063 format("\\\n// a b c d e f g h i j k", 1064 getLLVMStyleWithColumns(10))); 1065 EXPECT_EQ("if (true) // A comment that\n" 1066 " // doesn't fit on\n" 1067 " // one line", 1068 format("if (true) // A comment that doesn't fit on one line ", 1069 getLLVMStyleWithColumns(30))); 1070 EXPECT_EQ("// Don't_touch_leading_whitespace", 1071 format("// Don't_touch_leading_whitespace", 1072 getLLVMStyleWithColumns(20))); 1073 EXPECT_EQ("// Add leading\n" 1074 "// whitespace", 1075 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1076 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1077 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1078 "// limit", 1079 format("//Even if it makes the line exceed the column limit", 1080 getLLVMStyleWithColumns(51))); 1081 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1082 1083 EXPECT_EQ("// aa bb cc dd", 1084 format("// aa bb cc dd ", 1085 getLLVMStyleWithColumns(15))); 1086 1087 EXPECT_EQ("// A comment before\n" 1088 "// a macro\n" 1089 "// definition\n" 1090 "#define a b", 1091 format("// A comment before a macro definition\n" 1092 "#define a b", 1093 getLLVMStyleWithColumns(20))); 1094 EXPECT_EQ("void\n" 1095 "ffffff(int aaaaaaaaa, // wwww\n" 1096 " int bbbbbbbbbb, // xxxxxxx\n" 1097 " // yyyyyyyyyy\n" 1098 " int c, int d, int e) {}", 1099 format("void ffffff(\n" 1100 " int aaaaaaaaa, // wwww\n" 1101 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1102 " int c, int d, int e) {}", 1103 getLLVMStyleWithColumns(40))); 1104 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1105 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1106 getLLVMStyleWithColumns(20))); 1107 EXPECT_EQ( 1108 "#define XXX // a b c d\n" 1109 " // e f g h", 1110 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1111 EXPECT_EQ( 1112 "#define XXX // q w e r\n" 1113 " // t y u i", 1114 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1115 } 1116 1117 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1118 EXPECT_EQ("// A comment\n" 1119 "// that doesn't\n" 1120 "// fit on one\n" 1121 "// line", 1122 format("// A comment that doesn't fit on one line", 1123 getLLVMStyleWithColumns(20))); 1124 EXPECT_EQ("/// A comment\n" 1125 "/// that doesn't\n" 1126 "/// fit on one\n" 1127 "/// line", 1128 format("/// A comment that doesn't fit on one line", 1129 getLLVMStyleWithColumns(20))); 1130 } 1131 1132 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1133 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1134 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1135 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1136 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1137 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1138 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1139 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1140 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1141 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1142 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1143 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1144 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1145 getLLVMStyleWithColumns(50))); 1146 // FIXME: One day we might want to implement adjustment of leading whitespace 1147 // of the consecutive lines in this kind of comment: 1148 EXPECT_EQ("int\n" 1149 "a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1150 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1151 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1152 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1153 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1154 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1155 getLLVMStyleWithColumns(49))); 1156 } 1157 1158 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1159 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1160 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1161 EXPECT_EQ( 1162 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1163 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1164 EXPECT_EQ( 1165 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1166 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1167 } 1168 1169 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1170 EXPECT_EQ("if (xxx ==\n" 1171 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1172 " zzz)\n" 1173 " q();", 1174 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1175 " zzz) q();", 1176 getLLVMStyleWithColumns(40))); 1177 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1178 " yyy && // aaaaaa bbbbbbbb cccc\n" 1179 " zzz)\n" 1180 " q();", 1181 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1182 " zzz) q();", 1183 getLLVMStyleWithColumns(40))); 1184 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1185 " yyy || // aaaaaa bbbbbbbb cccc\n" 1186 " zzz)\n" 1187 " q();", 1188 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1189 " zzz) q();", 1190 getLLVMStyleWithColumns(40))); 1191 EXPECT_EQ("fffffffff(\n" 1192 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1193 " zzz);", 1194 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1195 " zzz);", 1196 getLLVMStyleWithColumns(40))); 1197 } 1198 1199 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1200 EXPECT_EQ("#define A(x) /* \\\n" 1201 " a comment \\\n" 1202 " inside */ \\\n" 1203 " f();", 1204 format("#define A(x) /* \\\n" 1205 " a comment \\\n" 1206 " inside */ \\\n" 1207 " f();", 1208 getLLVMStyleWithColumns(17))); 1209 EXPECT_EQ("#define A( \\\n" 1210 " x) /* \\\n" 1211 " a comment \\\n" 1212 " inside */ \\\n" 1213 " f();", 1214 format("#define A( \\\n" 1215 " x) /* \\\n" 1216 " a comment \\\n" 1217 " inside */ \\\n" 1218 " f();", 1219 getLLVMStyleWithColumns(17))); 1220 } 1221 1222 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1223 EXPECT_EQ("namespace {}\n// Test\n#define A", 1224 format("namespace {}\n // Test\n#define A")); 1225 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1226 format("namespace {}\n /* Test */\n#define A")); 1227 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1228 format("namespace {}\n /* Test */ #define A")); 1229 } 1230 1231 TEST_F(FormatTest, SplitsLongLinesInComments) { 1232 EXPECT_EQ("/* This is a long\n" 1233 " * comment that\n" 1234 " * doesn't\n" 1235 " * fit on one line.\n" 1236 " */", 1237 format("/* " 1238 "This is a long " 1239 "comment that " 1240 "doesn't " 1241 "fit on one line. */", 1242 getLLVMStyleWithColumns(20))); 1243 EXPECT_EQ("/* a b c d\n" 1244 " * e f g\n" 1245 " * h i j k\n" 1246 " */", 1247 format("/* a b c d e f g h i j k */", 1248 getLLVMStyleWithColumns(10))); 1249 EXPECT_EQ("/* a b c d\n" 1250 " * e f g\n" 1251 " * h i j k\n" 1252 " */", 1253 format("\\\n/* a b c d e f g h i j k */", 1254 getLLVMStyleWithColumns(10))); 1255 EXPECT_EQ("/*\n" 1256 "This is a long\n" 1257 "comment that doesn't\n" 1258 "fit on one line.\n" 1259 "*/", 1260 format("/*\n" 1261 "This is a long " 1262 "comment that doesn't " 1263 "fit on one line. \n" 1264 "*/", getLLVMStyleWithColumns(20))); 1265 EXPECT_EQ("/*\n" 1266 " * This is a long\n" 1267 " * comment that\n" 1268 " * doesn't fit on\n" 1269 " * one line.\n" 1270 " */", 1271 format("/* \n" 1272 " * This is a long " 1273 " comment that " 1274 " doesn't fit on " 1275 " one line. \n" 1276 " */", getLLVMStyleWithColumns(20))); 1277 EXPECT_EQ("/*\n" 1278 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1279 " * so_it_should_be_broken\n" 1280 " * wherever_a_space_occurs\n" 1281 " */", 1282 format("/*\n" 1283 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1284 " so_it_should_be_broken " 1285 " wherever_a_space_occurs \n" 1286 " */", 1287 getLLVMStyleWithColumns(20))); 1288 EXPECT_EQ("/*\n" 1289 " * This_comment_can_not_be_broken_into_lines\n" 1290 " */", 1291 format("/*\n" 1292 " * This_comment_can_not_be_broken_into_lines\n" 1293 " */", 1294 getLLVMStyleWithColumns(20))); 1295 EXPECT_EQ("{\n" 1296 " /*\n" 1297 " This is another\n" 1298 " long comment that\n" 1299 " doesn't fit on one\n" 1300 " line 1234567890\n" 1301 " */\n" 1302 "}", 1303 format("{\n" 1304 "/*\n" 1305 "This is another " 1306 " long comment that " 1307 " doesn't fit on one" 1308 " line 1234567890\n" 1309 "*/\n" 1310 "}", getLLVMStyleWithColumns(20))); 1311 EXPECT_EQ("{\n" 1312 " /*\n" 1313 " * This i s\n" 1314 " * another comment\n" 1315 " * t hat doesn' t\n" 1316 " * fit on one l i\n" 1317 " * n e\n" 1318 " */\n" 1319 "}", 1320 format("{\n" 1321 "/*\n" 1322 " * This i s" 1323 " another comment" 1324 " t hat doesn' t" 1325 " fit on one l i" 1326 " n e\n" 1327 " */\n" 1328 "}", getLLVMStyleWithColumns(20))); 1329 EXPECT_EQ("/*\n" 1330 " * This is a long\n" 1331 " * comment that\n" 1332 " * doesn't fit on\n" 1333 " * one line\n" 1334 " */", 1335 format(" /*\n" 1336 " * This is a long comment that doesn't fit on one line\n" 1337 " */", getLLVMStyleWithColumns(20))); 1338 EXPECT_EQ("{\n" 1339 " if (something) /* This is a\n" 1340 " long\n" 1341 " comment */\n" 1342 " ;\n" 1343 "}", 1344 format("{\n" 1345 " if (something) /* This is a long comment */\n" 1346 " ;\n" 1347 "}", 1348 getLLVMStyleWithColumns(30))); 1349 1350 EXPECT_EQ("/* A comment before\n" 1351 " * a macro\n" 1352 " * definition */\n" 1353 "#define a b", 1354 format("/* A comment before a macro definition */\n" 1355 "#define a b", 1356 getLLVMStyleWithColumns(20))); 1357 1358 EXPECT_EQ("/* some comment\n" 1359 " * a comment\n" 1360 "* that we break\n" 1361 " * another comment\n" 1362 "* we have to break\n" 1363 "* a left comment\n" 1364 " */", 1365 format(" /* some comment\n" 1366 " * a comment that we break\n" 1367 " * another comment we have to break\n" 1368 "* a left comment\n" 1369 " */", 1370 getLLVMStyleWithColumns(20))); 1371 1372 EXPECT_EQ("/*\n" 1373 "\n" 1374 "\n" 1375 " */\n", 1376 format(" /* \n" 1377 " \n" 1378 " \n" 1379 " */\n")); 1380 1381 EXPECT_EQ("/* a a */", 1382 format("/* a a */", getLLVMStyleWithColumns(15))); 1383 EXPECT_EQ("/* a a bc */", 1384 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1385 EXPECT_EQ("/* aaa aaa\n" 1386 " * aaaaa */", 1387 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1388 EXPECT_EQ("/* aaa aaa\n" 1389 " * aaaaa */", 1390 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1391 } 1392 1393 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1394 EXPECT_EQ("#define X \\\n" 1395 " /* \\\n" 1396 " Test \\\n" 1397 " Macro comment \\\n" 1398 " with a long \\\n" 1399 " line \\\n" 1400 " */ \\\n" 1401 " A + B", 1402 format("#define X \\\n" 1403 " /*\n" 1404 " Test\n" 1405 " Macro comment with a long line\n" 1406 " */ \\\n" 1407 " A + B", 1408 getLLVMStyleWithColumns(20))); 1409 EXPECT_EQ("#define X \\\n" 1410 " /* Macro comment \\\n" 1411 " with a long \\\n" 1412 " line */ \\\n" 1413 " A + B", 1414 format("#define X \\\n" 1415 " /* Macro comment with a long\n" 1416 " line */ \\\n" 1417 " A + B", 1418 getLLVMStyleWithColumns(20))); 1419 EXPECT_EQ("#define X \\\n" 1420 " /* Macro comment \\\n" 1421 " * with a long \\\n" 1422 " * line */ \\\n" 1423 " A + B", 1424 format("#define X \\\n" 1425 " /* Macro comment with a long line */ \\\n" 1426 " A + B", 1427 getLLVMStyleWithColumns(20))); 1428 } 1429 1430 TEST_F(FormatTest, CommentsInStaticInitializers) { 1431 EXPECT_EQ( 1432 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1433 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1434 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1435 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1436 " aaaaaaaaaaaaaaaaaaaa};", 1437 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1438 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1439 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1440 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1441 " aaaaaaaaaaaaaaaaaaaa };")); 1442 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1443 " bbbbbbbbbbb, ccccccccccc};"); 1444 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1445 " // comment for bb....\n" 1446 " bbbbbbbbbbb, ccccccccccc};"); 1447 verifyGoogleFormat( 1448 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1449 " bbbbbbbbbbb, ccccccccccc};"); 1450 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1451 " // comment for bb....\n" 1452 " bbbbbbbbbbb, ccccccccccc};"); 1453 1454 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1455 " {d, e, f}, // Group #2\n" 1456 " {g, h, i}}; // Group #3"); 1457 verifyFormat("S s = {{// Group #1\n" 1458 " a, b, c},\n" 1459 " {// Group #2\n" 1460 " d, e, f},\n" 1461 " {// Group #3\n" 1462 " g, h, i}};"); 1463 1464 EXPECT_EQ("S s = {\n" 1465 " // Some comment\n" 1466 " a,\n" 1467 "\n" 1468 " // Comment after empty line\n" 1469 " b}", 1470 format("S s = {\n" 1471 " // Some comment\n" 1472 " a,\n" 1473 " \n" 1474 " // Comment after empty line\n" 1475 " b\n" 1476 "}")); 1477 EXPECT_EQ("S s = {\n" 1478 " /* Some comment */\n" 1479 " a,\n" 1480 "\n" 1481 " /* Comment after empty line */\n" 1482 " b}", 1483 format("S s = {\n" 1484 " /* Some comment */\n" 1485 " a,\n" 1486 " \n" 1487 " /* Comment after empty line */\n" 1488 " b\n" 1489 "}")); 1490 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1491 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1492 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1493 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1494 } 1495 1496 TEST_F(FormatTest, IgnoresIf0Contents) { 1497 EXPECT_EQ("#if 0\n" 1498 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1499 "#endif\n" 1500 "void f() {}", 1501 format("#if 0\n" 1502 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1503 "#endif\n" 1504 "void f( ) { }")); 1505 EXPECT_EQ("#if false\n" 1506 "void f( ) { }\n" 1507 "#endif\n" 1508 "void g() {}\n", 1509 format("#if false\n" 1510 "void f( ) { }\n" 1511 "#endif\n" 1512 "void g( ) { }\n")); 1513 EXPECT_EQ("enum E {\n" 1514 " One,\n" 1515 " Two,\n" 1516 "#if 0\n" 1517 "Three,\n" 1518 " Four,\n" 1519 "#endif\n" 1520 " Five\n" 1521 "};", 1522 format("enum E {\n" 1523 " One,Two,\n" 1524 "#if 0\n" 1525 "Three,\n" 1526 " Four,\n" 1527 "#endif\n" 1528 " Five};")); 1529 EXPECT_EQ("enum F {\n" 1530 " One,\n" 1531 "#if 1\n" 1532 " Two,\n" 1533 "#if 0\n" 1534 "Three,\n" 1535 " Four,\n" 1536 "#endif\n" 1537 " Five\n" 1538 "#endif\n" 1539 "};", 1540 format("enum F {\n" 1541 "One,\n" 1542 "#if 1\n" 1543 "Two,\n" 1544 "#if 0\n" 1545 "Three,\n" 1546 " Four,\n" 1547 "#endif\n" 1548 "Five\n" 1549 "#endif\n" 1550 "};")); 1551 EXPECT_EQ("enum G {\n" 1552 " One,\n" 1553 "#if 0\n" 1554 "Two,\n" 1555 "#else\n" 1556 " Three,\n" 1557 "#endif\n" 1558 " Four\n" 1559 "};", 1560 format("enum G {\n" 1561 "One,\n" 1562 "#if 0\n" 1563 "Two,\n" 1564 "#else\n" 1565 "Three,\n" 1566 "#endif\n" 1567 "Four\n" 1568 "};")); 1569 EXPECT_EQ("enum H {\n" 1570 " One,\n" 1571 "#if 0\n" 1572 "#ifdef Q\n" 1573 "Two,\n" 1574 "#else\n" 1575 "Three,\n" 1576 "#endif\n" 1577 "#endif\n" 1578 " Four\n" 1579 "};", 1580 format("enum H {\n" 1581 "One,\n" 1582 "#if 0\n" 1583 "#ifdef Q\n" 1584 "Two,\n" 1585 "#else\n" 1586 "Three,\n" 1587 "#endif\n" 1588 "#endif\n" 1589 "Four\n" 1590 "};")); 1591 EXPECT_EQ("enum I {\n" 1592 " One,\n" 1593 "#if /* test */ 0 || 1\n" 1594 "Two,\n" 1595 "Three,\n" 1596 "#endif\n" 1597 " Four\n" 1598 "};", 1599 format("enum I {\n" 1600 "One,\n" 1601 "#if /* test */ 0 || 1\n" 1602 "Two,\n" 1603 "Three,\n" 1604 "#endif\n" 1605 "Four\n" 1606 "};")); 1607 EXPECT_EQ("enum J {\n" 1608 " One,\n" 1609 "#if 0\n" 1610 "#if 0\n" 1611 "Two,\n" 1612 "#else\n" 1613 "Three,\n" 1614 "#endif\n" 1615 "Four,\n" 1616 "#endif\n" 1617 " Five\n" 1618 "};", 1619 format("enum J {\n" 1620 "One,\n" 1621 "#if 0\n" 1622 "#if 0\n" 1623 "Two,\n" 1624 "#else\n" 1625 "Three,\n" 1626 "#endif\n" 1627 "Four,\n" 1628 "#endif\n" 1629 "Five\n" 1630 "};")); 1631 1632 } 1633 1634 //===----------------------------------------------------------------------===// 1635 // Tests for classes, namespaces, etc. 1636 //===----------------------------------------------------------------------===// 1637 1638 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1639 verifyFormat("class A {};"); 1640 } 1641 1642 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1643 verifyFormat("class A {\n" 1644 "public:\n" 1645 "public: // comment\n" 1646 "protected:\n" 1647 "private:\n" 1648 " void f() {}\n" 1649 "};"); 1650 verifyGoogleFormat("class A {\n" 1651 " public:\n" 1652 " protected:\n" 1653 " private:\n" 1654 " void f() {}\n" 1655 "};"); 1656 verifyFormat("class A {\n" 1657 "public slots:\n" 1658 " void f() {}\n" 1659 "public Q_SLOTS:\n" 1660 " void f() {}\n" 1661 "};"); 1662 } 1663 1664 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1665 EXPECT_EQ("class A {\n" 1666 "public:\n" 1667 " void f();\n" 1668 "\n" 1669 "private:\n" 1670 " void g() {}\n" 1671 " // test\n" 1672 "protected:\n" 1673 " int h;\n" 1674 "};", 1675 format("class A {\n" 1676 "public:\n" 1677 "void f();\n" 1678 "private:\n" 1679 "void g() {}\n" 1680 "// test\n" 1681 "protected:\n" 1682 "int h;\n" 1683 "};")); 1684 EXPECT_EQ("class A {\n" 1685 "protected:\n" 1686 "public:\n" 1687 " void f();\n" 1688 "};", 1689 format("class A {\n" 1690 "protected:\n" 1691 "\n" 1692 "public:\n" 1693 "\n" 1694 " void f();\n" 1695 "};")); 1696 } 1697 1698 TEST_F(FormatTest, FormatsClasses) { 1699 verifyFormat("class A : public B {};"); 1700 verifyFormat("class A : public ::B {};"); 1701 1702 verifyFormat( 1703 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1704 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1705 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1706 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1707 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1708 verifyFormat( 1709 "class A : public B, public C, public D, public E, public F {};"); 1710 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1711 " public C,\n" 1712 " public D,\n" 1713 " public E,\n" 1714 " public F,\n" 1715 " public G {};"); 1716 1717 verifyFormat("class\n" 1718 " ReallyReallyLongClassName {\n" 1719 " int i;\n" 1720 "};", 1721 getLLVMStyleWithColumns(32)); 1722 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1723 " aaaaaaaaaaaaaaaa> {};"); 1724 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1725 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1726 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1727 verifyFormat("template <class R, class C>\n" 1728 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1729 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1730 } 1731 1732 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1733 verifyFormat("class A {\n} a, b;"); 1734 verifyFormat("struct A {\n} a, b;"); 1735 verifyFormat("union A {\n} a;"); 1736 } 1737 1738 TEST_F(FormatTest, FormatsEnum) { 1739 verifyFormat("enum {\n" 1740 " Zero,\n" 1741 " One = 1,\n" 1742 " Two = One + 1,\n" 1743 " Three = (One + Two),\n" 1744 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1745 " Five = (One, Two, Three, Four, 5)\n" 1746 "};"); 1747 verifyGoogleFormat("enum {\n" 1748 " Zero,\n" 1749 " One = 1,\n" 1750 " Two = One + 1,\n" 1751 " Three = (One + Two),\n" 1752 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1753 " Five = (One, Two, Three, Four, 5)\n" 1754 "};"); 1755 verifyFormat("enum Enum {};"); 1756 verifyFormat("enum {};"); 1757 verifyFormat("enum X E {} d;"); 1758 verifyFormat("enum __attribute__((...)) E {} d;"); 1759 verifyFormat("enum __declspec__((...)) E {} d;"); 1760 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 1761 verifyFormat("enum {\n" 1762 " Bar = Foo<int, int>::value\n" 1763 "};", 1764 getLLVMStyleWithColumns(30)); 1765 1766 verifyFormat("enum ShortEnum { A, B, C };"); 1767 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1768 } 1769 1770 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1771 verifyFormat("enum Type {\n" 1772 " One = 0; // These semicolons should be commas.\n" 1773 " Two = 1;\n" 1774 "};"); 1775 verifyFormat("namespace n {\n" 1776 "enum Type {\n" 1777 " One,\n" 1778 " Two, // missing };\n" 1779 " int i;\n" 1780 "}\n" 1781 "void g() {}"); 1782 } 1783 1784 TEST_F(FormatTest, FormatsEnumStruct) { 1785 verifyFormat("enum struct {\n" 1786 " Zero,\n" 1787 " One = 1,\n" 1788 " Two = One + 1,\n" 1789 " Three = (One + Two),\n" 1790 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1791 " Five = (One, Two, Three, Four, 5)\n" 1792 "};"); 1793 verifyFormat("enum struct Enum {};"); 1794 verifyFormat("enum struct {};"); 1795 verifyFormat("enum struct X E {} d;"); 1796 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1797 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1798 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1799 } 1800 1801 TEST_F(FormatTest, FormatsEnumClass) { 1802 verifyFormat("enum class {\n" 1803 " Zero,\n" 1804 " One = 1,\n" 1805 " Two = One + 1,\n" 1806 " Three = (One + Two),\n" 1807 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1808 " Five = (One, Two, Three, Four, 5)\n" 1809 "};"); 1810 verifyFormat("enum class Enum {};"); 1811 verifyFormat("enum class {};"); 1812 verifyFormat("enum class X E {} d;"); 1813 verifyFormat("enum class __attribute__((...)) E {} d;"); 1814 verifyFormat("enum class __declspec__((...)) E {} d;"); 1815 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1816 } 1817 1818 TEST_F(FormatTest, FormatsEnumTypes) { 1819 verifyFormat("enum X : int {\n" 1820 " A, // Force multiple lines.\n" 1821 " B\n" 1822 "};"); 1823 verifyFormat("enum X : int { A, B };"); 1824 verifyFormat("enum X : std::uint32_t { A, B };"); 1825 } 1826 1827 TEST_F(FormatTest, FormatsNSEnums) { 1828 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1829 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1830 " // Information about someDecentlyLongValue.\n" 1831 " someDecentlyLongValue,\n" 1832 " // Information about anotherDecentlyLongValue.\n" 1833 " anotherDecentlyLongValue,\n" 1834 " // Information about aThirdDecentlyLongValue.\n" 1835 " aThirdDecentlyLongValue\n" 1836 "};"); 1837 } 1838 1839 TEST_F(FormatTest, FormatsBitfields) { 1840 verifyFormat("struct Bitfields {\n" 1841 " unsigned sClass : 8;\n" 1842 " unsigned ValueKind : 2;\n" 1843 "};"); 1844 verifyFormat("struct A {\n" 1845 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1846 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1847 "};"); 1848 } 1849 1850 TEST_F(FormatTest, FormatsNamespaces) { 1851 verifyFormat("namespace some_namespace {\n" 1852 "class A {};\n" 1853 "void f() { f(); }\n" 1854 "}"); 1855 verifyFormat("namespace {\n" 1856 "class A {};\n" 1857 "void f() { f(); }\n" 1858 "}"); 1859 verifyFormat("inline namespace X {\n" 1860 "class A {};\n" 1861 "void f() { f(); }\n" 1862 "}"); 1863 verifyFormat("using namespace some_namespace;\n" 1864 "class A {};\n" 1865 "void f() { f(); }"); 1866 1867 // This code is more common than we thought; if we 1868 // layout this correctly the semicolon will go into 1869 // its own line, which is undesirable. 1870 verifyFormat("namespace {};"); 1871 verifyFormat("namespace {\n" 1872 "class A {};\n" 1873 "};"); 1874 1875 verifyFormat("namespace {\n" 1876 "int SomeVariable = 0; // comment\n" 1877 "} // namespace"); 1878 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1879 "#define HEADER_GUARD\n" 1880 "namespace my_namespace {\n" 1881 "int i;\n" 1882 "} // my_namespace\n" 1883 "#endif // HEADER_GUARD", 1884 format("#ifndef HEADER_GUARD\n" 1885 " #define HEADER_GUARD\n" 1886 " namespace my_namespace {\n" 1887 "int i;\n" 1888 "} // my_namespace\n" 1889 "#endif // HEADER_GUARD")); 1890 1891 FormatStyle Style = getLLVMStyle(); 1892 Style.NamespaceIndentation = FormatStyle::NI_All; 1893 EXPECT_EQ("namespace out {\n" 1894 " int i;\n" 1895 " namespace in {\n" 1896 " int i;\n" 1897 " } // namespace\n" 1898 "} // namespace", 1899 format("namespace out {\n" 1900 "int i;\n" 1901 "namespace in {\n" 1902 "int i;\n" 1903 "} // namespace\n" 1904 "} // namespace", 1905 Style)); 1906 1907 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1908 EXPECT_EQ("namespace out {\n" 1909 "int i;\n" 1910 "namespace in {\n" 1911 " int i;\n" 1912 "} // namespace\n" 1913 "} // namespace", 1914 format("namespace out {\n" 1915 "int i;\n" 1916 "namespace in {\n" 1917 "int i;\n" 1918 "} // namespace\n" 1919 "} // namespace", 1920 Style)); 1921 } 1922 1923 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1924 1925 TEST_F(FormatTest, FormatsInlineASM) { 1926 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1927 verifyFormat( 1928 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1929 " \"cpuid\\n\\t\"\n" 1930 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1931 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1932 " : \"a\"(value));"); 1933 } 1934 1935 TEST_F(FormatTest, FormatTryCatch) { 1936 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll 1937 // also not create single-line-blocks. 1938 verifyFormat("try {\n" 1939 " throw a * b;\n" 1940 "}\n" 1941 "catch (int a) {\n" 1942 " // Do nothing.\n" 1943 "}\n" 1944 "catch (...) {\n" 1945 " exit(42);\n" 1946 "}"); 1947 1948 // Function-level try statements. 1949 verifyFormat("int f() try { return 4; }\n" 1950 "catch (...) {\n" 1951 " return 5;\n" 1952 "}"); 1953 verifyFormat("class A {\n" 1954 " int a;\n" 1955 " A() try : a(0) {}\n" 1956 " catch (...) {\n" 1957 " throw;\n" 1958 " }\n" 1959 "};\n"); 1960 } 1961 1962 TEST_F(FormatTest, FormatObjCTryCatch) { 1963 verifyFormat("@try {\n" 1964 " f();\n" 1965 "}\n" 1966 "@catch (NSException e) {\n" 1967 " @throw;\n" 1968 "}\n" 1969 "@finally {\n" 1970 " exit(42);\n" 1971 "}"); 1972 } 1973 1974 TEST_F(FormatTest, StaticInitializers) { 1975 verifyFormat("static SomeClass SC = {1, 'a'};"); 1976 1977 verifyFormat( 1978 "static SomeClass WithALoooooooooooooooooooongName = {\n" 1979 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1980 1981 // Here, everything other than the "}" would fit on a line. 1982 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1983 " 10000000000000000000000000};"); 1984 EXPECT_EQ("S s = {a, b};", format("S s = {\n" 1985 " a,\n" 1986 "\n" 1987 " b\n" 1988 "};")); 1989 1990 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1991 // line. However, the formatting looks a bit off and this probably doesn't 1992 // happen often in practice. 1993 verifyFormat("static int Variable[1] = {\n" 1994 " {1000000000000000000000000000000000000}};", 1995 getLLVMStyleWithColumns(40)); 1996 } 1997 1998 TEST_F(FormatTest, DesignatedInitializers) { 1999 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2000 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2001 " .bbbbbbbbbb = 2,\n" 2002 " .cccccccccc = 3,\n" 2003 " .dddddddddd = 4,\n" 2004 " .eeeeeeeeee = 5};"); 2005 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2006 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2007 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2008 " .ccccccccccccccccccccccccccc = 3,\n" 2009 " .ddddddddddddddddddddddddddd = 4,\n" 2010 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2011 2012 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2013 } 2014 2015 TEST_F(FormatTest, NestedStaticInitializers) { 2016 verifyFormat("static A x = {{{}}};\n"); 2017 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2018 " {init1, init2, init3, init4}}};", 2019 getLLVMStyleWithColumns(50)); 2020 2021 verifyFormat("somes Status::global_reps[3] = {\n" 2022 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2023 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2024 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2025 getLLVMStyleWithColumns(60)); 2026 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2027 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2028 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2029 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2030 verifyFormat( 2031 "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2032 " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};"); 2033 2034 verifyFormat( 2035 "SomeArrayOfSomeType a = {\n" 2036 " {{1, 2, 3},\n" 2037 " {1, 2, 3},\n" 2038 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2039 " 333333333333333333333333333333},\n" 2040 " {1, 2, 3},\n" 2041 " {1, 2, 3}}};"); 2042 verifyFormat( 2043 "SomeArrayOfSomeType a = {\n" 2044 " {{1, 2, 3}},\n" 2045 " {{1, 2, 3}},\n" 2046 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2047 " 333333333333333333333333333333}},\n" 2048 " {{1, 2, 3}},\n" 2049 " {{1, 2, 3}}};"); 2050 2051 verifyFormat( 2052 "struct {\n" 2053 " unsigned bit;\n" 2054 " const char *const name;\n" 2055 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2056 " {kOsWin, \"Windows\"},\n" 2057 " {kOsLinux, \"Linux\"},\n" 2058 " {kOsCrOS, \"Chrome OS\"}};"); 2059 } 2060 2061 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2062 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2063 " \\\n" 2064 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2065 } 2066 2067 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2068 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2069 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2070 } 2071 2072 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2073 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2074 getLLVMStyleWithColumns(40)); 2075 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2076 getLLVMStyleWithColumns(40)); 2077 EXPECT_EQ("#define Q \\\n" 2078 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2079 " \"aaaaaaaa.cpp\"", 2080 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2081 getLLVMStyleWithColumns(40))); 2082 } 2083 2084 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2085 EXPECT_EQ("# 123 \"A string literal\"", 2086 format(" # 123 \"A string literal\"")); 2087 } 2088 2089 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2090 EXPECT_EQ("#;", format("#;")); 2091 verifyFormat("#\n;\n;\n;"); 2092 } 2093 2094 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2095 EXPECT_EQ("#line 42 \"test\"\n", 2096 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2097 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2098 getLLVMStyleWithColumns(12))); 2099 } 2100 2101 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2102 EXPECT_EQ("#line 42 \"test\"", 2103 format("# \\\n line \\\n 42 \\\n \"test\"")); 2104 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2105 } 2106 2107 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2108 verifyFormat("#define A \\x20"); 2109 verifyFormat("#define A \\ x20"); 2110 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2111 verifyFormat("#define A ''"); 2112 verifyFormat("#define A ''qqq"); 2113 verifyFormat("#define A `qqq"); 2114 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2115 EXPECT_EQ("const char *c = STRINGIFY(\n" 2116 "\\na : b);", 2117 format("const char * c = STRINGIFY(\n" 2118 "\\na : b);")); 2119 } 2120 2121 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2122 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2123 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2124 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2125 // FIXME: We never break before the macro name. 2126 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2127 2128 verifyFormat("#define A A\n#define A A"); 2129 verifyFormat("#define A(X) A\n#define A A"); 2130 2131 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2132 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2133 } 2134 2135 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2136 EXPECT_EQ("// somecomment\n" 2137 "#include \"a.h\"\n" 2138 "#define A( \\\n" 2139 " A, B)\n" 2140 "#include \"b.h\"\n" 2141 "// somecomment\n", 2142 format(" // somecomment\n" 2143 " #include \"a.h\"\n" 2144 "#define A(A,\\\n" 2145 " B)\n" 2146 " #include \"b.h\"\n" 2147 " // somecomment\n", 2148 getLLVMStyleWithColumns(13))); 2149 } 2150 2151 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2152 2153 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2154 EXPECT_EQ("#define A \\\n" 2155 " c; \\\n" 2156 " e;\n" 2157 "f;", 2158 format("#define A c; e;\n" 2159 "f;", 2160 getLLVMStyleWithColumns(14))); 2161 } 2162 2163 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2164 2165 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) { 2166 EXPECT_EQ("int i;\n" 2167 "#define A \\\n" 2168 " int i; \\\n" 2169 " int j\n" 2170 "int k;", 2171 format("int i;\n" 2172 "#define A \\\n" 2173 " int i ; \\\n" 2174 " int j\n" 2175 "int k;", 2176 8, 0, getGoogleStyle())); // 8: position of "#define". 2177 EXPECT_EQ("int i;\n" 2178 "#define A \\\n" 2179 " int i; \\\n" 2180 " int j\n" 2181 "int k;", 2182 format("int i;\n" 2183 "#define A \\\n" 2184 " int i ; \\\n" 2185 " int j\n" 2186 "int k;", 2187 45, 0, getGoogleStyle())); // 45: position of "j". 2188 } 2189 2190 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2191 EXPECT_EQ("int x,\n" 2192 "#define A\n" 2193 " y;", 2194 format("int x,\n#define A\ny;")); 2195 } 2196 2197 TEST_F(FormatTest, HashInMacroDefinition) { 2198 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2199 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2200 verifyFormat("#define A \\\n" 2201 " { \\\n" 2202 " f(#c); \\\n" 2203 " }", 2204 getLLVMStyleWithColumns(11)); 2205 2206 verifyFormat("#define A(X) \\\n" 2207 " void function##X()", 2208 getLLVMStyleWithColumns(22)); 2209 2210 verifyFormat("#define A(a, b, c) \\\n" 2211 " void a##b##c()", 2212 getLLVMStyleWithColumns(22)); 2213 2214 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2215 } 2216 2217 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2218 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2219 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2220 } 2221 2222 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2223 EXPECT_EQ("#define A b;", format("#define A \\\n" 2224 " \\\n" 2225 " b;", 2226 getLLVMStyleWithColumns(25))); 2227 EXPECT_EQ("#define A \\\n" 2228 " \\\n" 2229 " a; \\\n" 2230 " b;", 2231 format("#define A \\\n" 2232 " \\\n" 2233 " a; \\\n" 2234 " b;", 2235 getLLVMStyleWithColumns(11))); 2236 EXPECT_EQ("#define A \\\n" 2237 " a; \\\n" 2238 " \\\n" 2239 " b;", 2240 format("#define A \\\n" 2241 " a; \\\n" 2242 " \\\n" 2243 " b;", 2244 getLLVMStyleWithColumns(11))); 2245 } 2246 2247 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2248 verifyFormat("#define A :"); 2249 verifyFormat("#define SOMECASES \\\n" 2250 " case 1: \\\n" 2251 " case 2\n", 2252 getLLVMStyleWithColumns(20)); 2253 verifyFormat("#define A template <typename T>"); 2254 verifyFormat("#define STR(x) #x\n" 2255 "f(STR(this_is_a_string_literal{));"); 2256 verifyFormat("#pragma omp threadprivate( \\\n" 2257 " y)), // expected-warning", 2258 getLLVMStyleWithColumns(28)); 2259 } 2260 2261 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2262 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2263 EXPECT_EQ("class A : public QObject {\n" 2264 " Q_OBJECT\n" 2265 "\n" 2266 " A() {}\n" 2267 "};", 2268 format("class A : public QObject {\n" 2269 " Q_OBJECT\n" 2270 "\n" 2271 " A() {\n}\n" 2272 "} ;")); 2273 EXPECT_EQ("SOME_MACRO\n" 2274 "namespace {\n" 2275 "void f();\n" 2276 "}", 2277 format("SOME_MACRO\n" 2278 " namespace {\n" 2279 "void f( );\n" 2280 "}")); 2281 // Only if the identifier contains at least 5 characters. 2282 EXPECT_EQ("HTTP f();", 2283 format("HTTP\nf();")); 2284 EXPECT_EQ("MACRO\nf();", 2285 format("MACRO\nf();")); 2286 // Only if everything is upper case. 2287 EXPECT_EQ("class A : public QObject {\n" 2288 " Q_Object A() {}\n" 2289 "};", 2290 format("class A : public QObject {\n" 2291 " Q_Object\n" 2292 "\n" 2293 " A() {\n}\n" 2294 "} ;")); 2295 } 2296 2297 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2298 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2299 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2300 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2301 "class X {};\n" 2302 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2303 "int *createScopDetectionPass() { return 0; }", 2304 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2305 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2306 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2307 " class X {};\n" 2308 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2309 " int *createScopDetectionPass() { return 0; }")); 2310 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2311 // braces, so that inner block is indented one level more. 2312 EXPECT_EQ("int q() {\n" 2313 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2314 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2315 " IPC_END_MESSAGE_MAP()\n" 2316 "}", 2317 format("int q() {\n" 2318 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2319 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2320 " IPC_END_MESSAGE_MAP()\n" 2321 "}")); 2322 2323 // Same inside macros. 2324 EXPECT_EQ("#define LIST(L) \\\n" 2325 " L(A) \\\n" 2326 " L(B) \\\n" 2327 " L(C)", 2328 format("#define LIST(L) \\\n" 2329 " L(A) \\\n" 2330 " L(B) \\\n" 2331 " L(C)", 2332 getGoogleStyle())); 2333 2334 // These must not be recognized as macros. 2335 EXPECT_EQ("int q() {\n" 2336 " f(x);\n" 2337 " f(x) {}\n" 2338 " f(x)->g();\n" 2339 " f(x)->*g();\n" 2340 " f(x).g();\n" 2341 " f(x) = x;\n" 2342 " f(x) += x;\n" 2343 " f(x) -= x;\n" 2344 " f(x) *= x;\n" 2345 " f(x) /= x;\n" 2346 " f(x) %= x;\n" 2347 " f(x) &= x;\n" 2348 " f(x) |= x;\n" 2349 " f(x) ^= x;\n" 2350 " f(x) >>= x;\n" 2351 " f(x) <<= x;\n" 2352 " f(x)[y].z();\n" 2353 " LOG(INFO) << x;\n" 2354 " ifstream(x) >> x;\n" 2355 "}\n", 2356 format("int q() {\n" 2357 " f(x)\n;\n" 2358 " f(x)\n {}\n" 2359 " f(x)\n->g();\n" 2360 " f(x)\n->*g();\n" 2361 " f(x)\n.g();\n" 2362 " f(x)\n = x;\n" 2363 " f(x)\n += x;\n" 2364 " f(x)\n -= x;\n" 2365 " f(x)\n *= x;\n" 2366 " f(x)\n /= x;\n" 2367 " f(x)\n %= x;\n" 2368 " f(x)\n &= x;\n" 2369 " f(x)\n |= x;\n" 2370 " f(x)\n ^= x;\n" 2371 " f(x)\n >>= x;\n" 2372 " f(x)\n <<= x;\n" 2373 " f(x)\n[y].z();\n" 2374 " LOG(INFO)\n << x;\n" 2375 " ifstream(x)\n >> x;\n" 2376 "}\n")); 2377 EXPECT_EQ("int q() {\n" 2378 " F(x)\n" 2379 " if (1) {\n" 2380 " }\n" 2381 " F(x)\n" 2382 " while (1) {\n" 2383 " }\n" 2384 " F(x)\n" 2385 " G(x);\n" 2386 " F(x)\n" 2387 " try {\n" 2388 " Q();\n" 2389 " }\n" 2390 " catch (...) {\n" 2391 " }\n" 2392 "}\n", 2393 format("int q() {\n" 2394 "F(x)\n" 2395 "if (1) {}\n" 2396 "F(x)\n" 2397 "while (1) {}\n" 2398 "F(x)\n" 2399 "G(x);\n" 2400 "F(x)\n" 2401 "try { Q(); } catch (...) {}\n" 2402 "}\n")); 2403 EXPECT_EQ("class A {\n" 2404 " A() : t(0) {}\n" 2405 " A(X x)\n" // FIXME: function-level try blocks are broken. 2406 " try : t(0) {\n" 2407 " }\n" 2408 " catch (...) {\n" 2409 " }\n" 2410 "};", 2411 format("class A {\n" 2412 " A()\n : t(0) {}\n" 2413 " A(X x)\n" 2414 " try : t(0) {} catch (...) {}\n" 2415 "};")); 2416 EXPECT_EQ( 2417 "class SomeClass {\n" 2418 "public:\n" 2419 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2420 "};", 2421 format("class SomeClass {\n" 2422 "public:\n" 2423 " SomeClass()\n" 2424 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2425 "};")); 2426 EXPECT_EQ( 2427 "class SomeClass {\n" 2428 "public:\n" 2429 " SomeClass()\n" 2430 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2431 "};", 2432 format("class SomeClass {\n" 2433 "public:\n" 2434 " SomeClass()\n" 2435 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2436 "};", getLLVMStyleWithColumns(40))); 2437 } 2438 2439 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2440 verifyFormat("#define A \\\n" 2441 " f({ \\\n" 2442 " g(); \\\n" 2443 " });", getLLVMStyleWithColumns(11)); 2444 } 2445 2446 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2447 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2448 } 2449 2450 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2451 verifyFormat("{\n { a #c; }\n}"); 2452 } 2453 2454 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2455 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2456 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2457 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2458 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2459 } 2460 2461 TEST_F(FormatTest, EscapedNewlineAtStartOfToken) { 2462 EXPECT_EQ( 2463 "#define A \\\n int i; \\\n int j;", 2464 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2465 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2466 } 2467 2468 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) { 2469 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 2470 } 2471 2472 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2473 verifyFormat("#define A \\\n" 2474 " int v( \\\n" 2475 " a); \\\n" 2476 " int i;", 2477 getLLVMStyleWithColumns(11)); 2478 } 2479 2480 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2481 EXPECT_EQ( 2482 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2483 " \\\n" 2484 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2485 "\n" 2486 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2487 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2488 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2489 "\\\n" 2490 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2491 " \n" 2492 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2493 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2494 } 2495 2496 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2497 EXPECT_EQ("int\n" 2498 "#define A\n" 2499 " a;", 2500 format("int\n#define A\na;", getGoogleStyle())); 2501 verifyFormat("functionCallTo(\n" 2502 " someOtherFunction(\n" 2503 " withSomeParameters, whichInSequence,\n" 2504 " areLongerThanALine(andAnotherCall,\n" 2505 "#define A B\n" 2506 " withMoreParamters,\n" 2507 " whichStronglyInfluenceTheLayout),\n" 2508 " andMoreParameters),\n" 2509 " trailing);", 2510 getLLVMStyleWithColumns(69)); 2511 verifyFormat("Foo::Foo()\n" 2512 "#ifdef BAR\n" 2513 " : baz(0)\n" 2514 "#endif\n" 2515 "{\n" 2516 "}"); 2517 verifyFormat("void f() {\n" 2518 " if (true)\n" 2519 "#ifdef A\n" 2520 " f(42);\n" 2521 " x();\n" 2522 "#else\n" 2523 " g();\n" 2524 " x();\n" 2525 "#endif\n" 2526 "}"); 2527 verifyFormat("void f(param1, param2,\n" 2528 " param3,\n" 2529 "#ifdef A\n" 2530 " param4(param5,\n" 2531 "#ifdef A1\n" 2532 " param6,\n" 2533 "#ifdef A2\n" 2534 " param7),\n" 2535 "#else\n" 2536 " param8),\n" 2537 " param9,\n" 2538 "#endif\n" 2539 " param10,\n" 2540 "#endif\n" 2541 " param11)\n" 2542 "#else\n" 2543 " param12)\n" 2544 "#endif\n" 2545 "{\n" 2546 " x();\n" 2547 "}", 2548 getLLVMStyleWithColumns(28)); 2549 verifyFormat("#if 1\n" 2550 "int i;"); 2551 verifyFormat( 2552 "#if 1\n" 2553 "#endif\n" 2554 "#if 1\n" 2555 "#else\n" 2556 "#endif\n"); 2557 verifyFormat("DEBUG({\n" 2558 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2560 "});\n" 2561 "#if a\n" 2562 "#else\n" 2563 "#endif"); 2564 } 2565 2566 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2567 verifyFormat("#endif\n" 2568 "#if B"); 2569 } 2570 2571 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2572 FormatStyle SingleLine = getLLVMStyle(); 2573 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2574 verifyFormat( 2575 "#if 0\n" 2576 "#elif 1\n" 2577 "#endif\n" 2578 "void foo() {\n" 2579 " if (test) foo2();\n" 2580 "}", 2581 SingleLine); 2582 } 2583 2584 TEST_F(FormatTest, LayoutBlockInsideParens) { 2585 EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );")); 2586 EXPECT_EQ("functionCall({\n" 2587 " int i;\n" 2588 " int j;\n" 2589 "});", 2590 format(" functionCall ( {int i;int j;} );")); 2591 EXPECT_EQ("functionCall({\n" 2592 " int i;\n" 2593 " int j;\n" 2594 " },\n" 2595 " aaaa, bbbb, cccc);", 2596 format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);")); 2597 EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", 2598 format(" functionCall (aaaa, bbbb, {int i;});")); 2599 EXPECT_EQ("functionCall(aaaa, bbbb, {\n" 2600 " int i;\n" 2601 " int j;\n" 2602 "});", 2603 format(" functionCall (aaaa, bbbb, {int i;int j;});")); 2604 EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", 2605 format(" functionCall (aaaa, bbbb, {int i;});")); 2606 verifyFormat( 2607 "Aaa({\n" 2608 " int i; // break\n" 2609 " },\n" 2610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2611 " ccccccccccccccccc));"); 2612 verifyFormat("DEBUG({\n" 2613 " if (a)\n" 2614 " f();\n" 2615 "});"); 2616 } 2617 2618 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2619 EXPECT_EQ("SOME_MACRO { int i; }\n" 2620 "int i;", 2621 format(" SOME_MACRO {int i;} int i;")); 2622 } 2623 2624 TEST_F(FormatTest, LayoutNestedBlocks) { 2625 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2626 " struct s {\n" 2627 " int i;\n" 2628 " };\n" 2629 " s kBitsToOs[] = {{10}};\n" 2630 " for (int i = 0; i < 10; ++i)\n" 2631 " return;\n" 2632 "}"); 2633 verifyFormat("call(parameter, {\n" 2634 " something();\n" 2635 " // Comment using all columns.\n" 2636 " somethingelse();\n" 2637 "});", 2638 getLLVMStyleWithColumns(40)); 2639 verifyFormat("DEBUG( //\n" 2640 " { f(); }, a);"); 2641 verifyFormat("DEBUG( //\n" 2642 " {\n" 2643 " f(); //\n" 2644 " },\n" 2645 " a);"); 2646 2647 EXPECT_EQ("call(parameter, {\n" 2648 " something();\n" 2649 " // Comment too\n" 2650 " // looooooooooong.\n" 2651 " somethingElse();\n" 2652 "});", 2653 format("call(parameter, {\n" 2654 " something();\n" 2655 " // Comment too looooooooooong.\n" 2656 " somethingElse();\n" 2657 "});", 2658 getLLVMStyleWithColumns(29))); 2659 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2660 EXPECT_EQ("DEBUG({ // comment\n" 2661 " int i;\n" 2662 "});", 2663 format("DEBUG({ // comment\n" 2664 "int i;\n" 2665 "});")); 2666 EXPECT_EQ("DEBUG({\n" 2667 " int i;\n" 2668 "\n" 2669 " // comment\n" 2670 " int j;\n" 2671 "});", 2672 format("DEBUG({\n" 2673 " int i;\n" 2674 "\n" 2675 " // comment\n" 2676 " int j;\n" 2677 "});")); 2678 2679 verifyFormat("DEBUG({\n" 2680 " if (a)\n" 2681 " return;\n" 2682 "});"); 2683 verifyGoogleFormat("DEBUG({\n" 2684 " if (a) return;\n" 2685 "});"); 2686 FormatStyle Style = getGoogleStyle(); 2687 Style.ColumnLimit = 45; 2688 verifyFormat("Debug(aaaaa, {\n" 2689 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 2690 " return;\n" 2691 " },\n" 2692 " a);", Style); 2693 } 2694 2695 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) { 2696 EXPECT_EQ("DEBUG({\n" 2697 " int i;\n" 2698 " int j;\n" 2699 "});", 2700 format("DEBUG( {\n" 2701 " int i;\n" 2702 " int j;\n" 2703 "} ) ;", 2704 20, 1, getLLVMStyle())); 2705 EXPECT_EQ("DEBUG( {\n" 2706 " int i;\n" 2707 " int j;\n" 2708 "} ) ;", 2709 format("DEBUG( {\n" 2710 " int i;\n" 2711 " int j;\n" 2712 "} ) ;", 2713 41, 1, getLLVMStyle())); 2714 EXPECT_EQ("DEBUG( {\n" 2715 " int i;\n" 2716 " int j;\n" 2717 "} ) ;", 2718 format("DEBUG( {\n" 2719 " int i;\n" 2720 " int j;\n" 2721 "} ) ;", 2722 41, 1, getLLVMStyle())); 2723 EXPECT_EQ("DEBUG({\n" 2724 " int i;\n" 2725 " int j;\n" 2726 "});", 2727 format("DEBUG( {\n" 2728 " int i;\n" 2729 " int j;\n" 2730 "} ) ;", 2731 20, 1, getLLVMStyle())); 2732 2733 EXPECT_EQ("Debug({\n" 2734 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 2735 " return;\n" 2736 " },\n" 2737 " a);", 2738 format("Debug({\n" 2739 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 2740 " return;\n" 2741 " },\n" 2742 " a);", 2743 50, 1, getLLVMStyle())); 2744 EXPECT_EQ("DEBUG({\n" 2745 " DEBUG({\n" 2746 " int a;\n" 2747 " int b;\n" 2748 " }) ;\n" 2749 "});", 2750 format("DEBUG({\n" 2751 " DEBUG({\n" 2752 " int a;\n" 2753 " int b;\n" // Format this line only. 2754 " }) ;\n" // Don't touch this line. 2755 "});", 2756 35, 0, getLLVMStyle())); 2757 EXPECT_EQ("DEBUG({\n" 2758 " int a; //\n" 2759 "});", 2760 format("DEBUG({\n" 2761 " int a; //\n" 2762 "});", 2763 0, 0, getLLVMStyle())); 2764 } 2765 2766 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2767 EXPECT_EQ("{}", format("{}")); 2768 verifyFormat("enum E {};"); 2769 verifyFormat("enum E {}"); 2770 } 2771 2772 //===----------------------------------------------------------------------===// 2773 // Line break tests. 2774 //===----------------------------------------------------------------------===// 2775 2776 TEST_F(FormatTest, PreventConfusingIndents) { 2777 verifyFormat( 2778 "void f() {\n" 2779 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2780 " parameter, parameter, parameter)),\n" 2781 " SecondLongCall(parameter));\n" 2782 "}"); 2783 verifyFormat( 2784 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2785 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2787 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2788 verifyFormat( 2789 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2790 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2791 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2792 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2793 verifyFormat( 2794 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2795 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2797 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2798 verifyFormat("int a = bbbb && ccc && fffff(\n" 2799 "#define A Just forcing a new line\n" 2800 " ddd);"); 2801 } 2802 2803 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2804 verifyFormat( 2805 "bool aaaaaaa =\n" 2806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2807 " bbbbbbbb();"); 2808 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2810 " ccccccccc == ddddddddddd;"); 2811 2812 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2813 " aaaaaa) &&\n" 2814 " bbbbbb && cccccc;"); 2815 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2816 " aaaaaa) >>\n" 2817 " bbbbbb;"); 2818 verifyFormat("Whitespaces.addUntouchableComment(\n" 2819 " SourceMgr.getSpellingColumnNumber(\n" 2820 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2821 " 1);"); 2822 2823 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2824 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2825 " cccccc) {\n}"); 2826 2827 // If the LHS of a comparison is not a binary expression itself, the 2828 // additional linebreak confuses many people. 2829 verifyFormat( 2830 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 2832 "}"); 2833 verifyFormat( 2834 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2836 "}"); 2837 verifyFormat( 2838 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 2839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2840 "}"); 2841 // Even explicit parentheses stress the precedence enough to make the 2842 // additional break unnecessary. 2843 verifyFormat( 2844 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2846 "}"); 2847 // This cases is borderline, but with the indentation it is still readable. 2848 verifyFormat( 2849 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2850 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2852 "}", 2853 getLLVMStyleWithColumns(75)); 2854 2855 // If the LHS is a binary expression, we should still use the additional break 2856 // as otherwise the formatting hides the operator precedence. 2857 verifyFormat( 2858 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2860 " 5) {\n" 2861 "}"); 2862 2863 FormatStyle OnePerLine = getLLVMStyle(); 2864 OnePerLine.BinPackParameters = false; 2865 verifyFormat( 2866 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2868 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2869 OnePerLine); 2870 } 2871 2872 TEST_F(FormatTest, ExpressionIndentation) { 2873 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2877 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2878 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2881 " ccccccccccccccccccccccccccccccccccccccccc;"); 2882 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2885 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2886 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2889 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2890 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2893 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2894 verifyFormat("if () {\n" 2895 "} else if (aaaaa &&\n" 2896 " bbbbb > // break\n" 2897 " ccccc) {\n" 2898 "}"); 2899 2900 // Presence of a trailing comment used to change indentation of b. 2901 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 2902 " b;\n" 2903 "return aaaaaaaaaaaaaaaaaaa +\n" 2904 " b; //", 2905 getLLVMStyleWithColumns(30)); 2906 } 2907 2908 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 2909 // Not sure what the best system is here. Like this, the LHS can be found 2910 // immediately above an operator (everything with the same or a higher 2911 // indent). The RHS is aligned right of the operator and so compasses 2912 // everything until something with the same indent as the operator is found. 2913 // FIXME: Is this a good system? 2914 FormatStyle Style = getLLVMStyle(); 2915 Style.BreakBeforeBinaryOperators = true; 2916 verifyFormat( 2917 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2918 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2919 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2920 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2921 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2922 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2923 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2924 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2925 " > ccccccccccccccccccccccccccccccccccccccccc;", 2926 Style); 2927 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2928 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2929 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2930 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2931 Style); 2932 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2933 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2934 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2935 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2936 Style); 2937 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2938 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2939 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2940 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2941 Style); 2942 verifyFormat("if () {\n" 2943 "} else if (aaaaa && bbbbb // break\n" 2944 " > ccccc) {\n" 2945 "}", 2946 Style); 2947 2948 // Forced by comments. 2949 verifyFormat( 2950 "unsigned ContentSize =\n" 2951 " sizeof(int16_t) // DWARF ARange version number\n" 2952 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 2953 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 2954 " + sizeof(int8_t); // Segment Size (in bytes)"); 2955 2956 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 2957 " == boost::fusion::at_c<1>(iiii).second;", 2958 Style); 2959 } 2960 2961 TEST_F(FormatTest, ConstructorInitializers) { 2962 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2963 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2964 getLLVMStyleWithColumns(45)); 2965 verifyFormat("Constructor()\n" 2966 " : Inttializer(FitsOnTheLine) {}", 2967 getLLVMStyleWithColumns(44)); 2968 verifyFormat("Constructor()\n" 2969 " : Inttializer(FitsOnTheLine) {}", 2970 getLLVMStyleWithColumns(43)); 2971 2972 verifyFormat( 2973 "SomeClass::Constructor()\n" 2974 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2975 2976 verifyFormat( 2977 "SomeClass::Constructor()\n" 2978 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2979 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2980 verifyFormat( 2981 "SomeClass::Constructor()\n" 2982 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2983 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2984 2985 verifyFormat("Constructor()\n" 2986 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2987 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2988 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2989 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 2990 2991 verifyFormat("Constructor()\n" 2992 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2993 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2994 2995 verifyFormat("Constructor(int Parameter = 0)\n" 2996 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2997 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 2998 verifyFormat("Constructor()\n" 2999 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3000 "}", 3001 getLLVMStyleWithColumns(60)); 3002 verifyFormat("Constructor()\n" 3003 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3004 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3005 3006 // Here a line could be saved by splitting the second initializer onto two 3007 // lines, but that is not desirable. 3008 verifyFormat("Constructor()\n" 3009 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3010 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3011 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3012 3013 FormatStyle OnePerLine = getLLVMStyle(); 3014 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3015 verifyFormat("SomeClass::Constructor()\n" 3016 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3017 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3018 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3019 OnePerLine); 3020 verifyFormat("SomeClass::Constructor()\n" 3021 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3022 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3023 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3024 OnePerLine); 3025 verifyFormat("MyClass::MyClass(int var)\n" 3026 " : some_var_(var), // 4 space indent\n" 3027 " some_other_var_(var + 1) { // lined up\n" 3028 "}", 3029 OnePerLine); 3030 verifyFormat("Constructor()\n" 3031 " : aaaaa(aaaaaa),\n" 3032 " aaaaa(aaaaaa),\n" 3033 " aaaaa(aaaaaa),\n" 3034 " aaaaa(aaaaaa),\n" 3035 " aaaaa(aaaaaa) {}", 3036 OnePerLine); 3037 verifyFormat("Constructor()\n" 3038 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3039 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3040 OnePerLine); 3041 3042 EXPECT_EQ("Constructor()\n" 3043 " : // Comment forcing unwanted break.\n" 3044 " aaaa(aaaa) {}", 3045 format("Constructor() :\n" 3046 " // Comment forcing unwanted break.\n" 3047 " aaaa(aaaa) {}")); 3048 } 3049 3050 TEST_F(FormatTest, MemoizationTests) { 3051 // This breaks if the memoization lookup does not take \c Indent and 3052 // \c LastSpace into account. 3053 verifyFormat( 3054 "extern CFRunLoopTimerRef\n" 3055 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3056 " CFTimeInterval interval, CFOptionFlags flags,\n" 3057 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3058 " CFRunLoopTimerContext *context) {}"); 3059 3060 // Deep nesting somewhat works around our memoization. 3061 verifyFormat( 3062 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3063 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3064 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3065 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3066 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3067 getLLVMStyleWithColumns(65)); 3068 verifyFormat( 3069 "aaaaa(\n" 3070 " aaaaa,\n" 3071 " aaaaa(\n" 3072 " aaaaa,\n" 3073 " aaaaa(\n" 3074 " aaaaa,\n" 3075 " aaaaa(\n" 3076 " aaaaa,\n" 3077 " aaaaa(\n" 3078 " aaaaa,\n" 3079 " aaaaa(\n" 3080 " aaaaa,\n" 3081 " aaaaa(\n" 3082 " aaaaa,\n" 3083 " aaaaa(\n" 3084 " aaaaa,\n" 3085 " aaaaa(\n" 3086 " aaaaa,\n" 3087 " aaaaa(\n" 3088 " aaaaa,\n" 3089 " aaaaa(\n" 3090 " aaaaa,\n" 3091 " aaaaa(\n" 3092 " aaaaa,\n" 3093 " aaaaa))))))))))));", 3094 getLLVMStyleWithColumns(65)); 3095 verifyFormat( 3096 "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" 3097 " a),\n" 3098 " a),\n" 3099 " a),\n" 3100 " a),\n" 3101 " a),\n" 3102 " a),\n" 3103 " a),\n" 3104 " a),\n" 3105 " a),\n" 3106 " a),\n" 3107 " a),\n" 3108 " a),\n" 3109 " a),\n" 3110 " a),\n" 3111 " a),\n" 3112 " a),\n" 3113 " a)", 3114 getLLVMStyleWithColumns(65)); 3115 3116 // This test takes VERY long when memoization is broken. 3117 FormatStyle OnePerLine = getLLVMStyle(); 3118 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3119 OnePerLine.BinPackParameters = false; 3120 std::string input = "Constructor()\n" 3121 " : aaaa(a,\n"; 3122 for (unsigned i = 0, e = 80; i != e; ++i) { 3123 input += " a,\n"; 3124 } 3125 input += " a) {}"; 3126 verifyFormat(input, OnePerLine); 3127 } 3128 3129 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3130 verifyFormat( 3131 "void f() {\n" 3132 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3133 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3134 " f();\n" 3135 "}"); 3136 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3137 " Intervals[i - 1].getRange().getLast()) {\n}"); 3138 } 3139 3140 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3141 // Principially, we break function declarations in a certain order: 3142 // 1) break amongst arguments. 3143 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3144 " Cccccccccccccc cccccccccccccc);"); 3145 verifyFormat( 3146 "template <class TemplateIt>\n" 3147 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3148 " TemplateIt *stop) {}"); 3149 3150 // 2) break after return type. 3151 verifyFormat( 3152 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3153 " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3154 getGoogleStyle()); 3155 3156 // 3) break after (. 3157 verifyFormat( 3158 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3159 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3160 getGoogleStyle()); 3161 3162 // 4) break before after nested name specifiers. 3163 verifyFormat( 3164 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3165 " SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3166 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3167 getGoogleStyle()); 3168 3169 // However, there are exceptions, if a sufficient amount of lines can be 3170 // saved. 3171 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3172 // more adjusting. 3173 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3174 " Cccccccccccccc cccccccccc,\n" 3175 " Cccccccccccccc cccccccccc,\n" 3176 " Cccccccccccccc cccccccccc,\n" 3177 " Cccccccccccccc cccccccccc);"); 3178 verifyFormat( 3179 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3180 " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3181 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3182 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3183 getGoogleStyle()); 3184 verifyFormat( 3185 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3186 " Cccccccccccccc cccccccccc,\n" 3187 " Cccccccccccccc cccccccccc,\n" 3188 " Cccccccccccccc cccccccccc,\n" 3189 " Cccccccccccccc cccccccccc,\n" 3190 " Cccccccccccccc cccccccccc,\n" 3191 " Cccccccccccccc cccccccccc);"); 3192 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3193 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3194 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3195 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3196 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3197 3198 // Break after multi-line parameters. 3199 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3202 " bbbb bbbb);"); 3203 3204 // Treat overloaded operators like other functions. 3205 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3206 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3207 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3208 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3209 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3210 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3211 verifyGoogleFormat( 3212 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3213 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3214 verifyGoogleFormat( 3215 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3216 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3217 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3218 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3219 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3220 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3221 } 3222 3223 TEST_F(FormatTest, TrailingReturnType) { 3224 verifyFormat("auto foo() -> int;\n"); 3225 verifyFormat("struct S {\n" 3226 " auto bar() const -> int;\n" 3227 "};"); 3228 verifyFormat("template <size_t Order, typename T>\n" 3229 "auto load_img(const std::string &filename)\n" 3230 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3231 3232 // Not trailing return types. 3233 verifyFormat("void f() { auto a = b->c(); }"); 3234 } 3235 3236 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3237 // Avoid breaking before trailing 'const' or other trailing annotations, if 3238 // they are not function-like. 3239 FormatStyle Style = getGoogleStyle(); 3240 Style.ColumnLimit = 47; 3241 verifyFormat("void\n" 3242 "someLongFunction(int someLongParameter) const {\n}", 3243 getLLVMStyleWithColumns(47)); 3244 verifyFormat("LoooooongReturnType\n" 3245 "someLoooooooongFunction() const {}", 3246 getLLVMStyleWithColumns(47)); 3247 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3248 " const {}", 3249 Style); 3250 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3251 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3252 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3253 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3254 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3255 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3256 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3257 " aaaaaaaaaaa aaaaa) const override;"); 3258 verifyGoogleFormat( 3259 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3260 " const override;"); 3261 3262 // Even if the first parameter has to be wrapped. 3263 verifyFormat("void someLongFunction(\n" 3264 " int someLongParameter) const {}", 3265 getLLVMStyleWithColumns(46)); 3266 verifyFormat("void someLongFunction(\n" 3267 " int someLongParameter) const {}", 3268 Style); 3269 verifyFormat("void someLongFunction(\n" 3270 " int someLongParameter) override {}", 3271 Style); 3272 verifyFormat("void someLongFunction(\n" 3273 " int someLongParameter) OVERRIDE {}", 3274 Style); 3275 verifyFormat("void someLongFunction(\n" 3276 " int someLongParameter) final {}", 3277 Style); 3278 verifyFormat("void someLongFunction(\n" 3279 " int someLongParameter) FINAL {}", 3280 Style); 3281 verifyFormat("void someLongFunction(\n" 3282 " int parameter) const override {}", 3283 Style); 3284 3285 // Unless these are unknown annotations. 3286 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3287 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3288 " LONG_AND_UGLY_ANNOTATION;"); 3289 3290 // Breaking before function-like trailing annotations is fine to keep them 3291 // close to their arguments. 3292 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3293 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3294 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3295 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3296 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3297 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3298 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3299 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3300 3301 verifyFormat( 3302 "void aaaaaaaaaaaaaaaaaa()\n" 3303 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3304 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3305 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3306 " __attribute__((unused));"); 3307 verifyFormat( 3308 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3309 " GUARDED_BY(aaaaaaaaaaaa);", 3310 getGoogleStyle()); 3311 verifyFormat( 3312 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3313 " GUARDED_BY(aaaaaaaaaaaa);", 3314 getGoogleStyle()); 3315 } 3316 3317 TEST_F(FormatTest, BreaksDesireably) { 3318 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3319 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3320 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3321 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3323 "}"); 3324 3325 verifyFormat( 3326 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3327 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3328 3329 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3332 3333 verifyFormat( 3334 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3336 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3338 3339 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3340 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3341 3342 verifyFormat( 3343 "void f() {\n" 3344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3346 "}"); 3347 verifyFormat( 3348 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3350 verifyFormat( 3351 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3353 verifyFormat( 3354 "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3357 3358 // Indent consistently independent of call expression. 3359 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3360 " dddddddddddddddddddddddddddddd));\n" 3361 "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3362 " dddddddddddddddddddddddddddddd));"); 3363 3364 // This test case breaks on an incorrect memoization, i.e. an optimization not 3365 // taking into account the StopAt value. 3366 verifyFormat( 3367 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3368 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3369 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3370 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3371 3372 verifyFormat("{\n {\n {\n" 3373 " Annotation.SpaceRequiredBefore =\n" 3374 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3375 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3376 " }\n }\n}"); 3377 3378 // Break on an outer level if there was a break on an inner level. 3379 EXPECT_EQ("f(g(h(a, // comment\n" 3380 " b, c),\n" 3381 " d, e),\n" 3382 " x, y);", 3383 format("f(g(h(a, // comment\n" 3384 " b, c), d, e), x, y);")); 3385 3386 // Prefer breaking similar line breaks. 3387 verifyFormat( 3388 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3389 " NSTrackingMouseEnteredAndExited |\n" 3390 " NSTrackingActiveAlways;"); 3391 } 3392 3393 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 3394 FormatStyle NoBinPacking = getGoogleStyle(); 3395 NoBinPacking.BinPackParameters = false; 3396 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 3397 " aaaaaaaaaaaaaaaaaaaa,\n" 3398 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 3399 NoBinPacking); 3400 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 3401 " aaaaaaaaaaaaa,\n" 3402 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 3403 NoBinPacking); 3404 verifyFormat( 3405 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3406 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3408 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 3410 NoBinPacking); 3411 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3412 " .aaaaaaaaaaaaaaaaaa();", 3413 NoBinPacking); 3414 verifyFormat("void f() {\n" 3415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3416 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 3417 "}", 3418 NoBinPacking); 3419 3420 verifyFormat( 3421 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3422 " aaaaaaaaaaaa,\n" 3423 " aaaaaaaaaaaa);", 3424 NoBinPacking); 3425 verifyFormat( 3426 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 3427 " ddddddddddddddddddddddddddddd),\n" 3428 " test);", 3429 NoBinPacking); 3430 3431 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 3433 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 3434 NoBinPacking); 3435 verifyFormat("a(\"a\"\n" 3436 " \"a\",\n" 3437 " a);"); 3438 3439 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3440 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 3441 " aaaaaaaaa,\n" 3442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3443 NoBinPacking); 3444 verifyFormat( 3445 "void f() {\n" 3446 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3447 " .aaaaaaa();\n" 3448 "}", 3449 NoBinPacking); 3450 verifyFormat( 3451 "template <class SomeType, class SomeOtherType>\n" 3452 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 3453 NoBinPacking); 3454 } 3455 3456 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 3457 FormatStyle Style = getLLVMStyleWithColumns(15); 3458 Style.ExperimentalAutoDetectBinPacking = true; 3459 EXPECT_EQ("aaa(aaaa,\n" 3460 " aaaa,\n" 3461 " aaaa);\n" 3462 "aaa(aaaa,\n" 3463 " aaaa,\n" 3464 " aaaa);", 3465 format("aaa(aaaa,\n" // one-per-line 3466 " aaaa,\n" 3467 " aaaa );\n" 3468 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3469 Style)); 3470 EXPECT_EQ("aaa(aaaa, aaaa,\n" 3471 " aaaa);\n" 3472 "aaa(aaaa, aaaa,\n" 3473 " aaaa);", 3474 format("aaa(aaaa, aaaa,\n" // bin-packed 3475 " aaaa );\n" 3476 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3477 Style)); 3478 } 3479 3480 TEST_F(FormatTest, FormatsBuilderPattern) { 3481 verifyFormat( 3482 "return llvm::StringSwitch<Reference::Kind>(name)\n" 3483 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 3484 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 3485 " .StartsWith(\".init\", ORDER_INIT)\n" 3486 " .StartsWith(\".fini\", ORDER_FINI)\n" 3487 " .StartsWith(\".hash\", ORDER_HASH)\n" 3488 " .Default(ORDER_TEXT);\n"); 3489 3490 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 3491 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 3492 verifyFormat( 3493 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n" 3494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3495 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3496 verifyFormat( 3497 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 3498 " aaaaaaaaaaaaaa);"); 3499 verifyFormat( 3500 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 3501 " aaaaaa->aaaaaaaaaaaa()\n" 3502 " ->aaaaaaaaaaaaaaaa(\n" 3503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3504 " ->aaaaaaaaaaaaaaaaa();"); 3505 verifyGoogleFormat( 3506 "void f() {\n" 3507 " someo->Add((new util::filetools::Handler(dir))\n" 3508 " ->OnEvent1(NewPermanentCallback(\n" 3509 " this, &HandlerHolderClass::EventHandlerCBA))\n" 3510 " ->OnEvent2(NewPermanentCallback(\n" 3511 " this, &HandlerHolderClass::EventHandlerCBB))\n" 3512 " ->OnEvent3(NewPermanentCallback(\n" 3513 " this, &HandlerHolderClass::EventHandlerCBC))\n" 3514 " ->OnEvent5(NewPermanentCallback(\n" 3515 " this, &HandlerHolderClass::EventHandlerCBD))\n" 3516 " ->OnEvent6(NewPermanentCallback(\n" 3517 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 3518 "}"); 3519 3520 verifyFormat( 3521 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 3522 verifyFormat("aaaaaaaaaaaaaaa()\n" 3523 " .aaaaaaaaaaaaaaa()\n" 3524 " .aaaaaaaaaaaaaaa()\n" 3525 " .aaaaaaaaaaaaaaa()\n" 3526 " .aaaaaaaaaaaaaaa();"); 3527 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3528 " .aaaaaaaaaaaaaaa()\n" 3529 " .aaaaaaaaaaaaaaa()\n" 3530 " .aaaaaaaaaaaaaaa();"); 3531 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3532 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3533 " .aaaaaaaaaaaaaaa();"); 3534 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 3535 " ->aaaaaaaaaaaaaae(0)\n" 3536 " ->aaaaaaaaaaaaaaa();"); 3537 3538 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3539 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3540 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 3541 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3542 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 3544 3545 // Prefer not to break after empty parentheses. 3546 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 3547 " First->LastNewlineOffset);"); 3548 } 3549 3550 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 3551 verifyFormat( 3552 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3553 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 3554 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3555 " ccccccccccccccccccccccccc) {\n}"); 3556 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3557 " ccccccccccccccccccccccccc) {\n}"); 3558 verifyFormat( 3559 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 3560 " ccccccccccccccccccccccccc) {\n}"); 3561 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 3562 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 3563 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 3564 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3565 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 3566 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 3567 " aaaaaaaaaaaaaaa != aa) {\n}"); 3568 } 3569 3570 TEST_F(FormatTest, BreaksAfterAssignments) { 3571 verifyFormat( 3572 "unsigned Cost =\n" 3573 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 3574 " SI->getPointerAddressSpaceee());\n"); 3575 verifyFormat( 3576 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 3577 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 3578 3579 verifyFormat( 3580 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 3581 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 3582 verifyFormat("unsigned OriginalStartColumn =\n" 3583 " SourceMgr.getSpellingColumnNumber(\n" 3584 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 3585 " 1;"); 3586 } 3587 3588 TEST_F(FormatTest, AlignsAfterAssignments) { 3589 verifyFormat( 3590 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3591 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3592 verifyFormat( 3593 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3594 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3595 verifyFormat( 3596 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3598 verifyFormat( 3599 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3600 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3601 verifyFormat( 3602 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3603 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3604 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 3605 } 3606 3607 TEST_F(FormatTest, AlignsAfterReturn) { 3608 verifyFormat( 3609 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3610 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3611 verifyFormat( 3612 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3613 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3614 verifyFormat( 3615 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3616 " aaaaaaaaaaaaaaaaaaaaaa();"); 3617 verifyFormat( 3618 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3619 " aaaaaaaaaaaaaaaaaaaaaa());"); 3620 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3622 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 3624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3625 verifyFormat("return\n" 3626 " // true if code is one of a or b.\n" 3627 " code == a || code == b;"); 3628 } 3629 3630 TEST_F(FormatTest, BreaksConditionalExpressions) { 3631 verifyFormat( 3632 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3633 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3634 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3635 verifyFormat( 3636 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3637 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3638 verifyFormat( 3639 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 3640 " : aaaaaaaaaaaaa);"); 3641 verifyFormat( 3642 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3643 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3644 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3645 " aaaaaaaaaaaaa);"); 3646 verifyFormat( 3647 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3648 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3649 " aaaaaaaaaaaaa);"); 3650 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3651 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3653 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3655 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3657 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3659 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3661 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3662 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3664 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3666 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3667 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3668 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3669 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3670 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 3671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3672 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3673 " : aaaaaaaaaaaaaaaa;"); 3674 verifyFormat( 3675 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3676 " ? aaaaaaaaaaaaaaa\n" 3677 " : aaaaaaaaaaaaaaa;"); 3678 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 3679 " aaaaaaaaa\n" 3680 " ? b\n" 3681 " : c);"); 3682 verifyFormat( 3683 "unsigned Indent =\n" 3684 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 3685 " ? IndentForLevel[TheLine.Level]\n" 3686 " : TheLine * 2,\n" 3687 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 3688 getLLVMStyleWithColumns(70)); 3689 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 3690 " ? aaaaaaaaaaaaaaa\n" 3691 " : bbbbbbbbbbbbbbb //\n" 3692 " ? ccccccccccccccc\n" 3693 " : ddddddddddddddd;"); 3694 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 3695 " ? aaaaaaaaaaaaaaa\n" 3696 " : (bbbbbbbbbbbbbbb //\n" 3697 " ? ccccccccccccccc\n" 3698 " : ddddddddddddddd);"); 3699 3700 FormatStyle NoBinPacking = getLLVMStyle(); 3701 NoBinPacking.BinPackParameters = false; 3702 verifyFormat( 3703 "void f() {\n" 3704 " g(aaa,\n" 3705 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 3706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3707 " ? aaaaaaaaaaaaaaa\n" 3708 " : aaaaaaaaaaaaaaa);\n" 3709 "}", 3710 NoBinPacking); 3711 verifyFormat( 3712 "void f() {\n" 3713 " g(aaa,\n" 3714 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 3715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3716 " ?: aaaaaaaaaaaaaaa);\n" 3717 "}", 3718 NoBinPacking); 3719 } 3720 3721 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 3722 FormatStyle Style = getLLVMStyle(); 3723 Style.BreakBeforeTernaryOperators = false; 3724 Style.ColumnLimit = 70; 3725 verifyFormat( 3726 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3729 Style); 3730 verifyFormat( 3731 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3733 Style); 3734 verifyFormat( 3735 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 3736 " aaaaaaaaaaaaa);", 3737 Style); 3738 verifyFormat( 3739 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3740 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3742 " aaaaaaaaaaaaa);", 3743 Style); 3744 verifyFormat( 3745 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3746 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3747 " aaaaaaaaaaaaa);", 3748 Style); 3749 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3754 Style); 3755 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3761 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3762 Style); 3763 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 3765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3767 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3768 Style); 3769 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3770 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3771 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3772 Style); 3773 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 3774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3777 Style); 3778 verifyFormat( 3779 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3780 " aaaaaaaaaaaaaaa :\n" 3781 " aaaaaaaaaaaaaaa;", 3782 Style); 3783 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 3784 " aaaaaaaaa ?\n" 3785 " b :\n" 3786 " c);", 3787 Style); 3788 verifyFormat( 3789 "unsigned Indent =\n" 3790 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 3791 " IndentForLevel[TheLine.Level] :\n" 3792 " TheLine * 2,\n" 3793 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 3794 Style); 3795 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 3796 " aaaaaaaaaaaaaaa :\n" 3797 " bbbbbbbbbbbbbbb ? //\n" 3798 " ccccccccccccccc :\n" 3799 " ddddddddddddddd;", 3800 Style); 3801 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 3802 " aaaaaaaaaaaaaaa :\n" 3803 " (bbbbbbbbbbbbbbb ? //\n" 3804 " ccccccccccccccc :\n" 3805 " ddddddddddddddd);", 3806 Style); 3807 } 3808 3809 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 3810 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 3811 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 3812 verifyFormat("bool a = true, b = false;"); 3813 3814 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 3816 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 3817 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 3818 verifyFormat( 3819 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3820 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 3821 " d = e && f;"); 3822 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 3823 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 3824 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 3825 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 3826 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 3827 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 3828 // FIXME: If multiple variables are defined, the "*" needs to move to the new 3829 // line. Also fix indent for breaking after the type, this looks bad. 3830 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 3832 " *b = bbbbbbbbbbbbbbbbbbb;", 3833 getGoogleStyle()); 3834 3835 // Not ideal, but pointer-with-type does not allow much here. 3836 verifyGoogleFormat( 3837 "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 3838 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;"); 3839 } 3840 3841 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 3842 verifyFormat("arr[foo ? bar : baz];"); 3843 verifyFormat("f()[foo ? bar : baz];"); 3844 verifyFormat("(a + b)[foo ? bar : baz];"); 3845 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 3846 } 3847 3848 TEST_F(FormatTest, AlignsStringLiterals) { 3849 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 3850 " \"short literal\");"); 3851 verifyFormat( 3852 "looooooooooooooooooooooooongFunction(\n" 3853 " \"short literal\"\n" 3854 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 3855 verifyFormat("someFunction(\"Always break between multi-line\"\n" 3856 " \" string literals\",\n" 3857 " and, other, parameters);"); 3858 EXPECT_EQ("fun + \"1243\" /* comment */\n" 3859 " \"5678\";", 3860 format("fun + \"1243\" /* comment */\n" 3861 " \"5678\";", 3862 getLLVMStyleWithColumns(28))); 3863 EXPECT_EQ( 3864 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 3865 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 3866 " \"aaaaaaaaaaaaaaaa\";", 3867 format("aaaaaa =" 3868 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 3869 "aaaaaaaaaaaaaaaaaaaaa\" " 3870 "\"aaaaaaaaaaaaaaaa\";")); 3871 verifyFormat("a = a + \"a\"\n" 3872 " \"a\"\n" 3873 " \"a\";"); 3874 verifyFormat("f(\"a\", \"b\"\n" 3875 " \"c\");"); 3876 3877 verifyFormat( 3878 "#define LL_FORMAT \"ll\"\n" 3879 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 3880 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 3881 3882 verifyFormat("#define A(X) \\\n" 3883 " \"aaaaa\" #X \"bbbbbb\" \\\n" 3884 " \"ccccc\"", 3885 getLLVMStyleWithColumns(23)); 3886 verifyFormat("#define A \"def\"\n" 3887 "f(\"abc\" A \"ghi\"\n" 3888 " \"jkl\");"); 3889 3890 verifyFormat("f(L\"a\"\n" 3891 " L\"b\")"); 3892 verifyFormat("#define A(X) \\\n" 3893 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 3894 " L\"ccccc\"", 3895 getLLVMStyleWithColumns(25)); 3896 } 3897 3898 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 3899 FormatStyle NoBreak = getLLVMStyle(); 3900 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 3901 FormatStyle Break = getLLVMStyle(); 3902 Break.AlwaysBreakBeforeMultilineStrings = true; 3903 verifyFormat("aaaa = \"bbbb\"\n" 3904 " \"cccc\";", 3905 NoBreak); 3906 verifyFormat("aaaa =\n" 3907 " \"bbbb\"\n" 3908 " \"cccc\";", 3909 Break); 3910 verifyFormat("aaaa(\"bbbb\"\n" 3911 " \"cccc\");", 3912 NoBreak); 3913 verifyFormat("aaaa(\n" 3914 " \"bbbb\"\n" 3915 " \"cccc\");", 3916 Break); 3917 verifyFormat("aaaa(qqq, \"bbbb\"\n" 3918 " \"cccc\");", 3919 NoBreak); 3920 verifyFormat("aaaa(qqq,\n" 3921 " \"bbbb\"\n" 3922 " \"cccc\");", 3923 Break); 3924 verifyFormat("aaaa(qqq,\n" 3925 " L\"bbbb\"\n" 3926 " L\"cccc\");", 3927 Break); 3928 3929 // Don't break if there is no column gain. 3930 verifyFormat("f(\"aaaa\"\n" 3931 " \"bbbb\");", 3932 Break); 3933 3934 // Treat literals with escaped newlines like multi-line string literals. 3935 EXPECT_EQ("x = \"a\\\n" 3936 "b\\\n" 3937 "c\";", 3938 format("x = \"a\\\n" 3939 "b\\\n" 3940 "c\";", 3941 NoBreak)); 3942 EXPECT_EQ("x =\n" 3943 " \"a\\\n" 3944 "b\\\n" 3945 "c\";", 3946 format("x = \"a\\\n" 3947 "b\\\n" 3948 "c\";", 3949 Break)); 3950 3951 // Exempt ObjC strings for now. 3952 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 3953 " \"bbbb\";", 3954 format("NSString *const kString = @\"aaaa\"\n" 3955 "\"bbbb\";", 3956 Break)); 3957 } 3958 3959 TEST_F(FormatTest, AlignsPipes) { 3960 verifyFormat( 3961 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3962 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3963 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3964 verifyFormat( 3965 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 3966 " << aaaaaaaaaaaaaaaaaaaa;"); 3967 verifyFormat( 3968 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3969 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3970 verifyFormat( 3971 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 3972 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 3973 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 3974 verifyFormat( 3975 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3976 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3977 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3978 3979 verifyFormat("return out << \"somepacket = {\\n\"\n" 3980 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 3981 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 3982 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 3983 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 3984 " << \"}\";"); 3985 3986 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 3987 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 3988 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 3989 verifyFormat( 3990 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 3991 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 3992 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 3993 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 3994 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 3995 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 3996 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 3997 verifyFormat( 3998 "void f() {\n" 3999 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4000 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4001 "}"); 4002 4003 // Breaking before the first "<<" is generally not desirable. 4004 verifyFormat( 4005 "llvm::errs()\n" 4006 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4007 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4008 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4009 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4010 getLLVMStyleWithColumns(70)); 4011 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4012 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4013 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4014 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4015 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4016 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4017 getLLVMStyleWithColumns(70)); 4018 4019 // But sometimes, breaking before the first "<<" is desirable. 4020 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4021 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4022 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4023 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4024 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4025 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4026 " << BEF << IsTemplate << Description << E->getType();"); 4027 4028 verifyFormat( 4029 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4030 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4031 4032 // Incomplete string literal. 4033 EXPECT_EQ("llvm::errs() << \"\n" 4034 " << a;", 4035 format("llvm::errs() << \"\n<<a;")); 4036 4037 verifyFormat("void f() {\n" 4038 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4039 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4040 "}"); 4041 } 4042 4043 TEST_F(FormatTest, UnderstandsEquals) { 4044 verifyFormat( 4045 "aaaaaaaaaaaaaaaaa =\n" 4046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4047 verifyFormat( 4048 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4050 verifyFormat( 4051 "if (a) {\n" 4052 " f();\n" 4053 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4055 "}"); 4056 4057 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4058 " 100000000 + 10000000) {\n}"); 4059 } 4060 4061 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4062 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4063 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4064 4065 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4066 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4067 4068 verifyFormat( 4069 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4070 " Parameter2);"); 4071 4072 verifyFormat( 4073 "ShortObject->shortFunction(\n" 4074 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4075 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4076 4077 verifyFormat("loooooooooooooongFunction(\n" 4078 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4079 4080 verifyFormat( 4081 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4082 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4083 4084 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4085 " .WillRepeatedly(Return(SomeValue));"); 4086 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 4087 " ccccccccccccccccccccccc);"); 4088 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n" 4090 " aaaaaaaaaaaaaaaaaaaaa);"); 4091 verifyFormat("void f() {\n" 4092 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4093 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 4094 "}"); 4095 verifyFormat( 4096 "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4098 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4099 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4100 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4101 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4102 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4103 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4104 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 4105 "}"); 4106 4107 // Here, it is not necessary to wrap at "." or "->". 4108 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 4109 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4110 verifyFormat( 4111 "aaaaaaaaaaa->aaaaaaaaa(\n" 4112 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4113 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 4114 4115 verifyFormat( 4116 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 4118 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 4119 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4120 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 4121 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4122 4123 // FIXME: Should we break before .a()? 4124 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); 4126 4127 FormatStyle NoBinPacking = getLLVMStyle(); 4128 NoBinPacking.BinPackParameters = false; 4129 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4130 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4131 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 4132 " aaaaaaaaaaaaaaaaaaa,\n" 4133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4134 NoBinPacking); 4135 4136 // If there is a subsequent call, change to hanging indentation. 4137 verifyFormat( 4138 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4139 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 4140 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4141 verifyFormat( 4142 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4143 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 4144 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4146 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4147 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4149 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4150 } 4151 4152 TEST_F(FormatTest, WrapsTemplateDeclarations) { 4153 verifyFormat("template <typename T>\n" 4154 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4155 verifyFormat("template <typename T>\n" 4156 "// T should be one of {A, B}.\n" 4157 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4158 verifyFormat( 4159 "template <typename T>\n" 4160 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 4161 verifyFormat("template <typename T>\n" 4162 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 4163 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 4164 verifyFormat( 4165 "template <typename T>\n" 4166 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 4167 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 4168 verifyFormat( 4169 "template <typename T>\n" 4170 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 4171 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 4172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4173 verifyFormat("template <typename T>\n" 4174 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4175 " int aaaaaaaaaaaaaaaaaaaaaa);"); 4176 verifyFormat( 4177 "template <typename T1, typename T2 = char, typename T3 = char,\n" 4178 " typename T4 = char>\n" 4179 "void f();"); 4180 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 4181 " template <typename> class cccccccccccccccccccccc,\n" 4182 " typename ddddddddddddd>\n" 4183 "class C {};"); 4184 verifyFormat( 4185 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 4186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4187 4188 verifyFormat("void f() {\n" 4189 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 4190 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 4191 "}"); 4192 4193 verifyFormat("template <typename T> class C {};"); 4194 verifyFormat("template <typename T> void f();"); 4195 verifyFormat("template <typename T> void f() {}"); 4196 verifyFormat( 4197 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 4200 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 4203 " bbbbbbbbbbbbbbbbbbbbbbbb);", 4204 getLLVMStyleWithColumns(72)); 4205 4206 FormatStyle AlwaysBreak = getLLVMStyle(); 4207 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 4208 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 4209 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 4210 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 4211 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4212 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 4213 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 4214 verifyFormat("template <template <typename> class Fooooooo,\n" 4215 " template <typename> class Baaaaaaar>\n" 4216 "struct C {};", 4217 AlwaysBreak); 4218 verifyFormat("template <typename T> // T can be A, B or C.\n" 4219 "struct C {};", 4220 AlwaysBreak); 4221 } 4222 4223 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 4224 verifyFormat( 4225 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4227 verifyFormat( 4228 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4231 4232 // FIXME: Should we have the extra indent after the second break? 4233 verifyFormat( 4234 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4237 4238 verifyFormat( 4239 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 4240 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 4241 4242 // Breaking at nested name specifiers is generally not desirable. 4243 verifyFormat( 4244 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4245 " aaaaaaaaaaaaaaaaaaaaaaa);"); 4246 4247 verifyFormat( 4248 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4250 " aaaaaaaaaaaaaaaaaaaaa);", 4251 getLLVMStyleWithColumns(74)); 4252 4253 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4255 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4256 } 4257 4258 TEST_F(FormatTest, UnderstandsTemplateParameters) { 4259 verifyFormat("A<int> a;"); 4260 verifyFormat("A<A<A<int>>> a;"); 4261 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 4262 verifyFormat("bool x = a < 1 || 2 > a;"); 4263 verifyFormat("bool x = 5 < f<int>();"); 4264 verifyFormat("bool x = f<int>() > 5;"); 4265 verifyFormat("bool x = 5 < a<int>::x;"); 4266 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 4267 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 4268 4269 verifyGoogleFormat("A<A<int>> a;"); 4270 verifyGoogleFormat("A<A<A<int>>> a;"); 4271 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 4272 verifyGoogleFormat("A<A<int> > a;"); 4273 verifyGoogleFormat("A<A<A<int> > > a;"); 4274 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 4275 verifyGoogleFormat("A<::A<int>> a;"); 4276 verifyGoogleFormat("A<::A> a;"); 4277 verifyGoogleFormat("A< ::A> a;"); 4278 verifyGoogleFormat("A< ::A<int> > a;"); 4279 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 4280 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 4281 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 4282 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 4283 4284 verifyFormat("test >> a >> b;"); 4285 verifyFormat("test << a >> b;"); 4286 4287 verifyFormat("f<int>();"); 4288 verifyFormat("template <typename T> void f() {}"); 4289 4290 // Not template parameters. 4291 verifyFormat("return a < b && c > d;"); 4292 verifyFormat("void f() {\n" 4293 " while (a < b && c > d) {\n" 4294 " }\n" 4295 "}"); 4296 verifyFormat("template <typename... Types>\n" 4297 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 4298 4299 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 4301 getLLVMStyleWithColumns(60)); 4302 } 4303 4304 TEST_F(FormatTest, UnderstandsBinaryOperators) { 4305 verifyFormat("COMPARE(a, ==, b);"); 4306 } 4307 4308 TEST_F(FormatTest, UnderstandsPointersToMembers) { 4309 verifyFormat("int A::*x;"); 4310 verifyFormat("int (S::*func)(void *);"); 4311 verifyFormat("void f() { int (S::*func)(void *); }"); 4312 verifyFormat("typedef bool *(Class::*Member)() const;"); 4313 verifyFormat("void f() {\n" 4314 " (a->*f)();\n" 4315 " a->*x;\n" 4316 " (a.*f)();\n" 4317 " ((*a).*f)();\n" 4318 " a.*x;\n" 4319 "}"); 4320 verifyFormat("void f() {\n" 4321 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 4322 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 4323 "}"); 4324 FormatStyle Style = getLLVMStyle(); 4325 Style.PointerBindsToType = true; 4326 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 4327 } 4328 4329 TEST_F(FormatTest, UnderstandsUnaryOperators) { 4330 verifyFormat("int a = -2;"); 4331 verifyFormat("f(-1, -2, -3);"); 4332 verifyFormat("a[-1] = 5;"); 4333 verifyFormat("int a = 5 + -2;"); 4334 verifyFormat("if (i == -1) {\n}"); 4335 verifyFormat("if (i != -1) {\n}"); 4336 verifyFormat("if (i > -1) {\n}"); 4337 verifyFormat("if (i < -1) {\n}"); 4338 verifyFormat("++(a->f());"); 4339 verifyFormat("--(a->f());"); 4340 verifyFormat("(a->f())++;"); 4341 verifyFormat("a[42]++;"); 4342 verifyFormat("if (!(a->f())) {\n}"); 4343 4344 verifyFormat("a-- > b;"); 4345 verifyFormat("b ? -a : c;"); 4346 verifyFormat("n * sizeof char16;"); 4347 verifyFormat("n * alignof char16;", getGoogleStyle()); 4348 verifyFormat("sizeof(char);"); 4349 verifyFormat("alignof(char);", getGoogleStyle()); 4350 4351 verifyFormat("return -1;"); 4352 verifyFormat("switch (a) {\n" 4353 "case -1:\n" 4354 " break;\n" 4355 "}"); 4356 verifyFormat("#define X -1"); 4357 verifyFormat("#define X -kConstant"); 4358 4359 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 4360 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 4361 4362 verifyFormat("int a = /* confusing comment */ -1;"); 4363 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 4364 verifyFormat("int a = i /* confusing comment */++;"); 4365 } 4366 4367 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 4368 verifyFormat("if (!aaaaaaaaaa( // break\n" 4369 " aaaaa)) {\n" 4370 "}"); 4371 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 4372 " aaaaa));"); 4373 verifyFormat("*aaa = aaaaaaa( // break\n" 4374 " bbbbbb);"); 4375 } 4376 4377 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 4378 verifyFormat("bool operator<();"); 4379 verifyFormat("bool operator>();"); 4380 verifyFormat("bool operator=();"); 4381 verifyFormat("bool operator==();"); 4382 verifyFormat("bool operator!=();"); 4383 verifyFormat("int operator+();"); 4384 verifyFormat("int operator++();"); 4385 verifyFormat("bool operator();"); 4386 verifyFormat("bool operator()();"); 4387 verifyFormat("bool operator[]();"); 4388 verifyFormat("operator bool();"); 4389 verifyFormat("operator int();"); 4390 verifyFormat("operator void *();"); 4391 verifyFormat("operator SomeType<int>();"); 4392 verifyFormat("operator SomeType<int, int>();"); 4393 verifyFormat("operator SomeType<SomeType<int>>();"); 4394 verifyFormat("void *operator new(std::size_t size);"); 4395 verifyFormat("void *operator new[](std::size_t size);"); 4396 verifyFormat("void operator delete(void *ptr);"); 4397 verifyFormat("void operator delete[](void *ptr);"); 4398 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 4399 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 4400 4401 verifyFormat( 4402 "ostream &operator<<(ostream &OutputStream,\n" 4403 " SomeReallyLongType WithSomeReallyLongValue);"); 4404 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 4405 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 4406 " return left.group < right.group;\n" 4407 "}"); 4408 verifyFormat("SomeType &operator=(const SomeType &S);"); 4409 4410 verifyGoogleFormat("operator void*();"); 4411 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 4412 verifyGoogleFormat("operator ::A();"); 4413 4414 verifyFormat("using A::operator+;"); 4415 4416 verifyFormat("Deleted &operator=(const Deleted &)& = default;"); 4417 verifyFormat("Deleted &operator=(const Deleted &)&& = delete;"); 4418 verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;"); 4419 verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;"); 4420 } 4421 4422 TEST_F(FormatTest, UnderstandsNewAndDelete) { 4423 verifyFormat("void f() {\n" 4424 " A *a = new A;\n" 4425 " A *a = new (placement) A;\n" 4426 " delete a;\n" 4427 " delete (A *)a;\n" 4428 "}"); 4429 } 4430 4431 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 4432 verifyFormat("int *f(int *a) {}"); 4433 verifyFormat("int main(int argc, char **argv) {}"); 4434 verifyFormat("Test::Test(int b) : a(b * b) {}"); 4435 verifyIndependentOfContext("f(a, *a);"); 4436 verifyFormat("void g() { f(*a); }"); 4437 verifyIndependentOfContext("int a = b * 10;"); 4438 verifyIndependentOfContext("int a = 10 * b;"); 4439 verifyIndependentOfContext("int a = b * c;"); 4440 verifyIndependentOfContext("int a += b * c;"); 4441 verifyIndependentOfContext("int a -= b * c;"); 4442 verifyIndependentOfContext("int a *= b * c;"); 4443 verifyIndependentOfContext("int a /= b * c;"); 4444 verifyIndependentOfContext("int a = *b;"); 4445 verifyIndependentOfContext("int a = *b * c;"); 4446 verifyIndependentOfContext("int a = b * *c;"); 4447 verifyIndependentOfContext("return 10 * b;"); 4448 verifyIndependentOfContext("return *b * *c;"); 4449 verifyIndependentOfContext("return a & ~b;"); 4450 verifyIndependentOfContext("f(b ? *c : *d);"); 4451 verifyIndependentOfContext("int a = b ? *c : *d;"); 4452 verifyIndependentOfContext("*b = a;"); 4453 verifyIndependentOfContext("a * ~b;"); 4454 verifyIndependentOfContext("a * !b;"); 4455 verifyIndependentOfContext("a * +b;"); 4456 verifyIndependentOfContext("a * -b;"); 4457 verifyIndependentOfContext("a * ++b;"); 4458 verifyIndependentOfContext("a * --b;"); 4459 verifyIndependentOfContext("a[4] * b;"); 4460 verifyIndependentOfContext("a[a * a] = 1;"); 4461 verifyIndependentOfContext("f() * b;"); 4462 verifyIndependentOfContext("a * [self dostuff];"); 4463 verifyIndependentOfContext("int x = a * (a + b);"); 4464 verifyIndependentOfContext("(a *)(a + b);"); 4465 verifyIndependentOfContext("int *pa = (int *)&a;"); 4466 verifyIndependentOfContext("return sizeof(int **);"); 4467 verifyIndependentOfContext("return sizeof(int ******);"); 4468 verifyIndependentOfContext("return (int **&)a;"); 4469 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 4470 verifyFormat("void f(Type (*parameter)[10]) {}"); 4471 verifyGoogleFormat("return sizeof(int**);"); 4472 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 4473 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 4474 verifyFormat("auto a = [](int **&, int ***) {};"); 4475 verifyFormat("auto PointerBinding = [](const char *S) {};"); 4476 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 4477 verifyIndependentOfContext("typedef void (*f)(int *a);"); 4478 4479 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 4480 4481 verifyIndependentOfContext("A<int *> a;"); 4482 verifyIndependentOfContext("A<int **> a;"); 4483 verifyIndependentOfContext("A<int *, int *> a;"); 4484 verifyIndependentOfContext("A<int *[]> a;"); 4485 verifyIndependentOfContext( 4486 "const char *const p = reinterpret_cast<const char *const>(q);"); 4487 verifyIndependentOfContext("A<int **, int **> a;"); 4488 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 4489 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 4490 verifyFormat("for (; a && b;) {\n}"); 4491 4492 verifyFormat( 4493 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4495 4496 verifyGoogleFormat("int main(int argc, char** argv) {}"); 4497 verifyGoogleFormat("A<int*> a;"); 4498 verifyGoogleFormat("A<int**> a;"); 4499 verifyGoogleFormat("A<int*, int*> a;"); 4500 verifyGoogleFormat("A<int**, int**> a;"); 4501 verifyGoogleFormat("f(b ? *c : *d);"); 4502 verifyGoogleFormat("int a = b ? *c : *d;"); 4503 verifyGoogleFormat("Type* t = **x;"); 4504 verifyGoogleFormat("Type* t = *++*x;"); 4505 verifyGoogleFormat("*++*x;"); 4506 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 4507 verifyGoogleFormat("Type* t = x++ * y;"); 4508 verifyGoogleFormat( 4509 "const char* const p = reinterpret_cast<const char* const>(q);"); 4510 4511 verifyIndependentOfContext("a = *(x + y);"); 4512 verifyIndependentOfContext("a = &(x + y);"); 4513 verifyIndependentOfContext("*(x + y).call();"); 4514 verifyIndependentOfContext("&(x + y)->call();"); 4515 verifyFormat("void f() { &(*I).first; }"); 4516 4517 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 4518 verifyFormat( 4519 "int *MyValues = {\n" 4520 " *A, // Operator detection might be confused by the '{'\n" 4521 " *BB // Operator detection might be confused by previous comment\n" 4522 "};"); 4523 4524 verifyIndependentOfContext("if (int *a = &b)"); 4525 verifyIndependentOfContext("if (int &a = *b)"); 4526 verifyIndependentOfContext("if (a & b[i])"); 4527 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 4528 verifyIndependentOfContext("if (*b[i])"); 4529 verifyIndependentOfContext("if (int *a = (&b))"); 4530 verifyIndependentOfContext("while (int *a = &b)"); 4531 verifyIndependentOfContext("size = sizeof *a;"); 4532 verifyFormat("void f() {\n" 4533 " for (const int &v : Values) {\n" 4534 " }\n" 4535 "}"); 4536 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 4537 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 4538 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 4539 4540 verifyFormat("#define A (!a * b)"); 4541 verifyFormat("#define MACRO \\\n" 4542 " int *i = a * b; \\\n" 4543 " void f(a *b);", 4544 getLLVMStyleWithColumns(19)); 4545 4546 verifyIndependentOfContext("A = new SomeType *[Length];"); 4547 verifyIndependentOfContext("A = new SomeType *[Length]();"); 4548 verifyIndependentOfContext("T **t = new T *;"); 4549 verifyIndependentOfContext("T **t = new T *();"); 4550 verifyGoogleFormat("A = new SomeType* [Length]();"); 4551 verifyGoogleFormat("A = new SomeType* [Length];"); 4552 verifyGoogleFormat("T** t = new T*;"); 4553 verifyGoogleFormat("T** t = new T*();"); 4554 4555 FormatStyle PointerLeft = getLLVMStyle(); 4556 PointerLeft.PointerBindsToType = true; 4557 verifyFormat("delete *x;", PointerLeft); 4558 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 4559 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 4560 verifyFormat("template <bool a, bool b> " 4561 "typename t::if<x && y>::type f() {};"); 4562 verifyFormat("template <int *y> f() {};"); 4563 verifyFormat("vector<int *> v;"); 4564 verifyFormat("vector<int *const> v;"); 4565 verifyFormat("vector<int *const **const *> v;"); 4566 verifyFormat("vector<int *volatile> v;"); 4567 verifyFormat("vector<a * b> v;"); 4568 verifyFormat("foo<b && false>();"); 4569 verifyFormat("foo<b & 1>();"); 4570 4571 verifyIndependentOfContext("MACRO(int *i);"); 4572 verifyIndependentOfContext("MACRO(auto *a);"); 4573 verifyIndependentOfContext("MACRO(const A *a);"); 4574 // FIXME: Is there a way to make this work? 4575 // verifyIndependentOfContext("MACRO(A *a);"); 4576 4577 // FIXME: We cannot handle this case yet; we might be able to figure out that 4578 // foo<x> d > v; doesn't make sense. 4579 verifyFormat("foo<a < b && c> d > v;"); 4580 } 4581 4582 TEST_F(FormatTest, UnderstandsAttributes) { 4583 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 4584 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 4585 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 4586 } 4587 4588 TEST_F(FormatTest, UnderstandsEllipsis) { 4589 verifyFormat("int printf(const char *fmt, ...);"); 4590 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 4591 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 4592 4593 FormatStyle PointersLeft = getLLVMStyle(); 4594 PointersLeft.PointerBindsToType = true; 4595 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 4596 } 4597 4598 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 4599 EXPECT_EQ("int *a;\n" 4600 "int *a;\n" 4601 "int *a;", 4602 format("int *a;\n" 4603 "int* a;\n" 4604 "int *a;", 4605 getGoogleStyle())); 4606 EXPECT_EQ("int* a;\n" 4607 "int* a;\n" 4608 "int* a;", 4609 format("int* a;\n" 4610 "int* a;\n" 4611 "int *a;", 4612 getGoogleStyle())); 4613 EXPECT_EQ("int *a;\n" 4614 "int *a;\n" 4615 "int *a;", 4616 format("int *a;\n" 4617 "int * a;\n" 4618 "int * a;", 4619 getGoogleStyle())); 4620 } 4621 4622 TEST_F(FormatTest, UnderstandsRvalueReferences) { 4623 verifyFormat("int f(int &&a) {}"); 4624 verifyFormat("int f(int a, char &&b) {}"); 4625 verifyFormat("void f() { int &&a = b; }"); 4626 verifyGoogleFormat("int f(int a, char&& b) {}"); 4627 verifyGoogleFormat("void f() { int&& a = b; }"); 4628 4629 verifyIndependentOfContext("A<int &&> a;"); 4630 verifyIndependentOfContext("A<int &&, int &&> a;"); 4631 verifyGoogleFormat("A<int&&> a;"); 4632 verifyGoogleFormat("A<int&&, int&&> a;"); 4633 4634 // Not rvalue references: 4635 verifyFormat("template <bool B, bool C> class A {\n" 4636 " static_assert(B && C, \"Something is wrong\");\n" 4637 "};"); 4638 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 4639 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 4640 } 4641 4642 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 4643 verifyFormat("void f() {\n" 4644 " x[aaaaaaaaa -\n" 4645 " b] = 23;\n" 4646 "}", 4647 getLLVMStyleWithColumns(15)); 4648 } 4649 4650 TEST_F(FormatTest, FormatsCasts) { 4651 verifyFormat("Type *A = static_cast<Type *>(P);"); 4652 verifyFormat("Type *A = (Type *)P;"); 4653 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 4654 verifyFormat("int a = (int)(2.0f);"); 4655 verifyFormat("int a = (int)2.0f;"); 4656 verifyFormat("x[(int32)y];"); 4657 verifyFormat("x = (int32)y;"); 4658 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 4659 verifyFormat("int a = (int)*b;"); 4660 verifyFormat("int a = (int)2.0f;"); 4661 verifyFormat("int a = (int)~0;"); 4662 verifyFormat("int a = (int)++a;"); 4663 verifyFormat("int a = (int)sizeof(int);"); 4664 verifyFormat("int a = (int)+2;"); 4665 verifyFormat("my_int a = (my_int)2.0f;"); 4666 verifyFormat("my_int a = (my_int)sizeof(int);"); 4667 verifyFormat("return (my_int)aaa;"); 4668 verifyFormat("#define x ((int)-1)"); 4669 verifyFormat("#define p(q) ((int *)&q)"); 4670 4671 // FIXME: Without type knowledge, this can still fall apart miserably. 4672 verifyFormat("void f() { my_int a = (my_int) * b; }"); 4673 verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }"); 4674 verifyFormat("my_int a = (my_int) ~0;"); 4675 verifyFormat("my_int a = (my_int)++ a;"); 4676 verifyFormat("my_int a = (my_int) + 2;"); 4677 4678 // Don't break after a cast's 4679 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4680 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 4681 " bbbbbbbbbbbbbbbbbbbbbb);"); 4682 4683 // These are not casts. 4684 verifyFormat("void f(int *) {}"); 4685 verifyFormat("f(foo)->b;"); 4686 verifyFormat("f(foo).b;"); 4687 verifyFormat("f(foo)(b);"); 4688 verifyFormat("f(foo)[b];"); 4689 verifyFormat("[](foo) { return 4; }(bar);"); 4690 verifyFormat("(*funptr)(foo)[4];"); 4691 verifyFormat("funptrs[4](foo)[4];"); 4692 verifyFormat("void f(int *);"); 4693 verifyFormat("void f(int *) = 0;"); 4694 verifyFormat("void f(SmallVector<int>) {}"); 4695 verifyFormat("void f(SmallVector<int>);"); 4696 verifyFormat("void f(SmallVector<int>) = 0;"); 4697 verifyFormat("void f(int i = (kValue) * kMask) {}"); 4698 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 4699 verifyFormat("int a = sizeof(int) * b;"); 4700 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 4701 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 4702 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 4703 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 4704 4705 // These are not casts, but at some point were confused with casts. 4706 verifyFormat("virtual void foo(int *) override;"); 4707 verifyFormat("virtual void foo(char &) const;"); 4708 verifyFormat("virtual void foo(int *a, char *) const;"); 4709 verifyFormat("int a = sizeof(int *) + b;"); 4710 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 4711 4712 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 4713 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4714 // FIXME: The indentation here is not ideal. 4715 verifyFormat( 4716 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4717 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 4718 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 4719 } 4720 4721 TEST_F(FormatTest, FormatsFunctionTypes) { 4722 verifyFormat("A<bool()> a;"); 4723 verifyFormat("A<SomeType()> a;"); 4724 verifyFormat("A<void (*)(int, std::string)> a;"); 4725 verifyFormat("A<void *(int)>;"); 4726 verifyFormat("void *(*a)(int *, SomeType *);"); 4727 verifyFormat("int (*func)(void *);"); 4728 verifyFormat("void f() { int (*func)(void *); }"); 4729 verifyFormat("template <class CallbackClass>\n" 4730 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 4731 4732 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 4733 verifyGoogleFormat("void* (*a)(int);"); 4734 verifyGoogleFormat( 4735 "template <class CallbackClass>\n" 4736 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 4737 4738 // Other constructs can look somewhat like function types: 4739 verifyFormat("A<sizeof(*x)> a;"); 4740 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 4741 verifyFormat("some_var = function(*some_pointer_var)[0];"); 4742 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 4743 } 4744 4745 TEST_F(FormatTest, BreaksLongDeclarations) { 4746 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 4747 " AnotherNameForTheLongType;", 4748 getGoogleStyle()); 4749 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4751 getGoogleStyle()); 4752 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 4753 " LoooooooooooooooooooooooooooooooooooooooongVariable;", 4754 getGoogleStyle()); 4755 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 4756 " LoooooooooooooooooooooooooooooooooooooooongVariable;", 4757 getGoogleStyle()); 4758 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 4759 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 4760 getGoogleStyle()); 4761 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 4762 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 4763 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 4764 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 4765 4766 // FIXME: Without the comment, this breaks after "(". 4767 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 4768 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 4769 getGoogleStyle()); 4770 4771 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 4772 " int LoooooooooooooooooooongParam2) {}"); 4773 verifyFormat( 4774 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 4775 " SourceLocation L, IdentifierIn *II,\n" 4776 " Type *T) {}"); 4777 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 4778 "ReallyReallyLongFunctionName(\n" 4779 " const std::string &SomeParameter,\n" 4780 " const SomeType<string, SomeOtherTemplateParameter> &\n" 4781 " ReallyReallyLongParameterName,\n" 4782 " const SomeType<string, SomeOtherTemplateParameter> &\n" 4783 " AnotherLongParameterName) {}"); 4784 verifyFormat("template <typename A>\n" 4785 "SomeLoooooooooooooooooooooongType<\n" 4786 " typename some_namespace::SomeOtherType<A>::Type>\n" 4787 "Function() {}"); 4788 4789 verifyGoogleFormat( 4790 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 4791 " aaaaaaaaaaaaaaaaaaaaaaa;"); 4792 verifyGoogleFormat( 4793 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 4794 " SourceLocation L) {}"); 4795 verifyGoogleFormat( 4796 "some_namespace::LongReturnType\n" 4797 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 4798 " int first_long_parameter, int second_parameter) {}"); 4799 4800 verifyGoogleFormat("template <typename T>\n" 4801 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4802 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 4803 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4804 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 4805 } 4806 4807 TEST_F(FormatTest, FormatsArrays) { 4808 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4809 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 4810 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4811 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 4812 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4813 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 4814 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4815 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4816 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 4817 verifyFormat( 4818 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 4819 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4820 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 4821 4822 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 4823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 4824 } 4825 4826 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 4827 verifyFormat("(a)->b();"); 4828 verifyFormat("--a;"); 4829 } 4830 4831 TEST_F(FormatTest, HandlesIncludeDirectives) { 4832 verifyFormat("#include <string>\n" 4833 "#include <a/b/c.h>\n" 4834 "#include \"a/b/string\"\n" 4835 "#include \"string.h\"\n" 4836 "#include \"string.h\"\n" 4837 "#include <a-a>\n" 4838 "#include < path with space >\n" 4839 "#include \"abc.h\" // this is included for ABC\n" 4840 "#include \"some long include\" // with a comment\n" 4841 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 4842 getLLVMStyleWithColumns(35)); 4843 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 4844 EXPECT_EQ("#include <a>", format("#include<a>")); 4845 4846 verifyFormat("#import <string>"); 4847 verifyFormat("#import <a/b/c.h>"); 4848 verifyFormat("#import \"a/b/string\""); 4849 verifyFormat("#import \"string.h\""); 4850 verifyFormat("#import \"string.h\""); 4851 verifyFormat("#if __has_include(<strstream>)\n" 4852 "#include <strstream>\n" 4853 "#endif"); 4854 4855 // Protocol buffer definition or missing "#". 4856 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 4857 getLLVMStyleWithColumns(30)); 4858 } 4859 4860 //===----------------------------------------------------------------------===// 4861 // Error recovery tests. 4862 //===----------------------------------------------------------------------===// 4863 4864 TEST_F(FormatTest, IncompleteParameterLists) { 4865 FormatStyle NoBinPacking = getLLVMStyle(); 4866 NoBinPacking.BinPackParameters = false; 4867 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 4868 " double *min_x,\n" 4869 " double *max_x,\n" 4870 " double *min_y,\n" 4871 " double *max_y,\n" 4872 " double *min_z,\n" 4873 " double *max_z, ) {}", 4874 NoBinPacking); 4875 } 4876 4877 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 4878 verifyFormat("void f() { return; }\n42"); 4879 verifyFormat("void f() {\n" 4880 " if (0)\n" 4881 " return;\n" 4882 "}\n" 4883 "42"); 4884 verifyFormat("void f() { return }\n42"); 4885 verifyFormat("void f() {\n" 4886 " if (0)\n" 4887 " return\n" 4888 "}\n" 4889 "42"); 4890 } 4891 4892 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 4893 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 4894 EXPECT_EQ("void f() {\n" 4895 " if (a)\n" 4896 " return\n" 4897 "}", 4898 format("void f ( ) { if ( a ) return }")); 4899 EXPECT_EQ("namespace N {\n" 4900 "void f()\n" 4901 "}", 4902 format("namespace N { void f() }")); 4903 EXPECT_EQ("namespace N {\n" 4904 "void f() {}\n" 4905 "void g()\n" 4906 "}", 4907 format("namespace N { void f( ) { } void g( ) }")); 4908 } 4909 4910 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 4911 verifyFormat("int aaaaaaaa =\n" 4912 " // Overlylongcomment\n" 4913 " b;", 4914 getLLVMStyleWithColumns(20)); 4915 verifyFormat("function(\n" 4916 " ShortArgument,\n" 4917 " LoooooooooooongArgument);\n", 4918 getLLVMStyleWithColumns(20)); 4919 } 4920 4921 TEST_F(FormatTest, IncorrectAccessSpecifier) { 4922 verifyFormat("public:"); 4923 verifyFormat("class A {\n" 4924 "public\n" 4925 " void f() {}\n" 4926 "};"); 4927 verifyFormat("public\n" 4928 "int qwerty;"); 4929 verifyFormat("public\n" 4930 "B {}"); 4931 verifyFormat("public\n" 4932 "{}"); 4933 verifyFormat("public\n" 4934 "B { int x; }"); 4935 } 4936 4937 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 4938 verifyFormat("{"); 4939 verifyFormat("#})"); 4940 } 4941 4942 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 4943 verifyFormat("do {\n}"); 4944 verifyFormat("do {\n}\n" 4945 "f();"); 4946 verifyFormat("do {\n}\n" 4947 "wheeee(fun);"); 4948 verifyFormat("do {\n" 4949 " f();\n" 4950 "}"); 4951 } 4952 4953 TEST_F(FormatTest, IncorrectCodeMissingParens) { 4954 verifyFormat("if {\n foo;\n foo();\n}"); 4955 verifyFormat("switch {\n foo;\n foo();\n}"); 4956 verifyFormat("for {\n foo;\n foo();\n}"); 4957 verifyFormat("while {\n foo;\n foo();\n}"); 4958 verifyFormat("do {\n foo;\n foo();\n} while;"); 4959 } 4960 4961 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 4962 verifyFormat("namespace {\n" 4963 "class Foo { Foo (\n" 4964 "};\n" 4965 "} // comment"); 4966 } 4967 4968 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 4969 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 4970 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 4971 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 4972 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 4973 4974 EXPECT_EQ("{\n" 4975 " {\n" 4976 " breakme(\n" 4977 " qwe);\n" 4978 " }\n", 4979 format("{\n" 4980 " {\n" 4981 " breakme(qwe);\n" 4982 "}\n", 4983 getLLVMStyleWithColumns(10))); 4984 } 4985 4986 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 4987 verifyFormat("int x = {\n" 4988 " avariable,\n" 4989 " b(alongervariable)};", 4990 getLLVMStyleWithColumns(25)); 4991 } 4992 4993 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 4994 verifyFormat("return (a)(b) {1, 2, 3};"); 4995 } 4996 4997 TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { 4998 verifyFormat("vector<int> x{1, 2, 3, 4};"); 4999 verifyFormat("vector<int> x{\n" 5000 " 1, 2, 3, 4,\n" 5001 "};"); 5002 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 5003 verifyFormat("f({1, 2});"); 5004 verifyFormat("auto v = Foo{-1};"); 5005 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 5006 verifyFormat("Class::Class : member{1, 2, 3} {}"); 5007 verifyFormat("new vector<int>{1, 2, 3};"); 5008 verifyFormat("new int[3]{1, 2, 3};"); 5009 verifyFormat("return {arg1, arg2};"); 5010 verifyFormat("return {arg1, SomeType{parameter}};"); 5011 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 5012 verifyFormat("new T{arg1, arg2};"); 5013 verifyFormat("f(MyMap[{composite, key}]);"); 5014 verifyFormat("class Class {\n" 5015 " T member = {arg1, arg2};\n" 5016 "};"); 5017 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 5018 5019 // FIXME: The alignment of these trailing comments might be bad. Then again, 5020 // this might be utterly useless in real code. 5021 verifyFormat("Constructor::Constructor()\n" 5022 " : some_value{ //\n" 5023 " aaaaaaa //\n" 5024 " } {}"); 5025 5026 // In braced lists, the first comment is always assumed to belong to the 5027 // first element. Thus, it can be moved to the next or previous line as 5028 // appropriate. 5029 EXPECT_EQ("function({// First element:\n" 5030 " 1,\n" 5031 " // Second element:\n" 5032 " 2});", 5033 format("function({\n" 5034 " // First element:\n" 5035 " 1,\n" 5036 " // Second element:\n" 5037 " 2});")); 5038 EXPECT_EQ("std::vector<int> MyNumbers{\n" 5039 " // First element:\n" 5040 " 1,\n" 5041 " // Second element:\n" 5042 " 2};", 5043 format("std::vector<int> MyNumbers{// First element:\n" 5044 " 1,\n" 5045 " // Second element:\n" 5046 " 2};", 5047 getLLVMStyleWithColumns(30))); 5048 5049 FormatStyle ExtraSpaces = getLLVMStyle(); 5050 ExtraSpaces.Cpp11BracedListStyle = false; 5051 ExtraSpaces.ColumnLimit = 75; 5052 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 5053 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 5054 verifyFormat("f({ 1, 2 });", ExtraSpaces); 5055 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 5056 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 5057 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 5058 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 5059 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 5060 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 5061 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 5062 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 5063 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 5064 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 5065 verifyFormat("class Class {\n" 5066 " T member = { arg1, arg2 };\n" 5067 "};", 5068 ExtraSpaces); 5069 verifyFormat( 5070 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5071 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 5072 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5073 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 5074 ExtraSpaces); 5075 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 5076 verifyFormat("DoSomethingWithVector({\n" 5077 " {} /* No data */\n" 5078 " },\n" 5079 " { { 1, 2 } });", 5080 ExtraSpaces); 5081 verifyFormat( 5082 "someFunction(OtherParam,\n" 5083 " BracedList{ // comment 1 (Forcing interesting break)\n" 5084 " param1, param2,\n" 5085 " // comment 2\n" 5086 " param3, param4 });", 5087 ExtraSpaces); 5088 verifyFormat( 5089 "std::this_thread::sleep_for(\n" 5090 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 5091 ExtraSpaces); 5092 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 5093 " aaaaaaa, aaaaaaaaaa,\n" 5094 " aaaaa, aaaaaaaaaaaaaaa,\n" 5095 " aaa, aaaaaaaaaa,\n" 5096 " a, aaaaaaaaaaaaaaaaaaaaa,\n" 5097 " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 5098 " aaaaaaa, a\n" 5099 "};", 5100 ExtraSpaces); 5101 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 5102 } 5103 5104 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 5105 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5106 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5107 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5108 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5109 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5110 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 5111 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5112 " // line comment\n" 5113 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5114 " 1, 22, 333, 4444, 55555,\n" 5115 " // line comment\n" 5116 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5117 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 5118 verifyFormat( 5119 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5120 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5121 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 5122 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 5123 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 5124 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 5125 " 7777777};"); 5126 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 5127 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 5128 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 5129 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 5130 " 1, 1, 1, 1};", 5131 getLLVMStyleWithColumns(39)); 5132 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 5133 " 1, 1, 1, 1};", 5134 getLLVMStyleWithColumns(38)); 5135 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 5136 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 5137 getLLVMStyleWithColumns(43)); 5138 5139 // Trailing commas. 5140 verifyFormat("vector<int> x = {\n" 5141 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 5142 "};", 5143 getLLVMStyleWithColumns(39)); 5144 verifyFormat("vector<int> x = {\n" 5145 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 5146 "};", 5147 getLLVMStyleWithColumns(39)); 5148 verifyFormat("vector<int> x = {\n" 5149 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 5150 " /**/ /**/\n" 5151 "};", 5152 getLLVMStyleWithColumns(39)); 5153 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 5154 " {aaaaaaaaaaaaaaaaaaa},\n" 5155 " {aaaaaaaaaaaaaaaaaaaaa},\n" 5156 " {aaaaaaaaaaaaaaaaa}};", 5157 getLLVMStyleWithColumns(60)); 5158 5159 // With nested lists, we should either format one item per line or all nested 5160 // lists one one line. 5161 // FIXME: For some nested lists, we can do better. 5162 verifyFormat( 5163 "SomeStruct my_struct_array = {\n" 5164 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 5165 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 5166 " {aaa, aaa},\n" 5167 " {aaa, aaa},\n" 5168 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 5169 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5170 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 5171 5172 // No column layout should be used here. 5173 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 5174 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 5175 } 5176 5177 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 5178 FormatStyle DoNotMerge = getLLVMStyle(); 5179 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 5180 5181 verifyFormat("void f() { return 42; }"); 5182 verifyFormat("void f() {\n" 5183 " return 42;\n" 5184 "}", 5185 DoNotMerge); 5186 verifyFormat("void f() {\n" 5187 " // Comment\n" 5188 "}"); 5189 verifyFormat("{\n" 5190 "#error {\n" 5191 " int a;\n" 5192 "}"); 5193 verifyFormat("{\n" 5194 " int a;\n" 5195 "#error {\n" 5196 "}"); 5197 verifyFormat("void f() {} // comment"); 5198 verifyFormat("void f() { int a; } // comment"); 5199 verifyFormat("void f() {\n" 5200 "} // comment", 5201 DoNotMerge); 5202 verifyFormat("void f() {\n" 5203 " int a;\n" 5204 "} // comment", 5205 DoNotMerge); 5206 verifyFormat("void f() {\n" 5207 "} // comment", 5208 getLLVMStyleWithColumns(15)); 5209 5210 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 5211 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 5212 5213 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 5214 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 5215 verifyFormat("class C {\n" 5216 " C()\n" 5217 " : iiiiiiii(nullptr),\n" 5218 " kkkkkkk(nullptr),\n" 5219 " mmmmmmm(nullptr),\n" 5220 " nnnnnnn(nullptr) {}\n" 5221 "};", 5222 getGoogleStyle()); 5223 5224 FormatStyle NoColumnLimit = getLLVMStyle(); 5225 NoColumnLimit.ColumnLimit = 0; 5226 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 5227 EXPECT_EQ("class C {\n" 5228 " A() : b(0) {}\n" 5229 "};", format("class C{A():b(0){}};", NoColumnLimit)); 5230 EXPECT_EQ("A()\n" 5231 " : b(0) {\n" 5232 "}", 5233 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 5234 5235 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 5236 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 5237 FormatStyle::SFS_None; 5238 EXPECT_EQ("A()\n" 5239 " : b(0) {\n" 5240 "}", 5241 format("A():b(0){}", DoNotMergeNoColumnLimit)); 5242 EXPECT_EQ("A()\n" 5243 " : b(0) {\n" 5244 "}", 5245 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 5246 5247 verifyFormat("#define A \\\n" 5248 " void f() { \\\n" 5249 " int i; \\\n" 5250 " }", 5251 getLLVMStyleWithColumns(20)); 5252 verifyFormat("#define A \\\n" 5253 " void f() { int i; }", 5254 getLLVMStyleWithColumns(21)); 5255 verifyFormat("#define A \\\n" 5256 " void f() { \\\n" 5257 " int i; \\\n" 5258 " } \\\n" 5259 " int j;", 5260 getLLVMStyleWithColumns(22)); 5261 verifyFormat("#define A \\\n" 5262 " void f() { int i; } \\\n" 5263 " int j;", 5264 getLLVMStyleWithColumns(23)); 5265 } 5266 5267 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 5268 FormatStyle MergeInlineOnly = getLLVMStyle(); 5269 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 5270 verifyFormat("class C {\n" 5271 " int f() { return 42; }\n" 5272 "};", 5273 MergeInlineOnly); 5274 verifyFormat("int f() {\n" 5275 " return 42;\n" 5276 "}", 5277 MergeInlineOnly); 5278 } 5279 5280 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 5281 // Elaborate type variable declarations. 5282 verifyFormat("struct foo a = {bar};\nint n;"); 5283 verifyFormat("class foo a = {bar};\nint n;"); 5284 verifyFormat("union foo a = {bar};\nint n;"); 5285 5286 // Elaborate types inside function definitions. 5287 verifyFormat("struct foo f() {}\nint n;"); 5288 verifyFormat("class foo f() {}\nint n;"); 5289 verifyFormat("union foo f() {}\nint n;"); 5290 5291 // Templates. 5292 verifyFormat("template <class X> void f() {}\nint n;"); 5293 verifyFormat("template <struct X> void f() {}\nint n;"); 5294 verifyFormat("template <union X> void f() {}\nint n;"); 5295 5296 // Actual definitions... 5297 verifyFormat("struct {\n} n;"); 5298 verifyFormat( 5299 "template <template <class T, class Y>, class Z> class X {\n} n;"); 5300 verifyFormat("union Z {\n int n;\n} x;"); 5301 verifyFormat("class MACRO Z {\n} n;"); 5302 verifyFormat("class MACRO(X) Z {\n} n;"); 5303 verifyFormat("class __attribute__(X) Z {\n} n;"); 5304 verifyFormat("class __declspec(X) Z {\n} n;"); 5305 verifyFormat("class A##B##C {\n} n;"); 5306 verifyFormat("class alignas(16) Z {\n} n;"); 5307 5308 // Redefinition from nested context: 5309 verifyFormat("class A::B::C {\n} n;"); 5310 5311 // Template definitions. 5312 verifyFormat( 5313 "template <typename F>\n" 5314 "Matcher(const Matcher<F> &Other,\n" 5315 " typename enable_if_c<is_base_of<F, T>::value &&\n" 5316 " !is_same<F, T>::value>::type * = 0)\n" 5317 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 5318 5319 // FIXME: This is still incorrectly handled at the formatter side. 5320 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 5321 5322 // FIXME: 5323 // This now gets parsed incorrectly as class definition. 5324 // verifyFormat("class A<int> f() {\n}\nint n;"); 5325 5326 // Elaborate types where incorrectly parsing the structural element would 5327 // break the indent. 5328 verifyFormat("if (true)\n" 5329 " class X x;\n" 5330 "else\n" 5331 " f();\n"); 5332 5333 // This is simply incomplete. Formatting is not important, but must not crash. 5334 verifyFormat("class A:"); 5335 } 5336 5337 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 5338 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 5339 format("#error Leave all white!!!!! space* alone!\n")); 5340 EXPECT_EQ( 5341 "#warning Leave all white!!!!! space* alone!\n", 5342 format("#warning Leave all white!!!!! space* alone!\n")); 5343 EXPECT_EQ("#error 1", format(" # error 1")); 5344 EXPECT_EQ("#warning 1", format(" # warning 1")); 5345 } 5346 5347 TEST_F(FormatTest, FormatHashIfExpressions) { 5348 verifyFormat("#if AAAA && BBBB"); 5349 // FIXME: Come up with a better indentation for #elif. 5350 verifyFormat( 5351 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 5352 " defined(BBBBBBBB)\n" 5353 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 5354 " defined(BBBBBBBB)\n" 5355 "#endif", 5356 getLLVMStyleWithColumns(65)); 5357 } 5358 5359 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 5360 FormatStyle AllowsMergedIf = getGoogleStyle(); 5361 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 5362 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 5363 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 5364 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 5365 EXPECT_EQ("if (true) return 42;", 5366 format("if (true)\nreturn 42;", AllowsMergedIf)); 5367 FormatStyle ShortMergedIf = AllowsMergedIf; 5368 ShortMergedIf.ColumnLimit = 25; 5369 verifyFormat("#define A \\\n" 5370 " if (true) return 42;", 5371 ShortMergedIf); 5372 verifyFormat("#define A \\\n" 5373 " f(); \\\n" 5374 " if (true)\n" 5375 "#define B", 5376 ShortMergedIf); 5377 verifyFormat("#define A \\\n" 5378 " f(); \\\n" 5379 " if (true)\n" 5380 "g();", 5381 ShortMergedIf); 5382 verifyFormat("{\n" 5383 "#ifdef A\n" 5384 " // Comment\n" 5385 " if (true) continue;\n" 5386 "#endif\n" 5387 " // Comment\n" 5388 " if (true) continue;\n" 5389 "}", 5390 ShortMergedIf); 5391 ShortMergedIf.ColumnLimit = 29; 5392 verifyFormat("#define A \\\n" 5393 " if (aaaaaaaaaa) return 1; \\\n" 5394 " return 2;", 5395 ShortMergedIf); 5396 ShortMergedIf.ColumnLimit = 28; 5397 verifyFormat("#define A \\\n" 5398 " if (aaaaaaaaaa) \\\n" 5399 " return 1; \\\n" 5400 " return 2;", 5401 ShortMergedIf); 5402 } 5403 5404 TEST_F(FormatTest, BlockCommentsInControlLoops) { 5405 verifyFormat("if (0) /* a comment in a strange place */ {\n" 5406 " f();\n" 5407 "}"); 5408 verifyFormat("if (0) /* a comment in a strange place */ {\n" 5409 " f();\n" 5410 "} /* another comment */ else /* comment #3 */ {\n" 5411 " g();\n" 5412 "}"); 5413 verifyFormat("while (0) /* a comment in a strange place */ {\n" 5414 " f();\n" 5415 "}"); 5416 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 5417 " f();\n" 5418 "}"); 5419 verifyFormat("do /* a comment in a strange place */ {\n" 5420 " f();\n" 5421 "} /* another comment */ while (0);"); 5422 } 5423 5424 TEST_F(FormatTest, BlockComments) { 5425 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 5426 format("/* *//* */ /* */\n/* *//* */ /* */")); 5427 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 5428 EXPECT_EQ("#define A /*123*/ \\\n" 5429 " b\n" 5430 "/* */\n" 5431 "someCall(\n" 5432 " parameter);", 5433 format("#define A /*123*/ b\n" 5434 "/* */\n" 5435 "someCall(parameter);", 5436 getLLVMStyleWithColumns(15))); 5437 5438 EXPECT_EQ("#define A\n" 5439 "/* */ someCall(\n" 5440 " parameter);", 5441 format("#define A\n" 5442 "/* */someCall(parameter);", 5443 getLLVMStyleWithColumns(15))); 5444 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 5445 EXPECT_EQ("/*\n" 5446 "*\n" 5447 " * aaaaaa\n" 5448 "*aaaaaa\n" 5449 "*/", 5450 format("/*\n" 5451 "*\n" 5452 " * aaaaaa aaaaaa\n" 5453 "*/", 5454 getLLVMStyleWithColumns(10))); 5455 EXPECT_EQ("/*\n" 5456 "**\n" 5457 "* aaaaaa\n" 5458 "*aaaaaa\n" 5459 "*/", 5460 format("/*\n" 5461 "**\n" 5462 "* aaaaaa aaaaaa\n" 5463 "*/", 5464 getLLVMStyleWithColumns(10))); 5465 5466 FormatStyle NoBinPacking = getLLVMStyle(); 5467 NoBinPacking.BinPackParameters = false; 5468 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 5469 " 2, /* comment 2 */\n" 5470 " 3, /* comment 3 */\n" 5471 " aaaa,\n" 5472 " bbbb);", 5473 format("someFunction (1, /* comment 1 */\n" 5474 " 2, /* comment 2 */ \n" 5475 " 3, /* comment 3 */\n" 5476 "aaaa, bbbb );", 5477 NoBinPacking)); 5478 verifyFormat( 5479 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5481 EXPECT_EQ( 5482 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 5483 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 5485 format( 5486 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 5487 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 5489 EXPECT_EQ( 5490 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 5491 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 5492 "int cccccccccccccccccccccccccccccc; /* comment */\n", 5493 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 5494 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 5495 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 5496 5497 verifyFormat("void f(int * /* unused */) {}"); 5498 5499 EXPECT_EQ("/*\n" 5500 " **\n" 5501 " */", 5502 format("/*\n" 5503 " **\n" 5504 " */")); 5505 EXPECT_EQ("/*\n" 5506 " *q\n" 5507 " */", 5508 format("/*\n" 5509 " *q\n" 5510 " */")); 5511 EXPECT_EQ("/*\n" 5512 " * q\n" 5513 " */", 5514 format("/*\n" 5515 " * q\n" 5516 " */")); 5517 EXPECT_EQ("/*\n" 5518 " **/", 5519 format("/*\n" 5520 " **/")); 5521 EXPECT_EQ("/*\n" 5522 " ***/", 5523 format("/*\n" 5524 " ***/")); 5525 } 5526 5527 TEST_F(FormatTest, BlockCommentsInMacros) { 5528 EXPECT_EQ("#define A \\\n" 5529 " { \\\n" 5530 " /* one line */ \\\n" 5531 " someCall();", 5532 format("#define A { \\\n" 5533 " /* one line */ \\\n" 5534 " someCall();", 5535 getLLVMStyleWithColumns(20))); 5536 EXPECT_EQ("#define A \\\n" 5537 " { \\\n" 5538 " /* previous */ \\\n" 5539 " /* one line */ \\\n" 5540 " someCall();", 5541 format("#define A { \\\n" 5542 " /* previous */ \\\n" 5543 " /* one line */ \\\n" 5544 " someCall();", 5545 getLLVMStyleWithColumns(20))); 5546 } 5547 5548 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 5549 EXPECT_EQ("a = {\n" 5550 " 1111 /* */\n" 5551 "};", 5552 format("a = {1111 /* */\n" 5553 "};", 5554 getLLVMStyleWithColumns(15))); 5555 EXPECT_EQ("a = {\n" 5556 " 1111 /* */\n" 5557 "};", 5558 format("a = {1111 /* */\n" 5559 "};", 5560 getLLVMStyleWithColumns(15))); 5561 5562 // FIXME: The formatting is still wrong here. 5563 EXPECT_EQ("a = {\n" 5564 " 1111 /* a\n" 5565 " */\n" 5566 "};", 5567 format("a = {1111 /* a */\n" 5568 "};", 5569 getLLVMStyleWithColumns(15))); 5570 } 5571 5572 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 5573 // FIXME: This is not what we want... 5574 verifyFormat("{\n" 5575 "// a" 5576 "// b"); 5577 } 5578 5579 TEST_F(FormatTest, FormatStarDependingOnContext) { 5580 verifyFormat("void f(int *a);"); 5581 verifyFormat("void f() { f(fint * b); }"); 5582 verifyFormat("class A {\n void f(int *a);\n};"); 5583 verifyFormat("class A {\n int *a;\n};"); 5584 verifyFormat("namespace a {\n" 5585 "namespace b {\n" 5586 "class A {\n" 5587 " void f() {}\n" 5588 " int *a;\n" 5589 "};\n" 5590 "}\n" 5591 "}"); 5592 } 5593 5594 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 5595 verifyFormat("while"); 5596 verifyFormat("operator"); 5597 } 5598 5599 //===----------------------------------------------------------------------===// 5600 // Objective-C tests. 5601 //===----------------------------------------------------------------------===// 5602 5603 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 5604 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 5605 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 5606 format("-(NSUInteger)indexOfObject:(id)anObject;")); 5607 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 5608 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 5609 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 5610 format("-(NSInteger)Method3:(id)anObject;")); 5611 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 5612 format("-(NSInteger)Method4:(id)anObject;")); 5613 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 5614 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 5615 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 5616 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 5617 EXPECT_EQ( 5618 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 5619 format( 5620 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 5621 5622 // Very long objectiveC method declaration. 5623 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 5624 " inRange:(NSRange)range\n" 5625 " outRange:(NSRange)out_range\n" 5626 " outRange1:(NSRange)out_range1\n" 5627 " outRange2:(NSRange)out_range2\n" 5628 " outRange3:(NSRange)out_range3\n" 5629 " outRange4:(NSRange)out_range4\n" 5630 " outRange5:(NSRange)out_range5\n" 5631 " outRange6:(NSRange)out_range6\n" 5632 " outRange7:(NSRange)out_range7\n" 5633 " outRange8:(NSRange)out_range8\n" 5634 " outRange9:(NSRange)out_range9;"); 5635 5636 verifyFormat("- (int)sum:(vector<int>)numbers;"); 5637 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 5638 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 5639 // protocol lists (but not for template classes): 5640 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 5641 5642 verifyFormat("- (int (*)())foo:(int (*)())f;"); 5643 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 5644 5645 // If there's no return type (very rare in practice!), LLVM and Google style 5646 // agree. 5647 verifyFormat("- foo;"); 5648 verifyFormat("- foo:(int)f;"); 5649 verifyGoogleFormat("- foo:(int)foo;"); 5650 } 5651 5652 TEST_F(FormatTest, FormatObjCInterface) { 5653 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 5654 "@public\n" 5655 " int field1;\n" 5656 "@protected\n" 5657 " int field2;\n" 5658 "@private\n" 5659 " int field3;\n" 5660 "@package\n" 5661 " int field4;\n" 5662 "}\n" 5663 "+ (id)init;\n" 5664 "@end"); 5665 5666 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 5667 " @public\n" 5668 " int field1;\n" 5669 " @protected\n" 5670 " int field2;\n" 5671 " @private\n" 5672 " int field3;\n" 5673 " @package\n" 5674 " int field4;\n" 5675 "}\n" 5676 "+ (id)init;\n" 5677 "@end"); 5678 5679 verifyFormat("@interface /* wait for it */ Foo\n" 5680 "+ (id)init;\n" 5681 "// Look, a comment!\n" 5682 "- (int)answerWith:(int)i;\n" 5683 "@end"); 5684 5685 verifyFormat("@interface Foo\n" 5686 "@end\n" 5687 "@interface Bar\n" 5688 "@end"); 5689 5690 verifyFormat("@interface Foo : Bar\n" 5691 "+ (id)init;\n" 5692 "@end"); 5693 5694 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 5695 "+ (id)init;\n" 5696 "@end"); 5697 5698 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 5699 "+ (id)init;\n" 5700 "@end"); 5701 5702 verifyFormat("@interface Foo (HackStuff)\n" 5703 "+ (id)init;\n" 5704 "@end"); 5705 5706 verifyFormat("@interface Foo ()\n" 5707 "+ (id)init;\n" 5708 "@end"); 5709 5710 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 5711 "+ (id)init;\n" 5712 "@end"); 5713 5714 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 5715 "+ (id)init;\n" 5716 "@end"); 5717 5718 verifyFormat("@interface Foo {\n" 5719 " int _i;\n" 5720 "}\n" 5721 "+ (id)init;\n" 5722 "@end"); 5723 5724 verifyFormat("@interface Foo : Bar {\n" 5725 " int _i;\n" 5726 "}\n" 5727 "+ (id)init;\n" 5728 "@end"); 5729 5730 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 5731 " int _i;\n" 5732 "}\n" 5733 "+ (id)init;\n" 5734 "@end"); 5735 5736 verifyFormat("@interface Foo (HackStuff) {\n" 5737 " int _i;\n" 5738 "}\n" 5739 "+ (id)init;\n" 5740 "@end"); 5741 5742 verifyFormat("@interface Foo () {\n" 5743 " int _i;\n" 5744 "}\n" 5745 "+ (id)init;\n" 5746 "@end"); 5747 5748 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 5749 " int _i;\n" 5750 "}\n" 5751 "+ (id)init;\n" 5752 "@end"); 5753 } 5754 5755 TEST_F(FormatTest, FormatObjCImplementation) { 5756 verifyFormat("@implementation Foo : NSObject {\n" 5757 "@public\n" 5758 " int field1;\n" 5759 "@protected\n" 5760 " int field2;\n" 5761 "@private\n" 5762 " int field3;\n" 5763 "@package\n" 5764 " int field4;\n" 5765 "}\n" 5766 "+ (id)init {\n}\n" 5767 "@end"); 5768 5769 verifyGoogleFormat("@implementation Foo : NSObject {\n" 5770 " @public\n" 5771 " int field1;\n" 5772 " @protected\n" 5773 " int field2;\n" 5774 " @private\n" 5775 " int field3;\n" 5776 " @package\n" 5777 " int field4;\n" 5778 "}\n" 5779 "+ (id)init {\n}\n" 5780 "@end"); 5781 5782 verifyFormat("@implementation Foo\n" 5783 "+ (id)init {\n" 5784 " if (true)\n" 5785 " return nil;\n" 5786 "}\n" 5787 "// Look, a comment!\n" 5788 "- (int)answerWith:(int)i {\n" 5789 " return i;\n" 5790 "}\n" 5791 "+ (int)answerWith:(int)i {\n" 5792 " return i;\n" 5793 "}\n" 5794 "@end"); 5795 5796 verifyFormat("@implementation Foo\n" 5797 "@end\n" 5798 "@implementation Bar\n" 5799 "@end"); 5800 5801 verifyFormat("@implementation Foo : Bar\n" 5802 "+ (id)init {\n}\n" 5803 "- (void)foo {\n}\n" 5804 "@end"); 5805 5806 verifyFormat("@implementation Foo {\n" 5807 " int _i;\n" 5808 "}\n" 5809 "+ (id)init {\n}\n" 5810 "@end"); 5811 5812 verifyFormat("@implementation Foo : Bar {\n" 5813 " int _i;\n" 5814 "}\n" 5815 "+ (id)init {\n}\n" 5816 "@end"); 5817 5818 verifyFormat("@implementation Foo (HackStuff)\n" 5819 "+ (id)init {\n}\n" 5820 "@end"); 5821 verifyFormat("@implementation ObjcClass\n" 5822 "- (void)method;\n" 5823 "{}\n" 5824 "@end"); 5825 } 5826 5827 TEST_F(FormatTest, FormatObjCProtocol) { 5828 verifyFormat("@protocol Foo\n" 5829 "@property(weak) id delegate;\n" 5830 "- (NSUInteger)numberOfThings;\n" 5831 "@end"); 5832 5833 verifyFormat("@protocol MyProtocol <NSObject>\n" 5834 "- (NSUInteger)numberOfThings;\n" 5835 "@end"); 5836 5837 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 5838 "- (NSUInteger)numberOfThings;\n" 5839 "@end"); 5840 5841 verifyFormat("@protocol Foo;\n" 5842 "@protocol Bar;\n"); 5843 5844 verifyFormat("@protocol Foo\n" 5845 "@end\n" 5846 "@protocol Bar\n" 5847 "@end"); 5848 5849 verifyFormat("@protocol myProtocol\n" 5850 "- (void)mandatoryWithInt:(int)i;\n" 5851 "@optional\n" 5852 "- (void)optional;\n" 5853 "@required\n" 5854 "- (void)required;\n" 5855 "@optional\n" 5856 "@property(assign) int madProp;\n" 5857 "@end\n"); 5858 5859 verifyFormat("@property(nonatomic, assign, readonly)\n" 5860 " int *looooooooooooooooooooooooooooongNumber;\n" 5861 "@property(nonatomic, assign, readonly)\n" 5862 " NSString *looooooooooooooooooooooooooooongName;"); 5863 5864 verifyFormat("@implementation PR18406\n" 5865 "}\n" 5866 "@end"); 5867 } 5868 5869 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 5870 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 5871 " rect:(NSRect)theRect\n" 5872 " interval:(float)theInterval {\n" 5873 "}"); 5874 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 5875 " longKeyword:(NSRect)theRect\n" 5876 " evenLongerKeyword:(float)theInterval\n" 5877 " error:(NSError **)theError {\n" 5878 "}"); 5879 } 5880 5881 TEST_F(FormatTest, FormatObjCMethodExpr) { 5882 verifyFormat("[foo bar:baz];"); 5883 verifyFormat("return [foo bar:baz];"); 5884 verifyFormat("f([foo bar:baz]);"); 5885 verifyFormat("f(2, [foo bar:baz]);"); 5886 verifyFormat("f(2, a ? b : c);"); 5887 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 5888 5889 // Unary operators. 5890 verifyFormat("int a = +[foo bar:baz];"); 5891 verifyFormat("int a = -[foo bar:baz];"); 5892 verifyFormat("int a = ![foo bar:baz];"); 5893 verifyFormat("int a = ~[foo bar:baz];"); 5894 verifyFormat("int a = ++[foo bar:baz];"); 5895 verifyFormat("int a = --[foo bar:baz];"); 5896 verifyFormat("int a = sizeof [foo bar:baz];"); 5897 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 5898 verifyFormat("int a = &[foo bar:baz];"); 5899 verifyFormat("int a = *[foo bar:baz];"); 5900 // FIXME: Make casts work, without breaking f()[4]. 5901 //verifyFormat("int a = (int)[foo bar:baz];"); 5902 //verifyFormat("return (int)[foo bar:baz];"); 5903 //verifyFormat("(void)[foo bar:baz];"); 5904 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 5905 5906 // Binary operators. 5907 verifyFormat("[foo bar:baz], [foo bar:baz];"); 5908 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 5909 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 5910 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 5911 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 5912 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 5913 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 5914 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 5915 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 5916 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 5917 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 5918 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 5919 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 5920 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 5921 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 5922 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 5923 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 5924 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 5925 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 5926 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 5927 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 5928 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 5929 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 5930 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 5931 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 5932 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 5933 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 5934 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 5935 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 5936 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 5937 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 5938 // Whew! 5939 5940 verifyFormat("return in[42];"); 5941 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 5942 "}"); 5943 5944 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 5945 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 5946 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 5947 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 5948 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 5949 verifyFormat("[button setAction:@selector(zoomOut:)];"); 5950 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 5951 5952 verifyFormat("arr[[self indexForFoo:a]];"); 5953 verifyFormat("throw [self errorFor:a];"); 5954 verifyFormat("@throw [self errorFor:a];"); 5955 5956 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 5957 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 5958 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 5959 5960 // This tests that the formatter doesn't break after "backing" but before ":", 5961 // which would be at 80 columns. 5962 verifyFormat( 5963 "void f() {\n" 5964 " if ((self = [super initWithContentRect:contentRect\n" 5965 " styleMask:styleMask ?: otherMask\n" 5966 " backing:NSBackingStoreBuffered\n" 5967 " defer:YES]))"); 5968 5969 verifyFormat( 5970 "[foo checkThatBreakingAfterColonWorksOk:\n" 5971 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 5972 5973 verifyFormat("[myObj short:arg1 // Force line break\n" 5974 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 5975 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 5976 " error:arg4];"); 5977 verifyFormat( 5978 "void f() {\n" 5979 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 5980 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 5981 " pos.width(), pos.height())\n" 5982 " styleMask:NSBorderlessWindowMask\n" 5983 " backing:NSBackingStoreBuffered\n" 5984 " defer:NO]);\n" 5985 "}"); 5986 verifyFormat( 5987 "void f() {\n" 5988 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 5989 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 5990 " pos.width(), pos.height())\n" 5991 " syeMask:NSBorderlessWindowMask\n" 5992 " bking:NSBackingStoreBuffered\n" 5993 " der:NO]);\n" 5994 "}", 5995 getLLVMStyleWithColumns(70)); 5996 verifyFormat("{\n" 5997 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 5998 " initWithContentRect:NSMakeRect(origin_global.x,\n" 5999 " origin_global.y,\n" 6000 " pos.width(),\n" 6001 " pos.height())\n" 6002 " styleMask:NSBorderlessWindowMask\n" 6003 " backing:NSBackingStoreBuffered\n" 6004 " defer:NO]);\n" 6005 "}", 6006 getChromiumStyle(FormatStyle::LK_Cpp)); 6007 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 6008 " with:contentsNativeView];"); 6009 6010 verifyFormat( 6011 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 6012 " owner:nillllll];"); 6013 6014 verifyFormat( 6015 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 6016 " forType:kBookmarkButtonDragType];"); 6017 6018 verifyFormat("[defaultCenter addObserver:self\n" 6019 " selector:@selector(willEnterFullscreen)\n" 6020 " name:kWillEnterFullscreenNotification\n" 6021 " object:nil];"); 6022 verifyFormat("[image_rep drawInRect:drawRect\n" 6023 " fromRect:NSZeroRect\n" 6024 " operation:NSCompositeCopy\n" 6025 " fraction:1.0\n" 6026 " respectFlipped:NO\n" 6027 " hints:nil];"); 6028 6029 verifyFormat( 6030 "scoped_nsobject<NSTextField> message(\n" 6031 " // The frame will be fixed up when |-setMessageText:| is called.\n" 6032 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 6033 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 6034 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 6035 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 6036 " aaaa:bbb];"); 6037 verifyFormat("[self param:function( //\n" 6038 " parameter)]"); 6039 verifyFormat( 6040 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 6041 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 6042 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 6043 6044 // Variadic parameters. 6045 verifyFormat( 6046 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 6047 verifyFormat( 6048 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 6049 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 6050 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 6051 verifyFormat("[self // break\n" 6052 " a:a\n" 6053 " aaa:aaa];"); 6054 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 6055 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 6056 } 6057 6058 TEST_F(FormatTest, ObjCAt) { 6059 verifyFormat("@autoreleasepool"); 6060 verifyFormat("@catch"); 6061 verifyFormat("@class"); 6062 verifyFormat("@compatibility_alias"); 6063 verifyFormat("@defs"); 6064 verifyFormat("@dynamic"); 6065 verifyFormat("@encode"); 6066 verifyFormat("@end"); 6067 verifyFormat("@finally"); 6068 verifyFormat("@implementation"); 6069 verifyFormat("@import"); 6070 verifyFormat("@interface"); 6071 verifyFormat("@optional"); 6072 verifyFormat("@package"); 6073 verifyFormat("@private"); 6074 verifyFormat("@property"); 6075 verifyFormat("@protected"); 6076 verifyFormat("@protocol"); 6077 verifyFormat("@public"); 6078 verifyFormat("@required"); 6079 verifyFormat("@selector"); 6080 verifyFormat("@synchronized"); 6081 verifyFormat("@synthesize"); 6082 verifyFormat("@throw"); 6083 verifyFormat("@try"); 6084 6085 EXPECT_EQ("@interface", format("@ interface")); 6086 6087 // The precise formatting of this doesn't matter, nobody writes code like 6088 // this. 6089 verifyFormat("@ /*foo*/ interface"); 6090 } 6091 6092 TEST_F(FormatTest, ObjCSnippets) { 6093 verifyFormat("@autoreleasepool {\n" 6094 " foo();\n" 6095 "}"); 6096 verifyFormat("@class Foo, Bar;"); 6097 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 6098 verifyFormat("@dynamic textColor;"); 6099 verifyFormat("char *buf1 = @encode(int *);"); 6100 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 6101 verifyFormat("char *buf1 = @encode(int **);"); 6102 verifyFormat("Protocol *proto = @protocol(p1);"); 6103 verifyFormat("SEL s = @selector(foo:);"); 6104 verifyFormat("@synchronized(self) {\n" 6105 " f();\n" 6106 "}"); 6107 6108 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 6109 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 6110 6111 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 6112 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 6113 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 6114 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 6115 getMozillaStyle()); 6116 verifyFormat("@property BOOL editable;", getMozillaStyle()); 6117 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 6118 getWebKitStyle()); 6119 verifyFormat("@property BOOL editable;", getWebKitStyle()); 6120 6121 verifyFormat("@import foo.bar;\n" 6122 "@import baz;"); 6123 } 6124 6125 TEST_F(FormatTest, ObjCLiterals) { 6126 verifyFormat("@\"String\""); 6127 verifyFormat("@1"); 6128 verifyFormat("@+4.8"); 6129 verifyFormat("@-4"); 6130 verifyFormat("@1LL"); 6131 verifyFormat("@.5"); 6132 verifyFormat("@'c'"); 6133 verifyFormat("@true"); 6134 6135 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 6136 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 6137 verifyFormat("NSNumber *favoriteColor = @(Green);"); 6138 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 6139 6140 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 6141 } 6142 6143 TEST_F(FormatTest, ObjCDictLiterals) { 6144 verifyFormat("@{"); 6145 verifyFormat("@{}"); 6146 verifyFormat("@{@\"one\" : @1}"); 6147 verifyFormat("return @{@\"one\" : @1;"); 6148 verifyFormat("@{@\"one\" : @1}"); 6149 6150 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 6151 verifyFormat("@{\n" 6152 " @\"one\" : @{@2 : @1},\n" 6153 "}"); 6154 6155 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 6156 verifyFormat("[self setDict:@{}"); 6157 verifyFormat("[self setDict:@{@1 : @2}"); 6158 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 6159 verifyFormat( 6160 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 6161 verifyFormat( 6162 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 6163 6164 verifyFormat( 6165 "NSDictionary *d = @{\n" 6166 " @\"nam\" : NSUserNam(),\n" 6167 " @\"dte\" : [NSDate date],\n" 6168 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 6169 "};"); 6170 verifyFormat( 6171 "@{\n" 6172 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 6173 "regularFont,\n" 6174 "};"); 6175 verifyGoogleFormat( 6176 "@{\n" 6177 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 6178 "regularFont,\n" 6179 "};"); 6180 verifyFormat( 6181 "@{\n" 6182 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 6183 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 6184 "};"); 6185 6186 // We should try to be robust in case someone forgets the "@". 6187 verifyFormat( 6188 "NSDictionary *d = {\n" 6189 " @\"nam\" : NSUserNam(),\n" 6190 " @\"dte\" : [NSDate date],\n" 6191 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 6192 "};"); 6193 } 6194 6195 TEST_F(FormatTest, ObjCArrayLiterals) { 6196 verifyFormat("@["); 6197 verifyFormat("@[]"); 6198 verifyFormat( 6199 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 6200 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 6201 verifyFormat("NSArray *array = @[ [foo description] ];"); 6202 6203 verifyFormat( 6204 "NSArray *some_variable = @[\n" 6205 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 6206 " @\"aaaaaaaaaaaaaaaaa\",\n" 6207 " @\"aaaaaaaaaaaaaaaaa\",\n" 6208 " @\"aaaaaaaaaaaaaaaaa\"\n" 6209 "];"); 6210 verifyFormat("NSArray *some_variable = @[\n" 6211 " @\"aaaaaaaaaaaaaaaaa\",\n" 6212 " @\"aaaaaaaaaaaaaaaaa\",\n" 6213 " @\"aaaaaaaaaaaaaaaaa\",\n" 6214 " @\"aaaaaaaaaaaaaaaaa\",\n" 6215 "];"); 6216 verifyGoogleFormat("NSArray *some_variable = @[\n" 6217 " @\"aaaaaaaaaaaaaaaaa\",\n" 6218 " @\"aaaaaaaaaaaaaaaaa\",\n" 6219 " @\"aaaaaaaaaaaaaaaaa\",\n" 6220 " @\"aaaaaaaaaaaaaaaaa\"\n" 6221 "];"); 6222 6223 // We should try to be robust in case someone forgets the "@". 6224 verifyFormat("NSArray *some_variable = [\n" 6225 " @\"aaaaaaaaaaaaaaaaa\",\n" 6226 " @\"aaaaaaaaaaaaaaaaa\",\n" 6227 " @\"aaaaaaaaaaaaaaaaa\",\n" 6228 " @\"aaaaaaaaaaaaaaaaa\",\n" 6229 "];"); 6230 verifyFormat( 6231 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 6232 " index:(NSUInteger)index\n" 6233 " nonDigitAttributes:\n" 6234 " (NSDictionary *)noDigitAttributes;"); 6235 verifyFormat( 6236 "[someFunction someLooooooooooooongParameter:\n" 6237 " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];"); 6238 } 6239 6240 TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 6241 EXPECT_EQ("{\n" 6242 "{\n" 6243 "a;\n" 6244 "b;\n" 6245 "}\n" 6246 "}", 6247 format("{\n" 6248 "{\n" 6249 "a;\n" 6250 " b;\n" 6251 "}\n" 6252 "}", 6253 13, 2, getLLVMStyle())); 6254 EXPECT_EQ("{\n" 6255 "{\n" 6256 " a;\n" 6257 "b;\n" 6258 "}\n" 6259 "}", 6260 format("{\n" 6261 "{\n" 6262 " a;\n" 6263 "b;\n" 6264 "}\n" 6265 "}", 6266 9, 2, getLLVMStyle())); 6267 EXPECT_EQ("{\n" 6268 "{\n" 6269 "public:\n" 6270 " b;\n" 6271 "}\n" 6272 "}", 6273 format("{\n" 6274 "{\n" 6275 "public:\n" 6276 " b;\n" 6277 "}\n" 6278 "}", 6279 17, 2, getLLVMStyle())); 6280 EXPECT_EQ("{\n" 6281 "{\n" 6282 "a;\n" 6283 "}\n" 6284 "{\n" 6285 " b; //\n" 6286 "}\n" 6287 "}", 6288 format("{\n" 6289 "{\n" 6290 "a;\n" 6291 "}\n" 6292 "{\n" 6293 " b; //\n" 6294 "}\n" 6295 "}", 6296 22, 2, getLLVMStyle())); 6297 EXPECT_EQ(" {\n" 6298 " a; //\n" 6299 " }", 6300 format(" {\n" 6301 "a; //\n" 6302 " }", 6303 4, 2, getLLVMStyle())); 6304 EXPECT_EQ("void f() {}\n" 6305 "void g() {}", 6306 format("void f() {}\n" 6307 "void g() {}", 6308 13, 0, getLLVMStyle())); 6309 EXPECT_EQ("int a; // comment\n" 6310 " // line 2\n" 6311 "int b;", 6312 format("int a; // comment\n" 6313 " // line 2\n" 6314 " int b;", 6315 35, 0, getLLVMStyle())); 6316 EXPECT_EQ(" int a;\n" 6317 " void\n" 6318 " ffffff() {\n" 6319 " }", 6320 format(" int a;\n" 6321 "void ffffff() {}", 6322 11, 0, getLLVMStyleWithColumns(11))); 6323 6324 EXPECT_EQ(" void f() {\n" 6325 "#define A 1\n" 6326 " }", 6327 format(" void f() {\n" 6328 " #define A 1\n" // Format this line. 6329 " }", 6330 20, 0, getLLVMStyle())); 6331 EXPECT_EQ(" void f() {\n" 6332 " int i;\n" 6333 "#define A \\\n" 6334 " int i; \\\n" 6335 " int j;\n" 6336 " int k;\n" 6337 " }", 6338 format(" void f() {\n" 6339 " int i;\n" 6340 "#define A \\\n" 6341 " int i; \\\n" 6342 " int j;\n" 6343 " int k;\n" // Format this line. 6344 " }", 6345 67, 0, getLLVMStyle())); 6346 } 6347 6348 TEST_F(FormatTest, BreakStringLiterals) { 6349 EXPECT_EQ("\"some text \"\n" 6350 "\"other\";", 6351 format("\"some text other\";", getLLVMStyleWithColumns(12))); 6352 EXPECT_EQ("\"some text \"\n" 6353 "\"other\";", 6354 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 6355 EXPECT_EQ( 6356 "#define A \\\n" 6357 " \"some \" \\\n" 6358 " \"text \" \\\n" 6359 " \"other\";", 6360 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 6361 EXPECT_EQ( 6362 "#define A \\\n" 6363 " \"so \" \\\n" 6364 " \"text \" \\\n" 6365 " \"other\";", 6366 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 6367 6368 EXPECT_EQ("\"some text\"", 6369 format("\"some text\"", getLLVMStyleWithColumns(1))); 6370 EXPECT_EQ("\"some text\"", 6371 format("\"some text\"", getLLVMStyleWithColumns(11))); 6372 EXPECT_EQ("\"some \"\n" 6373 "\"text\"", 6374 format("\"some text\"", getLLVMStyleWithColumns(10))); 6375 EXPECT_EQ("\"some \"\n" 6376 "\"text\"", 6377 format("\"some text\"", getLLVMStyleWithColumns(7))); 6378 EXPECT_EQ("\"some\"\n" 6379 "\" tex\"\n" 6380 "\"t\"", 6381 format("\"some text\"", getLLVMStyleWithColumns(6))); 6382 EXPECT_EQ("\"some\"\n" 6383 "\" tex\"\n" 6384 "\" and\"", 6385 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 6386 EXPECT_EQ("\"some\"\n" 6387 "\"/tex\"\n" 6388 "\"/and\"", 6389 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 6390 6391 EXPECT_EQ("variable =\n" 6392 " \"long string \"\n" 6393 " \"literal\";", 6394 format("variable = \"long string literal\";", 6395 getLLVMStyleWithColumns(20))); 6396 6397 EXPECT_EQ("variable = f(\n" 6398 " \"long string \"\n" 6399 " \"literal\",\n" 6400 " short,\n" 6401 " loooooooooooooooooooong);", 6402 format("variable = f(\"long string literal\", short, " 6403 "loooooooooooooooooooong);", 6404 getLLVMStyleWithColumns(20))); 6405 6406 EXPECT_EQ("f(g(\"long string \"\n" 6407 " \"literal\"),\n" 6408 " b);", 6409 format("f(g(\"long string literal\"), b);", 6410 getLLVMStyleWithColumns(20))); 6411 EXPECT_EQ("f(g(\"long string \"\n" 6412 " \"literal\",\n" 6413 " a),\n" 6414 " b);", 6415 format("f(g(\"long string literal\", a), b);", 6416 getLLVMStyleWithColumns(20))); 6417 EXPECT_EQ( 6418 "f(\"one two\".split(\n" 6419 " variable));", 6420 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 6421 EXPECT_EQ("f(\"one two three four five six \"\n" 6422 " \"seven\".split(\n" 6423 " really_looooong_variable));", 6424 format("f(\"one two three four five six seven\"." 6425 "split(really_looooong_variable));", 6426 getLLVMStyleWithColumns(33))); 6427 6428 EXPECT_EQ("f(\"some \"\n" 6429 " \"text\",\n" 6430 " other);", 6431 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 6432 6433 // Only break as a last resort. 6434 verifyFormat( 6435 "aaaaaaaaaaaaaaaaaaaa(\n" 6436 " aaaaaaaaaaaaaaaaaaaa,\n" 6437 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 6438 6439 EXPECT_EQ( 6440 "\"splitmea\"\n" 6441 "\"trandomp\"\n" 6442 "\"oint\"", 6443 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 6444 6445 EXPECT_EQ( 6446 "\"split/\"\n" 6447 "\"pathat/\"\n" 6448 "\"slashes\"", 6449 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6450 6451 EXPECT_EQ( 6452 "\"split/\"\n" 6453 "\"pathat/\"\n" 6454 "\"slashes\"", 6455 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6456 EXPECT_EQ("\"split at \"\n" 6457 "\"spaces/at/\"\n" 6458 "\"slashes.at.any$\"\n" 6459 "\"non-alphanumeric%\"\n" 6460 "\"1111111111characte\"\n" 6461 "\"rs\"", 6462 format("\"split at " 6463 "spaces/at/" 6464 "slashes.at." 6465 "any$non-" 6466 "alphanumeric%" 6467 "1111111111characte" 6468 "rs\"", 6469 getLLVMStyleWithColumns(20))); 6470 6471 // Verify that splitting the strings understands 6472 // Style::AlwaysBreakBeforeMultilineStrings. 6473 EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n" 6474 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 6475 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 6476 format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa " 6477 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa " 6478 "aaaaaaaaaaaaaaaaaaaaaa\");", 6479 getGoogleStyle())); 6480 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6481 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 6482 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 6483 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 6484 "aaaaaaaaaaaaaaaaaaaaaa\";", 6485 getGoogleStyle())); 6486 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6487 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 6488 format("llvm::outs() << " 6489 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 6490 "aaaaaaaaaaaaaaaaaaa\";")); 6491 EXPECT_EQ("ffff(\n" 6492 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6493 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 6494 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 6495 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 6496 getGoogleStyle())); 6497 6498 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 6499 AlignLeft.AlignEscapedNewlinesLeft = true; 6500 EXPECT_EQ( 6501 "#define A \\\n" 6502 " \"some \" \\\n" 6503 " \"text \" \\\n" 6504 " \"other\";", 6505 format("#define A \"some text other\";", AlignLeft)); 6506 } 6507 6508 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 6509 EXPECT_EQ( 6510 "u8\"utf8 string \"\n" 6511 "u8\"literal\";", 6512 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 6513 EXPECT_EQ( 6514 "u\"utf16 string \"\n" 6515 "u\"literal\";", 6516 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 6517 EXPECT_EQ( 6518 "U\"utf32 string \"\n" 6519 "U\"literal\";", 6520 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 6521 EXPECT_EQ("L\"wide string \"\n" 6522 "L\"literal\";", 6523 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 6524 EXPECT_EQ("@\"NSString \"\n" 6525 "@\"literal\";", 6526 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 6527 } 6528 6529 TEST_F(FormatTest, BreaksRawStringLiterals) { 6530 EXPECT_EQ("R\"x(raw )x\"\n" 6531 "R\"x(literal)x\";", 6532 format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15))); 6533 EXPECT_EQ("uR\"x(raw )x\"\n" 6534 "uR\"x(literal)x\";", 6535 format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16))); 6536 EXPECT_EQ("u8R\"x(raw )x\"\n" 6537 "u8R\"x(literal)x\";", 6538 format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17))); 6539 EXPECT_EQ("LR\"x(raw )x\"\n" 6540 "LR\"x(literal)x\";", 6541 format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16))); 6542 EXPECT_EQ("UR\"x(raw )x\"\n" 6543 "UR\"x(literal)x\";", 6544 format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16))); 6545 } 6546 6547 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 6548 FormatStyle Style = getLLVMStyleWithColumns(20); 6549 EXPECT_EQ( 6550 "_T(\"aaaaaaaaaaaaaa\")\n" 6551 "_T(\"aaaaaaaaaaaaaa\")\n" 6552 "_T(\"aaaaaaaaaaaa\")", 6553 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 6554 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 6555 " _T(\"aaaaaa\"),\n" 6556 " z);", 6557 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 6558 6559 // FIXME: Handle embedded spaces in one iteration. 6560 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 6561 // "_T(\"aaaaaaaaaaaaa\")\n" 6562 // "_T(\"aaaaaaaaaaaaa\")\n" 6563 // "_T(\"a\")", 6564 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 6565 // getLLVMStyleWithColumns(20))); 6566 EXPECT_EQ( 6567 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 6568 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 6569 } 6570 6571 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 6572 EXPECT_EQ( 6573 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 6576 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 6579 } 6580 6581 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 6582 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 6583 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 6584 EXPECT_EQ("fffffffffff(g(R\"x(\n" 6585 "multiline raw string literal xxxxxxxxxxxxxx\n" 6586 ")x\",\n" 6587 " a),\n" 6588 " b);", 6589 format("fffffffffff(g(R\"x(\n" 6590 "multiline raw string literal xxxxxxxxxxxxxx\n" 6591 ")x\", a), b);", 6592 getGoogleStyleWithColumns(20))); 6593 EXPECT_EQ("fffffffffff(\n" 6594 " g(R\"x(qqq\n" 6595 "multiline raw string literal xxxxxxxxxxxxxx\n" 6596 ")x\",\n" 6597 " a),\n" 6598 " b);", 6599 format("fffffffffff(g(R\"x(qqq\n" 6600 "multiline raw string literal xxxxxxxxxxxxxx\n" 6601 ")x\", a), b);", 6602 getGoogleStyleWithColumns(20))); 6603 6604 EXPECT_EQ("fffffffffff(R\"x(\n" 6605 "multiline raw string literal xxxxxxxxxxxxxx\n" 6606 ")x\");", 6607 format("fffffffffff(R\"x(\n" 6608 "multiline raw string literal xxxxxxxxxxxxxx\n" 6609 ")x\");", 6610 getGoogleStyleWithColumns(20))); 6611 EXPECT_EQ("fffffffffff(R\"x(\n" 6612 "multiline raw string literal xxxxxxxxxxxxxx\n" 6613 ")x\" + bbbbbb);", 6614 format("fffffffffff(R\"x(\n" 6615 "multiline raw string literal xxxxxxxxxxxxxx\n" 6616 ")x\" + bbbbbb);", 6617 getGoogleStyleWithColumns(20))); 6618 EXPECT_EQ("fffffffffff(\n" 6619 " R\"x(\n" 6620 "multiline raw string literal xxxxxxxxxxxxxx\n" 6621 ")x\" +\n" 6622 " bbbbbb);", 6623 format("fffffffffff(\n" 6624 " R\"x(\n" 6625 "multiline raw string literal xxxxxxxxxxxxxx\n" 6626 ")x\" + bbbbbb);", 6627 getGoogleStyleWithColumns(20))); 6628 } 6629 6630 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 6631 verifyFormat("string a = \"unterminated;"); 6632 EXPECT_EQ("function(\"unterminated,\n" 6633 " OtherParameter);", 6634 format("function( \"unterminated,\n" 6635 " OtherParameter);")); 6636 } 6637 6638 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 6639 FormatStyle Style = getLLVMStyle(); 6640 Style.Standard = FormatStyle::LS_Cpp03; 6641 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 6642 format("#define x(_a) printf(\"foo\"_a);", Style)); 6643 } 6644 6645 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 6646 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 6647 " \"ddeeefff\");", 6648 format("someFunction(\"aaabbbcccdddeeefff\");", 6649 getLLVMStyleWithColumns(25))); 6650 EXPECT_EQ("someFunction1234567890(\n" 6651 " \"aaabbbcccdddeeefff\");", 6652 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 6653 getLLVMStyleWithColumns(26))); 6654 EXPECT_EQ("someFunction1234567890(\n" 6655 " \"aaabbbcccdddeeeff\"\n" 6656 " \"f\");", 6657 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 6658 getLLVMStyleWithColumns(25))); 6659 EXPECT_EQ("someFunction1234567890(\n" 6660 " \"aaabbbcccdddeeeff\"\n" 6661 " \"f\");", 6662 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 6663 getLLVMStyleWithColumns(24))); 6664 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 6665 " \"ddde \"\n" 6666 " \"efff\");", 6667 format("someFunction(\"aaabbbcc ddde efff\");", 6668 getLLVMStyleWithColumns(25))); 6669 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 6670 " \"ddeeefff\");", 6671 format("someFunction(\"aaabbbccc ddeeefff\");", 6672 getLLVMStyleWithColumns(25))); 6673 EXPECT_EQ("someFunction1234567890(\n" 6674 " \"aaabb \"\n" 6675 " \"cccdddeeefff\");", 6676 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 6677 getLLVMStyleWithColumns(25))); 6678 EXPECT_EQ("#define A \\\n" 6679 " string s = \\\n" 6680 " \"123456789\" \\\n" 6681 " \"0\"; \\\n" 6682 " int i;", 6683 format("#define A string s = \"1234567890\"; int i;", 6684 getLLVMStyleWithColumns(20))); 6685 // FIXME: Put additional penalties on breaking at non-whitespace locations. 6686 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 6687 " \"dddeeeff\"\n" 6688 " \"f\");", 6689 format("someFunction(\"aaabbbcc dddeeefff\");", 6690 getLLVMStyleWithColumns(25))); 6691 } 6692 6693 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 6694 EXPECT_EQ("\"\\a\"", 6695 format("\"\\a\"", getLLVMStyleWithColumns(3))); 6696 EXPECT_EQ("\"\\\"", 6697 format("\"\\\"", getLLVMStyleWithColumns(2))); 6698 EXPECT_EQ("\"test\"\n" 6699 "\"\\n\"", 6700 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 6701 EXPECT_EQ("\"tes\\\\\"\n" 6702 "\"n\"", 6703 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 6704 EXPECT_EQ("\"\\\\\\\\\"\n" 6705 "\"\\n\"", 6706 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 6707 EXPECT_EQ("\"\\uff01\"", 6708 format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 6709 EXPECT_EQ("\"\\uff01\"\n" 6710 "\"test\"", 6711 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 6712 EXPECT_EQ("\"\\Uff01ff02\"", 6713 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 6714 EXPECT_EQ("\"\\x000000000001\"\n" 6715 "\"next\"", 6716 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 6717 EXPECT_EQ("\"\\x000000000001next\"", 6718 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 6719 EXPECT_EQ("\"\\x000000000001\"", 6720 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 6721 EXPECT_EQ("\"test\"\n" 6722 "\"\\000000\"\n" 6723 "\"000001\"", 6724 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 6725 EXPECT_EQ("\"test\\000\"\n" 6726 "\"00000000\"\n" 6727 "\"1\"", 6728 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 6729 // FIXME: We probably don't need to care about escape sequences in raw 6730 // literals. 6731 EXPECT_EQ("R\"(\\x)\"\n" 6732 "R\"(\\x00)\"\n", 6733 format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7))); 6734 } 6735 6736 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 6737 verifyFormat("void f() {\n" 6738 " return g() {}\n" 6739 " void h() {}"); 6740 verifyFormat("int a[] = {void forgot_closing_brace() {f();\n" 6741 "g();\n" 6742 "}"); 6743 } 6744 6745 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 6746 verifyFormat( 6747 "void f() {\n" 6748 " return C{param1, param2}.SomeCall(param1, param2);\n" 6749 "}\n"); 6750 } 6751 6752 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 6753 verifyFormat("class X {\n" 6754 " void f() {\n" 6755 " }\n" 6756 "};", 6757 getLLVMStyleWithColumns(12)); 6758 } 6759 6760 TEST_F(FormatTest, ConfigurableIndentWidth) { 6761 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 6762 EightIndent.IndentWidth = 8; 6763 EightIndent.ContinuationIndentWidth = 8; 6764 verifyFormat("void f() {\n" 6765 " someFunction();\n" 6766 " if (true) {\n" 6767 " f();\n" 6768 " }\n" 6769 "}", 6770 EightIndent); 6771 verifyFormat("class X {\n" 6772 " void f() {\n" 6773 " }\n" 6774 "};", 6775 EightIndent); 6776 verifyFormat("int x[] = {\n" 6777 " call(),\n" 6778 " call()};", 6779 EightIndent); 6780 } 6781 6782 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 6783 verifyFormat("void\n" 6784 "f();", 6785 getLLVMStyleWithColumns(8)); 6786 } 6787 6788 TEST_F(FormatTest, ConfigurableUseOfTab) { 6789 FormatStyle Tab = getLLVMStyleWithColumns(42); 6790 Tab.IndentWidth = 8; 6791 Tab.UseTab = FormatStyle::UT_Always; 6792 Tab.AlignEscapedNewlinesLeft = true; 6793 6794 EXPECT_EQ("if (aaaaaaaa && // q\n" 6795 " bb)\t\t// w\n" 6796 "\t;", 6797 format("if (aaaaaaaa &&// q\n" 6798 "bb)// w\n" 6799 ";", 6800 Tab)); 6801 EXPECT_EQ("if (aaa && bbb) // w\n" 6802 "\t;", 6803 format("if(aaa&&bbb)// w\n" 6804 ";", 6805 Tab)); 6806 6807 verifyFormat("class X {\n" 6808 "\tvoid f() {\n" 6809 "\t\tsomeFunction(parameter1,\n" 6810 "\t\t\t parameter2);\n" 6811 "\t}\n" 6812 "};", 6813 Tab); 6814 verifyFormat("#define A \\\n" 6815 "\tvoid f() { \\\n" 6816 "\t\tsomeFunction( \\\n" 6817 "\t\t parameter1, \\\n" 6818 "\t\t parameter2); \\\n" 6819 "\t}", 6820 Tab); 6821 EXPECT_EQ("void f() {\n" 6822 "\tf();\n" 6823 "\tg();\n" 6824 "}", 6825 format("void f() {\n" 6826 "\tf();\n" 6827 "\tg();\n" 6828 "}", 6829 0, 0, Tab)); 6830 EXPECT_EQ("void f() {\n" 6831 "\tf();\n" 6832 "\tg();\n" 6833 "}", 6834 format("void f() {\n" 6835 "\tf();\n" 6836 "\tg();\n" 6837 "}", 6838 16, 0, Tab)); 6839 EXPECT_EQ("void f() {\n" 6840 " \tf();\n" 6841 "\tg();\n" 6842 "}", 6843 format("void f() {\n" 6844 " \tf();\n" 6845 " \tg();\n" 6846 "}", 6847 21, 0, Tab)); 6848 6849 Tab.TabWidth = 4; 6850 Tab.IndentWidth = 8; 6851 verifyFormat("class TabWidth4Indent8 {\n" 6852 "\t\tvoid f() {\n" 6853 "\t\t\t\tsomeFunction(parameter1,\n" 6854 "\t\t\t\t\t\t\t parameter2);\n" 6855 "\t\t}\n" 6856 "};", 6857 Tab); 6858 6859 Tab.TabWidth = 4; 6860 Tab.IndentWidth = 4; 6861 verifyFormat("class TabWidth4Indent4 {\n" 6862 "\tvoid f() {\n" 6863 "\t\tsomeFunction(parameter1,\n" 6864 "\t\t\t\t\t parameter2);\n" 6865 "\t}\n" 6866 "};", 6867 Tab); 6868 6869 Tab.TabWidth = 8; 6870 Tab.IndentWidth = 4; 6871 verifyFormat("class TabWidth8Indent4 {\n" 6872 " void f() {\n" 6873 "\tsomeFunction(parameter1,\n" 6874 "\t\t parameter2);\n" 6875 " }\n" 6876 "};", 6877 Tab); 6878 6879 Tab.TabWidth = 8; 6880 Tab.IndentWidth = 8; 6881 EXPECT_EQ("/*\n" 6882 "\t a\t\tcomment\n" 6883 "\t in multiple lines\n" 6884 " */", 6885 format(" /*\t \t \n" 6886 " \t \t a\t\tcomment\t \t\n" 6887 " \t \t in multiple lines\t\n" 6888 " \t */", 6889 Tab)); 6890 6891 Tab.UseTab = FormatStyle::UT_ForIndentation; 6892 verifyFormat("{\n" 6893 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6894 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6895 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6896 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6897 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6898 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6899 "};", 6900 Tab); 6901 verifyFormat("enum A {\n" 6902 "\ta1, // Force multiple lines\n" 6903 "\ta2,\n" 6904 "\ta3\n" 6905 "};", 6906 Tab); 6907 EXPECT_EQ("if (aaaaaaaa && // q\n" 6908 " bb) // w\n" 6909 "\t;", 6910 format("if (aaaaaaaa &&// q\n" 6911 "bb)// w\n" 6912 ";", 6913 Tab)); 6914 verifyFormat("class X {\n" 6915 "\tvoid f() {\n" 6916 "\t\tsomeFunction(parameter1,\n" 6917 "\t\t parameter2);\n" 6918 "\t}\n" 6919 "};", 6920 Tab); 6921 verifyFormat("{\n" 6922 "\tQ({\n" 6923 "\t\t int a;\n" 6924 "\t\t someFunction(aaaaaaaaaa,\n" 6925 "\t\t bbbbbbbbb);\n" 6926 "\t },\n" 6927 "\t p);\n" 6928 "}", 6929 Tab); 6930 EXPECT_EQ("{\n" 6931 "\t/* aaaa\n" 6932 "\t bbbb */\n" 6933 "}", 6934 format("{\n" 6935 "/* aaaa\n" 6936 " bbbb */\n" 6937 "}", 6938 Tab)); 6939 EXPECT_EQ("{\n" 6940 "\t/*\n" 6941 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6942 "\t bbbbbbbbbbbbb\n" 6943 "\t*/\n" 6944 "}", 6945 format("{\n" 6946 "/*\n" 6947 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 6948 "*/\n" 6949 "}", 6950 Tab)); 6951 EXPECT_EQ("{\n" 6952 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6953 "\t// bbbbbbbbbbbbb\n" 6954 "}", 6955 format("{\n" 6956 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 6957 "}", 6958 Tab)); 6959 EXPECT_EQ("{\n" 6960 "\t/*\n" 6961 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6962 "\t bbbbbbbbbbbbb\n" 6963 "\t*/\n" 6964 "}", 6965 format("{\n" 6966 "\t/*\n" 6967 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 6968 "\t*/\n" 6969 "}", 6970 Tab)); 6971 EXPECT_EQ("{\n" 6972 "\t/*\n" 6973 "\n" 6974 "\t*/\n" 6975 "}", 6976 format("{\n" 6977 "\t/*\n" 6978 "\n" 6979 "\t*/\n" 6980 "}", 6981 Tab)); 6982 EXPECT_EQ("{\n" 6983 "\t/*\n" 6984 " asdf\n" 6985 "\t*/\n" 6986 "}", 6987 format("{\n" 6988 "\t/*\n" 6989 " asdf\n" 6990 "\t*/\n" 6991 "}", 6992 Tab)); 6993 6994 Tab.UseTab = FormatStyle::UT_Never; 6995 EXPECT_EQ("/*\n" 6996 " a\t\tcomment\n" 6997 " in multiple lines\n" 6998 " */", 6999 format(" /*\t \t \n" 7000 " \t \t a\t\tcomment\t \t\n" 7001 " \t \t in multiple lines\t\n" 7002 " \t */", 7003 Tab)); 7004 EXPECT_EQ("/* some\n" 7005 " comment */", 7006 format(" \t \t /* some\n" 7007 " \t \t comment */", 7008 Tab)); 7009 EXPECT_EQ("int a; /* some\n" 7010 " comment */", 7011 format(" \t \t int a; /* some\n" 7012 " \t \t comment */", 7013 Tab)); 7014 7015 EXPECT_EQ("int a; /* some\n" 7016 "comment */", 7017 format(" \t \t int\ta; /* some\n" 7018 " \t \t comment */", 7019 Tab)); 7020 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7021 " comment */", 7022 format(" \t \t f(\"\t\t\"); /* some\n" 7023 " \t \t comment */", 7024 Tab)); 7025 EXPECT_EQ("{\n" 7026 " /*\n" 7027 " * Comment\n" 7028 " */\n" 7029 " int i;\n" 7030 "}", 7031 format("{\n" 7032 "\t/*\n" 7033 "\t * Comment\n" 7034 "\t */\n" 7035 "\t int i;\n" 7036 "}")); 7037 } 7038 7039 TEST_F(FormatTest, CalculatesOriginalColumn) { 7040 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7041 "q\"; /* some\n" 7042 " comment */", 7043 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7044 "q\"; /* some\n" 7045 " comment */", 7046 getLLVMStyle())); 7047 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7048 "/* some\n" 7049 " comment */", 7050 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7051 " /* some\n" 7052 " comment */", 7053 getLLVMStyle())); 7054 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7055 "qqq\n" 7056 "/* some\n" 7057 " comment */", 7058 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7059 "qqq\n" 7060 " /* some\n" 7061 " comment */", 7062 getLLVMStyle())); 7063 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7064 "wwww; /* some\n" 7065 " comment */", 7066 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7067 "wwww; /* some\n" 7068 " comment */", 7069 getLLVMStyle())); 7070 } 7071 7072 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 7073 FormatStyle NoSpace = getLLVMStyle(); 7074 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 7075 7076 verifyFormat("while(true)\n" 7077 " continue;", NoSpace); 7078 verifyFormat("for(;;)\n" 7079 " continue;", NoSpace); 7080 verifyFormat("if(true)\n" 7081 " f();\n" 7082 "else if(true)\n" 7083 " f();", NoSpace); 7084 verifyFormat("do {\n" 7085 " do_something();\n" 7086 "} while(something());", NoSpace); 7087 verifyFormat("switch(x) {\n" 7088 "default:\n" 7089 " break;\n" 7090 "}", NoSpace); 7091 7092 FormatStyle Space = getLLVMStyle(); 7093 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 7094 7095 verifyFormat("int f ();", Space); 7096 verifyFormat("void f (int a, T b) {\n" 7097 " while (true)\n" 7098 " continue;\n" 7099 "}", 7100 Space); 7101 verifyFormat("if (true)\n" 7102 " f ();\n" 7103 "else if (true)\n" 7104 " f ();", 7105 Space); 7106 verifyFormat("do {\n" 7107 " do_something ();\n" 7108 "} while (something ());", 7109 Space); 7110 verifyFormat("switch (x) {\n" 7111 "default:\n" 7112 " break;\n" 7113 "}", 7114 Space); 7115 verifyFormat("A::A () : a (1) {}", Space); 7116 verifyFormat("void f () __attribute__ ((asdf));", Space); 7117 verifyFormat("*(&a + 1);\n" 7118 "&((&a)[1]);\n" 7119 "a[(b + c) * d];\n" 7120 "(((a + 1) * 2) + 3) * 4;", 7121 Space); 7122 verifyFormat("#define A(x) x", Space); 7123 verifyFormat("#define A (x) x", Space); 7124 verifyFormat("#if defined(x)\n" 7125 "#endif", 7126 Space); 7127 } 7128 7129 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 7130 FormatStyle Spaces = getLLVMStyle(); 7131 7132 Spaces.SpacesInParentheses = true; 7133 verifyFormat("call( x, y, z );", Spaces); 7134 verifyFormat("while ( (bool)1 )\n" 7135 " continue;", Spaces); 7136 verifyFormat("for ( ;; )\n" 7137 " continue;", Spaces); 7138 verifyFormat("if ( true )\n" 7139 " f();\n" 7140 "else if ( true )\n" 7141 " f();", Spaces); 7142 verifyFormat("do {\n" 7143 " do_something( (int)i );\n" 7144 "} while ( something() );", Spaces); 7145 verifyFormat("switch ( x ) {\n" 7146 "default:\n" 7147 " break;\n" 7148 "}", Spaces); 7149 7150 Spaces.SpacesInParentheses = false; 7151 Spaces.SpacesInCStyleCastParentheses = true; 7152 verifyFormat("Type *A = ( Type * )P;", Spaces); 7153 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 7154 verifyFormat("x = ( int32 )y;", Spaces); 7155 verifyFormat("int a = ( int )(2.0f);", Spaces); 7156 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 7157 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 7158 verifyFormat("#define x (( int )-1)", Spaces); 7159 7160 Spaces.SpacesInParentheses = false; 7161 Spaces.SpaceInEmptyParentheses = true; 7162 verifyFormat("call(x, y, z);", Spaces); 7163 verifyFormat("call( )", Spaces); 7164 7165 // Run the first set of tests again with 7166 // Spaces.SpacesInParentheses = false, 7167 // Spaces.SpaceInEmptyParentheses = true and 7168 // Spaces.SpacesInCStyleCastParentheses = true 7169 Spaces.SpacesInParentheses = false, 7170 Spaces.SpaceInEmptyParentheses = true; 7171 Spaces.SpacesInCStyleCastParentheses = true; 7172 verifyFormat("call(x, y, z);", Spaces); 7173 verifyFormat("while (( bool )1)\n" 7174 " continue;", Spaces); 7175 verifyFormat("for (;;)\n" 7176 " continue;", Spaces); 7177 verifyFormat("if (true)\n" 7178 " f( );\n" 7179 "else if (true)\n" 7180 " f( );", Spaces); 7181 verifyFormat("do {\n" 7182 " do_something(( int )i);\n" 7183 "} while (something( ));", Spaces); 7184 verifyFormat("switch (x) {\n" 7185 "default:\n" 7186 " break;\n" 7187 "}", Spaces); 7188 } 7189 7190 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 7191 verifyFormat("int a = 5;"); 7192 verifyFormat("a += 42;"); 7193 verifyFormat("a or_eq 8;"); 7194 7195 FormatStyle Spaces = getLLVMStyle(); 7196 Spaces.SpaceBeforeAssignmentOperators = false; 7197 verifyFormat("int a= 5;", Spaces); 7198 verifyFormat("a+= 42;", Spaces); 7199 verifyFormat("a or_eq 8;", Spaces); 7200 } 7201 7202 TEST_F(FormatTest, LinuxBraceBreaking) { 7203 FormatStyle BreakBeforeBrace = getLLVMStyle(); 7204 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux; 7205 verifyFormat("namespace a\n" 7206 "{\n" 7207 "class A\n" 7208 "{\n" 7209 " void f()\n" 7210 " {\n" 7211 " if (true) {\n" 7212 " a();\n" 7213 " b();\n" 7214 " }\n" 7215 " }\n" 7216 " void g() { return; }\n" 7217 "}\n" 7218 "}", 7219 BreakBeforeBrace); 7220 } 7221 7222 TEST_F(FormatTest, StroustrupBraceBreaking) { 7223 FormatStyle BreakBeforeBrace = getLLVMStyle(); 7224 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 7225 verifyFormat("namespace a {\n" 7226 "class A {\n" 7227 " void f()\n" 7228 " {\n" 7229 " if (true) {\n" 7230 " a();\n" 7231 " b();\n" 7232 " }\n" 7233 " }\n" 7234 " void g() { return; }\n" 7235 "}\n" 7236 "}", 7237 BreakBeforeBrace); 7238 } 7239 7240 TEST_F(FormatTest, AllmanBraceBreaking) { 7241 FormatStyle BreakBeforeBrace = getLLVMStyle(); 7242 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman; 7243 verifyFormat("namespace a\n" 7244 "{\n" 7245 "class A\n" 7246 "{\n" 7247 " void f()\n" 7248 " {\n" 7249 " if (true)\n" 7250 " {\n" 7251 " a();\n" 7252 " b();\n" 7253 " }\n" 7254 " }\n" 7255 " void g() { return; }\n" 7256 "}\n" 7257 "}", 7258 BreakBeforeBrace); 7259 7260 verifyFormat("void f()\n" 7261 "{\n" 7262 " if (true)\n" 7263 " {\n" 7264 " a();\n" 7265 " }\n" 7266 " else if (false)\n" 7267 " {\n" 7268 " b();\n" 7269 " }\n" 7270 " else\n" 7271 " {\n" 7272 " c();\n" 7273 " }\n" 7274 "}\n", 7275 BreakBeforeBrace); 7276 7277 verifyFormat("void f()\n" 7278 "{\n" 7279 " for (int i = 0; i < 10; ++i)\n" 7280 " {\n" 7281 " a();\n" 7282 " }\n" 7283 " while (false)\n" 7284 " {\n" 7285 " b();\n" 7286 " }\n" 7287 " do\n" 7288 " {\n" 7289 " c();\n" 7290 " } while (false)\n" 7291 "}\n", 7292 BreakBeforeBrace); 7293 7294 verifyFormat("void f(int a)\n" 7295 "{\n" 7296 " switch (a)\n" 7297 " {\n" 7298 " case 0:\n" 7299 " break;\n" 7300 " case 1:\n" 7301 " {\n" 7302 " break;\n" 7303 " }\n" 7304 " case 2:\n" 7305 " {\n" 7306 " }\n" 7307 " break;\n" 7308 " default:\n" 7309 " break;\n" 7310 " }\n" 7311 "}\n", 7312 BreakBeforeBrace); 7313 7314 verifyFormat("enum X\n" 7315 "{\n" 7316 " Y = 0,\n" 7317 "}\n", 7318 BreakBeforeBrace); 7319 7320 BreakBeforeBrace.ColumnLimit = 19; 7321 verifyFormat("void f() { int i; }", BreakBeforeBrace); 7322 BreakBeforeBrace.ColumnLimit = 18; 7323 verifyFormat("void f()\n" 7324 "{\n" 7325 " int i;\n" 7326 "}", 7327 BreakBeforeBrace); 7328 BreakBeforeBrace.ColumnLimit = 80; 7329 7330 FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace; 7331 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 7332 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 7333 verifyFormat("void f(bool b)\n" 7334 "{\n" 7335 " if (b)\n" 7336 " {\n" 7337 " return;\n" 7338 " }\n" 7339 "}\n", 7340 BreakBeforeBraceShortIfs); 7341 verifyFormat("void f(bool b)\n" 7342 "{\n" 7343 " if (b) return;\n" 7344 "}\n", 7345 BreakBeforeBraceShortIfs); 7346 verifyFormat("void f(bool b)\n" 7347 "{\n" 7348 " while (b)\n" 7349 " {\n" 7350 " return;\n" 7351 " }\n" 7352 "}\n", 7353 BreakBeforeBraceShortIfs); 7354 } 7355 7356 TEST_F(FormatTest, GNUBraceBreaking) { 7357 FormatStyle GNUBraceStyle = getLLVMStyle(); 7358 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 7359 verifyFormat("namespace a\n" 7360 "{\n" 7361 "class A\n" 7362 "{\n" 7363 " void f()\n" 7364 " {\n" 7365 " int a;\n" 7366 " {\n" 7367 " int b;\n" 7368 " }\n" 7369 " if (true)\n" 7370 " {\n" 7371 " a();\n" 7372 " b();\n" 7373 " }\n" 7374 " }\n" 7375 " void g() { return; }\n" 7376 "}\n" 7377 "}", 7378 GNUBraceStyle); 7379 7380 verifyFormat("void f()\n" 7381 "{\n" 7382 " if (true)\n" 7383 " {\n" 7384 " a();\n" 7385 " }\n" 7386 " else if (false)\n" 7387 " {\n" 7388 " b();\n" 7389 " }\n" 7390 " else\n" 7391 " {\n" 7392 " c();\n" 7393 " }\n" 7394 "}\n", 7395 GNUBraceStyle); 7396 7397 verifyFormat("void f()\n" 7398 "{\n" 7399 " for (int i = 0; i < 10; ++i)\n" 7400 " {\n" 7401 " a();\n" 7402 " }\n" 7403 " while (false)\n" 7404 " {\n" 7405 " b();\n" 7406 " }\n" 7407 " do\n" 7408 " {\n" 7409 " c();\n" 7410 " }\n" 7411 " while (false);\n" 7412 "}\n", 7413 GNUBraceStyle); 7414 7415 verifyFormat("void f(int a)\n" 7416 "{\n" 7417 " switch (a)\n" 7418 " {\n" 7419 " case 0:\n" 7420 " break;\n" 7421 " case 1:\n" 7422 " {\n" 7423 " break;\n" 7424 " }\n" 7425 " case 2:\n" 7426 " {\n" 7427 " }\n" 7428 " break;\n" 7429 " default:\n" 7430 " break;\n" 7431 " }\n" 7432 "}\n", 7433 GNUBraceStyle); 7434 7435 verifyFormat("enum X\n" 7436 "{\n" 7437 " Y = 0,\n" 7438 "}\n", 7439 GNUBraceStyle); 7440 } 7441 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 7442 verifyFormat("void f() {\n" 7443 " try {\n" 7444 " }\n" 7445 " catch (const Exception &e) {\n" 7446 " }\n" 7447 "}\n", 7448 getLLVMStyle()); 7449 } 7450 7451 TEST_F(FormatTest, UnderstandsPragmas) { 7452 verifyFormat("#pragma omp reduction(| : var)"); 7453 verifyFormat("#pragma omp reduction(+ : var)"); 7454 7455 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 7456 "(including parentheses).", 7457 format("#pragma mark Any non-hyphenated or hyphenated string " 7458 "(including parentheses).")); 7459 } 7460 7461 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 7462 for (size_t i = 1; i < Styles.size(); ++i) \ 7463 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \ 7464 << Styles.size() \ 7465 << " differs from Style #0" 7466 7467 TEST_F(FormatTest, GetsPredefinedStyleByName) { 7468 SmallVector<FormatStyle, 3> Styles; 7469 Styles.resize(3); 7470 7471 Styles[0] = getLLVMStyle(); 7472 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 7473 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 7474 EXPECT_ALL_STYLES_EQUAL(Styles); 7475 7476 Styles[0] = getGoogleStyle(); 7477 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 7478 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 7479 EXPECT_ALL_STYLES_EQUAL(Styles); 7480 7481 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 7482 EXPECT_TRUE( 7483 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 7484 EXPECT_TRUE( 7485 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 7486 EXPECT_ALL_STYLES_EQUAL(Styles); 7487 7488 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 7489 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 7490 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 7491 EXPECT_ALL_STYLES_EQUAL(Styles); 7492 7493 Styles[0] = getMozillaStyle(); 7494 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 7495 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 7496 EXPECT_ALL_STYLES_EQUAL(Styles); 7497 7498 Styles[0] = getWebKitStyle(); 7499 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 7500 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 7501 EXPECT_ALL_STYLES_EQUAL(Styles); 7502 7503 Styles[0] = getGNUStyle(); 7504 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 7505 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 7506 EXPECT_ALL_STYLES_EQUAL(Styles); 7507 7508 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 7509 } 7510 7511 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 7512 SmallVector<FormatStyle, 8> Styles; 7513 Styles.resize(2); 7514 7515 Styles[0] = getGoogleStyle(); 7516 Styles[1] = getLLVMStyle(); 7517 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 7518 EXPECT_ALL_STYLES_EQUAL(Styles); 7519 7520 Styles.resize(5); 7521 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 7522 Styles[1] = getLLVMStyle(); 7523 Styles[1].Language = FormatStyle::LK_JavaScript; 7524 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 7525 7526 Styles[2] = getLLVMStyle(); 7527 Styles[2].Language = FormatStyle::LK_JavaScript; 7528 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 7529 "BasedOnStyle: Google", 7530 &Styles[2]).value()); 7531 7532 Styles[3] = getLLVMStyle(); 7533 Styles[3].Language = FormatStyle::LK_JavaScript; 7534 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 7535 "Language: JavaScript", 7536 &Styles[3]).value()); 7537 7538 Styles[4] = getLLVMStyle(); 7539 Styles[4].Language = FormatStyle::LK_JavaScript; 7540 EXPECT_EQ(0, parseConfiguration("---\n" 7541 "BasedOnStyle: LLVM\n" 7542 "IndentWidth: 123\n" 7543 "---\n" 7544 "BasedOnStyle: Google\n" 7545 "Language: JavaScript", 7546 &Styles[4]).value()); 7547 EXPECT_ALL_STYLES_EQUAL(Styles); 7548 } 7549 7550 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 7551 EXPECT_NE(VALUE, Style.FIELD); \ 7552 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 7553 EXPECT_EQ(VALUE, Style.FIELD) 7554 7555 #define CHECK_PARSE_BOOL(FIELD) \ 7556 Style.FIELD = false; \ 7557 EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \ 7558 EXPECT_TRUE(Style.FIELD); \ 7559 EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \ 7560 EXPECT_FALSE(Style.FIELD); 7561 7562 TEST_F(FormatTest, ParsesConfiguration) { 7563 FormatStyle Style = {}; 7564 Style.Language = FormatStyle::LK_Cpp; 7565 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 7566 CHECK_PARSE_BOOL(AlignTrailingComments); 7567 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 7568 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 7569 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 7570 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 7571 CHECK_PARSE_BOOL(BinPackParameters); 7572 CHECK_PARSE_BOOL(BreakBeforeBinaryOperators); 7573 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 7574 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 7575 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 7576 CHECK_PARSE_BOOL(DerivePointerBinding); 7577 CHECK_PARSE_BOOL(IndentCaseLabels); 7578 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 7579 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 7580 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 7581 CHECK_PARSE_BOOL(PointerBindsToType); 7582 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 7583 CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType); 7584 CHECK_PARSE_BOOL(SpacesInParentheses); 7585 CHECK_PARSE_BOOL(SpacesInAngles); 7586 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 7587 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 7588 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 7589 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 7590 7591 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 7592 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 7593 ConstructorInitializerIndentWidth, 1234u); 7594 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 7595 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 7596 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 7597 PenaltyBreakBeforeFirstCallParameter, 1234u); 7598 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 7599 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 7600 PenaltyReturnTypeOnItsOwnLine, 1234u); 7601 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 7602 SpacesBeforeTrailingComments, 1234u); 7603 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 7604 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 7605 7606 Style.Standard = FormatStyle::LS_Auto; 7607 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 7608 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 7609 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 7610 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 7611 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 7612 7613 Style.UseTab = FormatStyle::UT_ForIndentation; 7614 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 7615 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 7616 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 7617 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 7618 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 7619 7620 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7621 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 7622 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 7623 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 7624 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 7625 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 7626 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 7627 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 7628 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 7629 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 7630 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 7631 7632 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 7633 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 7634 FormatStyle::SBPO_Never); 7635 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 7636 FormatStyle::SBPO_Always); 7637 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 7638 FormatStyle::SBPO_ControlStatements); 7639 // For backward compatibility: 7640 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 7641 FormatStyle::SBPO_Never); 7642 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 7643 FormatStyle::SBPO_ControlStatements); 7644 7645 Style.ColumnLimit = 123; 7646 FormatStyle BaseStyle = getLLVMStyle(); 7647 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 7648 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 7649 7650 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 7651 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 7652 FormatStyle::BS_Attach); 7653 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 7654 FormatStyle::BS_Linux); 7655 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 7656 FormatStyle::BS_Stroustrup); 7657 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 7658 FormatStyle::BS_Allman); 7659 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 7660 7661 Style.NamespaceIndentation = FormatStyle::NI_All; 7662 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 7663 FormatStyle::NI_None); 7664 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 7665 FormatStyle::NI_Inner); 7666 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 7667 FormatStyle::NI_All); 7668 7669 Style.ForEachMacros.clear(); 7670 std::vector<std::string> BoostForeach; 7671 BoostForeach.push_back("BOOST_FOREACH"); 7672 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 7673 std::vector<std::string> BoostAndQForeach; 7674 BoostAndQForeach.push_back("BOOST_FOREACH"); 7675 BoostAndQForeach.push_back("Q_FOREACH"); 7676 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 7677 BoostAndQForeach); 7678 } 7679 7680 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 7681 FormatStyle Style = {}; 7682 Style.Language = FormatStyle::LK_Cpp; 7683 CHECK_PARSE("Language: Cpp\n" 7684 "IndentWidth: 12", 7685 IndentWidth, 12u); 7686 EXPECT_EQ(llvm::errc::not_supported, 7687 parseConfiguration("Language: JavaScript\n" 7688 "IndentWidth: 34", 7689 &Style)); 7690 EXPECT_EQ(12u, Style.IndentWidth); 7691 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 7692 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 7693 7694 Style.Language = FormatStyle::LK_JavaScript; 7695 CHECK_PARSE("Language: JavaScript\n" 7696 "IndentWidth: 12", 7697 IndentWidth, 12u); 7698 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 7699 EXPECT_EQ(llvm::errc::not_supported, parseConfiguration("Language: Cpp\n" 7700 "IndentWidth: 34", 7701 &Style)); 7702 EXPECT_EQ(23u, Style.IndentWidth); 7703 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 7704 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 7705 7706 CHECK_PARSE("BasedOnStyle: LLVM\n" 7707 "IndentWidth: 67", 7708 IndentWidth, 67u); 7709 7710 CHECK_PARSE("---\n" 7711 "Language: JavaScript\n" 7712 "IndentWidth: 12\n" 7713 "---\n" 7714 "Language: Cpp\n" 7715 "IndentWidth: 34\n" 7716 "...\n", 7717 IndentWidth, 12u); 7718 7719 Style.Language = FormatStyle::LK_Cpp; 7720 CHECK_PARSE("---\n" 7721 "Language: JavaScript\n" 7722 "IndentWidth: 12\n" 7723 "---\n" 7724 "Language: Cpp\n" 7725 "IndentWidth: 34\n" 7726 "...\n", 7727 IndentWidth, 34u); 7728 CHECK_PARSE("---\n" 7729 "IndentWidth: 78\n" 7730 "---\n" 7731 "Language: JavaScript\n" 7732 "IndentWidth: 56\n" 7733 "...\n", 7734 IndentWidth, 78u); 7735 7736 Style.ColumnLimit = 123; 7737 Style.IndentWidth = 234; 7738 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 7739 Style.TabWidth = 345; 7740 EXPECT_EQ(llvm::errc::success, 7741 parseConfiguration("---\n" 7742 "IndentWidth: 456\n" 7743 "BreakBeforeBraces: Allman\n" 7744 "---\n" 7745 "Language: JavaScript\n" 7746 "IndentWidth: 111\n" 7747 "TabWidth: 111\n" 7748 "---\n" 7749 "Language: Cpp\n" 7750 "BreakBeforeBraces: Stroustrup\n" 7751 "TabWidth: 789\n" 7752 "...\n", 7753 &Style)); 7754 EXPECT_EQ(123u, Style.ColumnLimit); 7755 EXPECT_EQ(456u, Style.IndentWidth); 7756 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 7757 EXPECT_EQ(789u, Style.TabWidth); 7758 7759 7760 EXPECT_EQ(llvm::errc::invalid_argument, 7761 parseConfiguration("---\n" 7762 "Language: JavaScript\n" 7763 "IndentWidth: 56\n" 7764 "---\n" 7765 "IndentWidth: 78\n" 7766 "...\n", 7767 &Style)); 7768 EXPECT_EQ(llvm::errc::invalid_argument, 7769 parseConfiguration("---\n" 7770 "Language: JavaScript\n" 7771 "IndentWidth: 56\n" 7772 "---\n" 7773 "Language: JavaScript\n" 7774 "IndentWidth: 78\n" 7775 "...\n", 7776 &Style)); 7777 7778 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 7779 } 7780 7781 #undef CHECK_PARSE 7782 #undef CHECK_PARSE_BOOL 7783 7784 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 7785 FormatStyle Style = {}; 7786 Style.Language = FormatStyle::LK_JavaScript; 7787 Style.BreakBeforeTernaryOperators = true; 7788 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 7789 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 7790 7791 Style.BreakBeforeTernaryOperators = true; 7792 EXPECT_EQ(0, parseConfiguration("---\n" 7793 "BasedOnStyle: Google\n" 7794 "---\n" 7795 "Language: JavaScript\n" 7796 "IndentWidth: 76\n" 7797 "...\n", &Style).value()); 7798 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 7799 EXPECT_EQ(76u, Style.IndentWidth); 7800 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 7801 } 7802 7803 TEST_F(FormatTest, ConfigurationRoundTripTest) { 7804 FormatStyle Style = getLLVMStyle(); 7805 std::string YAML = configurationAsText(Style); 7806 FormatStyle ParsedStyle = {}; 7807 ParsedStyle.Language = FormatStyle::LK_Cpp; 7808 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 7809 EXPECT_EQ(Style, ParsedStyle); 7810 } 7811 7812 TEST_F(FormatTest, WorksFor8bitEncodings) { 7813 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 7814 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 7815 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 7816 "\"\xef\xee\xf0\xf3...\"", 7817 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 7818 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 7819 "\xef\xee\xf0\xf3...\"", 7820 getLLVMStyleWithColumns(12))); 7821 } 7822 7823 TEST_F(FormatTest, HandlesUTF8BOM) { 7824 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 7825 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 7826 format("\xef\xbb\xbf#include <iostream>")); 7827 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 7828 format("\xef\xbb\xbf\n#include <iostream>")); 7829 } 7830 7831 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 7832 #if !defined(_MSC_VER) 7833 7834 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 7835 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 7836 getLLVMStyleWithColumns(35)); 7837 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 7838 getLLVMStyleWithColumns(31)); 7839 verifyFormat("// Однажды в студёную зимнюю пору...", 7840 getLLVMStyleWithColumns(36)); 7841 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", 7842 getLLVMStyleWithColumns(32)); 7843 verifyFormat("/* Однажды в студёную зимнюю пору... */", 7844 getLLVMStyleWithColumns(39)); 7845 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 7846 getLLVMStyleWithColumns(35)); 7847 } 7848 7849 TEST_F(FormatTest, SplitsUTF8Strings) { 7850 // Non-printable characters' width is currently considered to be the length in 7851 // bytes in UTF8. The characters can be displayed in very different manner 7852 // (zero-width, single width with a substitution glyph, expanded to their code 7853 // (e.g. "<8d>"), so there's no single correct way to handle them. 7854 EXPECT_EQ("\"aaaaÄ\"\n" 7855 "\"\xc2\x8d\";", 7856 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 7857 EXPECT_EQ("\"aaaaaaaÄ\"\n" 7858 "\"\xc2\x8d\";", 7859 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 7860 EXPECT_EQ( 7861 "\"Однажды, в \"\n" 7862 "\"студёную \"\n" 7863 "\"зимнюю \"\n" 7864 "\"пору,\"", 7865 format("\"Однажды, в студёную зимнюю пору,\"", 7866 getLLVMStyleWithColumns(13))); 7867 EXPECT_EQ("\"一 二 三 \"\n" 7868 "\"四 五六 \"\n" 7869 "\"七 八 九 \"\n" 7870 "\"十\"", 7871 format("\"一 二 三 四 五六 七 八 九 十\"", 7872 getLLVMStyleWithColumns(11))); 7873 EXPECT_EQ("\"一\t二 \"\n" 7874 "\"\t三 \"\n" 7875 "\"四 五\t六 \"\n" 7876 "\"\t七 \"\n" 7877 "\"八九十\tqq\"", 7878 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 7879 getLLVMStyleWithColumns(11))); 7880 } 7881 7882 7883 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 7884 EXPECT_EQ("const char *sssss =\n" 7885 " \"一二三四五六七八\\\n" 7886 " 九 十\";", 7887 format("const char *sssss = \"一二三四五六七八\\\n" 7888 " 九 十\";", 7889 getLLVMStyleWithColumns(30))); 7890 } 7891 7892 TEST_F(FormatTest, SplitsUTF8LineComments) { 7893 EXPECT_EQ("// aaaaÄ\xc2\x8d", 7894 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 7895 EXPECT_EQ("// Я из лесу\n" 7896 "// вышел; был\n" 7897 "// сильный\n" 7898 "// мороз.", 7899 format("// Я из лесу вышел; был сильный мороз.", 7900 getLLVMStyleWithColumns(13))); 7901 EXPECT_EQ("// 一二三\n" 7902 "// 四五六七\n" 7903 "// 八 九\n" 7904 "// 十", 7905 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 7906 } 7907 7908 TEST_F(FormatTest, SplitsUTF8BlockComments) { 7909 EXPECT_EQ("/* Гляжу,\n" 7910 " * поднимается\n" 7911 " * медленно в\n" 7912 " * гору\n" 7913 " * Лошадка,\n" 7914 " * везущая\n" 7915 " * хворосту\n" 7916 " * воз. */", 7917 format("/* Гляжу, поднимается медленно в гору\n" 7918 " * Лошадка, везущая хворосту воз. */", 7919 getLLVMStyleWithColumns(13))); 7920 EXPECT_EQ( 7921 "/* 一二三\n" 7922 " * 四五六七\n" 7923 " * 八 九\n" 7924 " * 十 */", 7925 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 7926 EXPECT_EQ("/* \n" 7927 " * \n" 7928 " * - */", 7929 format("/* - */", getLLVMStyleWithColumns(12))); 7930 } 7931 7932 #endif // _MSC_VER 7933 7934 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 7935 FormatStyle Style = getLLVMStyle(); 7936 7937 Style.ConstructorInitializerIndentWidth = 4; 7938 verifyFormat( 7939 "SomeClass::Constructor()\n" 7940 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7941 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7942 Style); 7943 7944 Style.ConstructorInitializerIndentWidth = 2; 7945 verifyFormat( 7946 "SomeClass::Constructor()\n" 7947 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7948 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7949 Style); 7950 7951 Style.ConstructorInitializerIndentWidth = 0; 7952 verifyFormat( 7953 "SomeClass::Constructor()\n" 7954 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7955 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7956 Style); 7957 7958 Style.BreakConstructorInitializersBeforeComma = true; 7959 Style.ConstructorInitializerIndentWidth = 4; 7960 verifyFormat("SomeClass::Constructor()\n" 7961 " : a(a)\n" 7962 " , b(b)\n" 7963 " , c(c) {}", 7964 Style); 7965 verifyFormat("SomeClass::Constructor()\n" 7966 " : a(a) {}", 7967 Style); 7968 7969 Style.ColumnLimit = 0; 7970 verifyFormat("SomeClass::Constructor()\n" 7971 " : a(a) {}", 7972 Style); 7973 verifyFormat("SomeClass::Constructor()\n" 7974 " : a(a)\n" 7975 " , b(b)\n" 7976 " , c(c) {}", 7977 Style); 7978 verifyFormat("SomeClass::Constructor()\n" 7979 " : a(a) {\n" 7980 " foo();\n" 7981 " bar();\n" 7982 "}", 7983 Style); 7984 7985 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7986 verifyFormat("SomeClass::Constructor()\n" 7987 " : a(a)\n" 7988 " , b(b)\n" 7989 " , c(c) {\n}", 7990 Style); 7991 verifyFormat("SomeClass::Constructor()\n" 7992 " : a(a) {\n}", 7993 Style); 7994 7995 Style.ColumnLimit = 80; 7996 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7997 Style.ConstructorInitializerIndentWidth = 2; 7998 verifyFormat("SomeClass::Constructor()\n" 7999 " : a(a)\n" 8000 " , b(b)\n" 8001 " , c(c) {}", 8002 Style); 8003 8004 Style.ConstructorInitializerIndentWidth = 0; 8005 verifyFormat("SomeClass::Constructor()\n" 8006 ": a(a)\n" 8007 ", b(b)\n" 8008 ", c(c) {}", 8009 Style); 8010 8011 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 8012 Style.ConstructorInitializerIndentWidth = 4; 8013 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 8014 verifyFormat( 8015 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 8016 Style); 8017 verifyFormat( 8018 "SomeClass::Constructor()\n" 8019 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 8020 Style); 8021 Style.ConstructorInitializerIndentWidth = 4; 8022 Style.ColumnLimit = 60; 8023 verifyFormat("SomeClass::Constructor()\n" 8024 " : aaaaaaaa(aaaaaaaa)\n" 8025 " , aaaaaaaa(aaaaaaaa)\n" 8026 " , aaaaaaaa(aaaaaaaa) {}", 8027 Style); 8028 } 8029 8030 TEST_F(FormatTest, FormatsWithWebKitStyle) { 8031 FormatStyle Style = getWebKitStyle(); 8032 8033 // Don't indent in outer namespaces. 8034 verifyFormat("namespace outer {\n" 8035 "int i;\n" 8036 "namespace inner {\n" 8037 " int i;\n" 8038 "} // namespace inner\n" 8039 "} // namespace outer\n" 8040 "namespace other_outer {\n" 8041 "int i;\n" 8042 "}", 8043 Style); 8044 8045 // Don't indent case labels. 8046 verifyFormat("switch (variable) {\n" 8047 "case 1:\n" 8048 "case 2:\n" 8049 " doSomething();\n" 8050 " break;\n" 8051 "default:\n" 8052 " ++variable;\n" 8053 "}", 8054 Style); 8055 8056 // Wrap before binary operators. 8057 EXPECT_EQ( 8058 "void f()\n" 8059 "{\n" 8060 " if (aaaaaaaaaaaaaaaa\n" 8061 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 8062 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 8063 " return;\n" 8064 "}", 8065 format( 8066 "void f() {\n" 8067 "if (aaaaaaaaaaaaaaaa\n" 8068 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 8069 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 8070 "return;\n" 8071 "}", 8072 Style)); 8073 8074 // Constructor initializers are formatted one per line with the "," on the 8075 // new line. 8076 verifyFormat("Constructor()\n" 8077 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8078 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 8079 " aaaaaaaaaaaaaa)\n" 8080 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 8081 "{\n" 8082 "}", 8083 Style); 8084 verifyFormat("SomeClass::Constructor()\n" 8085 " : a(a)\n" 8086 "{\n" 8087 "}", 8088 Style); 8089 EXPECT_EQ("SomeClass::Constructor()\n" 8090 " : a(a)\n" 8091 "{\n" 8092 "}", 8093 format("SomeClass::Constructor():a(a){}", Style)); 8094 verifyFormat("SomeClass::Constructor()\n" 8095 " : a(a)\n" 8096 " , b(b)\n" 8097 " , c(c)\n" 8098 "{\n" 8099 "}", Style); 8100 verifyFormat("SomeClass::Constructor()\n" 8101 " : a(a)\n" 8102 "{\n" 8103 " foo();\n" 8104 " bar();\n" 8105 "}", 8106 Style); 8107 8108 // Access specifiers should be aligned left. 8109 verifyFormat("class C {\n" 8110 "public:\n" 8111 " int i;\n" 8112 "};", 8113 Style); 8114 8115 // Do not align comments. 8116 verifyFormat("int a; // Do not\n" 8117 "double b; // align comments.", 8118 Style); 8119 8120 // Accept input's line breaks. 8121 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 8122 " || bbbbbbbbbbbbbbb) {\n" 8123 " i++;\n" 8124 "}", 8125 format("if (aaaaaaaaaaaaaaa\n" 8126 "|| bbbbbbbbbbbbbbb) { i++; }", 8127 Style)); 8128 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 8129 " i++;\n" 8130 "}", 8131 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 8132 8133 // Don't automatically break all macro definitions (llvm.org/PR17842). 8134 verifyFormat("#define aNumber 10", Style); 8135 // However, generally keep the line breaks that the user authored. 8136 EXPECT_EQ("#define aNumber \\\n" 8137 " 10", 8138 format("#define aNumber \\\n" 8139 " 10", 8140 Style)); 8141 } 8142 8143 TEST_F(FormatTest, FormatsLambdas) { 8144 verifyFormat("int c = [b]() mutable {\n" 8145 " return [&b] { return b++; }();\n" 8146 "}();\n"); 8147 verifyFormat("int c = [&] {\n" 8148 " [=] { return b++; }();\n" 8149 "}();\n"); 8150 verifyFormat("int c = [&, &a, a] {\n" 8151 " [=, c, &d] { return b++; }();\n" 8152 "}();\n"); 8153 verifyFormat("int c = [&a, &a, a] {\n" 8154 " [=, a, b, &c] { return b++; }();\n" 8155 "}();\n"); 8156 verifyFormat("auto c = {[&a, &a, a] {\n" 8157 " [=, a, b, &c] { return b++; }();\n" 8158 "}}\n"); 8159 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 8160 verifyFormat("void f() {\n" 8161 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 8162 "}\n"); 8163 verifyFormat("void f() {\n" 8164 " other(x.begin(), //\n" 8165 " x.end(), //\n" 8166 " [&](int, int) { return 1; });\n" 8167 "}\n"); 8168 verifyFormat("SomeFunction([]() { // A cool function...\n" 8169 " return 43;\n" 8170 "});"); 8171 8172 // Lambdas with return types. 8173 verifyFormat("int c = []() -> int { return 2; }();\n"); 8174 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 8175 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 8176 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 8177 " int j) -> int {\n" 8178 " return fffffffffffffffffffffffffffffffffffffff(i * j);\n" 8179 "};"); 8180 8181 // Multiple lambdas in the same parentheses change indentation rules. 8182 verifyFormat("SomeFunction([]() {\n" 8183 " int i = 42;\n" 8184 " return i;\n" 8185 " },\n" 8186 " []() {\n" 8187 " int j = 43;\n" 8188 " return j;\n" 8189 " });"); 8190 8191 // Not lambdas. 8192 verifyFormat("constexpr char hello[]{\"hello\"};"); 8193 verifyFormat("double &operator[](int i) { return 0; }\n" 8194 "int i;"); 8195 verifyFormat("std::unique_ptr<int[]> foo() {}"); 8196 verifyFormat("int i = a[a][a]->f();"); 8197 verifyFormat("int i = (*b)[a]->f();"); 8198 8199 // Other corner cases. 8200 verifyFormat("void f() {\n" 8201 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 8202 " );\n" 8203 "}"); 8204 8205 // Lambdas created through weird macros. 8206 verifyFormat("void f() {\n" 8207 " MACRO((const AA &a) { return 1; });\n" 8208 "}"); 8209 } 8210 8211 TEST_F(FormatTest, FormatsBlocks) { 8212 verifyFormat("int (^Block)(int, int);"); 8213 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 8214 verifyFormat("void (^block)(int) = ^(id test) { int i; };"); 8215 verifyFormat("void (^block)(int) = ^(int test) { int i; };"); 8216 verifyFormat("void (^block)(int) = ^id(int test) { int i; };"); 8217 verifyFormat("void (^block)(int) = ^int(int test) { int i; };"); 8218 8219 verifyFormat("foo(^{ bar(); });"); 8220 verifyFormat("foo(a, ^{ bar(); });"); 8221 8222 // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how 8223 // it would ideally look. 8224 verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];"); 8225 verifyFormat("int i = {[operation setCompletionBlock : ^{ [self " 8226 "onOperationDone]; }]};"); 8227 verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];"); 8228 verifyFormat("int a = [operation block:^int(int *i) { return 1; }];"); 8229 verifyFormat("[myObject doSomethingWith:arg1\n" 8230 " aaa:^int(int *a) { return 1; }\n" 8231 " bbb:f(a * b)];"); 8232 8233 verifyFormat("[operation setCompletionBlock:^{\n" 8234 " [self.delegate newDataAvailable];\n" 8235 "}];", 8236 getLLVMStyleWithColumns(60)); 8237 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 8238 " NSString *path = [self sessionFilePath];\n" 8239 " if (path) {\n" 8240 " // ...\n" 8241 " }\n" 8242 "});"); 8243 verifyFormat("[[SessionService sharedService]\n" 8244 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 8245 " if (window) {\n" 8246 " [self windowDidLoad:window];\n" 8247 " } else {\n" 8248 " [self errorLoadingWindow];\n" 8249 " }\n" 8250 " }];"); 8251 verifyFormat("void (^largeBlock)(void) = ^{\n" 8252 " // ...\n" 8253 "};\n", 8254 getLLVMStyleWithColumns(40)); 8255 verifyFormat("[[SessionService sharedService]\n" 8256 " loadWindowWithCompletionBlock: //\n" 8257 " ^(SessionWindow *window) {\n" 8258 " if (window) {\n" 8259 " [self windowDidLoad:window];\n" 8260 " } else {\n" 8261 " [self errorLoadingWindow];\n" 8262 " }\n" 8263 " }];", 8264 getLLVMStyleWithColumns(60)); 8265 verifyFormat("[myObject doSomethingWith:arg1\n" 8266 " firstBlock:^(Foo *a) {\n" 8267 " // ...\n" 8268 " int i;\n" 8269 " }\n" 8270 " secondBlock:^(Bar *b) {\n" 8271 " // ...\n" 8272 " int i;\n" 8273 " }\n" 8274 " thirdBlock:^Foo(Bar *b) {\n" 8275 " // ...\n" 8276 " int i;\n" 8277 " }];"); 8278 verifyFormat("[myObject doSomethingWith:arg1\n" 8279 " firstBlock:-1\n" 8280 " secondBlock:^(Bar *b) {\n" 8281 " // ...\n" 8282 " int i;\n" 8283 " }];"); 8284 8285 verifyFormat("f(^{\n" 8286 " @autoreleasepool {\n" 8287 " if (a) {\n" 8288 " g();\n" 8289 " }\n" 8290 " }\n" 8291 "});"); 8292 } 8293 8294 TEST_F(FormatTest, SupportsCRLF) { 8295 EXPECT_EQ("int a;\r\n" 8296 "int b;\r\n" 8297 "int c;\r\n", 8298 format("int a;\r\n" 8299 " int b;\r\n" 8300 " int c;\r\n", 8301 getLLVMStyle())); 8302 EXPECT_EQ("int a;\r\n" 8303 "int b;\r\n" 8304 "int c;\r\n", 8305 format("int a;\r\n" 8306 " int b;\n" 8307 " int c;\r\n", 8308 getLLVMStyle())); 8309 EXPECT_EQ("int a;\n" 8310 "int b;\n" 8311 "int c;\n", 8312 format("int a;\r\n" 8313 " int b;\n" 8314 " int c;\n", 8315 getLLVMStyle())); 8316 EXPECT_EQ("\"aaaaaaa \"\r\n" 8317 "\"bbbbbbb\";\r\n", 8318 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 8319 EXPECT_EQ("#define A \\\r\n" 8320 " b; \\\r\n" 8321 " c; \\\r\n" 8322 " d;\r\n", 8323 format("#define A \\\r\n" 8324 " b; \\\r\n" 8325 " c; d; \r\n", 8326 getGoogleStyle())); 8327 8328 EXPECT_EQ("/*\r\n" 8329 "multi line block comments\r\n" 8330 "should not introduce\r\n" 8331 "an extra carriage return\r\n" 8332 "*/\r\n", 8333 format("/*\r\n" 8334 "multi line block comments\r\n" 8335 "should not introduce\r\n" 8336 "an extra carriage return\r\n" 8337 "*/\r\n")); 8338 } 8339 8340 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 8341 verifyFormat("MY_CLASS(C) {\n" 8342 " int i;\n" 8343 " int j;\n" 8344 "};"); 8345 } 8346 8347 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 8348 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 8349 TwoIndent.ContinuationIndentWidth = 2; 8350 8351 EXPECT_EQ("int i =\n" 8352 " longFunction(\n" 8353 " arg);", 8354 format("int i = longFunction(arg);", TwoIndent)); 8355 8356 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 8357 SixIndent.ContinuationIndentWidth = 6; 8358 8359 EXPECT_EQ("int i =\n" 8360 " longFunction(\n" 8361 " arg);", 8362 format("int i = longFunction(arg);", SixIndent)); 8363 } 8364 8365 TEST_F(FormatTest, SpacesInAngles) { 8366 FormatStyle Spaces = getLLVMStyle(); 8367 Spaces.SpacesInAngles = true; 8368 8369 verifyFormat("static_cast< int >(arg);", Spaces); 8370 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 8371 verifyFormat("f< int, float >();", Spaces); 8372 verifyFormat("template <> g() {}", Spaces); 8373 verifyFormat("template < std::vector< int > > f() {}", Spaces); 8374 8375 Spaces.Standard = FormatStyle::LS_Cpp03; 8376 Spaces.SpacesInAngles = true; 8377 verifyFormat("A< A< int > >();", Spaces); 8378 8379 Spaces.SpacesInAngles = false; 8380 verifyFormat("A<A<int> >();", Spaces); 8381 8382 Spaces.Standard = FormatStyle::LS_Cpp11; 8383 Spaces.SpacesInAngles = true; 8384 verifyFormat("A< A< int > >();", Spaces); 8385 8386 Spaces.SpacesInAngles = false; 8387 verifyFormat("A<A<int>>();", Spaces); 8388 } 8389 8390 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 8391 std::string code = "#if A\n" 8392 "#if B\n" 8393 "a.\n" 8394 "#endif\n" 8395 " a = 1;\n" 8396 "#else\n" 8397 "#endif\n" 8398 "#if C\n" 8399 "#else\n" 8400 "#endif\n"; 8401 EXPECT_EQ(code, format(code)); 8402 } 8403 8404 } // end namespace tooling 8405 } // end namespace clang 8406