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