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