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