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 "clang/Format/Format.h" 11 12 #include "../Tooling/RewriterTestContext.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 namespace clang { 22 namespace format { 23 namespace { 24 25 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 26 27 class FormatTest : public ::testing::Test { 28 protected: 29 enum IncompleteCheck { 30 IC_ExpectComplete, 31 IC_ExpectIncomplete, 32 IC_DoNotCheck 33 }; 34 35 std::string format(llvm::StringRef Code, 36 const FormatStyle &Style = getLLVMStyle(), 37 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 38 DEBUG(llvm::errs() << "---\n"); 39 DEBUG(llvm::errs() << Code << "\n\n"); 40 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 41 bool IncompleteFormat = false; 42 tooling::Replacements Replaces = 43 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 44 if (CheckIncomplete != IC_DoNotCheck) { 45 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 46 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 std::string Result = applyAllReplacements(Code, Replaces); 50 EXPECT_NE("", Result); 51 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 52 return Result; 53 } 54 55 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 56 FormatStyle Style = getLLVMStyle(); 57 Style.ColumnLimit = ColumnLimit; 58 return Style; 59 } 60 61 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 62 FormatStyle Style = getGoogleStyle(); 63 Style.ColumnLimit = ColumnLimit; 64 return Style; 65 } 66 67 void verifyFormat(llvm::StringRef Code, 68 const FormatStyle &Style = getLLVMStyle()) { 69 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 70 } 71 72 void verifyIncompleteFormat(llvm::StringRef Code, 73 const FormatStyle &Style = getLLVMStyle()) { 74 EXPECT_EQ(Code.str(), 75 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 76 } 77 78 void verifyGoogleFormat(llvm::StringRef Code) { 79 verifyFormat(Code, getGoogleStyle()); 80 } 81 82 void verifyIndependentOfContext(llvm::StringRef text) { 83 verifyFormat(text); 84 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 85 } 86 87 /// \brief Verify that clang-format does not crash on the given input. 88 void verifyNoCrash(llvm::StringRef Code, 89 const FormatStyle &Style = getLLVMStyle()) { 90 format(Code, Style, IC_DoNotCheck); 91 } 92 93 int ReplacementCount; 94 }; 95 96 TEST_F(FormatTest, MessUp) { 97 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 98 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 99 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 100 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 101 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 102 } 103 104 //===----------------------------------------------------------------------===// 105 // Basic function tests. 106 //===----------------------------------------------------------------------===// 107 108 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 109 EXPECT_EQ(";", format(";")); 110 } 111 112 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 113 EXPECT_EQ("int i;", format(" int i;")); 114 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 115 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 116 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 117 } 118 119 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 120 EXPECT_EQ("int i;", format("int\ni;")); 121 } 122 123 TEST_F(FormatTest, FormatsNestedBlockStatements) { 124 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 125 } 126 127 TEST_F(FormatTest, FormatsNestedCall) { 128 verifyFormat("Method(f1, f2(f3));"); 129 verifyFormat("Method(f1(f2, f3()));"); 130 verifyFormat("Method(f1(f2, (f3())));"); 131 } 132 133 TEST_F(FormatTest, NestedNameSpecifiers) { 134 verifyFormat("vector<::Type> v;"); 135 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 136 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 137 verifyFormat("bool a = 2 < ::SomeFunction();"); 138 } 139 140 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 141 EXPECT_EQ("if (a) {\n" 142 " f();\n" 143 "}", 144 format("if(a){f();}")); 145 EXPECT_EQ(4, ReplacementCount); 146 EXPECT_EQ("if (a) {\n" 147 " f();\n" 148 "}", 149 format("if (a) {\n" 150 " f();\n" 151 "}")); 152 EXPECT_EQ(0, ReplacementCount); 153 EXPECT_EQ("/*\r\n" 154 "\r\n" 155 "*/\r\n", 156 format("/*\r\n" 157 "\r\n" 158 "*/\r\n")); 159 EXPECT_EQ(0, ReplacementCount); 160 } 161 162 TEST_F(FormatTest, RemovesEmptyLines) { 163 EXPECT_EQ("class C {\n" 164 " int i;\n" 165 "};", 166 format("class C {\n" 167 " int i;\n" 168 "\n" 169 "};")); 170 171 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 172 EXPECT_EQ("namespace N {\n" 173 "\n" 174 "int i;\n" 175 "}", 176 format("namespace N {\n" 177 "\n" 178 "int i;\n" 179 "}", 180 getGoogleStyle())); 181 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 182 "\n" 183 "int i;\n" 184 "}", 185 format("extern /**/ \"C\" /**/ {\n" 186 "\n" 187 "int i;\n" 188 "}", 189 getGoogleStyle())); 190 191 // ...but do keep inlining and removing empty lines for non-block extern "C" 192 // functions. 193 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 194 EXPECT_EQ("extern \"C\" int f() {\n" 195 " int i = 42;\n" 196 " return i;\n" 197 "}", 198 format("extern \"C\" int f() {\n" 199 "\n" 200 " int i = 42;\n" 201 " return i;\n" 202 "}", 203 getGoogleStyle())); 204 205 // Remove empty lines at the beginning and end of blocks. 206 EXPECT_EQ("void f() {\n" 207 "\n" 208 " if (a) {\n" 209 "\n" 210 " f();\n" 211 " }\n" 212 "}", 213 format("void f() {\n" 214 "\n" 215 " if (a) {\n" 216 "\n" 217 " f();\n" 218 "\n" 219 " }\n" 220 "\n" 221 "}", 222 getLLVMStyle())); 223 EXPECT_EQ("void f() {\n" 224 " if (a) {\n" 225 " f();\n" 226 " }\n" 227 "}", 228 format("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 "\n" 234 " }\n" 235 "\n" 236 "}", 237 getGoogleStyle())); 238 239 // Don't remove empty lines in more complex control statements. 240 EXPECT_EQ("void f() {\n" 241 " if (a) {\n" 242 " f();\n" 243 "\n" 244 " } else if (b) {\n" 245 " f();\n" 246 " }\n" 247 "}", 248 format("void f() {\n" 249 " if (a) {\n" 250 " f();\n" 251 "\n" 252 " } else if (b) {\n" 253 " f();\n" 254 "\n" 255 " }\n" 256 "\n" 257 "}")); 258 259 // FIXME: This is slightly inconsistent. 260 EXPECT_EQ("namespace {\n" 261 "int i;\n" 262 "}", 263 format("namespace {\n" 264 "int i;\n" 265 "\n" 266 "}")); 267 EXPECT_EQ("namespace {\n" 268 "int i;\n" 269 "\n" 270 "} // namespace", 271 format("namespace {\n" 272 "int i;\n" 273 "\n" 274 "} // namespace")); 275 } 276 277 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 278 verifyFormat("x = (a) and (b);"); 279 verifyFormat("x = (a) or (b);"); 280 verifyFormat("x = (a) bitand (b);"); 281 verifyFormat("x = (a) bitor (b);"); 282 verifyFormat("x = (a) not_eq (b);"); 283 verifyFormat("x = (a) and_eq (b);"); 284 verifyFormat("x = (a) or_eq (b);"); 285 verifyFormat("x = (a) xor (b);"); 286 } 287 288 //===----------------------------------------------------------------------===// 289 // Tests for control statements. 290 //===----------------------------------------------------------------------===// 291 292 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 293 verifyFormat("if (true)\n f();\ng();"); 294 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 295 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 296 297 FormatStyle AllowsMergedIf = getLLVMStyle(); 298 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 299 verifyFormat("if (a)\n" 300 " // comment\n" 301 " f();", 302 AllowsMergedIf); 303 verifyFormat("if (a)\n" 304 " ;", 305 AllowsMergedIf); 306 verifyFormat("if (a)\n" 307 " if (b) return;", 308 AllowsMergedIf); 309 310 verifyFormat("if (a) // Can't merge this\n" 311 " f();\n", 312 AllowsMergedIf); 313 verifyFormat("if (a) /* still don't merge */\n" 314 " f();", 315 AllowsMergedIf); 316 verifyFormat("if (a) { // Never merge this\n" 317 " f();\n" 318 "}", 319 AllowsMergedIf); 320 verifyFormat("if (a) { /* Never merge this */\n" 321 " f();\n" 322 "}", 323 AllowsMergedIf); 324 325 AllowsMergedIf.ColumnLimit = 14; 326 verifyFormat("if (a) return;", AllowsMergedIf); 327 verifyFormat("if (aaaaaaaaa)\n" 328 " return;", 329 AllowsMergedIf); 330 331 AllowsMergedIf.ColumnLimit = 13; 332 verifyFormat("if (a)\n return;", AllowsMergedIf); 333 } 334 335 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 336 FormatStyle AllowsMergedLoops = getLLVMStyle(); 337 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 338 verifyFormat("while (true) continue;", AllowsMergedLoops); 339 verifyFormat("for (;;) continue;", AllowsMergedLoops); 340 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 341 verifyFormat("while (true)\n" 342 " ;", 343 AllowsMergedLoops); 344 verifyFormat("for (;;)\n" 345 " ;", 346 AllowsMergedLoops); 347 verifyFormat("for (;;)\n" 348 " for (;;) continue;", 349 AllowsMergedLoops); 350 verifyFormat("for (;;) // Can't merge this\n" 351 " continue;", 352 AllowsMergedLoops); 353 verifyFormat("for (;;) /* still don't merge */\n" 354 " continue;", 355 AllowsMergedLoops); 356 } 357 358 TEST_F(FormatTest, FormatShortBracedStatements) { 359 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 360 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 361 362 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 363 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 364 365 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 366 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 367 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 368 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 369 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 370 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 371 verifyFormat("if (true) { //\n" 372 " f();\n" 373 "}", 374 AllowSimpleBracedStatements); 375 verifyFormat("if (true) {\n" 376 " f();\n" 377 " f();\n" 378 "}", 379 AllowSimpleBracedStatements); 380 verifyFormat("if (true) {\n" 381 " f();\n" 382 "} else {\n" 383 " f();\n" 384 "}", 385 AllowSimpleBracedStatements); 386 387 verifyFormat("template <int> struct A2 {\n" 388 " struct B {};\n" 389 "};", 390 AllowSimpleBracedStatements); 391 392 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 393 verifyFormat("if (true) {\n" 394 " f();\n" 395 "}", 396 AllowSimpleBracedStatements); 397 verifyFormat("if (true) {\n" 398 " f();\n" 399 "} else {\n" 400 " f();\n" 401 "}", 402 AllowSimpleBracedStatements); 403 404 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 405 verifyFormat("while (true) {\n" 406 " f();\n" 407 "}", 408 AllowSimpleBracedStatements); 409 verifyFormat("for (;;) {\n" 410 " f();\n" 411 "}", 412 AllowSimpleBracedStatements); 413 } 414 415 TEST_F(FormatTest, ParseIfElse) { 416 verifyFormat("if (true)\n" 417 " if (true)\n" 418 " if (true)\n" 419 " f();\n" 420 " else\n" 421 " g();\n" 422 " else\n" 423 " h();\n" 424 "else\n" 425 " i();"); 426 verifyFormat("if (true)\n" 427 " if (true)\n" 428 " if (true) {\n" 429 " if (true)\n" 430 " f();\n" 431 " } else {\n" 432 " g();\n" 433 " }\n" 434 " else\n" 435 " h();\n" 436 "else {\n" 437 " i();\n" 438 "}"); 439 verifyFormat("void f() {\n" 440 " if (a) {\n" 441 " } else {\n" 442 " }\n" 443 "}"); 444 } 445 446 TEST_F(FormatTest, ElseIf) { 447 verifyFormat("if (a) {\n} else if (b) {\n}"); 448 verifyFormat("if (a)\n" 449 " f();\n" 450 "else if (b)\n" 451 " g();\n" 452 "else\n" 453 " h();"); 454 verifyFormat("if (a) {\n" 455 " f();\n" 456 "}\n" 457 "// or else ..\n" 458 "else {\n" 459 " g()\n" 460 "}"); 461 462 verifyFormat("if (a) {\n" 463 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 465 "}"); 466 verifyFormat("if (a) {\n" 467 "} else if (\n" 468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 469 "}", 470 getLLVMStyleWithColumns(62)); 471 } 472 473 TEST_F(FormatTest, FormatsForLoop) { 474 verifyFormat( 475 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 476 " ++VeryVeryLongLoopVariable)\n" 477 " ;"); 478 verifyFormat("for (;;)\n" 479 " f();"); 480 verifyFormat("for (;;) {\n}"); 481 verifyFormat("for (;;) {\n" 482 " f();\n" 483 "}"); 484 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 485 486 verifyFormat( 487 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 488 " E = UnwrappedLines.end();\n" 489 " I != E; ++I) {\n}"); 490 491 verifyFormat( 492 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 493 " ++IIIII) {\n}"); 494 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 495 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 496 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 497 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 498 " I = FD->getDeclsInPrototypeScope().begin(),\n" 499 " E = FD->getDeclsInPrototypeScope().end();\n" 500 " I != E; ++I) {\n}"); 501 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 502 " I = Container.begin(),\n" 503 " E = Container.end();\n" 504 " I != E; ++I) {\n}", 505 getLLVMStyleWithColumns(76)); 506 507 verifyFormat( 508 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 511 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 512 " ++aaaaaaaaaaa) {\n}"); 513 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 514 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 515 " ++i) {\n}"); 516 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 517 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 518 "}"); 519 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 520 " aaaaaaaaaa);\n" 521 " iter; ++iter) {\n" 522 "}"); 523 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 525 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 526 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 527 528 FormatStyle NoBinPacking = getLLVMStyle(); 529 NoBinPacking.BinPackParameters = false; 530 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 531 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 532 " aaaaaaaaaaaaaaaa,\n" 533 " aaaaaaaaaaaaaaaa,\n" 534 " aaaaaaaaaaaaaaaa);\n" 535 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 536 "}", 537 NoBinPacking); 538 verifyFormat( 539 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 540 " E = UnwrappedLines.end();\n" 541 " I != E;\n" 542 " ++I) {\n}", 543 NoBinPacking); 544 } 545 546 TEST_F(FormatTest, RangeBasedForLoops) { 547 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 549 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 550 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 551 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 553 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 554 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 555 } 556 557 TEST_F(FormatTest, ForEachLoops) { 558 verifyFormat("void f() {\n" 559 " foreach (Item *item, itemlist) {}\n" 560 " Q_FOREACH (Item *item, itemlist) {}\n" 561 " BOOST_FOREACH (Item *item, itemlist) {}\n" 562 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 563 "}"); 564 565 // As function-like macros. 566 verifyFormat("#define foreach(x, y)\n" 567 "#define Q_FOREACH(x, y)\n" 568 "#define BOOST_FOREACH(x, y)\n" 569 "#define UNKNOWN_FOREACH(x, y)\n"); 570 571 // Not as function-like macros. 572 verifyFormat("#define foreach (x, y)\n" 573 "#define Q_FOREACH (x, y)\n" 574 "#define BOOST_FOREACH (x, y)\n" 575 "#define UNKNOWN_FOREACH (x, y)\n"); 576 } 577 578 TEST_F(FormatTest, FormatsWhileLoop) { 579 verifyFormat("while (true) {\n}"); 580 verifyFormat("while (true)\n" 581 " f();"); 582 verifyFormat("while () {\n}"); 583 verifyFormat("while () {\n" 584 " f();\n" 585 "}"); 586 } 587 588 TEST_F(FormatTest, FormatsDoWhile) { 589 verifyFormat("do {\n" 590 " do_something();\n" 591 "} while (something());"); 592 verifyFormat("do\n" 593 " do_something();\n" 594 "while (something());"); 595 } 596 597 TEST_F(FormatTest, FormatsSwitchStatement) { 598 verifyFormat("switch (x) {\n" 599 "case 1:\n" 600 " f();\n" 601 " break;\n" 602 "case kFoo:\n" 603 "case ns::kBar:\n" 604 "case kBaz:\n" 605 " break;\n" 606 "default:\n" 607 " g();\n" 608 " break;\n" 609 "}"); 610 verifyFormat("switch (x) {\n" 611 "case 1: {\n" 612 " f();\n" 613 " break;\n" 614 "}\n" 615 "case 2: {\n" 616 " break;\n" 617 "}\n" 618 "}"); 619 verifyFormat("switch (x) {\n" 620 "case 1: {\n" 621 " f();\n" 622 " {\n" 623 " g();\n" 624 " h();\n" 625 " }\n" 626 " break;\n" 627 "}\n" 628 "}"); 629 verifyFormat("switch (x) {\n" 630 "case 1: {\n" 631 " f();\n" 632 " if (foo) {\n" 633 " g();\n" 634 " h();\n" 635 " }\n" 636 " break;\n" 637 "}\n" 638 "}"); 639 verifyFormat("switch (x) {\n" 640 "case 1: {\n" 641 " f();\n" 642 " g();\n" 643 "} break;\n" 644 "}"); 645 verifyFormat("switch (test)\n" 646 " ;"); 647 verifyFormat("switch (x) {\n" 648 "default: {\n" 649 " // Do nothing.\n" 650 "}\n" 651 "}"); 652 verifyFormat("switch (x) {\n" 653 "// comment\n" 654 "// if 1, do f()\n" 655 "case 1:\n" 656 " f();\n" 657 "}"); 658 verifyFormat("switch (x) {\n" 659 "case 1:\n" 660 " // Do amazing stuff\n" 661 " {\n" 662 " f();\n" 663 " g();\n" 664 " }\n" 665 " break;\n" 666 "}"); 667 verifyFormat("#define A \\\n" 668 " switch (x) { \\\n" 669 " case a: \\\n" 670 " foo = b; \\\n" 671 " }", 672 getLLVMStyleWithColumns(20)); 673 verifyFormat("#define OPERATION_CASE(name) \\\n" 674 " case OP_name: \\\n" 675 " return operations::Operation##name\n", 676 getLLVMStyleWithColumns(40)); 677 verifyFormat("switch (x) {\n" 678 "case 1:;\n" 679 "default:;\n" 680 " int i;\n" 681 "}"); 682 683 verifyGoogleFormat("switch (x) {\n" 684 " case 1:\n" 685 " f();\n" 686 " break;\n" 687 " case kFoo:\n" 688 " case ns::kBar:\n" 689 " case kBaz:\n" 690 " break;\n" 691 " default:\n" 692 " g();\n" 693 " break;\n" 694 "}"); 695 verifyGoogleFormat("switch (x) {\n" 696 " case 1: {\n" 697 " f();\n" 698 " break;\n" 699 " }\n" 700 "}"); 701 verifyGoogleFormat("switch (test)\n" 702 " ;"); 703 704 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 705 " case OP_name: \\\n" 706 " return operations::Operation##name\n"); 707 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 708 " // Get the correction operation class.\n" 709 " switch (OpCode) {\n" 710 " CASE(Add);\n" 711 " CASE(Subtract);\n" 712 " default:\n" 713 " return operations::Unknown;\n" 714 " }\n" 715 "#undef OPERATION_CASE\n" 716 "}"); 717 verifyFormat("DEBUG({\n" 718 " switch (x) {\n" 719 " case A:\n" 720 " f();\n" 721 " break;\n" 722 " // On B:\n" 723 " case B:\n" 724 " g();\n" 725 " break;\n" 726 " }\n" 727 "});"); 728 verifyFormat("switch (a) {\n" 729 "case (b):\n" 730 " return;\n" 731 "}"); 732 733 verifyFormat("switch (a) {\n" 734 "case some_namespace::\n" 735 " some_constant:\n" 736 " return;\n" 737 "}", 738 getLLVMStyleWithColumns(34)); 739 } 740 741 TEST_F(FormatTest, CaseRanges) { 742 verifyFormat("switch (x) {\n" 743 "case 'A' ... 'Z':\n" 744 "case 1 ... 5:\n" 745 " break;\n" 746 "}"); 747 } 748 749 TEST_F(FormatTest, ShortCaseLabels) { 750 FormatStyle Style = getLLVMStyle(); 751 Style.AllowShortCaseLabelsOnASingleLine = true; 752 verifyFormat("switch (a) {\n" 753 "case 1: x = 1; break;\n" 754 "case 2: return;\n" 755 "case 3:\n" 756 "case 4:\n" 757 "case 5: return;\n" 758 "case 6: // comment\n" 759 " return;\n" 760 "case 7:\n" 761 " // comment\n" 762 " return;\n" 763 "case 8:\n" 764 " x = 8; // comment\n" 765 " break;\n" 766 "default: y = 1; break;\n" 767 "}", 768 Style); 769 verifyFormat("switch (a) {\n" 770 "#if FOO\n" 771 "case 0: return 0;\n" 772 "#endif\n" 773 "}", 774 Style); 775 verifyFormat("switch (a) {\n" 776 "case 1: {\n" 777 "}\n" 778 "case 2: {\n" 779 " return;\n" 780 "}\n" 781 "case 3: {\n" 782 " x = 1;\n" 783 " return;\n" 784 "}\n" 785 "case 4:\n" 786 " if (x)\n" 787 " return;\n" 788 "}", 789 Style); 790 Style.ColumnLimit = 21; 791 verifyFormat("switch (a) {\n" 792 "case 1: x = 1; break;\n" 793 "case 2: return;\n" 794 "case 3:\n" 795 "case 4:\n" 796 "case 5: return;\n" 797 "default:\n" 798 " y = 1;\n" 799 " break;\n" 800 "}", 801 Style); 802 } 803 804 TEST_F(FormatTest, FormatsLabels) { 805 verifyFormat("void f() {\n" 806 " some_code();\n" 807 "test_label:\n" 808 " some_other_code();\n" 809 " {\n" 810 " some_more_code();\n" 811 " another_label:\n" 812 " some_more_code();\n" 813 " }\n" 814 "}"); 815 verifyFormat("{\n" 816 " some_code();\n" 817 "test_label:\n" 818 " some_other_code();\n" 819 "}"); 820 verifyFormat("{\n" 821 " some_code();\n" 822 "test_label:;\n" 823 " int i = 0;\n" 824 "}"); 825 } 826 827 //===----------------------------------------------------------------------===// 828 // Tests for comments. 829 //===----------------------------------------------------------------------===// 830 831 TEST_F(FormatTest, UnderstandsSingleLineComments) { 832 verifyFormat("//* */"); 833 verifyFormat("// line 1\n" 834 "// line 2\n" 835 "void f() {}\n"); 836 837 verifyFormat("void f() {\n" 838 " // Doesn't do anything\n" 839 "}"); 840 verifyFormat("SomeObject\n" 841 " // Calling someFunction on SomeObject\n" 842 " .someFunction();"); 843 verifyFormat("auto result = SomeObject\n" 844 " // Calling someFunction on SomeObject\n" 845 " .someFunction();"); 846 verifyFormat("void f(int i, // some comment (probably for i)\n" 847 " int j, // some comment (probably for j)\n" 848 " int k); // some comment (probably for k)"); 849 verifyFormat("void f(int i,\n" 850 " // some comment (probably for j)\n" 851 " int j,\n" 852 " // some comment (probably for k)\n" 853 " int k);"); 854 855 verifyFormat("int i // This is a fancy variable\n" 856 " = 5; // with nicely aligned comment."); 857 858 verifyFormat("// Leading comment.\n" 859 "int a; // Trailing comment."); 860 verifyFormat("int a; // Trailing comment\n" 861 " // on 2\n" 862 " // or 3 lines.\n" 863 "int b;"); 864 verifyFormat("int a; // Trailing comment\n" 865 "\n" 866 "// Leading comment.\n" 867 "int b;"); 868 verifyFormat("int a; // Comment.\n" 869 " // More details.\n" 870 "int bbbb; // Another comment."); 871 verifyFormat( 872 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 873 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 874 "int cccccccccccccccccccccccccccccc; // comment\n" 875 "int ddd; // looooooooooooooooooooooooong comment\n" 876 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 877 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 878 "int ccccccccccccccccccc; // comment"); 879 880 verifyFormat("#include \"a\" // comment\n" 881 "#include \"a/b/c\" // comment"); 882 verifyFormat("#include <a> // comment\n" 883 "#include <a/b/c> // comment"); 884 EXPECT_EQ("#include \"a\" // comment\n" 885 "#include \"a/b/c\" // comment", 886 format("#include \\\n" 887 " \"a\" // comment\n" 888 "#include \"a/b/c\" // comment")); 889 890 verifyFormat("enum E {\n" 891 " // comment\n" 892 " VAL_A, // comment\n" 893 " VAL_B\n" 894 "};"); 895 896 verifyFormat( 897 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 898 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 899 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 900 " // Comment inside a statement.\n" 901 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 902 verifyFormat("SomeFunction(a,\n" 903 " // comment\n" 904 " b + x);"); 905 verifyFormat("SomeFunction(a, a,\n" 906 " // comment\n" 907 " b + x);"); 908 verifyFormat( 909 "bool aaaaaaaaaaaaa = // comment\n" 910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 912 913 verifyFormat("int aaaa; // aaaaa\n" 914 "int aa; // aaaaaaa", 915 getLLVMStyleWithColumns(20)); 916 917 EXPECT_EQ("void f() { // This does something ..\n" 918 "}\n" 919 "int a; // This is unrelated", 920 format("void f() { // This does something ..\n" 921 " }\n" 922 "int a; // This is unrelated")); 923 EXPECT_EQ("class C {\n" 924 " void f() { // This does something ..\n" 925 " } // awesome..\n" 926 "\n" 927 " int a; // This is unrelated\n" 928 "};", 929 format("class C{void f() { // This does something ..\n" 930 " } // awesome..\n" 931 " \n" 932 "int a; // This is unrelated\n" 933 "};")); 934 935 EXPECT_EQ("int i; // single line trailing comment", 936 format("int i;\\\n// single line trailing comment")); 937 938 verifyGoogleFormat("int a; // Trailing comment."); 939 940 verifyFormat("someFunction(anotherFunction( // Force break.\n" 941 " parameter));"); 942 943 verifyGoogleFormat("#endif // HEADER_GUARD"); 944 945 verifyFormat("const char *test[] = {\n" 946 " // A\n" 947 " \"aaaa\",\n" 948 " // B\n" 949 " \"aaaaa\"};"); 950 verifyGoogleFormat( 951 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 952 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 953 EXPECT_EQ("D(a, {\n" 954 " // test\n" 955 " int a;\n" 956 "});", 957 format("D(a, {\n" 958 "// test\n" 959 "int a;\n" 960 "});")); 961 962 EXPECT_EQ("lineWith(); // comment\n" 963 "// at start\n" 964 "otherLine();", 965 format("lineWith(); // comment\n" 966 "// at start\n" 967 "otherLine();")); 968 EXPECT_EQ("lineWith(); // comment\n" 969 "/*\n" 970 " * at start */\n" 971 "otherLine();", 972 format("lineWith(); // comment\n" 973 "/*\n" 974 " * at start */\n" 975 "otherLine();")); 976 EXPECT_EQ("lineWith(); // comment\n" 977 " // at start\n" 978 "otherLine();", 979 format("lineWith(); // comment\n" 980 " // at start\n" 981 "otherLine();")); 982 983 EXPECT_EQ("lineWith(); // comment\n" 984 "// at start\n" 985 "otherLine(); // comment", 986 format("lineWith(); // comment\n" 987 "// at start\n" 988 "otherLine(); // comment")); 989 EXPECT_EQ("lineWith();\n" 990 "// at start\n" 991 "otherLine(); // comment", 992 format("lineWith();\n" 993 " // at start\n" 994 "otherLine(); // comment")); 995 EXPECT_EQ("// first\n" 996 "// at start\n" 997 "otherLine(); // comment", 998 format("// first\n" 999 " // at start\n" 1000 "otherLine(); // comment")); 1001 EXPECT_EQ("f();\n" 1002 "// first\n" 1003 "// at start\n" 1004 "otherLine(); // comment", 1005 format("f();\n" 1006 "// first\n" 1007 " // at start\n" 1008 "otherLine(); // comment")); 1009 verifyFormat("f(); // comment\n" 1010 "// first\n" 1011 "// at start\n" 1012 "otherLine();"); 1013 EXPECT_EQ("f(); // comment\n" 1014 "// first\n" 1015 "// at start\n" 1016 "otherLine();", 1017 format("f(); // comment\n" 1018 "// first\n" 1019 " // at start\n" 1020 "otherLine();")); 1021 EXPECT_EQ("f(); // comment\n" 1022 " // first\n" 1023 "// at start\n" 1024 "otherLine();", 1025 format("f(); // comment\n" 1026 " // first\n" 1027 "// at start\n" 1028 "otherLine();")); 1029 EXPECT_EQ("void f() {\n" 1030 " lineWith(); // comment\n" 1031 " // at start\n" 1032 "}", 1033 format("void f() {\n" 1034 " lineWith(); // comment\n" 1035 " // at start\n" 1036 "}")); 1037 EXPECT_EQ("int xy; // a\n" 1038 "int z; // b", 1039 format("int xy; // a\n" 1040 "int z; //b")); 1041 EXPECT_EQ("int xy; // a\n" 1042 "int z; // bb", 1043 format("int xy; // a\n" 1044 "int z; //bb", 1045 getLLVMStyleWithColumns(12))); 1046 1047 verifyFormat("#define A \\\n" 1048 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1049 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1050 getLLVMStyleWithColumns(60)); 1051 verifyFormat( 1052 "#define A \\\n" 1053 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1054 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1055 getLLVMStyleWithColumns(61)); 1056 1057 verifyFormat("if ( // This is some comment\n" 1058 " x + 3) {\n" 1059 "}"); 1060 EXPECT_EQ("if ( // This is some comment\n" 1061 " // spanning two lines\n" 1062 " x + 3) {\n" 1063 "}", 1064 format("if( // This is some comment\n" 1065 " // spanning two lines\n" 1066 " x + 3) {\n" 1067 "}")); 1068 1069 verifyNoCrash("/\\\n/"); 1070 verifyNoCrash("/\\\n* */"); 1071 // The 0-character somehow makes the lexer return a proper comment. 1072 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1073 } 1074 1075 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1076 EXPECT_EQ("SomeFunction(a,\n" 1077 " b, // comment\n" 1078 " c);", 1079 format("SomeFunction(a,\n" 1080 " b, // comment\n" 1081 " c);")); 1082 EXPECT_EQ("SomeFunction(a, b,\n" 1083 " // comment\n" 1084 " c);", 1085 format("SomeFunction(a,\n" 1086 " b,\n" 1087 " // comment\n" 1088 " c);")); 1089 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1090 " c);", 1091 format("SomeFunction(a, b, // comment (unclear relation)\n" 1092 " c);")); 1093 EXPECT_EQ("SomeFunction(a, // comment\n" 1094 " b,\n" 1095 " c); // comment", 1096 format("SomeFunction(a, // comment\n" 1097 " b,\n" 1098 " c); // comment")); 1099 } 1100 1101 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1102 EXPECT_EQ("// comment", format("// comment ")); 1103 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1104 format("int aaaaaaa, bbbbbbb; // comment ", 1105 getLLVMStyleWithColumns(33))); 1106 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1107 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1108 } 1109 1110 TEST_F(FormatTest, UnderstandsBlockComments) { 1111 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1112 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1113 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1114 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1115 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1116 "/* Trailing comment for aa... */\n" 1117 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1118 EXPECT_EQ( 1119 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1120 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1121 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1122 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1123 EXPECT_EQ( 1124 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1125 " aaaaaaaaaaaaaaaaaa,\n" 1126 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1127 "}", 1128 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1129 " aaaaaaaaaaaaaaaaaa ,\n" 1130 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1131 "}")); 1132 1133 FormatStyle NoBinPacking = getLLVMStyle(); 1134 NoBinPacking.BinPackParameters = false; 1135 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1136 " /* parameter 2 */ aaaaaa,\n" 1137 " /* parameter 3 */ aaaaaa,\n" 1138 " /* parameter 4 */ aaaaaa);", 1139 NoBinPacking); 1140 1141 // Aligning block comments in macros. 1142 verifyGoogleFormat("#define A \\\n" 1143 " int i; /*a*/ \\\n" 1144 " int jjj; /*b*/"); 1145 } 1146 1147 TEST_F(FormatTest, AlignsBlockComments) { 1148 EXPECT_EQ("/*\n" 1149 " * Really multi-line\n" 1150 " * comment.\n" 1151 " */\n" 1152 "void f() {}", 1153 format(" /*\n" 1154 " * Really multi-line\n" 1155 " * comment.\n" 1156 " */\n" 1157 " void f() {}")); 1158 EXPECT_EQ("class C {\n" 1159 " /*\n" 1160 " * Another multi-line\n" 1161 " * comment.\n" 1162 " */\n" 1163 " void f() {}\n" 1164 "};", 1165 format("class C {\n" 1166 "/*\n" 1167 " * Another multi-line\n" 1168 " * comment.\n" 1169 " */\n" 1170 "void f() {}\n" 1171 "};")); 1172 EXPECT_EQ("/*\n" 1173 " 1. This is a comment with non-trivial formatting.\n" 1174 " 1.1. We have to indent/outdent all lines equally\n" 1175 " 1.1.1. to keep the formatting.\n" 1176 " */", 1177 format(" /*\n" 1178 " 1. This is a comment with non-trivial formatting.\n" 1179 " 1.1. We have to indent/outdent all lines equally\n" 1180 " 1.1.1. to keep the formatting.\n" 1181 " */")); 1182 EXPECT_EQ("/*\n" 1183 "Don't try to outdent if there's not enough indentation.\n" 1184 "*/", 1185 format(" /*\n" 1186 " Don't try to outdent if there's not enough indentation.\n" 1187 " */")); 1188 1189 EXPECT_EQ("int i; /* Comment with empty...\n" 1190 " *\n" 1191 " * line. */", 1192 format("int i; /* Comment with empty...\n" 1193 " *\n" 1194 " * line. */")); 1195 EXPECT_EQ("int foobar = 0; /* comment */\n" 1196 "int bar = 0; /* multiline\n" 1197 " comment 1 */\n" 1198 "int baz = 0; /* multiline\n" 1199 " comment 2 */\n" 1200 "int bzz = 0; /* multiline\n" 1201 " comment 3 */", 1202 format("int foobar = 0; /* comment */\n" 1203 "int bar = 0; /* multiline\n" 1204 " comment 1 */\n" 1205 "int baz = 0; /* multiline\n" 1206 " comment 2 */\n" 1207 "int bzz = 0; /* multiline\n" 1208 " comment 3 */")); 1209 EXPECT_EQ("int foobar = 0; /* comment */\n" 1210 "int bar = 0; /* multiline\n" 1211 " comment */\n" 1212 "int baz = 0; /* multiline\n" 1213 "comment */", 1214 format("int foobar = 0; /* comment */\n" 1215 "int bar = 0; /* multiline\n" 1216 "comment */\n" 1217 "int baz = 0; /* multiline\n" 1218 "comment */")); 1219 } 1220 1221 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1222 FormatStyle Style = getLLVMStyleWithColumns(20); 1223 Style.ReflowComments = false; 1224 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1225 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1226 } 1227 1228 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1229 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1231 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1233 EXPECT_EQ( 1234 "void ffffffffffff(\n" 1235 " int aaaaaaaa, int bbbbbbbb,\n" 1236 " int cccccccccccc) { /*\n" 1237 " aaaaaaaaaa\n" 1238 " aaaaaaaaaaaaa\n" 1239 " bbbbbbbbbbbbbb\n" 1240 " bbbbbbbbbb\n" 1241 " */\n" 1242 "}", 1243 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1244 "{ /*\n" 1245 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1246 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1247 " */\n" 1248 "}", 1249 getLLVMStyleWithColumns(40))); 1250 } 1251 1252 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1253 EXPECT_EQ("void ffffffffff(\n" 1254 " int aaaaa /* test */);", 1255 format("void ffffffffff(int aaaaa /* test */);", 1256 getLLVMStyleWithColumns(35))); 1257 } 1258 1259 TEST_F(FormatTest, SplitsLongCxxComments) { 1260 EXPECT_EQ("// A comment that\n" 1261 "// doesn't fit on\n" 1262 "// one line", 1263 format("// A comment that doesn't fit on one line", 1264 getLLVMStyleWithColumns(20))); 1265 EXPECT_EQ("/// A comment that\n" 1266 "/// doesn't fit on\n" 1267 "/// one line", 1268 format("/// A comment that doesn't fit on one line", 1269 getLLVMStyleWithColumns(20))); 1270 EXPECT_EQ("//! A comment that\n" 1271 "//! doesn't fit on\n" 1272 "//! one line", 1273 format("//! A comment that doesn't fit on one line", 1274 getLLVMStyleWithColumns(20))); 1275 EXPECT_EQ("// a b c d\n" 1276 "// e f g\n" 1277 "// h i j k", 1278 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1279 EXPECT_EQ( 1280 "// a b c d\n" 1281 "// e f g\n" 1282 "// h i j k", 1283 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1284 EXPECT_EQ("if (true) // A comment that\n" 1285 " // doesn't fit on\n" 1286 " // one line", 1287 format("if (true) // A comment that doesn't fit on one line ", 1288 getLLVMStyleWithColumns(30))); 1289 EXPECT_EQ("// Don't_touch_leading_whitespace", 1290 format("// Don't_touch_leading_whitespace", 1291 getLLVMStyleWithColumns(20))); 1292 EXPECT_EQ("// Add leading\n" 1293 "// whitespace", 1294 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1295 EXPECT_EQ("/// Add leading\n" 1296 "/// whitespace", 1297 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1298 EXPECT_EQ("//! Add leading\n" 1299 "//! whitespace", 1300 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1301 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1302 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1303 "// limit", 1304 format("//Even if it makes the line exceed the column limit", 1305 getLLVMStyleWithColumns(51))); 1306 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1307 1308 EXPECT_EQ("// aa bb cc dd", 1309 format("// aa bb cc dd ", 1310 getLLVMStyleWithColumns(15))); 1311 1312 EXPECT_EQ("// A comment before\n" 1313 "// a macro\n" 1314 "// definition\n" 1315 "#define a b", 1316 format("// A comment before a macro definition\n" 1317 "#define a b", 1318 getLLVMStyleWithColumns(20))); 1319 EXPECT_EQ("void ffffff(\n" 1320 " int aaaaaaaaa, // wwww\n" 1321 " int bbbbbbbbbb, // xxxxxxx\n" 1322 " // yyyyyyyyyy\n" 1323 " int c, int d, int e) {}", 1324 format("void ffffff(\n" 1325 " int aaaaaaaaa, // wwww\n" 1326 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1327 " int c, int d, int e) {}", 1328 getLLVMStyleWithColumns(40))); 1329 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1330 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1331 getLLVMStyleWithColumns(20))); 1332 EXPECT_EQ( 1333 "#define XXX // a b c d\n" 1334 " // e f g h", 1335 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1336 EXPECT_EQ( 1337 "#define XXX // q w e r\n" 1338 " // t y u i", 1339 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1340 } 1341 1342 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1343 EXPECT_EQ("// A comment\n" 1344 "// that doesn't\n" 1345 "// fit on one\n" 1346 "// line", 1347 format("// A comment that doesn't fit on one line", 1348 getLLVMStyleWithColumns(20))); 1349 EXPECT_EQ("/// A comment\n" 1350 "/// that doesn't\n" 1351 "/// fit on one\n" 1352 "/// line", 1353 format("/// A comment that doesn't fit on one line", 1354 getLLVMStyleWithColumns(20))); 1355 } 1356 1357 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1358 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1359 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1360 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1361 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1362 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1363 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1364 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1365 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1366 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1367 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1368 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1369 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1370 getLLVMStyleWithColumns(50))); 1371 // FIXME: One day we might want to implement adjustment of leading whitespace 1372 // of the consecutive lines in this kind of comment: 1373 EXPECT_EQ("double\n" 1374 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1375 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1376 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1377 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1378 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1379 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1380 getLLVMStyleWithColumns(49))); 1381 } 1382 1383 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1384 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1385 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1386 EXPECT_EQ( 1387 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1388 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1389 EXPECT_EQ( 1390 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1391 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1392 } 1393 1394 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1395 EXPECT_EQ("if (xxx ==\n" 1396 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1397 " zzz)\n" 1398 " q();", 1399 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1400 " zzz) q();", 1401 getLLVMStyleWithColumns(40))); 1402 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1403 " yyy && // aaaaaa bbbbbbbb cccc\n" 1404 " zzz)\n" 1405 " q();", 1406 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1407 " zzz) q();", 1408 getLLVMStyleWithColumns(40))); 1409 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1410 " yyy || // aaaaaa bbbbbbbb cccc\n" 1411 " zzz)\n" 1412 " q();", 1413 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1414 " zzz) q();", 1415 getLLVMStyleWithColumns(40))); 1416 EXPECT_EQ("fffffffff(\n" 1417 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1418 " zzz);", 1419 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1420 " zzz);", 1421 getLLVMStyleWithColumns(40))); 1422 } 1423 1424 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1425 EXPECT_EQ("#define A(x) /* \\\n" 1426 " a comment \\\n" 1427 " inside */ \\\n" 1428 " f();", 1429 format("#define A(x) /* \\\n" 1430 " a comment \\\n" 1431 " inside */ \\\n" 1432 " f();", 1433 getLLVMStyleWithColumns(17))); 1434 EXPECT_EQ("#define A( \\\n" 1435 " x) /* \\\n" 1436 " a comment \\\n" 1437 " inside */ \\\n" 1438 " f();", 1439 format("#define A( \\\n" 1440 " x) /* \\\n" 1441 " a comment \\\n" 1442 " inside */ \\\n" 1443 " f();", 1444 getLLVMStyleWithColumns(17))); 1445 } 1446 1447 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1448 EXPECT_EQ("namespace {}\n// Test\n#define A", 1449 format("namespace {}\n // Test\n#define A")); 1450 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1451 format("namespace {}\n /* Test */\n#define A")); 1452 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1453 format("namespace {}\n /* Test */ #define A")); 1454 } 1455 1456 TEST_F(FormatTest, SplitsLongLinesInComments) { 1457 EXPECT_EQ("/* This is a long\n" 1458 " * comment that\n" 1459 " * doesn't\n" 1460 " * fit on one line.\n" 1461 " */", 1462 format("/* " 1463 "This is a long " 1464 "comment that " 1465 "doesn't " 1466 "fit on one line. */", 1467 getLLVMStyleWithColumns(20))); 1468 EXPECT_EQ( 1469 "/* a b c d\n" 1470 " * e f g\n" 1471 " * h i j k\n" 1472 " */", 1473 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1474 EXPECT_EQ( 1475 "/* a b c d\n" 1476 " * e f g\n" 1477 " * h i j k\n" 1478 " */", 1479 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1480 EXPECT_EQ("/*\n" 1481 "This is a long\n" 1482 "comment that doesn't\n" 1483 "fit on one line.\n" 1484 "*/", 1485 format("/*\n" 1486 "This is a long " 1487 "comment that doesn't " 1488 "fit on one line. \n" 1489 "*/", 1490 getLLVMStyleWithColumns(20))); 1491 EXPECT_EQ("/*\n" 1492 " * This is a long\n" 1493 " * comment that\n" 1494 " * doesn't fit on\n" 1495 " * one line.\n" 1496 " */", 1497 format("/* \n" 1498 " * This is a long " 1499 " comment that " 1500 " doesn't fit on " 1501 " one line. \n" 1502 " */", 1503 getLLVMStyleWithColumns(20))); 1504 EXPECT_EQ("/*\n" 1505 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1506 " * so_it_should_be_broken\n" 1507 " * wherever_a_space_occurs\n" 1508 " */", 1509 format("/*\n" 1510 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1511 " so_it_should_be_broken " 1512 " wherever_a_space_occurs \n" 1513 " */", 1514 getLLVMStyleWithColumns(20))); 1515 EXPECT_EQ("/*\n" 1516 " * This_comment_can_not_be_broken_into_lines\n" 1517 " */", 1518 format("/*\n" 1519 " * This_comment_can_not_be_broken_into_lines\n" 1520 " */", 1521 getLLVMStyleWithColumns(20))); 1522 EXPECT_EQ("{\n" 1523 " /*\n" 1524 " This is another\n" 1525 " long comment that\n" 1526 " doesn't fit on one\n" 1527 " line 1234567890\n" 1528 " */\n" 1529 "}", 1530 format("{\n" 1531 "/*\n" 1532 "This is another " 1533 " long comment that " 1534 " doesn't fit on one" 1535 " line 1234567890\n" 1536 "*/\n" 1537 "}", 1538 getLLVMStyleWithColumns(20))); 1539 EXPECT_EQ("{\n" 1540 " /*\n" 1541 " * This i s\n" 1542 " * another comment\n" 1543 " * t hat doesn' t\n" 1544 " * fit on one l i\n" 1545 " * n e\n" 1546 " */\n" 1547 "}", 1548 format("{\n" 1549 "/*\n" 1550 " * This i s" 1551 " another comment" 1552 " t hat doesn' t" 1553 " fit on one l i" 1554 " n e\n" 1555 " */\n" 1556 "}", 1557 getLLVMStyleWithColumns(20))); 1558 EXPECT_EQ("/*\n" 1559 " * This is a long\n" 1560 " * comment that\n" 1561 " * doesn't fit on\n" 1562 " * one line\n" 1563 " */", 1564 format(" /*\n" 1565 " * This is a long comment that doesn't fit on one line\n" 1566 " */", 1567 getLLVMStyleWithColumns(20))); 1568 EXPECT_EQ("{\n" 1569 " if (something) /* This is a\n" 1570 " long\n" 1571 " comment */\n" 1572 " ;\n" 1573 "}", 1574 format("{\n" 1575 " if (something) /* This is a long comment */\n" 1576 " ;\n" 1577 "}", 1578 getLLVMStyleWithColumns(30))); 1579 1580 EXPECT_EQ("/* A comment before\n" 1581 " * a macro\n" 1582 " * definition */\n" 1583 "#define a b", 1584 format("/* A comment before a macro definition */\n" 1585 "#define a b", 1586 getLLVMStyleWithColumns(20))); 1587 1588 EXPECT_EQ("/* some comment\n" 1589 " * a comment\n" 1590 "* that we break\n" 1591 " * another comment\n" 1592 "* we have to break\n" 1593 "* a left comment\n" 1594 " */", 1595 format(" /* some comment\n" 1596 " * a comment that we break\n" 1597 " * another comment we have to break\n" 1598 "* a left comment\n" 1599 " */", 1600 getLLVMStyleWithColumns(20))); 1601 1602 EXPECT_EQ("/**\n" 1603 " * multiline block\n" 1604 " * comment\n" 1605 " *\n" 1606 " */", 1607 format("/**\n" 1608 " * multiline block comment\n" 1609 " *\n" 1610 " */", 1611 getLLVMStyleWithColumns(20))); 1612 1613 EXPECT_EQ("/*\n" 1614 "\n" 1615 "\n" 1616 " */\n", 1617 format(" /* \n" 1618 " \n" 1619 " \n" 1620 " */\n")); 1621 1622 EXPECT_EQ("/* a a */", 1623 format("/* a a */", getLLVMStyleWithColumns(15))); 1624 EXPECT_EQ("/* a a bc */", 1625 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1626 EXPECT_EQ("/* aaa aaa\n" 1627 " * aaaaa */", 1628 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1629 EXPECT_EQ("/* aaa aaa\n" 1630 " * aaaaa */", 1631 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1632 } 1633 1634 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1635 EXPECT_EQ("#define X \\\n" 1636 " /* \\\n" 1637 " Test \\\n" 1638 " Macro comment \\\n" 1639 " with a long \\\n" 1640 " line \\\n" 1641 " */ \\\n" 1642 " A + B", 1643 format("#define X \\\n" 1644 " /*\n" 1645 " Test\n" 1646 " Macro comment with a long line\n" 1647 " */ \\\n" 1648 " A + B", 1649 getLLVMStyleWithColumns(20))); 1650 EXPECT_EQ("#define X \\\n" 1651 " /* Macro comment \\\n" 1652 " with a long \\\n" 1653 " line */ \\\n" 1654 " A + B", 1655 format("#define X \\\n" 1656 " /* Macro comment with a long\n" 1657 " line */ \\\n" 1658 " A + B", 1659 getLLVMStyleWithColumns(20))); 1660 EXPECT_EQ("#define X \\\n" 1661 " /* Macro comment \\\n" 1662 " * with a long \\\n" 1663 " * line */ \\\n" 1664 " A + B", 1665 format("#define X \\\n" 1666 " /* Macro comment with a long line */ \\\n" 1667 " A + B", 1668 getLLVMStyleWithColumns(20))); 1669 } 1670 1671 TEST_F(FormatTest, CommentsInStaticInitializers) { 1672 EXPECT_EQ( 1673 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1674 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1675 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1676 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1677 " aaaaaaaaaaaaaaaaaaaa};", 1678 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1679 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1680 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1681 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1682 " aaaaaaaaaaaaaaaaaaaa };")); 1683 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1684 " bbbbbbbbbbb, ccccccccccc};"); 1685 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1686 " // comment for bb....\n" 1687 " bbbbbbbbbbb, ccccccccccc};"); 1688 verifyGoogleFormat( 1689 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1690 " bbbbbbbbbbb, ccccccccccc};"); 1691 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1692 " // comment for bb....\n" 1693 " bbbbbbbbbbb, ccccccccccc};"); 1694 1695 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1696 " {d, e, f}, // Group #2\n" 1697 " {g, h, i}}; // Group #3"); 1698 verifyFormat("S s = {{// Group #1\n" 1699 " a, b, c},\n" 1700 " {// Group #2\n" 1701 " d, e, f},\n" 1702 " {// Group #3\n" 1703 " g, h, i}};"); 1704 1705 EXPECT_EQ("S s = {\n" 1706 " // Some comment\n" 1707 " a,\n" 1708 "\n" 1709 " // Comment after empty line\n" 1710 " b}", 1711 format("S s = {\n" 1712 " // Some comment\n" 1713 " a,\n" 1714 " \n" 1715 " // Comment after empty line\n" 1716 " b\n" 1717 "}")); 1718 EXPECT_EQ("S s = {\n" 1719 " /* Some comment */\n" 1720 " a,\n" 1721 "\n" 1722 " /* Comment after empty line */\n" 1723 " b}", 1724 format("S s = {\n" 1725 " /* Some comment */\n" 1726 " a,\n" 1727 " \n" 1728 " /* Comment after empty line */\n" 1729 " b\n" 1730 "}")); 1731 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1732 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1733 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1734 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1735 } 1736 1737 TEST_F(FormatTest, IgnoresIf0Contents) { 1738 EXPECT_EQ("#if 0\n" 1739 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1740 "#endif\n" 1741 "void f() {}", 1742 format("#if 0\n" 1743 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1744 "#endif\n" 1745 "void f( ) { }")); 1746 EXPECT_EQ("#if false\n" 1747 "void f( ) { }\n" 1748 "#endif\n" 1749 "void g() {}\n", 1750 format("#if false\n" 1751 "void f( ) { }\n" 1752 "#endif\n" 1753 "void g( ) { }\n")); 1754 EXPECT_EQ("enum E {\n" 1755 " One,\n" 1756 " Two,\n" 1757 "#if 0\n" 1758 "Three,\n" 1759 " Four,\n" 1760 "#endif\n" 1761 " Five\n" 1762 "};", 1763 format("enum E {\n" 1764 " One,Two,\n" 1765 "#if 0\n" 1766 "Three,\n" 1767 " Four,\n" 1768 "#endif\n" 1769 " Five};")); 1770 EXPECT_EQ("enum F {\n" 1771 " One,\n" 1772 "#if 1\n" 1773 " Two,\n" 1774 "#if 0\n" 1775 "Three,\n" 1776 " Four,\n" 1777 "#endif\n" 1778 " Five\n" 1779 "#endif\n" 1780 "};", 1781 format("enum F {\n" 1782 "One,\n" 1783 "#if 1\n" 1784 "Two,\n" 1785 "#if 0\n" 1786 "Three,\n" 1787 " Four,\n" 1788 "#endif\n" 1789 "Five\n" 1790 "#endif\n" 1791 "};")); 1792 EXPECT_EQ("enum G {\n" 1793 " One,\n" 1794 "#if 0\n" 1795 "Two,\n" 1796 "#else\n" 1797 " Three,\n" 1798 "#endif\n" 1799 " Four\n" 1800 "};", 1801 format("enum G {\n" 1802 "One,\n" 1803 "#if 0\n" 1804 "Two,\n" 1805 "#else\n" 1806 "Three,\n" 1807 "#endif\n" 1808 "Four\n" 1809 "};")); 1810 EXPECT_EQ("enum H {\n" 1811 " One,\n" 1812 "#if 0\n" 1813 "#ifdef Q\n" 1814 "Two,\n" 1815 "#else\n" 1816 "Three,\n" 1817 "#endif\n" 1818 "#endif\n" 1819 " Four\n" 1820 "};", 1821 format("enum H {\n" 1822 "One,\n" 1823 "#if 0\n" 1824 "#ifdef Q\n" 1825 "Two,\n" 1826 "#else\n" 1827 "Three,\n" 1828 "#endif\n" 1829 "#endif\n" 1830 "Four\n" 1831 "};")); 1832 EXPECT_EQ("enum I {\n" 1833 " One,\n" 1834 "#if /* test */ 0 || 1\n" 1835 "Two,\n" 1836 "Three,\n" 1837 "#endif\n" 1838 " Four\n" 1839 "};", 1840 format("enum I {\n" 1841 "One,\n" 1842 "#if /* test */ 0 || 1\n" 1843 "Two,\n" 1844 "Three,\n" 1845 "#endif\n" 1846 "Four\n" 1847 "};")); 1848 EXPECT_EQ("enum J {\n" 1849 " One,\n" 1850 "#if 0\n" 1851 "#if 0\n" 1852 "Two,\n" 1853 "#else\n" 1854 "Three,\n" 1855 "#endif\n" 1856 "Four,\n" 1857 "#endif\n" 1858 " Five\n" 1859 "};", 1860 format("enum J {\n" 1861 "One,\n" 1862 "#if 0\n" 1863 "#if 0\n" 1864 "Two,\n" 1865 "#else\n" 1866 "Three,\n" 1867 "#endif\n" 1868 "Four,\n" 1869 "#endif\n" 1870 "Five\n" 1871 "};")); 1872 } 1873 1874 //===----------------------------------------------------------------------===// 1875 // Tests for classes, namespaces, etc. 1876 //===----------------------------------------------------------------------===// 1877 1878 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1879 verifyFormat("class A {};"); 1880 } 1881 1882 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1883 verifyFormat("class A {\n" 1884 "public:\n" 1885 "public: // comment\n" 1886 "protected:\n" 1887 "private:\n" 1888 " void f() {}\n" 1889 "};"); 1890 verifyGoogleFormat("class A {\n" 1891 " public:\n" 1892 " protected:\n" 1893 " private:\n" 1894 " void f() {}\n" 1895 "};"); 1896 verifyFormat("class A {\n" 1897 "public slots:\n" 1898 " void f1() {}\n" 1899 "public Q_SLOTS:\n" 1900 " void f2() {}\n" 1901 "protected slots:\n" 1902 " void f3() {}\n" 1903 "protected Q_SLOTS:\n" 1904 " void f4() {}\n" 1905 "private slots:\n" 1906 " void f5() {}\n" 1907 "private Q_SLOTS:\n" 1908 " void f6() {}\n" 1909 "signals:\n" 1910 " void g1();\n" 1911 "Q_SIGNALS:\n" 1912 " void g2();\n" 1913 "};"); 1914 1915 // Don't interpret 'signals' the wrong way. 1916 verifyFormat("signals.set();"); 1917 verifyFormat("for (Signals signals : f()) {\n}"); 1918 verifyFormat("{\n" 1919 " signals.set(); // This needs indentation.\n" 1920 "}"); 1921 } 1922 1923 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1924 EXPECT_EQ("class A {\n" 1925 "public:\n" 1926 " void f();\n" 1927 "\n" 1928 "private:\n" 1929 " void g() {}\n" 1930 " // test\n" 1931 "protected:\n" 1932 " int h;\n" 1933 "};", 1934 format("class A {\n" 1935 "public:\n" 1936 "void f();\n" 1937 "private:\n" 1938 "void g() {}\n" 1939 "// test\n" 1940 "protected:\n" 1941 "int h;\n" 1942 "};")); 1943 EXPECT_EQ("class A {\n" 1944 "protected:\n" 1945 "public:\n" 1946 " void f();\n" 1947 "};", 1948 format("class A {\n" 1949 "protected:\n" 1950 "\n" 1951 "public:\n" 1952 "\n" 1953 " void f();\n" 1954 "};")); 1955 1956 // Even ensure proper spacing inside macros. 1957 EXPECT_EQ("#define B \\\n" 1958 " class A { \\\n" 1959 " protected: \\\n" 1960 " public: \\\n" 1961 " void f(); \\\n" 1962 " };", 1963 format("#define B \\\n" 1964 " class A { \\\n" 1965 " protected: \\\n" 1966 " \\\n" 1967 " public: \\\n" 1968 " \\\n" 1969 " void f(); \\\n" 1970 " };", 1971 getGoogleStyle())); 1972 // But don't remove empty lines after macros ending in access specifiers. 1973 EXPECT_EQ("#define A private:\n" 1974 "\n" 1975 "int i;", 1976 format("#define A private:\n" 1977 "\n" 1978 "int i;")); 1979 } 1980 1981 TEST_F(FormatTest, FormatsClasses) { 1982 verifyFormat("class A : public B {};"); 1983 verifyFormat("class A : public ::B {};"); 1984 1985 verifyFormat( 1986 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1987 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1988 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1989 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1990 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1991 verifyFormat( 1992 "class A : public B, public C, public D, public E, public F {};"); 1993 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1994 " public C,\n" 1995 " public D,\n" 1996 " public E,\n" 1997 " public F,\n" 1998 " public G {};"); 1999 2000 verifyFormat("class\n" 2001 " ReallyReallyLongClassName {\n" 2002 " int i;\n" 2003 "};", 2004 getLLVMStyleWithColumns(32)); 2005 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2006 " aaaaaaaaaaaaaaaa> {};"); 2007 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2008 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2009 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2010 verifyFormat("template <class R, class C>\n" 2011 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2012 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2013 verifyFormat("class ::A::B {};"); 2014 } 2015 2016 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2017 verifyFormat("class A {\n} a, b;"); 2018 verifyFormat("struct A {\n} a, b;"); 2019 verifyFormat("union A {\n} a;"); 2020 } 2021 2022 TEST_F(FormatTest, FormatsEnum) { 2023 verifyFormat("enum {\n" 2024 " Zero,\n" 2025 " One = 1,\n" 2026 " Two = One + 1,\n" 2027 " Three = (One + Two),\n" 2028 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2029 " Five = (One, Two, Three, Four, 5)\n" 2030 "};"); 2031 verifyGoogleFormat("enum {\n" 2032 " Zero,\n" 2033 " One = 1,\n" 2034 " Two = One + 1,\n" 2035 " Three = (One + Two),\n" 2036 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2037 " Five = (One, Two, Three, Four, 5)\n" 2038 "};"); 2039 verifyFormat("enum Enum {};"); 2040 verifyFormat("enum {};"); 2041 verifyFormat("enum X E {} d;"); 2042 verifyFormat("enum __attribute__((...)) E {} d;"); 2043 verifyFormat("enum __declspec__((...)) E {} d;"); 2044 verifyFormat("enum {\n" 2045 " Bar = Foo<int, int>::value\n" 2046 "};", 2047 getLLVMStyleWithColumns(30)); 2048 2049 verifyFormat("enum ShortEnum { A, B, C };"); 2050 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2051 2052 EXPECT_EQ("enum KeepEmptyLines {\n" 2053 " ONE,\n" 2054 "\n" 2055 " TWO,\n" 2056 "\n" 2057 " THREE\n" 2058 "}", 2059 format("enum KeepEmptyLines {\n" 2060 " ONE,\n" 2061 "\n" 2062 " TWO,\n" 2063 "\n" 2064 "\n" 2065 " THREE\n" 2066 "}")); 2067 verifyFormat("enum E { // comment\n" 2068 " ONE,\n" 2069 " TWO\n" 2070 "};\n" 2071 "int i;"); 2072 // Not enums. 2073 verifyFormat("enum X f() {\n" 2074 " a();\n" 2075 " return 42;\n" 2076 "}"); 2077 verifyFormat("enum X Type::f() {\n" 2078 " a();\n" 2079 " return 42;\n" 2080 "}"); 2081 verifyFormat("enum ::X f() {\n" 2082 " a();\n" 2083 " return 42;\n" 2084 "}"); 2085 verifyFormat("enum ns::X f() {\n" 2086 " a();\n" 2087 " return 42;\n" 2088 "}"); 2089 } 2090 2091 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2092 verifyFormat("enum Type {\n" 2093 " One = 0; // These semicolons should be commas.\n" 2094 " Two = 1;\n" 2095 "};"); 2096 verifyFormat("namespace n {\n" 2097 "enum Type {\n" 2098 " One,\n" 2099 " Two, // missing };\n" 2100 " int i;\n" 2101 "}\n" 2102 "void g() {}"); 2103 } 2104 2105 TEST_F(FormatTest, FormatsEnumStruct) { 2106 verifyFormat("enum struct {\n" 2107 " Zero,\n" 2108 " One = 1,\n" 2109 " Two = One + 1,\n" 2110 " Three = (One + Two),\n" 2111 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2112 " Five = (One, Two, Three, Four, 5)\n" 2113 "};"); 2114 verifyFormat("enum struct Enum {};"); 2115 verifyFormat("enum struct {};"); 2116 verifyFormat("enum struct X E {} d;"); 2117 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2118 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2119 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2120 } 2121 2122 TEST_F(FormatTest, FormatsEnumClass) { 2123 verifyFormat("enum class {\n" 2124 " Zero,\n" 2125 " One = 1,\n" 2126 " Two = One + 1,\n" 2127 " Three = (One + Two),\n" 2128 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2129 " Five = (One, Two, Three, Four, 5)\n" 2130 "};"); 2131 verifyFormat("enum class Enum {};"); 2132 verifyFormat("enum class {};"); 2133 verifyFormat("enum class X E {} d;"); 2134 verifyFormat("enum class __attribute__((...)) E {} d;"); 2135 verifyFormat("enum class __declspec__((...)) E {} d;"); 2136 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2137 } 2138 2139 TEST_F(FormatTest, FormatsEnumTypes) { 2140 verifyFormat("enum X : int {\n" 2141 " A, // Force multiple lines.\n" 2142 " B\n" 2143 "};"); 2144 verifyFormat("enum X : int { A, B };"); 2145 verifyFormat("enum X : std::uint32_t { A, B };"); 2146 } 2147 2148 TEST_F(FormatTest, FormatsNSEnums) { 2149 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2150 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2151 " // Information about someDecentlyLongValue.\n" 2152 " someDecentlyLongValue,\n" 2153 " // Information about anotherDecentlyLongValue.\n" 2154 " anotherDecentlyLongValue,\n" 2155 " // Information about aThirdDecentlyLongValue.\n" 2156 " aThirdDecentlyLongValue\n" 2157 "};"); 2158 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2159 " a = 1,\n" 2160 " b = 2,\n" 2161 " c = 3,\n" 2162 "};"); 2163 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2164 " a = 1,\n" 2165 " b = 2,\n" 2166 " c = 3,\n" 2167 "};"); 2168 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2169 " a = 1,\n" 2170 " b = 2,\n" 2171 " c = 3,\n" 2172 "};"); 2173 } 2174 2175 TEST_F(FormatTest, FormatsBitfields) { 2176 verifyFormat("struct Bitfields {\n" 2177 " unsigned sClass : 8;\n" 2178 " unsigned ValueKind : 2;\n" 2179 "};"); 2180 verifyFormat("struct A {\n" 2181 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2182 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2183 "};"); 2184 verifyFormat("struct MyStruct {\n" 2185 " uchar data;\n" 2186 " uchar : 8;\n" 2187 " uchar : 8;\n" 2188 " uchar other;\n" 2189 "};"); 2190 } 2191 2192 TEST_F(FormatTest, FormatsNamespaces) { 2193 verifyFormat("namespace some_namespace {\n" 2194 "class A {};\n" 2195 "void f() { f(); }\n" 2196 "}"); 2197 verifyFormat("namespace {\n" 2198 "class A {};\n" 2199 "void f() { f(); }\n" 2200 "}"); 2201 verifyFormat("inline namespace X {\n" 2202 "class A {};\n" 2203 "void f() { f(); }\n" 2204 "}"); 2205 verifyFormat("using namespace some_namespace;\n" 2206 "class A {};\n" 2207 "void f() { f(); }"); 2208 2209 // This code is more common than we thought; if we 2210 // layout this correctly the semicolon will go into 2211 // its own line, which is undesirable. 2212 verifyFormat("namespace {};"); 2213 verifyFormat("namespace {\n" 2214 "class A {};\n" 2215 "};"); 2216 2217 verifyFormat("namespace {\n" 2218 "int SomeVariable = 0; // comment\n" 2219 "} // namespace"); 2220 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2221 "#define HEADER_GUARD\n" 2222 "namespace my_namespace {\n" 2223 "int i;\n" 2224 "} // my_namespace\n" 2225 "#endif // HEADER_GUARD", 2226 format("#ifndef HEADER_GUARD\n" 2227 " #define HEADER_GUARD\n" 2228 " namespace my_namespace {\n" 2229 "int i;\n" 2230 "} // my_namespace\n" 2231 "#endif // HEADER_GUARD")); 2232 2233 EXPECT_EQ("namespace A::B {\n" 2234 "class C {};\n" 2235 "}", 2236 format("namespace A::B {\n" 2237 "class C {};\n" 2238 "}")); 2239 2240 FormatStyle Style = getLLVMStyle(); 2241 Style.NamespaceIndentation = FormatStyle::NI_All; 2242 EXPECT_EQ("namespace out {\n" 2243 " int i;\n" 2244 " namespace in {\n" 2245 " int i;\n" 2246 " } // namespace\n" 2247 "} // namespace", 2248 format("namespace out {\n" 2249 "int i;\n" 2250 "namespace in {\n" 2251 "int i;\n" 2252 "} // namespace\n" 2253 "} // namespace", 2254 Style)); 2255 2256 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2257 EXPECT_EQ("namespace out {\n" 2258 "int i;\n" 2259 "namespace in {\n" 2260 " int i;\n" 2261 "} // namespace\n" 2262 "} // namespace", 2263 format("namespace out {\n" 2264 "int i;\n" 2265 "namespace in {\n" 2266 "int i;\n" 2267 "} // namespace\n" 2268 "} // namespace", 2269 Style)); 2270 } 2271 2272 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2273 2274 TEST_F(FormatTest, FormatsInlineASM) { 2275 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2276 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2277 verifyFormat( 2278 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2279 " \"cpuid\\n\\t\"\n" 2280 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2281 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2282 " : \"a\"(value));"); 2283 EXPECT_EQ( 2284 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2285 " __asm {\n" 2286 " mov edx,[that] // vtable in edx\n" 2287 " mov eax,methodIndex\n" 2288 " call [edx][eax*4] // stdcall\n" 2289 " }\n" 2290 "}", 2291 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2292 " __asm {\n" 2293 " mov edx,[that] // vtable in edx\n" 2294 " mov eax,methodIndex\n" 2295 " call [edx][eax*4] // stdcall\n" 2296 " }\n" 2297 "}")); 2298 EXPECT_EQ("_asm {\n" 2299 " xor eax, eax;\n" 2300 " cpuid;\n" 2301 "}", 2302 format("_asm {\n" 2303 " xor eax, eax;\n" 2304 " cpuid;\n" 2305 "}")); 2306 verifyFormat("void function() {\n" 2307 " // comment\n" 2308 " asm(\"\");\n" 2309 "}"); 2310 EXPECT_EQ("__asm {\n" 2311 "}\n" 2312 "int i;", 2313 format("__asm {\n" 2314 "}\n" 2315 "int i;")); 2316 } 2317 2318 TEST_F(FormatTest, FormatTryCatch) { 2319 verifyFormat("try {\n" 2320 " throw a * b;\n" 2321 "} catch (int a) {\n" 2322 " // Do nothing.\n" 2323 "} catch (...) {\n" 2324 " exit(42);\n" 2325 "}"); 2326 2327 // Function-level try statements. 2328 verifyFormat("int f() try { return 4; } catch (...) {\n" 2329 " return 5;\n" 2330 "}"); 2331 verifyFormat("class A {\n" 2332 " int a;\n" 2333 " A() try : a(0) {\n" 2334 " } catch (...) {\n" 2335 " throw;\n" 2336 " }\n" 2337 "};\n"); 2338 2339 // Incomplete try-catch blocks. 2340 verifyIncompleteFormat("try {} catch ("); 2341 } 2342 2343 TEST_F(FormatTest, FormatSEHTryCatch) { 2344 verifyFormat("__try {\n" 2345 " int a = b * c;\n" 2346 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2347 " // Do nothing.\n" 2348 "}"); 2349 2350 verifyFormat("__try {\n" 2351 " int a = b * c;\n" 2352 "} __finally {\n" 2353 " // Do nothing.\n" 2354 "}"); 2355 2356 verifyFormat("DEBUG({\n" 2357 " __try {\n" 2358 " } __finally {\n" 2359 " }\n" 2360 "});\n"); 2361 } 2362 2363 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2364 verifyFormat("try {\n" 2365 " f();\n" 2366 "} catch {\n" 2367 " g();\n" 2368 "}"); 2369 verifyFormat("try {\n" 2370 " f();\n" 2371 "} catch (A a) MACRO(x) {\n" 2372 " g();\n" 2373 "} catch (B b) MACRO(x) {\n" 2374 " g();\n" 2375 "}"); 2376 } 2377 2378 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2379 FormatStyle Style = getLLVMStyle(); 2380 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2381 FormatStyle::BS_WebKit}) { 2382 Style.BreakBeforeBraces = BraceStyle; 2383 verifyFormat("try {\n" 2384 " // something\n" 2385 "} catch (...) {\n" 2386 " // something\n" 2387 "}", 2388 Style); 2389 } 2390 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2391 verifyFormat("try {\n" 2392 " // something\n" 2393 "}\n" 2394 "catch (...) {\n" 2395 " // something\n" 2396 "}", 2397 Style); 2398 verifyFormat("__try {\n" 2399 " // something\n" 2400 "}\n" 2401 "__finally {\n" 2402 " // something\n" 2403 "}", 2404 Style); 2405 verifyFormat("@try {\n" 2406 " // something\n" 2407 "}\n" 2408 "@finally {\n" 2409 " // something\n" 2410 "}", 2411 Style); 2412 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2413 verifyFormat("try\n" 2414 "{\n" 2415 " // something\n" 2416 "}\n" 2417 "catch (...)\n" 2418 "{\n" 2419 " // something\n" 2420 "}", 2421 Style); 2422 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2423 verifyFormat("try\n" 2424 " {\n" 2425 " // something\n" 2426 " }\n" 2427 "catch (...)\n" 2428 " {\n" 2429 " // something\n" 2430 " }", 2431 Style); 2432 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2433 Style.BraceWrapping.BeforeCatch = true; 2434 verifyFormat("try {\n" 2435 " // something\n" 2436 "}\n" 2437 "catch (...) {\n" 2438 " // something\n" 2439 "}", 2440 Style); 2441 } 2442 2443 TEST_F(FormatTest, FormatObjCTryCatch) { 2444 verifyFormat("@try {\n" 2445 " f();\n" 2446 "} @catch (NSException e) {\n" 2447 " @throw;\n" 2448 "} @finally {\n" 2449 " exit(42);\n" 2450 "}"); 2451 verifyFormat("DEBUG({\n" 2452 " @try {\n" 2453 " } @finally {\n" 2454 " }\n" 2455 "});\n"); 2456 } 2457 2458 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2459 FormatStyle Style = getLLVMStyle(); 2460 verifyFormat("@autoreleasepool {\n" 2461 " f();\n" 2462 "}\n" 2463 "@autoreleasepool {\n" 2464 " f();\n" 2465 "}\n", 2466 Style); 2467 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2468 verifyFormat("@autoreleasepool\n" 2469 "{\n" 2470 " f();\n" 2471 "}\n" 2472 "@autoreleasepool\n" 2473 "{\n" 2474 " f();\n" 2475 "}\n", 2476 Style); 2477 } 2478 2479 TEST_F(FormatTest, StaticInitializers) { 2480 verifyFormat("static SomeClass SC = {1, 'a'};"); 2481 2482 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2483 " 100000000, " 2484 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2485 2486 // Here, everything other than the "}" would fit on a line. 2487 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2488 " 10000000000000000000000000};"); 2489 EXPECT_EQ("S s = {a,\n" 2490 "\n" 2491 " b};", 2492 format("S s = {\n" 2493 " a,\n" 2494 "\n" 2495 " b\n" 2496 "};")); 2497 2498 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2499 // line. However, the formatting looks a bit off and this probably doesn't 2500 // happen often in practice. 2501 verifyFormat("static int Variable[1] = {\n" 2502 " {1000000000000000000000000000000000000}};", 2503 getLLVMStyleWithColumns(40)); 2504 } 2505 2506 TEST_F(FormatTest, DesignatedInitializers) { 2507 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2508 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2509 " .bbbbbbbbbb = 2,\n" 2510 " .cccccccccc = 3,\n" 2511 " .dddddddddd = 4,\n" 2512 " .eeeeeeeeee = 5};"); 2513 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2514 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2515 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2516 " .ccccccccccccccccccccccccccc = 3,\n" 2517 " .ddddddddddddddddddddddddddd = 4,\n" 2518 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2519 2520 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2521 } 2522 2523 TEST_F(FormatTest, NestedStaticInitializers) { 2524 verifyFormat("static A x = {{{}}};\n"); 2525 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2526 " {init1, init2, init3, init4}}};", 2527 getLLVMStyleWithColumns(50)); 2528 2529 verifyFormat("somes Status::global_reps[3] = {\n" 2530 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2531 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2532 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2533 getLLVMStyleWithColumns(60)); 2534 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2535 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2536 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2537 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2538 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2539 " {rect.fRight - rect.fLeft, rect.fBottom - " 2540 "rect.fTop}};"); 2541 2542 verifyFormat( 2543 "SomeArrayOfSomeType a = {\n" 2544 " {{1, 2, 3},\n" 2545 " {1, 2, 3},\n" 2546 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2547 " 333333333333333333333333333333},\n" 2548 " {1, 2, 3},\n" 2549 " {1, 2, 3}}};"); 2550 verifyFormat( 2551 "SomeArrayOfSomeType a = {\n" 2552 " {{1, 2, 3}},\n" 2553 " {{1, 2, 3}},\n" 2554 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2555 " 333333333333333333333333333333}},\n" 2556 " {{1, 2, 3}},\n" 2557 " {{1, 2, 3}}};"); 2558 2559 verifyFormat("struct {\n" 2560 " unsigned bit;\n" 2561 " const char *const name;\n" 2562 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2563 " {kOsWin, \"Windows\"},\n" 2564 " {kOsLinux, \"Linux\"},\n" 2565 " {kOsCrOS, \"Chrome OS\"}};"); 2566 verifyFormat("struct {\n" 2567 " unsigned bit;\n" 2568 " const char *const name;\n" 2569 "} kBitsToOs[] = {\n" 2570 " {kOsMac, \"Mac\"},\n" 2571 " {kOsWin, \"Windows\"},\n" 2572 " {kOsLinux, \"Linux\"},\n" 2573 " {kOsCrOS, \"Chrome OS\"},\n" 2574 "};"); 2575 } 2576 2577 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2578 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2579 " \\\n" 2580 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2581 } 2582 2583 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2584 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2585 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2586 2587 // Do break defaulted and deleted functions. 2588 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2589 " default;", 2590 getLLVMStyleWithColumns(40)); 2591 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2592 " delete;", 2593 getLLVMStyleWithColumns(40)); 2594 } 2595 2596 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2597 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2598 getLLVMStyleWithColumns(40)); 2599 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2600 getLLVMStyleWithColumns(40)); 2601 EXPECT_EQ("#define Q \\\n" 2602 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2603 " \"aaaaaaaa.cpp\"", 2604 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2605 getLLVMStyleWithColumns(40))); 2606 } 2607 2608 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2609 EXPECT_EQ("# 123 \"A string literal\"", 2610 format(" # 123 \"A string literal\"")); 2611 } 2612 2613 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2614 EXPECT_EQ("#;", format("#;")); 2615 verifyFormat("#\n;\n;\n;"); 2616 } 2617 2618 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2619 EXPECT_EQ("#line 42 \"test\"\n", 2620 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2621 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2622 getLLVMStyleWithColumns(12))); 2623 } 2624 2625 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2626 EXPECT_EQ("#line 42 \"test\"", 2627 format("# \\\n line \\\n 42 \\\n \"test\"")); 2628 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2629 } 2630 2631 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2632 verifyFormat("#define A \\x20"); 2633 verifyFormat("#define A \\ x20"); 2634 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2635 verifyFormat("#define A ''"); 2636 verifyFormat("#define A ''qqq"); 2637 verifyFormat("#define A `qqq"); 2638 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2639 EXPECT_EQ("const char *c = STRINGIFY(\n" 2640 "\\na : b);", 2641 format("const char * c = STRINGIFY(\n" 2642 "\\na : b);")); 2643 2644 verifyFormat("a\r\\"); 2645 verifyFormat("a\v\\"); 2646 verifyFormat("a\f\\"); 2647 } 2648 2649 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2650 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2651 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2652 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2653 // FIXME: We never break before the macro name. 2654 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2655 2656 verifyFormat("#define A A\n#define A A"); 2657 verifyFormat("#define A(X) A\n#define A A"); 2658 2659 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2660 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2661 } 2662 2663 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2664 EXPECT_EQ("// somecomment\n" 2665 "#include \"a.h\"\n" 2666 "#define A( \\\n" 2667 " A, B)\n" 2668 "#include \"b.h\"\n" 2669 "// somecomment\n", 2670 format(" // somecomment\n" 2671 " #include \"a.h\"\n" 2672 "#define A(A,\\\n" 2673 " B)\n" 2674 " #include \"b.h\"\n" 2675 " // somecomment\n", 2676 getLLVMStyleWithColumns(13))); 2677 } 2678 2679 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2680 2681 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2682 EXPECT_EQ("#define A \\\n" 2683 " c; \\\n" 2684 " e;\n" 2685 "f;", 2686 format("#define A c; e;\n" 2687 "f;", 2688 getLLVMStyleWithColumns(14))); 2689 } 2690 2691 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2692 2693 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2694 EXPECT_EQ("int x,\n" 2695 "#define A\n" 2696 " y;", 2697 format("int x,\n#define A\ny;")); 2698 } 2699 2700 TEST_F(FormatTest, HashInMacroDefinition) { 2701 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2702 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2703 verifyFormat("#define A \\\n" 2704 " { \\\n" 2705 " f(#c); \\\n" 2706 " }", 2707 getLLVMStyleWithColumns(11)); 2708 2709 verifyFormat("#define A(X) \\\n" 2710 " void function##X()", 2711 getLLVMStyleWithColumns(22)); 2712 2713 verifyFormat("#define A(a, b, c) \\\n" 2714 " void a##b##c()", 2715 getLLVMStyleWithColumns(22)); 2716 2717 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2718 } 2719 2720 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2721 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2722 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2723 } 2724 2725 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2726 EXPECT_EQ("#define A b;", format("#define A \\\n" 2727 " \\\n" 2728 " b;", 2729 getLLVMStyleWithColumns(25))); 2730 EXPECT_EQ("#define A \\\n" 2731 " \\\n" 2732 " a; \\\n" 2733 " b;", 2734 format("#define A \\\n" 2735 " \\\n" 2736 " a; \\\n" 2737 " b;", 2738 getLLVMStyleWithColumns(11))); 2739 EXPECT_EQ("#define A \\\n" 2740 " a; \\\n" 2741 " \\\n" 2742 " b;", 2743 format("#define A \\\n" 2744 " a; \\\n" 2745 " \\\n" 2746 " b;", 2747 getLLVMStyleWithColumns(11))); 2748 } 2749 2750 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2751 verifyIncompleteFormat("#define A :"); 2752 verifyFormat("#define SOMECASES \\\n" 2753 " case 1: \\\n" 2754 " case 2\n", 2755 getLLVMStyleWithColumns(20)); 2756 verifyFormat("#define A template <typename T>"); 2757 verifyIncompleteFormat("#define STR(x) #x\n" 2758 "f(STR(this_is_a_string_literal{));"); 2759 verifyFormat("#pragma omp threadprivate( \\\n" 2760 " y)), // expected-warning", 2761 getLLVMStyleWithColumns(28)); 2762 verifyFormat("#d, = };"); 2763 verifyFormat("#if \"a"); 2764 verifyIncompleteFormat("({\n" 2765 "#define b \\\n" 2766 " } \\\n" 2767 " a\n" 2768 "a", 2769 getLLVMStyleWithColumns(15)); 2770 verifyFormat("#define A \\\n" 2771 " { \\\n" 2772 " {\n" 2773 "#define B \\\n" 2774 " } \\\n" 2775 " }", 2776 getLLVMStyleWithColumns(15)); 2777 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2778 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2779 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2780 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2781 } 2782 2783 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2784 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2785 EXPECT_EQ("class A : public QObject {\n" 2786 " Q_OBJECT\n" 2787 "\n" 2788 " A() {}\n" 2789 "};", 2790 format("class A : public QObject {\n" 2791 " Q_OBJECT\n" 2792 "\n" 2793 " A() {\n}\n" 2794 "} ;")); 2795 EXPECT_EQ("MACRO\n" 2796 "/*static*/ int i;", 2797 format("MACRO\n" 2798 " /*static*/ int i;")); 2799 EXPECT_EQ("SOME_MACRO\n" 2800 "namespace {\n" 2801 "void f();\n" 2802 "}", 2803 format("SOME_MACRO\n" 2804 " namespace {\n" 2805 "void f( );\n" 2806 "}")); 2807 // Only if the identifier contains at least 5 characters. 2808 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2809 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2810 // Only if everything is upper case. 2811 EXPECT_EQ("class A : public QObject {\n" 2812 " Q_Object A() {}\n" 2813 "};", 2814 format("class A : public QObject {\n" 2815 " Q_Object\n" 2816 " A() {\n}\n" 2817 "} ;")); 2818 2819 // Only if the next line can actually start an unwrapped line. 2820 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2821 format("SOME_WEIRD_LOG_MACRO\n" 2822 "<< SomeThing;")); 2823 2824 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2825 "(n, buffers))\n", 2826 getChromiumStyle(FormatStyle::LK_Cpp)); 2827 } 2828 2829 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2830 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2831 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2832 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2833 "class X {};\n" 2834 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2835 "int *createScopDetectionPass() { return 0; }", 2836 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2837 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2838 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2839 " class X {};\n" 2840 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2841 " int *createScopDetectionPass() { return 0; }")); 2842 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2843 // braces, so that inner block is indented one level more. 2844 EXPECT_EQ("int q() {\n" 2845 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2846 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2847 " IPC_END_MESSAGE_MAP()\n" 2848 "}", 2849 format("int q() {\n" 2850 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2851 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2852 " IPC_END_MESSAGE_MAP()\n" 2853 "}")); 2854 2855 // Same inside macros. 2856 EXPECT_EQ("#define LIST(L) \\\n" 2857 " L(A) \\\n" 2858 " L(B) \\\n" 2859 " L(C)", 2860 format("#define LIST(L) \\\n" 2861 " L(A) \\\n" 2862 " L(B) \\\n" 2863 " L(C)", 2864 getGoogleStyle())); 2865 2866 // These must not be recognized as macros. 2867 EXPECT_EQ("int q() {\n" 2868 " f(x);\n" 2869 " f(x) {}\n" 2870 " f(x)->g();\n" 2871 " f(x)->*g();\n" 2872 " f(x).g();\n" 2873 " f(x) = x;\n" 2874 " f(x) += x;\n" 2875 " f(x) -= x;\n" 2876 " f(x) *= x;\n" 2877 " f(x) /= x;\n" 2878 " f(x) %= x;\n" 2879 " f(x) &= x;\n" 2880 " f(x) |= x;\n" 2881 " f(x) ^= x;\n" 2882 " f(x) >>= x;\n" 2883 " f(x) <<= x;\n" 2884 " f(x)[y].z();\n" 2885 " LOG(INFO) << x;\n" 2886 " ifstream(x) >> x;\n" 2887 "}\n", 2888 format("int q() {\n" 2889 " f(x)\n;\n" 2890 " f(x)\n {}\n" 2891 " f(x)\n->g();\n" 2892 " f(x)\n->*g();\n" 2893 " f(x)\n.g();\n" 2894 " f(x)\n = x;\n" 2895 " f(x)\n += x;\n" 2896 " f(x)\n -= x;\n" 2897 " f(x)\n *= x;\n" 2898 " f(x)\n /= x;\n" 2899 " f(x)\n %= x;\n" 2900 " f(x)\n &= x;\n" 2901 " f(x)\n |= x;\n" 2902 " f(x)\n ^= x;\n" 2903 " f(x)\n >>= x;\n" 2904 " f(x)\n <<= x;\n" 2905 " f(x)\n[y].z();\n" 2906 " LOG(INFO)\n << x;\n" 2907 " ifstream(x)\n >> x;\n" 2908 "}\n")); 2909 EXPECT_EQ("int q() {\n" 2910 " F(x)\n" 2911 " if (1) {\n" 2912 " }\n" 2913 " F(x)\n" 2914 " while (1) {\n" 2915 " }\n" 2916 " F(x)\n" 2917 " G(x);\n" 2918 " F(x)\n" 2919 " try {\n" 2920 " Q();\n" 2921 " } catch (...) {\n" 2922 " }\n" 2923 "}\n", 2924 format("int q() {\n" 2925 "F(x)\n" 2926 "if (1) {}\n" 2927 "F(x)\n" 2928 "while (1) {}\n" 2929 "F(x)\n" 2930 "G(x);\n" 2931 "F(x)\n" 2932 "try { Q(); } catch (...) {}\n" 2933 "}\n")); 2934 EXPECT_EQ("class A {\n" 2935 " A() : t(0) {}\n" 2936 " A(int i) noexcept() : {}\n" 2937 " A(X x)\n" // FIXME: function-level try blocks are broken. 2938 " try : t(0) {\n" 2939 " } catch (...) {\n" 2940 " }\n" 2941 "};", 2942 format("class A {\n" 2943 " A()\n : t(0) {}\n" 2944 " A(int i)\n noexcept() : {}\n" 2945 " A(X x)\n" 2946 " try : t(0) {} catch (...) {}\n" 2947 "};")); 2948 EXPECT_EQ("class SomeClass {\n" 2949 "public:\n" 2950 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2951 "};", 2952 format("class SomeClass {\n" 2953 "public:\n" 2954 " SomeClass()\n" 2955 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2956 "};")); 2957 EXPECT_EQ("class SomeClass {\n" 2958 "public:\n" 2959 " SomeClass()\n" 2960 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2961 "};", 2962 format("class SomeClass {\n" 2963 "public:\n" 2964 " SomeClass()\n" 2965 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2966 "};", 2967 getLLVMStyleWithColumns(40))); 2968 2969 verifyFormat("MACRO(>)"); 2970 } 2971 2972 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2973 verifyFormat("#define A \\\n" 2974 " f({ \\\n" 2975 " g(); \\\n" 2976 " });", 2977 getLLVMStyleWithColumns(11)); 2978 } 2979 2980 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2981 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2982 } 2983 2984 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2985 verifyFormat("{\n { a #c; }\n}"); 2986 } 2987 2988 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2989 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2990 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2991 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2992 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2993 } 2994 2995 TEST_F(FormatTest, EscapedNewlines) { 2996 EXPECT_EQ( 2997 "#define A \\\n int i; \\\n int j;", 2998 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2999 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3000 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3001 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3002 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3003 } 3004 3005 TEST_F(FormatTest, DontCrashOnBlockComments) { 3006 EXPECT_EQ( 3007 "int xxxxxxxxx; /* " 3008 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3009 "zzzzzz\n" 3010 "0*/", 3011 format("int xxxxxxxxx; /* " 3012 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3013 "0*/")); 3014 } 3015 3016 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3017 verifyFormat("#define A \\\n" 3018 " int v( \\\n" 3019 " a); \\\n" 3020 " int i;", 3021 getLLVMStyleWithColumns(11)); 3022 } 3023 3024 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3025 EXPECT_EQ( 3026 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3027 " \\\n" 3028 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3029 "\n" 3030 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3031 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3032 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3033 "\\\n" 3034 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3035 " \n" 3036 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3037 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3038 } 3039 3040 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3041 EXPECT_EQ("int\n" 3042 "#define A\n" 3043 " a;", 3044 format("int\n#define A\na;")); 3045 verifyFormat("functionCallTo(\n" 3046 " someOtherFunction(\n" 3047 " withSomeParameters, whichInSequence,\n" 3048 " areLongerThanALine(andAnotherCall,\n" 3049 "#define A B\n" 3050 " withMoreParamters,\n" 3051 " whichStronglyInfluenceTheLayout),\n" 3052 " andMoreParameters),\n" 3053 " trailing);", 3054 getLLVMStyleWithColumns(69)); 3055 verifyFormat("Foo::Foo()\n" 3056 "#ifdef BAR\n" 3057 " : baz(0)\n" 3058 "#endif\n" 3059 "{\n" 3060 "}"); 3061 verifyFormat("void f() {\n" 3062 " if (true)\n" 3063 "#ifdef A\n" 3064 " f(42);\n" 3065 " x();\n" 3066 "#else\n" 3067 " g();\n" 3068 " x();\n" 3069 "#endif\n" 3070 "}"); 3071 verifyFormat("void f(param1, param2,\n" 3072 " param3,\n" 3073 "#ifdef A\n" 3074 " param4(param5,\n" 3075 "#ifdef A1\n" 3076 " param6,\n" 3077 "#ifdef A2\n" 3078 " param7),\n" 3079 "#else\n" 3080 " param8),\n" 3081 " param9,\n" 3082 "#endif\n" 3083 " param10,\n" 3084 "#endif\n" 3085 " param11)\n" 3086 "#else\n" 3087 " param12)\n" 3088 "#endif\n" 3089 "{\n" 3090 " x();\n" 3091 "}", 3092 getLLVMStyleWithColumns(28)); 3093 verifyFormat("#if 1\n" 3094 "int i;"); 3095 verifyFormat("#if 1\n" 3096 "#endif\n" 3097 "#if 1\n" 3098 "#else\n" 3099 "#endif\n"); 3100 verifyFormat("DEBUG({\n" 3101 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3103 "});\n" 3104 "#if a\n" 3105 "#else\n" 3106 "#endif"); 3107 3108 verifyIncompleteFormat("void f(\n" 3109 "#if A\n" 3110 " );\n" 3111 "#else\n" 3112 "#endif"); 3113 } 3114 3115 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3116 verifyFormat("#endif\n" 3117 "#if B"); 3118 } 3119 3120 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3121 FormatStyle SingleLine = getLLVMStyle(); 3122 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3123 verifyFormat("#if 0\n" 3124 "#elif 1\n" 3125 "#endif\n" 3126 "void foo() {\n" 3127 " if (test) foo2();\n" 3128 "}", 3129 SingleLine); 3130 } 3131 3132 TEST_F(FormatTest, LayoutBlockInsideParens) { 3133 verifyFormat("functionCall({ int i; });"); 3134 verifyFormat("functionCall({\n" 3135 " int i;\n" 3136 " int j;\n" 3137 "});"); 3138 verifyFormat("functionCall(\n" 3139 " {\n" 3140 " int i;\n" 3141 " int j;\n" 3142 " },\n" 3143 " aaaa, bbbb, cccc);"); 3144 verifyFormat("functionA(functionB({\n" 3145 " int i;\n" 3146 " int j;\n" 3147 " }),\n" 3148 " aaaa, bbbb, cccc);"); 3149 verifyFormat("functionCall(\n" 3150 " {\n" 3151 " int i;\n" 3152 " int j;\n" 3153 " },\n" 3154 " aaaa, bbbb, // comment\n" 3155 " cccc);"); 3156 verifyFormat("functionA(functionB({\n" 3157 " int i;\n" 3158 " int j;\n" 3159 " }),\n" 3160 " aaaa, bbbb, // comment\n" 3161 " cccc);"); 3162 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3163 verifyFormat("functionCall(aaaa, bbbb, {\n" 3164 " int i;\n" 3165 " int j;\n" 3166 "});"); 3167 verifyFormat( 3168 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3169 " {\n" 3170 " int i; // break\n" 3171 " },\n" 3172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3173 " ccccccccccccccccc));"); 3174 verifyFormat("DEBUG({\n" 3175 " if (a)\n" 3176 " f();\n" 3177 "});"); 3178 } 3179 3180 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3181 EXPECT_EQ("SOME_MACRO { int i; }\n" 3182 "int i;", 3183 format(" SOME_MACRO {int i;} int i;")); 3184 } 3185 3186 TEST_F(FormatTest, LayoutNestedBlocks) { 3187 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3188 " struct s {\n" 3189 " int i;\n" 3190 " };\n" 3191 " s kBitsToOs[] = {{10}};\n" 3192 " for (int i = 0; i < 10; ++i)\n" 3193 " return;\n" 3194 "}"); 3195 verifyFormat("call(parameter, {\n" 3196 " something();\n" 3197 " // Comment using all columns.\n" 3198 " somethingelse();\n" 3199 "});", 3200 getLLVMStyleWithColumns(40)); 3201 verifyFormat("DEBUG( //\n" 3202 " { f(); }, a);"); 3203 verifyFormat("DEBUG( //\n" 3204 " {\n" 3205 " f(); //\n" 3206 " },\n" 3207 " a);"); 3208 3209 EXPECT_EQ("call(parameter, {\n" 3210 " something();\n" 3211 " // Comment too\n" 3212 " // looooooooooong.\n" 3213 " somethingElse();\n" 3214 "});", 3215 format("call(parameter, {\n" 3216 " something();\n" 3217 " // Comment too looooooooooong.\n" 3218 " somethingElse();\n" 3219 "});", 3220 getLLVMStyleWithColumns(29))); 3221 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3222 EXPECT_EQ("DEBUG({ // comment\n" 3223 " int i;\n" 3224 "});", 3225 format("DEBUG({ // comment\n" 3226 "int i;\n" 3227 "});")); 3228 EXPECT_EQ("DEBUG({\n" 3229 " int i;\n" 3230 "\n" 3231 " // comment\n" 3232 " int j;\n" 3233 "});", 3234 format("DEBUG({\n" 3235 " int i;\n" 3236 "\n" 3237 " // comment\n" 3238 " int j;\n" 3239 "});")); 3240 3241 verifyFormat("DEBUG({\n" 3242 " if (a)\n" 3243 " return;\n" 3244 "});"); 3245 verifyGoogleFormat("DEBUG({\n" 3246 " if (a) return;\n" 3247 "});"); 3248 FormatStyle Style = getGoogleStyle(); 3249 Style.ColumnLimit = 45; 3250 verifyFormat("Debug(aaaaa,\n" 3251 " {\n" 3252 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3253 " },\n" 3254 " a);", 3255 Style); 3256 3257 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3258 3259 verifyNoCrash("^{v^{a}}"); 3260 } 3261 3262 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3263 EXPECT_EQ("#define MACRO() \\\n" 3264 " Debug(aaa, /* force line break */ \\\n" 3265 " { \\\n" 3266 " int i; \\\n" 3267 " int j; \\\n" 3268 " })", 3269 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3270 " { int i; int j; })", 3271 getGoogleStyle())); 3272 3273 EXPECT_EQ("#define A \\\n" 3274 " [] { \\\n" 3275 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3276 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3277 " }", 3278 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3279 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3280 getGoogleStyle())); 3281 } 3282 3283 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3284 EXPECT_EQ("{}", format("{}")); 3285 verifyFormat("enum E {};"); 3286 verifyFormat("enum E {}"); 3287 } 3288 3289 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3290 FormatStyle Style = getLLVMStyle(); 3291 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3292 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3293 verifyFormat("FOO_BEGIN\n" 3294 " FOO_ENTRY\n" 3295 "FOO_END", Style); 3296 verifyFormat("FOO_BEGIN\n" 3297 " NESTED_FOO_BEGIN\n" 3298 " NESTED_FOO_ENTRY\n" 3299 " NESTED_FOO_END\n" 3300 "FOO_END", Style); 3301 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3302 " int x;\n" 3303 " x = 1;\n" 3304 "FOO_END(Baz)", Style); 3305 } 3306 3307 //===----------------------------------------------------------------------===// 3308 // Line break tests. 3309 //===----------------------------------------------------------------------===// 3310 3311 TEST_F(FormatTest, PreventConfusingIndents) { 3312 verifyFormat( 3313 "void f() {\n" 3314 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3315 " parameter, parameter, parameter)),\n" 3316 " SecondLongCall(parameter));\n" 3317 "}"); 3318 verifyFormat( 3319 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3320 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3322 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3323 verifyFormat( 3324 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3325 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3326 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3327 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3328 verifyFormat( 3329 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3330 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3332 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3333 verifyFormat("int a = bbbb && ccc && fffff(\n" 3334 "#define A Just forcing a new line\n" 3335 " ddd);"); 3336 } 3337 3338 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3339 verifyFormat( 3340 "bool aaaaaaa =\n" 3341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3342 " bbbbbbbb();"); 3343 verifyFormat( 3344 "bool aaaaaaa =\n" 3345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3346 " bbbbbbbb();"); 3347 3348 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3350 " ccccccccc == ddddddddddd;"); 3351 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3353 " ccccccccc == ddddddddddd;"); 3354 verifyFormat( 3355 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3357 " ccccccccc == ddddddddddd;"); 3358 3359 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3360 " aaaaaa) &&\n" 3361 " bbbbbb && cccccc;"); 3362 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3363 " aaaaaa) >>\n" 3364 " bbbbbb;"); 3365 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3366 " SourceMgr.getSpellingColumnNumber(\n" 3367 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3368 " 1);"); 3369 3370 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3371 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3372 " cccccc) {\n}"); 3373 verifyFormat("b = a &&\n" 3374 " // Comment\n" 3375 " b.c && d;"); 3376 3377 // If the LHS of a comparison is not a binary expression itself, the 3378 // additional linebreak confuses many people. 3379 verifyFormat( 3380 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3382 "}"); 3383 verifyFormat( 3384 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3386 "}"); 3387 verifyFormat( 3388 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3390 "}"); 3391 // Even explicit parentheses stress the precedence enough to make the 3392 // additional break unnecessary. 3393 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3395 "}"); 3396 // This cases is borderline, but with the indentation it is still readable. 3397 verifyFormat( 3398 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3399 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3400 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3401 "}", 3402 getLLVMStyleWithColumns(75)); 3403 3404 // If the LHS is a binary expression, we should still use the additional break 3405 // as otherwise the formatting hides the operator precedence. 3406 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3408 " 5) {\n" 3409 "}"); 3410 3411 FormatStyle OnePerLine = getLLVMStyle(); 3412 OnePerLine.BinPackParameters = false; 3413 verifyFormat( 3414 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3417 OnePerLine); 3418 } 3419 3420 TEST_F(FormatTest, ExpressionIndentation) { 3421 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3425 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3426 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3429 " ccccccccccccccccccccccccccccccccccccccccc;"); 3430 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3433 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3434 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3437 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3438 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3441 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3442 verifyFormat("if () {\n" 3443 "} else if (aaaaa &&\n" 3444 " bbbbb > // break\n" 3445 " ccccc) {\n" 3446 "}"); 3447 3448 // Presence of a trailing comment used to change indentation of b. 3449 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3450 " b;\n" 3451 "return aaaaaaaaaaaaaaaaaaa +\n" 3452 " b; //", 3453 getLLVMStyleWithColumns(30)); 3454 } 3455 3456 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3457 // Not sure what the best system is here. Like this, the LHS can be found 3458 // immediately above an operator (everything with the same or a higher 3459 // indent). The RHS is aligned right of the operator and so compasses 3460 // everything until something with the same indent as the operator is found. 3461 // FIXME: Is this a good system? 3462 FormatStyle Style = getLLVMStyle(); 3463 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3464 verifyFormat( 3465 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3466 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3467 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3468 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3469 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3470 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3471 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3472 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3473 " > ccccccccccccccccccccccccccccccccccccccccc;", 3474 Style); 3475 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3476 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3477 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3478 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3479 Style); 3480 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3481 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3482 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3483 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3484 Style); 3485 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3486 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3487 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3488 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3489 Style); 3490 verifyFormat("if () {\n" 3491 "} else if (aaaaa\n" 3492 " && bbbbb // break\n" 3493 " > ccccc) {\n" 3494 "}", 3495 Style); 3496 verifyFormat("return (a)\n" 3497 " // comment\n" 3498 " + b;", 3499 Style); 3500 verifyFormat( 3501 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3502 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3503 " + cc;", 3504 Style); 3505 3506 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3507 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3508 Style); 3509 3510 // Forced by comments. 3511 verifyFormat( 3512 "unsigned ContentSize =\n" 3513 " sizeof(int16_t) // DWARF ARange version number\n" 3514 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3515 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3516 " + sizeof(int8_t); // Segment Size (in bytes)"); 3517 3518 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3519 " == boost::fusion::at_c<1>(iiii).second;", 3520 Style); 3521 3522 Style.ColumnLimit = 60; 3523 verifyFormat("zzzzzzzzzz\n" 3524 " = bbbbbbbbbbbbbbbbb\n" 3525 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3526 Style); 3527 } 3528 3529 TEST_F(FormatTest, NoOperandAlignment) { 3530 FormatStyle Style = getLLVMStyle(); 3531 Style.AlignOperands = false; 3532 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3533 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3534 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3535 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3537 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3538 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3539 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3540 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3541 " > ccccccccccccccccccccccccccccccccccccccccc;", 3542 Style); 3543 3544 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3545 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3546 " + cc;", 3547 Style); 3548 verifyFormat("int a = aa\n" 3549 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3550 " * cccccccccccccccccccccccccccccccccccc;", 3551 Style); 3552 3553 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3554 verifyFormat("return (a > b\n" 3555 " // comment1\n" 3556 " // comment2\n" 3557 " || c);", 3558 Style); 3559 } 3560 3561 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3562 FormatStyle Style = getLLVMStyle(); 3563 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3564 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3566 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3567 Style); 3568 } 3569 3570 TEST_F(FormatTest, ConstructorInitializers) { 3571 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3572 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3573 getLLVMStyleWithColumns(45)); 3574 verifyFormat("Constructor()\n" 3575 " : Inttializer(FitsOnTheLine) {}", 3576 getLLVMStyleWithColumns(44)); 3577 verifyFormat("Constructor()\n" 3578 " : Inttializer(FitsOnTheLine) {}", 3579 getLLVMStyleWithColumns(43)); 3580 3581 verifyFormat("template <typename T>\n" 3582 "Constructor() : Initializer(FitsOnTheLine) {}", 3583 getLLVMStyleWithColumns(45)); 3584 3585 verifyFormat( 3586 "SomeClass::Constructor()\n" 3587 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3588 3589 verifyFormat( 3590 "SomeClass::Constructor()\n" 3591 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3592 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3593 verifyFormat( 3594 "SomeClass::Constructor()\n" 3595 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3596 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3597 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3598 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3599 " : aaaaaaaaaa(aaaaaa) {}"); 3600 3601 verifyFormat("Constructor()\n" 3602 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3603 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3604 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3605 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3606 3607 verifyFormat("Constructor()\n" 3608 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3610 3611 verifyFormat("Constructor(int Parameter = 0)\n" 3612 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3613 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3614 verifyFormat("Constructor()\n" 3615 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3616 "}", 3617 getLLVMStyleWithColumns(60)); 3618 verifyFormat("Constructor()\n" 3619 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3620 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3621 3622 // Here a line could be saved by splitting the second initializer onto two 3623 // lines, but that is not desirable. 3624 verifyFormat("Constructor()\n" 3625 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3626 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3627 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3628 3629 FormatStyle OnePerLine = getLLVMStyle(); 3630 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3631 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3632 verifyFormat("SomeClass::Constructor()\n" 3633 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3634 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3635 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3636 OnePerLine); 3637 verifyFormat("SomeClass::Constructor()\n" 3638 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3639 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3640 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3641 OnePerLine); 3642 verifyFormat("MyClass::MyClass(int var)\n" 3643 " : some_var_(var), // 4 space indent\n" 3644 " some_other_var_(var + 1) { // lined up\n" 3645 "}", 3646 OnePerLine); 3647 verifyFormat("Constructor()\n" 3648 " : aaaaa(aaaaaa),\n" 3649 " aaaaa(aaaaaa),\n" 3650 " aaaaa(aaaaaa),\n" 3651 " aaaaa(aaaaaa),\n" 3652 " aaaaa(aaaaaa) {}", 3653 OnePerLine); 3654 verifyFormat("Constructor()\n" 3655 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3656 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3657 OnePerLine); 3658 OnePerLine.BinPackParameters = false; 3659 verifyFormat( 3660 "Constructor()\n" 3661 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3662 " aaaaaaaaaaa().aaa(),\n" 3663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3664 OnePerLine); 3665 OnePerLine.ColumnLimit = 60; 3666 verifyFormat("Constructor()\n" 3667 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3668 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3669 OnePerLine); 3670 3671 EXPECT_EQ("Constructor()\n" 3672 " : // Comment forcing unwanted break.\n" 3673 " aaaa(aaaa) {}", 3674 format("Constructor() :\n" 3675 " // Comment forcing unwanted break.\n" 3676 " aaaa(aaaa) {}")); 3677 } 3678 3679 TEST_F(FormatTest, MemoizationTests) { 3680 // This breaks if the memoization lookup does not take \c Indent and 3681 // \c LastSpace into account. 3682 verifyFormat( 3683 "extern CFRunLoopTimerRef\n" 3684 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3685 " CFTimeInterval interval, CFOptionFlags flags,\n" 3686 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3687 " CFRunLoopTimerContext *context) {}"); 3688 3689 // Deep nesting somewhat works around our memoization. 3690 verifyFormat( 3691 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3692 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3693 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3694 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3695 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3696 getLLVMStyleWithColumns(65)); 3697 verifyFormat( 3698 "aaaaa(\n" 3699 " aaaaa,\n" 3700 " aaaaa(\n" 3701 " aaaaa,\n" 3702 " aaaaa(\n" 3703 " aaaaa,\n" 3704 " aaaaa(\n" 3705 " aaaaa,\n" 3706 " aaaaa(\n" 3707 " aaaaa,\n" 3708 " aaaaa(\n" 3709 " aaaaa,\n" 3710 " aaaaa(\n" 3711 " aaaaa,\n" 3712 " aaaaa(\n" 3713 " aaaaa,\n" 3714 " aaaaa(\n" 3715 " aaaaa,\n" 3716 " aaaaa(\n" 3717 " aaaaa,\n" 3718 " aaaaa(\n" 3719 " aaaaa,\n" 3720 " aaaaa(\n" 3721 " aaaaa,\n" 3722 " aaaaa))))))))))));", 3723 getLLVMStyleWithColumns(65)); 3724 verifyFormat( 3725 "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" 3726 " a),\n" 3727 " a),\n" 3728 " a),\n" 3729 " a),\n" 3730 " a),\n" 3731 " a),\n" 3732 " a),\n" 3733 " a),\n" 3734 " a),\n" 3735 " a),\n" 3736 " a),\n" 3737 " a),\n" 3738 " a),\n" 3739 " a),\n" 3740 " a),\n" 3741 " a),\n" 3742 " a)", 3743 getLLVMStyleWithColumns(65)); 3744 3745 // This test takes VERY long when memoization is broken. 3746 FormatStyle OnePerLine = getLLVMStyle(); 3747 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3748 OnePerLine.BinPackParameters = false; 3749 std::string input = "Constructor()\n" 3750 " : aaaa(a,\n"; 3751 for (unsigned i = 0, e = 80; i != e; ++i) { 3752 input += " a,\n"; 3753 } 3754 input += " a) {}"; 3755 verifyFormat(input, OnePerLine); 3756 } 3757 3758 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3759 verifyFormat( 3760 "void f() {\n" 3761 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3762 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3763 " f();\n" 3764 "}"); 3765 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3766 " Intervals[i - 1].getRange().getLast()) {\n}"); 3767 } 3768 3769 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3770 // Principially, we break function declarations in a certain order: 3771 // 1) break amongst arguments. 3772 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3773 " Cccccccccccccc cccccccccccccc);"); 3774 verifyFormat("template <class TemplateIt>\n" 3775 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3776 " TemplateIt *stop) {}"); 3777 3778 // 2) break after return type. 3779 verifyFormat( 3780 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3781 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3782 getGoogleStyle()); 3783 3784 // 3) break after (. 3785 verifyFormat( 3786 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3787 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3788 getGoogleStyle()); 3789 3790 // 4) break before after nested name specifiers. 3791 verifyFormat( 3792 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3793 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3794 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3795 getGoogleStyle()); 3796 3797 // However, there are exceptions, if a sufficient amount of lines can be 3798 // saved. 3799 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3800 // more adjusting. 3801 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3802 " Cccccccccccccc cccccccccc,\n" 3803 " Cccccccccccccc cccccccccc,\n" 3804 " Cccccccccccccc cccccccccc,\n" 3805 " Cccccccccccccc cccccccccc);"); 3806 verifyFormat( 3807 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3808 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3809 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3810 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3811 getGoogleStyle()); 3812 verifyFormat( 3813 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3814 " Cccccccccccccc cccccccccc,\n" 3815 " Cccccccccccccc cccccccccc,\n" 3816 " Cccccccccccccc cccccccccc,\n" 3817 " Cccccccccccccc cccccccccc,\n" 3818 " Cccccccccccccc cccccccccc,\n" 3819 " Cccccccccccccc cccccccccc);"); 3820 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3821 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3822 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3823 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3824 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3825 3826 // Break after multi-line parameters. 3827 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3830 " bbbb bbbb);"); 3831 verifyFormat("void SomeLoooooooooooongFunction(\n" 3832 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3833 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3834 " int bbbbbbbbbbbbb);"); 3835 3836 // Treat overloaded operators like other functions. 3837 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3838 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3839 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3840 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3841 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3842 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3843 verifyGoogleFormat( 3844 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3845 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3846 verifyGoogleFormat( 3847 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3848 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3849 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3850 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3851 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3852 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3853 verifyGoogleFormat( 3854 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3855 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3856 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3857 verifyGoogleFormat( 3858 "template <typename T>\n" 3859 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3860 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3861 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3862 3863 FormatStyle Style = getLLVMStyle(); 3864 Style.PointerAlignment = FormatStyle::PAS_Left; 3865 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3866 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3867 Style); 3868 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3870 Style); 3871 } 3872 3873 TEST_F(FormatTest, TrailingReturnType) { 3874 verifyFormat("auto foo() -> int;\n"); 3875 verifyFormat("struct S {\n" 3876 " auto bar() const -> int;\n" 3877 "};"); 3878 verifyFormat("template <size_t Order, typename T>\n" 3879 "auto load_img(const std::string &filename)\n" 3880 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3881 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3882 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3883 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3884 verifyFormat("template <typename T>\n" 3885 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3886 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3887 3888 // Not trailing return types. 3889 verifyFormat("void f() { auto a = b->c(); }"); 3890 } 3891 3892 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3893 // Avoid breaking before trailing 'const' or other trailing annotations, if 3894 // they are not function-like. 3895 FormatStyle Style = getGoogleStyle(); 3896 Style.ColumnLimit = 47; 3897 verifyFormat("void someLongFunction(\n" 3898 " int someLoooooooooooooongParameter) const {\n}", 3899 getLLVMStyleWithColumns(47)); 3900 verifyFormat("LoooooongReturnType\n" 3901 "someLoooooooongFunction() const {}", 3902 getLLVMStyleWithColumns(47)); 3903 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3904 " const {}", 3905 Style); 3906 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3907 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3908 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3909 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3910 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3911 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3912 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3913 " aaaaaaaaaaa aaaaa) const override;"); 3914 verifyGoogleFormat( 3915 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3916 " const override;"); 3917 3918 // Even if the first parameter has to be wrapped. 3919 verifyFormat("void someLongFunction(\n" 3920 " int someLongParameter) const {}", 3921 getLLVMStyleWithColumns(46)); 3922 verifyFormat("void someLongFunction(\n" 3923 " int someLongParameter) const {}", 3924 Style); 3925 verifyFormat("void someLongFunction(\n" 3926 " int someLongParameter) override {}", 3927 Style); 3928 verifyFormat("void someLongFunction(\n" 3929 " int someLongParameter) OVERRIDE {}", 3930 Style); 3931 verifyFormat("void someLongFunction(\n" 3932 " int someLongParameter) final {}", 3933 Style); 3934 verifyFormat("void someLongFunction(\n" 3935 " int someLongParameter) FINAL {}", 3936 Style); 3937 verifyFormat("void someLongFunction(\n" 3938 " int parameter) const override {}", 3939 Style); 3940 3941 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3942 verifyFormat("void someLongFunction(\n" 3943 " int someLongParameter) const\n" 3944 "{\n" 3945 "}", 3946 Style); 3947 3948 // Unless these are unknown annotations. 3949 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3950 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3951 " LONG_AND_UGLY_ANNOTATION;"); 3952 3953 // Breaking before function-like trailing annotations is fine to keep them 3954 // close to their arguments. 3955 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3956 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3957 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3958 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3959 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3960 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3961 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3962 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3963 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3964 3965 verifyFormat( 3966 "void aaaaaaaaaaaaaaaaaa()\n" 3967 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3968 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3969 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3970 " __attribute__((unused));"); 3971 verifyGoogleFormat( 3972 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3973 " GUARDED_BY(aaaaaaaaaaaa);"); 3974 verifyGoogleFormat( 3975 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3976 " GUARDED_BY(aaaaaaaaaaaa);"); 3977 verifyGoogleFormat( 3978 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3979 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3980 verifyGoogleFormat( 3981 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3982 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3983 } 3984 3985 TEST_F(FormatTest, FunctionAnnotations) { 3986 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3987 "int OldFunction(const string ¶meter) {}"); 3988 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3989 "string OldFunction(const string ¶meter) {}"); 3990 verifyFormat("template <typename T>\n" 3991 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3992 "string OldFunction(const string ¶meter) {}"); 3993 3994 // Not function annotations. 3995 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3996 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3997 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3998 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3999 } 4000 4001 TEST_F(FormatTest, BreaksDesireably) { 4002 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4003 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4004 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4005 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4007 "}"); 4008 4009 verifyFormat( 4010 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4012 4013 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4016 4017 verifyFormat( 4018 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4020 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4022 4023 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4024 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4025 4026 verifyFormat( 4027 "void f() {\n" 4028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4030 "}"); 4031 verifyFormat( 4032 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4034 verifyFormat( 4035 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4037 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4040 4041 // Indent consistently independent of call expression and unary operator. 4042 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4043 " dddddddddddddddddddddddddddddd));"); 4044 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4045 " dddddddddddddddddddddddddddddd));"); 4046 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4047 " dddddddddddddddddddddddddddddd));"); 4048 4049 // This test case breaks on an incorrect memoization, i.e. an optimization not 4050 // taking into account the StopAt value. 4051 verifyFormat( 4052 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4053 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4054 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4055 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4056 4057 verifyFormat("{\n {\n {\n" 4058 " Annotation.SpaceRequiredBefore =\n" 4059 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4060 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4061 " }\n }\n}"); 4062 4063 // Break on an outer level if there was a break on an inner level. 4064 EXPECT_EQ("f(g(h(a, // comment\n" 4065 " b, c),\n" 4066 " d, e),\n" 4067 " x, y);", 4068 format("f(g(h(a, // comment\n" 4069 " b, c), d, e), x, y);")); 4070 4071 // Prefer breaking similar line breaks. 4072 verifyFormat( 4073 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4074 " NSTrackingMouseEnteredAndExited |\n" 4075 " NSTrackingActiveAlways;"); 4076 } 4077 4078 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4079 FormatStyle NoBinPacking = getGoogleStyle(); 4080 NoBinPacking.BinPackParameters = false; 4081 NoBinPacking.BinPackArguments = true; 4082 verifyFormat("void f() {\n" 4083 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4085 "}", 4086 NoBinPacking); 4087 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4088 " int aaaaaaaaaaaaaaaaaaaa,\n" 4089 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4090 NoBinPacking); 4091 4092 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4093 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4094 " vector<int> bbbbbbbbbbbbbbb);", 4095 NoBinPacking); 4096 // FIXME: This behavior difference is probably not wanted. However, currently 4097 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4098 // template arguments from BreakBeforeParameter being set because of the 4099 // one-per-line formatting. 4100 verifyFormat( 4101 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4102 " aaaaaaaaaa> aaaaaaaaaa);", 4103 NoBinPacking); 4104 verifyFormat( 4105 "void fffffffffff(\n" 4106 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4107 " aaaaaaaaaa);"); 4108 } 4109 4110 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4111 FormatStyle NoBinPacking = getGoogleStyle(); 4112 NoBinPacking.BinPackParameters = false; 4113 NoBinPacking.BinPackArguments = false; 4114 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4115 " aaaaaaaaaaaaaaaaaaaa,\n" 4116 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4117 NoBinPacking); 4118 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4119 " aaaaaaaaaaaaa,\n" 4120 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4121 NoBinPacking); 4122 verifyFormat( 4123 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4124 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4126 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4128 NoBinPacking); 4129 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4130 " .aaaaaaaaaaaaaaaaaa();", 4131 NoBinPacking); 4132 verifyFormat("void f() {\n" 4133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4134 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4135 "}", 4136 NoBinPacking); 4137 4138 verifyFormat( 4139 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4140 " aaaaaaaaaaaa,\n" 4141 " aaaaaaaaaaaa);", 4142 NoBinPacking); 4143 verifyFormat( 4144 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4145 " ddddddddddddddddddddddddddddd),\n" 4146 " test);", 4147 NoBinPacking); 4148 4149 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4150 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4151 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4152 " aaaaaaaaaaaaaaaaaa;", 4153 NoBinPacking); 4154 verifyFormat("a(\"a\"\n" 4155 " \"a\",\n" 4156 " a);"); 4157 4158 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4159 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4160 " aaaaaaaaa,\n" 4161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4162 NoBinPacking); 4163 verifyFormat( 4164 "void f() {\n" 4165 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4166 " .aaaaaaa();\n" 4167 "}", 4168 NoBinPacking); 4169 verifyFormat( 4170 "template <class SomeType, class SomeOtherType>\n" 4171 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4172 NoBinPacking); 4173 } 4174 4175 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4176 FormatStyle Style = getLLVMStyleWithColumns(15); 4177 Style.ExperimentalAutoDetectBinPacking = true; 4178 EXPECT_EQ("aaa(aaaa,\n" 4179 " aaaa,\n" 4180 " aaaa);\n" 4181 "aaa(aaaa,\n" 4182 " aaaa,\n" 4183 " aaaa);", 4184 format("aaa(aaaa,\n" // one-per-line 4185 " aaaa,\n" 4186 " aaaa );\n" 4187 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4188 Style)); 4189 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4190 " aaaa);\n" 4191 "aaa(aaaa, aaaa,\n" 4192 " aaaa);", 4193 format("aaa(aaaa, aaaa,\n" // bin-packed 4194 " aaaa );\n" 4195 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4196 Style)); 4197 } 4198 4199 TEST_F(FormatTest, FormatsBuilderPattern) { 4200 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4201 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4202 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4203 " .StartsWith(\".init\", ORDER_INIT)\n" 4204 " .StartsWith(\".fini\", ORDER_FINI)\n" 4205 " .StartsWith(\".hash\", ORDER_HASH)\n" 4206 " .Default(ORDER_TEXT);\n"); 4207 4208 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4209 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4210 verifyFormat( 4211 "aaaaaaa->aaaaaaa\n" 4212 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4214 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4215 verifyFormat( 4216 "aaaaaaa->aaaaaaa\n" 4217 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4218 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4219 verifyFormat( 4220 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4221 " aaaaaaaaaaaaaa);"); 4222 verifyFormat( 4223 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4224 " aaaaaa->aaaaaaaaaaaa()\n" 4225 " ->aaaaaaaaaaaaaaaa(\n" 4226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4227 " ->aaaaaaaaaaaaaaaaa();"); 4228 verifyGoogleFormat( 4229 "void f() {\n" 4230 " someo->Add((new util::filetools::Handler(dir))\n" 4231 " ->OnEvent1(NewPermanentCallback(\n" 4232 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4233 " ->OnEvent2(NewPermanentCallback(\n" 4234 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4235 " ->OnEvent3(NewPermanentCallback(\n" 4236 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4237 " ->OnEvent5(NewPermanentCallback(\n" 4238 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4239 " ->OnEvent6(NewPermanentCallback(\n" 4240 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4241 "}"); 4242 4243 verifyFormat( 4244 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4245 verifyFormat("aaaaaaaaaaaaaaa()\n" 4246 " .aaaaaaaaaaaaaaa()\n" 4247 " .aaaaaaaaaaaaaaa()\n" 4248 " .aaaaaaaaaaaaaaa()\n" 4249 " .aaaaaaaaaaaaaaa();"); 4250 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4251 " .aaaaaaaaaaaaaaa()\n" 4252 " .aaaaaaaaaaaaaaa()\n" 4253 " .aaaaaaaaaaaaaaa();"); 4254 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4255 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4256 " .aaaaaaaaaaaaaaa();"); 4257 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4258 " ->aaaaaaaaaaaaaae(0)\n" 4259 " ->aaaaaaaaaaaaaaa();"); 4260 4261 // Don't linewrap after very short segments. 4262 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4263 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4264 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4265 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4266 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4267 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4268 verifyFormat("aaa()\n" 4269 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4270 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4271 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4272 4273 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4274 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4275 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4276 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4277 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4279 4280 // Prefer not to break after empty parentheses. 4281 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4282 " First->LastNewlineOffset);"); 4283 4284 // Prefer not to create "hanging" indents. 4285 verifyFormat( 4286 "return !soooooooooooooome_map\n" 4287 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4288 " .second;"); 4289 verifyFormat( 4290 "return aaaaaaaaaaaaaaaa\n" 4291 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4292 " .aaaa(aaaaaaaaaaaaaa);"); 4293 // No hanging indent here. 4294 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4296 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4298 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4299 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4300 getLLVMStyleWithColumns(60)); 4301 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4302 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4303 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4304 getLLVMStyleWithColumns(59)); 4305 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4307 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4308 } 4309 4310 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4311 verifyFormat( 4312 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4313 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4314 verifyFormat( 4315 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4316 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4317 4318 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4319 " ccccccccccccccccccccccccc) {\n}"); 4320 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4321 " ccccccccccccccccccccccccc) {\n}"); 4322 4323 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4324 " ccccccccccccccccccccccccc) {\n}"); 4325 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4326 " ccccccccccccccccccccccccc) {\n}"); 4327 4328 verifyFormat( 4329 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4330 " ccccccccccccccccccccccccc) {\n}"); 4331 verifyFormat( 4332 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4333 " ccccccccccccccccccccccccc) {\n}"); 4334 4335 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4336 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4337 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4338 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4339 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4340 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4341 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4342 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4343 4344 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4345 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4346 " aaaaaaaaaaaaaaa != aa) {\n}"); 4347 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4348 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4349 " aaaaaaaaaaaaaaa != aa) {\n}"); 4350 } 4351 4352 TEST_F(FormatTest, BreaksAfterAssignments) { 4353 verifyFormat( 4354 "unsigned Cost =\n" 4355 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4356 " SI->getPointerAddressSpaceee());\n"); 4357 verifyFormat( 4358 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4359 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4360 4361 verifyFormat( 4362 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4363 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4364 verifyFormat("unsigned OriginalStartColumn =\n" 4365 " SourceMgr.getSpellingColumnNumber(\n" 4366 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4367 " 1;"); 4368 } 4369 4370 TEST_F(FormatTest, AlignsAfterAssignments) { 4371 verifyFormat( 4372 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4373 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4374 verifyFormat( 4375 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4376 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4377 verifyFormat( 4378 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4379 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4380 verifyFormat( 4381 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4382 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4383 verifyFormat( 4384 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4385 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4387 } 4388 4389 TEST_F(FormatTest, AlignsAfterReturn) { 4390 verifyFormat( 4391 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4392 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4393 verifyFormat( 4394 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4395 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4396 verifyFormat( 4397 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4398 " aaaaaaaaaaaaaaaaaaaaaa();"); 4399 verifyFormat( 4400 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4401 " aaaaaaaaaaaaaaaaaaaaaa());"); 4402 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4404 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4407 verifyFormat("return\n" 4408 " // true if code is one of a or b.\n" 4409 " code == a || code == b;"); 4410 } 4411 4412 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4413 verifyFormat( 4414 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4415 " aaaaaaaaa aaaaaaa) {}"); 4416 verifyFormat( 4417 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4418 " aaaaaaaaaaa aaaaaaaaa);"); 4419 verifyFormat( 4420 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4421 " aaaaaaaaaaaaaaaaaaaaa));"); 4422 FormatStyle Style = getLLVMStyle(); 4423 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4424 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4425 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4426 Style); 4427 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4428 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4429 Style); 4430 verifyFormat("SomeLongVariableName->someFunction(\n" 4431 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4432 Style); 4433 verifyFormat( 4434 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4435 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4436 Style); 4437 verifyFormat( 4438 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4439 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4440 Style); 4441 verifyFormat( 4442 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4443 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4444 Style); 4445 4446 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4447 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4448 " b));", 4449 Style); 4450 4451 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4452 Style.BinPackArguments = false; 4453 Style.BinPackParameters = false; 4454 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4455 " aaaaaaaaaaa aaaaaaaa,\n" 4456 " aaaaaaaaa aaaaaaa,\n" 4457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4458 Style); 4459 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4460 " aaaaaaaaaaa aaaaaaaaa,\n" 4461 " aaaaaaaaaaa aaaaaaaaa,\n" 4462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4463 Style); 4464 verifyFormat("SomeLongVariableName->someFunction(\n" 4465 " foooooooo(\n" 4466 " aaaaaaaaaaaaaaa,\n" 4467 " aaaaaaaaaaaaaaaaaaaaa,\n" 4468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4469 Style); 4470 } 4471 4472 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4473 FormatStyle Style = getLLVMStyleWithColumns(40); 4474 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4475 " bbbbbbbbbbbbbbbbbbbbbb);", 4476 Style); 4477 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4478 Style.AlignOperands = false; 4479 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4480 " bbbbbbbbbbbbbbbbbbbbbb);", 4481 Style); 4482 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4483 Style.AlignOperands = true; 4484 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4485 " bbbbbbbbbbbbbbbbbbbbbb);", 4486 Style); 4487 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4488 Style.AlignOperands = false; 4489 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4490 " bbbbbbbbbbbbbbbbbbbbbb);", 4491 Style); 4492 } 4493 4494 TEST_F(FormatTest, BreaksConditionalExpressions) { 4495 verifyFormat( 4496 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4497 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4498 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4499 verifyFormat( 4500 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4501 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4502 verifyFormat( 4503 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4504 " : aaaaaaaaaaaaa);"); 4505 verifyFormat( 4506 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4507 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4508 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4509 " aaaaaaaaaaaaa);"); 4510 verifyFormat( 4511 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4512 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4513 " aaaaaaaaaaaaa);"); 4514 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4515 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4517 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4519 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4521 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4523 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4525 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4526 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4528 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4530 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4531 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4532 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4533 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4534 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4536 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4537 " : aaaaaaaaaaaaaaaa;"); 4538 verifyFormat( 4539 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4540 " ? aaaaaaaaaaaaaaa\n" 4541 " : aaaaaaaaaaaaaaa;"); 4542 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4543 " aaaaaaaaa\n" 4544 " ? b\n" 4545 " : c);"); 4546 verifyFormat("return aaaa == bbbb\n" 4547 " // comment\n" 4548 " ? aaaa\n" 4549 " : bbbb;"); 4550 verifyFormat("unsigned Indent =\n" 4551 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4552 " ? IndentForLevel[TheLine.Level]\n" 4553 " : TheLine * 2,\n" 4554 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4555 getLLVMStyleWithColumns(70)); 4556 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4557 " ? aaaaaaaaaaaaaaa\n" 4558 " : bbbbbbbbbbbbbbb //\n" 4559 " ? ccccccccccccccc\n" 4560 " : ddddddddddddddd;"); 4561 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4562 " ? aaaaaaaaaaaaaaa\n" 4563 " : (bbbbbbbbbbbbbbb //\n" 4564 " ? ccccccccccccccc\n" 4565 " : ddddddddddddddd);"); 4566 verifyFormat( 4567 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4568 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4569 " aaaaaaaaaaaaaaaaaaaaa +\n" 4570 " aaaaaaaaaaaaaaaaaaaaa\n" 4571 " : aaaaaaaaaa;"); 4572 verifyFormat( 4573 "aaaaaa = aaaaaaaaaaaa\n" 4574 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4575 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4576 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4577 4578 FormatStyle NoBinPacking = getLLVMStyle(); 4579 NoBinPacking.BinPackArguments = false; 4580 verifyFormat( 4581 "void f() {\n" 4582 " g(aaa,\n" 4583 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4585 " ? aaaaaaaaaaaaaaa\n" 4586 " : aaaaaaaaaaaaaaa);\n" 4587 "}", 4588 NoBinPacking); 4589 verifyFormat( 4590 "void f() {\n" 4591 " g(aaa,\n" 4592 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4594 " ?: aaaaaaaaaaaaaaa);\n" 4595 "}", 4596 NoBinPacking); 4597 4598 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4599 " // comment.\n" 4600 " ccccccccccccccccccccccccccccccccccccccc\n" 4601 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4602 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4603 4604 // Assignments in conditional expressions. Apparently not uncommon :-(. 4605 verifyFormat("return a != b\n" 4606 " // comment\n" 4607 " ? a = b\n" 4608 " : a = b;"); 4609 verifyFormat("return a != b\n" 4610 " // comment\n" 4611 " ? a = a != b\n" 4612 " // comment\n" 4613 " ? a = b\n" 4614 " : a\n" 4615 " : a;\n"); 4616 verifyFormat("return a != b\n" 4617 " // comment\n" 4618 " ? a\n" 4619 " : a = a != b\n" 4620 " // comment\n" 4621 " ? a = b\n" 4622 " : a;"); 4623 } 4624 4625 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4626 FormatStyle Style = getLLVMStyle(); 4627 Style.BreakBeforeTernaryOperators = false; 4628 Style.ColumnLimit = 70; 4629 verifyFormat( 4630 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4633 Style); 4634 verifyFormat( 4635 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4637 Style); 4638 verifyFormat( 4639 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4640 " aaaaaaaaaaaaa);", 4641 Style); 4642 verifyFormat( 4643 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4644 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4646 " aaaaaaaaaaaaa);", 4647 Style); 4648 verifyFormat( 4649 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4650 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4651 " aaaaaaaaaaaaa);", 4652 Style); 4653 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4658 Style); 4659 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4665 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4666 Style); 4667 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4669 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4671 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4672 Style); 4673 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4674 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4675 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4676 Style); 4677 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4681 Style); 4682 verifyFormat( 4683 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4684 " aaaaaaaaaaaaaaa :\n" 4685 " aaaaaaaaaaaaaaa;", 4686 Style); 4687 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4688 " aaaaaaaaa ?\n" 4689 " b :\n" 4690 " c);", 4691 Style); 4692 verifyFormat( 4693 "unsigned Indent =\n" 4694 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4695 " IndentForLevel[TheLine.Level] :\n" 4696 " TheLine * 2,\n" 4697 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4698 Style); 4699 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4700 " aaaaaaaaaaaaaaa :\n" 4701 " bbbbbbbbbbbbbbb ? //\n" 4702 " ccccccccccccccc :\n" 4703 " ddddddddddddddd;", 4704 Style); 4705 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4706 " aaaaaaaaaaaaaaa :\n" 4707 " (bbbbbbbbbbbbbbb ? //\n" 4708 " ccccccccccccccc :\n" 4709 " ddddddddddddddd);", 4710 Style); 4711 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4712 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4713 " ccccccccccccccccccccccccccc;", 4714 Style); 4715 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4716 " aaaaa :\n" 4717 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4718 Style); 4719 } 4720 4721 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4722 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4723 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4724 verifyFormat("bool a = true, b = false;"); 4725 4726 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4728 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4729 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4730 verifyFormat( 4731 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4732 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4733 " d = e && f;"); 4734 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4735 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4736 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4737 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4738 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4739 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4740 4741 FormatStyle Style = getGoogleStyle(); 4742 Style.PointerAlignment = FormatStyle::PAS_Left; 4743 Style.DerivePointerAlignment = false; 4744 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4745 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4746 " *b = bbbbbbbbbbbbbbbbbbb;", 4747 Style); 4748 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4749 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4750 Style); 4751 } 4752 4753 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4754 verifyFormat("arr[foo ? bar : baz];"); 4755 verifyFormat("f()[foo ? bar : baz];"); 4756 verifyFormat("(a + b)[foo ? bar : baz];"); 4757 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4758 } 4759 4760 TEST_F(FormatTest, AlignsStringLiterals) { 4761 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4762 " \"short literal\");"); 4763 verifyFormat( 4764 "looooooooooooooooooooooooongFunction(\n" 4765 " \"short literal\"\n" 4766 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4767 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4768 " \" string literals\",\n" 4769 " and, other, parameters);"); 4770 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4771 " \"5678\";", 4772 format("fun + \"1243\" /* comment */\n" 4773 " \"5678\";", 4774 getLLVMStyleWithColumns(28))); 4775 EXPECT_EQ( 4776 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4777 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4778 " \"aaaaaaaaaaaaaaaa\";", 4779 format("aaaaaa =" 4780 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4781 "aaaaaaaaaaaaaaaaaaaaa\" " 4782 "\"aaaaaaaaaaaaaaaa\";")); 4783 verifyFormat("a = a + \"a\"\n" 4784 " \"a\"\n" 4785 " \"a\";"); 4786 verifyFormat("f(\"a\", \"b\"\n" 4787 " \"c\");"); 4788 4789 verifyFormat( 4790 "#define LL_FORMAT \"ll\"\n" 4791 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4792 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4793 4794 verifyFormat("#define A(X) \\\n" 4795 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4796 " \"ccccc\"", 4797 getLLVMStyleWithColumns(23)); 4798 verifyFormat("#define A \"def\"\n" 4799 "f(\"abc\" A \"ghi\"\n" 4800 " \"jkl\");"); 4801 4802 verifyFormat("f(L\"a\"\n" 4803 " L\"b\");"); 4804 verifyFormat("#define A(X) \\\n" 4805 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4806 " L\"ccccc\"", 4807 getLLVMStyleWithColumns(25)); 4808 4809 verifyFormat("f(@\"a\"\n" 4810 " @\"b\");"); 4811 verifyFormat("NSString s = @\"a\"\n" 4812 " @\"b\"\n" 4813 " @\"c\";"); 4814 verifyFormat("NSString s = @\"a\"\n" 4815 " \"b\"\n" 4816 " \"c\";"); 4817 } 4818 4819 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4820 FormatStyle Style = getLLVMStyle(); 4821 // No declarations or definitions should be moved to own line. 4822 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4823 verifyFormat("class A {\n" 4824 " int f() { return 1; }\n" 4825 " int g();\n" 4826 "};\n" 4827 "int f() { return 1; }\n" 4828 "int g();\n", 4829 Style); 4830 4831 // All declarations and definitions should have the return type moved to its 4832 // own 4833 // line. 4834 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4835 verifyFormat("class E {\n" 4836 " int\n" 4837 " f() {\n" 4838 " return 1;\n" 4839 " }\n" 4840 " int\n" 4841 " g();\n" 4842 "};\n" 4843 "int\n" 4844 "f() {\n" 4845 " return 1;\n" 4846 "}\n" 4847 "int\n" 4848 "g();\n", 4849 Style); 4850 4851 // Top-level definitions, and no kinds of declarations should have the 4852 // return type moved to its own line. 4853 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4854 verifyFormat("class B {\n" 4855 " int f() { return 1; }\n" 4856 " int g();\n" 4857 "};\n" 4858 "int\n" 4859 "f() {\n" 4860 " return 1;\n" 4861 "}\n" 4862 "int g();\n", 4863 Style); 4864 4865 // Top-level definitions and declarations should have the return type moved 4866 // to its own line. 4867 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4868 verifyFormat("class C {\n" 4869 " int f() { return 1; }\n" 4870 " int g();\n" 4871 "};\n" 4872 "int\n" 4873 "f() {\n" 4874 " return 1;\n" 4875 "}\n" 4876 "int\n" 4877 "g();\n", 4878 Style); 4879 4880 // All definitions should have the return type moved to its own line, but no 4881 // kinds of declarations. 4882 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4883 verifyFormat("class D {\n" 4884 " int\n" 4885 " f() {\n" 4886 " return 1;\n" 4887 " }\n" 4888 " int g();\n" 4889 "};\n" 4890 "int\n" 4891 "f() {\n" 4892 " return 1;\n" 4893 "}\n" 4894 "int g();\n", 4895 Style); 4896 verifyFormat("const char *\n" 4897 "f(void) {\n" // Break here. 4898 " return \"\";\n" 4899 "}\n" 4900 "const char *bar(void);\n", // No break here. 4901 Style); 4902 verifyFormat("template <class T>\n" 4903 "T *\n" 4904 "f(T &c) {\n" // Break here. 4905 " return NULL;\n" 4906 "}\n" 4907 "template <class T> T *f(T &c);\n", // No break here. 4908 Style); 4909 verifyFormat("class C {\n" 4910 " int\n" 4911 " operator+() {\n" 4912 " return 1;\n" 4913 " }\n" 4914 " int\n" 4915 " operator()() {\n" 4916 " return 1;\n" 4917 " }\n" 4918 "};\n", 4919 Style); 4920 verifyFormat("void\n" 4921 "A::operator()() {}\n" 4922 "void\n" 4923 "A::operator>>() {}\n" 4924 "void\n" 4925 "A::operator+() {}\n", 4926 Style); 4927 verifyFormat("void *operator new(std::size_t s);", // No break here. 4928 Style); 4929 verifyFormat("void *\n" 4930 "operator new(std::size_t s) {}", 4931 Style); 4932 verifyFormat("void *\n" 4933 "operator delete[](void *ptr) {}", 4934 Style); 4935 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4936 verifyFormat("const char *\n" 4937 "f(void)\n" // Break here. 4938 "{\n" 4939 " return \"\";\n" 4940 "}\n" 4941 "const char *bar(void);\n", // No break here. 4942 Style); 4943 verifyFormat("template <class T>\n" 4944 "T *\n" // Problem here: no line break 4945 "f(T &c)\n" // Break here. 4946 "{\n" 4947 " return NULL;\n" 4948 "}\n" 4949 "template <class T> T *f(T &c);\n", // No break here. 4950 Style); 4951 } 4952 4953 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4954 FormatStyle NoBreak = getLLVMStyle(); 4955 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4956 FormatStyle Break = getLLVMStyle(); 4957 Break.AlwaysBreakBeforeMultilineStrings = true; 4958 verifyFormat("aaaa = \"bbbb\"\n" 4959 " \"cccc\";", 4960 NoBreak); 4961 verifyFormat("aaaa =\n" 4962 " \"bbbb\"\n" 4963 " \"cccc\";", 4964 Break); 4965 verifyFormat("aaaa(\"bbbb\"\n" 4966 " \"cccc\");", 4967 NoBreak); 4968 verifyFormat("aaaa(\n" 4969 " \"bbbb\"\n" 4970 " \"cccc\");", 4971 Break); 4972 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4973 " \"cccc\");", 4974 NoBreak); 4975 verifyFormat("aaaa(qqq,\n" 4976 " \"bbbb\"\n" 4977 " \"cccc\");", 4978 Break); 4979 verifyFormat("aaaa(qqq,\n" 4980 " L\"bbbb\"\n" 4981 " L\"cccc\");", 4982 Break); 4983 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4984 " \"bbbb\"));", 4985 Break); 4986 verifyFormat("string s = someFunction(\n" 4987 " \"abc\"\n" 4988 " \"abc\");", 4989 Break); 4990 4991 // As we break before unary operators, breaking right after them is bad. 4992 verifyFormat("string foo = abc ? \"x\"\n" 4993 " \"blah blah blah blah blah blah\"\n" 4994 " : \"y\";", 4995 Break); 4996 4997 // Don't break if there is no column gain. 4998 verifyFormat("f(\"aaaa\"\n" 4999 " \"bbbb\");", 5000 Break); 5001 5002 // Treat literals with escaped newlines like multi-line string literals. 5003 EXPECT_EQ("x = \"a\\\n" 5004 "b\\\n" 5005 "c\";", 5006 format("x = \"a\\\n" 5007 "b\\\n" 5008 "c\";", 5009 NoBreak)); 5010 EXPECT_EQ("xxxx =\n" 5011 " \"a\\\n" 5012 "b\\\n" 5013 "c\";", 5014 format("xxxx = \"a\\\n" 5015 "b\\\n" 5016 "c\";", 5017 Break)); 5018 5019 // Exempt ObjC strings for now. 5020 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5021 " @\"bbbb\";", 5022 format("NSString *const kString = @\"aaaa\"\n" 5023 "@\"bbbb\";", 5024 Break)); 5025 5026 Break.ColumnLimit = 0; 5027 verifyFormat("const char *hello = \"hello llvm\";", Break); 5028 } 5029 5030 TEST_F(FormatTest, AlignsPipes) { 5031 verifyFormat( 5032 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5033 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5034 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5035 verifyFormat( 5036 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5037 " << aaaaaaaaaaaaaaaaaaaa;"); 5038 verifyFormat( 5039 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5040 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5041 verifyFormat( 5042 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5043 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5044 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5045 verifyFormat( 5046 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5047 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5048 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5049 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5052 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5053 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5054 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5055 verifyFormat( 5056 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5058 5059 verifyFormat("return out << \"somepacket = {\\n\"\n" 5060 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5061 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5062 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5063 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5064 " << \"}\";"); 5065 5066 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5067 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5068 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5069 verifyFormat( 5070 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5071 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5072 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5073 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5074 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5075 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5076 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5077 verifyFormat( 5078 "void f() {\n" 5079 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5080 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5081 "}"); 5082 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5083 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5084 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5086 " aaaaaaaaaaaaaaaaaaaaa)\n" 5087 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5088 verifyFormat("LOG_IF(aaa == //\n" 5089 " bbb)\n" 5090 " << a << b;"); 5091 5092 // Breaking before the first "<<" is generally not desirable. 5093 verifyFormat( 5094 "llvm::errs()\n" 5095 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5096 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5097 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5098 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5099 getLLVMStyleWithColumns(70)); 5100 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5101 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5102 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5103 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5104 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5105 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5106 getLLVMStyleWithColumns(70)); 5107 5108 // But sometimes, breaking before the first "<<" is desirable. 5109 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5110 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5111 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5112 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5113 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5114 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5115 " << BEF << IsTemplate << Description << E->getType();"); 5116 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5117 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5119 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5120 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5122 " << aaa;"); 5123 5124 verifyFormat( 5125 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5126 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5127 5128 // Incomplete string literal. 5129 EXPECT_EQ("llvm::errs() << \"\n" 5130 " << a;", 5131 format("llvm::errs() << \"\n<<a;")); 5132 5133 verifyFormat("void f() {\n" 5134 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5135 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5136 "}"); 5137 5138 // Handle 'endl'. 5139 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5140 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5141 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5142 5143 // Handle '\n'. 5144 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5145 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5146 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5147 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5148 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5149 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5150 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5151 } 5152 5153 TEST_F(FormatTest, UnderstandsEquals) { 5154 verifyFormat( 5155 "aaaaaaaaaaaaaaaaa =\n" 5156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5157 verifyFormat( 5158 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5160 verifyFormat( 5161 "if (a) {\n" 5162 " f();\n" 5163 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5165 "}"); 5166 5167 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5168 " 100000000 + 10000000) {\n}"); 5169 } 5170 5171 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5172 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5173 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5174 5175 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5176 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5177 5178 verifyFormat( 5179 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5180 " Parameter2);"); 5181 5182 verifyFormat( 5183 "ShortObject->shortFunction(\n" 5184 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5185 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5186 5187 verifyFormat("loooooooooooooongFunction(\n" 5188 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5189 5190 verifyFormat( 5191 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5192 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5193 5194 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5195 " .WillRepeatedly(Return(SomeValue));"); 5196 verifyFormat("void f() {\n" 5197 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5198 " .Times(2)\n" 5199 " .WillRepeatedly(Return(SomeValue));\n" 5200 "}"); 5201 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5202 " ccccccccccccccccccccccc);"); 5203 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5205 " .aaaaa(aaaaa),\n" 5206 " aaaaaaaaaaaaaaaaaaaaa);"); 5207 verifyFormat("void f() {\n" 5208 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5209 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5210 "}"); 5211 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5213 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5214 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5215 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5216 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5217 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5218 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5219 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5220 "}"); 5221 5222 // Here, it is not necessary to wrap at "." or "->". 5223 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5224 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5225 verifyFormat( 5226 "aaaaaaaaaaa->aaaaaaaaa(\n" 5227 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5228 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5229 5230 verifyFormat( 5231 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5233 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5234 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5235 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5236 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5237 5238 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5240 " .a();"); 5241 5242 FormatStyle NoBinPacking = getLLVMStyle(); 5243 NoBinPacking.BinPackParameters = false; 5244 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5245 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5246 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5247 " aaaaaaaaaaaaaaaaaaa,\n" 5248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5249 NoBinPacking); 5250 5251 // If there is a subsequent call, change to hanging indentation. 5252 verifyFormat( 5253 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5254 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5255 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5256 verifyFormat( 5257 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5258 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5259 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5261 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5262 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5264 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5265 } 5266 5267 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5268 verifyFormat("template <typename T>\n" 5269 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5270 verifyFormat("template <typename T>\n" 5271 "// T should be one of {A, B}.\n" 5272 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5273 verifyFormat( 5274 "template <typename T>\n" 5275 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5276 verifyFormat("template <typename T>\n" 5277 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5278 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5279 verifyFormat( 5280 "template <typename T>\n" 5281 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5282 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5283 verifyFormat( 5284 "template <typename T>\n" 5285 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5286 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5288 verifyFormat("template <typename T>\n" 5289 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5290 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5291 verifyFormat( 5292 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5293 " typename T4 = char>\n" 5294 "void f();"); 5295 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5296 " template <typename> class cccccccccccccccccccccc,\n" 5297 " typename ddddddddddddd>\n" 5298 "class C {};"); 5299 verifyFormat( 5300 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5301 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5302 5303 verifyFormat("void f() {\n" 5304 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5305 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5306 "}"); 5307 5308 verifyFormat("template <typename T> class C {};"); 5309 verifyFormat("template <typename T> void f();"); 5310 verifyFormat("template <typename T> void f() {}"); 5311 verifyFormat( 5312 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5315 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5318 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5319 getLLVMStyleWithColumns(72)); 5320 EXPECT_EQ("static_cast<A< //\n" 5321 " B> *>(\n" 5322 "\n" 5323 " );", 5324 format("static_cast<A<//\n" 5325 " B>*>(\n" 5326 "\n" 5327 " );")); 5328 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5329 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5330 5331 FormatStyle AlwaysBreak = getLLVMStyle(); 5332 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5333 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5334 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5335 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5336 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5337 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5338 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5339 verifyFormat("template <template <typename> class Fooooooo,\n" 5340 " template <typename> class Baaaaaaar>\n" 5341 "struct C {};", 5342 AlwaysBreak); 5343 verifyFormat("template <typename T> // T can be A, B or C.\n" 5344 "struct C {};", 5345 AlwaysBreak); 5346 } 5347 5348 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5349 verifyFormat( 5350 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5352 verifyFormat( 5353 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5356 5357 // FIXME: Should we have the extra indent after the second break? 5358 verifyFormat( 5359 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5362 5363 verifyFormat( 5364 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5365 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5366 5367 // Breaking at nested name specifiers is generally not desirable. 5368 verifyFormat( 5369 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5370 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5371 5372 verifyFormat( 5373 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5374 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5375 " aaaaaaaaaaaaaaaaaaaaa);", 5376 getLLVMStyleWithColumns(74)); 5377 5378 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5380 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5381 } 5382 5383 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5384 verifyFormat("A<int> a;"); 5385 verifyFormat("A<A<A<int>>> a;"); 5386 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5387 verifyFormat("bool x = a < 1 || 2 > a;"); 5388 verifyFormat("bool x = 5 < f<int>();"); 5389 verifyFormat("bool x = f<int>() > 5;"); 5390 verifyFormat("bool x = 5 < a<int>::x;"); 5391 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5392 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5393 5394 verifyGoogleFormat("A<A<int>> a;"); 5395 verifyGoogleFormat("A<A<A<int>>> a;"); 5396 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5397 verifyGoogleFormat("A<A<int> > a;"); 5398 verifyGoogleFormat("A<A<A<int> > > a;"); 5399 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5400 verifyGoogleFormat("A<::A<int>> a;"); 5401 verifyGoogleFormat("A<::A> a;"); 5402 verifyGoogleFormat("A< ::A> a;"); 5403 verifyGoogleFormat("A< ::A<int> > a;"); 5404 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5405 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5406 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5407 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5408 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5409 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5410 5411 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5412 5413 verifyFormat("test >> a >> b;"); 5414 verifyFormat("test << a >> b;"); 5415 5416 verifyFormat("f<int>();"); 5417 verifyFormat("template <typename T> void f() {}"); 5418 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5419 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5420 "sizeof(char)>::type>;"); 5421 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5422 verifyFormat("f(a.operator()<A>());"); 5423 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5424 " .template operator()<A>());", 5425 getLLVMStyleWithColumns(35)); 5426 5427 // Not template parameters. 5428 verifyFormat("return a < b && c > d;"); 5429 verifyFormat("void f() {\n" 5430 " while (a < b && c > d) {\n" 5431 " }\n" 5432 "}"); 5433 verifyFormat("template <typename... Types>\n" 5434 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5435 5436 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5438 getLLVMStyleWithColumns(60)); 5439 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5440 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5441 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5442 } 5443 5444 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5445 verifyFormat("COMPARE(a, ==, b);"); 5446 } 5447 5448 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5449 verifyFormat("int A::*x;"); 5450 verifyFormat("int (S::*func)(void *);"); 5451 verifyFormat("void f() { int (S::*func)(void *); }"); 5452 verifyFormat("typedef bool *(Class::*Member)() const;"); 5453 verifyFormat("void f() {\n" 5454 " (a->*f)();\n" 5455 " a->*x;\n" 5456 " (a.*f)();\n" 5457 " ((*a).*f)();\n" 5458 " a.*x;\n" 5459 "}"); 5460 verifyFormat("void f() {\n" 5461 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5462 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5463 "}"); 5464 verifyFormat( 5465 "(aaaaaaaaaa->*bbbbbbb)(\n" 5466 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5467 FormatStyle Style = getLLVMStyle(); 5468 Style.PointerAlignment = FormatStyle::PAS_Left; 5469 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5470 } 5471 5472 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5473 verifyFormat("int a = -2;"); 5474 verifyFormat("f(-1, -2, -3);"); 5475 verifyFormat("a[-1] = 5;"); 5476 verifyFormat("int a = 5 + -2;"); 5477 verifyFormat("if (i == -1) {\n}"); 5478 verifyFormat("if (i != -1) {\n}"); 5479 verifyFormat("if (i > -1) {\n}"); 5480 verifyFormat("if (i < -1) {\n}"); 5481 verifyFormat("++(a->f());"); 5482 verifyFormat("--(a->f());"); 5483 verifyFormat("(a->f())++;"); 5484 verifyFormat("a[42]++;"); 5485 verifyFormat("if (!(a->f())) {\n}"); 5486 5487 verifyFormat("a-- > b;"); 5488 verifyFormat("b ? -a : c;"); 5489 verifyFormat("n * sizeof char16;"); 5490 verifyFormat("n * alignof char16;", getGoogleStyle()); 5491 verifyFormat("sizeof(char);"); 5492 verifyFormat("alignof(char);", getGoogleStyle()); 5493 5494 verifyFormat("return -1;"); 5495 verifyFormat("switch (a) {\n" 5496 "case -1:\n" 5497 " break;\n" 5498 "}"); 5499 verifyFormat("#define X -1"); 5500 verifyFormat("#define X -kConstant"); 5501 5502 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5503 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5504 5505 verifyFormat("int a = /* confusing comment */ -1;"); 5506 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5507 verifyFormat("int a = i /* confusing comment */++;"); 5508 } 5509 5510 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5511 verifyFormat("if (!aaaaaaaaaa( // break\n" 5512 " aaaaa)) {\n" 5513 "}"); 5514 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5515 " aaaaa));"); 5516 verifyFormat("*aaa = aaaaaaa( // break\n" 5517 " bbbbbb);"); 5518 } 5519 5520 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5521 verifyFormat("bool operator<();"); 5522 verifyFormat("bool operator>();"); 5523 verifyFormat("bool operator=();"); 5524 verifyFormat("bool operator==();"); 5525 verifyFormat("bool operator!=();"); 5526 verifyFormat("int operator+();"); 5527 verifyFormat("int operator++();"); 5528 verifyFormat("bool operator,();"); 5529 verifyFormat("bool operator();"); 5530 verifyFormat("bool operator()();"); 5531 verifyFormat("bool operator[]();"); 5532 verifyFormat("operator bool();"); 5533 verifyFormat("operator int();"); 5534 verifyFormat("operator void *();"); 5535 verifyFormat("operator SomeType<int>();"); 5536 verifyFormat("operator SomeType<int, int>();"); 5537 verifyFormat("operator SomeType<SomeType<int>>();"); 5538 verifyFormat("void *operator new(std::size_t size);"); 5539 verifyFormat("void *operator new[](std::size_t size);"); 5540 verifyFormat("void operator delete(void *ptr);"); 5541 verifyFormat("void operator delete[](void *ptr);"); 5542 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5543 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5544 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5545 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5546 5547 verifyFormat( 5548 "ostream &operator<<(ostream &OutputStream,\n" 5549 " SomeReallyLongType WithSomeReallyLongValue);"); 5550 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5551 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5552 " return left.group < right.group;\n" 5553 "}"); 5554 verifyFormat("SomeType &operator=(const SomeType &S);"); 5555 verifyFormat("f.template operator()<int>();"); 5556 5557 verifyGoogleFormat("operator void*();"); 5558 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5559 verifyGoogleFormat("operator ::A();"); 5560 5561 verifyFormat("using A::operator+;"); 5562 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5563 "int i;"); 5564 } 5565 5566 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5567 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5568 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5569 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5570 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5571 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5572 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5573 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5574 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5575 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5576 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5577 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5578 5579 FormatStyle AlignLeft = getLLVMStyle(); 5580 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5581 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5582 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5583 AlignLeft); 5584 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5585 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5586 5587 FormatStyle Spaces = getLLVMStyle(); 5588 Spaces.SpacesInCStyleCastParentheses = true; 5589 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5590 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5591 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5592 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5593 5594 Spaces.SpacesInCStyleCastParentheses = false; 5595 Spaces.SpacesInParentheses = true; 5596 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5597 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5598 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5599 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5600 } 5601 5602 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5603 verifyFormat("void f() {\n" 5604 " A *a = new A;\n" 5605 " A *a = new (placement) A;\n" 5606 " delete a;\n" 5607 " delete (A *)a;\n" 5608 "}"); 5609 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5610 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5611 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5612 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5613 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5614 verifyFormat("delete[] h->p;"); 5615 } 5616 5617 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5618 verifyFormat("int *f(int *a) {}"); 5619 verifyFormat("int main(int argc, char **argv) {}"); 5620 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5621 verifyIndependentOfContext("f(a, *a);"); 5622 verifyFormat("void g() { f(*a); }"); 5623 verifyIndependentOfContext("int a = b * 10;"); 5624 verifyIndependentOfContext("int a = 10 * b;"); 5625 verifyIndependentOfContext("int a = b * c;"); 5626 verifyIndependentOfContext("int a += b * c;"); 5627 verifyIndependentOfContext("int a -= b * c;"); 5628 verifyIndependentOfContext("int a *= b * c;"); 5629 verifyIndependentOfContext("int a /= b * c;"); 5630 verifyIndependentOfContext("int a = *b;"); 5631 verifyIndependentOfContext("int a = *b * c;"); 5632 verifyIndependentOfContext("int a = b * *c;"); 5633 verifyIndependentOfContext("int a = b * (10);"); 5634 verifyIndependentOfContext("S << b * (10);"); 5635 verifyIndependentOfContext("return 10 * b;"); 5636 verifyIndependentOfContext("return *b * *c;"); 5637 verifyIndependentOfContext("return a & ~b;"); 5638 verifyIndependentOfContext("f(b ? *c : *d);"); 5639 verifyIndependentOfContext("int a = b ? *c : *d;"); 5640 verifyIndependentOfContext("*b = a;"); 5641 verifyIndependentOfContext("a * ~b;"); 5642 verifyIndependentOfContext("a * !b;"); 5643 verifyIndependentOfContext("a * +b;"); 5644 verifyIndependentOfContext("a * -b;"); 5645 verifyIndependentOfContext("a * ++b;"); 5646 verifyIndependentOfContext("a * --b;"); 5647 verifyIndependentOfContext("a[4] * b;"); 5648 verifyIndependentOfContext("a[a * a] = 1;"); 5649 verifyIndependentOfContext("f() * b;"); 5650 verifyIndependentOfContext("a * [self dostuff];"); 5651 verifyIndependentOfContext("int x = a * (a + b);"); 5652 verifyIndependentOfContext("(a *)(a + b);"); 5653 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5654 verifyIndependentOfContext("int *pa = (int *)&a;"); 5655 verifyIndependentOfContext("return sizeof(int **);"); 5656 verifyIndependentOfContext("return sizeof(int ******);"); 5657 verifyIndependentOfContext("return (int **&)a;"); 5658 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5659 verifyFormat("void f(Type (*parameter)[10]) {}"); 5660 verifyFormat("void f(Type (¶meter)[10]) {}"); 5661 verifyGoogleFormat("return sizeof(int**);"); 5662 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5663 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5664 verifyFormat("auto a = [](int **&, int ***) {};"); 5665 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5666 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5667 verifyFormat("[](const decltype(*a) &value) {}"); 5668 verifyFormat("decltype(a * b) F();"); 5669 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5670 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5671 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5672 verifyIndependentOfContext("int i{a * b};"); 5673 verifyIndependentOfContext("aaa && aaa->f();"); 5674 verifyIndependentOfContext("int x = ~*p;"); 5675 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5676 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5677 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5678 verifyFormat("void f() { f(a, c * d); }"); 5679 verifyFormat("void f() { f(new a(), c * d); }"); 5680 5681 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5682 5683 verifyIndependentOfContext("A<int *> a;"); 5684 verifyIndependentOfContext("A<int **> a;"); 5685 verifyIndependentOfContext("A<int *, int *> a;"); 5686 verifyIndependentOfContext("A<int *[]> a;"); 5687 verifyIndependentOfContext( 5688 "const char *const p = reinterpret_cast<const char *const>(q);"); 5689 verifyIndependentOfContext("A<int **, int **> a;"); 5690 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5691 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5692 verifyFormat("for (; a && b;) {\n}"); 5693 verifyFormat("bool foo = true && [] { return false; }();"); 5694 5695 verifyFormat( 5696 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5698 5699 verifyGoogleFormat("**outparam = 1;"); 5700 verifyGoogleFormat("*outparam = a * b;"); 5701 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5702 verifyGoogleFormat("A<int*> a;"); 5703 verifyGoogleFormat("A<int**> a;"); 5704 verifyGoogleFormat("A<int*, int*> a;"); 5705 verifyGoogleFormat("A<int**, int**> a;"); 5706 verifyGoogleFormat("f(b ? *c : *d);"); 5707 verifyGoogleFormat("int a = b ? *c : *d;"); 5708 verifyGoogleFormat("Type* t = **x;"); 5709 verifyGoogleFormat("Type* t = *++*x;"); 5710 verifyGoogleFormat("*++*x;"); 5711 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5712 verifyGoogleFormat("Type* t = x++ * y;"); 5713 verifyGoogleFormat( 5714 "const char* const p = reinterpret_cast<const char* const>(q);"); 5715 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5716 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5717 verifyGoogleFormat("template <typename T>\n" 5718 "void f(int i = 0, SomeType** temps = NULL);"); 5719 5720 FormatStyle Left = getLLVMStyle(); 5721 Left.PointerAlignment = FormatStyle::PAS_Left; 5722 verifyFormat("x = *a(x) = *a(y);", Left); 5723 verifyFormat("for (;; * = b) {\n}", Left); 5724 verifyFormat("return *this += 1;", Left); 5725 5726 verifyIndependentOfContext("a = *(x + y);"); 5727 verifyIndependentOfContext("a = &(x + y);"); 5728 verifyIndependentOfContext("*(x + y).call();"); 5729 verifyIndependentOfContext("&(x + y)->call();"); 5730 verifyFormat("void f() { &(*I).first; }"); 5731 5732 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5733 verifyFormat( 5734 "int *MyValues = {\n" 5735 " *A, // Operator detection might be confused by the '{'\n" 5736 " *BB // Operator detection might be confused by previous comment\n" 5737 "};"); 5738 5739 verifyIndependentOfContext("if (int *a = &b)"); 5740 verifyIndependentOfContext("if (int &a = *b)"); 5741 verifyIndependentOfContext("if (a & b[i])"); 5742 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5743 verifyIndependentOfContext("if (*b[i])"); 5744 verifyIndependentOfContext("if (int *a = (&b))"); 5745 verifyIndependentOfContext("while (int *a = &b)"); 5746 verifyIndependentOfContext("size = sizeof *a;"); 5747 verifyIndependentOfContext("if (a && (b = c))"); 5748 verifyFormat("void f() {\n" 5749 " for (const int &v : Values) {\n" 5750 " }\n" 5751 "}"); 5752 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5753 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5754 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5755 5756 verifyFormat("#define A (!a * b)"); 5757 verifyFormat("#define MACRO \\\n" 5758 " int *i = a * b; \\\n" 5759 " void f(a *b);", 5760 getLLVMStyleWithColumns(19)); 5761 5762 verifyIndependentOfContext("A = new SomeType *[Length];"); 5763 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5764 verifyIndependentOfContext("T **t = new T *;"); 5765 verifyIndependentOfContext("T **t = new T *();"); 5766 verifyGoogleFormat("A = new SomeType*[Length]();"); 5767 verifyGoogleFormat("A = new SomeType*[Length];"); 5768 verifyGoogleFormat("T** t = new T*;"); 5769 verifyGoogleFormat("T** t = new T*();"); 5770 5771 FormatStyle PointerLeft = getLLVMStyle(); 5772 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5773 verifyFormat("delete *x;", PointerLeft); 5774 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5775 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5776 verifyFormat("template <bool a, bool b> " 5777 "typename t::if<x && y>::type f() {}"); 5778 verifyFormat("template <int *y> f() {}"); 5779 verifyFormat("vector<int *> v;"); 5780 verifyFormat("vector<int *const> v;"); 5781 verifyFormat("vector<int *const **const *> v;"); 5782 verifyFormat("vector<int *volatile> v;"); 5783 verifyFormat("vector<a * b> v;"); 5784 verifyFormat("foo<b && false>();"); 5785 verifyFormat("foo<b & 1>();"); 5786 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5787 verifyFormat( 5788 "template <class T, class = typename std::enable_if<\n" 5789 " std::is_integral<T>::value &&\n" 5790 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5791 "void F();", 5792 getLLVMStyleWithColumns(76)); 5793 verifyFormat( 5794 "template <class T,\n" 5795 " class = typename ::std::enable_if<\n" 5796 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5797 "void F();", 5798 getGoogleStyleWithColumns(68)); 5799 5800 verifyIndependentOfContext("MACRO(int *i);"); 5801 verifyIndependentOfContext("MACRO(auto *a);"); 5802 verifyIndependentOfContext("MACRO(const A *a);"); 5803 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5804 // FIXME: Is there a way to make this work? 5805 // verifyIndependentOfContext("MACRO(A *a);"); 5806 5807 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5808 verifyFormat("return options != nullptr && operator==(*options);"); 5809 5810 EXPECT_EQ("#define OP(x) \\\n" 5811 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5812 " return s << a.DebugString(); \\\n" 5813 " }", 5814 format("#define OP(x) \\\n" 5815 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5816 " return s << a.DebugString(); \\\n" 5817 " }", 5818 getLLVMStyleWithColumns(50))); 5819 5820 // FIXME: We cannot handle this case yet; we might be able to figure out that 5821 // foo<x> d > v; doesn't make sense. 5822 verifyFormat("foo<a<b && c> d> v;"); 5823 5824 FormatStyle PointerMiddle = getLLVMStyle(); 5825 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5826 verifyFormat("delete *x;", PointerMiddle); 5827 verifyFormat("int * x;", PointerMiddle); 5828 verifyFormat("template <int * y> f() {}", PointerMiddle); 5829 verifyFormat("int * f(int * a) {}", PointerMiddle); 5830 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5831 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5832 verifyFormat("A<int *> a;", PointerMiddle); 5833 verifyFormat("A<int **> a;", PointerMiddle); 5834 verifyFormat("A<int *, int *> a;", PointerMiddle); 5835 verifyFormat("A<int * []> a;", PointerMiddle); 5836 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5837 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5838 verifyFormat("T ** t = new T *;", PointerMiddle); 5839 5840 // Member function reference qualifiers aren't binary operators. 5841 verifyFormat("string // break\n" 5842 "operator()() & {}"); 5843 verifyFormat("string // break\n" 5844 "operator()() && {}"); 5845 verifyGoogleFormat("template <typename T>\n" 5846 "auto x() & -> int {}"); 5847 } 5848 5849 TEST_F(FormatTest, UnderstandsAttributes) { 5850 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5851 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5852 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5853 FormatStyle AfterType = getLLVMStyle(); 5854 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5855 verifyFormat("__attribute__((nodebug)) void\n" 5856 "foo() {}\n", 5857 AfterType); 5858 } 5859 5860 TEST_F(FormatTest, UnderstandsEllipsis) { 5861 verifyFormat("int printf(const char *fmt, ...);"); 5862 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5863 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5864 5865 FormatStyle PointersLeft = getLLVMStyle(); 5866 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5867 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5868 } 5869 5870 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5871 EXPECT_EQ("int *a;\n" 5872 "int *a;\n" 5873 "int *a;", 5874 format("int *a;\n" 5875 "int* a;\n" 5876 "int *a;", 5877 getGoogleStyle())); 5878 EXPECT_EQ("int* a;\n" 5879 "int* a;\n" 5880 "int* a;", 5881 format("int* a;\n" 5882 "int* a;\n" 5883 "int *a;", 5884 getGoogleStyle())); 5885 EXPECT_EQ("int *a;\n" 5886 "int *a;\n" 5887 "int *a;", 5888 format("int *a;\n" 5889 "int * a;\n" 5890 "int * a;", 5891 getGoogleStyle())); 5892 EXPECT_EQ("auto x = [] {\n" 5893 " int *a;\n" 5894 " int *a;\n" 5895 " int *a;\n" 5896 "};", 5897 format("auto x=[]{int *a;\n" 5898 "int * a;\n" 5899 "int * a;};", 5900 getGoogleStyle())); 5901 } 5902 5903 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5904 verifyFormat("int f(int &&a) {}"); 5905 verifyFormat("int f(int a, char &&b) {}"); 5906 verifyFormat("void f() { int &&a = b; }"); 5907 verifyGoogleFormat("int f(int a, char&& b) {}"); 5908 verifyGoogleFormat("void f() { int&& a = b; }"); 5909 5910 verifyIndependentOfContext("A<int &&> a;"); 5911 verifyIndependentOfContext("A<int &&, int &&> a;"); 5912 verifyGoogleFormat("A<int&&> a;"); 5913 verifyGoogleFormat("A<int&&, int&&> a;"); 5914 5915 // Not rvalue references: 5916 verifyFormat("template <bool B, bool C> class A {\n" 5917 " static_assert(B && C, \"Something is wrong\");\n" 5918 "};"); 5919 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5920 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5921 verifyFormat("#define A(a, b) (a && b)"); 5922 } 5923 5924 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5925 verifyFormat("void f() {\n" 5926 " x[aaaaaaaaa -\n" 5927 " b] = 23;\n" 5928 "}", 5929 getLLVMStyleWithColumns(15)); 5930 } 5931 5932 TEST_F(FormatTest, FormatsCasts) { 5933 verifyFormat("Type *A = static_cast<Type *>(P);"); 5934 verifyFormat("Type *A = (Type *)P;"); 5935 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5936 verifyFormat("int a = (int)(2.0f);"); 5937 verifyFormat("int a = (int)2.0f;"); 5938 verifyFormat("x[(int32)y];"); 5939 verifyFormat("x = (int32)y;"); 5940 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5941 verifyFormat("int a = (int)*b;"); 5942 verifyFormat("int a = (int)2.0f;"); 5943 verifyFormat("int a = (int)~0;"); 5944 verifyFormat("int a = (int)++a;"); 5945 verifyFormat("int a = (int)sizeof(int);"); 5946 verifyFormat("int a = (int)+2;"); 5947 verifyFormat("my_int a = (my_int)2.0f;"); 5948 verifyFormat("my_int a = (my_int)sizeof(int);"); 5949 verifyFormat("return (my_int)aaa;"); 5950 verifyFormat("#define x ((int)-1)"); 5951 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5952 verifyFormat("#define p(q) ((int *)&q)"); 5953 verifyFormat("fn(a)(b) + 1;"); 5954 5955 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5956 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5957 verifyFormat("my_int a = (my_int)~0;"); 5958 verifyFormat("my_int a = (my_int)++a;"); 5959 verifyFormat("my_int a = (my_int)-2;"); 5960 verifyFormat("my_int a = (my_int)1;"); 5961 verifyFormat("my_int a = (my_int *)1;"); 5962 verifyFormat("my_int a = (const my_int)-1;"); 5963 verifyFormat("my_int a = (const my_int *)-1;"); 5964 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5965 verifyFormat("my_int a = (ns::my_int)-2;"); 5966 verifyFormat("case (my_int)ONE:"); 5967 5968 // FIXME: single value wrapped with paren will be treated as cast. 5969 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5970 5971 verifyFormat("{ (void)F; }"); 5972 5973 // Don't break after a cast's 5974 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5975 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5976 " bbbbbbbbbbbbbbbbbbbbbb);"); 5977 5978 // These are not casts. 5979 verifyFormat("void f(int *) {}"); 5980 verifyFormat("f(foo)->b;"); 5981 verifyFormat("f(foo).b;"); 5982 verifyFormat("f(foo)(b);"); 5983 verifyFormat("f(foo)[b];"); 5984 verifyFormat("[](foo) { return 4; }(bar);"); 5985 verifyFormat("(*funptr)(foo)[4];"); 5986 verifyFormat("funptrs[4](foo)[4];"); 5987 verifyFormat("void f(int *);"); 5988 verifyFormat("void f(int *) = 0;"); 5989 verifyFormat("void f(SmallVector<int>) {}"); 5990 verifyFormat("void f(SmallVector<int>);"); 5991 verifyFormat("void f(SmallVector<int>) = 0;"); 5992 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5993 verifyFormat("int a = sizeof(int) * b;"); 5994 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5995 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5996 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5997 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5998 5999 // These are not casts, but at some point were confused with casts. 6000 verifyFormat("virtual void foo(int *) override;"); 6001 verifyFormat("virtual void foo(char &) const;"); 6002 verifyFormat("virtual void foo(int *a, char *) const;"); 6003 verifyFormat("int a = sizeof(int *) + b;"); 6004 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6005 verifyFormat("bool b = f(g<int>) && c;"); 6006 verifyFormat("typedef void (*f)(int i) func;"); 6007 6008 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6009 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6010 // FIXME: The indentation here is not ideal. 6011 verifyFormat( 6012 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6013 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6014 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6015 } 6016 6017 TEST_F(FormatTest, FormatsFunctionTypes) { 6018 verifyFormat("A<bool()> a;"); 6019 verifyFormat("A<SomeType()> a;"); 6020 verifyFormat("A<void (*)(int, std::string)> a;"); 6021 verifyFormat("A<void *(int)>;"); 6022 verifyFormat("void *(*a)(int *, SomeType *);"); 6023 verifyFormat("int (*func)(void *);"); 6024 verifyFormat("void f() { int (*func)(void *); }"); 6025 verifyFormat("template <class CallbackClass>\n" 6026 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6027 6028 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6029 verifyGoogleFormat("void* (*a)(int);"); 6030 verifyGoogleFormat( 6031 "template <class CallbackClass>\n" 6032 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6033 6034 // Other constructs can look somewhat like function types: 6035 verifyFormat("A<sizeof(*x)> a;"); 6036 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6037 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6038 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6039 verifyFormat("int x = f(&h)();"); 6040 } 6041 6042 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6043 verifyFormat("A (*foo_)[6];"); 6044 verifyFormat("vector<int> (*foo_)[6];"); 6045 } 6046 6047 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6048 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6049 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6050 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6051 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6052 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6053 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6054 6055 // Different ways of ()-initializiation. 6056 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6057 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6058 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6059 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6060 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6061 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6062 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6063 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6064 } 6065 6066 TEST_F(FormatTest, BreaksLongDeclarations) { 6067 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6068 " AnotherNameForTheLongType;"); 6069 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6071 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6072 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6073 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6074 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6075 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6076 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6077 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6078 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6079 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6080 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6081 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6082 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6083 FormatStyle Indented = getLLVMStyle(); 6084 Indented.IndentWrappedFunctionNames = true; 6085 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6086 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6087 Indented); 6088 verifyFormat( 6089 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6090 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6091 Indented); 6092 verifyFormat( 6093 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6094 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6095 Indented); 6096 verifyFormat( 6097 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6098 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6099 Indented); 6100 6101 // FIXME: Without the comment, this breaks after "(". 6102 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6103 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6104 getGoogleStyle()); 6105 6106 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6107 " int LoooooooooooooooooooongParam2) {}"); 6108 verifyFormat( 6109 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6110 " SourceLocation L, IdentifierIn *II,\n" 6111 " Type *T) {}"); 6112 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6113 "ReallyReaaallyLongFunctionName(\n" 6114 " const std::string &SomeParameter,\n" 6115 " const SomeType<string, SomeOtherTemplateParameter>\n" 6116 " &ReallyReallyLongParameterName,\n" 6117 " const SomeType<string, SomeOtherTemplateParameter>\n" 6118 " &AnotherLongParameterName) {}"); 6119 verifyFormat("template <typename A>\n" 6120 "SomeLoooooooooooooooooooooongType<\n" 6121 " typename some_namespace::SomeOtherType<A>::Type>\n" 6122 "Function() {}"); 6123 6124 verifyGoogleFormat( 6125 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6126 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6127 verifyGoogleFormat( 6128 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6129 " SourceLocation L) {}"); 6130 verifyGoogleFormat( 6131 "some_namespace::LongReturnType\n" 6132 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6133 " int first_long_parameter, int second_parameter) {}"); 6134 6135 verifyGoogleFormat("template <typename T>\n" 6136 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6137 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6138 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6139 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6140 6141 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6142 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6143 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6144 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6145 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6146 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6147 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6148 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6151 } 6152 6153 TEST_F(FormatTest, FormatsArrays) { 6154 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6155 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6156 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6157 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6158 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6159 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6160 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6161 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6162 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6163 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6164 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6165 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6166 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6167 verifyFormat( 6168 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6169 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6170 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6171 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6172 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6173 6174 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6176 verifyFormat( 6177 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6178 " .aaaaaaa[0]\n" 6179 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6180 verifyFormat("a[::b::c];"); 6181 6182 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6183 6184 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6185 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6186 } 6187 6188 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6189 verifyFormat("(a)->b();"); 6190 verifyFormat("--a;"); 6191 } 6192 6193 TEST_F(FormatTest, HandlesIncludeDirectives) { 6194 verifyFormat("#include <string>\n" 6195 "#include <a/b/c.h>\n" 6196 "#include \"a/b/string\"\n" 6197 "#include \"string.h\"\n" 6198 "#include \"string.h\"\n" 6199 "#include <a-a>\n" 6200 "#include < path with space >\n" 6201 "#include_next <test.h>" 6202 "#include \"abc.h\" // this is included for ABC\n" 6203 "#include \"some long include\" // with a comment\n" 6204 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6205 getLLVMStyleWithColumns(35)); 6206 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6207 EXPECT_EQ("#include <a>", format("#include<a>")); 6208 6209 verifyFormat("#import <string>"); 6210 verifyFormat("#import <a/b/c.h>"); 6211 verifyFormat("#import \"a/b/string\""); 6212 verifyFormat("#import \"string.h\""); 6213 verifyFormat("#import \"string.h\""); 6214 verifyFormat("#if __has_include(<strstream>)\n" 6215 "#include <strstream>\n" 6216 "#endif"); 6217 6218 verifyFormat("#define MY_IMPORT <a/b>"); 6219 6220 // Protocol buffer definition or missing "#". 6221 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6222 getLLVMStyleWithColumns(30)); 6223 6224 FormatStyle Style = getLLVMStyle(); 6225 Style.AlwaysBreakBeforeMultilineStrings = true; 6226 Style.ColumnLimit = 0; 6227 verifyFormat("#import \"abc.h\"", Style); 6228 6229 // But 'import' might also be a regular C++ namespace. 6230 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6232 } 6233 6234 //===----------------------------------------------------------------------===// 6235 // Error recovery tests. 6236 //===----------------------------------------------------------------------===// 6237 6238 TEST_F(FormatTest, IncompleteParameterLists) { 6239 FormatStyle NoBinPacking = getLLVMStyle(); 6240 NoBinPacking.BinPackParameters = false; 6241 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6242 " double *min_x,\n" 6243 " double *max_x,\n" 6244 " double *min_y,\n" 6245 " double *max_y,\n" 6246 " double *min_z,\n" 6247 " double *max_z, ) {}", 6248 NoBinPacking); 6249 } 6250 6251 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6252 verifyFormat("void f() { return; }\n42"); 6253 verifyFormat("void f() {\n" 6254 " if (0)\n" 6255 " return;\n" 6256 "}\n" 6257 "42"); 6258 verifyFormat("void f() { return }\n42"); 6259 verifyFormat("void f() {\n" 6260 " if (0)\n" 6261 " return\n" 6262 "}\n" 6263 "42"); 6264 } 6265 6266 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6267 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6268 EXPECT_EQ("void f() {\n" 6269 " if (a)\n" 6270 " return\n" 6271 "}", 6272 format("void f ( ) { if ( a ) return }")); 6273 EXPECT_EQ("namespace N {\n" 6274 "void f()\n" 6275 "}", 6276 format("namespace N { void f() }")); 6277 EXPECT_EQ("namespace N {\n" 6278 "void f() {}\n" 6279 "void g()\n" 6280 "}", 6281 format("namespace N { void f( ) { } void g( ) }")); 6282 } 6283 6284 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6285 verifyFormat("int aaaaaaaa =\n" 6286 " // Overlylongcomment\n" 6287 " b;", 6288 getLLVMStyleWithColumns(20)); 6289 verifyFormat("function(\n" 6290 " ShortArgument,\n" 6291 " LoooooooooooongArgument);\n", 6292 getLLVMStyleWithColumns(20)); 6293 } 6294 6295 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6296 verifyFormat("public:"); 6297 verifyFormat("class A {\n" 6298 "public\n" 6299 " void f() {}\n" 6300 "};"); 6301 verifyFormat("public\n" 6302 "int qwerty;"); 6303 verifyFormat("public\n" 6304 "B {}"); 6305 verifyFormat("public\n" 6306 "{}"); 6307 verifyFormat("public\n" 6308 "B { int x; }"); 6309 } 6310 6311 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6312 verifyFormat("{"); 6313 verifyFormat("#})"); 6314 verifyNoCrash("(/**/[:!] ?[)."); 6315 } 6316 6317 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6318 verifyFormat("do {\n}"); 6319 verifyFormat("do {\n}\n" 6320 "f();"); 6321 verifyFormat("do {\n}\n" 6322 "wheeee(fun);"); 6323 verifyFormat("do {\n" 6324 " f();\n" 6325 "}"); 6326 } 6327 6328 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6329 verifyFormat("if {\n foo;\n foo();\n}"); 6330 verifyFormat("switch {\n foo;\n foo();\n}"); 6331 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6332 verifyFormat("while {\n foo;\n foo();\n}"); 6333 verifyFormat("do {\n foo;\n foo();\n} while;"); 6334 } 6335 6336 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6337 verifyIncompleteFormat("namespace {\n" 6338 "class Foo { Foo (\n" 6339 "};\n" 6340 "} // comment"); 6341 } 6342 6343 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6344 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6345 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6346 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6347 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6348 6349 EXPECT_EQ("{\n" 6350 " {\n" 6351 " breakme(\n" 6352 " qwe);\n" 6353 " }\n", 6354 format("{\n" 6355 " {\n" 6356 " breakme(qwe);\n" 6357 "}\n", 6358 getLLVMStyleWithColumns(10))); 6359 } 6360 6361 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6362 verifyFormat("int x = {\n" 6363 " avariable,\n" 6364 " b(alongervariable)};", 6365 getLLVMStyleWithColumns(25)); 6366 } 6367 6368 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6369 verifyFormat("return (a)(b){1, 2, 3};"); 6370 } 6371 6372 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6373 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6374 verifyFormat("vector<int> x{\n" 6375 " 1, 2, 3, 4,\n" 6376 "};"); 6377 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6378 verifyFormat("f({1, 2});"); 6379 verifyFormat("auto v = Foo{-1};"); 6380 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6381 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6382 verifyFormat("new vector<int>{1, 2, 3};"); 6383 verifyFormat("new int[3]{1, 2, 3};"); 6384 verifyFormat("new int{1};"); 6385 verifyFormat("return {arg1, arg2};"); 6386 verifyFormat("return {arg1, SomeType{parameter}};"); 6387 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6388 verifyFormat("new T{arg1, arg2};"); 6389 verifyFormat("f(MyMap[{composite, key}]);"); 6390 verifyFormat("class Class {\n" 6391 " T member = {arg1, arg2};\n" 6392 "};"); 6393 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6394 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6395 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6396 6397 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6398 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6399 verifyFormat("auto i = decltype(x){};"); 6400 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6401 verifyFormat("Node n{1, Node{1000}, //\n" 6402 " 2};"); 6403 verifyFormat("Aaaa aaaaaaa{\n" 6404 " {\n" 6405 " aaaa,\n" 6406 " },\n" 6407 "};"); 6408 verifyFormat("class C : public D {\n" 6409 " SomeClass SC{2};\n" 6410 "};"); 6411 verifyFormat("class C : public A {\n" 6412 " class D : public B {\n" 6413 " void f() { int i{2}; }\n" 6414 " };\n" 6415 "};"); 6416 verifyFormat("#define A {a, a},"); 6417 6418 // In combination with BinPackArguments = false. 6419 FormatStyle NoBinPacking = getLLVMStyle(); 6420 NoBinPacking.BinPackArguments = false; 6421 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6422 " bbbbb,\n" 6423 " ccccc,\n" 6424 " ddddd,\n" 6425 " eeeee,\n" 6426 " ffffff,\n" 6427 " ggggg,\n" 6428 " hhhhhh,\n" 6429 " iiiiii,\n" 6430 " jjjjjj,\n" 6431 " kkkkkk};", 6432 NoBinPacking); 6433 verifyFormat("const Aaaaaa aaaaa = {\n" 6434 " aaaaa,\n" 6435 " bbbbb,\n" 6436 " ccccc,\n" 6437 " ddddd,\n" 6438 " eeeee,\n" 6439 " ffffff,\n" 6440 " ggggg,\n" 6441 " hhhhhh,\n" 6442 " iiiiii,\n" 6443 " jjjjjj,\n" 6444 " kkkkkk,\n" 6445 "};", 6446 NoBinPacking); 6447 verifyFormat( 6448 "const Aaaaaa aaaaa = {\n" 6449 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6450 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6451 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6452 "};", 6453 NoBinPacking); 6454 6455 // FIXME: The alignment of these trailing comments might be bad. Then again, 6456 // this might be utterly useless in real code. 6457 verifyFormat("Constructor::Constructor()\n" 6458 " : some_value{ //\n" 6459 " aaaaaaa, //\n" 6460 " bbbbbbb} {}"); 6461 6462 // In braced lists, the first comment is always assumed to belong to the 6463 // first element. Thus, it can be moved to the next or previous line as 6464 // appropriate. 6465 EXPECT_EQ("function({// First element:\n" 6466 " 1,\n" 6467 " // Second element:\n" 6468 " 2});", 6469 format("function({\n" 6470 " // First element:\n" 6471 " 1,\n" 6472 " // Second element:\n" 6473 " 2});")); 6474 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6475 " // First element:\n" 6476 " 1,\n" 6477 " // Second element:\n" 6478 " 2};", 6479 format("std::vector<int> MyNumbers{// First element:\n" 6480 " 1,\n" 6481 " // Second element:\n" 6482 " 2};", 6483 getLLVMStyleWithColumns(30))); 6484 // A trailing comma should still lead to an enforced line break. 6485 EXPECT_EQ("vector<int> SomeVector = {\n" 6486 " // aaa\n" 6487 " 1, 2,\n" 6488 "};", 6489 format("vector<int> SomeVector = { // aaa\n" 6490 " 1, 2, };")); 6491 6492 FormatStyle ExtraSpaces = getLLVMStyle(); 6493 ExtraSpaces.Cpp11BracedListStyle = false; 6494 ExtraSpaces.ColumnLimit = 75; 6495 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6496 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6497 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6498 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6499 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6500 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6501 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6502 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6503 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6504 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6505 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6506 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6507 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6508 verifyFormat("class Class {\n" 6509 " T member = { arg1, arg2 };\n" 6510 "};", 6511 ExtraSpaces); 6512 verifyFormat( 6513 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6514 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6515 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6516 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6517 ExtraSpaces); 6518 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6519 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6520 ExtraSpaces); 6521 verifyFormat( 6522 "someFunction(OtherParam,\n" 6523 " BracedList{ // comment 1 (Forcing interesting break)\n" 6524 " param1, param2,\n" 6525 " // comment 2\n" 6526 " param3, param4 });", 6527 ExtraSpaces); 6528 verifyFormat( 6529 "std::this_thread::sleep_for(\n" 6530 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6531 ExtraSpaces); 6532 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6533 " aaaaaaa,\n" 6534 " aaaaaaaaaa,\n" 6535 " aaaaa,\n" 6536 " aaaaaaaaaaaaaaa,\n" 6537 " aaa,\n" 6538 " aaaaaaaaaa,\n" 6539 " a,\n" 6540 " aaaaaaaaaaaaaaaaaaaaa,\n" 6541 " aaaaaaaaaaaa,\n" 6542 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6543 " aaaaaaa,\n" 6544 " a};"); 6545 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6546 } 6547 6548 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6549 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6550 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6551 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6552 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6553 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6554 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6555 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6556 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6557 " 1, 22, 333, 4444, 55555, //\n" 6558 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6559 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6560 verifyFormat( 6561 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6562 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6563 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6564 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6565 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6566 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6567 " 7777777};"); 6568 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6569 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6570 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6571 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6572 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6573 " // Separating comment.\n" 6574 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6575 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6576 " // Leading comment\n" 6577 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6578 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6579 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6580 " 1, 1, 1, 1};", 6581 getLLVMStyleWithColumns(39)); 6582 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6583 " 1, 1, 1, 1};", 6584 getLLVMStyleWithColumns(38)); 6585 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6586 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6587 getLLVMStyleWithColumns(43)); 6588 verifyFormat( 6589 "static unsigned SomeValues[10][3] = {\n" 6590 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6591 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6592 verifyFormat("static auto fields = new vector<string>{\n" 6593 " \"aaaaaaaaaaaaa\",\n" 6594 " \"aaaaaaaaaaaaa\",\n" 6595 " \"aaaaaaaaaaaa\",\n" 6596 " \"aaaaaaaaaaaaaa\",\n" 6597 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6598 " \"aaaaaaaaaaaa\",\n" 6599 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6600 "};"); 6601 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6602 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6603 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6604 " 3, cccccccccccccccccccccc};", 6605 getLLVMStyleWithColumns(60)); 6606 6607 // Trailing commas. 6608 verifyFormat("vector<int> x = {\n" 6609 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6610 "};", 6611 getLLVMStyleWithColumns(39)); 6612 verifyFormat("vector<int> x = {\n" 6613 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6614 "};", 6615 getLLVMStyleWithColumns(39)); 6616 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6617 " 1, 1, 1, 1,\n" 6618 " /**/ /**/};", 6619 getLLVMStyleWithColumns(39)); 6620 6621 // Trailing comment in the first line. 6622 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6623 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6624 " 111111111, 222222222, 3333333333, 444444444, //\n" 6625 " 11111111, 22222222, 333333333, 44444444};"); 6626 // Trailing comment in the last line. 6627 verifyFormat("int aaaaa[] = {\n" 6628 " 1, 2, 3, // comment\n" 6629 " 4, 5, 6 // comment\n" 6630 "};"); 6631 6632 // With nested lists, we should either format one item per line or all nested 6633 // lists one on line. 6634 // FIXME: For some nested lists, we can do better. 6635 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6636 " {aaaaaaaaaaaaaaaaaaa},\n" 6637 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6638 " {aaaaaaaaaaaaaaaaa}};", 6639 getLLVMStyleWithColumns(60)); 6640 verifyFormat( 6641 "SomeStruct my_struct_array = {\n" 6642 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6643 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6644 " {aaa, aaa},\n" 6645 " {aaa, aaa},\n" 6646 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6647 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6648 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6649 6650 // No column layout should be used here. 6651 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6652 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6653 6654 verifyNoCrash("a<,"); 6655 6656 // No braced initializer here. 6657 verifyFormat("void f() {\n" 6658 " struct Dummy {};\n" 6659 " f(v);\n" 6660 "}"); 6661 6662 // Long lists should be formatted in columns even if they are nested. 6663 verifyFormat( 6664 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6665 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6666 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6667 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6668 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6669 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6670 } 6671 6672 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6673 FormatStyle DoNotMerge = getLLVMStyle(); 6674 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6675 6676 verifyFormat("void f() { return 42; }"); 6677 verifyFormat("void f() {\n" 6678 " return 42;\n" 6679 "}", 6680 DoNotMerge); 6681 verifyFormat("void f() {\n" 6682 " // Comment\n" 6683 "}"); 6684 verifyFormat("{\n" 6685 "#error {\n" 6686 " int a;\n" 6687 "}"); 6688 verifyFormat("{\n" 6689 " int a;\n" 6690 "#error {\n" 6691 "}"); 6692 verifyFormat("void f() {} // comment"); 6693 verifyFormat("void f() { int a; } // comment"); 6694 verifyFormat("void f() {\n" 6695 "} // comment", 6696 DoNotMerge); 6697 verifyFormat("void f() {\n" 6698 " int a;\n" 6699 "} // comment", 6700 DoNotMerge); 6701 verifyFormat("void f() {\n" 6702 "} // comment", 6703 getLLVMStyleWithColumns(15)); 6704 6705 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6706 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6707 6708 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6709 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6710 verifyFormat("class C {\n" 6711 " C()\n" 6712 " : iiiiiiii(nullptr),\n" 6713 " kkkkkkk(nullptr),\n" 6714 " mmmmmmm(nullptr),\n" 6715 " nnnnnnn(nullptr) {}\n" 6716 "};", 6717 getGoogleStyle()); 6718 6719 FormatStyle NoColumnLimit = getLLVMStyle(); 6720 NoColumnLimit.ColumnLimit = 0; 6721 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6722 EXPECT_EQ("class C {\n" 6723 " A() : b(0) {}\n" 6724 "};", 6725 format("class C{A():b(0){}};", NoColumnLimit)); 6726 EXPECT_EQ("A()\n" 6727 " : b(0) {\n" 6728 "}", 6729 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6730 6731 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6732 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6733 FormatStyle::SFS_None; 6734 EXPECT_EQ("A()\n" 6735 " : b(0) {\n" 6736 "}", 6737 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6738 EXPECT_EQ("A()\n" 6739 " : b(0) {\n" 6740 "}", 6741 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6742 6743 verifyFormat("#define A \\\n" 6744 " void f() { \\\n" 6745 " int i; \\\n" 6746 " }", 6747 getLLVMStyleWithColumns(20)); 6748 verifyFormat("#define A \\\n" 6749 " void f() { int i; }", 6750 getLLVMStyleWithColumns(21)); 6751 verifyFormat("#define A \\\n" 6752 " void f() { \\\n" 6753 " int i; \\\n" 6754 " } \\\n" 6755 " int j;", 6756 getLLVMStyleWithColumns(22)); 6757 verifyFormat("#define A \\\n" 6758 " void f() { int i; } \\\n" 6759 " int j;", 6760 getLLVMStyleWithColumns(23)); 6761 } 6762 6763 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6764 FormatStyle MergeInlineOnly = getLLVMStyle(); 6765 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6766 verifyFormat("class C {\n" 6767 " int f() { return 42; }\n" 6768 "};", 6769 MergeInlineOnly); 6770 verifyFormat("int f() {\n" 6771 " return 42;\n" 6772 "}", 6773 MergeInlineOnly); 6774 } 6775 6776 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6777 // Elaborate type variable declarations. 6778 verifyFormat("struct foo a = {bar};\nint n;"); 6779 verifyFormat("class foo a = {bar};\nint n;"); 6780 verifyFormat("union foo a = {bar};\nint n;"); 6781 6782 // Elaborate types inside function definitions. 6783 verifyFormat("struct foo f() {}\nint n;"); 6784 verifyFormat("class foo f() {}\nint n;"); 6785 verifyFormat("union foo f() {}\nint n;"); 6786 6787 // Templates. 6788 verifyFormat("template <class X> void f() {}\nint n;"); 6789 verifyFormat("template <struct X> void f() {}\nint n;"); 6790 verifyFormat("template <union X> void f() {}\nint n;"); 6791 6792 // Actual definitions... 6793 verifyFormat("struct {\n} n;"); 6794 verifyFormat( 6795 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6796 verifyFormat("union Z {\n int n;\n} x;"); 6797 verifyFormat("class MACRO Z {\n} n;"); 6798 verifyFormat("class MACRO(X) Z {\n} n;"); 6799 verifyFormat("class __attribute__(X) Z {\n} n;"); 6800 verifyFormat("class __declspec(X) Z {\n} n;"); 6801 verifyFormat("class A##B##C {\n} n;"); 6802 verifyFormat("class alignas(16) Z {\n} n;"); 6803 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6804 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6805 6806 // Redefinition from nested context: 6807 verifyFormat("class A::B::C {\n} n;"); 6808 6809 // Template definitions. 6810 verifyFormat( 6811 "template <typename F>\n" 6812 "Matcher(const Matcher<F> &Other,\n" 6813 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6814 " !is_same<F, T>::value>::type * = 0)\n" 6815 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6816 6817 // FIXME: This is still incorrectly handled at the formatter side. 6818 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6819 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6820 6821 // FIXME: 6822 // This now gets parsed incorrectly as class definition. 6823 // verifyFormat("class A<int> f() {\n}\nint n;"); 6824 6825 // Elaborate types where incorrectly parsing the structural element would 6826 // break the indent. 6827 verifyFormat("if (true)\n" 6828 " class X x;\n" 6829 "else\n" 6830 " f();\n"); 6831 6832 // This is simply incomplete. Formatting is not important, but must not crash. 6833 verifyFormat("class A:"); 6834 } 6835 6836 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6837 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6838 format("#error Leave all white!!!!! space* alone!\n")); 6839 EXPECT_EQ( 6840 "#warning Leave all white!!!!! space* alone!\n", 6841 format("#warning Leave all white!!!!! space* alone!\n")); 6842 EXPECT_EQ("#error 1", format(" # error 1")); 6843 EXPECT_EQ("#warning 1", format(" # warning 1")); 6844 } 6845 6846 TEST_F(FormatTest, FormatHashIfExpressions) { 6847 verifyFormat("#if AAAA && BBBB"); 6848 verifyFormat("#if (AAAA && BBBB)"); 6849 verifyFormat("#elif (AAAA && BBBB)"); 6850 // FIXME: Come up with a better indentation for #elif. 6851 verifyFormat( 6852 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6853 " defined(BBBBBBBB)\n" 6854 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6855 " defined(BBBBBBBB)\n" 6856 "#endif", 6857 getLLVMStyleWithColumns(65)); 6858 } 6859 6860 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6861 FormatStyle AllowsMergedIf = getGoogleStyle(); 6862 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6863 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6864 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6865 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6866 EXPECT_EQ("if (true) return 42;", 6867 format("if (true)\nreturn 42;", AllowsMergedIf)); 6868 FormatStyle ShortMergedIf = AllowsMergedIf; 6869 ShortMergedIf.ColumnLimit = 25; 6870 verifyFormat("#define A \\\n" 6871 " if (true) return 42;", 6872 ShortMergedIf); 6873 verifyFormat("#define A \\\n" 6874 " f(); \\\n" 6875 " if (true)\n" 6876 "#define B", 6877 ShortMergedIf); 6878 verifyFormat("#define A \\\n" 6879 " f(); \\\n" 6880 " if (true)\n" 6881 "g();", 6882 ShortMergedIf); 6883 verifyFormat("{\n" 6884 "#ifdef A\n" 6885 " // Comment\n" 6886 " if (true) continue;\n" 6887 "#endif\n" 6888 " // Comment\n" 6889 " if (true) continue;\n" 6890 "}", 6891 ShortMergedIf); 6892 ShortMergedIf.ColumnLimit = 29; 6893 verifyFormat("#define A \\\n" 6894 " if (aaaaaaaaaa) return 1; \\\n" 6895 " return 2;", 6896 ShortMergedIf); 6897 ShortMergedIf.ColumnLimit = 28; 6898 verifyFormat("#define A \\\n" 6899 " if (aaaaaaaaaa) \\\n" 6900 " return 1; \\\n" 6901 " return 2;", 6902 ShortMergedIf); 6903 } 6904 6905 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6906 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6907 " f();\n" 6908 "}"); 6909 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6910 " f();\n" 6911 "} /* another comment */ else /* comment #3 */ {\n" 6912 " g();\n" 6913 "}"); 6914 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6915 " f();\n" 6916 "}"); 6917 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6918 " f();\n" 6919 "}"); 6920 verifyFormat("do /* a comment in a strange place */ {\n" 6921 " f();\n" 6922 "} /* another comment */ while (0);"); 6923 } 6924 6925 TEST_F(FormatTest, BlockComments) { 6926 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6927 format("/* *//* */ /* */\n/* *//* */ /* */")); 6928 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6929 EXPECT_EQ("#define A /*123*/ \\\n" 6930 " b\n" 6931 "/* */\n" 6932 "someCall(\n" 6933 " parameter);", 6934 format("#define A /*123*/ b\n" 6935 "/* */\n" 6936 "someCall(parameter);", 6937 getLLVMStyleWithColumns(15))); 6938 6939 EXPECT_EQ("#define A\n" 6940 "/* */ someCall(\n" 6941 " parameter);", 6942 format("#define A\n" 6943 "/* */someCall(parameter);", 6944 getLLVMStyleWithColumns(15))); 6945 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6946 EXPECT_EQ("/*\n" 6947 "*\n" 6948 " * aaaaaa\n" 6949 " * aaaaaa\n" 6950 "*/", 6951 format("/*\n" 6952 "*\n" 6953 " * aaaaaa aaaaaa\n" 6954 "*/", 6955 getLLVMStyleWithColumns(10))); 6956 EXPECT_EQ("/*\n" 6957 "**\n" 6958 "* aaaaaa\n" 6959 "*aaaaaa\n" 6960 "*/", 6961 format("/*\n" 6962 "**\n" 6963 "* aaaaaa aaaaaa\n" 6964 "*/", 6965 getLLVMStyleWithColumns(10))); 6966 6967 FormatStyle NoBinPacking = getLLVMStyle(); 6968 NoBinPacking.BinPackParameters = false; 6969 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6970 " 2, /* comment 2 */\n" 6971 " 3, /* comment 3 */\n" 6972 " aaaa,\n" 6973 " bbbb);", 6974 format("someFunction (1, /* comment 1 */\n" 6975 " 2, /* comment 2 */ \n" 6976 " 3, /* comment 3 */\n" 6977 "aaaa, bbbb );", 6978 NoBinPacking)); 6979 verifyFormat( 6980 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6982 EXPECT_EQ( 6983 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6984 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 6986 format( 6987 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6988 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 6990 EXPECT_EQ( 6991 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6992 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6993 "int cccccccccccccccccccccccccccccc; /* comment */\n", 6994 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6995 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6996 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 6997 6998 verifyFormat("void f(int * /* unused */) {}"); 6999 7000 EXPECT_EQ("/*\n" 7001 " **\n" 7002 " */", 7003 format("/*\n" 7004 " **\n" 7005 " */")); 7006 EXPECT_EQ("/*\n" 7007 " *q\n" 7008 " */", 7009 format("/*\n" 7010 " *q\n" 7011 " */")); 7012 EXPECT_EQ("/*\n" 7013 " * q\n" 7014 " */", 7015 format("/*\n" 7016 " * q\n" 7017 " */")); 7018 EXPECT_EQ("/*\n" 7019 " **/", 7020 format("/*\n" 7021 " **/")); 7022 EXPECT_EQ("/*\n" 7023 " ***/", 7024 format("/*\n" 7025 " ***/")); 7026 } 7027 7028 TEST_F(FormatTest, BlockCommentsInMacros) { 7029 EXPECT_EQ("#define A \\\n" 7030 " { \\\n" 7031 " /* one line */ \\\n" 7032 " someCall();", 7033 format("#define A { \\\n" 7034 " /* one line */ \\\n" 7035 " someCall();", 7036 getLLVMStyleWithColumns(20))); 7037 EXPECT_EQ("#define A \\\n" 7038 " { \\\n" 7039 " /* previous */ \\\n" 7040 " /* one line */ \\\n" 7041 " someCall();", 7042 format("#define A { \\\n" 7043 " /* previous */ \\\n" 7044 " /* one line */ \\\n" 7045 " someCall();", 7046 getLLVMStyleWithColumns(20))); 7047 } 7048 7049 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7050 EXPECT_EQ("a = {\n" 7051 " 1111 /* */\n" 7052 "};", 7053 format("a = {1111 /* */\n" 7054 "};", 7055 getLLVMStyleWithColumns(15))); 7056 EXPECT_EQ("a = {\n" 7057 " 1111 /* */\n" 7058 "};", 7059 format("a = {1111 /* */\n" 7060 "};", 7061 getLLVMStyleWithColumns(15))); 7062 7063 // FIXME: The formatting is still wrong here. 7064 EXPECT_EQ("a = {\n" 7065 " 1111 /* a\n" 7066 " */\n" 7067 "};", 7068 format("a = {1111 /* a */\n" 7069 "};", 7070 getLLVMStyleWithColumns(15))); 7071 } 7072 7073 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7074 // FIXME: This is not what we want... 7075 verifyFormat("{\n" 7076 "// a" 7077 "// b"); 7078 } 7079 7080 TEST_F(FormatTest, FormatStarDependingOnContext) { 7081 verifyFormat("void f(int *a);"); 7082 verifyFormat("void f() { f(fint * b); }"); 7083 verifyFormat("class A {\n void f(int *a);\n};"); 7084 verifyFormat("class A {\n int *a;\n};"); 7085 verifyFormat("namespace a {\n" 7086 "namespace b {\n" 7087 "class A {\n" 7088 " void f() {}\n" 7089 " int *a;\n" 7090 "};\n" 7091 "}\n" 7092 "}"); 7093 } 7094 7095 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7096 verifyFormat("while"); 7097 verifyFormat("operator"); 7098 } 7099 7100 //===----------------------------------------------------------------------===// 7101 // Objective-C tests. 7102 //===----------------------------------------------------------------------===// 7103 7104 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7105 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7106 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7107 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7108 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7109 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7110 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7111 format("-(NSInteger)Method3:(id)anObject;")); 7112 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7113 format("-(NSInteger)Method4:(id)anObject;")); 7114 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7115 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7116 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7117 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7118 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7119 "forAllCells:(BOOL)flag;", 7120 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7121 "forAllCells:(BOOL)flag;")); 7122 7123 // Very long objectiveC method declaration. 7124 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7125 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7126 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7127 " inRange:(NSRange)range\n" 7128 " outRange:(NSRange)out_range\n" 7129 " outRange1:(NSRange)out_range1\n" 7130 " outRange2:(NSRange)out_range2\n" 7131 " outRange3:(NSRange)out_range3\n" 7132 " outRange4:(NSRange)out_range4\n" 7133 " outRange5:(NSRange)out_range5\n" 7134 " outRange6:(NSRange)out_range6\n" 7135 " outRange7:(NSRange)out_range7\n" 7136 " outRange8:(NSRange)out_range8\n" 7137 " outRange9:(NSRange)out_range9;"); 7138 7139 // When the function name has to be wrapped. 7140 FormatStyle Style = getLLVMStyle(); 7141 Style.IndentWrappedFunctionNames = false; 7142 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7143 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7144 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7145 "}", 7146 Style); 7147 Style.IndentWrappedFunctionNames = true; 7148 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7149 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7150 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7151 "}", 7152 Style); 7153 7154 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7155 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7156 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7157 // protocol lists (but not for template classes): 7158 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7159 7160 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7161 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7162 7163 // If there's no return type (very rare in practice!), LLVM and Google style 7164 // agree. 7165 verifyFormat("- foo;"); 7166 verifyFormat("- foo:(int)f;"); 7167 verifyGoogleFormat("- foo:(int)foo;"); 7168 } 7169 7170 TEST_F(FormatTest, FormatObjCInterface) { 7171 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7172 "@public\n" 7173 " int field1;\n" 7174 "@protected\n" 7175 " int field2;\n" 7176 "@private\n" 7177 " int field3;\n" 7178 "@package\n" 7179 " int field4;\n" 7180 "}\n" 7181 "+ (id)init;\n" 7182 "@end"); 7183 7184 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7185 " @public\n" 7186 " int field1;\n" 7187 " @protected\n" 7188 " int field2;\n" 7189 " @private\n" 7190 " int field3;\n" 7191 " @package\n" 7192 " int field4;\n" 7193 "}\n" 7194 "+ (id)init;\n" 7195 "@end"); 7196 7197 verifyFormat("@interface /* wait for it */ Foo\n" 7198 "+ (id)init;\n" 7199 "// Look, a comment!\n" 7200 "- (int)answerWith:(int)i;\n" 7201 "@end"); 7202 7203 verifyFormat("@interface Foo\n" 7204 "@end\n" 7205 "@interface Bar\n" 7206 "@end"); 7207 7208 verifyFormat("@interface Foo : Bar\n" 7209 "+ (id)init;\n" 7210 "@end"); 7211 7212 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7213 "+ (id)init;\n" 7214 "@end"); 7215 7216 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7217 "+ (id)init;\n" 7218 "@end"); 7219 7220 verifyFormat("@interface Foo (HackStuff)\n" 7221 "+ (id)init;\n" 7222 "@end"); 7223 7224 verifyFormat("@interface Foo ()\n" 7225 "+ (id)init;\n" 7226 "@end"); 7227 7228 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7229 "+ (id)init;\n" 7230 "@end"); 7231 7232 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7233 "+ (id)init;\n" 7234 "@end"); 7235 7236 verifyFormat("@interface Foo {\n" 7237 " int _i;\n" 7238 "}\n" 7239 "+ (id)init;\n" 7240 "@end"); 7241 7242 verifyFormat("@interface Foo : Bar {\n" 7243 " int _i;\n" 7244 "}\n" 7245 "+ (id)init;\n" 7246 "@end"); 7247 7248 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7249 " int _i;\n" 7250 "}\n" 7251 "+ (id)init;\n" 7252 "@end"); 7253 7254 verifyFormat("@interface Foo (HackStuff) {\n" 7255 " int _i;\n" 7256 "}\n" 7257 "+ (id)init;\n" 7258 "@end"); 7259 7260 verifyFormat("@interface Foo () {\n" 7261 " int _i;\n" 7262 "}\n" 7263 "+ (id)init;\n" 7264 "@end"); 7265 7266 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7267 " int _i;\n" 7268 "}\n" 7269 "+ (id)init;\n" 7270 "@end"); 7271 7272 FormatStyle OnePerLine = getGoogleStyle(); 7273 OnePerLine.BinPackParameters = false; 7274 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7279 "}", 7280 OnePerLine); 7281 } 7282 7283 TEST_F(FormatTest, FormatObjCImplementation) { 7284 verifyFormat("@implementation Foo : NSObject {\n" 7285 "@public\n" 7286 " int field1;\n" 7287 "@protected\n" 7288 " int field2;\n" 7289 "@private\n" 7290 " int field3;\n" 7291 "@package\n" 7292 " int field4;\n" 7293 "}\n" 7294 "+ (id)init {\n}\n" 7295 "@end"); 7296 7297 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7298 " @public\n" 7299 " int field1;\n" 7300 " @protected\n" 7301 " int field2;\n" 7302 " @private\n" 7303 " int field3;\n" 7304 " @package\n" 7305 " int field4;\n" 7306 "}\n" 7307 "+ (id)init {\n}\n" 7308 "@end"); 7309 7310 verifyFormat("@implementation Foo\n" 7311 "+ (id)init {\n" 7312 " if (true)\n" 7313 " return nil;\n" 7314 "}\n" 7315 "// Look, a comment!\n" 7316 "- (int)answerWith:(int)i {\n" 7317 " return i;\n" 7318 "}\n" 7319 "+ (int)answerWith:(int)i {\n" 7320 " return i;\n" 7321 "}\n" 7322 "@end"); 7323 7324 verifyFormat("@implementation Foo\n" 7325 "@end\n" 7326 "@implementation Bar\n" 7327 "@end"); 7328 7329 EXPECT_EQ("@implementation Foo : Bar\n" 7330 "+ (id)init {\n}\n" 7331 "- (void)foo {\n}\n" 7332 "@end", 7333 format("@implementation Foo : Bar\n" 7334 "+(id)init{}\n" 7335 "-(void)foo{}\n" 7336 "@end")); 7337 7338 verifyFormat("@implementation Foo {\n" 7339 " int _i;\n" 7340 "}\n" 7341 "+ (id)init {\n}\n" 7342 "@end"); 7343 7344 verifyFormat("@implementation Foo : Bar {\n" 7345 " int _i;\n" 7346 "}\n" 7347 "+ (id)init {\n}\n" 7348 "@end"); 7349 7350 verifyFormat("@implementation Foo (HackStuff)\n" 7351 "+ (id)init {\n}\n" 7352 "@end"); 7353 verifyFormat("@implementation ObjcClass\n" 7354 "- (void)method;\n" 7355 "{}\n" 7356 "@end"); 7357 } 7358 7359 TEST_F(FormatTest, FormatObjCProtocol) { 7360 verifyFormat("@protocol Foo\n" 7361 "@property(weak) id delegate;\n" 7362 "- (NSUInteger)numberOfThings;\n" 7363 "@end"); 7364 7365 verifyFormat("@protocol MyProtocol <NSObject>\n" 7366 "- (NSUInteger)numberOfThings;\n" 7367 "@end"); 7368 7369 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7370 "- (NSUInteger)numberOfThings;\n" 7371 "@end"); 7372 7373 verifyFormat("@protocol Foo;\n" 7374 "@protocol Bar;\n"); 7375 7376 verifyFormat("@protocol Foo\n" 7377 "@end\n" 7378 "@protocol Bar\n" 7379 "@end"); 7380 7381 verifyFormat("@protocol myProtocol\n" 7382 "- (void)mandatoryWithInt:(int)i;\n" 7383 "@optional\n" 7384 "- (void)optional;\n" 7385 "@required\n" 7386 "- (void)required;\n" 7387 "@optional\n" 7388 "@property(assign) int madProp;\n" 7389 "@end\n"); 7390 7391 verifyFormat("@property(nonatomic, assign, readonly)\n" 7392 " int *looooooooooooooooooooooooooooongNumber;\n" 7393 "@property(nonatomic, assign, readonly)\n" 7394 " NSString *looooooooooooooooooooooooooooongName;"); 7395 7396 verifyFormat("@implementation PR18406\n" 7397 "}\n" 7398 "@end"); 7399 } 7400 7401 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7402 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7403 " rect:(NSRect)theRect\n" 7404 " interval:(float)theInterval {\n" 7405 "}"); 7406 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7407 " longKeyword:(NSRect)theRect\n" 7408 " longerKeyword:(float)theInterval\n" 7409 " error:(NSError **)theError {\n" 7410 "}"); 7411 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7412 " longKeyword:(NSRect)theRect\n" 7413 " evenLongerKeyword:(float)theInterval\n" 7414 " error:(NSError **)theError {\n" 7415 "}"); 7416 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7417 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7418 " NS_DESIGNATED_INITIALIZER;", 7419 getLLVMStyleWithColumns(60)); 7420 7421 // Continuation indent width should win over aligning colons if the function 7422 // name is long. 7423 FormatStyle continuationStyle = getGoogleStyle(); 7424 continuationStyle.ColumnLimit = 40; 7425 continuationStyle.IndentWrappedFunctionNames = true; 7426 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7427 " dontAlignNamef:(NSRect)theRect {\n" 7428 "}", 7429 continuationStyle); 7430 7431 // Make sure we don't break aligning for short parameter names. 7432 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7433 " aShortf:(NSRect)theRect {\n" 7434 "}", 7435 continuationStyle); 7436 } 7437 7438 TEST_F(FormatTest, FormatObjCMethodExpr) { 7439 verifyFormat("[foo bar:baz];"); 7440 verifyFormat("return [foo bar:baz];"); 7441 verifyFormat("return (a)[foo bar:baz];"); 7442 verifyFormat("f([foo bar:baz]);"); 7443 verifyFormat("f(2, [foo bar:baz]);"); 7444 verifyFormat("f(2, a ? b : c);"); 7445 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7446 7447 // Unary operators. 7448 verifyFormat("int a = +[foo bar:baz];"); 7449 verifyFormat("int a = -[foo bar:baz];"); 7450 verifyFormat("int a = ![foo bar:baz];"); 7451 verifyFormat("int a = ~[foo bar:baz];"); 7452 verifyFormat("int a = ++[foo bar:baz];"); 7453 verifyFormat("int a = --[foo bar:baz];"); 7454 verifyFormat("int a = sizeof [foo bar:baz];"); 7455 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7456 verifyFormat("int a = &[foo bar:baz];"); 7457 verifyFormat("int a = *[foo bar:baz];"); 7458 // FIXME: Make casts work, without breaking f()[4]. 7459 // verifyFormat("int a = (int)[foo bar:baz];"); 7460 // verifyFormat("return (int)[foo bar:baz];"); 7461 // verifyFormat("(void)[foo bar:baz];"); 7462 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7463 7464 // Binary operators. 7465 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7466 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7467 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7468 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7469 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7470 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7471 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7472 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7473 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7474 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7475 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7476 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7477 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7478 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7479 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7480 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7481 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7482 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7483 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7484 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7485 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7486 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7487 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7488 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7489 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7490 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7491 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7492 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7493 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7494 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7495 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7496 // Whew! 7497 7498 verifyFormat("return in[42];"); 7499 verifyFormat("for (auto v : in[1]) {\n}"); 7500 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7501 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7502 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7503 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7504 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7505 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7506 "}"); 7507 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7508 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7509 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7510 7511 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7512 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7513 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7514 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7515 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7516 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7517 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7518 7519 verifyFormat("arr[[self indexForFoo:a]];"); 7520 verifyFormat("throw [self errorFor:a];"); 7521 verifyFormat("@throw [self errorFor:a];"); 7522 7523 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7524 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7525 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7526 7527 // This tests that the formatter doesn't break after "backing" but before ":", 7528 // which would be at 80 columns. 7529 verifyFormat( 7530 "void f() {\n" 7531 " if ((self = [super initWithContentRect:contentRect\n" 7532 " styleMask:styleMask ?: otherMask\n" 7533 " backing:NSBackingStoreBuffered\n" 7534 " defer:YES]))"); 7535 7536 verifyFormat( 7537 "[foo checkThatBreakingAfterColonWorksOk:\n" 7538 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7539 7540 verifyFormat("[myObj short:arg1 // Force line break\n" 7541 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7542 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7543 " error:arg4];"); 7544 verifyFormat( 7545 "void f() {\n" 7546 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7547 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7548 " pos.width(), pos.height())\n" 7549 " styleMask:NSBorderlessWindowMask\n" 7550 " backing:NSBackingStoreBuffered\n" 7551 " defer:NO]);\n" 7552 "}"); 7553 verifyFormat( 7554 "void f() {\n" 7555 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7556 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7557 " pos.width(), pos.height())\n" 7558 " syeMask:NSBorderlessWindowMask\n" 7559 " bking:NSBackingStoreBuffered\n" 7560 " der:NO]);\n" 7561 "}", 7562 getLLVMStyleWithColumns(70)); 7563 verifyFormat( 7564 "void f() {\n" 7565 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7566 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7567 " pos.width(), pos.height())\n" 7568 " styleMask:NSBorderlessWindowMask\n" 7569 " backing:NSBackingStoreBuffered\n" 7570 " defer:NO]);\n" 7571 "}", 7572 getChromiumStyle(FormatStyle::LK_Cpp)); 7573 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7574 " with:contentsNativeView];"); 7575 7576 verifyFormat( 7577 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7578 " owner:nillllll];"); 7579 7580 verifyFormat( 7581 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7582 " forType:kBookmarkButtonDragType];"); 7583 7584 verifyFormat("[defaultCenter addObserver:self\n" 7585 " selector:@selector(willEnterFullscreen)\n" 7586 " name:kWillEnterFullscreenNotification\n" 7587 " object:nil];"); 7588 verifyFormat("[image_rep drawInRect:drawRect\n" 7589 " fromRect:NSZeroRect\n" 7590 " operation:NSCompositeCopy\n" 7591 " fraction:1.0\n" 7592 " respectFlipped:NO\n" 7593 " hints:nil];"); 7594 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7596 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7598 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7599 " aaaaaaaaaaaaaaaaaaaaaa];"); 7600 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7601 " .aaaaaaaa];", // FIXME: Indentation seems off. 7602 getLLVMStyleWithColumns(60)); 7603 7604 verifyFormat( 7605 "scoped_nsobject<NSTextField> message(\n" 7606 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7607 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7608 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7609 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7610 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7611 " aaaa:bbb];"); 7612 verifyFormat("[self param:function( //\n" 7613 " parameter)]"); 7614 verifyFormat( 7615 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7616 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7617 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7618 7619 // FIXME: This violates the column limit. 7620 verifyFormat( 7621 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7622 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7623 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7624 getLLVMStyleWithColumns(60)); 7625 7626 // Variadic parameters. 7627 verifyFormat( 7628 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7629 verifyFormat( 7630 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7631 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7632 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7633 verifyFormat("[self // break\n" 7634 " a:a\n" 7635 " aaa:aaa];"); 7636 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7637 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7638 } 7639 7640 TEST_F(FormatTest, ObjCAt) { 7641 verifyFormat("@autoreleasepool"); 7642 verifyFormat("@catch"); 7643 verifyFormat("@class"); 7644 verifyFormat("@compatibility_alias"); 7645 verifyFormat("@defs"); 7646 verifyFormat("@dynamic"); 7647 verifyFormat("@encode"); 7648 verifyFormat("@end"); 7649 verifyFormat("@finally"); 7650 verifyFormat("@implementation"); 7651 verifyFormat("@import"); 7652 verifyFormat("@interface"); 7653 verifyFormat("@optional"); 7654 verifyFormat("@package"); 7655 verifyFormat("@private"); 7656 verifyFormat("@property"); 7657 verifyFormat("@protected"); 7658 verifyFormat("@protocol"); 7659 verifyFormat("@public"); 7660 verifyFormat("@required"); 7661 verifyFormat("@selector"); 7662 verifyFormat("@synchronized"); 7663 verifyFormat("@synthesize"); 7664 verifyFormat("@throw"); 7665 verifyFormat("@try"); 7666 7667 EXPECT_EQ("@interface", format("@ interface")); 7668 7669 // The precise formatting of this doesn't matter, nobody writes code like 7670 // this. 7671 verifyFormat("@ /*foo*/ interface"); 7672 } 7673 7674 TEST_F(FormatTest, ObjCSnippets) { 7675 verifyFormat("@autoreleasepool {\n" 7676 " foo();\n" 7677 "}"); 7678 verifyFormat("@class Foo, Bar;"); 7679 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7680 verifyFormat("@dynamic textColor;"); 7681 verifyFormat("char *buf1 = @encode(int *);"); 7682 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7683 verifyFormat("char *buf1 = @encode(int **);"); 7684 verifyFormat("Protocol *proto = @protocol(p1);"); 7685 verifyFormat("SEL s = @selector(foo:);"); 7686 verifyFormat("@synchronized(self) {\n" 7687 " f();\n" 7688 "}"); 7689 7690 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7691 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7692 7693 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7694 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7695 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7696 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7697 getMozillaStyle()); 7698 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7699 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7700 getWebKitStyle()); 7701 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7702 7703 verifyFormat("@import foo.bar;\n" 7704 "@import baz;"); 7705 } 7706 7707 TEST_F(FormatTest, ObjCForIn) { 7708 verifyFormat("- (void)test {\n" 7709 " for (NSString *n in arrayOfStrings) {\n" 7710 " foo(n);\n" 7711 " }\n" 7712 "}"); 7713 verifyFormat("- (void)test {\n" 7714 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7715 " foo(n);\n" 7716 " }\n" 7717 "}"); 7718 } 7719 7720 TEST_F(FormatTest, ObjCLiterals) { 7721 verifyFormat("@\"String\""); 7722 verifyFormat("@1"); 7723 verifyFormat("@+4.8"); 7724 verifyFormat("@-4"); 7725 verifyFormat("@1LL"); 7726 verifyFormat("@.5"); 7727 verifyFormat("@'c'"); 7728 verifyFormat("@true"); 7729 7730 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7731 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7732 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7733 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7734 7735 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7736 } 7737 7738 TEST_F(FormatTest, ObjCDictLiterals) { 7739 verifyFormat("@{"); 7740 verifyFormat("@{}"); 7741 verifyFormat("@{@\"one\" : @1}"); 7742 verifyFormat("return @{@\"one\" : @1;"); 7743 verifyFormat("@{@\"one\" : @1}"); 7744 7745 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7746 verifyFormat("@{\n" 7747 " @\"one\" : @{@2 : @1},\n" 7748 "}"); 7749 7750 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7751 verifyIncompleteFormat("[self setDict:@{}"); 7752 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7753 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7754 verifyFormat( 7755 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7756 verifyFormat( 7757 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7758 7759 verifyFormat("NSDictionary *d = @{\n" 7760 " @\"nam\" : NSUserNam(),\n" 7761 " @\"dte\" : [NSDate date],\n" 7762 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7763 "};"); 7764 verifyFormat( 7765 "@{\n" 7766 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7767 "regularFont,\n" 7768 "};"); 7769 verifyGoogleFormat( 7770 "@{\n" 7771 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7772 "regularFont,\n" 7773 "};"); 7774 verifyFormat( 7775 "@{\n" 7776 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7777 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7778 "};"); 7779 7780 // We should try to be robust in case someone forgets the "@". 7781 verifyFormat("NSDictionary *d = {\n" 7782 " @\"nam\" : NSUserNam(),\n" 7783 " @\"dte\" : [NSDate date],\n" 7784 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7785 "};"); 7786 verifyFormat("NSMutableDictionary *dictionary =\n" 7787 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7788 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7789 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7790 " cccccccccccccccc : ccccccccccccccc\n" 7791 " }];"); 7792 7793 // Ensure that casts before the key are kept on the same line as the key. 7794 verifyFormat( 7795 "NSDictionary *d = @{\n" 7796 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7797 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7798 "};"); 7799 } 7800 7801 TEST_F(FormatTest, ObjCArrayLiterals) { 7802 verifyIncompleteFormat("@["); 7803 verifyFormat("@[]"); 7804 verifyFormat( 7805 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7806 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7807 verifyFormat("NSArray *array = @[ [foo description] ];"); 7808 7809 verifyFormat( 7810 "NSArray *some_variable = @[\n" 7811 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7812 " @\"aaaaaaaaaaaaaaaaa\",\n" 7813 " @\"aaaaaaaaaaaaaaaaa\",\n" 7814 " @\"aaaaaaaaaaaaaaaaa\",\n" 7815 "];"); 7816 verifyFormat( 7817 "NSArray *some_variable = @[\n" 7818 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7819 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7820 "];"); 7821 verifyFormat("NSArray *some_variable = @[\n" 7822 " @\"aaaaaaaaaaaaaaaaa\",\n" 7823 " @\"aaaaaaaaaaaaaaaaa\",\n" 7824 " @\"aaaaaaaaaaaaaaaaa\",\n" 7825 " @\"aaaaaaaaaaaaaaaaa\",\n" 7826 "];"); 7827 verifyFormat("NSArray *array = @[\n" 7828 " @\"a\",\n" 7829 " @\"a\",\n" // Trailing comma -> one per line. 7830 "];"); 7831 7832 // We should try to be robust in case someone forgets the "@". 7833 verifyFormat("NSArray *some_variable = [\n" 7834 " @\"aaaaaaaaaaaaaaaaa\",\n" 7835 " @\"aaaaaaaaaaaaaaaaa\",\n" 7836 " @\"aaaaaaaaaaaaaaaaa\",\n" 7837 " @\"aaaaaaaaaaaaaaaaa\",\n" 7838 "];"); 7839 verifyFormat( 7840 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7841 " index:(NSUInteger)index\n" 7842 " nonDigitAttributes:\n" 7843 " (NSDictionary *)noDigitAttributes;"); 7844 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7845 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7846 "]];"); 7847 } 7848 7849 TEST_F(FormatTest, BreaksStringLiterals) { 7850 EXPECT_EQ("\"some text \"\n" 7851 "\"other\";", 7852 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7853 EXPECT_EQ("\"some text \"\n" 7854 "\"other\";", 7855 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7856 EXPECT_EQ( 7857 "#define A \\\n" 7858 " \"some \" \\\n" 7859 " \"text \" \\\n" 7860 " \"other\";", 7861 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7862 EXPECT_EQ( 7863 "#define A \\\n" 7864 " \"so \" \\\n" 7865 " \"text \" \\\n" 7866 " \"other\";", 7867 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7868 7869 EXPECT_EQ("\"some text\"", 7870 format("\"some text\"", getLLVMStyleWithColumns(1))); 7871 EXPECT_EQ("\"some text\"", 7872 format("\"some text\"", getLLVMStyleWithColumns(11))); 7873 EXPECT_EQ("\"some \"\n" 7874 "\"text\"", 7875 format("\"some text\"", getLLVMStyleWithColumns(10))); 7876 EXPECT_EQ("\"some \"\n" 7877 "\"text\"", 7878 format("\"some text\"", getLLVMStyleWithColumns(7))); 7879 EXPECT_EQ("\"some\"\n" 7880 "\" tex\"\n" 7881 "\"t\"", 7882 format("\"some text\"", getLLVMStyleWithColumns(6))); 7883 EXPECT_EQ("\"some\"\n" 7884 "\" tex\"\n" 7885 "\" and\"", 7886 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7887 EXPECT_EQ("\"some\"\n" 7888 "\"/tex\"\n" 7889 "\"/and\"", 7890 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7891 7892 EXPECT_EQ("variable =\n" 7893 " \"long string \"\n" 7894 " \"literal\";", 7895 format("variable = \"long string literal\";", 7896 getLLVMStyleWithColumns(20))); 7897 7898 EXPECT_EQ("variable = f(\n" 7899 " \"long string \"\n" 7900 " \"literal\",\n" 7901 " short,\n" 7902 " loooooooooooooooooooong);", 7903 format("variable = f(\"long string literal\", short, " 7904 "loooooooooooooooooooong);", 7905 getLLVMStyleWithColumns(20))); 7906 7907 EXPECT_EQ( 7908 "f(g(\"long string \"\n" 7909 " \"literal\"),\n" 7910 " b);", 7911 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7912 EXPECT_EQ("f(g(\"long string \"\n" 7913 " \"literal\",\n" 7914 " a),\n" 7915 " b);", 7916 format("f(g(\"long string literal\", a), b);", 7917 getLLVMStyleWithColumns(20))); 7918 EXPECT_EQ( 7919 "f(\"one two\".split(\n" 7920 " variable));", 7921 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7922 EXPECT_EQ("f(\"one two three four five six \"\n" 7923 " \"seven\".split(\n" 7924 " really_looooong_variable));", 7925 format("f(\"one two three four five six seven\"." 7926 "split(really_looooong_variable));", 7927 getLLVMStyleWithColumns(33))); 7928 7929 EXPECT_EQ("f(\"some \"\n" 7930 " \"text\",\n" 7931 " other);", 7932 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7933 7934 // Only break as a last resort. 7935 verifyFormat( 7936 "aaaaaaaaaaaaaaaaaaaa(\n" 7937 " aaaaaaaaaaaaaaaaaaaa,\n" 7938 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7939 7940 EXPECT_EQ("\"splitmea\"\n" 7941 "\"trandomp\"\n" 7942 "\"oint\"", 7943 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7944 7945 EXPECT_EQ("\"split/\"\n" 7946 "\"pathat/\"\n" 7947 "\"slashes\"", 7948 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7949 7950 EXPECT_EQ("\"split/\"\n" 7951 "\"pathat/\"\n" 7952 "\"slashes\"", 7953 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7954 EXPECT_EQ("\"split at \"\n" 7955 "\"spaces/at/\"\n" 7956 "\"slashes.at.any$\"\n" 7957 "\"non-alphanumeric%\"\n" 7958 "\"1111111111characte\"\n" 7959 "\"rs\"", 7960 format("\"split at " 7961 "spaces/at/" 7962 "slashes.at." 7963 "any$non-" 7964 "alphanumeric%" 7965 "1111111111characte" 7966 "rs\"", 7967 getLLVMStyleWithColumns(20))); 7968 7969 // Verify that splitting the strings understands 7970 // Style::AlwaysBreakBeforeMultilineStrings. 7971 EXPECT_EQ( 7972 "aaaaaaaaaaaa(\n" 7973 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7974 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7975 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7976 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7977 "aaaaaaaaaaaaaaaaaaaaaa\");", 7978 getGoogleStyle())); 7979 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7980 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7981 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7982 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7983 "aaaaaaaaaaaaaaaaaaaaaa\";", 7984 getGoogleStyle())); 7985 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7986 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7987 format("llvm::outs() << " 7988 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7989 "aaaaaaaaaaaaaaaaaaa\";")); 7990 EXPECT_EQ("ffff(\n" 7991 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7992 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7993 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7994 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7995 getGoogleStyle())); 7996 7997 FormatStyle Style = getLLVMStyleWithColumns(12); 7998 Style.BreakStringLiterals = false; 7999 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8000 8001 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8002 AlignLeft.AlignEscapedNewlinesLeft = true; 8003 EXPECT_EQ("#define A \\\n" 8004 " \"some \" \\\n" 8005 " \"text \" \\\n" 8006 " \"other\";", 8007 format("#define A \"some text other\";", AlignLeft)); 8008 } 8009 8010 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8011 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8012 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8013 EXPECT_EQ("int i = a(b());", 8014 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8015 } 8016 8017 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8018 EXPECT_EQ( 8019 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8020 "(\n" 8021 " \"x\t\");", 8022 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8023 "aaaaaaa(" 8024 "\"x\t\");")); 8025 } 8026 8027 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8028 EXPECT_EQ( 8029 "u8\"utf8 string \"\n" 8030 "u8\"literal\";", 8031 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8032 EXPECT_EQ( 8033 "u\"utf16 string \"\n" 8034 "u\"literal\";", 8035 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8036 EXPECT_EQ( 8037 "U\"utf32 string \"\n" 8038 "U\"literal\";", 8039 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8040 EXPECT_EQ("L\"wide string \"\n" 8041 "L\"literal\";", 8042 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8043 EXPECT_EQ("@\"NSString \"\n" 8044 "@\"literal\";", 8045 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8046 8047 // This input makes clang-format try to split the incomplete unicode escape 8048 // sequence, which used to lead to a crasher. 8049 verifyNoCrash( 8050 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8051 getLLVMStyleWithColumns(60)); 8052 } 8053 8054 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8055 FormatStyle Style = getGoogleStyleWithColumns(15); 8056 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8057 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8058 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8059 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8060 EXPECT_EQ("u8R\"x(raw literal)x\";", 8061 format("u8R\"x(raw literal)x\";", Style)); 8062 } 8063 8064 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8065 FormatStyle Style = getLLVMStyleWithColumns(20); 8066 EXPECT_EQ( 8067 "_T(\"aaaaaaaaaaaaaa\")\n" 8068 "_T(\"aaaaaaaaaaaaaa\")\n" 8069 "_T(\"aaaaaaaaaaaa\")", 8070 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8071 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8072 " _T(\"aaaaaa\"),\n" 8073 " z);", 8074 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8075 8076 // FIXME: Handle embedded spaces in one iteration. 8077 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8078 // "_T(\"aaaaaaaaaaaaa\")\n" 8079 // "_T(\"aaaaaaaaaaaaa\")\n" 8080 // "_T(\"a\")", 8081 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8082 // getLLVMStyleWithColumns(20))); 8083 EXPECT_EQ( 8084 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8085 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8086 EXPECT_EQ("f(\n" 8087 "#if !TEST\n" 8088 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8089 "#endif\n" 8090 " );", 8091 format("f(\n" 8092 "#if !TEST\n" 8093 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8094 "#endif\n" 8095 ");")); 8096 EXPECT_EQ("f(\n" 8097 "\n" 8098 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8099 format("f(\n" 8100 "\n" 8101 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8102 } 8103 8104 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8105 EXPECT_EQ( 8106 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8109 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8112 } 8113 8114 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8115 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8116 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8117 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8118 "multiline raw string literal xxxxxxxxxxxxxx\n" 8119 ")x\",\n" 8120 " a),\n" 8121 " b);", 8122 format("fffffffffff(g(R\"x(\n" 8123 "multiline raw string literal xxxxxxxxxxxxxx\n" 8124 ")x\", a), b);", 8125 getGoogleStyleWithColumns(20))); 8126 EXPECT_EQ("fffffffffff(\n" 8127 " g(R\"x(qqq\n" 8128 "multiline raw string literal xxxxxxxxxxxxxx\n" 8129 ")x\",\n" 8130 " a),\n" 8131 " b);", 8132 format("fffffffffff(g(R\"x(qqq\n" 8133 "multiline raw string literal xxxxxxxxxxxxxx\n" 8134 ")x\", a), b);", 8135 getGoogleStyleWithColumns(20))); 8136 8137 EXPECT_EQ("fffffffffff(R\"x(\n" 8138 "multiline raw string literal xxxxxxxxxxxxxx\n" 8139 ")x\");", 8140 format("fffffffffff(R\"x(\n" 8141 "multiline raw string literal xxxxxxxxxxxxxx\n" 8142 ")x\");", 8143 getGoogleStyleWithColumns(20))); 8144 EXPECT_EQ("fffffffffff(R\"x(\n" 8145 "multiline raw string literal xxxxxxxxxxxxxx\n" 8146 ")x\" + bbbbbb);", 8147 format("fffffffffff(R\"x(\n" 8148 "multiline raw string literal xxxxxxxxxxxxxx\n" 8149 ")x\" + bbbbbb);", 8150 getGoogleStyleWithColumns(20))); 8151 EXPECT_EQ("fffffffffff(\n" 8152 " R\"x(\n" 8153 "multiline raw string literal xxxxxxxxxxxxxx\n" 8154 ")x\" +\n" 8155 " bbbbbb);", 8156 format("fffffffffff(\n" 8157 " R\"x(\n" 8158 "multiline raw string literal xxxxxxxxxxxxxx\n" 8159 ")x\" + bbbbbb);", 8160 getGoogleStyleWithColumns(20))); 8161 } 8162 8163 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8164 verifyFormat("string a = \"unterminated;"); 8165 EXPECT_EQ("function(\"unterminated,\n" 8166 " OtherParameter);", 8167 format("function( \"unterminated,\n" 8168 " OtherParameter);")); 8169 } 8170 8171 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8172 FormatStyle Style = getLLVMStyle(); 8173 Style.Standard = FormatStyle::LS_Cpp03; 8174 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8175 format("#define x(_a) printf(\"foo\"_a);", Style)); 8176 } 8177 8178 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8179 8180 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8181 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8182 " \"ddeeefff\");", 8183 format("someFunction(\"aaabbbcccdddeeefff\");", 8184 getLLVMStyleWithColumns(25))); 8185 EXPECT_EQ("someFunction1234567890(\n" 8186 " \"aaabbbcccdddeeefff\");", 8187 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8188 getLLVMStyleWithColumns(26))); 8189 EXPECT_EQ("someFunction1234567890(\n" 8190 " \"aaabbbcccdddeeeff\"\n" 8191 " \"f\");", 8192 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8193 getLLVMStyleWithColumns(25))); 8194 EXPECT_EQ("someFunction1234567890(\n" 8195 " \"aaabbbcccdddeeeff\"\n" 8196 " \"f\");", 8197 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8198 getLLVMStyleWithColumns(24))); 8199 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8200 " \"ddde \"\n" 8201 " \"efff\");", 8202 format("someFunction(\"aaabbbcc ddde efff\");", 8203 getLLVMStyleWithColumns(25))); 8204 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8205 " \"ddeeefff\");", 8206 format("someFunction(\"aaabbbccc ddeeefff\");", 8207 getLLVMStyleWithColumns(25))); 8208 EXPECT_EQ("someFunction1234567890(\n" 8209 " \"aaabb \"\n" 8210 " \"cccdddeeefff\");", 8211 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8212 getLLVMStyleWithColumns(25))); 8213 EXPECT_EQ("#define A \\\n" 8214 " string s = \\\n" 8215 " \"123456789\" \\\n" 8216 " \"0\"; \\\n" 8217 " int i;", 8218 format("#define A string s = \"1234567890\"; int i;", 8219 getLLVMStyleWithColumns(20))); 8220 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8221 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8222 " \"dddeeeff\"\n" 8223 " \"f\");", 8224 format("someFunction(\"aaabbbcc dddeeefff\");", 8225 getLLVMStyleWithColumns(25))); 8226 } 8227 8228 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8229 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8230 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8231 EXPECT_EQ("\"test\"\n" 8232 "\"\\n\"", 8233 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8234 EXPECT_EQ("\"tes\\\\\"\n" 8235 "\"n\"", 8236 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8237 EXPECT_EQ("\"\\\\\\\\\"\n" 8238 "\"\\n\"", 8239 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8240 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8241 EXPECT_EQ("\"\\uff01\"\n" 8242 "\"test\"", 8243 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8244 EXPECT_EQ("\"\\Uff01ff02\"", 8245 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8246 EXPECT_EQ("\"\\x000000000001\"\n" 8247 "\"next\"", 8248 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8249 EXPECT_EQ("\"\\x000000000001next\"", 8250 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8251 EXPECT_EQ("\"\\x000000000001\"", 8252 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8253 EXPECT_EQ("\"test\"\n" 8254 "\"\\000000\"\n" 8255 "\"000001\"", 8256 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8257 EXPECT_EQ("\"test\\000\"\n" 8258 "\"00000000\"\n" 8259 "\"1\"", 8260 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8261 } 8262 8263 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8264 verifyFormat("void f() {\n" 8265 " return g() {}\n" 8266 " void h() {}"); 8267 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8268 "g();\n" 8269 "}"); 8270 } 8271 8272 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8273 verifyFormat( 8274 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8275 } 8276 8277 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8278 verifyFormat("class X {\n" 8279 " void f() {\n" 8280 " }\n" 8281 "};", 8282 getLLVMStyleWithColumns(12)); 8283 } 8284 8285 TEST_F(FormatTest, ConfigurableIndentWidth) { 8286 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8287 EightIndent.IndentWidth = 8; 8288 EightIndent.ContinuationIndentWidth = 8; 8289 verifyFormat("void f() {\n" 8290 " someFunction();\n" 8291 " if (true) {\n" 8292 " f();\n" 8293 " }\n" 8294 "}", 8295 EightIndent); 8296 verifyFormat("class X {\n" 8297 " void f() {\n" 8298 " }\n" 8299 "};", 8300 EightIndent); 8301 verifyFormat("int x[] = {\n" 8302 " call(),\n" 8303 " call()};", 8304 EightIndent); 8305 } 8306 8307 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8308 verifyFormat("double\n" 8309 "f();", 8310 getLLVMStyleWithColumns(8)); 8311 } 8312 8313 TEST_F(FormatTest, ConfigurableUseOfTab) { 8314 FormatStyle Tab = getLLVMStyleWithColumns(42); 8315 Tab.IndentWidth = 8; 8316 Tab.UseTab = FormatStyle::UT_Always; 8317 Tab.AlignEscapedNewlinesLeft = true; 8318 8319 EXPECT_EQ("if (aaaaaaaa && // q\n" 8320 " bb)\t\t// w\n" 8321 "\t;", 8322 format("if (aaaaaaaa &&// q\n" 8323 "bb)// w\n" 8324 ";", 8325 Tab)); 8326 EXPECT_EQ("if (aaa && bbb) // w\n" 8327 "\t;", 8328 format("if(aaa&&bbb)// w\n" 8329 ";", 8330 Tab)); 8331 8332 verifyFormat("class X {\n" 8333 "\tvoid f() {\n" 8334 "\t\tsomeFunction(parameter1,\n" 8335 "\t\t\t parameter2);\n" 8336 "\t}\n" 8337 "};", 8338 Tab); 8339 verifyFormat("#define A \\\n" 8340 "\tvoid f() { \\\n" 8341 "\t\tsomeFunction( \\\n" 8342 "\t\t parameter1, \\\n" 8343 "\t\t parameter2); \\\n" 8344 "\t}", 8345 Tab); 8346 8347 Tab.TabWidth = 4; 8348 Tab.IndentWidth = 8; 8349 verifyFormat("class TabWidth4Indent8 {\n" 8350 "\t\tvoid f() {\n" 8351 "\t\t\t\tsomeFunction(parameter1,\n" 8352 "\t\t\t\t\t\t\t parameter2);\n" 8353 "\t\t}\n" 8354 "};", 8355 Tab); 8356 8357 Tab.TabWidth = 4; 8358 Tab.IndentWidth = 4; 8359 verifyFormat("class TabWidth4Indent4 {\n" 8360 "\tvoid f() {\n" 8361 "\t\tsomeFunction(parameter1,\n" 8362 "\t\t\t\t\t parameter2);\n" 8363 "\t}\n" 8364 "};", 8365 Tab); 8366 8367 Tab.TabWidth = 8; 8368 Tab.IndentWidth = 4; 8369 verifyFormat("class TabWidth8Indent4 {\n" 8370 " void f() {\n" 8371 "\tsomeFunction(parameter1,\n" 8372 "\t\t parameter2);\n" 8373 " }\n" 8374 "};", 8375 Tab); 8376 8377 Tab.TabWidth = 8; 8378 Tab.IndentWidth = 8; 8379 EXPECT_EQ("/*\n" 8380 "\t a\t\tcomment\n" 8381 "\t in multiple lines\n" 8382 " */", 8383 format(" /*\t \t \n" 8384 " \t \t a\t\tcomment\t \t\n" 8385 " \t \t in multiple lines\t\n" 8386 " \t */", 8387 Tab)); 8388 8389 Tab.UseTab = FormatStyle::UT_ForIndentation; 8390 verifyFormat("{\n" 8391 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8392 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8393 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8394 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8395 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8396 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8397 "};", 8398 Tab); 8399 verifyFormat("enum AA {\n" 8400 "\ta1, // Force multiple lines\n" 8401 "\ta2,\n" 8402 "\ta3\n" 8403 "};", 8404 Tab); 8405 EXPECT_EQ("if (aaaaaaaa && // q\n" 8406 " bb) // w\n" 8407 "\t;", 8408 format("if (aaaaaaaa &&// q\n" 8409 "bb)// w\n" 8410 ";", 8411 Tab)); 8412 verifyFormat("class X {\n" 8413 "\tvoid f() {\n" 8414 "\t\tsomeFunction(parameter1,\n" 8415 "\t\t parameter2);\n" 8416 "\t}\n" 8417 "};", 8418 Tab); 8419 verifyFormat("{\n" 8420 "\tQ(\n" 8421 "\t {\n" 8422 "\t\t int a;\n" 8423 "\t\t someFunction(aaaaaaaa,\n" 8424 "\t\t bbbbbbb);\n" 8425 "\t },\n" 8426 "\t p);\n" 8427 "}", 8428 Tab); 8429 EXPECT_EQ("{\n" 8430 "\t/* aaaa\n" 8431 "\t bbbb */\n" 8432 "}", 8433 format("{\n" 8434 "/* aaaa\n" 8435 " bbbb */\n" 8436 "}", 8437 Tab)); 8438 EXPECT_EQ("{\n" 8439 "\t/*\n" 8440 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8441 "\t bbbbbbbbbbbbb\n" 8442 "\t*/\n" 8443 "}", 8444 format("{\n" 8445 "/*\n" 8446 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8447 "*/\n" 8448 "}", 8449 Tab)); 8450 EXPECT_EQ("{\n" 8451 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8452 "\t// bbbbbbbbbbbbb\n" 8453 "}", 8454 format("{\n" 8455 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8456 "}", 8457 Tab)); 8458 EXPECT_EQ("{\n" 8459 "\t/*\n" 8460 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8461 "\t bbbbbbbbbbbbb\n" 8462 "\t*/\n" 8463 "}", 8464 format("{\n" 8465 "\t/*\n" 8466 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8467 "\t*/\n" 8468 "}", 8469 Tab)); 8470 EXPECT_EQ("{\n" 8471 "\t/*\n" 8472 "\n" 8473 "\t*/\n" 8474 "}", 8475 format("{\n" 8476 "\t/*\n" 8477 "\n" 8478 "\t*/\n" 8479 "}", 8480 Tab)); 8481 EXPECT_EQ("{\n" 8482 "\t/*\n" 8483 " asdf\n" 8484 "\t*/\n" 8485 "}", 8486 format("{\n" 8487 "\t/*\n" 8488 " asdf\n" 8489 "\t*/\n" 8490 "}", 8491 Tab)); 8492 8493 Tab.UseTab = FormatStyle::UT_Never; 8494 EXPECT_EQ("/*\n" 8495 " a\t\tcomment\n" 8496 " in multiple lines\n" 8497 " */", 8498 format(" /*\t \t \n" 8499 " \t \t a\t\tcomment\t \t\n" 8500 " \t \t in multiple lines\t\n" 8501 " \t */", 8502 Tab)); 8503 EXPECT_EQ("/* some\n" 8504 " comment */", 8505 format(" \t \t /* some\n" 8506 " \t \t comment */", 8507 Tab)); 8508 EXPECT_EQ("int a; /* some\n" 8509 " comment */", 8510 format(" \t \t int a; /* some\n" 8511 " \t \t comment */", 8512 Tab)); 8513 8514 EXPECT_EQ("int a; /* some\n" 8515 "comment */", 8516 format(" \t \t int\ta; /* some\n" 8517 " \t \t comment */", 8518 Tab)); 8519 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8520 " comment */", 8521 format(" \t \t f(\"\t\t\"); /* some\n" 8522 " \t \t comment */", 8523 Tab)); 8524 EXPECT_EQ("{\n" 8525 " /*\n" 8526 " * Comment\n" 8527 " */\n" 8528 " int i;\n" 8529 "}", 8530 format("{\n" 8531 "\t/*\n" 8532 "\t * Comment\n" 8533 "\t */\n" 8534 "\t int i;\n" 8535 "}")); 8536 } 8537 8538 TEST_F(FormatTest, CalculatesOriginalColumn) { 8539 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8540 "q\"; /* some\n" 8541 " comment */", 8542 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8543 "q\"; /* some\n" 8544 " comment */", 8545 getLLVMStyle())); 8546 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8547 "/* some\n" 8548 " comment */", 8549 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8550 " /* some\n" 8551 " comment */", 8552 getLLVMStyle())); 8553 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8554 "qqq\n" 8555 "/* some\n" 8556 " comment */", 8557 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8558 "qqq\n" 8559 " /* some\n" 8560 " comment */", 8561 getLLVMStyle())); 8562 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8563 "wwww; /* some\n" 8564 " comment */", 8565 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8566 "wwww; /* some\n" 8567 " comment */", 8568 getLLVMStyle())); 8569 } 8570 8571 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8572 FormatStyle NoSpace = getLLVMStyle(); 8573 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8574 8575 verifyFormat("while(true)\n" 8576 " continue;", 8577 NoSpace); 8578 verifyFormat("for(;;)\n" 8579 " continue;", 8580 NoSpace); 8581 verifyFormat("if(true)\n" 8582 " f();\n" 8583 "else if(true)\n" 8584 " f();", 8585 NoSpace); 8586 verifyFormat("do {\n" 8587 " do_something();\n" 8588 "} while(something());", 8589 NoSpace); 8590 verifyFormat("switch(x) {\n" 8591 "default:\n" 8592 " break;\n" 8593 "}", 8594 NoSpace); 8595 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8596 verifyFormat("size_t x = sizeof(x);", NoSpace); 8597 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8598 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8599 verifyFormat("alignas(128) char a[128];", NoSpace); 8600 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8601 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8602 verifyFormat("int f() throw(Deprecated);", NoSpace); 8603 verifyFormat("typedef void (*cb)(int);", NoSpace); 8604 verifyFormat("T A::operator()();", NoSpace); 8605 verifyFormat("X A::operator++(T);", NoSpace); 8606 8607 FormatStyle Space = getLLVMStyle(); 8608 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8609 8610 verifyFormat("int f ();", Space); 8611 verifyFormat("void f (int a, T b) {\n" 8612 " while (true)\n" 8613 " continue;\n" 8614 "}", 8615 Space); 8616 verifyFormat("if (true)\n" 8617 " f ();\n" 8618 "else if (true)\n" 8619 " f ();", 8620 Space); 8621 verifyFormat("do {\n" 8622 " do_something ();\n" 8623 "} while (something ());", 8624 Space); 8625 verifyFormat("switch (x) {\n" 8626 "default:\n" 8627 " break;\n" 8628 "}", 8629 Space); 8630 verifyFormat("A::A () : a (1) {}", Space); 8631 verifyFormat("void f () __attribute__ ((asdf));", Space); 8632 verifyFormat("*(&a + 1);\n" 8633 "&((&a)[1]);\n" 8634 "a[(b + c) * d];\n" 8635 "(((a + 1) * 2) + 3) * 4;", 8636 Space); 8637 verifyFormat("#define A(x) x", Space); 8638 verifyFormat("#define A (x) x", Space); 8639 verifyFormat("#if defined(x)\n" 8640 "#endif", 8641 Space); 8642 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8643 verifyFormat("size_t x = sizeof (x);", Space); 8644 verifyFormat("auto f (int x) -> decltype (x);", Space); 8645 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8646 verifyFormat("alignas (128) char a[128];", Space); 8647 verifyFormat("size_t x = alignof (MyType);", Space); 8648 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8649 verifyFormat("int f () throw (Deprecated);", Space); 8650 verifyFormat("typedef void (*cb) (int);", Space); 8651 verifyFormat("T A::operator() ();", Space); 8652 verifyFormat("X A::operator++ (T);", Space); 8653 } 8654 8655 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8656 FormatStyle Spaces = getLLVMStyle(); 8657 8658 Spaces.SpacesInParentheses = true; 8659 verifyFormat("call( x, y, z );", Spaces); 8660 verifyFormat("call();", Spaces); 8661 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8662 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8663 Spaces); 8664 verifyFormat("while ( (bool)1 )\n" 8665 " continue;", 8666 Spaces); 8667 verifyFormat("for ( ;; )\n" 8668 " continue;", 8669 Spaces); 8670 verifyFormat("if ( true )\n" 8671 " f();\n" 8672 "else if ( true )\n" 8673 " f();", 8674 Spaces); 8675 verifyFormat("do {\n" 8676 " do_something( (int)i );\n" 8677 "} while ( something() );", 8678 Spaces); 8679 verifyFormat("switch ( x ) {\n" 8680 "default:\n" 8681 " break;\n" 8682 "}", 8683 Spaces); 8684 8685 Spaces.SpacesInParentheses = false; 8686 Spaces.SpacesInCStyleCastParentheses = true; 8687 verifyFormat("Type *A = ( Type * )P;", Spaces); 8688 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8689 verifyFormat("x = ( int32 )y;", Spaces); 8690 verifyFormat("int a = ( int )(2.0f);", Spaces); 8691 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8692 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8693 verifyFormat("#define x (( int )-1)", Spaces); 8694 8695 // Run the first set of tests again with: 8696 Spaces.SpacesInParentheses = false; 8697 Spaces.SpaceInEmptyParentheses = true; 8698 Spaces.SpacesInCStyleCastParentheses = true; 8699 verifyFormat("call(x, y, z);", Spaces); 8700 verifyFormat("call( );", Spaces); 8701 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8702 verifyFormat("while (( bool )1)\n" 8703 " continue;", 8704 Spaces); 8705 verifyFormat("for (;;)\n" 8706 " continue;", 8707 Spaces); 8708 verifyFormat("if (true)\n" 8709 " f( );\n" 8710 "else if (true)\n" 8711 " f( );", 8712 Spaces); 8713 verifyFormat("do {\n" 8714 " do_something(( int )i);\n" 8715 "} while (something( ));", 8716 Spaces); 8717 verifyFormat("switch (x) {\n" 8718 "default:\n" 8719 " break;\n" 8720 "}", 8721 Spaces); 8722 8723 // Run the first set of tests again with: 8724 Spaces.SpaceAfterCStyleCast = true; 8725 verifyFormat("call(x, y, z);", Spaces); 8726 verifyFormat("call( );", Spaces); 8727 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8728 verifyFormat("while (( bool ) 1)\n" 8729 " continue;", 8730 Spaces); 8731 verifyFormat("for (;;)\n" 8732 " continue;", 8733 Spaces); 8734 verifyFormat("if (true)\n" 8735 " f( );\n" 8736 "else if (true)\n" 8737 " f( );", 8738 Spaces); 8739 verifyFormat("do {\n" 8740 " do_something(( int ) i);\n" 8741 "} while (something( ));", 8742 Spaces); 8743 verifyFormat("switch (x) {\n" 8744 "default:\n" 8745 " break;\n" 8746 "}", 8747 Spaces); 8748 8749 // Run subset of tests again with: 8750 Spaces.SpacesInCStyleCastParentheses = false; 8751 Spaces.SpaceAfterCStyleCast = true; 8752 verifyFormat("while ((bool) 1)\n" 8753 " continue;", 8754 Spaces); 8755 verifyFormat("do {\n" 8756 " do_something((int) i);\n" 8757 "} while (something( ));", 8758 Spaces); 8759 } 8760 8761 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8762 verifyFormat("int a[5];"); 8763 verifyFormat("a[3] += 42;"); 8764 8765 FormatStyle Spaces = getLLVMStyle(); 8766 Spaces.SpacesInSquareBrackets = true; 8767 // Lambdas unchanged. 8768 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8769 verifyFormat("return [i, args...] {};", Spaces); 8770 8771 // Not lambdas. 8772 verifyFormat("int a[ 5 ];", Spaces); 8773 verifyFormat("a[ 3 ] += 42;", Spaces); 8774 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8775 verifyFormat("double &operator[](int i) { return 0; }\n" 8776 "int i;", 8777 Spaces); 8778 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8779 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8780 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8781 } 8782 8783 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8784 verifyFormat("int a = 5;"); 8785 verifyFormat("a += 42;"); 8786 verifyFormat("a or_eq 8;"); 8787 8788 FormatStyle Spaces = getLLVMStyle(); 8789 Spaces.SpaceBeforeAssignmentOperators = false; 8790 verifyFormat("int a= 5;", Spaces); 8791 verifyFormat("a+= 42;", Spaces); 8792 verifyFormat("a or_eq 8;", Spaces); 8793 } 8794 8795 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8796 FormatStyle Alignment = getLLVMStyle(); 8797 Alignment.AlignConsecutiveAssignments = false; 8798 verifyFormat("int a = 5;\n" 8799 "int oneTwoThree = 123;", 8800 Alignment); 8801 verifyFormat("int a = 5;\n" 8802 "int oneTwoThree = 123;", 8803 Alignment); 8804 8805 Alignment.AlignConsecutiveAssignments = true; 8806 verifyFormat("int a = 5;\n" 8807 "int oneTwoThree = 123;", 8808 Alignment); 8809 verifyFormat("int a = method();\n" 8810 "int oneTwoThree = 133;", 8811 Alignment); 8812 verifyFormat("a &= 5;\n" 8813 "bcd *= 5;\n" 8814 "ghtyf += 5;\n" 8815 "dvfvdb -= 5;\n" 8816 "a /= 5;\n" 8817 "vdsvsv %= 5;\n" 8818 "sfdbddfbdfbb ^= 5;\n" 8819 "dvsdsv |= 5;\n" 8820 "int dsvvdvsdvvv = 123;", 8821 Alignment); 8822 verifyFormat("int i = 1, j = 10;\n" 8823 "something = 2000;", 8824 Alignment); 8825 verifyFormat("something = 2000;\n" 8826 "int i = 1, j = 10;\n", 8827 Alignment); 8828 verifyFormat("something = 2000;\n" 8829 "another = 911;\n" 8830 "int i = 1, j = 10;\n" 8831 "oneMore = 1;\n" 8832 "i = 2;", 8833 Alignment); 8834 verifyFormat("int a = 5;\n" 8835 "int one = 1;\n" 8836 "method();\n" 8837 "int oneTwoThree = 123;\n" 8838 "int oneTwo = 12;", 8839 Alignment); 8840 verifyFormat("int oneTwoThree = 123;\n" 8841 "int oneTwo = 12;\n" 8842 "method();\n", 8843 Alignment); 8844 verifyFormat("int oneTwoThree = 123; // comment\n" 8845 "int oneTwo = 12; // comment", 8846 Alignment); 8847 EXPECT_EQ("int a = 5;\n" 8848 "\n" 8849 "int oneTwoThree = 123;", 8850 format("int a = 5;\n" 8851 "\n" 8852 "int oneTwoThree= 123;", 8853 Alignment)); 8854 EXPECT_EQ("int a = 5;\n" 8855 "int one = 1;\n" 8856 "\n" 8857 "int oneTwoThree = 123;", 8858 format("int a = 5;\n" 8859 "int one = 1;\n" 8860 "\n" 8861 "int oneTwoThree = 123;", 8862 Alignment)); 8863 EXPECT_EQ("int a = 5;\n" 8864 "int one = 1;\n" 8865 "\n" 8866 "int oneTwoThree = 123;\n" 8867 "int oneTwo = 12;", 8868 format("int a = 5;\n" 8869 "int one = 1;\n" 8870 "\n" 8871 "int oneTwoThree = 123;\n" 8872 "int oneTwo = 12;", 8873 Alignment)); 8874 Alignment.AlignEscapedNewlinesLeft = true; 8875 verifyFormat("#define A \\\n" 8876 " int aaaa = 12; \\\n" 8877 " int b = 23; \\\n" 8878 " int ccc = 234; \\\n" 8879 " int dddddddddd = 2345;", 8880 Alignment); 8881 Alignment.AlignEscapedNewlinesLeft = false; 8882 verifyFormat("#define A " 8883 " \\\n" 8884 " int aaaa = 12; " 8885 " \\\n" 8886 " int b = 23; " 8887 " \\\n" 8888 " int ccc = 234; " 8889 " \\\n" 8890 " int dddddddddd = 2345;", 8891 Alignment); 8892 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8893 "k = 4, int l = 5,\n" 8894 " int m = 6) {\n" 8895 " int j = 10;\n" 8896 " otherThing = 1;\n" 8897 "}", 8898 Alignment); 8899 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8900 " int i = 1;\n" 8901 " int j = 2;\n" 8902 " int big = 10000;\n" 8903 "}", 8904 Alignment); 8905 verifyFormat("class C {\n" 8906 "public:\n" 8907 " int i = 1;\n" 8908 " virtual void f() = 0;\n" 8909 "};", 8910 Alignment); 8911 verifyFormat("int i = 1;\n" 8912 "if (SomeType t = getSomething()) {\n" 8913 "}\n" 8914 "int j = 2;\n" 8915 "int big = 10000;", 8916 Alignment); 8917 verifyFormat("int j = 7;\n" 8918 "for (int k = 0; k < N; ++k) {\n" 8919 "}\n" 8920 "int j = 2;\n" 8921 "int big = 10000;\n" 8922 "}", 8923 Alignment); 8924 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8925 verifyFormat("int i = 1;\n" 8926 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8927 " = someLooooooooooooooooongFunction();\n" 8928 "int j = 2;", 8929 Alignment); 8930 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8931 verifyFormat("int i = 1;\n" 8932 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8933 " someLooooooooooooooooongFunction();\n" 8934 "int j = 2;", 8935 Alignment); 8936 8937 verifyFormat("auto lambda = []() {\n" 8938 " auto i = 0;\n" 8939 " return 0;\n" 8940 "};\n" 8941 "int i = 0;\n" 8942 "auto v = type{\n" 8943 " i = 1, //\n" 8944 " (i = 2), //\n" 8945 " i = 3 //\n" 8946 "};", 8947 Alignment); 8948 8949 // FIXME: Should align all three assignments 8950 verifyFormat( 8951 "int i = 1;\n" 8952 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8953 " loooooooooooooooooooooongParameterB);\n" 8954 "int j = 2;", 8955 Alignment); 8956 8957 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8958 " typename B = very_long_type_name_1,\n" 8959 " typename T_2 = very_long_type_name_2>\n" 8960 "auto foo() {}\n", 8961 Alignment); 8962 verifyFormat("int a, b = 1;\n" 8963 "int c = 2;\n" 8964 "int dd = 3;\n", 8965 Alignment); 8966 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8967 "float b[1][] = {{3.f}};\n", 8968 Alignment); 8969 } 8970 8971 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8972 FormatStyle Alignment = getLLVMStyle(); 8973 Alignment.AlignConsecutiveDeclarations = false; 8974 verifyFormat("float const a = 5;\n" 8975 "int oneTwoThree = 123;", 8976 Alignment); 8977 verifyFormat("int a = 5;\n" 8978 "float const oneTwoThree = 123;", 8979 Alignment); 8980 8981 Alignment.AlignConsecutiveDeclarations = true; 8982 verifyFormat("float const a = 5;\n" 8983 "int oneTwoThree = 123;", 8984 Alignment); 8985 verifyFormat("int a = method();\n" 8986 "float const oneTwoThree = 133;", 8987 Alignment); 8988 verifyFormat("int i = 1, j = 10;\n" 8989 "something = 2000;", 8990 Alignment); 8991 verifyFormat("something = 2000;\n" 8992 "int i = 1, j = 10;\n", 8993 Alignment); 8994 verifyFormat("float something = 2000;\n" 8995 "double another = 911;\n" 8996 "int i = 1, j = 10;\n" 8997 "const int *oneMore = 1;\n" 8998 "unsigned i = 2;", 8999 Alignment); 9000 verifyFormat("float a = 5;\n" 9001 "int one = 1;\n" 9002 "method();\n" 9003 "const double oneTwoThree = 123;\n" 9004 "const unsigned int oneTwo = 12;", 9005 Alignment); 9006 verifyFormat("int oneTwoThree{0}; // comment\n" 9007 "unsigned oneTwo; // comment", 9008 Alignment); 9009 EXPECT_EQ("float const a = 5;\n" 9010 "\n" 9011 "int oneTwoThree = 123;", 9012 format("float const a = 5;\n" 9013 "\n" 9014 "int oneTwoThree= 123;", 9015 Alignment)); 9016 EXPECT_EQ("float a = 5;\n" 9017 "int one = 1;\n" 9018 "\n" 9019 "unsigned oneTwoThree = 123;", 9020 format("float a = 5;\n" 9021 "int one = 1;\n" 9022 "\n" 9023 "unsigned oneTwoThree = 123;", 9024 Alignment)); 9025 EXPECT_EQ("float a = 5;\n" 9026 "int one = 1;\n" 9027 "\n" 9028 "unsigned oneTwoThree = 123;\n" 9029 "int oneTwo = 12;", 9030 format("float a = 5;\n" 9031 "int one = 1;\n" 9032 "\n" 9033 "unsigned oneTwoThree = 123;\n" 9034 "int oneTwo = 12;", 9035 Alignment)); 9036 Alignment.AlignConsecutiveAssignments = true; 9037 verifyFormat("float something = 2000;\n" 9038 "double another = 911;\n" 9039 "int i = 1, j = 10;\n" 9040 "const int *oneMore = 1;\n" 9041 "unsigned i = 2;", 9042 Alignment); 9043 verifyFormat("int oneTwoThree = {0}; // comment\n" 9044 "unsigned oneTwo = 0; // comment", 9045 Alignment); 9046 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9047 " int const i = 1;\n" 9048 " int * j = 2;\n" 9049 " int big = 10000;\n" 9050 "\n" 9051 " unsigned oneTwoThree = 123;\n" 9052 " int oneTwo = 12;\n" 9053 " method();\n" 9054 " float k = 2;\n" 9055 " int ll = 10000;\n" 9056 "}", 9057 format("void SomeFunction(int parameter= 0) {\n" 9058 " int const i= 1;\n" 9059 " int *j=2;\n" 9060 " int big = 10000;\n" 9061 "\n" 9062 "unsigned oneTwoThree =123;\n" 9063 "int oneTwo = 12;\n" 9064 " method();\n" 9065 "float k= 2;\n" 9066 "int ll=10000;\n" 9067 "}", 9068 Alignment)); 9069 Alignment.AlignConsecutiveAssignments = false; 9070 Alignment.AlignEscapedNewlinesLeft = true; 9071 verifyFormat("#define A \\\n" 9072 " int aaaa = 12; \\\n" 9073 " float b = 23; \\\n" 9074 " const int ccc = 234; \\\n" 9075 " unsigned dddddddddd = 2345;", 9076 Alignment); 9077 Alignment.AlignEscapedNewlinesLeft = false; 9078 Alignment.ColumnLimit = 30; 9079 verifyFormat("#define A \\\n" 9080 " int aaaa = 12; \\\n" 9081 " float b = 23; \\\n" 9082 " const int ccc = 234; \\\n" 9083 " int dddddddddd = 2345;", 9084 Alignment); 9085 Alignment.ColumnLimit = 80; 9086 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9087 "k = 4, int l = 5,\n" 9088 " int m = 6) {\n" 9089 " const int j = 10;\n" 9090 " otherThing = 1;\n" 9091 "}", 9092 Alignment); 9093 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9094 " int const i = 1;\n" 9095 " int * j = 2;\n" 9096 " int big = 10000;\n" 9097 "}", 9098 Alignment); 9099 verifyFormat("class C {\n" 9100 "public:\n" 9101 " int i = 1;\n" 9102 " virtual void f() = 0;\n" 9103 "};", 9104 Alignment); 9105 verifyFormat("float i = 1;\n" 9106 "if (SomeType t = getSomething()) {\n" 9107 "}\n" 9108 "const unsigned j = 2;\n" 9109 "int big = 10000;", 9110 Alignment); 9111 verifyFormat("float j = 7;\n" 9112 "for (int k = 0; k < N; ++k) {\n" 9113 "}\n" 9114 "unsigned j = 2;\n" 9115 "int big = 10000;\n" 9116 "}", 9117 Alignment); 9118 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9119 verifyFormat("float i = 1;\n" 9120 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9121 " = someLooooooooooooooooongFunction();\n" 9122 "int j = 2;", 9123 Alignment); 9124 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9125 verifyFormat("int i = 1;\n" 9126 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9127 " someLooooooooooooooooongFunction();\n" 9128 "int j = 2;", 9129 Alignment); 9130 9131 Alignment.AlignConsecutiveAssignments = true; 9132 verifyFormat("auto lambda = []() {\n" 9133 " auto ii = 0;\n" 9134 " float j = 0;\n" 9135 " return 0;\n" 9136 "};\n" 9137 "int i = 0;\n" 9138 "float i2 = 0;\n" 9139 "auto v = type{\n" 9140 " i = 1, //\n" 9141 " (i = 2), //\n" 9142 " i = 3 //\n" 9143 "};", 9144 Alignment); 9145 Alignment.AlignConsecutiveAssignments = false; 9146 9147 // FIXME: Should align all three declarations 9148 verifyFormat( 9149 "int i = 1;\n" 9150 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9151 " loooooooooooooooooooooongParameterB);\n" 9152 "int j = 2;", 9153 Alignment); 9154 9155 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9156 // We expect declarations and assignments to align, as long as it doesn't 9157 // exceed the column limit, starting a new alignemnt sequence whenever it 9158 // happens. 9159 Alignment.AlignConsecutiveAssignments = true; 9160 Alignment.ColumnLimit = 30; 9161 verifyFormat("float ii = 1;\n" 9162 "unsigned j = 2;\n" 9163 "int someVerylongVariable = 1;\n" 9164 "AnotherLongType ll = 123456;\n" 9165 "VeryVeryLongType k = 2;\n" 9166 "int myvar = 1;", 9167 Alignment); 9168 Alignment.ColumnLimit = 80; 9169 Alignment.AlignConsecutiveAssignments = false; 9170 9171 verifyFormat( 9172 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9173 " typename LongType, typename B>\n" 9174 "auto foo() {}\n", 9175 Alignment); 9176 verifyFormat("float a, b = 1;\n" 9177 "int c = 2;\n" 9178 "int dd = 3;\n", 9179 Alignment); 9180 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9181 "float b[1][] = {{3.f}};\n", 9182 Alignment); 9183 Alignment.AlignConsecutiveAssignments = true; 9184 verifyFormat("float a, b = 1;\n" 9185 "int c = 2;\n" 9186 "int dd = 3;\n", 9187 Alignment); 9188 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9189 "float b[1][] = {{3.f}};\n", 9190 Alignment); 9191 Alignment.AlignConsecutiveAssignments = false; 9192 9193 Alignment.ColumnLimit = 30; 9194 Alignment.BinPackParameters = false; 9195 verifyFormat("void foo(float a,\n" 9196 " float b,\n" 9197 " int c,\n" 9198 " uint32_t *d) {\n" 9199 " int * e = 0;\n" 9200 " float f = 0;\n" 9201 " double g = 0;\n" 9202 "}\n" 9203 "void bar(ino_t a,\n" 9204 " int b,\n" 9205 " uint32_t *c,\n" 9206 " bool d) {}\n", 9207 Alignment); 9208 Alignment.BinPackParameters = true; 9209 Alignment.ColumnLimit = 80; 9210 } 9211 9212 TEST_F(FormatTest, LinuxBraceBreaking) { 9213 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9214 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9215 verifyFormat("namespace a\n" 9216 "{\n" 9217 "class A\n" 9218 "{\n" 9219 " void f()\n" 9220 " {\n" 9221 " if (true) {\n" 9222 " a();\n" 9223 " b();\n" 9224 " } else {\n" 9225 " a();\n" 9226 " }\n" 9227 " }\n" 9228 " void g() { return; }\n" 9229 "};\n" 9230 "struct B {\n" 9231 " int x;\n" 9232 "};\n" 9233 "}\n", 9234 LinuxBraceStyle); 9235 verifyFormat("enum X {\n" 9236 " Y = 0,\n" 9237 "}\n", 9238 LinuxBraceStyle); 9239 verifyFormat("struct S {\n" 9240 " int Type;\n" 9241 " union {\n" 9242 " int x;\n" 9243 " double y;\n" 9244 " } Value;\n" 9245 " class C\n" 9246 " {\n" 9247 " MyFavoriteType Value;\n" 9248 " } Class;\n" 9249 "}\n", 9250 LinuxBraceStyle); 9251 } 9252 9253 TEST_F(FormatTest, MozillaBraceBreaking) { 9254 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9255 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9256 verifyFormat("namespace a {\n" 9257 "class A\n" 9258 "{\n" 9259 " void f()\n" 9260 " {\n" 9261 " if (true) {\n" 9262 " a();\n" 9263 " b();\n" 9264 " }\n" 9265 " }\n" 9266 " void g() { return; }\n" 9267 "};\n" 9268 "enum E\n" 9269 "{\n" 9270 " A,\n" 9271 " // foo\n" 9272 " B,\n" 9273 " C\n" 9274 "};\n" 9275 "struct B\n" 9276 "{\n" 9277 " int x;\n" 9278 "};\n" 9279 "}\n", 9280 MozillaBraceStyle); 9281 verifyFormat("struct S\n" 9282 "{\n" 9283 " int Type;\n" 9284 " union\n" 9285 " {\n" 9286 " int x;\n" 9287 " double y;\n" 9288 " } Value;\n" 9289 " class C\n" 9290 " {\n" 9291 " MyFavoriteType Value;\n" 9292 " } Class;\n" 9293 "}\n", 9294 MozillaBraceStyle); 9295 } 9296 9297 TEST_F(FormatTest, StroustrupBraceBreaking) { 9298 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9299 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9300 verifyFormat("namespace a {\n" 9301 "class A {\n" 9302 " void f()\n" 9303 " {\n" 9304 " if (true) {\n" 9305 " a();\n" 9306 " b();\n" 9307 " }\n" 9308 " }\n" 9309 " void g() { return; }\n" 9310 "};\n" 9311 "struct B {\n" 9312 " int x;\n" 9313 "};\n" 9314 "}\n", 9315 StroustrupBraceStyle); 9316 9317 verifyFormat("void foo()\n" 9318 "{\n" 9319 " if (a) {\n" 9320 " a();\n" 9321 " }\n" 9322 " else {\n" 9323 " b();\n" 9324 " }\n" 9325 "}\n", 9326 StroustrupBraceStyle); 9327 9328 verifyFormat("#ifdef _DEBUG\n" 9329 "int foo(int i = 0)\n" 9330 "#else\n" 9331 "int foo(int i = 5)\n" 9332 "#endif\n" 9333 "{\n" 9334 " return i;\n" 9335 "}", 9336 StroustrupBraceStyle); 9337 9338 verifyFormat("void foo() {}\n" 9339 "void bar()\n" 9340 "#ifdef _DEBUG\n" 9341 "{\n" 9342 " foo();\n" 9343 "}\n" 9344 "#else\n" 9345 "{\n" 9346 "}\n" 9347 "#endif", 9348 StroustrupBraceStyle); 9349 9350 verifyFormat("void foobar() { int i = 5; }\n" 9351 "#ifdef _DEBUG\n" 9352 "void bar() {}\n" 9353 "#else\n" 9354 "void bar() { foobar(); }\n" 9355 "#endif", 9356 StroustrupBraceStyle); 9357 } 9358 9359 TEST_F(FormatTest, AllmanBraceBreaking) { 9360 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9361 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9362 verifyFormat("namespace a\n" 9363 "{\n" 9364 "class A\n" 9365 "{\n" 9366 " void f()\n" 9367 " {\n" 9368 " if (true)\n" 9369 " {\n" 9370 " a();\n" 9371 " b();\n" 9372 " }\n" 9373 " }\n" 9374 " void g() { return; }\n" 9375 "};\n" 9376 "struct B\n" 9377 "{\n" 9378 " int x;\n" 9379 "};\n" 9380 "}", 9381 AllmanBraceStyle); 9382 9383 verifyFormat("void f()\n" 9384 "{\n" 9385 " if (true)\n" 9386 " {\n" 9387 " a();\n" 9388 " }\n" 9389 " else if (false)\n" 9390 " {\n" 9391 " b();\n" 9392 " }\n" 9393 " else\n" 9394 " {\n" 9395 " c();\n" 9396 " }\n" 9397 "}\n", 9398 AllmanBraceStyle); 9399 9400 verifyFormat("void f()\n" 9401 "{\n" 9402 " for (int i = 0; i < 10; ++i)\n" 9403 " {\n" 9404 " a();\n" 9405 " }\n" 9406 " while (false)\n" 9407 " {\n" 9408 " b();\n" 9409 " }\n" 9410 " do\n" 9411 " {\n" 9412 " c();\n" 9413 " } while (false)\n" 9414 "}\n", 9415 AllmanBraceStyle); 9416 9417 verifyFormat("void f(int a)\n" 9418 "{\n" 9419 " switch (a)\n" 9420 " {\n" 9421 " case 0:\n" 9422 " break;\n" 9423 " case 1:\n" 9424 " {\n" 9425 " break;\n" 9426 " }\n" 9427 " case 2:\n" 9428 " {\n" 9429 " }\n" 9430 " break;\n" 9431 " default:\n" 9432 " break;\n" 9433 " }\n" 9434 "}\n", 9435 AllmanBraceStyle); 9436 9437 verifyFormat("enum X\n" 9438 "{\n" 9439 " Y = 0,\n" 9440 "}\n", 9441 AllmanBraceStyle); 9442 verifyFormat("enum X\n" 9443 "{\n" 9444 " Y = 0\n" 9445 "}\n", 9446 AllmanBraceStyle); 9447 9448 verifyFormat("@interface BSApplicationController ()\n" 9449 "{\n" 9450 "@private\n" 9451 " id _extraIvar;\n" 9452 "}\n" 9453 "@end\n", 9454 AllmanBraceStyle); 9455 9456 verifyFormat("#ifdef _DEBUG\n" 9457 "int foo(int i = 0)\n" 9458 "#else\n" 9459 "int foo(int i = 5)\n" 9460 "#endif\n" 9461 "{\n" 9462 " return i;\n" 9463 "}", 9464 AllmanBraceStyle); 9465 9466 verifyFormat("void foo() {}\n" 9467 "void bar()\n" 9468 "#ifdef _DEBUG\n" 9469 "{\n" 9470 " foo();\n" 9471 "}\n" 9472 "#else\n" 9473 "{\n" 9474 "}\n" 9475 "#endif", 9476 AllmanBraceStyle); 9477 9478 verifyFormat("void foobar() { int i = 5; }\n" 9479 "#ifdef _DEBUG\n" 9480 "void bar() {}\n" 9481 "#else\n" 9482 "void bar() { foobar(); }\n" 9483 "#endif", 9484 AllmanBraceStyle); 9485 9486 // This shouldn't affect ObjC blocks.. 9487 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9488 " // ...\n" 9489 " int i;\n" 9490 "}];", 9491 AllmanBraceStyle); 9492 verifyFormat("void (^block)(void) = ^{\n" 9493 " // ...\n" 9494 " int i;\n" 9495 "};", 9496 AllmanBraceStyle); 9497 // .. or dict literals. 9498 verifyFormat("void f()\n" 9499 "{\n" 9500 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9501 "}", 9502 AllmanBraceStyle); 9503 verifyFormat("int f()\n" 9504 "{ // comment\n" 9505 " return 42;\n" 9506 "}", 9507 AllmanBraceStyle); 9508 9509 AllmanBraceStyle.ColumnLimit = 19; 9510 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9511 AllmanBraceStyle.ColumnLimit = 18; 9512 verifyFormat("void f()\n" 9513 "{\n" 9514 " int i;\n" 9515 "}", 9516 AllmanBraceStyle); 9517 AllmanBraceStyle.ColumnLimit = 80; 9518 9519 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9520 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9521 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9522 verifyFormat("void f(bool b)\n" 9523 "{\n" 9524 " if (b)\n" 9525 " {\n" 9526 " return;\n" 9527 " }\n" 9528 "}\n", 9529 BreakBeforeBraceShortIfs); 9530 verifyFormat("void f(bool b)\n" 9531 "{\n" 9532 " if (b) return;\n" 9533 "}\n", 9534 BreakBeforeBraceShortIfs); 9535 verifyFormat("void f(bool b)\n" 9536 "{\n" 9537 " while (b)\n" 9538 " {\n" 9539 " return;\n" 9540 " }\n" 9541 "}\n", 9542 BreakBeforeBraceShortIfs); 9543 } 9544 9545 TEST_F(FormatTest, GNUBraceBreaking) { 9546 FormatStyle GNUBraceStyle = getLLVMStyle(); 9547 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9548 verifyFormat("namespace a\n" 9549 "{\n" 9550 "class A\n" 9551 "{\n" 9552 " void f()\n" 9553 " {\n" 9554 " int a;\n" 9555 " {\n" 9556 " int b;\n" 9557 " }\n" 9558 " if (true)\n" 9559 " {\n" 9560 " a();\n" 9561 " b();\n" 9562 " }\n" 9563 " }\n" 9564 " void g() { return; }\n" 9565 "}\n" 9566 "}", 9567 GNUBraceStyle); 9568 9569 verifyFormat("void f()\n" 9570 "{\n" 9571 " if (true)\n" 9572 " {\n" 9573 " a();\n" 9574 " }\n" 9575 " else if (false)\n" 9576 " {\n" 9577 " b();\n" 9578 " }\n" 9579 " else\n" 9580 " {\n" 9581 " c();\n" 9582 " }\n" 9583 "}\n", 9584 GNUBraceStyle); 9585 9586 verifyFormat("void f()\n" 9587 "{\n" 9588 " for (int i = 0; i < 10; ++i)\n" 9589 " {\n" 9590 " a();\n" 9591 " }\n" 9592 " while (false)\n" 9593 " {\n" 9594 " b();\n" 9595 " }\n" 9596 " do\n" 9597 " {\n" 9598 " c();\n" 9599 " }\n" 9600 " while (false);\n" 9601 "}\n", 9602 GNUBraceStyle); 9603 9604 verifyFormat("void f(int a)\n" 9605 "{\n" 9606 " switch (a)\n" 9607 " {\n" 9608 " case 0:\n" 9609 " break;\n" 9610 " case 1:\n" 9611 " {\n" 9612 " break;\n" 9613 " }\n" 9614 " case 2:\n" 9615 " {\n" 9616 " }\n" 9617 " break;\n" 9618 " default:\n" 9619 " break;\n" 9620 " }\n" 9621 "}\n", 9622 GNUBraceStyle); 9623 9624 verifyFormat("enum X\n" 9625 "{\n" 9626 " Y = 0,\n" 9627 "}\n", 9628 GNUBraceStyle); 9629 9630 verifyFormat("@interface BSApplicationController ()\n" 9631 "{\n" 9632 "@private\n" 9633 " id _extraIvar;\n" 9634 "}\n" 9635 "@end\n", 9636 GNUBraceStyle); 9637 9638 verifyFormat("#ifdef _DEBUG\n" 9639 "int foo(int i = 0)\n" 9640 "#else\n" 9641 "int foo(int i = 5)\n" 9642 "#endif\n" 9643 "{\n" 9644 " return i;\n" 9645 "}", 9646 GNUBraceStyle); 9647 9648 verifyFormat("void foo() {}\n" 9649 "void bar()\n" 9650 "#ifdef _DEBUG\n" 9651 "{\n" 9652 " foo();\n" 9653 "}\n" 9654 "#else\n" 9655 "{\n" 9656 "}\n" 9657 "#endif", 9658 GNUBraceStyle); 9659 9660 verifyFormat("void foobar() { int i = 5; }\n" 9661 "#ifdef _DEBUG\n" 9662 "void bar() {}\n" 9663 "#else\n" 9664 "void bar() { foobar(); }\n" 9665 "#endif", 9666 GNUBraceStyle); 9667 } 9668 9669 TEST_F(FormatTest, WebKitBraceBreaking) { 9670 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9671 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9672 verifyFormat("namespace a {\n" 9673 "class A {\n" 9674 " void f()\n" 9675 " {\n" 9676 " if (true) {\n" 9677 " a();\n" 9678 " b();\n" 9679 " }\n" 9680 " }\n" 9681 " void g() { return; }\n" 9682 "};\n" 9683 "enum E {\n" 9684 " A,\n" 9685 " // foo\n" 9686 " B,\n" 9687 " C\n" 9688 "};\n" 9689 "struct B {\n" 9690 " int x;\n" 9691 "};\n" 9692 "}\n", 9693 WebKitBraceStyle); 9694 verifyFormat("struct S {\n" 9695 " int Type;\n" 9696 " union {\n" 9697 " int x;\n" 9698 " double y;\n" 9699 " } Value;\n" 9700 " class C {\n" 9701 " MyFavoriteType Value;\n" 9702 " } Class;\n" 9703 "};\n", 9704 WebKitBraceStyle); 9705 } 9706 9707 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9708 verifyFormat("void f() {\n" 9709 " try {\n" 9710 " } catch (const Exception &e) {\n" 9711 " }\n" 9712 "}\n", 9713 getLLVMStyle()); 9714 } 9715 9716 TEST_F(FormatTest, UnderstandsPragmas) { 9717 verifyFormat("#pragma omp reduction(| : var)"); 9718 verifyFormat("#pragma omp reduction(+ : var)"); 9719 9720 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9721 "(including parentheses).", 9722 format("#pragma mark Any non-hyphenated or hyphenated string " 9723 "(including parentheses).")); 9724 } 9725 9726 TEST_F(FormatTest, UnderstandPragmaOption) { 9727 verifyFormat("#pragma option -C -A"); 9728 9729 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9730 } 9731 9732 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9733 for (size_t i = 1; i < Styles.size(); ++i) \ 9734 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9735 << " differs from Style #0" 9736 9737 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9738 SmallVector<FormatStyle, 3> Styles; 9739 Styles.resize(3); 9740 9741 Styles[0] = getLLVMStyle(); 9742 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9743 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9744 EXPECT_ALL_STYLES_EQUAL(Styles); 9745 9746 Styles[0] = getGoogleStyle(); 9747 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9748 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9749 EXPECT_ALL_STYLES_EQUAL(Styles); 9750 9751 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9752 EXPECT_TRUE( 9753 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9754 EXPECT_TRUE( 9755 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9756 EXPECT_ALL_STYLES_EQUAL(Styles); 9757 9758 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9759 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9760 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9761 EXPECT_ALL_STYLES_EQUAL(Styles); 9762 9763 Styles[0] = getMozillaStyle(); 9764 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9765 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9766 EXPECT_ALL_STYLES_EQUAL(Styles); 9767 9768 Styles[0] = getWebKitStyle(); 9769 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9770 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9771 EXPECT_ALL_STYLES_EQUAL(Styles); 9772 9773 Styles[0] = getGNUStyle(); 9774 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9775 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9776 EXPECT_ALL_STYLES_EQUAL(Styles); 9777 9778 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9779 } 9780 9781 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9782 SmallVector<FormatStyle, 8> Styles; 9783 Styles.resize(2); 9784 9785 Styles[0] = getGoogleStyle(); 9786 Styles[1] = getLLVMStyle(); 9787 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9788 EXPECT_ALL_STYLES_EQUAL(Styles); 9789 9790 Styles.resize(5); 9791 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9792 Styles[1] = getLLVMStyle(); 9793 Styles[1].Language = FormatStyle::LK_JavaScript; 9794 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9795 9796 Styles[2] = getLLVMStyle(); 9797 Styles[2].Language = FormatStyle::LK_JavaScript; 9798 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9799 "BasedOnStyle: Google", 9800 &Styles[2]) 9801 .value()); 9802 9803 Styles[3] = getLLVMStyle(); 9804 Styles[3].Language = FormatStyle::LK_JavaScript; 9805 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9806 "Language: JavaScript", 9807 &Styles[3]) 9808 .value()); 9809 9810 Styles[4] = getLLVMStyle(); 9811 Styles[4].Language = FormatStyle::LK_JavaScript; 9812 EXPECT_EQ(0, parseConfiguration("---\n" 9813 "BasedOnStyle: LLVM\n" 9814 "IndentWidth: 123\n" 9815 "---\n" 9816 "BasedOnStyle: Google\n" 9817 "Language: JavaScript", 9818 &Styles[4]) 9819 .value()); 9820 EXPECT_ALL_STYLES_EQUAL(Styles); 9821 } 9822 9823 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9824 Style.FIELD = false; \ 9825 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9826 EXPECT_TRUE(Style.FIELD); \ 9827 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9828 EXPECT_FALSE(Style.FIELD); 9829 9830 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9831 9832 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9833 Style.STRUCT.FIELD = false; \ 9834 EXPECT_EQ(0, \ 9835 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9836 .value()); \ 9837 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9838 EXPECT_EQ(0, \ 9839 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9840 .value()); \ 9841 EXPECT_FALSE(Style.STRUCT.FIELD); 9842 9843 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9844 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9845 9846 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9847 EXPECT_NE(VALUE, Style.FIELD); \ 9848 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9849 EXPECT_EQ(VALUE, Style.FIELD) 9850 9851 TEST_F(FormatTest, ParsesConfigurationBools) { 9852 FormatStyle Style = {}; 9853 Style.Language = FormatStyle::LK_Cpp; 9854 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9855 CHECK_PARSE_BOOL(AlignOperands); 9856 CHECK_PARSE_BOOL(AlignTrailingComments); 9857 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9858 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9859 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9860 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9861 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9862 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9863 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9864 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9865 CHECK_PARSE_BOOL(BinPackArguments); 9866 CHECK_PARSE_BOOL(BinPackParameters); 9867 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9868 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9869 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9870 CHECK_PARSE_BOOL(BreakStringLiterals); 9871 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9872 CHECK_PARSE_BOOL(DerivePointerAlignment); 9873 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9874 CHECK_PARSE_BOOL(DisableFormat); 9875 CHECK_PARSE_BOOL(IndentCaseLabels); 9876 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9877 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9878 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9879 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9880 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9881 CHECK_PARSE_BOOL(ReflowComments); 9882 CHECK_PARSE_BOOL(SortIncludes); 9883 CHECK_PARSE_BOOL(SpacesInParentheses); 9884 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9885 CHECK_PARSE_BOOL(SpacesInAngles); 9886 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9887 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9888 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9889 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9890 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9891 9892 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9893 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9894 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9895 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9896 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9897 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9898 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9899 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9900 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9901 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9902 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9903 } 9904 9905 #undef CHECK_PARSE_BOOL 9906 9907 TEST_F(FormatTest, ParsesConfiguration) { 9908 FormatStyle Style = {}; 9909 Style.Language = FormatStyle::LK_Cpp; 9910 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9911 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9912 ConstructorInitializerIndentWidth, 1234u); 9913 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9914 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9915 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9916 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9917 PenaltyBreakBeforeFirstCallParameter, 1234u); 9918 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9919 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9920 PenaltyReturnTypeOnItsOwnLine, 1234u); 9921 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9922 SpacesBeforeTrailingComments, 1234u); 9923 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9924 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9925 9926 Style.PointerAlignment = FormatStyle::PAS_Middle; 9927 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9928 FormatStyle::PAS_Left); 9929 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9930 FormatStyle::PAS_Right); 9931 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9932 FormatStyle::PAS_Middle); 9933 // For backward compatibility: 9934 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9935 FormatStyle::PAS_Left); 9936 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9937 FormatStyle::PAS_Right); 9938 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9939 FormatStyle::PAS_Middle); 9940 9941 Style.Standard = FormatStyle::LS_Auto; 9942 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9943 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9944 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9945 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9946 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9947 9948 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9949 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9950 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9951 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9952 FormatStyle::BOS_None); 9953 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9954 FormatStyle::BOS_All); 9955 // For backward compatibility: 9956 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9957 FormatStyle::BOS_None); 9958 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9959 FormatStyle::BOS_All); 9960 9961 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9962 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9963 FormatStyle::BAS_Align); 9964 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9965 FormatStyle::BAS_DontAlign); 9966 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9967 FormatStyle::BAS_AlwaysBreak); 9968 // For backward compatibility: 9969 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9970 FormatStyle::BAS_DontAlign); 9971 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9972 FormatStyle::BAS_Align); 9973 9974 Style.UseTab = FormatStyle::UT_ForIndentation; 9975 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9976 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9977 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9978 // For backward compatibility: 9979 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9980 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9981 9982 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9983 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9984 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9985 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9986 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9987 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9988 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9989 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9990 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9991 // For backward compatibility: 9992 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9993 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9994 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9995 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9996 9997 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9998 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9999 FormatStyle::SBPO_Never); 10000 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10001 FormatStyle::SBPO_Always); 10002 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10003 FormatStyle::SBPO_ControlStatements); 10004 // For backward compatibility: 10005 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10006 FormatStyle::SBPO_Never); 10007 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10008 FormatStyle::SBPO_ControlStatements); 10009 10010 Style.ColumnLimit = 123; 10011 FormatStyle BaseStyle = getLLVMStyle(); 10012 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10013 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10014 10015 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10016 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10017 FormatStyle::BS_Attach); 10018 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10019 FormatStyle::BS_Linux); 10020 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10021 FormatStyle::BS_Mozilla); 10022 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10023 FormatStyle::BS_Stroustrup); 10024 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10025 FormatStyle::BS_Allman); 10026 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10027 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10028 FormatStyle::BS_WebKit); 10029 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10030 FormatStyle::BS_Custom); 10031 10032 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10033 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10034 FormatStyle::RTBS_None); 10035 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10036 FormatStyle::RTBS_All); 10037 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10038 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10039 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10040 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10041 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10042 AlwaysBreakAfterReturnType, 10043 FormatStyle::RTBS_TopLevelDefinitions); 10044 10045 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10046 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10047 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10048 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10049 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10050 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10051 AlwaysBreakAfterDefinitionReturnType, 10052 FormatStyle::DRTBS_TopLevel); 10053 10054 Style.NamespaceIndentation = FormatStyle::NI_All; 10055 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10056 FormatStyle::NI_None); 10057 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10058 FormatStyle::NI_Inner); 10059 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10060 FormatStyle::NI_All); 10061 10062 // FIXME: This is required because parsing a configuration simply overwrites 10063 // the first N elements of the list instead of resetting it. 10064 Style.ForEachMacros.clear(); 10065 std::vector<std::string> BoostForeach; 10066 BoostForeach.push_back("BOOST_FOREACH"); 10067 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10068 std::vector<std::string> BoostAndQForeach; 10069 BoostAndQForeach.push_back("BOOST_FOREACH"); 10070 BoostAndQForeach.push_back("Q_FOREACH"); 10071 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10072 BoostAndQForeach); 10073 10074 Style.IncludeCategories.clear(); 10075 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10076 {".*", 1}}; 10077 CHECK_PARSE("IncludeCategories:\n" 10078 " - Regex: abc/.*\n" 10079 " Priority: 2\n" 10080 " - Regex: .*\n" 10081 " Priority: 1", 10082 IncludeCategories, ExpectedCategories); 10083 } 10084 10085 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10086 FormatStyle Style = {}; 10087 Style.Language = FormatStyle::LK_Cpp; 10088 CHECK_PARSE("Language: Cpp\n" 10089 "IndentWidth: 12", 10090 IndentWidth, 12u); 10091 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10092 "IndentWidth: 34", 10093 &Style), 10094 ParseError::Unsuitable); 10095 EXPECT_EQ(12u, Style.IndentWidth); 10096 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10097 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10098 10099 Style.Language = FormatStyle::LK_JavaScript; 10100 CHECK_PARSE("Language: JavaScript\n" 10101 "IndentWidth: 12", 10102 IndentWidth, 12u); 10103 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10104 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10105 "IndentWidth: 34", 10106 &Style), 10107 ParseError::Unsuitable); 10108 EXPECT_EQ(23u, Style.IndentWidth); 10109 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10110 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10111 10112 CHECK_PARSE("BasedOnStyle: LLVM\n" 10113 "IndentWidth: 67", 10114 IndentWidth, 67u); 10115 10116 CHECK_PARSE("---\n" 10117 "Language: JavaScript\n" 10118 "IndentWidth: 12\n" 10119 "---\n" 10120 "Language: Cpp\n" 10121 "IndentWidth: 34\n" 10122 "...\n", 10123 IndentWidth, 12u); 10124 10125 Style.Language = FormatStyle::LK_Cpp; 10126 CHECK_PARSE("---\n" 10127 "Language: JavaScript\n" 10128 "IndentWidth: 12\n" 10129 "---\n" 10130 "Language: Cpp\n" 10131 "IndentWidth: 34\n" 10132 "...\n", 10133 IndentWidth, 34u); 10134 CHECK_PARSE("---\n" 10135 "IndentWidth: 78\n" 10136 "---\n" 10137 "Language: JavaScript\n" 10138 "IndentWidth: 56\n" 10139 "...\n", 10140 IndentWidth, 78u); 10141 10142 Style.ColumnLimit = 123; 10143 Style.IndentWidth = 234; 10144 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10145 Style.TabWidth = 345; 10146 EXPECT_FALSE(parseConfiguration("---\n" 10147 "IndentWidth: 456\n" 10148 "BreakBeforeBraces: Allman\n" 10149 "---\n" 10150 "Language: JavaScript\n" 10151 "IndentWidth: 111\n" 10152 "TabWidth: 111\n" 10153 "---\n" 10154 "Language: Cpp\n" 10155 "BreakBeforeBraces: Stroustrup\n" 10156 "TabWidth: 789\n" 10157 "...\n", 10158 &Style)); 10159 EXPECT_EQ(123u, Style.ColumnLimit); 10160 EXPECT_EQ(456u, Style.IndentWidth); 10161 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10162 EXPECT_EQ(789u, Style.TabWidth); 10163 10164 EXPECT_EQ(parseConfiguration("---\n" 10165 "Language: JavaScript\n" 10166 "IndentWidth: 56\n" 10167 "---\n" 10168 "IndentWidth: 78\n" 10169 "...\n", 10170 &Style), 10171 ParseError::Error); 10172 EXPECT_EQ(parseConfiguration("---\n" 10173 "Language: JavaScript\n" 10174 "IndentWidth: 56\n" 10175 "---\n" 10176 "Language: JavaScript\n" 10177 "IndentWidth: 78\n" 10178 "...\n", 10179 &Style), 10180 ParseError::Error); 10181 10182 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10183 } 10184 10185 #undef CHECK_PARSE 10186 10187 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10188 FormatStyle Style = {}; 10189 Style.Language = FormatStyle::LK_JavaScript; 10190 Style.BreakBeforeTernaryOperators = true; 10191 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10192 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10193 10194 Style.BreakBeforeTernaryOperators = true; 10195 EXPECT_EQ(0, parseConfiguration("---\n" 10196 "BasedOnStyle: Google\n" 10197 "---\n" 10198 "Language: JavaScript\n" 10199 "IndentWidth: 76\n" 10200 "...\n", 10201 &Style) 10202 .value()); 10203 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10204 EXPECT_EQ(76u, Style.IndentWidth); 10205 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10206 } 10207 10208 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10209 FormatStyle Style = getLLVMStyle(); 10210 std::string YAML = configurationAsText(Style); 10211 FormatStyle ParsedStyle = {}; 10212 ParsedStyle.Language = FormatStyle::LK_Cpp; 10213 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10214 EXPECT_EQ(Style, ParsedStyle); 10215 } 10216 10217 TEST_F(FormatTest, WorksFor8bitEncodings) { 10218 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10219 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10220 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10221 "\"\xef\xee\xf0\xf3...\"", 10222 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10223 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10224 "\xef\xee\xf0\xf3...\"", 10225 getLLVMStyleWithColumns(12))); 10226 } 10227 10228 TEST_F(FormatTest, HandlesUTF8BOM) { 10229 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10230 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10231 format("\xef\xbb\xbf#include <iostream>")); 10232 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10233 format("\xef\xbb\xbf\n#include <iostream>")); 10234 } 10235 10236 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10237 #if !defined(_MSC_VER) 10238 10239 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10240 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10241 getLLVMStyleWithColumns(35)); 10242 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10243 getLLVMStyleWithColumns(31)); 10244 verifyFormat("// Однажды в студёную зимнюю пору...", 10245 getLLVMStyleWithColumns(36)); 10246 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10247 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10248 getLLVMStyleWithColumns(39)); 10249 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10250 getLLVMStyleWithColumns(35)); 10251 } 10252 10253 TEST_F(FormatTest, SplitsUTF8Strings) { 10254 // Non-printable characters' width is currently considered to be the length in 10255 // bytes in UTF8. The characters can be displayed in very different manner 10256 // (zero-width, single width with a substitution glyph, expanded to their code 10257 // (e.g. "<8d>"), so there's no single correct way to handle them. 10258 EXPECT_EQ("\"aaaaÄ\"\n" 10259 "\"\xc2\x8d\";", 10260 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10261 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10262 "\"\xc2\x8d\";", 10263 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10264 EXPECT_EQ("\"Однажды, в \"\n" 10265 "\"студёную \"\n" 10266 "\"зимнюю \"\n" 10267 "\"пору,\"", 10268 format("\"Однажды, в студёную зимнюю пору,\"", 10269 getLLVMStyleWithColumns(13))); 10270 EXPECT_EQ( 10271 "\"一 二 三 \"\n" 10272 "\"四 五六 \"\n" 10273 "\"七 八 九 \"\n" 10274 "\"十\"", 10275 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10276 EXPECT_EQ("\"一\t二 \"\n" 10277 "\"\t三 \"\n" 10278 "\"四 五\t六 \"\n" 10279 "\"\t七 \"\n" 10280 "\"八九十\tqq\"", 10281 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10282 getLLVMStyleWithColumns(11))); 10283 10284 // UTF8 character in an escape sequence. 10285 EXPECT_EQ("\"aaaaaa\"\n" 10286 "\"\\\xC2\x8D\"", 10287 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10288 } 10289 10290 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10291 EXPECT_EQ("const char *sssss =\n" 10292 " \"一二三四五六七八\\\n" 10293 " 九 十\";", 10294 format("const char *sssss = \"一二三四五六七八\\\n" 10295 " 九 十\";", 10296 getLLVMStyleWithColumns(30))); 10297 } 10298 10299 TEST_F(FormatTest, SplitsUTF8LineComments) { 10300 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10301 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10302 EXPECT_EQ("// Я из лесу\n" 10303 "// вышел; был\n" 10304 "// сильный\n" 10305 "// мороз.", 10306 format("// Я из лесу вышел; был сильный мороз.", 10307 getLLVMStyleWithColumns(13))); 10308 EXPECT_EQ("// 一二三\n" 10309 "// 四五六七\n" 10310 "// 八 九\n" 10311 "// 十", 10312 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10313 } 10314 10315 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10316 EXPECT_EQ("/* Гляжу,\n" 10317 " * поднимается\n" 10318 " * медленно в\n" 10319 " * гору\n" 10320 " * Лошадка,\n" 10321 " * везущая\n" 10322 " * хворосту\n" 10323 " * воз. */", 10324 format("/* Гляжу, поднимается медленно в гору\n" 10325 " * Лошадка, везущая хворосту воз. */", 10326 getLLVMStyleWithColumns(13))); 10327 EXPECT_EQ( 10328 "/* 一二三\n" 10329 " * 四五六七\n" 10330 " * 八 九\n" 10331 " * 十 */", 10332 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10333 EXPECT_EQ("/* \n" 10334 " * \n" 10335 " * - */", 10336 format("/* - */", getLLVMStyleWithColumns(12))); 10337 } 10338 10339 #endif // _MSC_VER 10340 10341 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10342 FormatStyle Style = getLLVMStyle(); 10343 10344 Style.ConstructorInitializerIndentWidth = 4; 10345 verifyFormat( 10346 "SomeClass::Constructor()\n" 10347 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10348 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10349 Style); 10350 10351 Style.ConstructorInitializerIndentWidth = 2; 10352 verifyFormat( 10353 "SomeClass::Constructor()\n" 10354 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10355 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10356 Style); 10357 10358 Style.ConstructorInitializerIndentWidth = 0; 10359 verifyFormat( 10360 "SomeClass::Constructor()\n" 10361 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10362 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10363 Style); 10364 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10365 verifyFormat( 10366 "SomeLongTemplateVariableName<\n" 10367 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10368 Style); 10369 verifyFormat( 10370 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10372 Style); 10373 } 10374 10375 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10376 FormatStyle Style = getLLVMStyle(); 10377 Style.BreakConstructorInitializersBeforeComma = true; 10378 Style.ConstructorInitializerIndentWidth = 4; 10379 verifyFormat("SomeClass::Constructor()\n" 10380 " : a(a)\n" 10381 " , b(b)\n" 10382 " , c(c) {}", 10383 Style); 10384 verifyFormat("SomeClass::Constructor()\n" 10385 " : a(a) {}", 10386 Style); 10387 10388 Style.ColumnLimit = 0; 10389 verifyFormat("SomeClass::Constructor()\n" 10390 " : a(a) {}", 10391 Style); 10392 verifyFormat("SomeClass::Constructor() noexcept\n" 10393 " : a(a) {}", 10394 Style); 10395 verifyFormat("SomeClass::Constructor()\n" 10396 " : a(a)\n" 10397 " , b(b)\n" 10398 " , c(c) {}", 10399 Style); 10400 verifyFormat("SomeClass::Constructor()\n" 10401 " : a(a) {\n" 10402 " foo();\n" 10403 " bar();\n" 10404 "}", 10405 Style); 10406 10407 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10408 verifyFormat("SomeClass::Constructor()\n" 10409 " : a(a)\n" 10410 " , b(b)\n" 10411 " , c(c) {\n}", 10412 Style); 10413 verifyFormat("SomeClass::Constructor()\n" 10414 " : a(a) {\n}", 10415 Style); 10416 10417 Style.ColumnLimit = 80; 10418 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10419 Style.ConstructorInitializerIndentWidth = 2; 10420 verifyFormat("SomeClass::Constructor()\n" 10421 " : a(a)\n" 10422 " , b(b)\n" 10423 " , c(c) {}", 10424 Style); 10425 10426 Style.ConstructorInitializerIndentWidth = 0; 10427 verifyFormat("SomeClass::Constructor()\n" 10428 ": a(a)\n" 10429 ", b(b)\n" 10430 ", c(c) {}", 10431 Style); 10432 10433 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10434 Style.ConstructorInitializerIndentWidth = 4; 10435 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10436 verifyFormat( 10437 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10438 Style); 10439 verifyFormat( 10440 "SomeClass::Constructor()\n" 10441 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10442 Style); 10443 Style.ConstructorInitializerIndentWidth = 4; 10444 Style.ColumnLimit = 60; 10445 verifyFormat("SomeClass::Constructor()\n" 10446 " : aaaaaaaa(aaaaaaaa)\n" 10447 " , aaaaaaaa(aaaaaaaa)\n" 10448 " , aaaaaaaa(aaaaaaaa) {}", 10449 Style); 10450 } 10451 10452 TEST_F(FormatTest, Destructors) { 10453 verifyFormat("void F(int &i) { i.~int(); }"); 10454 verifyFormat("void F(int &i) { i->~int(); }"); 10455 } 10456 10457 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10458 FormatStyle Style = getWebKitStyle(); 10459 10460 // Don't indent in outer namespaces. 10461 verifyFormat("namespace outer {\n" 10462 "int i;\n" 10463 "namespace inner {\n" 10464 " int i;\n" 10465 "} // namespace inner\n" 10466 "} // namespace outer\n" 10467 "namespace other_outer {\n" 10468 "int i;\n" 10469 "}", 10470 Style); 10471 10472 // Don't indent case labels. 10473 verifyFormat("switch (variable) {\n" 10474 "case 1:\n" 10475 "case 2:\n" 10476 " doSomething();\n" 10477 " break;\n" 10478 "default:\n" 10479 " ++variable;\n" 10480 "}", 10481 Style); 10482 10483 // Wrap before binary operators. 10484 EXPECT_EQ("void f()\n" 10485 "{\n" 10486 " if (aaaaaaaaaaaaaaaa\n" 10487 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10488 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10489 " return;\n" 10490 "}", 10491 format("void f() {\n" 10492 "if (aaaaaaaaaaaaaaaa\n" 10493 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10494 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10495 "return;\n" 10496 "}", 10497 Style)); 10498 10499 // Allow functions on a single line. 10500 verifyFormat("void f() { return; }", Style); 10501 10502 // Constructor initializers are formatted one per line with the "," on the 10503 // new line. 10504 verifyFormat("Constructor()\n" 10505 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10506 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10507 " aaaaaaaaaaaaaa)\n" 10508 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10509 "{\n" 10510 "}", 10511 Style); 10512 verifyFormat("SomeClass::Constructor()\n" 10513 " : a(a)\n" 10514 "{\n" 10515 "}", 10516 Style); 10517 EXPECT_EQ("SomeClass::Constructor()\n" 10518 " : a(a)\n" 10519 "{\n" 10520 "}", 10521 format("SomeClass::Constructor():a(a){}", Style)); 10522 verifyFormat("SomeClass::Constructor()\n" 10523 " : a(a)\n" 10524 " , b(b)\n" 10525 " , c(c)\n" 10526 "{\n" 10527 "}", 10528 Style); 10529 verifyFormat("SomeClass::Constructor()\n" 10530 " : a(a)\n" 10531 "{\n" 10532 " foo();\n" 10533 " bar();\n" 10534 "}", 10535 Style); 10536 10537 // Access specifiers should be aligned left. 10538 verifyFormat("class C {\n" 10539 "public:\n" 10540 " int i;\n" 10541 "};", 10542 Style); 10543 10544 // Do not align comments. 10545 verifyFormat("int a; // Do not\n" 10546 "double b; // align comments.", 10547 Style); 10548 10549 // Do not align operands. 10550 EXPECT_EQ("ASSERT(aaaa\n" 10551 " || bbbb);", 10552 format("ASSERT ( aaaa\n||bbbb);", Style)); 10553 10554 // Accept input's line breaks. 10555 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10556 " || bbbbbbbbbbbbbbb) {\n" 10557 " i++;\n" 10558 "}", 10559 format("if (aaaaaaaaaaaaaaa\n" 10560 "|| bbbbbbbbbbbbbbb) { i++; }", 10561 Style)); 10562 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10563 " i++;\n" 10564 "}", 10565 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10566 10567 // Don't automatically break all macro definitions (llvm.org/PR17842). 10568 verifyFormat("#define aNumber 10", Style); 10569 // However, generally keep the line breaks that the user authored. 10570 EXPECT_EQ("#define aNumber \\\n" 10571 " 10", 10572 format("#define aNumber \\\n" 10573 " 10", 10574 Style)); 10575 10576 // Keep empty and one-element array literals on a single line. 10577 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10578 " copyItems:YES];", 10579 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10580 "copyItems:YES];", 10581 Style)); 10582 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10583 " copyItems:YES];", 10584 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10585 " copyItems:YES];", 10586 Style)); 10587 // FIXME: This does not seem right, there should be more indentation before 10588 // the array literal's entries. Nested blocks have the same problem. 10589 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10590 " @\"a\",\n" 10591 " @\"a\"\n" 10592 "]\n" 10593 " copyItems:YES];", 10594 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10595 " @\"a\",\n" 10596 " @\"a\"\n" 10597 " ]\n" 10598 " copyItems:YES];", 10599 Style)); 10600 EXPECT_EQ( 10601 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10602 " copyItems:YES];", 10603 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10604 " copyItems:YES];", 10605 Style)); 10606 10607 verifyFormat("[self.a b:c c:d];", Style); 10608 EXPECT_EQ("[self.a b:c\n" 10609 " c:d];", 10610 format("[self.a b:c\n" 10611 "c:d];", 10612 Style)); 10613 } 10614 10615 TEST_F(FormatTest, FormatsLambdas) { 10616 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10617 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10618 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10619 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10620 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10621 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10622 verifyFormat("void f() {\n" 10623 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10624 "}\n"); 10625 verifyFormat("void f() {\n" 10626 " other(x.begin(), //\n" 10627 " x.end(), //\n" 10628 " [&](int, int) { return 1; });\n" 10629 "}\n"); 10630 verifyFormat("SomeFunction([]() { // A cool function...\n" 10631 " return 43;\n" 10632 "});"); 10633 EXPECT_EQ("SomeFunction([]() {\n" 10634 "#define A a\n" 10635 " return 43;\n" 10636 "});", 10637 format("SomeFunction([](){\n" 10638 "#define A a\n" 10639 "return 43;\n" 10640 "});")); 10641 verifyFormat("void f() {\n" 10642 " SomeFunction([](decltype(x), A *a) {});\n" 10643 "}"); 10644 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10645 " [](const aaaaaaaaaa &a) { return a; });"); 10646 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10647 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10648 "});"); 10649 verifyFormat("Constructor()\n" 10650 " : Field([] { // comment\n" 10651 " int i;\n" 10652 " }) {}"); 10653 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10654 " return some_parameter.size();\n" 10655 "};"); 10656 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10657 " [](const string &s) { return s; };"); 10658 verifyFormat("int i = aaaaaa ? 1 //\n" 10659 " : [] {\n" 10660 " return 2; //\n" 10661 " }();"); 10662 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10663 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10664 " return x == 2; // force break\n" 10665 " });"); 10666 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10667 " int iiiiiiiiiiii) {\n" 10668 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10669 "});", 10670 getLLVMStyleWithColumns(60)); 10671 verifyFormat("SomeFunction({[&] {\n" 10672 " // comment\n" 10673 " },\n" 10674 " [&] {\n" 10675 " // comment\n" 10676 " }});"); 10677 verifyFormat("SomeFunction({[&] {\n" 10678 " // comment\n" 10679 "}});"); 10680 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10681 " [&]() { return true; },\n" 10682 " aaaaa aaaaaaaaa);"); 10683 10684 // Lambdas with return types. 10685 verifyFormat("int c = []() -> int { return 2; }();\n"); 10686 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10687 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10688 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10689 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10690 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10691 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10692 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10693 verifyFormat("[a, a]() -> a<1> {};"); 10694 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10695 " int j) -> int {\n" 10696 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10697 "};"); 10698 verifyFormat( 10699 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10700 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10701 " return aaaaaaaaaaaaaaaaa;\n" 10702 " });", 10703 getLLVMStyleWithColumns(70)); 10704 10705 // Multiple lambdas in the same parentheses change indentation rules. 10706 verifyFormat("SomeFunction(\n" 10707 " []() {\n" 10708 " int i = 42;\n" 10709 " return i;\n" 10710 " },\n" 10711 " []() {\n" 10712 " int j = 43;\n" 10713 " return j;\n" 10714 " });"); 10715 10716 // More complex introducers. 10717 verifyFormat("return [i, args...] {};"); 10718 10719 // Not lambdas. 10720 verifyFormat("constexpr char hello[]{\"hello\"};"); 10721 verifyFormat("double &operator[](int i) { return 0; }\n" 10722 "int i;"); 10723 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10724 verifyFormat("int i = a[a][a]->f();"); 10725 verifyFormat("int i = (*b)[a]->f();"); 10726 10727 // Other corner cases. 10728 verifyFormat("void f() {\n" 10729 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10730 " );\n" 10731 "}"); 10732 10733 // Lambdas created through weird macros. 10734 verifyFormat("void f() {\n" 10735 " MACRO((const AA &a) { return 1; });\n" 10736 " MACRO((AA &a) { return 1; });\n" 10737 "}"); 10738 10739 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10740 " doo_dah();\n" 10741 " doo_dah();\n" 10742 " })) {\n" 10743 "}"); 10744 verifyFormat("auto lambda = []() {\n" 10745 " int a = 2\n" 10746 "#if A\n" 10747 " + 2\n" 10748 "#endif\n" 10749 " ;\n" 10750 "};"); 10751 } 10752 10753 TEST_F(FormatTest, FormatsBlocks) { 10754 FormatStyle ShortBlocks = getLLVMStyle(); 10755 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10756 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10757 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10758 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10759 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10760 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10761 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10762 10763 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10764 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10765 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10766 10767 verifyFormat("[operation setCompletionBlock:^{\n" 10768 " [self onOperationDone];\n" 10769 "}];"); 10770 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10771 " [self onOperationDone];\n" 10772 "}]};"); 10773 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10774 " f();\n" 10775 "}];"); 10776 verifyFormat("int a = [operation block:^int(int *i) {\n" 10777 " return 1;\n" 10778 "}];"); 10779 verifyFormat("[myObject doSomethingWith:arg1\n" 10780 " aaa:^int(int *a) {\n" 10781 " return 1;\n" 10782 " }\n" 10783 " bbb:f(a * bbbbbbbb)];"); 10784 10785 verifyFormat("[operation setCompletionBlock:^{\n" 10786 " [self.delegate newDataAvailable];\n" 10787 "}];", 10788 getLLVMStyleWithColumns(60)); 10789 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10790 " NSString *path = [self sessionFilePath];\n" 10791 " if (path) {\n" 10792 " // ...\n" 10793 " }\n" 10794 "});"); 10795 verifyFormat("[[SessionService sharedService]\n" 10796 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10797 " if (window) {\n" 10798 " [self windowDidLoad:window];\n" 10799 " } else {\n" 10800 " [self errorLoadingWindow];\n" 10801 " }\n" 10802 " }];"); 10803 verifyFormat("void (^largeBlock)(void) = ^{\n" 10804 " // ...\n" 10805 "};\n", 10806 getLLVMStyleWithColumns(40)); 10807 verifyFormat("[[SessionService sharedService]\n" 10808 " loadWindowWithCompletionBlock: //\n" 10809 " ^(SessionWindow *window) {\n" 10810 " if (window) {\n" 10811 " [self windowDidLoad:window];\n" 10812 " } else {\n" 10813 " [self errorLoadingWindow];\n" 10814 " }\n" 10815 " }];", 10816 getLLVMStyleWithColumns(60)); 10817 verifyFormat("[myObject doSomethingWith:arg1\n" 10818 " firstBlock:^(Foo *a) {\n" 10819 " // ...\n" 10820 " int i;\n" 10821 " }\n" 10822 " secondBlock:^(Bar *b) {\n" 10823 " // ...\n" 10824 " int i;\n" 10825 " }\n" 10826 " thirdBlock:^Foo(Bar *b) {\n" 10827 " // ...\n" 10828 " int i;\n" 10829 " }];"); 10830 verifyFormat("[myObject doSomethingWith:arg1\n" 10831 " firstBlock:-1\n" 10832 " secondBlock:^(Bar *b) {\n" 10833 " // ...\n" 10834 " int i;\n" 10835 " }];"); 10836 10837 verifyFormat("f(^{\n" 10838 " @autoreleasepool {\n" 10839 " if (a) {\n" 10840 " g();\n" 10841 " }\n" 10842 " }\n" 10843 "});"); 10844 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10845 10846 FormatStyle FourIndent = getLLVMStyle(); 10847 FourIndent.ObjCBlockIndentWidth = 4; 10848 verifyFormat("[operation setCompletionBlock:^{\n" 10849 " [self onOperationDone];\n" 10850 "}];", 10851 FourIndent); 10852 } 10853 10854 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10855 FormatStyle ZeroColumn = getLLVMStyle(); 10856 ZeroColumn.ColumnLimit = 0; 10857 10858 verifyFormat("[[SessionService sharedService] " 10859 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10860 " if (window) {\n" 10861 " [self windowDidLoad:window];\n" 10862 " } else {\n" 10863 " [self errorLoadingWindow];\n" 10864 " }\n" 10865 "}];", 10866 ZeroColumn); 10867 EXPECT_EQ("[[SessionService sharedService]\n" 10868 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10869 " if (window) {\n" 10870 " [self windowDidLoad:window];\n" 10871 " } else {\n" 10872 " [self errorLoadingWindow];\n" 10873 " }\n" 10874 " }];", 10875 format("[[SessionService sharedService]\n" 10876 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10877 " if (window) {\n" 10878 " [self windowDidLoad:window];\n" 10879 " } else {\n" 10880 " [self errorLoadingWindow];\n" 10881 " }\n" 10882 "}];", 10883 ZeroColumn)); 10884 verifyFormat("[myObject doSomethingWith:arg1\n" 10885 " firstBlock:^(Foo *a) {\n" 10886 " // ...\n" 10887 " int i;\n" 10888 " }\n" 10889 " secondBlock:^(Bar *b) {\n" 10890 " // ...\n" 10891 " int i;\n" 10892 " }\n" 10893 " thirdBlock:^Foo(Bar *b) {\n" 10894 " // ...\n" 10895 " int i;\n" 10896 " }];", 10897 ZeroColumn); 10898 verifyFormat("f(^{\n" 10899 " @autoreleasepool {\n" 10900 " if (a) {\n" 10901 " g();\n" 10902 " }\n" 10903 " }\n" 10904 "});", 10905 ZeroColumn); 10906 verifyFormat("void (^largeBlock)(void) = ^{\n" 10907 " // ...\n" 10908 "};", 10909 ZeroColumn); 10910 10911 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10912 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10913 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10914 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10915 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10916 " int i;\n" 10917 "};", 10918 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10919 } 10920 10921 TEST_F(FormatTest, SupportsCRLF) { 10922 EXPECT_EQ("int a;\r\n" 10923 "int b;\r\n" 10924 "int c;\r\n", 10925 format("int a;\r\n" 10926 " int b;\r\n" 10927 " int c;\r\n", 10928 getLLVMStyle())); 10929 EXPECT_EQ("int a;\r\n" 10930 "int b;\r\n" 10931 "int c;\r\n", 10932 format("int a;\r\n" 10933 " int b;\n" 10934 " int c;\r\n", 10935 getLLVMStyle())); 10936 EXPECT_EQ("int a;\n" 10937 "int b;\n" 10938 "int c;\n", 10939 format("int a;\r\n" 10940 " int b;\n" 10941 " int c;\n", 10942 getLLVMStyle())); 10943 EXPECT_EQ("\"aaaaaaa \"\r\n" 10944 "\"bbbbbbb\";\r\n", 10945 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10946 EXPECT_EQ("#define A \\\r\n" 10947 " b; \\\r\n" 10948 " c; \\\r\n" 10949 " d;\r\n", 10950 format("#define A \\\r\n" 10951 " b; \\\r\n" 10952 " c; d; \r\n", 10953 getGoogleStyle())); 10954 10955 EXPECT_EQ("/*\r\n" 10956 "multi line block comments\r\n" 10957 "should not introduce\r\n" 10958 "an extra carriage return\r\n" 10959 "*/\r\n", 10960 format("/*\r\n" 10961 "multi line block comments\r\n" 10962 "should not introduce\r\n" 10963 "an extra carriage return\r\n" 10964 "*/\r\n")); 10965 } 10966 10967 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10968 verifyFormat("MY_CLASS(C) {\n" 10969 " int i;\n" 10970 " int j;\n" 10971 "};"); 10972 } 10973 10974 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10975 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10976 TwoIndent.ContinuationIndentWidth = 2; 10977 10978 EXPECT_EQ("int i =\n" 10979 " longFunction(\n" 10980 " arg);", 10981 format("int i = longFunction(arg);", TwoIndent)); 10982 10983 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10984 SixIndent.ContinuationIndentWidth = 6; 10985 10986 EXPECT_EQ("int i =\n" 10987 " longFunction(\n" 10988 " arg);", 10989 format("int i = longFunction(arg);", SixIndent)); 10990 } 10991 10992 TEST_F(FormatTest, SpacesInAngles) { 10993 FormatStyle Spaces = getLLVMStyle(); 10994 Spaces.SpacesInAngles = true; 10995 10996 verifyFormat("static_cast< int >(arg);", Spaces); 10997 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10998 verifyFormat("f< int, float >();", Spaces); 10999 verifyFormat("template <> g() {}", Spaces); 11000 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11001 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11002 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11003 Spaces); 11004 11005 Spaces.Standard = FormatStyle::LS_Cpp03; 11006 Spaces.SpacesInAngles = true; 11007 verifyFormat("A< A< int > >();", Spaces); 11008 11009 Spaces.SpacesInAngles = false; 11010 verifyFormat("A<A<int> >();", Spaces); 11011 11012 Spaces.Standard = FormatStyle::LS_Cpp11; 11013 Spaces.SpacesInAngles = true; 11014 verifyFormat("A< A< int > >();", Spaces); 11015 11016 Spaces.SpacesInAngles = false; 11017 verifyFormat("A<A<int>>();", Spaces); 11018 } 11019 11020 TEST_F(FormatTest, TripleAngleBrackets) { 11021 verifyFormat("f<<<1, 1>>>();"); 11022 verifyFormat("f<<<1, 1, 1, s>>>();"); 11023 verifyFormat("f<<<a, b, c, d>>>();"); 11024 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11025 verifyFormat("f<param><<<1, 1>>>();"); 11026 verifyFormat("f<1><<<1, 1>>>();"); 11027 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11028 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11029 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11030 } 11031 11032 TEST_F(FormatTest, MergeLessLessAtEnd) { 11033 verifyFormat("<<"); 11034 EXPECT_EQ("< < <", format("\\\n<<<")); 11035 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11036 "aaallvm::outs() <<"); 11037 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11038 "aaaallvm::outs()\n <<"); 11039 } 11040 11041 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11042 std::string code = "#if A\n" 11043 "#if B\n" 11044 "a.\n" 11045 "#endif\n" 11046 " a = 1;\n" 11047 "#else\n" 11048 "#endif\n" 11049 "#if C\n" 11050 "#else\n" 11051 "#endif\n"; 11052 EXPECT_EQ(code, format(code)); 11053 } 11054 11055 TEST_F(FormatTest, HandleConflictMarkers) { 11056 // Git/SVN conflict markers. 11057 EXPECT_EQ("int a;\n" 11058 "void f() {\n" 11059 " callme(some(parameter1,\n" 11060 "<<<<<<< text by the vcs\n" 11061 " parameter2),\n" 11062 "||||||| text by the vcs\n" 11063 " parameter2),\n" 11064 " parameter3,\n" 11065 "======= text by the vcs\n" 11066 " parameter2, parameter3),\n" 11067 ">>>>>>> text by the vcs\n" 11068 " otherparameter);\n", 11069 format("int a;\n" 11070 "void f() {\n" 11071 " callme(some(parameter1,\n" 11072 "<<<<<<< text by the vcs\n" 11073 " parameter2),\n" 11074 "||||||| text by the vcs\n" 11075 " parameter2),\n" 11076 " parameter3,\n" 11077 "======= text by the vcs\n" 11078 " parameter2,\n" 11079 " parameter3),\n" 11080 ">>>>>>> text by the vcs\n" 11081 " otherparameter);\n")); 11082 11083 // Perforce markers. 11084 EXPECT_EQ("void f() {\n" 11085 " function(\n" 11086 ">>>> text by the vcs\n" 11087 " parameter,\n" 11088 "==== text by the vcs\n" 11089 " parameter,\n" 11090 "==== text by the vcs\n" 11091 " parameter,\n" 11092 "<<<< text by the vcs\n" 11093 " parameter);\n", 11094 format("void f() {\n" 11095 " function(\n" 11096 ">>>> text by the vcs\n" 11097 " parameter,\n" 11098 "==== text by the vcs\n" 11099 " parameter,\n" 11100 "==== text by the vcs\n" 11101 " parameter,\n" 11102 "<<<< text by the vcs\n" 11103 " parameter);\n")); 11104 11105 EXPECT_EQ("<<<<<<<\n" 11106 "|||||||\n" 11107 "=======\n" 11108 ">>>>>>>", 11109 format("<<<<<<<\n" 11110 "|||||||\n" 11111 "=======\n" 11112 ">>>>>>>")); 11113 11114 EXPECT_EQ("<<<<<<<\n" 11115 "|||||||\n" 11116 "int i;\n" 11117 "=======\n" 11118 ">>>>>>>", 11119 format("<<<<<<<\n" 11120 "|||||||\n" 11121 "int i;\n" 11122 "=======\n" 11123 ">>>>>>>")); 11124 11125 // FIXME: Handle parsing of macros around conflict markers correctly: 11126 EXPECT_EQ("#define Macro \\\n" 11127 "<<<<<<<\n" 11128 "Something \\\n" 11129 "|||||||\n" 11130 "Else \\\n" 11131 "=======\n" 11132 "Other \\\n" 11133 ">>>>>>>\n" 11134 " End int i;\n", 11135 format("#define Macro \\\n" 11136 "<<<<<<<\n" 11137 " Something \\\n" 11138 "|||||||\n" 11139 " Else \\\n" 11140 "=======\n" 11141 " Other \\\n" 11142 ">>>>>>>\n" 11143 " End\n" 11144 "int i;\n")); 11145 } 11146 11147 TEST_F(FormatTest, DisableRegions) { 11148 EXPECT_EQ("int i;\n" 11149 "// clang-format off\n" 11150 " int j;\n" 11151 "// clang-format on\n" 11152 "int k;", 11153 format(" int i;\n" 11154 " // clang-format off\n" 11155 " int j;\n" 11156 " // clang-format on\n" 11157 " int k;")); 11158 EXPECT_EQ("int i;\n" 11159 "/* clang-format off */\n" 11160 " int j;\n" 11161 "/* clang-format on */\n" 11162 "int k;", 11163 format(" int i;\n" 11164 " /* clang-format off */\n" 11165 " int j;\n" 11166 " /* clang-format on */\n" 11167 " int k;")); 11168 } 11169 11170 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11171 format("? ) ="); 11172 verifyNoCrash("#define a\\\n /**/}"); 11173 } 11174 11175 TEST_F(FormatTest, FormatsTableGenCode) { 11176 FormatStyle Style = getLLVMStyle(); 11177 Style.Language = FormatStyle::LK_TableGen; 11178 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11179 } 11180 11181 class ReplacementTest : public ::testing::Test { 11182 protected: 11183 tooling::Replacement createReplacement(SourceLocation Start, unsigned Length, 11184 llvm::StringRef ReplacementText) { 11185 return tooling::Replacement(Context.Sources, Start, Length, 11186 ReplacementText); 11187 } 11188 11189 RewriterTestContext Context; 11190 }; 11191 11192 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11193 // Column limit is 20. 11194 std::string Code = "Type *a =\n" 11195 " new Type();\n" 11196 "g(iiiii, 0, jjjjj,\n" 11197 " 0, kkkkk, 0, mm);\n" 11198 "int bad = format ;"; 11199 std::string Expected = "auto a = new Type();\n" 11200 "g(iiiii, nullptr,\n" 11201 " jjjjj, nullptr,\n" 11202 " kkkkk, nullptr,\n" 11203 " mm);\n" 11204 "int bad = format ;"; 11205 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11206 tooling::Replacements Replaces; 11207 Replaces.insert(tooling::Replacement( 11208 Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto ")); 11209 Replaces.insert(tooling::Replacement( 11210 Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr")); 11211 Replaces.insert(tooling::Replacement( 11212 Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr")); 11213 Replaces.insert(tooling::Replacement( 11214 Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr")); 11215 11216 format::FormatStyle Style = format::getLLVMStyle(); 11217 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11218 EXPECT_EQ(Expected, applyAllReplacementsAndFormat(Code, Replaces, Style)); 11219 } 11220 11221 } // end namespace 11222 } // end namespace format 11223 } // end namespace clang 11224