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