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/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum IncompleteCheck { 34 IC_ExpectComplete, 35 IC_ExpectIncomplete, 36 IC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 bool IncompleteFormat = false; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 48 if (CheckIncomplete != IC_DoNotCheck) { 49 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 50 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 60 FormatStyle Style = getLLVMStyle(); 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 66 FormatStyle Style = getGoogleStyle(); 67 Style.ColumnLimit = ColumnLimit; 68 return Style; 69 } 70 71 void verifyFormat(llvm::StringRef Code, 72 const FormatStyle &Style = getLLVMStyle()) { 73 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 74 } 75 76 void verifyIncompleteFormat(llvm::StringRef Code, 77 const FormatStyle &Style = getLLVMStyle()) { 78 EXPECT_EQ(Code.str(), 79 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 80 } 81 82 void verifyGoogleFormat(llvm::StringRef Code) { 83 verifyFormat(Code, getGoogleStyle()); 84 } 85 86 void verifyIndependentOfContext(llvm::StringRef text) { 87 verifyFormat(text); 88 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 89 } 90 91 /// \brief Verify that clang-format does not crash on the given input. 92 void verifyNoCrash(llvm::StringRef Code, 93 const FormatStyle &Style = getLLVMStyle()) { 94 format(Code, Style, IC_DoNotCheck); 95 } 96 97 int ReplacementCount; 98 }; 99 100 TEST_F(FormatTest, MessUp) { 101 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 102 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 103 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 104 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 105 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 106 } 107 108 //===----------------------------------------------------------------------===// 109 // Basic function tests. 110 //===----------------------------------------------------------------------===// 111 112 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 113 EXPECT_EQ(";", format(";")); 114 } 115 116 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 117 EXPECT_EQ("int i;", format(" int i;")); 118 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 119 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 120 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 121 } 122 123 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 124 EXPECT_EQ("int i;", format("int\ni;")); 125 } 126 127 TEST_F(FormatTest, FormatsNestedBlockStatements) { 128 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 129 } 130 131 TEST_F(FormatTest, FormatsNestedCall) { 132 verifyFormat("Method(f1, f2(f3));"); 133 verifyFormat("Method(f1(f2, f3()));"); 134 verifyFormat("Method(f1(f2, (f3())));"); 135 } 136 137 TEST_F(FormatTest, NestedNameSpecifiers) { 138 verifyFormat("vector<::Type> v;"); 139 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 140 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 141 verifyFormat("bool a = 2 < ::SomeFunction();"); 142 } 143 144 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 145 EXPECT_EQ("if (a) {\n" 146 " f();\n" 147 "}", 148 format("if(a){f();}")); 149 EXPECT_EQ(4, ReplacementCount); 150 EXPECT_EQ("if (a) {\n" 151 " f();\n" 152 "}", 153 format("if (a) {\n" 154 " f();\n" 155 "}")); 156 EXPECT_EQ(0, ReplacementCount); 157 EXPECT_EQ("/*\r\n" 158 "\r\n" 159 "*/\r\n", 160 format("/*\r\n" 161 "\r\n" 162 "*/\r\n")); 163 EXPECT_EQ(0, ReplacementCount); 164 } 165 166 TEST_F(FormatTest, RemovesEmptyLines) { 167 EXPECT_EQ("class C {\n" 168 " int i;\n" 169 "};", 170 format("class C {\n" 171 " int i;\n" 172 "\n" 173 "};")); 174 175 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 176 EXPECT_EQ("namespace N {\n" 177 "\n" 178 "int i;\n" 179 "}", 180 format("namespace N {\n" 181 "\n" 182 "int i;\n" 183 "}", 184 getGoogleStyle())); 185 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 186 "\n" 187 "int i;\n" 188 "}", 189 format("extern /**/ \"C\" /**/ {\n" 190 "\n" 191 "int i;\n" 192 "}", 193 getGoogleStyle())); 194 195 // ...but do keep inlining and removing empty lines for non-block extern "C" 196 // functions. 197 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 198 EXPECT_EQ("extern \"C\" int f() {\n" 199 " int i = 42;\n" 200 " return i;\n" 201 "}", 202 format("extern \"C\" int f() {\n" 203 "\n" 204 " int i = 42;\n" 205 " return i;\n" 206 "}", 207 getGoogleStyle())); 208 209 // Remove empty lines at the beginning and end of blocks. 210 EXPECT_EQ("void f() {\n" 211 "\n" 212 " if (a) {\n" 213 "\n" 214 " f();\n" 215 " }\n" 216 "}", 217 format("void f() {\n" 218 "\n" 219 " if (a) {\n" 220 "\n" 221 " f();\n" 222 "\n" 223 " }\n" 224 "\n" 225 "}", 226 getLLVMStyle())); 227 EXPECT_EQ("void f() {\n" 228 " if (a) {\n" 229 " f();\n" 230 " }\n" 231 "}", 232 format("void f() {\n" 233 "\n" 234 " if (a) {\n" 235 "\n" 236 " f();\n" 237 "\n" 238 " }\n" 239 "\n" 240 "}", 241 getGoogleStyle())); 242 243 // Don't remove empty lines in more complex control statements. 244 EXPECT_EQ("void f() {\n" 245 " if (a) {\n" 246 " f();\n" 247 "\n" 248 " } else if (b) {\n" 249 " f();\n" 250 " }\n" 251 "}", 252 format("void f() {\n" 253 " if (a) {\n" 254 " f();\n" 255 "\n" 256 " } else if (b) {\n" 257 " f();\n" 258 "\n" 259 " }\n" 260 "\n" 261 "}")); 262 263 // FIXME: This is slightly inconsistent. 264 EXPECT_EQ("namespace {\n" 265 "int i;\n" 266 "}", 267 format("namespace {\n" 268 "int i;\n" 269 "\n" 270 "}")); 271 EXPECT_EQ("namespace {\n" 272 "int i;\n" 273 "\n" 274 "} // namespace", 275 format("namespace {\n" 276 "int i;\n" 277 "\n" 278 "} // namespace")); 279 } 280 281 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 282 verifyFormat("x = (a) and (b);"); 283 verifyFormat("x = (a) or (b);"); 284 verifyFormat("x = (a) bitand (b);"); 285 verifyFormat("x = (a) bitor (b);"); 286 verifyFormat("x = (a) not_eq (b);"); 287 verifyFormat("x = (a) and_eq (b);"); 288 verifyFormat("x = (a) or_eq (b);"); 289 verifyFormat("x = (a) xor (b);"); 290 } 291 292 //===----------------------------------------------------------------------===// 293 // Tests for control statements. 294 //===----------------------------------------------------------------------===// 295 296 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 297 verifyFormat("if (true)\n f();\ng();"); 298 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 299 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 300 301 FormatStyle AllowsMergedIf = getLLVMStyle(); 302 AllowsMergedIf.AlignEscapedNewlinesLeft = true; 303 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 304 verifyFormat("if (a)\n" 305 " // comment\n" 306 " f();", 307 AllowsMergedIf); 308 verifyFormat("{\n" 309 " if (a)\n" 310 " label:\n" 311 " f();\n" 312 "}", 313 AllowsMergedIf); 314 verifyFormat("#define A \\\n" 315 " if (a) \\\n" 316 " label: \\\n" 317 " f()", 318 AllowsMergedIf); 319 verifyFormat("if (a)\n" 320 " ;", 321 AllowsMergedIf); 322 verifyFormat("if (a)\n" 323 " if (b) return;", 324 AllowsMergedIf); 325 326 verifyFormat("if (a) // Can't merge this\n" 327 " f();\n", 328 AllowsMergedIf); 329 verifyFormat("if (a) /* still don't merge */\n" 330 " f();", 331 AllowsMergedIf); 332 verifyFormat("if (a) { // Never merge this\n" 333 " f();\n" 334 "}", 335 AllowsMergedIf); 336 verifyFormat("if (a) { /* Never merge this */\n" 337 " f();\n" 338 "}", 339 AllowsMergedIf); 340 341 AllowsMergedIf.ColumnLimit = 14; 342 verifyFormat("if (a) return;", AllowsMergedIf); 343 verifyFormat("if (aaaaaaaaa)\n" 344 " return;", 345 AllowsMergedIf); 346 347 AllowsMergedIf.ColumnLimit = 13; 348 verifyFormat("if (a)\n return;", AllowsMergedIf); 349 } 350 351 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 352 FormatStyle AllowsMergedLoops = getLLVMStyle(); 353 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 354 verifyFormat("while (true) continue;", AllowsMergedLoops); 355 verifyFormat("for (;;) continue;", AllowsMergedLoops); 356 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 357 verifyFormat("while (true)\n" 358 " ;", 359 AllowsMergedLoops); 360 verifyFormat("for (;;)\n" 361 " ;", 362 AllowsMergedLoops); 363 verifyFormat("for (;;)\n" 364 " for (;;) continue;", 365 AllowsMergedLoops); 366 verifyFormat("for (;;) // Can't merge this\n" 367 " continue;", 368 AllowsMergedLoops); 369 verifyFormat("for (;;) /* still don't merge */\n" 370 " continue;", 371 AllowsMergedLoops); 372 } 373 374 TEST_F(FormatTest, FormatShortBracedStatements) { 375 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 376 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 377 378 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 379 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 380 381 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 382 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 383 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 384 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 385 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 386 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 387 verifyFormat("if (true) { //\n" 388 " f();\n" 389 "}", 390 AllowSimpleBracedStatements); 391 verifyFormat("if (true) {\n" 392 " f();\n" 393 " f();\n" 394 "}", 395 AllowSimpleBracedStatements); 396 verifyFormat("if (true) {\n" 397 " f();\n" 398 "} else {\n" 399 " f();\n" 400 "}", 401 AllowSimpleBracedStatements); 402 403 verifyFormat("template <int> struct A2 {\n" 404 " struct B {};\n" 405 "};", 406 AllowSimpleBracedStatements); 407 408 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 409 verifyFormat("if (true) {\n" 410 " f();\n" 411 "}", 412 AllowSimpleBracedStatements); 413 verifyFormat("if (true) {\n" 414 " f();\n" 415 "} else {\n" 416 " f();\n" 417 "}", 418 AllowSimpleBracedStatements); 419 420 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 421 verifyFormat("while (true) {\n" 422 " f();\n" 423 "}", 424 AllowSimpleBracedStatements); 425 verifyFormat("for (;;) {\n" 426 " f();\n" 427 "}", 428 AllowSimpleBracedStatements); 429 } 430 431 TEST_F(FormatTest, ParseIfElse) { 432 verifyFormat("if (true)\n" 433 " if (true)\n" 434 " if (true)\n" 435 " f();\n" 436 " else\n" 437 " g();\n" 438 " else\n" 439 " h();\n" 440 "else\n" 441 " i();"); 442 verifyFormat("if (true)\n" 443 " if (true)\n" 444 " if (true) {\n" 445 " if (true)\n" 446 " f();\n" 447 " } else {\n" 448 " g();\n" 449 " }\n" 450 " else\n" 451 " h();\n" 452 "else {\n" 453 " i();\n" 454 "}"); 455 verifyFormat("void f() {\n" 456 " if (a) {\n" 457 " } else {\n" 458 " }\n" 459 "}"); 460 } 461 462 TEST_F(FormatTest, ElseIf) { 463 verifyFormat("if (a) {\n} else if (b) {\n}"); 464 verifyFormat("if (a)\n" 465 " f();\n" 466 "else if (b)\n" 467 " g();\n" 468 "else\n" 469 " h();"); 470 verifyFormat("if (a) {\n" 471 " f();\n" 472 "}\n" 473 "// or else ..\n" 474 "else {\n" 475 " g()\n" 476 "}"); 477 478 verifyFormat("if (a) {\n" 479 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 481 "}"); 482 verifyFormat("if (a) {\n" 483 "} else if (\n" 484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 485 "}", 486 getLLVMStyleWithColumns(62)); 487 } 488 489 TEST_F(FormatTest, FormatsForLoop) { 490 verifyFormat( 491 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 492 " ++VeryVeryLongLoopVariable)\n" 493 " ;"); 494 verifyFormat("for (;;)\n" 495 " f();"); 496 verifyFormat("for (;;) {\n}"); 497 verifyFormat("for (;;) {\n" 498 " f();\n" 499 "}"); 500 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 501 502 verifyFormat( 503 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 504 " E = UnwrappedLines.end();\n" 505 " I != E; ++I) {\n}"); 506 507 verifyFormat( 508 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 509 " ++IIIII) {\n}"); 510 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 511 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 512 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 513 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 514 " I = FD->getDeclsInPrototypeScope().begin(),\n" 515 " E = FD->getDeclsInPrototypeScope().end();\n" 516 " I != E; ++I) {\n}"); 517 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 518 " I = Container.begin(),\n" 519 " E = Container.end();\n" 520 " I != E; ++I) {\n}", 521 getLLVMStyleWithColumns(76)); 522 523 verifyFormat( 524 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 528 " ++aaaaaaaaaaa) {\n}"); 529 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 530 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 531 " ++i) {\n}"); 532 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 533 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 534 "}"); 535 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 536 " aaaaaaaaaa);\n" 537 " iter; ++iter) {\n" 538 "}"); 539 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 541 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 542 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 543 544 FormatStyle NoBinPacking = getLLVMStyle(); 545 NoBinPacking.BinPackParameters = false; 546 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 547 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 548 " aaaaaaaaaaaaaaaa,\n" 549 " aaaaaaaaaaaaaaaa,\n" 550 " aaaaaaaaaaaaaaaa);\n" 551 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 552 "}", 553 NoBinPacking); 554 verifyFormat( 555 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 556 " E = UnwrappedLines.end();\n" 557 " I != E;\n" 558 " ++I) {\n}", 559 NoBinPacking); 560 } 561 562 TEST_F(FormatTest, RangeBasedForLoops) { 563 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 565 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 566 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 567 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 569 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 570 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 571 } 572 573 TEST_F(FormatTest, ForEachLoops) { 574 verifyFormat("void f() {\n" 575 " foreach (Item *item, itemlist) {}\n" 576 " Q_FOREACH (Item *item, itemlist) {}\n" 577 " BOOST_FOREACH (Item *item, itemlist) {}\n" 578 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 579 "}"); 580 581 // As function-like macros. 582 verifyFormat("#define foreach(x, y)\n" 583 "#define Q_FOREACH(x, y)\n" 584 "#define BOOST_FOREACH(x, y)\n" 585 "#define UNKNOWN_FOREACH(x, y)\n"); 586 587 // Not as function-like macros. 588 verifyFormat("#define foreach (x, y)\n" 589 "#define Q_FOREACH (x, y)\n" 590 "#define BOOST_FOREACH (x, y)\n" 591 "#define UNKNOWN_FOREACH (x, y)\n"); 592 } 593 594 TEST_F(FormatTest, FormatsWhileLoop) { 595 verifyFormat("while (true) {\n}"); 596 verifyFormat("while (true)\n" 597 " f();"); 598 verifyFormat("while () {\n}"); 599 verifyFormat("while () {\n" 600 " f();\n" 601 "}"); 602 } 603 604 TEST_F(FormatTest, FormatsDoWhile) { 605 verifyFormat("do {\n" 606 " do_something();\n" 607 "} while (something());"); 608 verifyFormat("do\n" 609 " do_something();\n" 610 "while (something());"); 611 } 612 613 TEST_F(FormatTest, FormatsSwitchStatement) { 614 verifyFormat("switch (x) {\n" 615 "case 1:\n" 616 " f();\n" 617 " break;\n" 618 "case kFoo:\n" 619 "case ns::kBar:\n" 620 "case kBaz:\n" 621 " break;\n" 622 "default:\n" 623 " g();\n" 624 " break;\n" 625 "}"); 626 verifyFormat("switch (x) {\n" 627 "case 1: {\n" 628 " f();\n" 629 " break;\n" 630 "}\n" 631 "case 2: {\n" 632 " break;\n" 633 "}\n" 634 "}"); 635 verifyFormat("switch (x) {\n" 636 "case 1: {\n" 637 " f();\n" 638 " {\n" 639 " g();\n" 640 " h();\n" 641 " }\n" 642 " break;\n" 643 "}\n" 644 "}"); 645 verifyFormat("switch (x) {\n" 646 "case 1: {\n" 647 " f();\n" 648 " if (foo) {\n" 649 " g();\n" 650 " h();\n" 651 " }\n" 652 " break;\n" 653 "}\n" 654 "}"); 655 verifyFormat("switch (x) {\n" 656 "case 1: {\n" 657 " f();\n" 658 " g();\n" 659 "} break;\n" 660 "}"); 661 verifyFormat("switch (test)\n" 662 " ;"); 663 verifyFormat("switch (x) {\n" 664 "default: {\n" 665 " // Do nothing.\n" 666 "}\n" 667 "}"); 668 verifyFormat("switch (x) {\n" 669 "// comment\n" 670 "// if 1, do f()\n" 671 "case 1:\n" 672 " f();\n" 673 "}"); 674 verifyFormat("switch (x) {\n" 675 "case 1:\n" 676 " // Do amazing stuff\n" 677 " {\n" 678 " f();\n" 679 " g();\n" 680 " }\n" 681 " break;\n" 682 "}"); 683 verifyFormat("#define A \\\n" 684 " switch (x) { \\\n" 685 " case a: \\\n" 686 " foo = b; \\\n" 687 " }", 688 getLLVMStyleWithColumns(20)); 689 verifyFormat("#define OPERATION_CASE(name) \\\n" 690 " case OP_name: \\\n" 691 " return operations::Operation##name\n", 692 getLLVMStyleWithColumns(40)); 693 verifyFormat("switch (x) {\n" 694 "case 1:;\n" 695 "default:;\n" 696 " int i;\n" 697 "}"); 698 699 verifyGoogleFormat("switch (x) {\n" 700 " case 1:\n" 701 " f();\n" 702 " break;\n" 703 " case kFoo:\n" 704 " case ns::kBar:\n" 705 " case kBaz:\n" 706 " break;\n" 707 " default:\n" 708 " g();\n" 709 " break;\n" 710 "}"); 711 verifyGoogleFormat("switch (x) {\n" 712 " case 1: {\n" 713 " f();\n" 714 " break;\n" 715 " }\n" 716 "}"); 717 verifyGoogleFormat("switch (test)\n" 718 " ;"); 719 720 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 721 " case OP_name: \\\n" 722 " return operations::Operation##name\n"); 723 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 724 " // Get the correction operation class.\n" 725 " switch (OpCode) {\n" 726 " CASE(Add);\n" 727 " CASE(Subtract);\n" 728 " default:\n" 729 " return operations::Unknown;\n" 730 " }\n" 731 "#undef OPERATION_CASE\n" 732 "}"); 733 verifyFormat("DEBUG({\n" 734 " switch (x) {\n" 735 " case A:\n" 736 " f();\n" 737 " break;\n" 738 " // On B:\n" 739 " case B:\n" 740 " g();\n" 741 " break;\n" 742 " }\n" 743 "});"); 744 verifyFormat("switch (a) {\n" 745 "case (b):\n" 746 " return;\n" 747 "}"); 748 749 verifyFormat("switch (a) {\n" 750 "case some_namespace::\n" 751 " some_constant:\n" 752 " return;\n" 753 "}", 754 getLLVMStyleWithColumns(34)); 755 } 756 757 TEST_F(FormatTest, CaseRanges) { 758 verifyFormat("switch (x) {\n" 759 "case 'A' ... 'Z':\n" 760 "case 1 ... 5:\n" 761 "case a ... b:\n" 762 " break;\n" 763 "}"); 764 } 765 766 TEST_F(FormatTest, ShortCaseLabels) { 767 FormatStyle Style = getLLVMStyle(); 768 Style.AllowShortCaseLabelsOnASingleLine = true; 769 verifyFormat("switch (a) {\n" 770 "case 1: x = 1; break;\n" 771 "case 2: return;\n" 772 "case 3:\n" 773 "case 4:\n" 774 "case 5: return;\n" 775 "case 6: // comment\n" 776 " return;\n" 777 "case 7:\n" 778 " // comment\n" 779 " return;\n" 780 "case 8:\n" 781 " x = 8; // comment\n" 782 " break;\n" 783 "default: y = 1; break;\n" 784 "}", 785 Style); 786 verifyFormat("switch (a) {\n" 787 "#if FOO\n" 788 "case 0: return 0;\n" 789 "#endif\n" 790 "}", 791 Style); 792 verifyFormat("switch (a) {\n" 793 "case 1: {\n" 794 "}\n" 795 "case 2: {\n" 796 " return;\n" 797 "}\n" 798 "case 3: {\n" 799 " x = 1;\n" 800 " return;\n" 801 "}\n" 802 "case 4:\n" 803 " if (x)\n" 804 " return;\n" 805 "}", 806 Style); 807 Style.ColumnLimit = 21; 808 verifyFormat("switch (a) {\n" 809 "case 1: x = 1; break;\n" 810 "case 2: return;\n" 811 "case 3:\n" 812 "case 4:\n" 813 "case 5: return;\n" 814 "default:\n" 815 " y = 1;\n" 816 " break;\n" 817 "}", 818 Style); 819 } 820 821 TEST_F(FormatTest, FormatsLabels) { 822 verifyFormat("void f() {\n" 823 " some_code();\n" 824 "test_label:\n" 825 " some_other_code();\n" 826 " {\n" 827 " some_more_code();\n" 828 " another_label:\n" 829 " some_more_code();\n" 830 " }\n" 831 "}"); 832 verifyFormat("{\n" 833 " some_code();\n" 834 "test_label:\n" 835 " some_other_code();\n" 836 "}"); 837 verifyFormat("{\n" 838 " some_code();\n" 839 "test_label:;\n" 840 " int i = 0;\n" 841 "}"); 842 } 843 844 //===----------------------------------------------------------------------===// 845 // Tests for comments. 846 //===----------------------------------------------------------------------===// 847 848 TEST_F(FormatTest, UnderstandsSingleLineComments) { 849 verifyFormat("//* */"); 850 verifyFormat("// line 1\n" 851 "// line 2\n" 852 "void f() {}\n"); 853 854 verifyFormat("void f() {\n" 855 " // Doesn't do anything\n" 856 "}"); 857 verifyFormat("SomeObject\n" 858 " // Calling someFunction on SomeObject\n" 859 " .someFunction();"); 860 verifyFormat("auto result = SomeObject\n" 861 " // Calling someFunction on SomeObject\n" 862 " .someFunction();"); 863 verifyFormat("void f(int i, // some comment (probably for i)\n" 864 " int j, // some comment (probably for j)\n" 865 " int k); // some comment (probably for k)"); 866 verifyFormat("void f(int i,\n" 867 " // some comment (probably for j)\n" 868 " int j,\n" 869 " // some comment (probably for k)\n" 870 " int k);"); 871 872 verifyFormat("int i // This is a fancy variable\n" 873 " = 5; // with nicely aligned comment."); 874 875 verifyFormat("// Leading comment.\n" 876 "int a; // Trailing comment."); 877 verifyFormat("int a; // Trailing comment\n" 878 " // on 2\n" 879 " // or 3 lines.\n" 880 "int b;"); 881 verifyFormat("int a; // Trailing comment\n" 882 "\n" 883 "// Leading comment.\n" 884 "int b;"); 885 verifyFormat("int a; // Comment.\n" 886 " // More details.\n" 887 "int bbbb; // Another comment."); 888 verifyFormat( 889 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 890 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 891 "int cccccccccccccccccccccccccccccc; // comment\n" 892 "int ddd; // looooooooooooooooooooooooong comment\n" 893 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 894 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 895 "int ccccccccccccccccccc; // comment"); 896 897 verifyFormat("#include \"a\" // comment\n" 898 "#include \"a/b/c\" // comment"); 899 verifyFormat("#include <a> // comment\n" 900 "#include <a/b/c> // comment"); 901 EXPECT_EQ("#include \"a\" // comment\n" 902 "#include \"a/b/c\" // comment", 903 format("#include \\\n" 904 " \"a\" // comment\n" 905 "#include \"a/b/c\" // comment")); 906 907 verifyFormat("enum E {\n" 908 " // comment\n" 909 " VAL_A, // comment\n" 910 " VAL_B\n" 911 "};"); 912 913 verifyFormat( 914 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 915 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 916 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 917 " // Comment inside a statement.\n" 918 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 919 verifyFormat("SomeFunction(a,\n" 920 " // comment\n" 921 " b + x);"); 922 verifyFormat("SomeFunction(a, a,\n" 923 " // comment\n" 924 " b + x);"); 925 verifyFormat( 926 "bool aaaaaaaaaaaaa = // comment\n" 927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 929 930 verifyFormat("int aaaa; // aaaaa\n" 931 "int aa; // aaaaaaa", 932 getLLVMStyleWithColumns(20)); 933 934 EXPECT_EQ("void f() { // This does something ..\n" 935 "}\n" 936 "int a; // This is unrelated", 937 format("void f() { // This does something ..\n" 938 " }\n" 939 "int a; // This is unrelated")); 940 EXPECT_EQ("class C {\n" 941 " void f() { // This does something ..\n" 942 " } // awesome..\n" 943 "\n" 944 " int a; // This is unrelated\n" 945 "};", 946 format("class C{void f() { // This does something ..\n" 947 " } // awesome..\n" 948 " \n" 949 "int a; // This is unrelated\n" 950 "};")); 951 952 EXPECT_EQ("int i; // single line trailing comment", 953 format("int i;\\\n// single line trailing comment")); 954 955 verifyGoogleFormat("int a; // Trailing comment."); 956 957 verifyFormat("someFunction(anotherFunction( // Force break.\n" 958 " parameter));"); 959 960 verifyGoogleFormat("#endif // HEADER_GUARD"); 961 962 verifyFormat("const char *test[] = {\n" 963 " // A\n" 964 " \"aaaa\",\n" 965 " // B\n" 966 " \"aaaaa\"};"); 967 verifyGoogleFormat( 968 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 969 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 970 EXPECT_EQ("D(a, {\n" 971 " // test\n" 972 " int a;\n" 973 "});", 974 format("D(a, {\n" 975 "// test\n" 976 "int a;\n" 977 "});")); 978 979 EXPECT_EQ("lineWith(); // comment\n" 980 "// at start\n" 981 "otherLine();", 982 format("lineWith(); // comment\n" 983 "// at start\n" 984 "otherLine();")); 985 EXPECT_EQ("lineWith(); // comment\n" 986 "/*\n" 987 " * at start */\n" 988 "otherLine();", 989 format("lineWith(); // comment\n" 990 "/*\n" 991 " * at start */\n" 992 "otherLine();")); 993 EXPECT_EQ("lineWith(); // comment\n" 994 " // at start\n" 995 "otherLine();", 996 format("lineWith(); // comment\n" 997 " // at start\n" 998 "otherLine();")); 999 1000 EXPECT_EQ("lineWith(); // comment\n" 1001 "// at start\n" 1002 "otherLine(); // comment", 1003 format("lineWith(); // comment\n" 1004 "// at start\n" 1005 "otherLine(); // comment")); 1006 EXPECT_EQ("lineWith();\n" 1007 "// at start\n" 1008 "otherLine(); // comment", 1009 format("lineWith();\n" 1010 " // at start\n" 1011 "otherLine(); // comment")); 1012 EXPECT_EQ("// first\n" 1013 "// at start\n" 1014 "otherLine(); // comment", 1015 format("// first\n" 1016 " // at start\n" 1017 "otherLine(); // comment")); 1018 EXPECT_EQ("f();\n" 1019 "// first\n" 1020 "// at start\n" 1021 "otherLine(); // comment", 1022 format("f();\n" 1023 "// first\n" 1024 " // at start\n" 1025 "otherLine(); // comment")); 1026 verifyFormat("f(); // comment\n" 1027 "// first\n" 1028 "// at start\n" 1029 "otherLine();"); 1030 EXPECT_EQ("f(); // comment\n" 1031 "// first\n" 1032 "// at start\n" 1033 "otherLine();", 1034 format("f(); // comment\n" 1035 "// first\n" 1036 " // at start\n" 1037 "otherLine();")); 1038 EXPECT_EQ("f(); // comment\n" 1039 " // first\n" 1040 "// at start\n" 1041 "otherLine();", 1042 format("f(); // comment\n" 1043 " // first\n" 1044 "// at start\n" 1045 "otherLine();")); 1046 EXPECT_EQ("void f() {\n" 1047 " lineWith(); // comment\n" 1048 " // at start\n" 1049 "}", 1050 format("void f() {\n" 1051 " lineWith(); // comment\n" 1052 " // at start\n" 1053 "}")); 1054 EXPECT_EQ("int xy; // a\n" 1055 "int z; // b", 1056 format("int xy; // a\n" 1057 "int z; //b")); 1058 EXPECT_EQ("int xy; // a\n" 1059 "int z; // bb", 1060 format("int xy; // a\n" 1061 "int z; //bb", 1062 getLLVMStyleWithColumns(12))); 1063 1064 verifyFormat("#define A \\\n" 1065 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1066 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1067 getLLVMStyleWithColumns(60)); 1068 verifyFormat( 1069 "#define A \\\n" 1070 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1071 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1072 getLLVMStyleWithColumns(61)); 1073 1074 verifyFormat("if ( // This is some comment\n" 1075 " x + 3) {\n" 1076 "}"); 1077 EXPECT_EQ("if ( // This is some comment\n" 1078 " // spanning two lines\n" 1079 " x + 3) {\n" 1080 "}", 1081 format("if( // This is some comment\n" 1082 " // spanning two lines\n" 1083 " x + 3) {\n" 1084 "}")); 1085 1086 verifyNoCrash("/\\\n/"); 1087 verifyNoCrash("/\\\n* */"); 1088 // The 0-character somehow makes the lexer return a proper comment. 1089 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1090 } 1091 1092 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1093 EXPECT_EQ("SomeFunction(a,\n" 1094 " b, // comment\n" 1095 " c);", 1096 format("SomeFunction(a,\n" 1097 " b, // comment\n" 1098 " c);")); 1099 EXPECT_EQ("SomeFunction(a, b,\n" 1100 " // comment\n" 1101 " c);", 1102 format("SomeFunction(a,\n" 1103 " b,\n" 1104 " // comment\n" 1105 " c);")); 1106 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1107 " c);", 1108 format("SomeFunction(a, b, // comment (unclear relation)\n" 1109 " c);")); 1110 EXPECT_EQ("SomeFunction(a, // comment\n" 1111 " b,\n" 1112 " c); // comment", 1113 format("SomeFunction(a, // comment\n" 1114 " b,\n" 1115 " c); // comment")); 1116 } 1117 1118 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1119 EXPECT_EQ("// comment", format("// comment ")); 1120 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1121 format("int aaaaaaa, bbbbbbb; // comment ", 1122 getLLVMStyleWithColumns(33))); 1123 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1124 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1125 } 1126 1127 TEST_F(FormatTest, UnderstandsBlockComments) { 1128 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1129 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 1130 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1131 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1132 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1133 "/* Trailing comment for aa... */\n" 1134 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1135 EXPECT_EQ( 1136 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1137 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1138 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1139 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1140 EXPECT_EQ( 1141 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1142 " aaaaaaaaaaaaaaaaaa,\n" 1143 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1144 "}", 1145 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1146 " aaaaaaaaaaaaaaaaaa ,\n" 1147 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1148 "}")); 1149 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 1150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1151 1152 FormatStyle NoBinPacking = getLLVMStyle(); 1153 NoBinPacking.BinPackParameters = false; 1154 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1155 " /* parameter 2 */ aaaaaa,\n" 1156 " /* parameter 3 */ aaaaaa,\n" 1157 " /* parameter 4 */ aaaaaa);", 1158 NoBinPacking); 1159 1160 // Aligning block comments in macros. 1161 verifyGoogleFormat("#define A \\\n" 1162 " int i; /*a*/ \\\n" 1163 " int jjj; /*b*/"); 1164 } 1165 1166 TEST_F(FormatTest, AlignsBlockComments) { 1167 EXPECT_EQ("/*\n" 1168 " * Really multi-line\n" 1169 " * comment.\n" 1170 " */\n" 1171 "void f() {}", 1172 format(" /*\n" 1173 " * Really multi-line\n" 1174 " * comment.\n" 1175 " */\n" 1176 " void f() {}")); 1177 EXPECT_EQ("class C {\n" 1178 " /*\n" 1179 " * Another multi-line\n" 1180 " * comment.\n" 1181 " */\n" 1182 " void f() {}\n" 1183 "};", 1184 format("class C {\n" 1185 "/*\n" 1186 " * Another multi-line\n" 1187 " * comment.\n" 1188 " */\n" 1189 "void f() {}\n" 1190 "};")); 1191 EXPECT_EQ("/*\n" 1192 " 1. This is a comment with non-trivial formatting.\n" 1193 " 1.1. We have to indent/outdent all lines equally\n" 1194 " 1.1.1. to keep the formatting.\n" 1195 " */", 1196 format(" /*\n" 1197 " 1. This is a comment with non-trivial formatting.\n" 1198 " 1.1. We have to indent/outdent all lines equally\n" 1199 " 1.1.1. to keep the formatting.\n" 1200 " */")); 1201 EXPECT_EQ("/*\n" 1202 "Don't try to outdent if there's not enough indentation.\n" 1203 "*/", 1204 format(" /*\n" 1205 " Don't try to outdent if there's not enough indentation.\n" 1206 " */")); 1207 1208 EXPECT_EQ("int i; /* Comment with empty...\n" 1209 " *\n" 1210 " * line. */", 1211 format("int i; /* Comment with empty...\n" 1212 " *\n" 1213 " * line. */")); 1214 EXPECT_EQ("int foobar = 0; /* comment */\n" 1215 "int bar = 0; /* multiline\n" 1216 " comment 1 */\n" 1217 "int baz = 0; /* multiline\n" 1218 " comment 2 */\n" 1219 "int bzz = 0; /* multiline\n" 1220 " comment 3 */", 1221 format("int foobar = 0; /* comment */\n" 1222 "int bar = 0; /* multiline\n" 1223 " comment 1 */\n" 1224 "int baz = 0; /* multiline\n" 1225 " comment 2 */\n" 1226 "int bzz = 0; /* multiline\n" 1227 " comment 3 */")); 1228 EXPECT_EQ("int foobar = 0; /* comment */\n" 1229 "int bar = 0; /* multiline\n" 1230 " comment */\n" 1231 "int baz = 0; /* multiline\n" 1232 "comment */", 1233 format("int foobar = 0; /* comment */\n" 1234 "int bar = 0; /* multiline\n" 1235 "comment */\n" 1236 "int baz = 0; /* multiline\n" 1237 "comment */")); 1238 } 1239 1240 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1241 FormatStyle Style = getLLVMStyleWithColumns(20); 1242 Style.ReflowComments = false; 1243 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1244 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1245 } 1246 1247 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1248 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1250 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1252 EXPECT_EQ( 1253 "void ffffffffffff(\n" 1254 " int aaaaaaaa, int bbbbbbbb,\n" 1255 " int cccccccccccc) { /*\n" 1256 " aaaaaaaaaa\n" 1257 " aaaaaaaaaaaaa\n" 1258 " bbbbbbbbbbbbbb\n" 1259 " bbbbbbbbbb\n" 1260 " */\n" 1261 "}", 1262 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1263 "{ /*\n" 1264 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1265 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1266 " */\n" 1267 "}", 1268 getLLVMStyleWithColumns(40))); 1269 } 1270 1271 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1272 EXPECT_EQ("void ffffffffff(\n" 1273 " int aaaaa /* test */);", 1274 format("void ffffffffff(int aaaaa /* test */);", 1275 getLLVMStyleWithColumns(35))); 1276 } 1277 1278 TEST_F(FormatTest, SplitsLongCxxComments) { 1279 EXPECT_EQ("// A comment that\n" 1280 "// doesn't fit on\n" 1281 "// one line", 1282 format("// A comment that doesn't fit on one line", 1283 getLLVMStyleWithColumns(20))); 1284 EXPECT_EQ("/// A comment that\n" 1285 "/// doesn't fit on\n" 1286 "/// one line", 1287 format("/// A comment that doesn't fit on one line", 1288 getLLVMStyleWithColumns(20))); 1289 EXPECT_EQ("//! A comment that\n" 1290 "//! doesn't fit on\n" 1291 "//! one line", 1292 format("//! A comment that doesn't fit on one line", 1293 getLLVMStyleWithColumns(20))); 1294 EXPECT_EQ("// a b c d\n" 1295 "// e f g\n" 1296 "// h i j k", 1297 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1298 EXPECT_EQ( 1299 "// a b c d\n" 1300 "// e f g\n" 1301 "// h i j k", 1302 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1303 EXPECT_EQ("if (true) // A comment that\n" 1304 " // doesn't fit on\n" 1305 " // one line", 1306 format("if (true) // A comment that doesn't fit on one line ", 1307 getLLVMStyleWithColumns(30))); 1308 EXPECT_EQ("// Don't_touch_leading_whitespace", 1309 format("// Don't_touch_leading_whitespace", 1310 getLLVMStyleWithColumns(20))); 1311 EXPECT_EQ("// Add leading\n" 1312 "// whitespace", 1313 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1314 EXPECT_EQ("/// Add leading\n" 1315 "/// whitespace", 1316 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1317 EXPECT_EQ("//! Add leading\n" 1318 "//! whitespace", 1319 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1320 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1321 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1322 "// limit", 1323 format("//Even if it makes the line exceed the column limit", 1324 getLLVMStyleWithColumns(51))); 1325 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1326 1327 EXPECT_EQ("// aa bb cc dd", 1328 format("// aa bb cc dd ", 1329 getLLVMStyleWithColumns(15))); 1330 1331 EXPECT_EQ("// A comment before\n" 1332 "// a macro\n" 1333 "// definition\n" 1334 "#define a b", 1335 format("// A comment before a macro definition\n" 1336 "#define a b", 1337 getLLVMStyleWithColumns(20))); 1338 EXPECT_EQ("void ffffff(\n" 1339 " int aaaaaaaaa, // wwww\n" 1340 " int bbbbbbbbbb, // xxxxxxx\n" 1341 " // yyyyyyyyyy\n" 1342 " int c, int d, int e) {}", 1343 format("void ffffff(\n" 1344 " int aaaaaaaaa, // wwww\n" 1345 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1346 " int c, int d, int e) {}", 1347 getLLVMStyleWithColumns(40))); 1348 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1349 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1350 getLLVMStyleWithColumns(20))); 1351 EXPECT_EQ( 1352 "#define XXX // a b c d\n" 1353 " // e f g h", 1354 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1355 EXPECT_EQ( 1356 "#define XXX // q w e r\n" 1357 " // t y u i", 1358 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1359 } 1360 1361 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1362 EXPECT_EQ("// A comment\n" 1363 "// that doesn't\n" 1364 "// fit on one\n" 1365 "// line", 1366 format("// A comment that doesn't fit on one line", 1367 getLLVMStyleWithColumns(20))); 1368 EXPECT_EQ("/// A comment\n" 1369 "/// that doesn't\n" 1370 "/// fit on one\n" 1371 "/// line", 1372 format("/// A comment that doesn't fit on one line", 1373 getLLVMStyleWithColumns(20))); 1374 } 1375 1376 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1377 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1378 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1379 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1380 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1381 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1382 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1383 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1384 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1385 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1386 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1387 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1388 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1389 getLLVMStyleWithColumns(50))); 1390 // FIXME: One day we might want to implement adjustment of leading whitespace 1391 // of the consecutive lines in this kind of comment: 1392 EXPECT_EQ("double\n" 1393 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1394 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1395 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1396 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1397 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1398 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1399 getLLVMStyleWithColumns(49))); 1400 } 1401 1402 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1403 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1404 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1405 EXPECT_EQ( 1406 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1407 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1408 EXPECT_EQ( 1409 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1410 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1411 } 1412 1413 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1414 EXPECT_EQ("if (xxx ==\n" 1415 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1416 " zzz)\n" 1417 " q();", 1418 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1419 " zzz) q();", 1420 getLLVMStyleWithColumns(40))); 1421 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1422 " yyy && // aaaaaa bbbbbbbb cccc\n" 1423 " zzz)\n" 1424 " q();", 1425 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1426 " zzz) q();", 1427 getLLVMStyleWithColumns(40))); 1428 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1429 " yyy || // aaaaaa bbbbbbbb cccc\n" 1430 " zzz)\n" 1431 " q();", 1432 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1433 " zzz) q();", 1434 getLLVMStyleWithColumns(40))); 1435 EXPECT_EQ("fffffffff(\n" 1436 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1437 " zzz);", 1438 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1439 " zzz);", 1440 getLLVMStyleWithColumns(40))); 1441 } 1442 1443 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1444 EXPECT_EQ("#define A(x) /* \\\n" 1445 " a comment \\\n" 1446 " inside */ \\\n" 1447 " f();", 1448 format("#define A(x) /* \\\n" 1449 " a comment \\\n" 1450 " inside */ \\\n" 1451 " f();", 1452 getLLVMStyleWithColumns(17))); 1453 EXPECT_EQ("#define A( \\\n" 1454 " x) /* \\\n" 1455 " a comment \\\n" 1456 " inside */ \\\n" 1457 " f();", 1458 format("#define A( \\\n" 1459 " x) /* \\\n" 1460 " a comment \\\n" 1461 " inside */ \\\n" 1462 " f();", 1463 getLLVMStyleWithColumns(17))); 1464 } 1465 1466 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1467 EXPECT_EQ("namespace {}\n// Test\n#define A", 1468 format("namespace {}\n // Test\n#define A")); 1469 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1470 format("namespace {}\n /* Test */\n#define A")); 1471 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1472 format("namespace {}\n /* Test */ #define A")); 1473 } 1474 1475 TEST_F(FormatTest, SplitsLongLinesInComments) { 1476 EXPECT_EQ("/* This is a long\n" 1477 " * comment that\n" 1478 " * doesn't\n" 1479 " * fit on one line.\n" 1480 " */", 1481 format("/* " 1482 "This is a long " 1483 "comment that " 1484 "doesn't " 1485 "fit on one line. */", 1486 getLLVMStyleWithColumns(20))); 1487 EXPECT_EQ( 1488 "/* a b c d\n" 1489 " * e f g\n" 1490 " * h i j k\n" 1491 " */", 1492 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1493 EXPECT_EQ( 1494 "/* a b c d\n" 1495 " * e f g\n" 1496 " * h i j k\n" 1497 " */", 1498 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1499 EXPECT_EQ("/*\n" 1500 "This is a long\n" 1501 "comment that doesn't\n" 1502 "fit on one line.\n" 1503 "*/", 1504 format("/*\n" 1505 "This is a long " 1506 "comment that doesn't " 1507 "fit on one line. \n" 1508 "*/", 1509 getLLVMStyleWithColumns(20))); 1510 EXPECT_EQ("/*\n" 1511 " * This is a long\n" 1512 " * comment that\n" 1513 " * doesn't fit on\n" 1514 " * one line.\n" 1515 " */", 1516 format("/* \n" 1517 " * This is a long " 1518 " comment that " 1519 " doesn't fit on " 1520 " one line. \n" 1521 " */", 1522 getLLVMStyleWithColumns(20))); 1523 EXPECT_EQ("/*\n" 1524 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1525 " * so_it_should_be_broken\n" 1526 " * wherever_a_space_occurs\n" 1527 " */", 1528 format("/*\n" 1529 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1530 " so_it_should_be_broken " 1531 " wherever_a_space_occurs \n" 1532 " */", 1533 getLLVMStyleWithColumns(20))); 1534 EXPECT_EQ("/*\n" 1535 " * This_comment_can_not_be_broken_into_lines\n" 1536 " */", 1537 format("/*\n" 1538 " * This_comment_can_not_be_broken_into_lines\n" 1539 " */", 1540 getLLVMStyleWithColumns(20))); 1541 EXPECT_EQ("{\n" 1542 " /*\n" 1543 " This is another\n" 1544 " long comment that\n" 1545 " doesn't fit on one\n" 1546 " line 1234567890\n" 1547 " */\n" 1548 "}", 1549 format("{\n" 1550 "/*\n" 1551 "This is another " 1552 " long comment that " 1553 " doesn't fit on one" 1554 " line 1234567890\n" 1555 "*/\n" 1556 "}", 1557 getLLVMStyleWithColumns(20))); 1558 EXPECT_EQ("{\n" 1559 " /*\n" 1560 " * This i s\n" 1561 " * another comment\n" 1562 " * t hat doesn' t\n" 1563 " * fit on one l i\n" 1564 " * n e\n" 1565 " */\n" 1566 "}", 1567 format("{\n" 1568 "/*\n" 1569 " * This i s" 1570 " another comment" 1571 " t hat doesn' t" 1572 " fit on one l i" 1573 " n e\n" 1574 " */\n" 1575 "}", 1576 getLLVMStyleWithColumns(20))); 1577 EXPECT_EQ("/*\n" 1578 " * This is a long\n" 1579 " * comment that\n" 1580 " * doesn't fit on\n" 1581 " * one line\n" 1582 " */", 1583 format(" /*\n" 1584 " * This is a long comment that doesn't fit on one line\n" 1585 " */", 1586 getLLVMStyleWithColumns(20))); 1587 EXPECT_EQ("{\n" 1588 " if (something) /* This is a\n" 1589 " long\n" 1590 " comment */\n" 1591 " ;\n" 1592 "}", 1593 format("{\n" 1594 " if (something) /* This is a long comment */\n" 1595 " ;\n" 1596 "}", 1597 getLLVMStyleWithColumns(30))); 1598 1599 EXPECT_EQ("/* A comment before\n" 1600 " * a macro\n" 1601 " * definition */\n" 1602 "#define a b", 1603 format("/* A comment before a macro definition */\n" 1604 "#define a b", 1605 getLLVMStyleWithColumns(20))); 1606 1607 EXPECT_EQ("/* some comment\n" 1608 " * a comment\n" 1609 "* that we break\n" 1610 " * another comment\n" 1611 "* we have to break\n" 1612 "* a left comment\n" 1613 " */", 1614 format(" /* some comment\n" 1615 " * a comment that we break\n" 1616 " * another comment we have to break\n" 1617 "* a left comment\n" 1618 " */", 1619 getLLVMStyleWithColumns(20))); 1620 1621 EXPECT_EQ("/**\n" 1622 " * multiline block\n" 1623 " * comment\n" 1624 " *\n" 1625 " */", 1626 format("/**\n" 1627 " * multiline block comment\n" 1628 " *\n" 1629 " */", 1630 getLLVMStyleWithColumns(20))); 1631 1632 EXPECT_EQ("/*\n" 1633 "\n" 1634 "\n" 1635 " */\n", 1636 format(" /* \n" 1637 " \n" 1638 " \n" 1639 " */\n")); 1640 1641 EXPECT_EQ("/* a a */", 1642 format("/* a a */", getLLVMStyleWithColumns(15))); 1643 EXPECT_EQ("/* a a bc */", 1644 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1645 EXPECT_EQ("/* aaa aaa\n" 1646 " * aaaaa */", 1647 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1648 EXPECT_EQ("/* aaa aaa\n" 1649 " * aaaaa */", 1650 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1651 } 1652 1653 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1654 EXPECT_EQ("#define X \\\n" 1655 " /* \\\n" 1656 " Test \\\n" 1657 " Macro comment \\\n" 1658 " with a long \\\n" 1659 " line \\\n" 1660 " */ \\\n" 1661 " A + B", 1662 format("#define X \\\n" 1663 " /*\n" 1664 " Test\n" 1665 " Macro comment with a long line\n" 1666 " */ \\\n" 1667 " A + B", 1668 getLLVMStyleWithColumns(20))); 1669 EXPECT_EQ("#define X \\\n" 1670 " /* Macro comment \\\n" 1671 " with a long \\\n" 1672 " line */ \\\n" 1673 " A + B", 1674 format("#define X \\\n" 1675 " /* Macro comment with a long\n" 1676 " line */ \\\n" 1677 " A + B", 1678 getLLVMStyleWithColumns(20))); 1679 EXPECT_EQ("#define X \\\n" 1680 " /* Macro comment \\\n" 1681 " * with a long \\\n" 1682 " * line */ \\\n" 1683 " A + B", 1684 format("#define X \\\n" 1685 " /* Macro comment with a long line */ \\\n" 1686 " A + B", 1687 getLLVMStyleWithColumns(20))); 1688 } 1689 1690 TEST_F(FormatTest, CommentsInStaticInitializers) { 1691 EXPECT_EQ( 1692 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1693 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1694 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1695 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1696 " aaaaaaaaaaaaaaaaaaaa};", 1697 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1698 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1699 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1700 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1701 " aaaaaaaaaaaaaaaaaaaa };")); 1702 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1703 " bbbbbbbbbbb, ccccccccccc};"); 1704 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1705 " // comment for bb....\n" 1706 " bbbbbbbbbbb, ccccccccccc};"); 1707 verifyGoogleFormat( 1708 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1709 " bbbbbbbbbbb, ccccccccccc};"); 1710 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1711 " // comment for bb....\n" 1712 " bbbbbbbbbbb, ccccccccccc};"); 1713 1714 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1715 " {d, e, f}, // Group #2\n" 1716 " {g, h, i}}; // Group #3"); 1717 verifyFormat("S s = {{// Group #1\n" 1718 " a, b, c},\n" 1719 " {// Group #2\n" 1720 " d, e, f},\n" 1721 " {// Group #3\n" 1722 " g, h, i}};"); 1723 1724 EXPECT_EQ("S s = {\n" 1725 " // Some comment\n" 1726 " a,\n" 1727 "\n" 1728 " // Comment after empty line\n" 1729 " b}", 1730 format("S s = {\n" 1731 " // Some comment\n" 1732 " a,\n" 1733 " \n" 1734 " // Comment after empty line\n" 1735 " b\n" 1736 "}")); 1737 EXPECT_EQ("S s = {\n" 1738 " /* Some comment */\n" 1739 " a,\n" 1740 "\n" 1741 " /* Comment after empty line */\n" 1742 " b}", 1743 format("S s = {\n" 1744 " /* Some comment */\n" 1745 " a,\n" 1746 " \n" 1747 " /* Comment after empty line */\n" 1748 " b\n" 1749 "}")); 1750 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1751 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1752 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1753 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1754 } 1755 1756 TEST_F(FormatTest, IgnoresIf0Contents) { 1757 EXPECT_EQ("#if 0\n" 1758 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1759 "#endif\n" 1760 "void f() {}", 1761 format("#if 0\n" 1762 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1763 "#endif\n" 1764 "void f( ) { }")); 1765 EXPECT_EQ("#if false\n" 1766 "void f( ) { }\n" 1767 "#endif\n" 1768 "void g() {}\n", 1769 format("#if false\n" 1770 "void f( ) { }\n" 1771 "#endif\n" 1772 "void g( ) { }\n")); 1773 EXPECT_EQ("enum E {\n" 1774 " One,\n" 1775 " Two,\n" 1776 "#if 0\n" 1777 "Three,\n" 1778 " Four,\n" 1779 "#endif\n" 1780 " Five\n" 1781 "};", 1782 format("enum E {\n" 1783 " One,Two,\n" 1784 "#if 0\n" 1785 "Three,\n" 1786 " Four,\n" 1787 "#endif\n" 1788 " Five};")); 1789 EXPECT_EQ("enum F {\n" 1790 " One,\n" 1791 "#if 1\n" 1792 " Two,\n" 1793 "#if 0\n" 1794 "Three,\n" 1795 " Four,\n" 1796 "#endif\n" 1797 " Five\n" 1798 "#endif\n" 1799 "};", 1800 format("enum F {\n" 1801 "One,\n" 1802 "#if 1\n" 1803 "Two,\n" 1804 "#if 0\n" 1805 "Three,\n" 1806 " Four,\n" 1807 "#endif\n" 1808 "Five\n" 1809 "#endif\n" 1810 "};")); 1811 EXPECT_EQ("enum G {\n" 1812 " One,\n" 1813 "#if 0\n" 1814 "Two,\n" 1815 "#else\n" 1816 " Three,\n" 1817 "#endif\n" 1818 " Four\n" 1819 "};", 1820 format("enum G {\n" 1821 "One,\n" 1822 "#if 0\n" 1823 "Two,\n" 1824 "#else\n" 1825 "Three,\n" 1826 "#endif\n" 1827 "Four\n" 1828 "};")); 1829 EXPECT_EQ("enum H {\n" 1830 " One,\n" 1831 "#if 0\n" 1832 "#ifdef Q\n" 1833 "Two,\n" 1834 "#else\n" 1835 "Three,\n" 1836 "#endif\n" 1837 "#endif\n" 1838 " Four\n" 1839 "};", 1840 format("enum H {\n" 1841 "One,\n" 1842 "#if 0\n" 1843 "#ifdef Q\n" 1844 "Two,\n" 1845 "#else\n" 1846 "Three,\n" 1847 "#endif\n" 1848 "#endif\n" 1849 "Four\n" 1850 "};")); 1851 EXPECT_EQ("enum I {\n" 1852 " One,\n" 1853 "#if /* test */ 0 || 1\n" 1854 "Two,\n" 1855 "Three,\n" 1856 "#endif\n" 1857 " Four\n" 1858 "};", 1859 format("enum I {\n" 1860 "One,\n" 1861 "#if /* test */ 0 || 1\n" 1862 "Two,\n" 1863 "Three,\n" 1864 "#endif\n" 1865 "Four\n" 1866 "};")); 1867 EXPECT_EQ("enum J {\n" 1868 " One,\n" 1869 "#if 0\n" 1870 "#if 0\n" 1871 "Two,\n" 1872 "#else\n" 1873 "Three,\n" 1874 "#endif\n" 1875 "Four,\n" 1876 "#endif\n" 1877 " Five\n" 1878 "};", 1879 format("enum J {\n" 1880 "One,\n" 1881 "#if 0\n" 1882 "#if 0\n" 1883 "Two,\n" 1884 "#else\n" 1885 "Three,\n" 1886 "#endif\n" 1887 "Four,\n" 1888 "#endif\n" 1889 "Five\n" 1890 "};")); 1891 } 1892 1893 //===----------------------------------------------------------------------===// 1894 // Tests for classes, namespaces, etc. 1895 //===----------------------------------------------------------------------===// 1896 1897 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1898 verifyFormat("class A {};"); 1899 } 1900 1901 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1902 verifyFormat("class A {\n" 1903 "public:\n" 1904 "public: // comment\n" 1905 "protected:\n" 1906 "private:\n" 1907 " void f() {}\n" 1908 "};"); 1909 verifyGoogleFormat("class A {\n" 1910 " public:\n" 1911 " protected:\n" 1912 " private:\n" 1913 " void f() {}\n" 1914 "};"); 1915 verifyFormat("class A {\n" 1916 "public slots:\n" 1917 " void f1() {}\n" 1918 "public Q_SLOTS:\n" 1919 " void f2() {}\n" 1920 "protected slots:\n" 1921 " void f3() {}\n" 1922 "protected Q_SLOTS:\n" 1923 " void f4() {}\n" 1924 "private slots:\n" 1925 " void f5() {}\n" 1926 "private Q_SLOTS:\n" 1927 " void f6() {}\n" 1928 "signals:\n" 1929 " void g1();\n" 1930 "Q_SIGNALS:\n" 1931 " void g2();\n" 1932 "};"); 1933 1934 // Don't interpret 'signals' the wrong way. 1935 verifyFormat("signals.set();"); 1936 verifyFormat("for (Signals signals : f()) {\n}"); 1937 verifyFormat("{\n" 1938 " signals.set(); // This needs indentation.\n" 1939 "}"); 1940 verifyFormat("void f() {\n" 1941 "label:\n" 1942 " signals.baz();\n" 1943 "}"); 1944 } 1945 1946 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1947 EXPECT_EQ("class A {\n" 1948 "public:\n" 1949 " void f();\n" 1950 "\n" 1951 "private:\n" 1952 " void g() {}\n" 1953 " // test\n" 1954 "protected:\n" 1955 " int h;\n" 1956 "};", 1957 format("class A {\n" 1958 "public:\n" 1959 "void f();\n" 1960 "private:\n" 1961 "void g() {}\n" 1962 "// test\n" 1963 "protected:\n" 1964 "int h;\n" 1965 "};")); 1966 EXPECT_EQ("class A {\n" 1967 "protected:\n" 1968 "public:\n" 1969 " void f();\n" 1970 "};", 1971 format("class A {\n" 1972 "protected:\n" 1973 "\n" 1974 "public:\n" 1975 "\n" 1976 " void f();\n" 1977 "};")); 1978 1979 // Even ensure proper spacing inside macros. 1980 EXPECT_EQ("#define B \\\n" 1981 " class A { \\\n" 1982 " protected: \\\n" 1983 " public: \\\n" 1984 " void f(); \\\n" 1985 " };", 1986 format("#define B \\\n" 1987 " class A { \\\n" 1988 " protected: \\\n" 1989 " \\\n" 1990 " public: \\\n" 1991 " \\\n" 1992 " void f(); \\\n" 1993 " };", 1994 getGoogleStyle())); 1995 // But don't remove empty lines after macros ending in access specifiers. 1996 EXPECT_EQ("#define A private:\n" 1997 "\n" 1998 "int i;", 1999 format("#define A private:\n" 2000 "\n" 2001 "int i;")); 2002 } 2003 2004 TEST_F(FormatTest, FormatsClasses) { 2005 verifyFormat("class A : public B {};"); 2006 verifyFormat("class A : public ::B {};"); 2007 2008 verifyFormat( 2009 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2010 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2011 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2012 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2013 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2014 verifyFormat( 2015 "class A : public B, public C, public D, public E, public F {};"); 2016 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2017 " public C,\n" 2018 " public D,\n" 2019 " public E,\n" 2020 " public F,\n" 2021 " public G {};"); 2022 2023 verifyFormat("class\n" 2024 " ReallyReallyLongClassName {\n" 2025 " int i;\n" 2026 "};", 2027 getLLVMStyleWithColumns(32)); 2028 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2029 " aaaaaaaaaaaaaaaa> {};"); 2030 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2031 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2032 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2033 verifyFormat("template <class R, class C>\n" 2034 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2035 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2036 verifyFormat("class ::A::B {};"); 2037 } 2038 2039 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2040 verifyFormat("class A {\n} a, b;"); 2041 verifyFormat("struct A {\n} a, b;"); 2042 verifyFormat("union A {\n} a;"); 2043 } 2044 2045 TEST_F(FormatTest, FormatsEnum) { 2046 verifyFormat("enum {\n" 2047 " Zero,\n" 2048 " One = 1,\n" 2049 " Two = One + 1,\n" 2050 " Three = (One + Two),\n" 2051 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2052 " Five = (One, Two, Three, Four, 5)\n" 2053 "};"); 2054 verifyGoogleFormat("enum {\n" 2055 " Zero,\n" 2056 " One = 1,\n" 2057 " Two = One + 1,\n" 2058 " Three = (One + Two),\n" 2059 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2060 " Five = (One, Two, Three, Four, 5)\n" 2061 "};"); 2062 verifyFormat("enum Enum {};"); 2063 verifyFormat("enum {};"); 2064 verifyFormat("enum X E {} d;"); 2065 verifyFormat("enum __attribute__((...)) E {} d;"); 2066 verifyFormat("enum __declspec__((...)) E {} d;"); 2067 verifyFormat("enum {\n" 2068 " Bar = Foo<int, int>::value\n" 2069 "};", 2070 getLLVMStyleWithColumns(30)); 2071 2072 verifyFormat("enum ShortEnum { A, B, C };"); 2073 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2074 2075 EXPECT_EQ("enum KeepEmptyLines {\n" 2076 " ONE,\n" 2077 "\n" 2078 " TWO,\n" 2079 "\n" 2080 " THREE\n" 2081 "}", 2082 format("enum KeepEmptyLines {\n" 2083 " ONE,\n" 2084 "\n" 2085 " TWO,\n" 2086 "\n" 2087 "\n" 2088 " THREE\n" 2089 "}")); 2090 verifyFormat("enum E { // comment\n" 2091 " ONE,\n" 2092 " TWO\n" 2093 "};\n" 2094 "int i;"); 2095 // Not enums. 2096 verifyFormat("enum X f() {\n" 2097 " a();\n" 2098 " return 42;\n" 2099 "}"); 2100 verifyFormat("enum X Type::f() {\n" 2101 " a();\n" 2102 " return 42;\n" 2103 "}"); 2104 verifyFormat("enum ::X f() {\n" 2105 " a();\n" 2106 " return 42;\n" 2107 "}"); 2108 verifyFormat("enum ns::X f() {\n" 2109 " a();\n" 2110 " return 42;\n" 2111 "}"); 2112 } 2113 2114 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2115 verifyFormat("enum Type {\n" 2116 " One = 0; // These semicolons should be commas.\n" 2117 " Two = 1;\n" 2118 "};"); 2119 verifyFormat("namespace n {\n" 2120 "enum Type {\n" 2121 " One,\n" 2122 " Two, // missing };\n" 2123 " int i;\n" 2124 "}\n" 2125 "void g() {}"); 2126 } 2127 2128 TEST_F(FormatTest, FormatsEnumStruct) { 2129 verifyFormat("enum struct {\n" 2130 " Zero,\n" 2131 " One = 1,\n" 2132 " Two = One + 1,\n" 2133 " Three = (One + Two),\n" 2134 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2135 " Five = (One, Two, Three, Four, 5)\n" 2136 "};"); 2137 verifyFormat("enum struct Enum {};"); 2138 verifyFormat("enum struct {};"); 2139 verifyFormat("enum struct X E {} d;"); 2140 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2141 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2142 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2143 } 2144 2145 TEST_F(FormatTest, FormatsEnumClass) { 2146 verifyFormat("enum class {\n" 2147 " Zero,\n" 2148 " One = 1,\n" 2149 " Two = One + 1,\n" 2150 " Three = (One + Two),\n" 2151 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2152 " Five = (One, Two, Three, Four, 5)\n" 2153 "};"); 2154 verifyFormat("enum class Enum {};"); 2155 verifyFormat("enum class {};"); 2156 verifyFormat("enum class X E {} d;"); 2157 verifyFormat("enum class __attribute__((...)) E {} d;"); 2158 verifyFormat("enum class __declspec__((...)) E {} d;"); 2159 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2160 } 2161 2162 TEST_F(FormatTest, FormatsEnumTypes) { 2163 verifyFormat("enum X : int {\n" 2164 " A, // Force multiple lines.\n" 2165 " B\n" 2166 "};"); 2167 verifyFormat("enum X : int { A, B };"); 2168 verifyFormat("enum X : std::uint32_t { A, B };"); 2169 } 2170 2171 TEST_F(FormatTest, FormatsNSEnums) { 2172 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2173 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2174 " // Information about someDecentlyLongValue.\n" 2175 " someDecentlyLongValue,\n" 2176 " // Information about anotherDecentlyLongValue.\n" 2177 " anotherDecentlyLongValue,\n" 2178 " // Information about aThirdDecentlyLongValue.\n" 2179 " aThirdDecentlyLongValue\n" 2180 "};"); 2181 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2182 " a = 1,\n" 2183 " b = 2,\n" 2184 " c = 3,\n" 2185 "};"); 2186 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2187 " a = 1,\n" 2188 " b = 2,\n" 2189 " c = 3,\n" 2190 "};"); 2191 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2192 " a = 1,\n" 2193 " b = 2,\n" 2194 " c = 3,\n" 2195 "};"); 2196 } 2197 2198 TEST_F(FormatTest, FormatsBitfields) { 2199 verifyFormat("struct Bitfields {\n" 2200 " unsigned sClass : 8;\n" 2201 " unsigned ValueKind : 2;\n" 2202 "};"); 2203 verifyFormat("struct A {\n" 2204 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2205 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2206 "};"); 2207 verifyFormat("struct MyStruct {\n" 2208 " uchar data;\n" 2209 " uchar : 8;\n" 2210 " uchar : 8;\n" 2211 " uchar other;\n" 2212 "};"); 2213 } 2214 2215 TEST_F(FormatTest, FormatsNamespaces) { 2216 verifyFormat("namespace some_namespace {\n" 2217 "class A {};\n" 2218 "void f() { f(); }\n" 2219 "}"); 2220 verifyFormat("namespace {\n" 2221 "class A {};\n" 2222 "void f() { f(); }\n" 2223 "}"); 2224 verifyFormat("inline namespace X {\n" 2225 "class A {};\n" 2226 "void f() { f(); }\n" 2227 "}"); 2228 verifyFormat("using namespace some_namespace;\n" 2229 "class A {};\n" 2230 "void f() { f(); }"); 2231 2232 // This code is more common than we thought; if we 2233 // layout this correctly the semicolon will go into 2234 // its own line, which is undesirable. 2235 verifyFormat("namespace {};"); 2236 verifyFormat("namespace {\n" 2237 "class A {};\n" 2238 "};"); 2239 2240 verifyFormat("namespace {\n" 2241 "int SomeVariable = 0; // comment\n" 2242 "} // namespace"); 2243 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2244 "#define HEADER_GUARD\n" 2245 "namespace my_namespace {\n" 2246 "int i;\n" 2247 "} // my_namespace\n" 2248 "#endif // HEADER_GUARD", 2249 format("#ifndef HEADER_GUARD\n" 2250 " #define HEADER_GUARD\n" 2251 " namespace my_namespace {\n" 2252 "int i;\n" 2253 "} // my_namespace\n" 2254 "#endif // HEADER_GUARD")); 2255 2256 EXPECT_EQ("namespace A::B {\n" 2257 "class C {};\n" 2258 "}", 2259 format("namespace A::B {\n" 2260 "class C {};\n" 2261 "}")); 2262 2263 FormatStyle Style = getLLVMStyle(); 2264 Style.NamespaceIndentation = FormatStyle::NI_All; 2265 EXPECT_EQ("namespace out {\n" 2266 " int i;\n" 2267 " namespace in {\n" 2268 " int i;\n" 2269 " } // namespace\n" 2270 "} // namespace", 2271 format("namespace out {\n" 2272 "int i;\n" 2273 "namespace in {\n" 2274 "int i;\n" 2275 "} // namespace\n" 2276 "} // namespace", 2277 Style)); 2278 2279 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2280 EXPECT_EQ("namespace out {\n" 2281 "int i;\n" 2282 "namespace in {\n" 2283 " int i;\n" 2284 "} // namespace\n" 2285 "} // namespace", 2286 format("namespace out {\n" 2287 "int i;\n" 2288 "namespace in {\n" 2289 "int i;\n" 2290 "} // namespace\n" 2291 "} // namespace", 2292 Style)); 2293 } 2294 2295 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2296 2297 TEST_F(FormatTest, FormatsInlineASM) { 2298 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2299 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2300 verifyFormat( 2301 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2302 " \"cpuid\\n\\t\"\n" 2303 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2304 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2305 " : \"a\"(value));"); 2306 EXPECT_EQ( 2307 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2308 " __asm {\n" 2309 " mov edx,[that] // vtable in edx\n" 2310 " mov eax,methodIndex\n" 2311 " call [edx][eax*4] // stdcall\n" 2312 " }\n" 2313 "}", 2314 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2315 " __asm {\n" 2316 " mov edx,[that] // vtable in edx\n" 2317 " mov eax,methodIndex\n" 2318 " call [edx][eax*4] // stdcall\n" 2319 " }\n" 2320 "}")); 2321 EXPECT_EQ("_asm {\n" 2322 " xor eax, eax;\n" 2323 " cpuid;\n" 2324 "}", 2325 format("_asm {\n" 2326 " xor eax, eax;\n" 2327 " cpuid;\n" 2328 "}")); 2329 verifyFormat("void function() {\n" 2330 " // comment\n" 2331 " asm(\"\");\n" 2332 "}"); 2333 EXPECT_EQ("__asm {\n" 2334 "}\n" 2335 "int i;", 2336 format("__asm {\n" 2337 "}\n" 2338 "int i;")); 2339 } 2340 2341 TEST_F(FormatTest, FormatTryCatch) { 2342 verifyFormat("try {\n" 2343 " throw a * b;\n" 2344 "} catch (int a) {\n" 2345 " // Do nothing.\n" 2346 "} catch (...) {\n" 2347 " exit(42);\n" 2348 "}"); 2349 2350 // Function-level try statements. 2351 verifyFormat("int f() try { return 4; } catch (...) {\n" 2352 " return 5;\n" 2353 "}"); 2354 verifyFormat("class A {\n" 2355 " int a;\n" 2356 " A() try : a(0) {\n" 2357 " } catch (...) {\n" 2358 " throw;\n" 2359 " }\n" 2360 "};\n"); 2361 2362 // Incomplete try-catch blocks. 2363 verifyIncompleteFormat("try {} catch ("); 2364 } 2365 2366 TEST_F(FormatTest, FormatSEHTryCatch) { 2367 verifyFormat("__try {\n" 2368 " int a = b * c;\n" 2369 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2370 " // Do nothing.\n" 2371 "}"); 2372 2373 verifyFormat("__try {\n" 2374 " int a = b * c;\n" 2375 "} __finally {\n" 2376 " // Do nothing.\n" 2377 "}"); 2378 2379 verifyFormat("DEBUG({\n" 2380 " __try {\n" 2381 " } __finally {\n" 2382 " }\n" 2383 "});\n"); 2384 } 2385 2386 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2387 verifyFormat("try {\n" 2388 " f();\n" 2389 "} catch {\n" 2390 " g();\n" 2391 "}"); 2392 verifyFormat("try {\n" 2393 " f();\n" 2394 "} catch (A a) MACRO(x) {\n" 2395 " g();\n" 2396 "} catch (B b) MACRO(x) {\n" 2397 " g();\n" 2398 "}"); 2399 } 2400 2401 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2402 FormatStyle Style = getLLVMStyle(); 2403 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2404 FormatStyle::BS_WebKit}) { 2405 Style.BreakBeforeBraces = BraceStyle; 2406 verifyFormat("try {\n" 2407 " // something\n" 2408 "} catch (...) {\n" 2409 " // something\n" 2410 "}", 2411 Style); 2412 } 2413 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2414 verifyFormat("try {\n" 2415 " // something\n" 2416 "}\n" 2417 "catch (...) {\n" 2418 " // something\n" 2419 "}", 2420 Style); 2421 verifyFormat("__try {\n" 2422 " // something\n" 2423 "}\n" 2424 "__finally {\n" 2425 " // something\n" 2426 "}", 2427 Style); 2428 verifyFormat("@try {\n" 2429 " // something\n" 2430 "}\n" 2431 "@finally {\n" 2432 " // something\n" 2433 "}", 2434 Style); 2435 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2436 verifyFormat("try\n" 2437 "{\n" 2438 " // something\n" 2439 "}\n" 2440 "catch (...)\n" 2441 "{\n" 2442 " // something\n" 2443 "}", 2444 Style); 2445 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2446 verifyFormat("try\n" 2447 " {\n" 2448 " // something\n" 2449 " }\n" 2450 "catch (...)\n" 2451 " {\n" 2452 " // something\n" 2453 " }", 2454 Style); 2455 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2456 Style.BraceWrapping.BeforeCatch = true; 2457 verifyFormat("try {\n" 2458 " // something\n" 2459 "}\n" 2460 "catch (...) {\n" 2461 " // something\n" 2462 "}", 2463 Style); 2464 } 2465 2466 TEST_F(FormatTest, FormatObjCTryCatch) { 2467 verifyFormat("@try {\n" 2468 " f();\n" 2469 "} @catch (NSException e) {\n" 2470 " @throw;\n" 2471 "} @finally {\n" 2472 " exit(42);\n" 2473 "}"); 2474 verifyFormat("DEBUG({\n" 2475 " @try {\n" 2476 " } @finally {\n" 2477 " }\n" 2478 "});\n"); 2479 } 2480 2481 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2482 FormatStyle Style = getLLVMStyle(); 2483 verifyFormat("@autoreleasepool {\n" 2484 " f();\n" 2485 "}\n" 2486 "@autoreleasepool {\n" 2487 " f();\n" 2488 "}\n", 2489 Style); 2490 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2491 verifyFormat("@autoreleasepool\n" 2492 "{\n" 2493 " f();\n" 2494 "}\n" 2495 "@autoreleasepool\n" 2496 "{\n" 2497 " f();\n" 2498 "}\n", 2499 Style); 2500 } 2501 2502 TEST_F(FormatTest, StaticInitializers) { 2503 verifyFormat("static SomeClass SC = {1, 'a'};"); 2504 2505 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2506 " 100000000, " 2507 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2508 2509 // Here, everything other than the "}" would fit on a line. 2510 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2511 " 10000000000000000000000000};"); 2512 EXPECT_EQ("S s = {a,\n" 2513 "\n" 2514 " b};", 2515 format("S s = {\n" 2516 " a,\n" 2517 "\n" 2518 " b\n" 2519 "};")); 2520 2521 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2522 // line. However, the formatting looks a bit off and this probably doesn't 2523 // happen often in practice. 2524 verifyFormat("static int Variable[1] = {\n" 2525 " {1000000000000000000000000000000000000}};", 2526 getLLVMStyleWithColumns(40)); 2527 } 2528 2529 TEST_F(FormatTest, DesignatedInitializers) { 2530 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2531 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2532 " .bbbbbbbbbb = 2,\n" 2533 " .cccccccccc = 3,\n" 2534 " .dddddddddd = 4,\n" 2535 " .eeeeeeeeee = 5};"); 2536 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2537 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2538 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2539 " .ccccccccccccccccccccccccccc = 3,\n" 2540 " .ddddddddddddddddddddddddddd = 4,\n" 2541 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2542 2543 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2544 } 2545 2546 TEST_F(FormatTest, NestedStaticInitializers) { 2547 verifyFormat("static A x = {{{}}};\n"); 2548 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2549 " {init1, init2, init3, init4}}};", 2550 getLLVMStyleWithColumns(50)); 2551 2552 verifyFormat("somes Status::global_reps[3] = {\n" 2553 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2554 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2555 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2556 getLLVMStyleWithColumns(60)); 2557 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2558 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2559 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2560 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2561 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2562 " {rect.fRight - rect.fLeft, rect.fBottom - " 2563 "rect.fTop}};"); 2564 2565 verifyFormat( 2566 "SomeArrayOfSomeType a = {\n" 2567 " {{1, 2, 3},\n" 2568 " {1, 2, 3},\n" 2569 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2570 " 333333333333333333333333333333},\n" 2571 " {1, 2, 3},\n" 2572 " {1, 2, 3}}};"); 2573 verifyFormat( 2574 "SomeArrayOfSomeType a = {\n" 2575 " {{1, 2, 3}},\n" 2576 " {{1, 2, 3}},\n" 2577 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2578 " 333333333333333333333333333333}},\n" 2579 " {{1, 2, 3}},\n" 2580 " {{1, 2, 3}}};"); 2581 2582 verifyFormat("struct {\n" 2583 " unsigned bit;\n" 2584 " const char *const name;\n" 2585 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2586 " {kOsWin, \"Windows\"},\n" 2587 " {kOsLinux, \"Linux\"},\n" 2588 " {kOsCrOS, \"Chrome OS\"}};"); 2589 verifyFormat("struct {\n" 2590 " unsigned bit;\n" 2591 " const char *const name;\n" 2592 "} kBitsToOs[] = {\n" 2593 " {kOsMac, \"Mac\"},\n" 2594 " {kOsWin, \"Windows\"},\n" 2595 " {kOsLinux, \"Linux\"},\n" 2596 " {kOsCrOS, \"Chrome OS\"},\n" 2597 "};"); 2598 } 2599 2600 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2601 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2602 " \\\n" 2603 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2604 } 2605 2606 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2607 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2608 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2609 2610 // Do break defaulted and deleted functions. 2611 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2612 " default;", 2613 getLLVMStyleWithColumns(40)); 2614 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2615 " delete;", 2616 getLLVMStyleWithColumns(40)); 2617 } 2618 2619 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2620 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2621 getLLVMStyleWithColumns(40)); 2622 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2623 getLLVMStyleWithColumns(40)); 2624 EXPECT_EQ("#define Q \\\n" 2625 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2626 " \"aaaaaaaa.cpp\"", 2627 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2628 getLLVMStyleWithColumns(40))); 2629 } 2630 2631 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2632 EXPECT_EQ("# 123 \"A string literal\"", 2633 format(" # 123 \"A string literal\"")); 2634 } 2635 2636 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2637 EXPECT_EQ("#;", format("#;")); 2638 verifyFormat("#\n;\n;\n;"); 2639 } 2640 2641 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2642 EXPECT_EQ("#line 42 \"test\"\n", 2643 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2644 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2645 getLLVMStyleWithColumns(12))); 2646 } 2647 2648 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2649 EXPECT_EQ("#line 42 \"test\"", 2650 format("# \\\n line \\\n 42 \\\n \"test\"")); 2651 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2652 } 2653 2654 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2655 verifyFormat("#define A \\x20"); 2656 verifyFormat("#define A \\ x20"); 2657 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2658 verifyFormat("#define A ''"); 2659 verifyFormat("#define A ''qqq"); 2660 verifyFormat("#define A `qqq"); 2661 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2662 EXPECT_EQ("const char *c = STRINGIFY(\n" 2663 "\\na : b);", 2664 format("const char * c = STRINGIFY(\n" 2665 "\\na : b);")); 2666 2667 verifyFormat("a\r\\"); 2668 verifyFormat("a\v\\"); 2669 verifyFormat("a\f\\"); 2670 } 2671 2672 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2673 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2674 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2675 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2676 // FIXME: We never break before the macro name. 2677 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2678 2679 verifyFormat("#define A A\n#define A A"); 2680 verifyFormat("#define A(X) A\n#define A A"); 2681 2682 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2683 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2684 } 2685 2686 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2687 EXPECT_EQ("// somecomment\n" 2688 "#include \"a.h\"\n" 2689 "#define A( \\\n" 2690 " A, B)\n" 2691 "#include \"b.h\"\n" 2692 "// somecomment\n", 2693 format(" // somecomment\n" 2694 " #include \"a.h\"\n" 2695 "#define A(A,\\\n" 2696 " B)\n" 2697 " #include \"b.h\"\n" 2698 " // somecomment\n", 2699 getLLVMStyleWithColumns(13))); 2700 } 2701 2702 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2703 2704 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2705 EXPECT_EQ("#define A \\\n" 2706 " c; \\\n" 2707 " e;\n" 2708 "f;", 2709 format("#define A c; e;\n" 2710 "f;", 2711 getLLVMStyleWithColumns(14))); 2712 } 2713 2714 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2715 2716 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2717 EXPECT_EQ("int x,\n" 2718 "#define A\n" 2719 " y;", 2720 format("int x,\n#define A\ny;")); 2721 } 2722 2723 TEST_F(FormatTest, HashInMacroDefinition) { 2724 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2725 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2726 verifyFormat("#define A \\\n" 2727 " { \\\n" 2728 " f(#c); \\\n" 2729 " }", 2730 getLLVMStyleWithColumns(11)); 2731 2732 verifyFormat("#define A(X) \\\n" 2733 " void function##X()", 2734 getLLVMStyleWithColumns(22)); 2735 2736 verifyFormat("#define A(a, b, c) \\\n" 2737 " void a##b##c()", 2738 getLLVMStyleWithColumns(22)); 2739 2740 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2741 } 2742 2743 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2744 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2745 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2746 } 2747 2748 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2749 EXPECT_EQ("#define A b;", format("#define A \\\n" 2750 " \\\n" 2751 " b;", 2752 getLLVMStyleWithColumns(25))); 2753 EXPECT_EQ("#define A \\\n" 2754 " \\\n" 2755 " a; \\\n" 2756 " b;", 2757 format("#define A \\\n" 2758 " \\\n" 2759 " a; \\\n" 2760 " b;", 2761 getLLVMStyleWithColumns(11))); 2762 EXPECT_EQ("#define A \\\n" 2763 " a; \\\n" 2764 " \\\n" 2765 " b;", 2766 format("#define A \\\n" 2767 " a; \\\n" 2768 " \\\n" 2769 " b;", 2770 getLLVMStyleWithColumns(11))); 2771 } 2772 2773 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2774 verifyIncompleteFormat("#define A :"); 2775 verifyFormat("#define SOMECASES \\\n" 2776 " case 1: \\\n" 2777 " case 2\n", 2778 getLLVMStyleWithColumns(20)); 2779 verifyFormat("#define MACRO(a) \\\n" 2780 " if (a) \\\n" 2781 " f(); \\\n" 2782 " else \\\n" 2783 " g()", 2784 getLLVMStyleWithColumns(18)); 2785 verifyFormat("#define A template <typename T>"); 2786 verifyIncompleteFormat("#define STR(x) #x\n" 2787 "f(STR(this_is_a_string_literal{));"); 2788 verifyFormat("#pragma omp threadprivate( \\\n" 2789 " y)), // expected-warning", 2790 getLLVMStyleWithColumns(28)); 2791 verifyFormat("#d, = };"); 2792 verifyFormat("#if \"a"); 2793 verifyIncompleteFormat("({\n" 2794 "#define b \\\n" 2795 " } \\\n" 2796 " a\n" 2797 "a", 2798 getLLVMStyleWithColumns(15)); 2799 verifyFormat("#define A \\\n" 2800 " { \\\n" 2801 " {\n" 2802 "#define B \\\n" 2803 " } \\\n" 2804 " }", 2805 getLLVMStyleWithColumns(15)); 2806 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2807 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2808 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2809 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2810 } 2811 2812 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2813 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2814 EXPECT_EQ("class A : public QObject {\n" 2815 " Q_OBJECT\n" 2816 "\n" 2817 " A() {}\n" 2818 "};", 2819 format("class A : public QObject {\n" 2820 " Q_OBJECT\n" 2821 "\n" 2822 " A() {\n}\n" 2823 "} ;")); 2824 EXPECT_EQ("MACRO\n" 2825 "/*static*/ int i;", 2826 format("MACRO\n" 2827 " /*static*/ int i;")); 2828 EXPECT_EQ("SOME_MACRO\n" 2829 "namespace {\n" 2830 "void f();\n" 2831 "}", 2832 format("SOME_MACRO\n" 2833 " namespace {\n" 2834 "void f( );\n" 2835 "}")); 2836 // Only if the identifier contains at least 5 characters. 2837 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2838 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2839 // Only if everything is upper case. 2840 EXPECT_EQ("class A : public QObject {\n" 2841 " Q_Object A() {}\n" 2842 "};", 2843 format("class A : public QObject {\n" 2844 " Q_Object\n" 2845 " A() {\n}\n" 2846 "} ;")); 2847 2848 // Only if the next line can actually start an unwrapped line. 2849 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2850 format("SOME_WEIRD_LOG_MACRO\n" 2851 "<< SomeThing;")); 2852 2853 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2854 "(n, buffers))\n", 2855 getChromiumStyle(FormatStyle::LK_Cpp)); 2856 } 2857 2858 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2859 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2860 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2861 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2862 "class X {};\n" 2863 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2864 "int *createScopDetectionPass() { return 0; }", 2865 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2866 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2867 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2868 " class X {};\n" 2869 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2870 " int *createScopDetectionPass() { return 0; }")); 2871 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2872 // braces, so that inner block is indented one level more. 2873 EXPECT_EQ("int q() {\n" 2874 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2875 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2876 " IPC_END_MESSAGE_MAP()\n" 2877 "}", 2878 format("int q() {\n" 2879 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2880 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2881 " IPC_END_MESSAGE_MAP()\n" 2882 "}")); 2883 2884 // Same inside macros. 2885 EXPECT_EQ("#define LIST(L) \\\n" 2886 " L(A) \\\n" 2887 " L(B) \\\n" 2888 " L(C)", 2889 format("#define LIST(L) \\\n" 2890 " L(A) \\\n" 2891 " L(B) \\\n" 2892 " L(C)", 2893 getGoogleStyle())); 2894 2895 // These must not be recognized as macros. 2896 EXPECT_EQ("int q() {\n" 2897 " f(x);\n" 2898 " f(x) {}\n" 2899 " f(x)->g();\n" 2900 " f(x)->*g();\n" 2901 " f(x).g();\n" 2902 " f(x) = x;\n" 2903 " f(x) += x;\n" 2904 " f(x) -= x;\n" 2905 " f(x) *= x;\n" 2906 " f(x) /= x;\n" 2907 " f(x) %= x;\n" 2908 " f(x) &= x;\n" 2909 " f(x) |= x;\n" 2910 " f(x) ^= x;\n" 2911 " f(x) >>= x;\n" 2912 " f(x) <<= x;\n" 2913 " f(x)[y].z();\n" 2914 " LOG(INFO) << x;\n" 2915 " ifstream(x) >> x;\n" 2916 "}\n", 2917 format("int q() {\n" 2918 " f(x)\n;\n" 2919 " f(x)\n {}\n" 2920 " f(x)\n->g();\n" 2921 " f(x)\n->*g();\n" 2922 " f(x)\n.g();\n" 2923 " f(x)\n = x;\n" 2924 " f(x)\n += x;\n" 2925 " f(x)\n -= x;\n" 2926 " f(x)\n *= x;\n" 2927 " f(x)\n /= x;\n" 2928 " f(x)\n %= x;\n" 2929 " f(x)\n &= x;\n" 2930 " f(x)\n |= x;\n" 2931 " f(x)\n ^= x;\n" 2932 " f(x)\n >>= x;\n" 2933 " f(x)\n <<= x;\n" 2934 " f(x)\n[y].z();\n" 2935 " LOG(INFO)\n << x;\n" 2936 " ifstream(x)\n >> x;\n" 2937 "}\n")); 2938 EXPECT_EQ("int q() {\n" 2939 " F(x)\n" 2940 " if (1) {\n" 2941 " }\n" 2942 " F(x)\n" 2943 " while (1) {\n" 2944 " }\n" 2945 " F(x)\n" 2946 " G(x);\n" 2947 " F(x)\n" 2948 " try {\n" 2949 " Q();\n" 2950 " } catch (...) {\n" 2951 " }\n" 2952 "}\n", 2953 format("int q() {\n" 2954 "F(x)\n" 2955 "if (1) {}\n" 2956 "F(x)\n" 2957 "while (1) {}\n" 2958 "F(x)\n" 2959 "G(x);\n" 2960 "F(x)\n" 2961 "try { Q(); } catch (...) {}\n" 2962 "}\n")); 2963 EXPECT_EQ("class A {\n" 2964 " A() : t(0) {}\n" 2965 " A(int i) noexcept() : {}\n" 2966 " A(X x)\n" // FIXME: function-level try blocks are broken. 2967 " try : t(0) {\n" 2968 " } catch (...) {\n" 2969 " }\n" 2970 "};", 2971 format("class A {\n" 2972 " A()\n : t(0) {}\n" 2973 " A(int i)\n noexcept() : {}\n" 2974 " A(X x)\n" 2975 " try : t(0) {} catch (...) {}\n" 2976 "};")); 2977 EXPECT_EQ("class SomeClass {\n" 2978 "public:\n" 2979 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2980 "};", 2981 format("class SomeClass {\n" 2982 "public:\n" 2983 " SomeClass()\n" 2984 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2985 "};")); 2986 EXPECT_EQ("class SomeClass {\n" 2987 "public:\n" 2988 " SomeClass()\n" 2989 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2990 "};", 2991 format("class SomeClass {\n" 2992 "public:\n" 2993 " SomeClass()\n" 2994 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2995 "};", 2996 getLLVMStyleWithColumns(40))); 2997 2998 verifyFormat("MACRO(>)"); 2999 } 3000 3001 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3002 verifyFormat("#define A \\\n" 3003 " f({ \\\n" 3004 " g(); \\\n" 3005 " });", 3006 getLLVMStyleWithColumns(11)); 3007 } 3008 3009 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 3010 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 3011 } 3012 3013 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3014 verifyFormat("{\n { a #c; }\n}"); 3015 } 3016 3017 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3018 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3019 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3020 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3021 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3022 } 3023 3024 TEST_F(FormatTest, EscapedNewlines) { 3025 EXPECT_EQ( 3026 "#define A \\\n int i; \\\n int j;", 3027 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3028 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3029 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3030 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3031 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3032 } 3033 3034 TEST_F(FormatTest, DontCrashOnBlockComments) { 3035 EXPECT_EQ( 3036 "int xxxxxxxxx; /* " 3037 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3038 "zzzzzz\n" 3039 "0*/", 3040 format("int xxxxxxxxx; /* " 3041 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3042 "0*/")); 3043 } 3044 3045 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3046 verifyFormat("#define A \\\n" 3047 " int v( \\\n" 3048 " a); \\\n" 3049 " int i;", 3050 getLLVMStyleWithColumns(11)); 3051 } 3052 3053 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3054 EXPECT_EQ( 3055 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3056 " \\\n" 3057 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3058 "\n" 3059 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3060 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3061 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3062 "\\\n" 3063 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3064 " \n" 3065 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3066 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3067 } 3068 3069 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3070 EXPECT_EQ("int\n" 3071 "#define A\n" 3072 " a;", 3073 format("int\n#define A\na;")); 3074 verifyFormat("functionCallTo(\n" 3075 " someOtherFunction(\n" 3076 " withSomeParameters, whichInSequence,\n" 3077 " areLongerThanALine(andAnotherCall,\n" 3078 "#define A B\n" 3079 " withMoreParamters,\n" 3080 " whichStronglyInfluenceTheLayout),\n" 3081 " andMoreParameters),\n" 3082 " trailing);", 3083 getLLVMStyleWithColumns(69)); 3084 verifyFormat("Foo::Foo()\n" 3085 "#ifdef BAR\n" 3086 " : baz(0)\n" 3087 "#endif\n" 3088 "{\n" 3089 "}"); 3090 verifyFormat("void f() {\n" 3091 " if (true)\n" 3092 "#ifdef A\n" 3093 " f(42);\n" 3094 " x();\n" 3095 "#else\n" 3096 " g();\n" 3097 " x();\n" 3098 "#endif\n" 3099 "}"); 3100 verifyFormat("void f(param1, param2,\n" 3101 " param3,\n" 3102 "#ifdef A\n" 3103 " param4(param5,\n" 3104 "#ifdef A1\n" 3105 " param6,\n" 3106 "#ifdef A2\n" 3107 " param7),\n" 3108 "#else\n" 3109 " param8),\n" 3110 " param9,\n" 3111 "#endif\n" 3112 " param10,\n" 3113 "#endif\n" 3114 " param11)\n" 3115 "#else\n" 3116 " param12)\n" 3117 "#endif\n" 3118 "{\n" 3119 " x();\n" 3120 "}", 3121 getLLVMStyleWithColumns(28)); 3122 verifyFormat("#if 1\n" 3123 "int i;"); 3124 verifyFormat("#if 1\n" 3125 "#endif\n" 3126 "#if 1\n" 3127 "#else\n" 3128 "#endif\n"); 3129 verifyFormat("DEBUG({\n" 3130 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3132 "});\n" 3133 "#if a\n" 3134 "#else\n" 3135 "#endif"); 3136 3137 verifyIncompleteFormat("void f(\n" 3138 "#if A\n" 3139 " );\n" 3140 "#else\n" 3141 "#endif"); 3142 } 3143 3144 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3145 verifyFormat("#endif\n" 3146 "#if B"); 3147 } 3148 3149 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3150 FormatStyle SingleLine = getLLVMStyle(); 3151 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3152 verifyFormat("#if 0\n" 3153 "#elif 1\n" 3154 "#endif\n" 3155 "void foo() {\n" 3156 " if (test) foo2();\n" 3157 "}", 3158 SingleLine); 3159 } 3160 3161 TEST_F(FormatTest, LayoutBlockInsideParens) { 3162 verifyFormat("functionCall({ int i; });"); 3163 verifyFormat("functionCall({\n" 3164 " int i;\n" 3165 " int j;\n" 3166 "});"); 3167 verifyFormat("functionCall(\n" 3168 " {\n" 3169 " int i;\n" 3170 " int j;\n" 3171 " },\n" 3172 " aaaa, bbbb, cccc);"); 3173 verifyFormat("functionA(functionB({\n" 3174 " int i;\n" 3175 " int j;\n" 3176 " }),\n" 3177 " aaaa, bbbb, cccc);"); 3178 verifyFormat("functionCall(\n" 3179 " {\n" 3180 " int i;\n" 3181 " int j;\n" 3182 " },\n" 3183 " aaaa, bbbb, // comment\n" 3184 " cccc);"); 3185 verifyFormat("functionA(functionB({\n" 3186 " int i;\n" 3187 " int j;\n" 3188 " }),\n" 3189 " aaaa, bbbb, // comment\n" 3190 " cccc);"); 3191 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3192 verifyFormat("functionCall(aaaa, bbbb, {\n" 3193 " int i;\n" 3194 " int j;\n" 3195 "});"); 3196 verifyFormat( 3197 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3198 " {\n" 3199 " int i; // break\n" 3200 " },\n" 3201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3202 " ccccccccccccccccc));"); 3203 verifyFormat("DEBUG({\n" 3204 " if (a)\n" 3205 " f();\n" 3206 "});"); 3207 } 3208 3209 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3210 EXPECT_EQ("SOME_MACRO { int i; }\n" 3211 "int i;", 3212 format(" SOME_MACRO {int i;} int i;")); 3213 } 3214 3215 TEST_F(FormatTest, LayoutNestedBlocks) { 3216 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3217 " struct s {\n" 3218 " int i;\n" 3219 " };\n" 3220 " s kBitsToOs[] = {{10}};\n" 3221 " for (int i = 0; i < 10; ++i)\n" 3222 " return;\n" 3223 "}"); 3224 verifyFormat("call(parameter, {\n" 3225 " something();\n" 3226 " // Comment using all columns.\n" 3227 " somethingelse();\n" 3228 "});", 3229 getLLVMStyleWithColumns(40)); 3230 verifyFormat("DEBUG( //\n" 3231 " { f(); }, a);"); 3232 verifyFormat("DEBUG( //\n" 3233 " {\n" 3234 " f(); //\n" 3235 " },\n" 3236 " a);"); 3237 3238 EXPECT_EQ("call(parameter, {\n" 3239 " something();\n" 3240 " // Comment too\n" 3241 " // looooooooooong.\n" 3242 " somethingElse();\n" 3243 "});", 3244 format("call(parameter, {\n" 3245 " something();\n" 3246 " // Comment too looooooooooong.\n" 3247 " somethingElse();\n" 3248 "});", 3249 getLLVMStyleWithColumns(29))); 3250 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3251 EXPECT_EQ("DEBUG({ // comment\n" 3252 " int i;\n" 3253 "});", 3254 format("DEBUG({ // comment\n" 3255 "int i;\n" 3256 "});")); 3257 EXPECT_EQ("DEBUG({\n" 3258 " int i;\n" 3259 "\n" 3260 " // comment\n" 3261 " int j;\n" 3262 "});", 3263 format("DEBUG({\n" 3264 " int i;\n" 3265 "\n" 3266 " // comment\n" 3267 " int j;\n" 3268 "});")); 3269 3270 verifyFormat("DEBUG({\n" 3271 " if (a)\n" 3272 " return;\n" 3273 "});"); 3274 verifyGoogleFormat("DEBUG({\n" 3275 " if (a) return;\n" 3276 "});"); 3277 FormatStyle Style = getGoogleStyle(); 3278 Style.ColumnLimit = 45; 3279 verifyFormat("Debug(aaaaa,\n" 3280 " {\n" 3281 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3282 " },\n" 3283 " a);", 3284 Style); 3285 3286 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3287 3288 verifyNoCrash("^{v^{a}}"); 3289 } 3290 3291 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3292 EXPECT_EQ("#define MACRO() \\\n" 3293 " Debug(aaa, /* force line break */ \\\n" 3294 " { \\\n" 3295 " int i; \\\n" 3296 " int j; \\\n" 3297 " })", 3298 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3299 " { int i; int j; })", 3300 getGoogleStyle())); 3301 3302 EXPECT_EQ("#define A \\\n" 3303 " [] { \\\n" 3304 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3305 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3306 " }", 3307 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3308 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3309 getGoogleStyle())); 3310 } 3311 3312 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3313 EXPECT_EQ("{}", format("{}")); 3314 verifyFormat("enum E {};"); 3315 verifyFormat("enum E {}"); 3316 } 3317 3318 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3319 FormatStyle Style = getLLVMStyle(); 3320 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3321 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3322 verifyFormat("FOO_BEGIN\n" 3323 " FOO_ENTRY\n" 3324 "FOO_END", Style); 3325 verifyFormat("FOO_BEGIN\n" 3326 " NESTED_FOO_BEGIN\n" 3327 " NESTED_FOO_ENTRY\n" 3328 " NESTED_FOO_END\n" 3329 "FOO_END", Style); 3330 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3331 " int x;\n" 3332 " x = 1;\n" 3333 "FOO_END(Baz)", Style); 3334 } 3335 3336 //===----------------------------------------------------------------------===// 3337 // Line break tests. 3338 //===----------------------------------------------------------------------===// 3339 3340 TEST_F(FormatTest, PreventConfusingIndents) { 3341 verifyFormat( 3342 "void f() {\n" 3343 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3344 " parameter, parameter, parameter)),\n" 3345 " SecondLongCall(parameter));\n" 3346 "}"); 3347 verifyFormat( 3348 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3349 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3351 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3352 verifyFormat( 3353 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3354 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3355 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3356 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3357 verifyFormat( 3358 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3359 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3361 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3362 verifyFormat("int a = bbbb && ccc && fffff(\n" 3363 "#define A Just forcing a new line\n" 3364 " ddd);"); 3365 } 3366 3367 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3368 verifyFormat( 3369 "bool aaaaaaa =\n" 3370 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3371 " bbbbbbbb();"); 3372 verifyFormat( 3373 "bool aaaaaaa =\n" 3374 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3375 " bbbbbbbb();"); 3376 3377 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3379 " ccccccccc == ddddddddddd;"); 3380 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3382 " ccccccccc == ddddddddddd;"); 3383 verifyFormat( 3384 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3386 " ccccccccc == ddddddddddd;"); 3387 3388 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3389 " aaaaaa) &&\n" 3390 " bbbbbb && cccccc;"); 3391 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3392 " aaaaaa) >>\n" 3393 " bbbbbb;"); 3394 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3395 " SourceMgr.getSpellingColumnNumber(\n" 3396 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3397 " 1);"); 3398 3399 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3400 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3401 " cccccc) {\n}"); 3402 verifyFormat("b = a &&\n" 3403 " // Comment\n" 3404 " b.c && d;"); 3405 3406 // If the LHS of a comparison is not a binary expression itself, the 3407 // additional linebreak confuses many people. 3408 verifyFormat( 3409 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3411 "}"); 3412 verifyFormat( 3413 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3415 "}"); 3416 verifyFormat( 3417 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3419 "}"); 3420 // Even explicit parentheses stress the precedence enough to make the 3421 // additional break unnecessary. 3422 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3424 "}"); 3425 // This cases is borderline, but with the indentation it is still readable. 3426 verifyFormat( 3427 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3428 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3430 "}", 3431 getLLVMStyleWithColumns(75)); 3432 3433 // If the LHS is a binary expression, we should still use the additional break 3434 // as otherwise the formatting hides the operator precedence. 3435 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3437 " 5) {\n" 3438 "}"); 3439 3440 FormatStyle OnePerLine = getLLVMStyle(); 3441 OnePerLine.BinPackParameters = false; 3442 verifyFormat( 3443 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3446 OnePerLine); 3447 } 3448 3449 TEST_F(FormatTest, ExpressionIndentation) { 3450 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3454 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3455 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3458 " ccccccccccccccccccccccccccccccccccccccccc;"); 3459 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3462 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3463 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3466 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3467 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3470 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3471 verifyFormat("if () {\n" 3472 "} else if (aaaaa &&\n" 3473 " bbbbb > // break\n" 3474 " ccccc) {\n" 3475 "}"); 3476 3477 // Presence of a trailing comment used to change indentation of b. 3478 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3479 " b;\n" 3480 "return aaaaaaaaaaaaaaaaaaa +\n" 3481 " b; //", 3482 getLLVMStyleWithColumns(30)); 3483 } 3484 3485 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3486 // Not sure what the best system is here. Like this, the LHS can be found 3487 // immediately above an operator (everything with the same or a higher 3488 // indent). The RHS is aligned right of the operator and so compasses 3489 // everything until something with the same indent as the operator is found. 3490 // FIXME: Is this a good system? 3491 FormatStyle Style = getLLVMStyle(); 3492 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3493 verifyFormat( 3494 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3495 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3496 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3497 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3498 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3499 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3500 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3501 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3502 " > ccccccccccccccccccccccccccccccccccccccccc;", 3503 Style); 3504 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3505 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3506 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3507 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3508 Style); 3509 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3510 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3511 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3512 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3513 Style); 3514 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3515 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3516 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3517 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3518 Style); 3519 verifyFormat("if () {\n" 3520 "} else if (aaaaa\n" 3521 " && bbbbb // break\n" 3522 " > ccccc) {\n" 3523 "}", 3524 Style); 3525 verifyFormat("return (a)\n" 3526 " // comment\n" 3527 " + b;", 3528 Style); 3529 verifyFormat( 3530 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3531 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3532 " + cc;", 3533 Style); 3534 3535 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3537 Style); 3538 3539 // Forced by comments. 3540 verifyFormat( 3541 "unsigned ContentSize =\n" 3542 " sizeof(int16_t) // DWARF ARange version number\n" 3543 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3544 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3545 " + sizeof(int8_t); // Segment Size (in bytes)"); 3546 3547 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3548 " == boost::fusion::at_c<1>(iiii).second;", 3549 Style); 3550 3551 Style.ColumnLimit = 60; 3552 verifyFormat("zzzzzzzzzz\n" 3553 " = bbbbbbbbbbbbbbbbb\n" 3554 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3555 Style); 3556 } 3557 3558 TEST_F(FormatTest, NoOperandAlignment) { 3559 FormatStyle Style = getLLVMStyle(); 3560 Style.AlignOperands = false; 3561 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3562 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3563 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3564 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3565 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3566 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3567 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3568 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3569 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3570 " > ccccccccccccccccccccccccccccccccccccccccc;", 3571 Style); 3572 3573 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3574 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3575 " + cc;", 3576 Style); 3577 verifyFormat("int a = aa\n" 3578 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3579 " * cccccccccccccccccccccccccccccccccccc;", 3580 Style); 3581 3582 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3583 verifyFormat("return (a > b\n" 3584 " // comment1\n" 3585 " // comment2\n" 3586 " || c);", 3587 Style); 3588 } 3589 3590 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3591 FormatStyle Style = getLLVMStyle(); 3592 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3593 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3594 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3595 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3596 Style); 3597 } 3598 3599 TEST_F(FormatTest, ConstructorInitializers) { 3600 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3601 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3602 getLLVMStyleWithColumns(45)); 3603 verifyFormat("Constructor()\n" 3604 " : Inttializer(FitsOnTheLine) {}", 3605 getLLVMStyleWithColumns(44)); 3606 verifyFormat("Constructor()\n" 3607 " : Inttializer(FitsOnTheLine) {}", 3608 getLLVMStyleWithColumns(43)); 3609 3610 verifyFormat("template <typename T>\n" 3611 "Constructor() : Initializer(FitsOnTheLine) {}", 3612 getLLVMStyleWithColumns(45)); 3613 3614 verifyFormat( 3615 "SomeClass::Constructor()\n" 3616 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3617 3618 verifyFormat( 3619 "SomeClass::Constructor()\n" 3620 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3621 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3622 verifyFormat( 3623 "SomeClass::Constructor()\n" 3624 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3625 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3626 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3627 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3628 " : aaaaaaaaaa(aaaaaa) {}"); 3629 3630 verifyFormat("Constructor()\n" 3631 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3632 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3633 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3634 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3635 3636 verifyFormat("Constructor()\n" 3637 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3639 3640 verifyFormat("Constructor(int Parameter = 0)\n" 3641 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3642 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3643 verifyFormat("Constructor()\n" 3644 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3645 "}", 3646 getLLVMStyleWithColumns(60)); 3647 verifyFormat("Constructor()\n" 3648 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3649 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3650 3651 // Here a line could be saved by splitting the second initializer onto two 3652 // lines, but that is not desirable. 3653 verifyFormat("Constructor()\n" 3654 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3655 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3656 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3657 3658 FormatStyle OnePerLine = getLLVMStyle(); 3659 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3660 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3661 verifyFormat("SomeClass::Constructor()\n" 3662 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3663 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3664 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3665 OnePerLine); 3666 verifyFormat("SomeClass::Constructor()\n" 3667 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3668 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3669 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3670 OnePerLine); 3671 verifyFormat("MyClass::MyClass(int var)\n" 3672 " : some_var_(var), // 4 space indent\n" 3673 " some_other_var_(var + 1) { // lined up\n" 3674 "}", 3675 OnePerLine); 3676 verifyFormat("Constructor()\n" 3677 " : aaaaa(aaaaaa),\n" 3678 " aaaaa(aaaaaa),\n" 3679 " aaaaa(aaaaaa),\n" 3680 " aaaaa(aaaaaa),\n" 3681 " aaaaa(aaaaaa) {}", 3682 OnePerLine); 3683 verifyFormat("Constructor()\n" 3684 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3685 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3686 OnePerLine); 3687 OnePerLine.BinPackParameters = false; 3688 verifyFormat( 3689 "Constructor()\n" 3690 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3691 " aaaaaaaaaaa().aaa(),\n" 3692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3693 OnePerLine); 3694 OnePerLine.ColumnLimit = 60; 3695 verifyFormat("Constructor()\n" 3696 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3697 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3698 OnePerLine); 3699 3700 EXPECT_EQ("Constructor()\n" 3701 " : // Comment forcing unwanted break.\n" 3702 " aaaa(aaaa) {}", 3703 format("Constructor() :\n" 3704 " // Comment forcing unwanted break.\n" 3705 " aaaa(aaaa) {}")); 3706 } 3707 3708 TEST_F(FormatTest, MemoizationTests) { 3709 // This breaks if the memoization lookup does not take \c Indent and 3710 // \c LastSpace into account. 3711 verifyFormat( 3712 "extern CFRunLoopTimerRef\n" 3713 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3714 " CFTimeInterval interval, CFOptionFlags flags,\n" 3715 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3716 " CFRunLoopTimerContext *context) {}"); 3717 3718 // Deep nesting somewhat works around our memoization. 3719 verifyFormat( 3720 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3721 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3722 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3723 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3724 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3725 getLLVMStyleWithColumns(65)); 3726 verifyFormat( 3727 "aaaaa(\n" 3728 " aaaaa,\n" 3729 " aaaaa(\n" 3730 " aaaaa,\n" 3731 " aaaaa(\n" 3732 " aaaaa,\n" 3733 " aaaaa(\n" 3734 " aaaaa,\n" 3735 " aaaaa(\n" 3736 " aaaaa,\n" 3737 " aaaaa(\n" 3738 " aaaaa,\n" 3739 " aaaaa(\n" 3740 " aaaaa,\n" 3741 " aaaaa(\n" 3742 " aaaaa,\n" 3743 " aaaaa(\n" 3744 " aaaaa,\n" 3745 " aaaaa(\n" 3746 " aaaaa,\n" 3747 " aaaaa(\n" 3748 " aaaaa,\n" 3749 " aaaaa(\n" 3750 " aaaaa,\n" 3751 " aaaaa))))))))))));", 3752 getLLVMStyleWithColumns(65)); 3753 verifyFormat( 3754 "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" 3755 " a),\n" 3756 " a),\n" 3757 " a),\n" 3758 " a),\n" 3759 " a),\n" 3760 " a),\n" 3761 " a),\n" 3762 " a),\n" 3763 " a),\n" 3764 " a),\n" 3765 " a),\n" 3766 " a),\n" 3767 " a),\n" 3768 " a),\n" 3769 " a),\n" 3770 " a),\n" 3771 " a)", 3772 getLLVMStyleWithColumns(65)); 3773 3774 // This test takes VERY long when memoization is broken. 3775 FormatStyle OnePerLine = getLLVMStyle(); 3776 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3777 OnePerLine.BinPackParameters = false; 3778 std::string input = "Constructor()\n" 3779 " : aaaa(a,\n"; 3780 for (unsigned i = 0, e = 80; i != e; ++i) { 3781 input += " a,\n"; 3782 } 3783 input += " a) {}"; 3784 verifyFormat(input, OnePerLine); 3785 } 3786 3787 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3788 verifyFormat( 3789 "void f() {\n" 3790 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3791 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3792 " f();\n" 3793 "}"); 3794 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3795 " Intervals[i - 1].getRange().getLast()) {\n}"); 3796 } 3797 3798 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3799 // Principially, we break function declarations in a certain order: 3800 // 1) break amongst arguments. 3801 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3802 " Cccccccccccccc cccccccccccccc);"); 3803 verifyFormat("template <class TemplateIt>\n" 3804 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3805 " TemplateIt *stop) {}"); 3806 3807 // 2) break after return type. 3808 verifyFormat( 3809 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3810 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3811 getGoogleStyle()); 3812 3813 // 3) break after (. 3814 verifyFormat( 3815 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3816 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3817 getGoogleStyle()); 3818 3819 // 4) break before after nested name specifiers. 3820 verifyFormat( 3821 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3822 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3823 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3824 getGoogleStyle()); 3825 3826 // However, there are exceptions, if a sufficient amount of lines can be 3827 // saved. 3828 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3829 // more adjusting. 3830 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3831 " Cccccccccccccc cccccccccc,\n" 3832 " Cccccccccccccc cccccccccc,\n" 3833 " Cccccccccccccc cccccccccc,\n" 3834 " Cccccccccccccc cccccccccc);"); 3835 verifyFormat( 3836 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3837 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3838 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3839 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3840 getGoogleStyle()); 3841 verifyFormat( 3842 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3843 " Cccccccccccccc cccccccccc,\n" 3844 " Cccccccccccccc cccccccccc,\n" 3845 " Cccccccccccccc cccccccccc,\n" 3846 " Cccccccccccccc cccccccccc,\n" 3847 " Cccccccccccccc cccccccccc,\n" 3848 " Cccccccccccccc cccccccccc);"); 3849 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3850 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3851 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3852 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3853 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3854 3855 // Break after multi-line parameters. 3856 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3859 " bbbb bbbb);"); 3860 verifyFormat("void SomeLoooooooooooongFunction(\n" 3861 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3862 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3863 " int bbbbbbbbbbbbb);"); 3864 3865 // Treat overloaded operators like other functions. 3866 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3867 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3868 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3869 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3870 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3871 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3872 verifyGoogleFormat( 3873 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3874 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3875 verifyGoogleFormat( 3876 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3877 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3878 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3879 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3880 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3881 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3882 verifyGoogleFormat( 3883 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3884 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3885 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3886 verifyGoogleFormat( 3887 "template <typename T>\n" 3888 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3889 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3890 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3891 3892 FormatStyle Style = getLLVMStyle(); 3893 Style.PointerAlignment = FormatStyle::PAS_Left; 3894 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3895 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3896 Style); 3897 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3899 Style); 3900 } 3901 3902 TEST_F(FormatTest, TrailingReturnType) { 3903 verifyFormat("auto foo() -> int;\n"); 3904 verifyFormat("struct S {\n" 3905 " auto bar() const -> int;\n" 3906 "};"); 3907 verifyFormat("template <size_t Order, typename T>\n" 3908 "auto load_img(const std::string &filename)\n" 3909 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3910 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3911 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3912 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3913 verifyFormat("template <typename T>\n" 3914 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3915 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3916 3917 // Not trailing return types. 3918 verifyFormat("void f() { auto a = b->c(); }"); 3919 } 3920 3921 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3922 // Avoid breaking before trailing 'const' or other trailing annotations, if 3923 // they are not function-like. 3924 FormatStyle Style = getGoogleStyle(); 3925 Style.ColumnLimit = 47; 3926 verifyFormat("void someLongFunction(\n" 3927 " int someLoooooooooooooongParameter) const {\n}", 3928 getLLVMStyleWithColumns(47)); 3929 verifyFormat("LoooooongReturnType\n" 3930 "someLoooooooongFunction() const {}", 3931 getLLVMStyleWithColumns(47)); 3932 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3933 " const {}", 3934 Style); 3935 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3936 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3937 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3938 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3939 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3940 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3941 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3942 " aaaaaaaaaaa aaaaa) const override;"); 3943 verifyGoogleFormat( 3944 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3945 " const override;"); 3946 3947 // Even if the first parameter has to be wrapped. 3948 verifyFormat("void someLongFunction(\n" 3949 " int someLongParameter) const {}", 3950 getLLVMStyleWithColumns(46)); 3951 verifyFormat("void someLongFunction(\n" 3952 " int someLongParameter) const {}", 3953 Style); 3954 verifyFormat("void someLongFunction(\n" 3955 " int someLongParameter) override {}", 3956 Style); 3957 verifyFormat("void someLongFunction(\n" 3958 " int someLongParameter) OVERRIDE {}", 3959 Style); 3960 verifyFormat("void someLongFunction(\n" 3961 " int someLongParameter) final {}", 3962 Style); 3963 verifyFormat("void someLongFunction(\n" 3964 " int someLongParameter) FINAL {}", 3965 Style); 3966 verifyFormat("void someLongFunction(\n" 3967 " int parameter) const override {}", 3968 Style); 3969 3970 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3971 verifyFormat("void someLongFunction(\n" 3972 " int someLongParameter) const\n" 3973 "{\n" 3974 "}", 3975 Style); 3976 3977 // Unless these are unknown annotations. 3978 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3979 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3980 " LONG_AND_UGLY_ANNOTATION;"); 3981 3982 // Breaking before function-like trailing annotations is fine to keep them 3983 // close to their arguments. 3984 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3985 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3986 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3987 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3988 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3989 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3990 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3991 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3992 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3993 3994 verifyFormat( 3995 "void aaaaaaaaaaaaaaaaaa()\n" 3996 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3997 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3998 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3999 " __attribute__((unused));"); 4000 verifyGoogleFormat( 4001 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4002 " GUARDED_BY(aaaaaaaaaaaa);"); 4003 verifyGoogleFormat( 4004 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4005 " GUARDED_BY(aaaaaaaaaaaa);"); 4006 verifyGoogleFormat( 4007 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4008 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4009 verifyGoogleFormat( 4010 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4011 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4012 } 4013 4014 TEST_F(FormatTest, FunctionAnnotations) { 4015 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4016 "int OldFunction(const string ¶meter) {}"); 4017 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4018 "string OldFunction(const string ¶meter) {}"); 4019 verifyFormat("template <typename T>\n" 4020 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4021 "string OldFunction(const string ¶meter) {}"); 4022 4023 // Not function annotations. 4024 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4025 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4026 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4027 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4028 verifyFormat("MACRO(abc).function() // wrap\n" 4029 " << abc;"); 4030 verifyFormat("MACRO(abc)->function() // wrap\n" 4031 " << abc;"); 4032 verifyFormat("MACRO(abc)::function() // wrap\n" 4033 " << abc;"); 4034 } 4035 4036 TEST_F(FormatTest, BreaksDesireably) { 4037 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4038 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4039 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4040 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4042 "}"); 4043 4044 verifyFormat( 4045 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4047 4048 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4051 4052 verifyFormat( 4053 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4055 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4057 4058 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4059 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4060 4061 verifyFormat( 4062 "void f() {\n" 4063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4065 "}"); 4066 verifyFormat( 4067 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4069 verifyFormat( 4070 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4072 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4075 4076 // Indent consistently independent of call expression and unary operator. 4077 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4078 " dddddddddddddddddddddddddddddd));"); 4079 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4080 " dddddddddddddddddddddddddddddd));"); 4081 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4082 " dddddddddddddddddddddddddddddd));"); 4083 4084 // This test case breaks on an incorrect memoization, i.e. an optimization not 4085 // taking into account the StopAt value. 4086 verifyFormat( 4087 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4088 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4089 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4090 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4091 4092 verifyFormat("{\n {\n {\n" 4093 " Annotation.SpaceRequiredBefore =\n" 4094 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4095 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4096 " }\n }\n}"); 4097 4098 // Break on an outer level if there was a break on an inner level. 4099 EXPECT_EQ("f(g(h(a, // comment\n" 4100 " b, c),\n" 4101 " d, e),\n" 4102 " x, y);", 4103 format("f(g(h(a, // comment\n" 4104 " b, c), d, e), x, y);")); 4105 4106 // Prefer breaking similar line breaks. 4107 verifyFormat( 4108 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4109 " NSTrackingMouseEnteredAndExited |\n" 4110 " NSTrackingActiveAlways;"); 4111 } 4112 4113 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4114 FormatStyle NoBinPacking = getGoogleStyle(); 4115 NoBinPacking.BinPackParameters = false; 4116 NoBinPacking.BinPackArguments = true; 4117 verifyFormat("void f() {\n" 4118 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4120 "}", 4121 NoBinPacking); 4122 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4123 " int aaaaaaaaaaaaaaaaaaaa,\n" 4124 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4125 NoBinPacking); 4126 4127 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4128 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4129 " vector<int> bbbbbbbbbbbbbbb);", 4130 NoBinPacking); 4131 // FIXME: This behavior difference is probably not wanted. However, currently 4132 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4133 // template arguments from BreakBeforeParameter being set because of the 4134 // one-per-line formatting. 4135 verifyFormat( 4136 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4137 " aaaaaaaaaa> aaaaaaaaaa);", 4138 NoBinPacking); 4139 verifyFormat( 4140 "void fffffffffff(\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4142 " aaaaaaaaaa);"); 4143 } 4144 4145 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4146 FormatStyle NoBinPacking = getGoogleStyle(); 4147 NoBinPacking.BinPackParameters = false; 4148 NoBinPacking.BinPackArguments = false; 4149 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4150 " aaaaaaaaaaaaaaaaaaaa,\n" 4151 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4152 NoBinPacking); 4153 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4154 " aaaaaaaaaaaaa,\n" 4155 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4156 NoBinPacking); 4157 verifyFormat( 4158 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4159 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4161 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4163 NoBinPacking); 4164 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4165 " .aaaaaaaaaaaaaaaaaa();", 4166 NoBinPacking); 4167 verifyFormat("void f() {\n" 4168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4169 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4170 "}", 4171 NoBinPacking); 4172 4173 verifyFormat( 4174 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4175 " aaaaaaaaaaaa,\n" 4176 " aaaaaaaaaaaa);", 4177 NoBinPacking); 4178 verifyFormat( 4179 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4180 " ddddddddddddddddddddddddddddd),\n" 4181 " test);", 4182 NoBinPacking); 4183 4184 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4185 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4186 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4187 " aaaaaaaaaaaaaaaaaa;", 4188 NoBinPacking); 4189 verifyFormat("a(\"a\"\n" 4190 " \"a\",\n" 4191 " a);"); 4192 4193 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4194 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4195 " aaaaaaaaa,\n" 4196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4197 NoBinPacking); 4198 verifyFormat( 4199 "void f() {\n" 4200 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4201 " .aaaaaaa();\n" 4202 "}", 4203 NoBinPacking); 4204 verifyFormat( 4205 "template <class SomeType, class SomeOtherType>\n" 4206 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4207 NoBinPacking); 4208 } 4209 4210 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4211 FormatStyle Style = getLLVMStyleWithColumns(15); 4212 Style.ExperimentalAutoDetectBinPacking = true; 4213 EXPECT_EQ("aaa(aaaa,\n" 4214 " aaaa,\n" 4215 " aaaa);\n" 4216 "aaa(aaaa,\n" 4217 " aaaa,\n" 4218 " aaaa);", 4219 format("aaa(aaaa,\n" // one-per-line 4220 " aaaa,\n" 4221 " aaaa );\n" 4222 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4223 Style)); 4224 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4225 " aaaa);\n" 4226 "aaa(aaaa, aaaa,\n" 4227 " aaaa);", 4228 format("aaa(aaaa, aaaa,\n" // bin-packed 4229 " aaaa );\n" 4230 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4231 Style)); 4232 } 4233 4234 TEST_F(FormatTest, FormatsBuilderPattern) { 4235 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4236 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4237 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4238 " .StartsWith(\".init\", ORDER_INIT)\n" 4239 " .StartsWith(\".fini\", ORDER_FINI)\n" 4240 " .StartsWith(\".hash\", ORDER_HASH)\n" 4241 " .Default(ORDER_TEXT);\n"); 4242 4243 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4244 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4245 verifyFormat( 4246 "aaaaaaa->aaaaaaa\n" 4247 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4249 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4250 verifyFormat( 4251 "aaaaaaa->aaaaaaa\n" 4252 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4253 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4254 verifyFormat( 4255 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4256 " aaaaaaaaaaaaaa);"); 4257 verifyFormat( 4258 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4259 " aaaaaa->aaaaaaaaaaaa()\n" 4260 " ->aaaaaaaaaaaaaaaa(\n" 4261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4262 " ->aaaaaaaaaaaaaaaaa();"); 4263 verifyGoogleFormat( 4264 "void f() {\n" 4265 " someo->Add((new util::filetools::Handler(dir))\n" 4266 " ->OnEvent1(NewPermanentCallback(\n" 4267 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4268 " ->OnEvent2(NewPermanentCallback(\n" 4269 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4270 " ->OnEvent3(NewPermanentCallback(\n" 4271 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4272 " ->OnEvent5(NewPermanentCallback(\n" 4273 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4274 " ->OnEvent6(NewPermanentCallback(\n" 4275 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4276 "}"); 4277 4278 verifyFormat( 4279 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4280 verifyFormat("aaaaaaaaaaaaaaa()\n" 4281 " .aaaaaaaaaaaaaaa()\n" 4282 " .aaaaaaaaaaaaaaa()\n" 4283 " .aaaaaaaaaaaaaaa()\n" 4284 " .aaaaaaaaaaaaaaa();"); 4285 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4286 " .aaaaaaaaaaaaaaa()\n" 4287 " .aaaaaaaaaaaaaaa()\n" 4288 " .aaaaaaaaaaaaaaa();"); 4289 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4290 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4291 " .aaaaaaaaaaaaaaa();"); 4292 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4293 " ->aaaaaaaaaaaaaae(0)\n" 4294 " ->aaaaaaaaaaaaaaa();"); 4295 4296 // Don't linewrap after very short segments. 4297 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4298 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4299 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4300 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4301 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4303 verifyFormat("aaa()\n" 4304 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4307 4308 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4309 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4310 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4311 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4312 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4314 4315 // Prefer not to break after empty parentheses. 4316 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4317 " First->LastNewlineOffset);"); 4318 4319 // Prefer not to create "hanging" indents. 4320 verifyFormat( 4321 "return !soooooooooooooome_map\n" 4322 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4323 " .second;"); 4324 verifyFormat( 4325 "return aaaaaaaaaaaaaaaa\n" 4326 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4327 " .aaaa(aaaaaaaaaaaaaa);"); 4328 // No hanging indent here. 4329 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4331 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4333 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4334 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4335 getLLVMStyleWithColumns(60)); 4336 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4337 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4338 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4339 getLLVMStyleWithColumns(59)); 4340 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4342 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4343 } 4344 4345 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4346 verifyFormat( 4347 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4348 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4349 verifyFormat( 4350 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4351 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4352 4353 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4354 " ccccccccccccccccccccccccc) {\n}"); 4355 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4356 " ccccccccccccccccccccccccc) {\n}"); 4357 4358 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4359 " ccccccccccccccccccccccccc) {\n}"); 4360 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4361 " ccccccccccccccccccccccccc) {\n}"); 4362 4363 verifyFormat( 4364 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4365 " ccccccccccccccccccccccccc) {\n}"); 4366 verifyFormat( 4367 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4368 " ccccccccccccccccccccccccc) {\n}"); 4369 4370 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4371 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4372 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4373 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4374 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4375 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4376 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4377 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4378 4379 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4380 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4381 " aaaaaaaaaaaaaaa != aa) {\n}"); 4382 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4383 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4384 " aaaaaaaaaaaaaaa != aa) {\n}"); 4385 } 4386 4387 TEST_F(FormatTest, BreaksAfterAssignments) { 4388 verifyFormat( 4389 "unsigned Cost =\n" 4390 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4391 " SI->getPointerAddressSpaceee());\n"); 4392 verifyFormat( 4393 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4394 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4395 4396 verifyFormat( 4397 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4398 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4399 verifyFormat("unsigned OriginalStartColumn =\n" 4400 " SourceMgr.getSpellingColumnNumber(\n" 4401 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4402 " 1;"); 4403 } 4404 4405 TEST_F(FormatTest, AlignsAfterAssignments) { 4406 verifyFormat( 4407 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4408 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4409 verifyFormat( 4410 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4411 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4412 verifyFormat( 4413 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4414 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4415 verifyFormat( 4416 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4417 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4418 verifyFormat( 4419 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4420 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4421 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4422 } 4423 4424 TEST_F(FormatTest, AlignsAfterReturn) { 4425 verifyFormat( 4426 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4427 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4428 verifyFormat( 4429 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4430 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4431 verifyFormat( 4432 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4433 " aaaaaaaaaaaaaaaaaaaaaa();"); 4434 verifyFormat( 4435 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4436 " aaaaaaaaaaaaaaaaaaaaaa());"); 4437 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4439 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4442 verifyFormat("return\n" 4443 " // true if code is one of a or b.\n" 4444 " code == a || code == b;"); 4445 } 4446 4447 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4448 verifyFormat( 4449 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4450 " aaaaaaaaa aaaaaaa) {}"); 4451 verifyFormat( 4452 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4453 " aaaaaaaaaaa aaaaaaaaa);"); 4454 verifyFormat( 4455 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4456 " aaaaaaaaaaaaaaaaaaaaa));"); 4457 FormatStyle Style = getLLVMStyle(); 4458 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4459 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4460 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4461 Style); 4462 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4463 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4464 Style); 4465 verifyFormat("SomeLongVariableName->someFunction(\n" 4466 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4467 Style); 4468 verifyFormat( 4469 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4470 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4471 Style); 4472 verifyFormat( 4473 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4474 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4475 Style); 4476 verifyFormat( 4477 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4478 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4479 Style); 4480 4481 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4482 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4483 " b));", 4484 Style); 4485 4486 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4487 Style.BinPackArguments = false; 4488 Style.BinPackParameters = false; 4489 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4490 " aaaaaaaaaaa aaaaaaaa,\n" 4491 " aaaaaaaaa aaaaaaa,\n" 4492 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4493 Style); 4494 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4495 " aaaaaaaaaaa aaaaaaaaa,\n" 4496 " aaaaaaaaaaa aaaaaaaaa,\n" 4497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4498 Style); 4499 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4500 " aaaaaaaaaaaaaaa,\n" 4501 " aaaaaaaaaaaaaaaaaaaaa,\n" 4502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4503 Style); 4504 verifyFormat( 4505 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4506 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4507 Style); 4508 verifyFormat( 4509 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4510 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4511 Style); 4512 verifyFormat( 4513 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4514 " aaaaaaaaaaaaaaaaaaaaa(\n" 4515 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4516 " aaaaaaaaaaaaaaaa);", 4517 Style); 4518 verifyFormat( 4519 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4520 " aaaaaaaaaaaaaaaaaaaaa(\n" 4521 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4522 " aaaaaaaaaaaaaaaa);", 4523 Style); 4524 } 4525 4526 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4527 FormatStyle Style = getLLVMStyleWithColumns(40); 4528 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4529 " bbbbbbbbbbbbbbbbbbbbbb);", 4530 Style); 4531 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4532 Style.AlignOperands = false; 4533 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4534 " bbbbbbbbbbbbbbbbbbbbbb);", 4535 Style); 4536 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4537 Style.AlignOperands = true; 4538 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4539 " bbbbbbbbbbbbbbbbbbbbbb);", 4540 Style); 4541 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4542 Style.AlignOperands = false; 4543 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4544 " bbbbbbbbbbbbbbbbbbbbbb);", 4545 Style); 4546 } 4547 4548 TEST_F(FormatTest, BreaksConditionalExpressions) { 4549 verifyFormat( 4550 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4551 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4552 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4553 verifyFormat( 4554 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4556 verifyFormat( 4557 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4558 " : aaaaaaaaaaaaa);"); 4559 verifyFormat( 4560 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4561 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4562 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4563 " aaaaaaaaaaaaa);"); 4564 verifyFormat( 4565 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4566 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4567 " aaaaaaaaaaaaa);"); 4568 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4569 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4571 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4573 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4575 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4577 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4579 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4580 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4582 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4584 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4585 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4588 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4590 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4591 " : aaaaaaaaaaaaaaaa;"); 4592 verifyFormat( 4593 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4594 " ? aaaaaaaaaaaaaaa\n" 4595 " : aaaaaaaaaaaaaaa;"); 4596 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4597 " aaaaaaaaa\n" 4598 " ? b\n" 4599 " : c);"); 4600 verifyFormat("return aaaa == bbbb\n" 4601 " // comment\n" 4602 " ? aaaa\n" 4603 " : bbbb;"); 4604 verifyFormat("unsigned Indent =\n" 4605 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4606 " ? IndentForLevel[TheLine.Level]\n" 4607 " : TheLine * 2,\n" 4608 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4609 getLLVMStyleWithColumns(70)); 4610 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4611 " ? aaaaaaaaaaaaaaa\n" 4612 " : bbbbbbbbbbbbbbb //\n" 4613 " ? ccccccccccccccc\n" 4614 " : ddddddddddddddd;"); 4615 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4616 " ? aaaaaaaaaaaaaaa\n" 4617 " : (bbbbbbbbbbbbbbb //\n" 4618 " ? ccccccccccccccc\n" 4619 " : ddddddddddddddd);"); 4620 verifyFormat( 4621 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4622 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4623 " aaaaaaaaaaaaaaaaaaaaa +\n" 4624 " aaaaaaaaaaaaaaaaaaaaa\n" 4625 " : aaaaaaaaaa;"); 4626 verifyFormat( 4627 "aaaaaa = aaaaaaaaaaaa\n" 4628 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4629 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4630 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4631 4632 FormatStyle NoBinPacking = getLLVMStyle(); 4633 NoBinPacking.BinPackArguments = false; 4634 verifyFormat( 4635 "void f() {\n" 4636 " g(aaa,\n" 4637 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4639 " ? aaaaaaaaaaaaaaa\n" 4640 " : aaaaaaaaaaaaaaa);\n" 4641 "}", 4642 NoBinPacking); 4643 verifyFormat( 4644 "void f() {\n" 4645 " g(aaa,\n" 4646 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4648 " ?: aaaaaaaaaaaaaaa);\n" 4649 "}", 4650 NoBinPacking); 4651 4652 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4653 " // comment.\n" 4654 " ccccccccccccccccccccccccccccccccccccccc\n" 4655 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4656 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4657 4658 // Assignments in conditional expressions. Apparently not uncommon :-(. 4659 verifyFormat("return a != b\n" 4660 " // comment\n" 4661 " ? a = b\n" 4662 " : a = b;"); 4663 verifyFormat("return a != b\n" 4664 " // comment\n" 4665 " ? a = a != b\n" 4666 " // comment\n" 4667 " ? a = b\n" 4668 " : a\n" 4669 " : a;\n"); 4670 verifyFormat("return a != b\n" 4671 " // comment\n" 4672 " ? a\n" 4673 " : a = a != b\n" 4674 " // comment\n" 4675 " ? a = b\n" 4676 " : a;"); 4677 } 4678 4679 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4680 FormatStyle Style = getLLVMStyle(); 4681 Style.BreakBeforeTernaryOperators = false; 4682 Style.ColumnLimit = 70; 4683 verifyFormat( 4684 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4686 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4687 Style); 4688 verifyFormat( 4689 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4691 Style); 4692 verifyFormat( 4693 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4694 " aaaaaaaaaaaaa);", 4695 Style); 4696 verifyFormat( 4697 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4698 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4700 " aaaaaaaaaaaaa);", 4701 Style); 4702 verifyFormat( 4703 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4704 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4705 " aaaaaaaaaaaaa);", 4706 Style); 4707 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4712 Style); 4713 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4716 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4720 Style); 4721 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4725 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4726 Style); 4727 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4729 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4730 Style); 4731 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4735 Style); 4736 verifyFormat( 4737 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4738 " aaaaaaaaaaaaaaa :\n" 4739 " aaaaaaaaaaaaaaa;", 4740 Style); 4741 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4742 " aaaaaaaaa ?\n" 4743 " b :\n" 4744 " c);", 4745 Style); 4746 verifyFormat( 4747 "unsigned Indent =\n" 4748 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4749 " IndentForLevel[TheLine.Level] :\n" 4750 " TheLine * 2,\n" 4751 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4752 Style); 4753 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4754 " aaaaaaaaaaaaaaa :\n" 4755 " bbbbbbbbbbbbbbb ? //\n" 4756 " ccccccccccccccc :\n" 4757 " ddddddddddddddd;", 4758 Style); 4759 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4760 " aaaaaaaaaaaaaaa :\n" 4761 " (bbbbbbbbbbbbbbb ? //\n" 4762 " ccccccccccccccc :\n" 4763 " ddddddddddddddd);", 4764 Style); 4765 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4766 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4767 " ccccccccccccccccccccccccccc;", 4768 Style); 4769 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4770 " aaaaa :\n" 4771 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4772 Style); 4773 } 4774 4775 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4776 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4777 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4778 verifyFormat("bool a = true, b = false;"); 4779 4780 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4782 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4783 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4784 verifyFormat( 4785 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4786 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4787 " d = e && f;"); 4788 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4789 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4790 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4791 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4792 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4793 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4794 4795 FormatStyle Style = getGoogleStyle(); 4796 Style.PointerAlignment = FormatStyle::PAS_Left; 4797 Style.DerivePointerAlignment = false; 4798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4799 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4800 " *b = bbbbbbbbbbbbbbbbbbb;", 4801 Style); 4802 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4803 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4804 Style); 4805 verifyFormat("vector<int*> a, b;", Style); 4806 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4807 } 4808 4809 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4810 verifyFormat("arr[foo ? bar : baz];"); 4811 verifyFormat("f()[foo ? bar : baz];"); 4812 verifyFormat("(a + b)[foo ? bar : baz];"); 4813 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4814 } 4815 4816 TEST_F(FormatTest, AlignsStringLiterals) { 4817 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4818 " \"short literal\");"); 4819 verifyFormat( 4820 "looooooooooooooooooooooooongFunction(\n" 4821 " \"short literal\"\n" 4822 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4823 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4824 " \" string literals\",\n" 4825 " and, other, parameters);"); 4826 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4827 " \"5678\";", 4828 format("fun + \"1243\" /* comment */\n" 4829 " \"5678\";", 4830 getLLVMStyleWithColumns(28))); 4831 EXPECT_EQ( 4832 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4833 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4834 " \"aaaaaaaaaaaaaaaa\";", 4835 format("aaaaaa =" 4836 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4837 "aaaaaaaaaaaaaaaaaaaaa\" " 4838 "\"aaaaaaaaaaaaaaaa\";")); 4839 verifyFormat("a = a + \"a\"\n" 4840 " \"a\"\n" 4841 " \"a\";"); 4842 verifyFormat("f(\"a\", \"b\"\n" 4843 " \"c\");"); 4844 4845 verifyFormat( 4846 "#define LL_FORMAT \"ll\"\n" 4847 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4848 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4849 4850 verifyFormat("#define A(X) \\\n" 4851 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4852 " \"ccccc\"", 4853 getLLVMStyleWithColumns(23)); 4854 verifyFormat("#define A \"def\"\n" 4855 "f(\"abc\" A \"ghi\"\n" 4856 " \"jkl\");"); 4857 4858 verifyFormat("f(L\"a\"\n" 4859 " L\"b\");"); 4860 verifyFormat("#define A(X) \\\n" 4861 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4862 " L\"ccccc\"", 4863 getLLVMStyleWithColumns(25)); 4864 4865 verifyFormat("f(@\"a\"\n" 4866 " @\"b\");"); 4867 verifyFormat("NSString s = @\"a\"\n" 4868 " @\"b\"\n" 4869 " @\"c\";"); 4870 verifyFormat("NSString s = @\"a\"\n" 4871 " \"b\"\n" 4872 " \"c\";"); 4873 } 4874 4875 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4876 FormatStyle Style = getLLVMStyle(); 4877 // No declarations or definitions should be moved to own line. 4878 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4879 verifyFormat("class A {\n" 4880 " int f() { return 1; }\n" 4881 " int g();\n" 4882 "};\n" 4883 "int f() { return 1; }\n" 4884 "int g();\n", 4885 Style); 4886 4887 // All declarations and definitions should have the return type moved to its 4888 // own 4889 // line. 4890 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4891 verifyFormat("class E {\n" 4892 " int\n" 4893 " f() {\n" 4894 " return 1;\n" 4895 " }\n" 4896 " int\n" 4897 " g();\n" 4898 "};\n" 4899 "int\n" 4900 "f() {\n" 4901 " return 1;\n" 4902 "}\n" 4903 "int\n" 4904 "g();\n", 4905 Style); 4906 4907 // Top-level definitions, and no kinds of declarations should have the 4908 // return type moved to its own line. 4909 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4910 verifyFormat("class B {\n" 4911 " int f() { return 1; }\n" 4912 " int g();\n" 4913 "};\n" 4914 "int\n" 4915 "f() {\n" 4916 " return 1;\n" 4917 "}\n" 4918 "int g();\n", 4919 Style); 4920 4921 // Top-level definitions and declarations should have the return type moved 4922 // to its own line. 4923 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4924 verifyFormat("class C {\n" 4925 " int f() { return 1; }\n" 4926 " int g();\n" 4927 "};\n" 4928 "int\n" 4929 "f() {\n" 4930 " return 1;\n" 4931 "}\n" 4932 "int\n" 4933 "g();\n", 4934 Style); 4935 4936 // All definitions should have the return type moved to its own line, but no 4937 // kinds of declarations. 4938 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4939 verifyFormat("class D {\n" 4940 " int\n" 4941 " f() {\n" 4942 " return 1;\n" 4943 " }\n" 4944 " int g();\n" 4945 "};\n" 4946 "int\n" 4947 "f() {\n" 4948 " return 1;\n" 4949 "}\n" 4950 "int g();\n", 4951 Style); 4952 verifyFormat("const char *\n" 4953 "f(void) {\n" // Break here. 4954 " return \"\";\n" 4955 "}\n" 4956 "const char *bar(void);\n", // No break here. 4957 Style); 4958 verifyFormat("template <class T>\n" 4959 "T *\n" 4960 "f(T &c) {\n" // Break here. 4961 " return NULL;\n" 4962 "}\n" 4963 "template <class T> T *f(T &c);\n", // No break here. 4964 Style); 4965 verifyFormat("class C {\n" 4966 " int\n" 4967 " operator+() {\n" 4968 " return 1;\n" 4969 " }\n" 4970 " int\n" 4971 " operator()() {\n" 4972 " return 1;\n" 4973 " }\n" 4974 "};\n", 4975 Style); 4976 verifyFormat("void\n" 4977 "A::operator()() {}\n" 4978 "void\n" 4979 "A::operator>>() {}\n" 4980 "void\n" 4981 "A::operator+() {}\n", 4982 Style); 4983 verifyFormat("void *operator new(std::size_t s);", // No break here. 4984 Style); 4985 verifyFormat("void *\n" 4986 "operator new(std::size_t s) {}", 4987 Style); 4988 verifyFormat("void *\n" 4989 "operator delete[](void *ptr) {}", 4990 Style); 4991 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4992 verifyFormat("const char *\n" 4993 "f(void)\n" // Break here. 4994 "{\n" 4995 " return \"\";\n" 4996 "}\n" 4997 "const char *bar(void);\n", // No break here. 4998 Style); 4999 verifyFormat("template <class T>\n" 5000 "T *\n" // Problem here: no line break 5001 "f(T &c)\n" // Break here. 5002 "{\n" 5003 " return NULL;\n" 5004 "}\n" 5005 "template <class T> T *f(T &c);\n", // No break here. 5006 Style); 5007 } 5008 5009 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5010 FormatStyle NoBreak = getLLVMStyle(); 5011 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5012 FormatStyle Break = getLLVMStyle(); 5013 Break.AlwaysBreakBeforeMultilineStrings = true; 5014 verifyFormat("aaaa = \"bbbb\"\n" 5015 " \"cccc\";", 5016 NoBreak); 5017 verifyFormat("aaaa =\n" 5018 " \"bbbb\"\n" 5019 " \"cccc\";", 5020 Break); 5021 verifyFormat("aaaa(\"bbbb\"\n" 5022 " \"cccc\");", 5023 NoBreak); 5024 verifyFormat("aaaa(\n" 5025 " \"bbbb\"\n" 5026 " \"cccc\");", 5027 Break); 5028 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5029 " \"cccc\");", 5030 NoBreak); 5031 verifyFormat("aaaa(qqq,\n" 5032 " \"bbbb\"\n" 5033 " \"cccc\");", 5034 Break); 5035 verifyFormat("aaaa(qqq,\n" 5036 " L\"bbbb\"\n" 5037 " L\"cccc\");", 5038 Break); 5039 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5040 " \"bbbb\"));", 5041 Break); 5042 verifyFormat("string s = someFunction(\n" 5043 " \"abc\"\n" 5044 " \"abc\");", 5045 Break); 5046 5047 // As we break before unary operators, breaking right after them is bad. 5048 verifyFormat("string foo = abc ? \"x\"\n" 5049 " \"blah blah blah blah blah blah\"\n" 5050 " : \"y\";", 5051 Break); 5052 5053 // Don't break if there is no column gain. 5054 verifyFormat("f(\"aaaa\"\n" 5055 " \"bbbb\");", 5056 Break); 5057 5058 // Treat literals with escaped newlines like multi-line string literals. 5059 EXPECT_EQ("x = \"a\\\n" 5060 "b\\\n" 5061 "c\";", 5062 format("x = \"a\\\n" 5063 "b\\\n" 5064 "c\";", 5065 NoBreak)); 5066 EXPECT_EQ("xxxx =\n" 5067 " \"a\\\n" 5068 "b\\\n" 5069 "c\";", 5070 format("xxxx = \"a\\\n" 5071 "b\\\n" 5072 "c\";", 5073 Break)); 5074 5075 // Exempt ObjC strings for now. 5076 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5077 " @\"bbbb\";", 5078 format("NSString *const kString = @\"aaaa\"\n" 5079 "@\"bbbb\";", 5080 Break)); 5081 5082 Break.ColumnLimit = 0; 5083 verifyFormat("const char *hello = \"hello llvm\";", Break); 5084 } 5085 5086 TEST_F(FormatTest, AlignsPipes) { 5087 verifyFormat( 5088 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5089 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5090 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5091 verifyFormat( 5092 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5093 " << aaaaaaaaaaaaaaaaaaaa;"); 5094 verifyFormat( 5095 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5096 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5097 verifyFormat( 5098 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5099 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5100 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5101 verifyFormat( 5102 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5103 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5104 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5105 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5108 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5109 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5110 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5111 verifyFormat( 5112 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5114 5115 verifyFormat("return out << \"somepacket = {\\n\"\n" 5116 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5117 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5118 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5119 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5120 " << \"}\";"); 5121 5122 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5123 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5124 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5125 verifyFormat( 5126 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5127 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5128 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5129 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5130 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5131 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5132 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5133 verifyFormat( 5134 "void f() {\n" 5135 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5136 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5137 "}"); 5138 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5139 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5140 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5142 " aaaaaaaaaaaaaaaaaaaaa)\n" 5143 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5144 verifyFormat("LOG_IF(aaa == //\n" 5145 " bbb)\n" 5146 " << a << b;"); 5147 5148 // Breaking before the first "<<" is generally not desirable. 5149 verifyFormat( 5150 "llvm::errs()\n" 5151 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5152 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5153 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5154 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5155 getLLVMStyleWithColumns(70)); 5156 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5157 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5158 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5159 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5160 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5161 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5162 getLLVMStyleWithColumns(70)); 5163 5164 // But sometimes, breaking before the first "<<" is desirable. 5165 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5166 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5167 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5168 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5169 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5170 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5171 " << BEF << IsTemplate << Description << E->getType();"); 5172 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5173 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5175 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5176 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5178 " << aaa;"); 5179 5180 verifyFormat( 5181 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5182 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5183 5184 // Incomplete string literal. 5185 EXPECT_EQ("llvm::errs() << \"\n" 5186 " << a;", 5187 format("llvm::errs() << \"\n<<a;")); 5188 5189 verifyFormat("void f() {\n" 5190 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5191 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5192 "}"); 5193 5194 // Handle 'endl'. 5195 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5196 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5197 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5198 5199 // Handle '\n'. 5200 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5201 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5202 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5203 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5204 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5205 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5206 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5207 } 5208 5209 TEST_F(FormatTest, UnderstandsEquals) { 5210 verifyFormat( 5211 "aaaaaaaaaaaaaaaaa =\n" 5212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5213 verifyFormat( 5214 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5216 verifyFormat( 5217 "if (a) {\n" 5218 " f();\n" 5219 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5221 "}"); 5222 5223 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5224 " 100000000 + 10000000) {\n}"); 5225 } 5226 5227 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5228 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5229 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5230 5231 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5232 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5233 5234 verifyFormat( 5235 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5236 " Parameter2);"); 5237 5238 verifyFormat( 5239 "ShortObject->shortFunction(\n" 5240 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5241 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5242 5243 verifyFormat("loooooooooooooongFunction(\n" 5244 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5245 5246 verifyFormat( 5247 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5248 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5249 5250 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5251 " .WillRepeatedly(Return(SomeValue));"); 5252 verifyFormat("void f() {\n" 5253 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5254 " .Times(2)\n" 5255 " .WillRepeatedly(Return(SomeValue));\n" 5256 "}"); 5257 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5258 " ccccccccccccccccccccccc);"); 5259 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5261 " .aaaaa(aaaaa),\n" 5262 " aaaaaaaaaaaaaaaaaaaaa);"); 5263 verifyFormat("void f() {\n" 5264 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5265 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5266 "}"); 5267 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5269 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5270 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5271 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5272 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5273 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5274 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5275 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5276 "}"); 5277 5278 // Here, it is not necessary to wrap at "." or "->". 5279 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5280 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5281 verifyFormat( 5282 "aaaaaaaaaaa->aaaaaaaaa(\n" 5283 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5284 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5285 5286 verifyFormat( 5287 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5289 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5290 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5291 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5292 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5293 5294 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5296 " .a();"); 5297 5298 FormatStyle NoBinPacking = getLLVMStyle(); 5299 NoBinPacking.BinPackParameters = false; 5300 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5301 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5302 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5303 " aaaaaaaaaaaaaaaaaaa,\n" 5304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5305 NoBinPacking); 5306 5307 // If there is a subsequent call, change to hanging indentation. 5308 verifyFormat( 5309 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5310 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5311 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5312 verifyFormat( 5313 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5314 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5315 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5317 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5318 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5319 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5320 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5321 } 5322 5323 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5324 verifyFormat("template <typename T>\n" 5325 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5326 verifyFormat("template <typename T>\n" 5327 "// T should be one of {A, B}.\n" 5328 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5329 verifyFormat( 5330 "template <typename T>\n" 5331 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5332 verifyFormat("template <typename T>\n" 5333 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5334 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5335 verifyFormat( 5336 "template <typename T>\n" 5337 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5338 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5339 verifyFormat( 5340 "template <typename T>\n" 5341 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5344 verifyFormat("template <typename T>\n" 5345 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5346 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5347 verifyFormat( 5348 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5349 " typename T4 = char>\n" 5350 "void f();"); 5351 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5352 " template <typename> class cccccccccccccccccccccc,\n" 5353 " typename ddddddddddddd>\n" 5354 "class C {};"); 5355 verifyFormat( 5356 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5358 5359 verifyFormat("void f() {\n" 5360 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5361 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5362 "}"); 5363 5364 verifyFormat("template <typename T> class C {};"); 5365 verifyFormat("template <typename T> void f();"); 5366 verifyFormat("template <typename T> void f() {}"); 5367 verifyFormat( 5368 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5369 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5370 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5371 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5374 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5375 getLLVMStyleWithColumns(72)); 5376 EXPECT_EQ("static_cast<A< //\n" 5377 " B> *>(\n" 5378 "\n" 5379 " );", 5380 format("static_cast<A<//\n" 5381 " B>*>(\n" 5382 "\n" 5383 " );")); 5384 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5385 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5386 5387 FormatStyle AlwaysBreak = getLLVMStyle(); 5388 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5389 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5390 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5391 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5392 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5393 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5394 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5395 verifyFormat("template <template <typename> class Fooooooo,\n" 5396 " template <typename> class Baaaaaaar>\n" 5397 "struct C {};", 5398 AlwaysBreak); 5399 verifyFormat("template <typename T> // T can be A, B or C.\n" 5400 "struct C {};", 5401 AlwaysBreak); 5402 verifyFormat("template <enum E> class A {\n" 5403 "public:\n" 5404 " E *f();\n" 5405 "};"); 5406 } 5407 5408 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5409 verifyFormat( 5410 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5412 verifyFormat( 5413 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5416 5417 // FIXME: Should we have the extra indent after the second break? 5418 verifyFormat( 5419 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5422 5423 verifyFormat( 5424 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5425 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5426 5427 // Breaking at nested name specifiers is generally not desirable. 5428 verifyFormat( 5429 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5431 5432 verifyFormat( 5433 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5435 " aaaaaaaaaaaaaaaaaaaaa);", 5436 getLLVMStyleWithColumns(74)); 5437 5438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5440 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5441 } 5442 5443 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5444 verifyFormat("A<int> a;"); 5445 verifyFormat("A<A<A<int>>> a;"); 5446 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5447 verifyFormat("bool x = a < 1 || 2 > a;"); 5448 verifyFormat("bool x = 5 < f<int>();"); 5449 verifyFormat("bool x = f<int>() > 5;"); 5450 verifyFormat("bool x = 5 < a<int>::x;"); 5451 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5452 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5453 5454 verifyGoogleFormat("A<A<int>> a;"); 5455 verifyGoogleFormat("A<A<A<int>>> a;"); 5456 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5457 verifyGoogleFormat("A<A<int> > a;"); 5458 verifyGoogleFormat("A<A<A<int> > > a;"); 5459 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5460 verifyGoogleFormat("A<::A<int>> a;"); 5461 verifyGoogleFormat("A<::A> a;"); 5462 verifyGoogleFormat("A< ::A> a;"); 5463 verifyGoogleFormat("A< ::A<int> > a;"); 5464 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5465 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5466 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5467 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5468 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5469 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5470 5471 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5472 5473 verifyFormat("test >> a >> b;"); 5474 verifyFormat("test << a >> b;"); 5475 5476 verifyFormat("f<int>();"); 5477 verifyFormat("template <typename T> void f() {}"); 5478 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5479 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5480 "sizeof(char)>::type>;"); 5481 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5482 verifyFormat("f(a.operator()<A>());"); 5483 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5484 " .template operator()<A>());", 5485 getLLVMStyleWithColumns(35)); 5486 5487 // Not template parameters. 5488 verifyFormat("return a < b && c > d;"); 5489 verifyFormat("void f() {\n" 5490 " while (a < b && c > d) {\n" 5491 " }\n" 5492 "}"); 5493 verifyFormat("template <typename... Types>\n" 5494 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5495 5496 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5498 getLLVMStyleWithColumns(60)); 5499 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5500 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5501 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5502 } 5503 5504 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5505 verifyFormat("COMPARE(a, ==, b);"); 5506 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5507 } 5508 5509 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5510 verifyFormat("int A::*x;"); 5511 verifyFormat("int (S::*func)(void *);"); 5512 verifyFormat("void f() { int (S::*func)(void *); }"); 5513 verifyFormat("typedef bool *(Class::*Member)() const;"); 5514 verifyFormat("void f() {\n" 5515 " (a->*f)();\n" 5516 " a->*x;\n" 5517 " (a.*f)();\n" 5518 " ((*a).*f)();\n" 5519 " a.*x;\n" 5520 "}"); 5521 verifyFormat("void f() {\n" 5522 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5523 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5524 "}"); 5525 verifyFormat( 5526 "(aaaaaaaaaa->*bbbbbbb)(\n" 5527 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5528 FormatStyle Style = getLLVMStyle(); 5529 Style.PointerAlignment = FormatStyle::PAS_Left; 5530 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5531 } 5532 5533 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5534 verifyFormat("int a = -2;"); 5535 verifyFormat("f(-1, -2, -3);"); 5536 verifyFormat("a[-1] = 5;"); 5537 verifyFormat("int a = 5 + -2;"); 5538 verifyFormat("if (i == -1) {\n}"); 5539 verifyFormat("if (i != -1) {\n}"); 5540 verifyFormat("if (i > -1) {\n}"); 5541 verifyFormat("if (i < -1) {\n}"); 5542 verifyFormat("++(a->f());"); 5543 verifyFormat("--(a->f());"); 5544 verifyFormat("(a->f())++;"); 5545 verifyFormat("a[42]++;"); 5546 verifyFormat("if (!(a->f())) {\n}"); 5547 5548 verifyFormat("a-- > b;"); 5549 verifyFormat("b ? -a : c;"); 5550 verifyFormat("n * sizeof char16;"); 5551 verifyFormat("n * alignof char16;", getGoogleStyle()); 5552 verifyFormat("sizeof(char);"); 5553 verifyFormat("alignof(char);", getGoogleStyle()); 5554 5555 verifyFormat("return -1;"); 5556 verifyFormat("switch (a) {\n" 5557 "case -1:\n" 5558 " break;\n" 5559 "}"); 5560 verifyFormat("#define X -1"); 5561 verifyFormat("#define X -kConstant"); 5562 5563 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5564 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5565 5566 verifyFormat("int a = /* confusing comment */ -1;"); 5567 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5568 verifyFormat("int a = i /* confusing comment */++;"); 5569 } 5570 5571 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5572 verifyFormat("if (!aaaaaaaaaa( // break\n" 5573 " aaaaa)) {\n" 5574 "}"); 5575 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5576 " aaaaa));"); 5577 verifyFormat("*aaa = aaaaaaa( // break\n" 5578 " bbbbbb);"); 5579 } 5580 5581 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5582 verifyFormat("bool operator<();"); 5583 verifyFormat("bool operator>();"); 5584 verifyFormat("bool operator=();"); 5585 verifyFormat("bool operator==();"); 5586 verifyFormat("bool operator!=();"); 5587 verifyFormat("int operator+();"); 5588 verifyFormat("int operator++();"); 5589 verifyFormat("bool operator,();"); 5590 verifyFormat("bool operator();"); 5591 verifyFormat("bool operator()();"); 5592 verifyFormat("bool operator[]();"); 5593 verifyFormat("operator bool();"); 5594 verifyFormat("operator int();"); 5595 verifyFormat("operator void *();"); 5596 verifyFormat("operator SomeType<int>();"); 5597 verifyFormat("operator SomeType<int, int>();"); 5598 verifyFormat("operator SomeType<SomeType<int>>();"); 5599 verifyFormat("void *operator new(std::size_t size);"); 5600 verifyFormat("void *operator new[](std::size_t size);"); 5601 verifyFormat("void operator delete(void *ptr);"); 5602 verifyFormat("void operator delete[](void *ptr);"); 5603 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5604 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5605 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5606 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5607 5608 verifyFormat( 5609 "ostream &operator<<(ostream &OutputStream,\n" 5610 " SomeReallyLongType WithSomeReallyLongValue);"); 5611 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5612 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5613 " return left.group < right.group;\n" 5614 "}"); 5615 verifyFormat("SomeType &operator=(const SomeType &S);"); 5616 verifyFormat("f.template operator()<int>();"); 5617 5618 verifyGoogleFormat("operator void*();"); 5619 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5620 verifyGoogleFormat("operator ::A();"); 5621 5622 verifyFormat("using A::operator+;"); 5623 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5624 "int i;"); 5625 } 5626 5627 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5628 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5629 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5630 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5631 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5632 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5633 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5634 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5635 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5636 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5637 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5638 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5639 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5640 5641 FormatStyle AlignLeft = getLLVMStyle(); 5642 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5643 verifyFormat("void A::b() && {}", AlignLeft); 5644 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5645 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5646 AlignLeft); 5647 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5648 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5649 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5650 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5651 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5652 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5653 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5654 5655 FormatStyle Spaces = getLLVMStyle(); 5656 Spaces.SpacesInCStyleCastParentheses = true; 5657 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5658 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5659 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5660 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5661 5662 Spaces.SpacesInCStyleCastParentheses = false; 5663 Spaces.SpacesInParentheses = true; 5664 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5665 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5666 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5667 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5668 } 5669 5670 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5671 verifyFormat("void f() {\n" 5672 " A *a = new A;\n" 5673 " A *a = new (placement) A;\n" 5674 " delete a;\n" 5675 " delete (A *)a;\n" 5676 "}"); 5677 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5678 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5679 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5680 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5681 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5682 verifyFormat("delete[] h->p;"); 5683 } 5684 5685 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5686 verifyFormat("int *f(int *a) {}"); 5687 verifyFormat("int main(int argc, char **argv) {}"); 5688 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5689 verifyIndependentOfContext("f(a, *a);"); 5690 verifyFormat("void g() { f(*a); }"); 5691 verifyIndependentOfContext("int a = b * 10;"); 5692 verifyIndependentOfContext("int a = 10 * b;"); 5693 verifyIndependentOfContext("int a = b * c;"); 5694 verifyIndependentOfContext("int a += b * c;"); 5695 verifyIndependentOfContext("int a -= b * c;"); 5696 verifyIndependentOfContext("int a *= b * c;"); 5697 verifyIndependentOfContext("int a /= b * c;"); 5698 verifyIndependentOfContext("int a = *b;"); 5699 verifyIndependentOfContext("int a = *b * c;"); 5700 verifyIndependentOfContext("int a = b * *c;"); 5701 verifyIndependentOfContext("int a = b * (10);"); 5702 verifyIndependentOfContext("S << b * (10);"); 5703 verifyIndependentOfContext("return 10 * b;"); 5704 verifyIndependentOfContext("return *b * *c;"); 5705 verifyIndependentOfContext("return a & ~b;"); 5706 verifyIndependentOfContext("f(b ? *c : *d);"); 5707 verifyIndependentOfContext("int a = b ? *c : *d;"); 5708 verifyIndependentOfContext("*b = a;"); 5709 verifyIndependentOfContext("a * ~b;"); 5710 verifyIndependentOfContext("a * !b;"); 5711 verifyIndependentOfContext("a * +b;"); 5712 verifyIndependentOfContext("a * -b;"); 5713 verifyIndependentOfContext("a * ++b;"); 5714 verifyIndependentOfContext("a * --b;"); 5715 verifyIndependentOfContext("a[4] * b;"); 5716 verifyIndependentOfContext("a[a * a] = 1;"); 5717 verifyIndependentOfContext("f() * b;"); 5718 verifyIndependentOfContext("a * [self dostuff];"); 5719 verifyIndependentOfContext("int x = a * (a + b);"); 5720 verifyIndependentOfContext("(a *)(a + b);"); 5721 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5722 verifyIndependentOfContext("int *pa = (int *)&a;"); 5723 verifyIndependentOfContext("return sizeof(int **);"); 5724 verifyIndependentOfContext("return sizeof(int ******);"); 5725 verifyIndependentOfContext("return (int **&)a;"); 5726 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5727 verifyFormat("void f(Type (*parameter)[10]) {}"); 5728 verifyFormat("void f(Type (¶meter)[10]) {}"); 5729 verifyGoogleFormat("return sizeof(int**);"); 5730 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5731 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5732 verifyFormat("auto a = [](int **&, int ***) {};"); 5733 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5734 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5735 verifyFormat("[](const decltype(*a) &value) {}"); 5736 verifyFormat("decltype(a * b) F();"); 5737 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5738 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5739 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5740 verifyIndependentOfContext("int i{a * b};"); 5741 verifyIndependentOfContext("aaa && aaa->f();"); 5742 verifyIndependentOfContext("int x = ~*p;"); 5743 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5744 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5745 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5746 verifyFormat("void f() { f(a, c * d); }"); 5747 verifyFormat("void f() { f(new a(), c * d); }"); 5748 5749 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5750 5751 verifyIndependentOfContext("A<int *> a;"); 5752 verifyIndependentOfContext("A<int **> a;"); 5753 verifyIndependentOfContext("A<int *, int *> a;"); 5754 verifyIndependentOfContext("A<int *[]> a;"); 5755 verifyIndependentOfContext( 5756 "const char *const p = reinterpret_cast<const char *const>(q);"); 5757 verifyIndependentOfContext("A<int **, int **> a;"); 5758 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5759 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5760 verifyFormat("for (; a && b;) {\n}"); 5761 verifyFormat("bool foo = true && [] { return false; }();"); 5762 5763 verifyFormat( 5764 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5766 5767 verifyGoogleFormat("int const* a = &b;"); 5768 verifyGoogleFormat("**outparam = 1;"); 5769 verifyGoogleFormat("*outparam = a * b;"); 5770 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5771 verifyGoogleFormat("A<int*> a;"); 5772 verifyGoogleFormat("A<int**> a;"); 5773 verifyGoogleFormat("A<int*, int*> a;"); 5774 verifyGoogleFormat("A<int**, int**> a;"); 5775 verifyGoogleFormat("f(b ? *c : *d);"); 5776 verifyGoogleFormat("int a = b ? *c : *d;"); 5777 verifyGoogleFormat("Type* t = **x;"); 5778 verifyGoogleFormat("Type* t = *++*x;"); 5779 verifyGoogleFormat("*++*x;"); 5780 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5781 verifyGoogleFormat("Type* t = x++ * y;"); 5782 verifyGoogleFormat( 5783 "const char* const p = reinterpret_cast<const char* const>(q);"); 5784 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5785 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5786 verifyGoogleFormat("template <typename T>\n" 5787 "void f(int i = 0, SomeType** temps = NULL);"); 5788 5789 FormatStyle Left = getLLVMStyle(); 5790 Left.PointerAlignment = FormatStyle::PAS_Left; 5791 verifyFormat("x = *a(x) = *a(y);", Left); 5792 verifyFormat("for (;; * = b) {\n}", Left); 5793 verifyFormat("return *this += 1;", Left); 5794 5795 verifyIndependentOfContext("a = *(x + y);"); 5796 verifyIndependentOfContext("a = &(x + y);"); 5797 verifyIndependentOfContext("*(x + y).call();"); 5798 verifyIndependentOfContext("&(x + y)->call();"); 5799 verifyFormat("void f() { &(*I).first; }"); 5800 5801 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5802 verifyFormat( 5803 "int *MyValues = {\n" 5804 " *A, // Operator detection might be confused by the '{'\n" 5805 " *BB // Operator detection might be confused by previous comment\n" 5806 "};"); 5807 5808 verifyIndependentOfContext("if (int *a = &b)"); 5809 verifyIndependentOfContext("if (int &a = *b)"); 5810 verifyIndependentOfContext("if (a & b[i])"); 5811 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5812 verifyIndependentOfContext("if (*b[i])"); 5813 verifyIndependentOfContext("if (int *a = (&b))"); 5814 verifyIndependentOfContext("while (int *a = &b)"); 5815 verifyIndependentOfContext("size = sizeof *a;"); 5816 verifyIndependentOfContext("if (a && (b = c))"); 5817 verifyFormat("void f() {\n" 5818 " for (const int &v : Values) {\n" 5819 " }\n" 5820 "}"); 5821 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5822 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5823 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5824 5825 verifyFormat("#define A (!a * b)"); 5826 verifyFormat("#define MACRO \\\n" 5827 " int *i = a * b; \\\n" 5828 " void f(a *b);", 5829 getLLVMStyleWithColumns(19)); 5830 5831 verifyIndependentOfContext("A = new SomeType *[Length];"); 5832 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5833 verifyIndependentOfContext("T **t = new T *;"); 5834 verifyIndependentOfContext("T **t = new T *();"); 5835 verifyGoogleFormat("A = new SomeType*[Length]();"); 5836 verifyGoogleFormat("A = new SomeType*[Length];"); 5837 verifyGoogleFormat("T** t = new T*;"); 5838 verifyGoogleFormat("T** t = new T*();"); 5839 5840 FormatStyle PointerLeft = getLLVMStyle(); 5841 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5842 verifyFormat("delete *x;", PointerLeft); 5843 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5844 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5845 verifyFormat("template <bool a, bool b> " 5846 "typename t::if<x && y>::type f() {}"); 5847 verifyFormat("template <int *y> f() {}"); 5848 verifyFormat("vector<int *> v;"); 5849 verifyFormat("vector<int *const> v;"); 5850 verifyFormat("vector<int *const **const *> v;"); 5851 verifyFormat("vector<int *volatile> v;"); 5852 verifyFormat("vector<a * b> v;"); 5853 verifyFormat("foo<b && false>();"); 5854 verifyFormat("foo<b & 1>();"); 5855 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5856 verifyFormat( 5857 "template <class T, class = typename std::enable_if<\n" 5858 " std::is_integral<T>::value &&\n" 5859 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5860 "void F();", 5861 getLLVMStyleWithColumns(76)); 5862 verifyFormat( 5863 "template <class T,\n" 5864 " class = typename ::std::enable_if<\n" 5865 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5866 "void F();", 5867 getGoogleStyleWithColumns(68)); 5868 5869 verifyIndependentOfContext("MACRO(int *i);"); 5870 verifyIndependentOfContext("MACRO(auto *a);"); 5871 verifyIndependentOfContext("MACRO(const A *a);"); 5872 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5873 // FIXME: Is there a way to make this work? 5874 // verifyIndependentOfContext("MACRO(A *a);"); 5875 5876 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5877 verifyFormat("return options != nullptr && operator==(*options);"); 5878 5879 EXPECT_EQ("#define OP(x) \\\n" 5880 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5881 " return s << a.DebugString(); \\\n" 5882 " }", 5883 format("#define OP(x) \\\n" 5884 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5885 " return s << a.DebugString(); \\\n" 5886 " }", 5887 getLLVMStyleWithColumns(50))); 5888 5889 // FIXME: We cannot handle this case yet; we might be able to figure out that 5890 // foo<x> d > v; doesn't make sense. 5891 verifyFormat("foo<a<b && c> d> v;"); 5892 5893 FormatStyle PointerMiddle = getLLVMStyle(); 5894 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5895 verifyFormat("delete *x;", PointerMiddle); 5896 verifyFormat("int * x;", PointerMiddle); 5897 verifyFormat("template <int * y> f() {}", PointerMiddle); 5898 verifyFormat("int * f(int * a) {}", PointerMiddle); 5899 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5900 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5901 verifyFormat("A<int *> a;", PointerMiddle); 5902 verifyFormat("A<int **> a;", PointerMiddle); 5903 verifyFormat("A<int *, int *> a;", PointerMiddle); 5904 verifyFormat("A<int * []> a;", PointerMiddle); 5905 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5906 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5907 verifyFormat("T ** t = new T *;", PointerMiddle); 5908 5909 // Member function reference qualifiers aren't binary operators. 5910 verifyFormat("string // break\n" 5911 "operator()() & {}"); 5912 verifyFormat("string // break\n" 5913 "operator()() && {}"); 5914 verifyGoogleFormat("template <typename T>\n" 5915 "auto x() & -> int {}"); 5916 } 5917 5918 TEST_F(FormatTest, UnderstandsAttributes) { 5919 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5920 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5921 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5922 FormatStyle AfterType = getLLVMStyle(); 5923 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5924 verifyFormat("__attribute__((nodebug)) void\n" 5925 "foo() {}\n", 5926 AfterType); 5927 } 5928 5929 TEST_F(FormatTest, UnderstandsEllipsis) { 5930 verifyFormat("int printf(const char *fmt, ...);"); 5931 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5932 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5933 5934 FormatStyle PointersLeft = getLLVMStyle(); 5935 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5936 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5937 } 5938 5939 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5940 EXPECT_EQ("int *a;\n" 5941 "int *a;\n" 5942 "int *a;", 5943 format("int *a;\n" 5944 "int* a;\n" 5945 "int *a;", 5946 getGoogleStyle())); 5947 EXPECT_EQ("int* a;\n" 5948 "int* a;\n" 5949 "int* a;", 5950 format("int* a;\n" 5951 "int* a;\n" 5952 "int *a;", 5953 getGoogleStyle())); 5954 EXPECT_EQ("int *a;\n" 5955 "int *a;\n" 5956 "int *a;", 5957 format("int *a;\n" 5958 "int * a;\n" 5959 "int * a;", 5960 getGoogleStyle())); 5961 EXPECT_EQ("auto x = [] {\n" 5962 " int *a;\n" 5963 " int *a;\n" 5964 " int *a;\n" 5965 "};", 5966 format("auto x=[]{int *a;\n" 5967 "int * a;\n" 5968 "int * a;};", 5969 getGoogleStyle())); 5970 } 5971 5972 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5973 verifyFormat("int f(int &&a) {}"); 5974 verifyFormat("int f(int a, char &&b) {}"); 5975 verifyFormat("void f() { int &&a = b; }"); 5976 verifyGoogleFormat("int f(int a, char&& b) {}"); 5977 verifyGoogleFormat("void f() { int&& a = b; }"); 5978 5979 verifyIndependentOfContext("A<int &&> a;"); 5980 verifyIndependentOfContext("A<int &&, int &&> a;"); 5981 verifyGoogleFormat("A<int&&> a;"); 5982 verifyGoogleFormat("A<int&&, int&&> a;"); 5983 5984 // Not rvalue references: 5985 verifyFormat("template <bool B, bool C> class A {\n" 5986 " static_assert(B && C, \"Something is wrong\");\n" 5987 "};"); 5988 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5989 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5990 verifyFormat("#define A(a, b) (a && b)"); 5991 } 5992 5993 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5994 verifyFormat("void f() {\n" 5995 " x[aaaaaaaaa -\n" 5996 " b] = 23;\n" 5997 "}", 5998 getLLVMStyleWithColumns(15)); 5999 } 6000 6001 TEST_F(FormatTest, FormatsCasts) { 6002 verifyFormat("Type *A = static_cast<Type *>(P);"); 6003 verifyFormat("Type *A = (Type *)P;"); 6004 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6005 verifyFormat("int a = (int)(2.0f);"); 6006 verifyFormat("int a = (int)2.0f;"); 6007 verifyFormat("x[(int32)y];"); 6008 verifyFormat("x = (int32)y;"); 6009 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6010 verifyFormat("int a = (int)*b;"); 6011 verifyFormat("int a = (int)2.0f;"); 6012 verifyFormat("int a = (int)~0;"); 6013 verifyFormat("int a = (int)++a;"); 6014 verifyFormat("int a = (int)sizeof(int);"); 6015 verifyFormat("int a = (int)+2;"); 6016 verifyFormat("my_int a = (my_int)2.0f;"); 6017 verifyFormat("my_int a = (my_int)sizeof(int);"); 6018 verifyFormat("return (my_int)aaa;"); 6019 verifyFormat("#define x ((int)-1)"); 6020 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6021 verifyFormat("#define p(q) ((int *)&q)"); 6022 verifyFormat("fn(a)(b) + 1;"); 6023 6024 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6025 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6026 verifyFormat("my_int a = (my_int)~0;"); 6027 verifyFormat("my_int a = (my_int)++a;"); 6028 verifyFormat("my_int a = (my_int)-2;"); 6029 verifyFormat("my_int a = (my_int)1;"); 6030 verifyFormat("my_int a = (my_int *)1;"); 6031 verifyFormat("my_int a = (const my_int)-1;"); 6032 verifyFormat("my_int a = (const my_int *)-1;"); 6033 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6034 verifyFormat("my_int a = (ns::my_int)-2;"); 6035 verifyFormat("case (my_int)ONE:"); 6036 verifyFormat("auto x = (X)this;"); 6037 6038 // FIXME: single value wrapped with paren will be treated as cast. 6039 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6040 6041 verifyFormat("{ (void)F; }"); 6042 6043 // Don't break after a cast's 6044 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6045 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6046 " bbbbbbbbbbbbbbbbbbbbbb);"); 6047 6048 // These are not casts. 6049 verifyFormat("void f(int *) {}"); 6050 verifyFormat("f(foo)->b;"); 6051 verifyFormat("f(foo).b;"); 6052 verifyFormat("f(foo)(b);"); 6053 verifyFormat("f(foo)[b];"); 6054 verifyFormat("[](foo) { return 4; }(bar);"); 6055 verifyFormat("(*funptr)(foo)[4];"); 6056 verifyFormat("funptrs[4](foo)[4];"); 6057 verifyFormat("void f(int *);"); 6058 verifyFormat("void f(int *) = 0;"); 6059 verifyFormat("void f(SmallVector<int>) {}"); 6060 verifyFormat("void f(SmallVector<int>);"); 6061 verifyFormat("void f(SmallVector<int>) = 0;"); 6062 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6063 verifyFormat("int a = sizeof(int) * b;"); 6064 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6065 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6066 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6067 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6068 6069 // These are not casts, but at some point were confused with casts. 6070 verifyFormat("virtual void foo(int *) override;"); 6071 verifyFormat("virtual void foo(char &) const;"); 6072 verifyFormat("virtual void foo(int *a, char *) const;"); 6073 verifyFormat("int a = sizeof(int *) + b;"); 6074 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6075 verifyFormat("bool b = f(g<int>) && c;"); 6076 verifyFormat("typedef void (*f)(int i) func;"); 6077 6078 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6079 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6080 // FIXME: The indentation here is not ideal. 6081 verifyFormat( 6082 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6083 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6084 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6085 } 6086 6087 TEST_F(FormatTest, FormatsFunctionTypes) { 6088 verifyFormat("A<bool()> a;"); 6089 verifyFormat("A<SomeType()> a;"); 6090 verifyFormat("A<void (*)(int, std::string)> a;"); 6091 verifyFormat("A<void *(int)>;"); 6092 verifyFormat("void *(*a)(int *, SomeType *);"); 6093 verifyFormat("int (*func)(void *);"); 6094 verifyFormat("void f() { int (*func)(void *); }"); 6095 verifyFormat("template <class CallbackClass>\n" 6096 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6097 6098 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6099 verifyGoogleFormat("void* (*a)(int);"); 6100 verifyGoogleFormat( 6101 "template <class CallbackClass>\n" 6102 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6103 6104 // Other constructs can look somewhat like function types: 6105 verifyFormat("A<sizeof(*x)> a;"); 6106 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6107 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6108 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6109 verifyFormat("int x = f(&h)();"); 6110 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6111 } 6112 6113 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6114 verifyFormat("A (*foo_)[6];"); 6115 verifyFormat("vector<int> (*foo_)[6];"); 6116 } 6117 6118 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6119 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6120 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6121 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6122 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6123 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6124 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6125 6126 // Different ways of ()-initializiation. 6127 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6128 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6129 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6130 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6131 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6132 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6133 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6134 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6135 } 6136 6137 TEST_F(FormatTest, BreaksLongDeclarations) { 6138 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6139 " AnotherNameForTheLongType;"); 6140 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6142 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6143 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6144 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6145 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6146 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6147 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6148 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6149 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6150 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6151 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6152 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6153 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6154 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6155 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6156 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6157 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6158 FormatStyle Indented = getLLVMStyle(); 6159 Indented.IndentWrappedFunctionNames = true; 6160 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6161 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6162 Indented); 6163 verifyFormat( 6164 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6165 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6166 Indented); 6167 verifyFormat( 6168 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6169 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6170 Indented); 6171 verifyFormat( 6172 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6173 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6174 Indented); 6175 6176 // FIXME: Without the comment, this breaks after "(". 6177 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6178 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6179 getGoogleStyle()); 6180 6181 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6182 " int LoooooooooooooooooooongParam2) {}"); 6183 verifyFormat( 6184 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6185 " SourceLocation L, IdentifierIn *II,\n" 6186 " Type *T) {}"); 6187 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6188 "ReallyReaaallyLongFunctionName(\n" 6189 " const std::string &SomeParameter,\n" 6190 " const SomeType<string, SomeOtherTemplateParameter>\n" 6191 " &ReallyReallyLongParameterName,\n" 6192 " const SomeType<string, SomeOtherTemplateParameter>\n" 6193 " &AnotherLongParameterName) {}"); 6194 verifyFormat("template <typename A>\n" 6195 "SomeLoooooooooooooooooooooongType<\n" 6196 " typename some_namespace::SomeOtherType<A>::Type>\n" 6197 "Function() {}"); 6198 6199 verifyGoogleFormat( 6200 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6201 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6202 verifyGoogleFormat( 6203 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6204 " SourceLocation L) {}"); 6205 verifyGoogleFormat( 6206 "some_namespace::LongReturnType\n" 6207 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6208 " int first_long_parameter, int second_parameter) {}"); 6209 6210 verifyGoogleFormat("template <typename T>\n" 6211 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6212 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6213 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6214 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6215 6216 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6217 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6218 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6219 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6220 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6221 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6222 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6223 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6226 } 6227 6228 TEST_F(FormatTest, FormatsArrays) { 6229 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6230 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6231 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6232 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6233 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6234 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6235 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6236 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6237 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6238 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6239 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6240 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6241 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6242 verifyFormat( 6243 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6244 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6245 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6246 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6247 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6248 6249 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6250 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6251 verifyFormat( 6252 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6253 " .aaaaaaa[0]\n" 6254 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6255 verifyFormat("a[::b::c];"); 6256 6257 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6258 6259 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6260 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6261 } 6262 6263 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6264 verifyFormat("(a)->b();"); 6265 verifyFormat("--a;"); 6266 } 6267 6268 TEST_F(FormatTest, HandlesIncludeDirectives) { 6269 verifyFormat("#include <string>\n" 6270 "#include <a/b/c.h>\n" 6271 "#include \"a/b/string\"\n" 6272 "#include \"string.h\"\n" 6273 "#include \"string.h\"\n" 6274 "#include <a-a>\n" 6275 "#include < path with space >\n" 6276 "#include_next <test.h>" 6277 "#include \"abc.h\" // this is included for ABC\n" 6278 "#include \"some long include\" // with a comment\n" 6279 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6280 getLLVMStyleWithColumns(35)); 6281 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6282 EXPECT_EQ("#include <a>", format("#include<a>")); 6283 6284 verifyFormat("#import <string>"); 6285 verifyFormat("#import <a/b/c.h>"); 6286 verifyFormat("#import \"a/b/string\""); 6287 verifyFormat("#import \"string.h\""); 6288 verifyFormat("#import \"string.h\""); 6289 verifyFormat("#if __has_include(<strstream>)\n" 6290 "#include <strstream>\n" 6291 "#endif"); 6292 6293 verifyFormat("#define MY_IMPORT <a/b>"); 6294 6295 // Protocol buffer definition or missing "#". 6296 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6297 getLLVMStyleWithColumns(30)); 6298 6299 FormatStyle Style = getLLVMStyle(); 6300 Style.AlwaysBreakBeforeMultilineStrings = true; 6301 Style.ColumnLimit = 0; 6302 verifyFormat("#import \"abc.h\"", Style); 6303 6304 // But 'import' might also be a regular C++ namespace. 6305 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6307 } 6308 6309 //===----------------------------------------------------------------------===// 6310 // Error recovery tests. 6311 //===----------------------------------------------------------------------===// 6312 6313 TEST_F(FormatTest, IncompleteParameterLists) { 6314 FormatStyle NoBinPacking = getLLVMStyle(); 6315 NoBinPacking.BinPackParameters = false; 6316 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6317 " double *min_x,\n" 6318 " double *max_x,\n" 6319 " double *min_y,\n" 6320 " double *max_y,\n" 6321 " double *min_z,\n" 6322 " double *max_z, ) {}", 6323 NoBinPacking); 6324 } 6325 6326 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6327 verifyFormat("void f() { return; }\n42"); 6328 verifyFormat("void f() {\n" 6329 " if (0)\n" 6330 " return;\n" 6331 "}\n" 6332 "42"); 6333 verifyFormat("void f() { return }\n42"); 6334 verifyFormat("void f() {\n" 6335 " if (0)\n" 6336 " return\n" 6337 "}\n" 6338 "42"); 6339 } 6340 6341 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6342 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6343 EXPECT_EQ("void f() {\n" 6344 " if (a)\n" 6345 " return\n" 6346 "}", 6347 format("void f ( ) { if ( a ) return }")); 6348 EXPECT_EQ("namespace N {\n" 6349 "void f()\n" 6350 "}", 6351 format("namespace N { void f() }")); 6352 EXPECT_EQ("namespace N {\n" 6353 "void f() {}\n" 6354 "void g()\n" 6355 "}", 6356 format("namespace N { void f( ) { } void g( ) }")); 6357 } 6358 6359 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6360 verifyFormat("int aaaaaaaa =\n" 6361 " // Overlylongcomment\n" 6362 " b;", 6363 getLLVMStyleWithColumns(20)); 6364 verifyFormat("function(\n" 6365 " ShortArgument,\n" 6366 " LoooooooooooongArgument);\n", 6367 getLLVMStyleWithColumns(20)); 6368 } 6369 6370 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6371 verifyFormat("public:"); 6372 verifyFormat("class A {\n" 6373 "public\n" 6374 " void f() {}\n" 6375 "};"); 6376 verifyFormat("public\n" 6377 "int qwerty;"); 6378 verifyFormat("public\n" 6379 "B {}"); 6380 verifyFormat("public\n" 6381 "{}"); 6382 verifyFormat("public\n" 6383 "B { int x; }"); 6384 } 6385 6386 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6387 verifyFormat("{"); 6388 verifyFormat("#})"); 6389 verifyNoCrash("(/**/[:!] ?[)."); 6390 } 6391 6392 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6393 verifyFormat("do {\n}"); 6394 verifyFormat("do {\n}\n" 6395 "f();"); 6396 verifyFormat("do {\n}\n" 6397 "wheeee(fun);"); 6398 verifyFormat("do {\n" 6399 " f();\n" 6400 "}"); 6401 } 6402 6403 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6404 verifyFormat("if {\n foo;\n foo();\n}"); 6405 verifyFormat("switch {\n foo;\n foo();\n}"); 6406 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6407 verifyFormat("while {\n foo;\n foo();\n}"); 6408 verifyFormat("do {\n foo;\n foo();\n} while;"); 6409 } 6410 6411 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6412 verifyIncompleteFormat("namespace {\n" 6413 "class Foo { Foo (\n" 6414 "};\n" 6415 "} // comment"); 6416 } 6417 6418 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6419 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6420 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6421 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6422 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6423 6424 EXPECT_EQ("{\n" 6425 " {\n" 6426 " breakme(\n" 6427 " qwe);\n" 6428 " }\n", 6429 format("{\n" 6430 " {\n" 6431 " breakme(qwe);\n" 6432 "}\n", 6433 getLLVMStyleWithColumns(10))); 6434 } 6435 6436 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6437 verifyFormat("int x = {\n" 6438 " avariable,\n" 6439 " b(alongervariable)};", 6440 getLLVMStyleWithColumns(25)); 6441 } 6442 6443 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6444 verifyFormat("return (a)(b){1, 2, 3};"); 6445 } 6446 6447 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6448 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6449 verifyFormat("vector<int> x{\n" 6450 " 1, 2, 3, 4,\n" 6451 "};"); 6452 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6453 verifyFormat("f({1, 2});"); 6454 verifyFormat("auto v = Foo{-1};"); 6455 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6456 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6457 verifyFormat("new vector<int>{1, 2, 3};"); 6458 verifyFormat("new int[3]{1, 2, 3};"); 6459 verifyFormat("new int{1};"); 6460 verifyFormat("return {arg1, arg2};"); 6461 verifyFormat("return {arg1, SomeType{parameter}};"); 6462 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6463 verifyFormat("new T{arg1, arg2};"); 6464 verifyFormat("f(MyMap[{composite, key}]);"); 6465 verifyFormat("class Class {\n" 6466 " T member = {arg1, arg2};\n" 6467 "};"); 6468 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6469 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6470 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6471 6472 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6473 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6474 verifyFormat("auto i = decltype(x){};"); 6475 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6476 verifyFormat("Node n{1, Node{1000}, //\n" 6477 " 2};"); 6478 verifyFormat("Aaaa aaaaaaa{\n" 6479 " {\n" 6480 " aaaa,\n" 6481 " },\n" 6482 "};"); 6483 verifyFormat("class C : public D {\n" 6484 " SomeClass SC{2};\n" 6485 "};"); 6486 verifyFormat("class C : public A {\n" 6487 " class D : public B {\n" 6488 " void f() { int i{2}; }\n" 6489 " };\n" 6490 "};"); 6491 verifyFormat("#define A {a, a},"); 6492 6493 // In combination with BinPackArguments = false. 6494 FormatStyle NoBinPacking = getLLVMStyle(); 6495 NoBinPacking.BinPackArguments = false; 6496 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6497 " bbbbb,\n" 6498 " ccccc,\n" 6499 " ddddd,\n" 6500 " eeeee,\n" 6501 " ffffff,\n" 6502 " ggggg,\n" 6503 " hhhhhh,\n" 6504 " iiiiii,\n" 6505 " jjjjjj,\n" 6506 " kkkkkk};", 6507 NoBinPacking); 6508 verifyFormat("const Aaaaaa aaaaa = {\n" 6509 " aaaaa,\n" 6510 " bbbbb,\n" 6511 " ccccc,\n" 6512 " ddddd,\n" 6513 " eeeee,\n" 6514 " ffffff,\n" 6515 " ggggg,\n" 6516 " hhhhhh,\n" 6517 " iiiiii,\n" 6518 " jjjjjj,\n" 6519 " kkkkkk,\n" 6520 "};", 6521 NoBinPacking); 6522 verifyFormat( 6523 "const Aaaaaa aaaaa = {\n" 6524 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6525 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6526 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6527 "};", 6528 NoBinPacking); 6529 6530 // FIXME: The alignment of these trailing comments might be bad. Then again, 6531 // this might be utterly useless in real code. 6532 verifyFormat("Constructor::Constructor()\n" 6533 " : some_value{ //\n" 6534 " aaaaaaa, //\n" 6535 " bbbbbbb} {}"); 6536 6537 // In braced lists, the first comment is always assumed to belong to the 6538 // first element. Thus, it can be moved to the next or previous line as 6539 // appropriate. 6540 EXPECT_EQ("function({// First element:\n" 6541 " 1,\n" 6542 " // Second element:\n" 6543 " 2});", 6544 format("function({\n" 6545 " // First element:\n" 6546 " 1,\n" 6547 " // Second element:\n" 6548 " 2});")); 6549 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6550 " // First element:\n" 6551 " 1,\n" 6552 " // Second element:\n" 6553 " 2};", 6554 format("std::vector<int> MyNumbers{// First element:\n" 6555 " 1,\n" 6556 " // Second element:\n" 6557 " 2};", 6558 getLLVMStyleWithColumns(30))); 6559 // A trailing comma should still lead to an enforced line break. 6560 EXPECT_EQ("vector<int> SomeVector = {\n" 6561 " // aaa\n" 6562 " 1, 2,\n" 6563 "};", 6564 format("vector<int> SomeVector = { // aaa\n" 6565 " 1, 2, };")); 6566 6567 FormatStyle ExtraSpaces = getLLVMStyle(); 6568 ExtraSpaces.Cpp11BracedListStyle = false; 6569 ExtraSpaces.ColumnLimit = 75; 6570 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6571 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6572 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6573 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6574 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6575 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6576 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6577 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6578 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6579 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6580 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6581 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6582 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6583 verifyFormat("class Class {\n" 6584 " T member = { arg1, arg2 };\n" 6585 "};", 6586 ExtraSpaces); 6587 verifyFormat( 6588 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6589 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6590 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6591 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6592 ExtraSpaces); 6593 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6594 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6595 ExtraSpaces); 6596 verifyFormat( 6597 "someFunction(OtherParam,\n" 6598 " BracedList{ // comment 1 (Forcing interesting break)\n" 6599 " param1, param2,\n" 6600 " // comment 2\n" 6601 " param3, param4 });", 6602 ExtraSpaces); 6603 verifyFormat( 6604 "std::this_thread::sleep_for(\n" 6605 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6606 ExtraSpaces); 6607 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6608 " aaaaaaa,\n" 6609 " aaaaaaaaaa,\n" 6610 " aaaaa,\n" 6611 " aaaaaaaaaaaaaaa,\n" 6612 " aaa,\n" 6613 " aaaaaaaaaa,\n" 6614 " a,\n" 6615 " aaaaaaaaaaaaaaaaaaaaa,\n" 6616 " aaaaaaaaaaaa,\n" 6617 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6618 " aaaaaaa,\n" 6619 " a};"); 6620 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6621 } 6622 6623 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6624 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6625 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6626 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6627 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6628 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6629 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6630 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6631 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6632 " 1, 22, 333, 4444, 55555, //\n" 6633 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6634 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6635 verifyFormat( 6636 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6637 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6638 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6639 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6640 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6641 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6642 " 7777777};"); 6643 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6644 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6645 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6646 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6647 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6648 " // Separating comment.\n" 6649 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6650 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6651 " // Leading comment\n" 6652 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6653 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6654 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6655 " 1, 1, 1, 1};", 6656 getLLVMStyleWithColumns(39)); 6657 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6658 " 1, 1, 1, 1};", 6659 getLLVMStyleWithColumns(38)); 6660 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6661 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6662 getLLVMStyleWithColumns(43)); 6663 verifyFormat( 6664 "static unsigned SomeValues[10][3] = {\n" 6665 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6666 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6667 verifyFormat("static auto fields = new vector<string>{\n" 6668 " \"aaaaaaaaaaaaa\",\n" 6669 " \"aaaaaaaaaaaaa\",\n" 6670 " \"aaaaaaaaaaaa\",\n" 6671 " \"aaaaaaaaaaaaaa\",\n" 6672 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6673 " \"aaaaaaaaaaaa\",\n" 6674 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6675 "};"); 6676 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6677 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6678 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6679 " 3, cccccccccccccccccccccc};", 6680 getLLVMStyleWithColumns(60)); 6681 6682 // Trailing commas. 6683 verifyFormat("vector<int> x = {\n" 6684 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6685 "};", 6686 getLLVMStyleWithColumns(39)); 6687 verifyFormat("vector<int> x = {\n" 6688 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6689 "};", 6690 getLLVMStyleWithColumns(39)); 6691 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6692 " 1, 1, 1, 1,\n" 6693 " /**/ /**/};", 6694 getLLVMStyleWithColumns(39)); 6695 6696 // Trailing comment in the first line. 6697 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6698 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6699 " 111111111, 222222222, 3333333333, 444444444, //\n" 6700 " 11111111, 22222222, 333333333, 44444444};"); 6701 // Trailing comment in the last line. 6702 verifyFormat("int aaaaa[] = {\n" 6703 " 1, 2, 3, // comment\n" 6704 " 4, 5, 6 // comment\n" 6705 "};"); 6706 6707 // With nested lists, we should either format one item per line or all nested 6708 // lists one on line. 6709 // FIXME: For some nested lists, we can do better. 6710 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6711 " {aaaaaaaaaaaaaaaaaaa},\n" 6712 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6713 " {aaaaaaaaaaaaaaaaa}};", 6714 getLLVMStyleWithColumns(60)); 6715 verifyFormat( 6716 "SomeStruct my_struct_array = {\n" 6717 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6718 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6719 " {aaa, aaa},\n" 6720 " {aaa, aaa},\n" 6721 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6722 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6723 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6724 6725 // No column layout should be used here. 6726 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6727 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6728 6729 verifyNoCrash("a<,"); 6730 6731 // No braced initializer here. 6732 verifyFormat("void f() {\n" 6733 " struct Dummy {};\n" 6734 " f(v);\n" 6735 "}"); 6736 6737 // Long lists should be formatted in columns even if they are nested. 6738 verifyFormat( 6739 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6740 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6741 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6742 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6743 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6744 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6745 } 6746 6747 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6748 FormatStyle DoNotMerge = getLLVMStyle(); 6749 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6750 6751 verifyFormat("void f() { return 42; }"); 6752 verifyFormat("void f() {\n" 6753 " return 42;\n" 6754 "}", 6755 DoNotMerge); 6756 verifyFormat("void f() {\n" 6757 " // Comment\n" 6758 "}"); 6759 verifyFormat("{\n" 6760 "#error {\n" 6761 " int a;\n" 6762 "}"); 6763 verifyFormat("{\n" 6764 " int a;\n" 6765 "#error {\n" 6766 "}"); 6767 verifyFormat("void f() {} // comment"); 6768 verifyFormat("void f() { int a; } // comment"); 6769 verifyFormat("void f() {\n" 6770 "} // comment", 6771 DoNotMerge); 6772 verifyFormat("void f() {\n" 6773 " int a;\n" 6774 "} // comment", 6775 DoNotMerge); 6776 verifyFormat("void f() {\n" 6777 "} // comment", 6778 getLLVMStyleWithColumns(15)); 6779 6780 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6781 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6782 6783 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6784 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6785 verifyFormat("class C {\n" 6786 " C()\n" 6787 " : iiiiiiii(nullptr),\n" 6788 " kkkkkkk(nullptr),\n" 6789 " mmmmmmm(nullptr),\n" 6790 " nnnnnnn(nullptr) {}\n" 6791 "};", 6792 getGoogleStyle()); 6793 6794 FormatStyle NoColumnLimit = getLLVMStyle(); 6795 NoColumnLimit.ColumnLimit = 0; 6796 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6797 EXPECT_EQ("class C {\n" 6798 " A() : b(0) {}\n" 6799 "};", 6800 format("class C{A():b(0){}};", NoColumnLimit)); 6801 EXPECT_EQ("A()\n" 6802 " : b(0) {\n" 6803 "}", 6804 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6805 6806 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6807 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6808 FormatStyle::SFS_None; 6809 EXPECT_EQ("A()\n" 6810 " : b(0) {\n" 6811 "}", 6812 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6813 EXPECT_EQ("A()\n" 6814 " : b(0) {\n" 6815 "}", 6816 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6817 6818 verifyFormat("#define A \\\n" 6819 " void f() { \\\n" 6820 " int i; \\\n" 6821 " }", 6822 getLLVMStyleWithColumns(20)); 6823 verifyFormat("#define A \\\n" 6824 " void f() { int i; }", 6825 getLLVMStyleWithColumns(21)); 6826 verifyFormat("#define A \\\n" 6827 " void f() { \\\n" 6828 " int i; \\\n" 6829 " } \\\n" 6830 " int j;", 6831 getLLVMStyleWithColumns(22)); 6832 verifyFormat("#define A \\\n" 6833 " void f() { int i; } \\\n" 6834 " int j;", 6835 getLLVMStyleWithColumns(23)); 6836 } 6837 6838 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6839 FormatStyle MergeInlineOnly = getLLVMStyle(); 6840 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6841 verifyFormat("class C {\n" 6842 " int f() { return 42; }\n" 6843 "};", 6844 MergeInlineOnly); 6845 verifyFormat("int f() {\n" 6846 " return 42;\n" 6847 "}", 6848 MergeInlineOnly); 6849 } 6850 6851 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6852 // Elaborate type variable declarations. 6853 verifyFormat("struct foo a = {bar};\nint n;"); 6854 verifyFormat("class foo a = {bar};\nint n;"); 6855 verifyFormat("union foo a = {bar};\nint n;"); 6856 6857 // Elaborate types inside function definitions. 6858 verifyFormat("struct foo f() {}\nint n;"); 6859 verifyFormat("class foo f() {}\nint n;"); 6860 verifyFormat("union foo f() {}\nint n;"); 6861 6862 // Templates. 6863 verifyFormat("template <class X> void f() {}\nint n;"); 6864 verifyFormat("template <struct X> void f() {}\nint n;"); 6865 verifyFormat("template <union X> void f() {}\nint n;"); 6866 6867 // Actual definitions... 6868 verifyFormat("struct {\n} n;"); 6869 verifyFormat( 6870 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6871 verifyFormat("union Z {\n int n;\n} x;"); 6872 verifyFormat("class MACRO Z {\n} n;"); 6873 verifyFormat("class MACRO(X) Z {\n} n;"); 6874 verifyFormat("class __attribute__(X) Z {\n} n;"); 6875 verifyFormat("class __declspec(X) Z {\n} n;"); 6876 verifyFormat("class A##B##C {\n} n;"); 6877 verifyFormat("class alignas(16) Z {\n} n;"); 6878 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6879 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6880 6881 // Redefinition from nested context: 6882 verifyFormat("class A::B::C {\n} n;"); 6883 6884 // Template definitions. 6885 verifyFormat( 6886 "template <typename F>\n" 6887 "Matcher(const Matcher<F> &Other,\n" 6888 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6889 " !is_same<F, T>::value>::type * = 0)\n" 6890 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6891 6892 // FIXME: This is still incorrectly handled at the formatter side. 6893 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6894 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6895 6896 // FIXME: 6897 // This now gets parsed incorrectly as class definition. 6898 // verifyFormat("class A<int> f() {\n}\nint n;"); 6899 6900 // Elaborate types where incorrectly parsing the structural element would 6901 // break the indent. 6902 verifyFormat("if (true)\n" 6903 " class X x;\n" 6904 "else\n" 6905 " f();\n"); 6906 6907 // This is simply incomplete. Formatting is not important, but must not crash. 6908 verifyFormat("class A:"); 6909 } 6910 6911 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6912 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6913 format("#error Leave all white!!!!! space* alone!\n")); 6914 EXPECT_EQ( 6915 "#warning Leave all white!!!!! space* alone!\n", 6916 format("#warning Leave all white!!!!! space* alone!\n")); 6917 EXPECT_EQ("#error 1", format(" # error 1")); 6918 EXPECT_EQ("#warning 1", format(" # warning 1")); 6919 } 6920 6921 TEST_F(FormatTest, FormatHashIfExpressions) { 6922 verifyFormat("#if AAAA && BBBB"); 6923 verifyFormat("#if (AAAA && BBBB)"); 6924 verifyFormat("#elif (AAAA && BBBB)"); 6925 // FIXME: Come up with a better indentation for #elif. 6926 verifyFormat( 6927 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6928 " defined(BBBBBBBB)\n" 6929 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6930 " defined(BBBBBBBB)\n" 6931 "#endif", 6932 getLLVMStyleWithColumns(65)); 6933 } 6934 6935 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6936 FormatStyle AllowsMergedIf = getGoogleStyle(); 6937 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6938 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6939 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6940 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6941 EXPECT_EQ("if (true) return 42;", 6942 format("if (true)\nreturn 42;", AllowsMergedIf)); 6943 FormatStyle ShortMergedIf = AllowsMergedIf; 6944 ShortMergedIf.ColumnLimit = 25; 6945 verifyFormat("#define A \\\n" 6946 " if (true) return 42;", 6947 ShortMergedIf); 6948 verifyFormat("#define A \\\n" 6949 " f(); \\\n" 6950 " if (true)\n" 6951 "#define B", 6952 ShortMergedIf); 6953 verifyFormat("#define A \\\n" 6954 " f(); \\\n" 6955 " if (true)\n" 6956 "g();", 6957 ShortMergedIf); 6958 verifyFormat("{\n" 6959 "#ifdef A\n" 6960 " // Comment\n" 6961 " if (true) continue;\n" 6962 "#endif\n" 6963 " // Comment\n" 6964 " if (true) continue;\n" 6965 "}", 6966 ShortMergedIf); 6967 ShortMergedIf.ColumnLimit = 29; 6968 verifyFormat("#define A \\\n" 6969 " if (aaaaaaaaaa) return 1; \\\n" 6970 " return 2;", 6971 ShortMergedIf); 6972 ShortMergedIf.ColumnLimit = 28; 6973 verifyFormat("#define A \\\n" 6974 " if (aaaaaaaaaa) \\\n" 6975 " return 1; \\\n" 6976 " return 2;", 6977 ShortMergedIf); 6978 } 6979 6980 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6981 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6982 " f();\n" 6983 "}"); 6984 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6985 " f();\n" 6986 "} /* another comment */ else /* comment #3 */ {\n" 6987 " g();\n" 6988 "}"); 6989 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6990 " f();\n" 6991 "}"); 6992 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6993 " f();\n" 6994 "}"); 6995 verifyFormat("do /* a comment in a strange place */ {\n" 6996 " f();\n" 6997 "} /* another comment */ while (0);"); 6998 } 6999 7000 TEST_F(FormatTest, BlockComments) { 7001 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 7002 format("/* *//* */ /* */\n/* *//* */ /* */")); 7003 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 7004 EXPECT_EQ("#define A /*123*/ \\\n" 7005 " b\n" 7006 "/* */\n" 7007 "someCall(\n" 7008 " parameter);", 7009 format("#define A /*123*/ b\n" 7010 "/* */\n" 7011 "someCall(parameter);", 7012 getLLVMStyleWithColumns(15))); 7013 7014 EXPECT_EQ("#define A\n" 7015 "/* */ someCall(\n" 7016 " parameter);", 7017 format("#define A\n" 7018 "/* */someCall(parameter);", 7019 getLLVMStyleWithColumns(15))); 7020 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7021 EXPECT_EQ("/*\n" 7022 "*\n" 7023 " * aaaaaa\n" 7024 " * aaaaaa\n" 7025 "*/", 7026 format("/*\n" 7027 "*\n" 7028 " * aaaaaa aaaaaa\n" 7029 "*/", 7030 getLLVMStyleWithColumns(10))); 7031 EXPECT_EQ("/*\n" 7032 "**\n" 7033 "* aaaaaa\n" 7034 "*aaaaaa\n" 7035 "*/", 7036 format("/*\n" 7037 "**\n" 7038 "* aaaaaa aaaaaa\n" 7039 "*/", 7040 getLLVMStyleWithColumns(10))); 7041 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7042 " /* line 1\n" 7043 " bbbbbbbbbbbb */\n" 7044 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7045 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7046 " /* line 1\n" 7047 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7048 getLLVMStyleWithColumns(50))); 7049 7050 FormatStyle NoBinPacking = getLLVMStyle(); 7051 NoBinPacking.BinPackParameters = false; 7052 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7053 " 2, /* comment 2 */\n" 7054 " 3, /* comment 3 */\n" 7055 " aaaa,\n" 7056 " bbbb);", 7057 format("someFunction (1, /* comment 1 */\n" 7058 " 2, /* comment 2 */ \n" 7059 " 3, /* comment 3 */\n" 7060 "aaaa, bbbb );", 7061 NoBinPacking)); 7062 verifyFormat( 7063 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7065 EXPECT_EQ( 7066 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7067 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7069 format( 7070 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7071 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7073 EXPECT_EQ( 7074 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7075 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7076 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7077 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7078 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7079 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7080 7081 verifyFormat("void f(int * /* unused */) {}"); 7082 7083 EXPECT_EQ("/*\n" 7084 " **\n" 7085 " */", 7086 format("/*\n" 7087 " **\n" 7088 " */")); 7089 EXPECT_EQ("/*\n" 7090 " *q\n" 7091 " */", 7092 format("/*\n" 7093 " *q\n" 7094 " */")); 7095 EXPECT_EQ("/*\n" 7096 " * q\n" 7097 " */", 7098 format("/*\n" 7099 " * q\n" 7100 " */")); 7101 EXPECT_EQ("/*\n" 7102 " **/", 7103 format("/*\n" 7104 " **/")); 7105 EXPECT_EQ("/*\n" 7106 " ***/", 7107 format("/*\n" 7108 " ***/")); 7109 } 7110 7111 TEST_F(FormatTest, BlockCommentsInMacros) { 7112 EXPECT_EQ("#define A \\\n" 7113 " { \\\n" 7114 " /* one line */ \\\n" 7115 " someCall();", 7116 format("#define A { \\\n" 7117 " /* one line */ \\\n" 7118 " someCall();", 7119 getLLVMStyleWithColumns(20))); 7120 EXPECT_EQ("#define A \\\n" 7121 " { \\\n" 7122 " /* previous */ \\\n" 7123 " /* one line */ \\\n" 7124 " someCall();", 7125 format("#define A { \\\n" 7126 " /* previous */ \\\n" 7127 " /* one line */ \\\n" 7128 " someCall();", 7129 getLLVMStyleWithColumns(20))); 7130 } 7131 7132 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7133 EXPECT_EQ("a = {\n" 7134 " 1111 /* */\n" 7135 "};", 7136 format("a = {1111 /* */\n" 7137 "};", 7138 getLLVMStyleWithColumns(15))); 7139 EXPECT_EQ("a = {\n" 7140 " 1111 /* */\n" 7141 "};", 7142 format("a = {1111 /* */\n" 7143 "};", 7144 getLLVMStyleWithColumns(15))); 7145 7146 // FIXME: The formatting is still wrong here. 7147 EXPECT_EQ("a = {\n" 7148 " 1111 /* a\n" 7149 " */\n" 7150 "};", 7151 format("a = {1111 /* a */\n" 7152 "};", 7153 getLLVMStyleWithColumns(15))); 7154 } 7155 7156 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7157 verifyFormat("{\n" 7158 " // a\n" 7159 " // b"); 7160 } 7161 7162 TEST_F(FormatTest, FormatStarDependingOnContext) { 7163 verifyFormat("void f(int *a);"); 7164 verifyFormat("void f() { f(fint * b); }"); 7165 verifyFormat("class A {\n void f(int *a);\n};"); 7166 verifyFormat("class A {\n int *a;\n};"); 7167 verifyFormat("namespace a {\n" 7168 "namespace b {\n" 7169 "class A {\n" 7170 " void f() {}\n" 7171 " int *a;\n" 7172 "};\n" 7173 "}\n" 7174 "}"); 7175 } 7176 7177 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7178 verifyFormat("while"); 7179 verifyFormat("operator"); 7180 } 7181 7182 //===----------------------------------------------------------------------===// 7183 // Objective-C tests. 7184 //===----------------------------------------------------------------------===// 7185 7186 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7187 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7188 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7189 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7190 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7191 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7192 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7193 format("-(NSInteger)Method3:(id)anObject;")); 7194 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7195 format("-(NSInteger)Method4:(id)anObject;")); 7196 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7197 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7198 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7199 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7200 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7201 "forAllCells:(BOOL)flag;", 7202 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7203 "forAllCells:(BOOL)flag;")); 7204 7205 // Very long objectiveC method declaration. 7206 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7207 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7208 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7209 " inRange:(NSRange)range\n" 7210 " outRange:(NSRange)out_range\n" 7211 " outRange1:(NSRange)out_range1\n" 7212 " outRange2:(NSRange)out_range2\n" 7213 " outRange3:(NSRange)out_range3\n" 7214 " outRange4:(NSRange)out_range4\n" 7215 " outRange5:(NSRange)out_range5\n" 7216 " outRange6:(NSRange)out_range6\n" 7217 " outRange7:(NSRange)out_range7\n" 7218 " outRange8:(NSRange)out_range8\n" 7219 " outRange9:(NSRange)out_range9;"); 7220 7221 // When the function name has to be wrapped. 7222 FormatStyle Style = getLLVMStyle(); 7223 Style.IndentWrappedFunctionNames = false; 7224 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7225 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7226 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7227 "}", 7228 Style); 7229 Style.IndentWrappedFunctionNames = true; 7230 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7231 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7232 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7233 "}", 7234 Style); 7235 7236 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7237 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7238 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7239 // protocol lists (but not for template classes): 7240 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7241 7242 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7243 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7244 7245 // If there's no return type (very rare in practice!), LLVM and Google style 7246 // agree. 7247 verifyFormat("- foo;"); 7248 verifyFormat("- foo:(int)f;"); 7249 verifyGoogleFormat("- foo:(int)foo;"); 7250 } 7251 7252 TEST_F(FormatTest, FormatObjCInterface) { 7253 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7254 "@public\n" 7255 " int field1;\n" 7256 "@protected\n" 7257 " int field2;\n" 7258 "@private\n" 7259 " int field3;\n" 7260 "@package\n" 7261 " int field4;\n" 7262 "}\n" 7263 "+ (id)init;\n" 7264 "@end"); 7265 7266 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7267 " @public\n" 7268 " int field1;\n" 7269 " @protected\n" 7270 " int field2;\n" 7271 " @private\n" 7272 " int field3;\n" 7273 " @package\n" 7274 " int field4;\n" 7275 "}\n" 7276 "+ (id)init;\n" 7277 "@end"); 7278 7279 verifyFormat("@interface /* wait for it */ Foo\n" 7280 "+ (id)init;\n" 7281 "// Look, a comment!\n" 7282 "- (int)answerWith:(int)i;\n" 7283 "@end"); 7284 7285 verifyFormat("@interface Foo\n" 7286 "@end\n" 7287 "@interface Bar\n" 7288 "@end"); 7289 7290 verifyFormat("@interface Foo : Bar\n" 7291 "+ (id)init;\n" 7292 "@end"); 7293 7294 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7295 "+ (id)init;\n" 7296 "@end"); 7297 7298 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7299 "+ (id)init;\n" 7300 "@end"); 7301 7302 verifyFormat("@interface Foo (HackStuff)\n" 7303 "+ (id)init;\n" 7304 "@end"); 7305 7306 verifyFormat("@interface Foo ()\n" 7307 "+ (id)init;\n" 7308 "@end"); 7309 7310 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7311 "+ (id)init;\n" 7312 "@end"); 7313 7314 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7315 "+ (id)init;\n" 7316 "@end"); 7317 7318 verifyFormat("@interface Foo {\n" 7319 " int _i;\n" 7320 "}\n" 7321 "+ (id)init;\n" 7322 "@end"); 7323 7324 verifyFormat("@interface Foo : Bar {\n" 7325 " int _i;\n" 7326 "}\n" 7327 "+ (id)init;\n" 7328 "@end"); 7329 7330 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7331 " int _i;\n" 7332 "}\n" 7333 "+ (id)init;\n" 7334 "@end"); 7335 7336 verifyFormat("@interface Foo (HackStuff) {\n" 7337 " int _i;\n" 7338 "}\n" 7339 "+ (id)init;\n" 7340 "@end"); 7341 7342 verifyFormat("@interface Foo () {\n" 7343 " int _i;\n" 7344 "}\n" 7345 "+ (id)init;\n" 7346 "@end"); 7347 7348 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7349 " int _i;\n" 7350 "}\n" 7351 "+ (id)init;\n" 7352 "@end"); 7353 7354 FormatStyle OnePerLine = getGoogleStyle(); 7355 OnePerLine.BinPackParameters = false; 7356 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7361 "}", 7362 OnePerLine); 7363 } 7364 7365 TEST_F(FormatTest, FormatObjCImplementation) { 7366 verifyFormat("@implementation Foo : NSObject {\n" 7367 "@public\n" 7368 " int field1;\n" 7369 "@protected\n" 7370 " int field2;\n" 7371 "@private\n" 7372 " int field3;\n" 7373 "@package\n" 7374 " int field4;\n" 7375 "}\n" 7376 "+ (id)init {\n}\n" 7377 "@end"); 7378 7379 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7380 " @public\n" 7381 " int field1;\n" 7382 " @protected\n" 7383 " int field2;\n" 7384 " @private\n" 7385 " int field3;\n" 7386 " @package\n" 7387 " int field4;\n" 7388 "}\n" 7389 "+ (id)init {\n}\n" 7390 "@end"); 7391 7392 verifyFormat("@implementation Foo\n" 7393 "+ (id)init {\n" 7394 " if (true)\n" 7395 " return nil;\n" 7396 "}\n" 7397 "// Look, a comment!\n" 7398 "- (int)answerWith:(int)i {\n" 7399 " return i;\n" 7400 "}\n" 7401 "+ (int)answerWith:(int)i {\n" 7402 " return i;\n" 7403 "}\n" 7404 "@end"); 7405 7406 verifyFormat("@implementation Foo\n" 7407 "@end\n" 7408 "@implementation Bar\n" 7409 "@end"); 7410 7411 EXPECT_EQ("@implementation Foo : Bar\n" 7412 "+ (id)init {\n}\n" 7413 "- (void)foo {\n}\n" 7414 "@end", 7415 format("@implementation Foo : Bar\n" 7416 "+(id)init{}\n" 7417 "-(void)foo{}\n" 7418 "@end")); 7419 7420 verifyFormat("@implementation Foo {\n" 7421 " int _i;\n" 7422 "}\n" 7423 "+ (id)init {\n}\n" 7424 "@end"); 7425 7426 verifyFormat("@implementation Foo : Bar {\n" 7427 " int _i;\n" 7428 "}\n" 7429 "+ (id)init {\n}\n" 7430 "@end"); 7431 7432 verifyFormat("@implementation Foo (HackStuff)\n" 7433 "+ (id)init {\n}\n" 7434 "@end"); 7435 verifyFormat("@implementation ObjcClass\n" 7436 "- (void)method;\n" 7437 "{}\n" 7438 "@end"); 7439 } 7440 7441 TEST_F(FormatTest, FormatObjCProtocol) { 7442 verifyFormat("@protocol Foo\n" 7443 "@property(weak) id delegate;\n" 7444 "- (NSUInteger)numberOfThings;\n" 7445 "@end"); 7446 7447 verifyFormat("@protocol MyProtocol <NSObject>\n" 7448 "- (NSUInteger)numberOfThings;\n" 7449 "@end"); 7450 7451 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7452 "- (NSUInteger)numberOfThings;\n" 7453 "@end"); 7454 7455 verifyFormat("@protocol Foo;\n" 7456 "@protocol Bar;\n"); 7457 7458 verifyFormat("@protocol Foo\n" 7459 "@end\n" 7460 "@protocol Bar\n" 7461 "@end"); 7462 7463 verifyFormat("@protocol myProtocol\n" 7464 "- (void)mandatoryWithInt:(int)i;\n" 7465 "@optional\n" 7466 "- (void)optional;\n" 7467 "@required\n" 7468 "- (void)required;\n" 7469 "@optional\n" 7470 "@property(assign) int madProp;\n" 7471 "@end\n"); 7472 7473 verifyFormat("@property(nonatomic, assign, readonly)\n" 7474 " int *looooooooooooooooooooooooooooongNumber;\n" 7475 "@property(nonatomic, assign, readonly)\n" 7476 " NSString *looooooooooooooooooooooooooooongName;"); 7477 7478 verifyFormat("@implementation PR18406\n" 7479 "}\n" 7480 "@end"); 7481 } 7482 7483 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7484 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7485 " rect:(NSRect)theRect\n" 7486 " interval:(float)theInterval {\n" 7487 "}"); 7488 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7489 " longKeyword:(NSRect)theRect\n" 7490 " longerKeyword:(float)theInterval\n" 7491 " error:(NSError **)theError {\n" 7492 "}"); 7493 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7494 " longKeyword:(NSRect)theRect\n" 7495 " evenLongerKeyword:(float)theInterval\n" 7496 " error:(NSError **)theError {\n" 7497 "}"); 7498 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7499 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7500 " NS_DESIGNATED_INITIALIZER;", 7501 getLLVMStyleWithColumns(60)); 7502 7503 // Continuation indent width should win over aligning colons if the function 7504 // name is long. 7505 FormatStyle continuationStyle = getGoogleStyle(); 7506 continuationStyle.ColumnLimit = 40; 7507 continuationStyle.IndentWrappedFunctionNames = true; 7508 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7509 " dontAlignNamef:(NSRect)theRect {\n" 7510 "}", 7511 continuationStyle); 7512 7513 // Make sure we don't break aligning for short parameter names. 7514 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7515 " aShortf:(NSRect)theRect {\n" 7516 "}", 7517 continuationStyle); 7518 } 7519 7520 TEST_F(FormatTest, FormatObjCMethodExpr) { 7521 verifyFormat("[foo bar:baz];"); 7522 verifyFormat("return [foo bar:baz];"); 7523 verifyFormat("return (a)[foo bar:baz];"); 7524 verifyFormat("f([foo bar:baz]);"); 7525 verifyFormat("f(2, [foo bar:baz]);"); 7526 verifyFormat("f(2, a ? b : c);"); 7527 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7528 7529 // Unary operators. 7530 verifyFormat("int a = +[foo bar:baz];"); 7531 verifyFormat("int a = -[foo bar:baz];"); 7532 verifyFormat("int a = ![foo bar:baz];"); 7533 verifyFormat("int a = ~[foo bar:baz];"); 7534 verifyFormat("int a = ++[foo bar:baz];"); 7535 verifyFormat("int a = --[foo bar:baz];"); 7536 verifyFormat("int a = sizeof [foo bar:baz];"); 7537 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7538 verifyFormat("int a = &[foo bar:baz];"); 7539 verifyFormat("int a = *[foo bar:baz];"); 7540 // FIXME: Make casts work, without breaking f()[4]. 7541 // verifyFormat("int a = (int)[foo bar:baz];"); 7542 // verifyFormat("return (int)[foo bar:baz];"); 7543 // verifyFormat("(void)[foo bar:baz];"); 7544 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7545 7546 // Binary operators. 7547 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7548 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7549 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7550 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7551 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7552 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7553 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7554 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7555 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7556 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7557 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7558 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7559 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7560 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7561 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7562 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7563 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7564 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7565 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7566 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7567 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7568 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7569 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7570 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7571 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7572 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7573 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7574 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7575 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7576 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7577 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7578 // Whew! 7579 7580 verifyFormat("return in[42];"); 7581 verifyFormat("for (auto v : in[1]) {\n}"); 7582 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7583 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7584 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7585 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7586 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7587 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7588 "}"); 7589 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7590 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7591 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7592 7593 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7594 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7595 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7596 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7597 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7598 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7599 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7600 7601 verifyFormat("arr[[self indexForFoo:a]];"); 7602 verifyFormat("throw [self errorFor:a];"); 7603 verifyFormat("@throw [self errorFor:a];"); 7604 7605 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7606 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7607 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7608 7609 // This tests that the formatter doesn't break after "backing" but before ":", 7610 // which would be at 80 columns. 7611 verifyFormat( 7612 "void f() {\n" 7613 " if ((self = [super initWithContentRect:contentRect\n" 7614 " styleMask:styleMask ?: otherMask\n" 7615 " backing:NSBackingStoreBuffered\n" 7616 " defer:YES]))"); 7617 7618 verifyFormat( 7619 "[foo checkThatBreakingAfterColonWorksOk:\n" 7620 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7621 7622 verifyFormat("[myObj short:arg1 // Force line break\n" 7623 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7624 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7625 " error:arg4];"); 7626 verifyFormat( 7627 "void f() {\n" 7628 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7629 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7630 " pos.width(), pos.height())\n" 7631 " styleMask:NSBorderlessWindowMask\n" 7632 " backing:NSBackingStoreBuffered\n" 7633 " defer:NO]);\n" 7634 "}"); 7635 verifyFormat( 7636 "void f() {\n" 7637 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7638 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7639 " pos.width(), pos.height())\n" 7640 " syeMask:NSBorderlessWindowMask\n" 7641 " bking:NSBackingStoreBuffered\n" 7642 " der:NO]);\n" 7643 "}", 7644 getLLVMStyleWithColumns(70)); 7645 verifyFormat( 7646 "void f() {\n" 7647 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7648 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7649 " pos.width(), pos.height())\n" 7650 " styleMask:NSBorderlessWindowMask\n" 7651 " backing:NSBackingStoreBuffered\n" 7652 " defer:NO]);\n" 7653 "}", 7654 getChromiumStyle(FormatStyle::LK_Cpp)); 7655 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7656 " with:contentsNativeView];"); 7657 7658 verifyFormat( 7659 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7660 " owner:nillllll];"); 7661 7662 verifyFormat( 7663 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7664 " forType:kBookmarkButtonDragType];"); 7665 7666 verifyFormat("[defaultCenter addObserver:self\n" 7667 " selector:@selector(willEnterFullscreen)\n" 7668 " name:kWillEnterFullscreenNotification\n" 7669 " object:nil];"); 7670 verifyFormat("[image_rep drawInRect:drawRect\n" 7671 " fromRect:NSZeroRect\n" 7672 " operation:NSCompositeCopy\n" 7673 " fraction:1.0\n" 7674 " respectFlipped:NO\n" 7675 " hints:nil];"); 7676 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7678 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7680 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7681 " aaaaaaaaaaaaaaaaaaaaaa];"); 7682 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7683 " .aaaaaaaa];", // FIXME: Indentation seems off. 7684 getLLVMStyleWithColumns(60)); 7685 7686 verifyFormat( 7687 "scoped_nsobject<NSTextField> message(\n" 7688 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7689 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7690 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7691 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7692 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7693 " aaaa:bbb];"); 7694 verifyFormat("[self param:function( //\n" 7695 " parameter)]"); 7696 verifyFormat( 7697 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7698 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7699 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7700 7701 // FIXME: This violates the column limit. 7702 verifyFormat( 7703 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7704 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7705 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7706 getLLVMStyleWithColumns(60)); 7707 7708 // Variadic parameters. 7709 verifyFormat( 7710 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7711 verifyFormat( 7712 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7713 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7714 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7715 verifyFormat("[self // break\n" 7716 " a:a\n" 7717 " aaa:aaa];"); 7718 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7719 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7720 } 7721 7722 TEST_F(FormatTest, ObjCAt) { 7723 verifyFormat("@autoreleasepool"); 7724 verifyFormat("@catch"); 7725 verifyFormat("@class"); 7726 verifyFormat("@compatibility_alias"); 7727 verifyFormat("@defs"); 7728 verifyFormat("@dynamic"); 7729 verifyFormat("@encode"); 7730 verifyFormat("@end"); 7731 verifyFormat("@finally"); 7732 verifyFormat("@implementation"); 7733 verifyFormat("@import"); 7734 verifyFormat("@interface"); 7735 verifyFormat("@optional"); 7736 verifyFormat("@package"); 7737 verifyFormat("@private"); 7738 verifyFormat("@property"); 7739 verifyFormat("@protected"); 7740 verifyFormat("@protocol"); 7741 verifyFormat("@public"); 7742 verifyFormat("@required"); 7743 verifyFormat("@selector"); 7744 verifyFormat("@synchronized"); 7745 verifyFormat("@synthesize"); 7746 verifyFormat("@throw"); 7747 verifyFormat("@try"); 7748 7749 EXPECT_EQ("@interface", format("@ interface")); 7750 7751 // The precise formatting of this doesn't matter, nobody writes code like 7752 // this. 7753 verifyFormat("@ /*foo*/ interface"); 7754 } 7755 7756 TEST_F(FormatTest, ObjCSnippets) { 7757 verifyFormat("@autoreleasepool {\n" 7758 " foo();\n" 7759 "}"); 7760 verifyFormat("@class Foo, Bar;"); 7761 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7762 verifyFormat("@dynamic textColor;"); 7763 verifyFormat("char *buf1 = @encode(int *);"); 7764 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7765 verifyFormat("char *buf1 = @encode(int **);"); 7766 verifyFormat("Protocol *proto = @protocol(p1);"); 7767 verifyFormat("SEL s = @selector(foo:);"); 7768 verifyFormat("@synchronized(self) {\n" 7769 " f();\n" 7770 "}"); 7771 7772 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7773 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7774 7775 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7776 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7777 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7778 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7779 getMozillaStyle()); 7780 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7781 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7782 getWebKitStyle()); 7783 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7784 7785 verifyFormat("@import foo.bar;\n" 7786 "@import baz;"); 7787 } 7788 7789 TEST_F(FormatTest, ObjCForIn) { 7790 verifyFormat("- (void)test {\n" 7791 " for (NSString *n in arrayOfStrings) {\n" 7792 " foo(n);\n" 7793 " }\n" 7794 "}"); 7795 verifyFormat("- (void)test {\n" 7796 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7797 " foo(n);\n" 7798 " }\n" 7799 "}"); 7800 } 7801 7802 TEST_F(FormatTest, ObjCLiterals) { 7803 verifyFormat("@\"String\""); 7804 verifyFormat("@1"); 7805 verifyFormat("@+4.8"); 7806 verifyFormat("@-4"); 7807 verifyFormat("@1LL"); 7808 verifyFormat("@.5"); 7809 verifyFormat("@'c'"); 7810 verifyFormat("@true"); 7811 7812 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7813 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7814 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7815 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7816 7817 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7818 } 7819 7820 TEST_F(FormatTest, ObjCDictLiterals) { 7821 verifyFormat("@{"); 7822 verifyFormat("@{}"); 7823 verifyFormat("@{@\"one\" : @1}"); 7824 verifyFormat("return @{@\"one\" : @1;"); 7825 verifyFormat("@{@\"one\" : @1}"); 7826 7827 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7828 verifyFormat("@{\n" 7829 " @\"one\" : @{@2 : @1},\n" 7830 "}"); 7831 7832 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7833 verifyIncompleteFormat("[self setDict:@{}"); 7834 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7835 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7836 verifyFormat( 7837 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7838 verifyFormat( 7839 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7840 7841 verifyFormat("NSDictionary *d = @{\n" 7842 " @\"nam\" : NSUserNam(),\n" 7843 " @\"dte\" : [NSDate date],\n" 7844 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7845 "};"); 7846 verifyFormat( 7847 "@{\n" 7848 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7849 "regularFont,\n" 7850 "};"); 7851 verifyGoogleFormat( 7852 "@{\n" 7853 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7854 "regularFont,\n" 7855 "};"); 7856 verifyFormat( 7857 "@{\n" 7858 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7859 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7860 "};"); 7861 7862 // We should try to be robust in case someone forgets the "@". 7863 verifyFormat("NSDictionary *d = {\n" 7864 " @\"nam\" : NSUserNam(),\n" 7865 " @\"dte\" : [NSDate date],\n" 7866 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7867 "};"); 7868 verifyFormat("NSMutableDictionary *dictionary =\n" 7869 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7870 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7871 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7872 " cccccccccccccccc : ccccccccccccccc\n" 7873 " }];"); 7874 7875 // Ensure that casts before the key are kept on the same line as the key. 7876 verifyFormat( 7877 "NSDictionary *d = @{\n" 7878 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7879 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7880 "};"); 7881 } 7882 7883 TEST_F(FormatTest, ObjCArrayLiterals) { 7884 verifyIncompleteFormat("@["); 7885 verifyFormat("@[]"); 7886 verifyFormat( 7887 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7888 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7889 verifyFormat("NSArray *array = @[ [foo description] ];"); 7890 7891 verifyFormat( 7892 "NSArray *some_variable = @[\n" 7893 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7894 " @\"aaaaaaaaaaaaaaaaa\",\n" 7895 " @\"aaaaaaaaaaaaaaaaa\",\n" 7896 " @\"aaaaaaaaaaaaaaaaa\",\n" 7897 "];"); 7898 verifyFormat( 7899 "NSArray *some_variable = @[\n" 7900 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7901 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7902 "];"); 7903 verifyFormat("NSArray *some_variable = @[\n" 7904 " @\"aaaaaaaaaaaaaaaaa\",\n" 7905 " @\"aaaaaaaaaaaaaaaaa\",\n" 7906 " @\"aaaaaaaaaaaaaaaaa\",\n" 7907 " @\"aaaaaaaaaaaaaaaaa\",\n" 7908 "];"); 7909 verifyFormat("NSArray *array = @[\n" 7910 " @\"a\",\n" 7911 " @\"a\",\n" // Trailing comma -> one per line. 7912 "];"); 7913 7914 // We should try to be robust in case someone forgets the "@". 7915 verifyFormat("NSArray *some_variable = [\n" 7916 " @\"aaaaaaaaaaaaaaaaa\",\n" 7917 " @\"aaaaaaaaaaaaaaaaa\",\n" 7918 " @\"aaaaaaaaaaaaaaaaa\",\n" 7919 " @\"aaaaaaaaaaaaaaaaa\",\n" 7920 "];"); 7921 verifyFormat( 7922 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7923 " index:(NSUInteger)index\n" 7924 " nonDigitAttributes:\n" 7925 " (NSDictionary *)noDigitAttributes;"); 7926 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7927 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7928 "]];"); 7929 } 7930 7931 TEST_F(FormatTest, BreaksStringLiterals) { 7932 EXPECT_EQ("\"some text \"\n" 7933 "\"other\";", 7934 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7935 EXPECT_EQ("\"some text \"\n" 7936 "\"other\";", 7937 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7938 EXPECT_EQ( 7939 "#define A \\\n" 7940 " \"some \" \\\n" 7941 " \"text \" \\\n" 7942 " \"other\";", 7943 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7944 EXPECT_EQ( 7945 "#define A \\\n" 7946 " \"so \" \\\n" 7947 " \"text \" \\\n" 7948 " \"other\";", 7949 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7950 7951 EXPECT_EQ("\"some text\"", 7952 format("\"some text\"", getLLVMStyleWithColumns(1))); 7953 EXPECT_EQ("\"some text\"", 7954 format("\"some text\"", getLLVMStyleWithColumns(11))); 7955 EXPECT_EQ("\"some \"\n" 7956 "\"text\"", 7957 format("\"some text\"", getLLVMStyleWithColumns(10))); 7958 EXPECT_EQ("\"some \"\n" 7959 "\"text\"", 7960 format("\"some text\"", getLLVMStyleWithColumns(7))); 7961 EXPECT_EQ("\"some\"\n" 7962 "\" tex\"\n" 7963 "\"t\"", 7964 format("\"some text\"", getLLVMStyleWithColumns(6))); 7965 EXPECT_EQ("\"some\"\n" 7966 "\" tex\"\n" 7967 "\" and\"", 7968 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7969 EXPECT_EQ("\"some\"\n" 7970 "\"/tex\"\n" 7971 "\"/and\"", 7972 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7973 7974 EXPECT_EQ("variable =\n" 7975 " \"long string \"\n" 7976 " \"literal\";", 7977 format("variable = \"long string literal\";", 7978 getLLVMStyleWithColumns(20))); 7979 7980 EXPECT_EQ("variable = f(\n" 7981 " \"long string \"\n" 7982 " \"literal\",\n" 7983 " short,\n" 7984 " loooooooooooooooooooong);", 7985 format("variable = f(\"long string literal\", short, " 7986 "loooooooooooooooooooong);", 7987 getLLVMStyleWithColumns(20))); 7988 7989 EXPECT_EQ( 7990 "f(g(\"long string \"\n" 7991 " \"literal\"),\n" 7992 " b);", 7993 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7994 EXPECT_EQ("f(g(\"long string \"\n" 7995 " \"literal\",\n" 7996 " a),\n" 7997 " b);", 7998 format("f(g(\"long string literal\", a), b);", 7999 getLLVMStyleWithColumns(20))); 8000 EXPECT_EQ( 8001 "f(\"one two\".split(\n" 8002 " variable));", 8003 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8004 EXPECT_EQ("f(\"one two three four five six \"\n" 8005 " \"seven\".split(\n" 8006 " really_looooong_variable));", 8007 format("f(\"one two three four five six seven\"." 8008 "split(really_looooong_variable));", 8009 getLLVMStyleWithColumns(33))); 8010 8011 EXPECT_EQ("f(\"some \"\n" 8012 " \"text\",\n" 8013 " other);", 8014 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8015 8016 // Only break as a last resort. 8017 verifyFormat( 8018 "aaaaaaaaaaaaaaaaaaaa(\n" 8019 " aaaaaaaaaaaaaaaaaaaa,\n" 8020 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8021 8022 EXPECT_EQ("\"splitmea\"\n" 8023 "\"trandomp\"\n" 8024 "\"oint\"", 8025 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8026 8027 EXPECT_EQ("\"split/\"\n" 8028 "\"pathat/\"\n" 8029 "\"slashes\"", 8030 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8031 8032 EXPECT_EQ("\"split/\"\n" 8033 "\"pathat/\"\n" 8034 "\"slashes\"", 8035 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8036 EXPECT_EQ("\"split at \"\n" 8037 "\"spaces/at/\"\n" 8038 "\"slashes.at.any$\"\n" 8039 "\"non-alphanumeric%\"\n" 8040 "\"1111111111characte\"\n" 8041 "\"rs\"", 8042 format("\"split at " 8043 "spaces/at/" 8044 "slashes.at." 8045 "any$non-" 8046 "alphanumeric%" 8047 "1111111111characte" 8048 "rs\"", 8049 getLLVMStyleWithColumns(20))); 8050 8051 // Verify that splitting the strings understands 8052 // Style::AlwaysBreakBeforeMultilineStrings. 8053 EXPECT_EQ( 8054 "aaaaaaaaaaaa(\n" 8055 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8056 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8057 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8058 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8059 "aaaaaaaaaaaaaaaaaaaaaa\");", 8060 getGoogleStyle())); 8061 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8062 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8063 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8064 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8065 "aaaaaaaaaaaaaaaaaaaaaa\";", 8066 getGoogleStyle())); 8067 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8068 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8069 format("llvm::outs() << " 8070 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8071 "aaaaaaaaaaaaaaaaaaa\";")); 8072 EXPECT_EQ("ffff(\n" 8073 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8074 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8075 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8076 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8077 getGoogleStyle())); 8078 8079 FormatStyle Style = getLLVMStyleWithColumns(12); 8080 Style.BreakStringLiterals = false; 8081 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8082 8083 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8084 AlignLeft.AlignEscapedNewlinesLeft = true; 8085 EXPECT_EQ("#define A \\\n" 8086 " \"some \" \\\n" 8087 " \"text \" \\\n" 8088 " \"other\";", 8089 format("#define A \"some text other\";", AlignLeft)); 8090 } 8091 8092 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8093 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8094 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8095 EXPECT_EQ("int i = a(b());", 8096 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8097 } 8098 8099 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8100 EXPECT_EQ( 8101 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8102 "(\n" 8103 " \"x\t\");", 8104 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8105 "aaaaaaa(" 8106 "\"x\t\");")); 8107 } 8108 8109 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8110 EXPECT_EQ( 8111 "u8\"utf8 string \"\n" 8112 "u8\"literal\";", 8113 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8114 EXPECT_EQ( 8115 "u\"utf16 string \"\n" 8116 "u\"literal\";", 8117 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8118 EXPECT_EQ( 8119 "U\"utf32 string \"\n" 8120 "U\"literal\";", 8121 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8122 EXPECT_EQ("L\"wide string \"\n" 8123 "L\"literal\";", 8124 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8125 EXPECT_EQ("@\"NSString \"\n" 8126 "@\"literal\";", 8127 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8128 8129 // This input makes clang-format try to split the incomplete unicode escape 8130 // sequence, which used to lead to a crasher. 8131 verifyNoCrash( 8132 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8133 getLLVMStyleWithColumns(60)); 8134 } 8135 8136 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8137 FormatStyle Style = getGoogleStyleWithColumns(15); 8138 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8139 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8140 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8141 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8142 EXPECT_EQ("u8R\"x(raw literal)x\";", 8143 format("u8R\"x(raw literal)x\";", Style)); 8144 } 8145 8146 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8147 FormatStyle Style = getLLVMStyleWithColumns(20); 8148 EXPECT_EQ( 8149 "_T(\"aaaaaaaaaaaaaa\")\n" 8150 "_T(\"aaaaaaaaaaaaaa\")\n" 8151 "_T(\"aaaaaaaaaaaa\")", 8152 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8153 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8154 " _T(\"aaaaaa\"),\n" 8155 " z);", 8156 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8157 8158 // FIXME: Handle embedded spaces in one iteration. 8159 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8160 // "_T(\"aaaaaaaaaaaaa\")\n" 8161 // "_T(\"aaaaaaaaaaaaa\")\n" 8162 // "_T(\"a\")", 8163 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8164 // getLLVMStyleWithColumns(20))); 8165 EXPECT_EQ( 8166 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8167 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8168 EXPECT_EQ("f(\n" 8169 "#if !TEST\n" 8170 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8171 "#endif\n" 8172 " );", 8173 format("f(\n" 8174 "#if !TEST\n" 8175 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8176 "#endif\n" 8177 ");")); 8178 EXPECT_EQ("f(\n" 8179 "\n" 8180 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8181 format("f(\n" 8182 "\n" 8183 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8184 } 8185 8186 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8187 EXPECT_EQ( 8188 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8191 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8194 } 8195 8196 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8197 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8198 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8199 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8200 "multiline raw string literal xxxxxxxxxxxxxx\n" 8201 ")x\",\n" 8202 " a),\n" 8203 " b);", 8204 format("fffffffffff(g(R\"x(\n" 8205 "multiline raw string literal xxxxxxxxxxxxxx\n" 8206 ")x\", a), b);", 8207 getGoogleStyleWithColumns(20))); 8208 EXPECT_EQ("fffffffffff(\n" 8209 " g(R\"x(qqq\n" 8210 "multiline raw string literal xxxxxxxxxxxxxx\n" 8211 ")x\",\n" 8212 " a),\n" 8213 " b);", 8214 format("fffffffffff(g(R\"x(qqq\n" 8215 "multiline raw string literal xxxxxxxxxxxxxx\n" 8216 ")x\", a), b);", 8217 getGoogleStyleWithColumns(20))); 8218 8219 EXPECT_EQ("fffffffffff(R\"x(\n" 8220 "multiline raw string literal xxxxxxxxxxxxxx\n" 8221 ")x\");", 8222 format("fffffffffff(R\"x(\n" 8223 "multiline raw string literal xxxxxxxxxxxxxx\n" 8224 ")x\");", 8225 getGoogleStyleWithColumns(20))); 8226 EXPECT_EQ("fffffffffff(R\"x(\n" 8227 "multiline raw string literal xxxxxxxxxxxxxx\n" 8228 ")x\" + bbbbbb);", 8229 format("fffffffffff(R\"x(\n" 8230 "multiline raw string literal xxxxxxxxxxxxxx\n" 8231 ")x\" + bbbbbb);", 8232 getGoogleStyleWithColumns(20))); 8233 EXPECT_EQ("fffffffffff(\n" 8234 " R\"x(\n" 8235 "multiline raw string literal xxxxxxxxxxxxxx\n" 8236 ")x\" +\n" 8237 " bbbbbb);", 8238 format("fffffffffff(\n" 8239 " R\"x(\n" 8240 "multiline raw string literal xxxxxxxxxxxxxx\n" 8241 ")x\" + bbbbbb);", 8242 getGoogleStyleWithColumns(20))); 8243 } 8244 8245 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8246 verifyFormat("string a = \"unterminated;"); 8247 EXPECT_EQ("function(\"unterminated,\n" 8248 " OtherParameter);", 8249 format("function( \"unterminated,\n" 8250 " OtherParameter);")); 8251 } 8252 8253 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8254 FormatStyle Style = getLLVMStyle(); 8255 Style.Standard = FormatStyle::LS_Cpp03; 8256 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8257 format("#define x(_a) printf(\"foo\"_a);", Style)); 8258 } 8259 8260 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8261 8262 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8263 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8264 " \"ddeeefff\");", 8265 format("someFunction(\"aaabbbcccdddeeefff\");", 8266 getLLVMStyleWithColumns(25))); 8267 EXPECT_EQ("someFunction1234567890(\n" 8268 " \"aaabbbcccdddeeefff\");", 8269 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8270 getLLVMStyleWithColumns(26))); 8271 EXPECT_EQ("someFunction1234567890(\n" 8272 " \"aaabbbcccdddeeeff\"\n" 8273 " \"f\");", 8274 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8275 getLLVMStyleWithColumns(25))); 8276 EXPECT_EQ("someFunction1234567890(\n" 8277 " \"aaabbbcccdddeeeff\"\n" 8278 " \"f\");", 8279 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8280 getLLVMStyleWithColumns(24))); 8281 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8282 " \"ddde \"\n" 8283 " \"efff\");", 8284 format("someFunction(\"aaabbbcc ddde efff\");", 8285 getLLVMStyleWithColumns(25))); 8286 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8287 " \"ddeeefff\");", 8288 format("someFunction(\"aaabbbccc ddeeefff\");", 8289 getLLVMStyleWithColumns(25))); 8290 EXPECT_EQ("someFunction1234567890(\n" 8291 " \"aaabb \"\n" 8292 " \"cccdddeeefff\");", 8293 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8294 getLLVMStyleWithColumns(25))); 8295 EXPECT_EQ("#define A \\\n" 8296 " string s = \\\n" 8297 " \"123456789\" \\\n" 8298 " \"0\"; \\\n" 8299 " int i;", 8300 format("#define A string s = \"1234567890\"; int i;", 8301 getLLVMStyleWithColumns(20))); 8302 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8303 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8304 " \"dddeeeff\"\n" 8305 " \"f\");", 8306 format("someFunction(\"aaabbbcc dddeeefff\");", 8307 getLLVMStyleWithColumns(25))); 8308 } 8309 8310 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8311 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8312 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8313 EXPECT_EQ("\"test\"\n" 8314 "\"\\n\"", 8315 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8316 EXPECT_EQ("\"tes\\\\\"\n" 8317 "\"n\"", 8318 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8319 EXPECT_EQ("\"\\\\\\\\\"\n" 8320 "\"\\n\"", 8321 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8322 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8323 EXPECT_EQ("\"\\uff01\"\n" 8324 "\"test\"", 8325 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8326 EXPECT_EQ("\"\\Uff01ff02\"", 8327 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8328 EXPECT_EQ("\"\\x000000000001\"\n" 8329 "\"next\"", 8330 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8331 EXPECT_EQ("\"\\x000000000001next\"", 8332 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8333 EXPECT_EQ("\"\\x000000000001\"", 8334 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8335 EXPECT_EQ("\"test\"\n" 8336 "\"\\000000\"\n" 8337 "\"000001\"", 8338 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8339 EXPECT_EQ("\"test\\000\"\n" 8340 "\"00000000\"\n" 8341 "\"1\"", 8342 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8343 } 8344 8345 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8346 verifyFormat("void f() {\n" 8347 " return g() {}\n" 8348 " void h() {}"); 8349 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8350 "g();\n" 8351 "}"); 8352 } 8353 8354 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8355 verifyFormat( 8356 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8357 } 8358 8359 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8360 verifyFormat("class X {\n" 8361 " void f() {\n" 8362 " }\n" 8363 "};", 8364 getLLVMStyleWithColumns(12)); 8365 } 8366 8367 TEST_F(FormatTest, ConfigurableIndentWidth) { 8368 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8369 EightIndent.IndentWidth = 8; 8370 EightIndent.ContinuationIndentWidth = 8; 8371 verifyFormat("void f() {\n" 8372 " someFunction();\n" 8373 " if (true) {\n" 8374 " f();\n" 8375 " }\n" 8376 "}", 8377 EightIndent); 8378 verifyFormat("class X {\n" 8379 " void f() {\n" 8380 " }\n" 8381 "};", 8382 EightIndent); 8383 verifyFormat("int x[] = {\n" 8384 " call(),\n" 8385 " call()};", 8386 EightIndent); 8387 } 8388 8389 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8390 verifyFormat("double\n" 8391 "f();", 8392 getLLVMStyleWithColumns(8)); 8393 } 8394 8395 TEST_F(FormatTest, ConfigurableUseOfTab) { 8396 FormatStyle Tab = getLLVMStyleWithColumns(42); 8397 Tab.IndentWidth = 8; 8398 Tab.UseTab = FormatStyle::UT_Always; 8399 Tab.AlignEscapedNewlinesLeft = true; 8400 8401 EXPECT_EQ("if (aaaaaaaa && // q\n" 8402 " bb)\t\t// w\n" 8403 "\t;", 8404 format("if (aaaaaaaa &&// q\n" 8405 "bb)// w\n" 8406 ";", 8407 Tab)); 8408 EXPECT_EQ("if (aaa && bbb) // w\n" 8409 "\t;", 8410 format("if(aaa&&bbb)// w\n" 8411 ";", 8412 Tab)); 8413 8414 verifyFormat("class X {\n" 8415 "\tvoid f() {\n" 8416 "\t\tsomeFunction(parameter1,\n" 8417 "\t\t\t parameter2);\n" 8418 "\t}\n" 8419 "};", 8420 Tab); 8421 verifyFormat("#define A \\\n" 8422 "\tvoid f() { \\\n" 8423 "\t\tsomeFunction( \\\n" 8424 "\t\t parameter1, \\\n" 8425 "\t\t parameter2); \\\n" 8426 "\t}", 8427 Tab); 8428 8429 Tab.TabWidth = 4; 8430 Tab.IndentWidth = 8; 8431 verifyFormat("class TabWidth4Indent8 {\n" 8432 "\t\tvoid f() {\n" 8433 "\t\t\t\tsomeFunction(parameter1,\n" 8434 "\t\t\t\t\t\t\t parameter2);\n" 8435 "\t\t}\n" 8436 "};", 8437 Tab); 8438 8439 Tab.TabWidth = 4; 8440 Tab.IndentWidth = 4; 8441 verifyFormat("class TabWidth4Indent4 {\n" 8442 "\tvoid f() {\n" 8443 "\t\tsomeFunction(parameter1,\n" 8444 "\t\t\t\t\t parameter2);\n" 8445 "\t}\n" 8446 "};", 8447 Tab); 8448 8449 Tab.TabWidth = 8; 8450 Tab.IndentWidth = 4; 8451 verifyFormat("class TabWidth8Indent4 {\n" 8452 " void f() {\n" 8453 "\tsomeFunction(parameter1,\n" 8454 "\t\t parameter2);\n" 8455 " }\n" 8456 "};", 8457 Tab); 8458 8459 Tab.TabWidth = 8; 8460 Tab.IndentWidth = 8; 8461 EXPECT_EQ("/*\n" 8462 "\t a\t\tcomment\n" 8463 "\t in multiple lines\n" 8464 " */", 8465 format(" /*\t \t \n" 8466 " \t \t a\t\tcomment\t \t\n" 8467 " \t \t in multiple lines\t\n" 8468 " \t */", 8469 Tab)); 8470 8471 Tab.UseTab = FormatStyle::UT_ForIndentation; 8472 verifyFormat("{\n" 8473 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8474 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8475 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8476 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8477 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8478 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8479 "};", 8480 Tab); 8481 verifyFormat("enum AA {\n" 8482 "\ta1, // Force multiple lines\n" 8483 "\ta2,\n" 8484 "\ta3\n" 8485 "};", 8486 Tab); 8487 EXPECT_EQ("if (aaaaaaaa && // q\n" 8488 " bb) // w\n" 8489 "\t;", 8490 format("if (aaaaaaaa &&// q\n" 8491 "bb)// w\n" 8492 ";", 8493 Tab)); 8494 verifyFormat("class X {\n" 8495 "\tvoid f() {\n" 8496 "\t\tsomeFunction(parameter1,\n" 8497 "\t\t parameter2);\n" 8498 "\t}\n" 8499 "};", 8500 Tab); 8501 verifyFormat("{\n" 8502 "\tQ(\n" 8503 "\t {\n" 8504 "\t\t int a;\n" 8505 "\t\t someFunction(aaaaaaaa,\n" 8506 "\t\t bbbbbbb);\n" 8507 "\t },\n" 8508 "\t p);\n" 8509 "}", 8510 Tab); 8511 EXPECT_EQ("{\n" 8512 "\t/* aaaa\n" 8513 "\t bbbb */\n" 8514 "}", 8515 format("{\n" 8516 "/* aaaa\n" 8517 " bbbb */\n" 8518 "}", 8519 Tab)); 8520 EXPECT_EQ("{\n" 8521 "\t/*\n" 8522 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8523 "\t bbbbbbbbbbbbb\n" 8524 "\t*/\n" 8525 "}", 8526 format("{\n" 8527 "/*\n" 8528 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8529 "*/\n" 8530 "}", 8531 Tab)); 8532 EXPECT_EQ("{\n" 8533 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8534 "\t// bbbbbbbbbbbbb\n" 8535 "}", 8536 format("{\n" 8537 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8538 "}", 8539 Tab)); 8540 EXPECT_EQ("{\n" 8541 "\t/*\n" 8542 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8543 "\t bbbbbbbbbbbbb\n" 8544 "\t*/\n" 8545 "}", 8546 format("{\n" 8547 "\t/*\n" 8548 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8549 "\t*/\n" 8550 "}", 8551 Tab)); 8552 EXPECT_EQ("{\n" 8553 "\t/*\n" 8554 "\n" 8555 "\t*/\n" 8556 "}", 8557 format("{\n" 8558 "\t/*\n" 8559 "\n" 8560 "\t*/\n" 8561 "}", 8562 Tab)); 8563 EXPECT_EQ("{\n" 8564 "\t/*\n" 8565 " asdf\n" 8566 "\t*/\n" 8567 "}", 8568 format("{\n" 8569 "\t/*\n" 8570 " asdf\n" 8571 "\t*/\n" 8572 "}", 8573 Tab)); 8574 8575 Tab.UseTab = FormatStyle::UT_Never; 8576 EXPECT_EQ("/*\n" 8577 " a\t\tcomment\n" 8578 " in multiple lines\n" 8579 " */", 8580 format(" /*\t \t \n" 8581 " \t \t a\t\tcomment\t \t\n" 8582 " \t \t in multiple lines\t\n" 8583 " \t */", 8584 Tab)); 8585 EXPECT_EQ("/* some\n" 8586 " comment */", 8587 format(" \t \t /* some\n" 8588 " \t \t comment */", 8589 Tab)); 8590 EXPECT_EQ("int a; /* some\n" 8591 " comment */", 8592 format(" \t \t int a; /* some\n" 8593 " \t \t comment */", 8594 Tab)); 8595 8596 EXPECT_EQ("int a; /* some\n" 8597 "comment */", 8598 format(" \t \t int\ta; /* some\n" 8599 " \t \t comment */", 8600 Tab)); 8601 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8602 " comment */", 8603 format(" \t \t f(\"\t\t\"); /* some\n" 8604 " \t \t comment */", 8605 Tab)); 8606 EXPECT_EQ("{\n" 8607 " /*\n" 8608 " * Comment\n" 8609 " */\n" 8610 " int i;\n" 8611 "}", 8612 format("{\n" 8613 "\t/*\n" 8614 "\t * Comment\n" 8615 "\t */\n" 8616 "\t int i;\n" 8617 "}")); 8618 8619 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8620 Tab.TabWidth = 8; 8621 Tab.IndentWidth = 8; 8622 EXPECT_EQ("if (aaaaaaaa && // q\n" 8623 " bb) // w\n" 8624 "\t;", 8625 format("if (aaaaaaaa &&// q\n" 8626 "bb)// w\n" 8627 ";", 8628 Tab)); 8629 EXPECT_EQ("if (aaa && bbb) // w\n" 8630 "\t;", 8631 format("if(aaa&&bbb)// w\n" 8632 ";", 8633 Tab)); 8634 verifyFormat("class X {\n" 8635 "\tvoid f() {\n" 8636 "\t\tsomeFunction(parameter1,\n" 8637 "\t\t\t parameter2);\n" 8638 "\t}\n" 8639 "};", 8640 Tab); 8641 verifyFormat("#define A \\\n" 8642 "\tvoid f() { \\\n" 8643 "\t\tsomeFunction( \\\n" 8644 "\t\t parameter1, \\\n" 8645 "\t\t parameter2); \\\n" 8646 "\t}", 8647 Tab); 8648 Tab.TabWidth = 4; 8649 Tab.IndentWidth = 8; 8650 verifyFormat("class TabWidth4Indent8 {\n" 8651 "\t\tvoid f() {\n" 8652 "\t\t\t\tsomeFunction(parameter1,\n" 8653 "\t\t\t\t\t\t\t parameter2);\n" 8654 "\t\t}\n" 8655 "};", 8656 Tab); 8657 Tab.TabWidth = 4; 8658 Tab.IndentWidth = 4; 8659 verifyFormat("class TabWidth4Indent4 {\n" 8660 "\tvoid f() {\n" 8661 "\t\tsomeFunction(parameter1,\n" 8662 "\t\t\t\t\t parameter2);\n" 8663 "\t}\n" 8664 "};", 8665 Tab); 8666 Tab.TabWidth = 8; 8667 Tab.IndentWidth = 4; 8668 verifyFormat("class TabWidth8Indent4 {\n" 8669 " void f() {\n" 8670 "\tsomeFunction(parameter1,\n" 8671 "\t\t parameter2);\n" 8672 " }\n" 8673 "};", 8674 Tab); 8675 Tab.TabWidth = 8; 8676 Tab.IndentWidth = 8; 8677 EXPECT_EQ("/*\n" 8678 "\t a\t\tcomment\n" 8679 "\t in multiple lines\n" 8680 " */", 8681 format(" /*\t \t \n" 8682 " \t \t a\t\tcomment\t \t\n" 8683 " \t \t in multiple lines\t\n" 8684 " \t */", 8685 Tab)); 8686 verifyFormat("{\n" 8687 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8688 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8689 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8690 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8691 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8692 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8693 "};", 8694 Tab); 8695 verifyFormat("enum AA {\n" 8696 "\ta1, // Force multiple lines\n" 8697 "\ta2,\n" 8698 "\ta3\n" 8699 "};", 8700 Tab); 8701 EXPECT_EQ("if (aaaaaaaa && // q\n" 8702 " bb) // w\n" 8703 "\t;", 8704 format("if (aaaaaaaa &&// q\n" 8705 "bb)// w\n" 8706 ";", 8707 Tab)); 8708 verifyFormat("class X {\n" 8709 "\tvoid f() {\n" 8710 "\t\tsomeFunction(parameter1,\n" 8711 "\t\t\t parameter2);\n" 8712 "\t}\n" 8713 "};", 8714 Tab); 8715 verifyFormat("{\n" 8716 "\tQ(\n" 8717 "\t {\n" 8718 "\t\t int a;\n" 8719 "\t\t someFunction(aaaaaaaa,\n" 8720 "\t\t\t\t bbbbbbb);\n" 8721 "\t },\n" 8722 "\t p);\n" 8723 "}", 8724 Tab); 8725 EXPECT_EQ("{\n" 8726 "\t/* aaaa\n" 8727 "\t bbbb */\n" 8728 "}", 8729 format("{\n" 8730 "/* aaaa\n" 8731 " bbbb */\n" 8732 "}", 8733 Tab)); 8734 EXPECT_EQ("{\n" 8735 "\t/*\n" 8736 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8737 "\t bbbbbbbbbbbbb\n" 8738 "\t*/\n" 8739 "}", 8740 format("{\n" 8741 "/*\n" 8742 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8743 "*/\n" 8744 "}", 8745 Tab)); 8746 EXPECT_EQ("{\n" 8747 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8748 "\t// bbbbbbbbbbbbb\n" 8749 "}", 8750 format("{\n" 8751 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8752 "}", 8753 Tab)); 8754 EXPECT_EQ("{\n" 8755 "\t/*\n" 8756 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8757 "\t bbbbbbbbbbbbb\n" 8758 "\t*/\n" 8759 "}", 8760 format("{\n" 8761 "\t/*\n" 8762 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8763 "\t*/\n" 8764 "}", 8765 Tab)); 8766 EXPECT_EQ("{\n" 8767 "\t/*\n" 8768 "\n" 8769 "\t*/\n" 8770 "}", 8771 format("{\n" 8772 "\t/*\n" 8773 "\n" 8774 "\t*/\n" 8775 "}", 8776 Tab)); 8777 EXPECT_EQ("{\n" 8778 "\t/*\n" 8779 " asdf\n" 8780 "\t*/\n" 8781 "}", 8782 format("{\n" 8783 "\t/*\n" 8784 " asdf\n" 8785 "\t*/\n" 8786 "}", 8787 Tab)); 8788 EXPECT_EQ("/*\n" 8789 "\t a\t\tcomment\n" 8790 "\t in multiple lines\n" 8791 " */", 8792 format(" /*\t \t \n" 8793 " \t \t a\t\tcomment\t \t\n" 8794 " \t \t in multiple lines\t\n" 8795 " \t */", 8796 Tab)); 8797 EXPECT_EQ("/* some\n" 8798 " comment */", 8799 format(" \t \t /* some\n" 8800 " \t \t comment */", 8801 Tab)); 8802 EXPECT_EQ("int a; /* some\n" 8803 " comment */", 8804 format(" \t \t int a; /* some\n" 8805 " \t \t comment */", 8806 Tab)); 8807 EXPECT_EQ("int a; /* some\n" 8808 "comment */", 8809 format(" \t \t int\ta; /* some\n" 8810 " \t \t comment */", 8811 Tab)); 8812 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8813 " comment */", 8814 format(" \t \t f(\"\t\t\"); /* some\n" 8815 " \t \t comment */", 8816 Tab)); 8817 EXPECT_EQ("{\n" 8818 " /*\n" 8819 " * Comment\n" 8820 " */\n" 8821 " int i;\n" 8822 "}", 8823 format("{\n" 8824 "\t/*\n" 8825 "\t * Comment\n" 8826 "\t */\n" 8827 "\t int i;\n" 8828 "}")); 8829 Tab.AlignConsecutiveAssignments = true; 8830 Tab.AlignConsecutiveDeclarations = true; 8831 Tab.TabWidth = 4; 8832 Tab.IndentWidth = 4; 8833 verifyFormat("class Assign {\n" 8834 "\tvoid f() {\n" 8835 "\t\tint x = 123;\n" 8836 "\t\tint random = 4;\n" 8837 "\t\tstd::string alphabet =\n" 8838 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8839 "\t}\n" 8840 "};", 8841 Tab); 8842 } 8843 8844 TEST_F(FormatTest, CalculatesOriginalColumn) { 8845 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8846 "q\"; /* some\n" 8847 " comment */", 8848 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8849 "q\"; /* some\n" 8850 " comment */", 8851 getLLVMStyle())); 8852 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8853 "/* some\n" 8854 " comment */", 8855 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8856 " /* some\n" 8857 " comment */", 8858 getLLVMStyle())); 8859 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8860 "qqq\n" 8861 "/* some\n" 8862 " comment */", 8863 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8864 "qqq\n" 8865 " /* some\n" 8866 " comment */", 8867 getLLVMStyle())); 8868 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8869 "wwww; /* some\n" 8870 " comment */", 8871 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8872 "wwww; /* some\n" 8873 " comment */", 8874 getLLVMStyle())); 8875 } 8876 8877 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8878 FormatStyle NoSpace = getLLVMStyle(); 8879 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8880 8881 verifyFormat("while(true)\n" 8882 " continue;", 8883 NoSpace); 8884 verifyFormat("for(;;)\n" 8885 " continue;", 8886 NoSpace); 8887 verifyFormat("if(true)\n" 8888 " f();\n" 8889 "else if(true)\n" 8890 " f();", 8891 NoSpace); 8892 verifyFormat("do {\n" 8893 " do_something();\n" 8894 "} while(something());", 8895 NoSpace); 8896 verifyFormat("switch(x) {\n" 8897 "default:\n" 8898 " break;\n" 8899 "}", 8900 NoSpace); 8901 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8902 verifyFormat("size_t x = sizeof(x);", NoSpace); 8903 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8904 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8905 verifyFormat("alignas(128) char a[128];", NoSpace); 8906 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8907 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8908 verifyFormat("int f() throw(Deprecated);", NoSpace); 8909 verifyFormat("typedef void (*cb)(int);", NoSpace); 8910 verifyFormat("T A::operator()();", NoSpace); 8911 verifyFormat("X A::operator++(T);", NoSpace); 8912 8913 FormatStyle Space = getLLVMStyle(); 8914 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8915 8916 verifyFormat("int f ();", Space); 8917 verifyFormat("void f (int a, T b) {\n" 8918 " while (true)\n" 8919 " continue;\n" 8920 "}", 8921 Space); 8922 verifyFormat("if (true)\n" 8923 " f ();\n" 8924 "else if (true)\n" 8925 " f ();", 8926 Space); 8927 verifyFormat("do {\n" 8928 " do_something ();\n" 8929 "} while (something ());", 8930 Space); 8931 verifyFormat("switch (x) {\n" 8932 "default:\n" 8933 " break;\n" 8934 "}", 8935 Space); 8936 verifyFormat("A::A () : a (1) {}", Space); 8937 verifyFormat("void f () __attribute__ ((asdf));", Space); 8938 verifyFormat("*(&a + 1);\n" 8939 "&((&a)[1]);\n" 8940 "a[(b + c) * d];\n" 8941 "(((a + 1) * 2) + 3) * 4;", 8942 Space); 8943 verifyFormat("#define A(x) x", Space); 8944 verifyFormat("#define A (x) x", Space); 8945 verifyFormat("#if defined(x)\n" 8946 "#endif", 8947 Space); 8948 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8949 verifyFormat("size_t x = sizeof (x);", Space); 8950 verifyFormat("auto f (int x) -> decltype (x);", Space); 8951 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8952 verifyFormat("alignas (128) char a[128];", Space); 8953 verifyFormat("size_t x = alignof (MyType);", Space); 8954 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8955 verifyFormat("int f () throw (Deprecated);", Space); 8956 verifyFormat("typedef void (*cb) (int);", Space); 8957 verifyFormat("T A::operator() ();", Space); 8958 verifyFormat("X A::operator++ (T);", Space); 8959 } 8960 8961 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8962 FormatStyle Spaces = getLLVMStyle(); 8963 8964 Spaces.SpacesInParentheses = true; 8965 verifyFormat("call( x, y, z );", Spaces); 8966 verifyFormat("call();", Spaces); 8967 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8968 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8969 Spaces); 8970 verifyFormat("while ( (bool)1 )\n" 8971 " continue;", 8972 Spaces); 8973 verifyFormat("for ( ;; )\n" 8974 " continue;", 8975 Spaces); 8976 verifyFormat("if ( true )\n" 8977 " f();\n" 8978 "else if ( true )\n" 8979 " f();", 8980 Spaces); 8981 verifyFormat("do {\n" 8982 " do_something( (int)i );\n" 8983 "} while ( something() );", 8984 Spaces); 8985 verifyFormat("switch ( x ) {\n" 8986 "default:\n" 8987 " break;\n" 8988 "}", 8989 Spaces); 8990 8991 Spaces.SpacesInParentheses = false; 8992 Spaces.SpacesInCStyleCastParentheses = true; 8993 verifyFormat("Type *A = ( Type * )P;", Spaces); 8994 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8995 verifyFormat("x = ( int32 )y;", Spaces); 8996 verifyFormat("int a = ( int )(2.0f);", Spaces); 8997 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8998 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8999 verifyFormat("#define x (( int )-1)", Spaces); 9000 9001 // Run the first set of tests again with: 9002 Spaces.SpacesInParentheses = false; 9003 Spaces.SpaceInEmptyParentheses = true; 9004 Spaces.SpacesInCStyleCastParentheses = true; 9005 verifyFormat("call(x, y, z);", Spaces); 9006 verifyFormat("call( );", Spaces); 9007 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9008 verifyFormat("while (( bool )1)\n" 9009 " continue;", 9010 Spaces); 9011 verifyFormat("for (;;)\n" 9012 " continue;", 9013 Spaces); 9014 verifyFormat("if (true)\n" 9015 " f( );\n" 9016 "else if (true)\n" 9017 " f( );", 9018 Spaces); 9019 verifyFormat("do {\n" 9020 " do_something(( int )i);\n" 9021 "} while (something( ));", 9022 Spaces); 9023 verifyFormat("switch (x) {\n" 9024 "default:\n" 9025 " break;\n" 9026 "}", 9027 Spaces); 9028 9029 // Run the first set of tests again with: 9030 Spaces.SpaceAfterCStyleCast = true; 9031 verifyFormat("call(x, y, z);", Spaces); 9032 verifyFormat("call( );", Spaces); 9033 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9034 verifyFormat("while (( bool ) 1)\n" 9035 " continue;", 9036 Spaces); 9037 verifyFormat("for (;;)\n" 9038 " continue;", 9039 Spaces); 9040 verifyFormat("if (true)\n" 9041 " f( );\n" 9042 "else if (true)\n" 9043 " f( );", 9044 Spaces); 9045 verifyFormat("do {\n" 9046 " do_something(( int ) i);\n" 9047 "} while (something( ));", 9048 Spaces); 9049 verifyFormat("switch (x) {\n" 9050 "default:\n" 9051 " break;\n" 9052 "}", 9053 Spaces); 9054 9055 // Run subset of tests again with: 9056 Spaces.SpacesInCStyleCastParentheses = false; 9057 Spaces.SpaceAfterCStyleCast = true; 9058 verifyFormat("while ((bool) 1)\n" 9059 " continue;", 9060 Spaces); 9061 verifyFormat("do {\n" 9062 " do_something((int) i);\n" 9063 "} while (something( ));", 9064 Spaces); 9065 } 9066 9067 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9068 verifyFormat("int a[5];"); 9069 verifyFormat("a[3] += 42;"); 9070 9071 FormatStyle Spaces = getLLVMStyle(); 9072 Spaces.SpacesInSquareBrackets = true; 9073 // Lambdas unchanged. 9074 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9075 verifyFormat("return [i, args...] {};", Spaces); 9076 9077 // Not lambdas. 9078 verifyFormat("int a[ 5 ];", Spaces); 9079 verifyFormat("a[ 3 ] += 42;", Spaces); 9080 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9081 verifyFormat("double &operator[](int i) { return 0; }\n" 9082 "int i;", 9083 Spaces); 9084 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9085 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9086 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9087 } 9088 9089 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9090 verifyFormat("int a = 5;"); 9091 verifyFormat("a += 42;"); 9092 verifyFormat("a or_eq 8;"); 9093 9094 FormatStyle Spaces = getLLVMStyle(); 9095 Spaces.SpaceBeforeAssignmentOperators = false; 9096 verifyFormat("int a= 5;", Spaces); 9097 verifyFormat("a+= 42;", Spaces); 9098 verifyFormat("a or_eq 8;", Spaces); 9099 } 9100 9101 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9102 FormatStyle Alignment = getLLVMStyle(); 9103 Alignment.AlignConsecutiveAssignments = false; 9104 verifyFormat("int a = 5;\n" 9105 "int oneTwoThree = 123;", 9106 Alignment); 9107 verifyFormat("int a = 5;\n" 9108 "int oneTwoThree = 123;", 9109 Alignment); 9110 9111 Alignment.AlignConsecutiveAssignments = true; 9112 verifyFormat("int a = 5;\n" 9113 "int oneTwoThree = 123;", 9114 Alignment); 9115 verifyFormat("int a = method();\n" 9116 "int oneTwoThree = 133;", 9117 Alignment); 9118 verifyFormat("a &= 5;\n" 9119 "bcd *= 5;\n" 9120 "ghtyf += 5;\n" 9121 "dvfvdb -= 5;\n" 9122 "a /= 5;\n" 9123 "vdsvsv %= 5;\n" 9124 "sfdbddfbdfbb ^= 5;\n" 9125 "dvsdsv |= 5;\n" 9126 "int dsvvdvsdvvv = 123;", 9127 Alignment); 9128 verifyFormat("int i = 1, j = 10;\n" 9129 "something = 2000;", 9130 Alignment); 9131 verifyFormat("something = 2000;\n" 9132 "int i = 1, j = 10;\n", 9133 Alignment); 9134 verifyFormat("something = 2000;\n" 9135 "another = 911;\n" 9136 "int i = 1, j = 10;\n" 9137 "oneMore = 1;\n" 9138 "i = 2;", 9139 Alignment); 9140 verifyFormat("int a = 5;\n" 9141 "int one = 1;\n" 9142 "method();\n" 9143 "int oneTwoThree = 123;\n" 9144 "int oneTwo = 12;", 9145 Alignment); 9146 verifyFormat("int oneTwoThree = 123;\n" 9147 "int oneTwo = 12;\n" 9148 "method();\n", 9149 Alignment); 9150 verifyFormat("int oneTwoThree = 123; // comment\n" 9151 "int oneTwo = 12; // comment", 9152 Alignment); 9153 EXPECT_EQ("int a = 5;\n" 9154 "\n" 9155 "int oneTwoThree = 123;", 9156 format("int a = 5;\n" 9157 "\n" 9158 "int oneTwoThree= 123;", 9159 Alignment)); 9160 EXPECT_EQ("int a = 5;\n" 9161 "int one = 1;\n" 9162 "\n" 9163 "int oneTwoThree = 123;", 9164 format("int a = 5;\n" 9165 "int one = 1;\n" 9166 "\n" 9167 "int oneTwoThree = 123;", 9168 Alignment)); 9169 EXPECT_EQ("int a = 5;\n" 9170 "int one = 1;\n" 9171 "\n" 9172 "int oneTwoThree = 123;\n" 9173 "int oneTwo = 12;", 9174 format("int a = 5;\n" 9175 "int one = 1;\n" 9176 "\n" 9177 "int oneTwoThree = 123;\n" 9178 "int oneTwo = 12;", 9179 Alignment)); 9180 Alignment.AlignEscapedNewlinesLeft = true; 9181 verifyFormat("#define A \\\n" 9182 " int aaaa = 12; \\\n" 9183 " int b = 23; \\\n" 9184 " int ccc = 234; \\\n" 9185 " int dddddddddd = 2345;", 9186 Alignment); 9187 Alignment.AlignEscapedNewlinesLeft = false; 9188 verifyFormat("#define A " 9189 " \\\n" 9190 " int aaaa = 12; " 9191 " \\\n" 9192 " int b = 23; " 9193 " \\\n" 9194 " int ccc = 234; " 9195 " \\\n" 9196 " int dddddddddd = 2345;", 9197 Alignment); 9198 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9199 "k = 4, int l = 5,\n" 9200 " int m = 6) {\n" 9201 " int j = 10;\n" 9202 " otherThing = 1;\n" 9203 "}", 9204 Alignment); 9205 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9206 " int i = 1;\n" 9207 " int j = 2;\n" 9208 " int big = 10000;\n" 9209 "}", 9210 Alignment); 9211 verifyFormat("class C {\n" 9212 "public:\n" 9213 " int i = 1;\n" 9214 " virtual void f() = 0;\n" 9215 "};", 9216 Alignment); 9217 verifyFormat("int i = 1;\n" 9218 "if (SomeType t = getSomething()) {\n" 9219 "}\n" 9220 "int j = 2;\n" 9221 "int big = 10000;", 9222 Alignment); 9223 verifyFormat("int j = 7;\n" 9224 "for (int k = 0; k < N; ++k) {\n" 9225 "}\n" 9226 "int j = 2;\n" 9227 "int big = 10000;\n" 9228 "}", 9229 Alignment); 9230 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9231 verifyFormat("int i = 1;\n" 9232 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9233 " = someLooooooooooooooooongFunction();\n" 9234 "int j = 2;", 9235 Alignment); 9236 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9237 verifyFormat("int i = 1;\n" 9238 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9239 " someLooooooooooooooooongFunction();\n" 9240 "int j = 2;", 9241 Alignment); 9242 9243 verifyFormat("auto lambda = []() {\n" 9244 " auto i = 0;\n" 9245 " return 0;\n" 9246 "};\n" 9247 "int i = 0;\n" 9248 "auto v = type{\n" 9249 " i = 1, //\n" 9250 " (i = 2), //\n" 9251 " i = 3 //\n" 9252 "};", 9253 Alignment); 9254 9255 // FIXME: Should align all three assignments 9256 verifyFormat( 9257 "int i = 1;\n" 9258 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9259 " loooooooooooooooooooooongParameterB);\n" 9260 "int j = 2;", 9261 Alignment); 9262 9263 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9264 " typename B = very_long_type_name_1,\n" 9265 " typename T_2 = very_long_type_name_2>\n" 9266 "auto foo() {}\n", 9267 Alignment); 9268 verifyFormat("int a, b = 1;\n" 9269 "int c = 2;\n" 9270 "int dd = 3;\n", 9271 Alignment); 9272 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9273 "float b[1][] = {{3.f}};\n", 9274 Alignment); 9275 } 9276 9277 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9278 FormatStyle Alignment = getLLVMStyle(); 9279 Alignment.AlignConsecutiveDeclarations = false; 9280 verifyFormat("float const a = 5;\n" 9281 "int oneTwoThree = 123;", 9282 Alignment); 9283 verifyFormat("int a = 5;\n" 9284 "float const oneTwoThree = 123;", 9285 Alignment); 9286 9287 Alignment.AlignConsecutiveDeclarations = true; 9288 verifyFormat("float const a = 5;\n" 9289 "int oneTwoThree = 123;", 9290 Alignment); 9291 verifyFormat("int a = method();\n" 9292 "float const oneTwoThree = 133;", 9293 Alignment); 9294 verifyFormat("int i = 1, j = 10;\n" 9295 "something = 2000;", 9296 Alignment); 9297 verifyFormat("something = 2000;\n" 9298 "int i = 1, j = 10;\n", 9299 Alignment); 9300 verifyFormat("float something = 2000;\n" 9301 "double another = 911;\n" 9302 "int i = 1, j = 10;\n" 9303 "const int *oneMore = 1;\n" 9304 "unsigned i = 2;", 9305 Alignment); 9306 verifyFormat("float a = 5;\n" 9307 "int one = 1;\n" 9308 "method();\n" 9309 "const double oneTwoThree = 123;\n" 9310 "const unsigned int oneTwo = 12;", 9311 Alignment); 9312 verifyFormat("int oneTwoThree{0}; // comment\n" 9313 "unsigned oneTwo; // comment", 9314 Alignment); 9315 EXPECT_EQ("float const a = 5;\n" 9316 "\n" 9317 "int oneTwoThree = 123;", 9318 format("float const a = 5;\n" 9319 "\n" 9320 "int oneTwoThree= 123;", 9321 Alignment)); 9322 EXPECT_EQ("float a = 5;\n" 9323 "int one = 1;\n" 9324 "\n" 9325 "unsigned oneTwoThree = 123;", 9326 format("float a = 5;\n" 9327 "int one = 1;\n" 9328 "\n" 9329 "unsigned oneTwoThree = 123;", 9330 Alignment)); 9331 EXPECT_EQ("float a = 5;\n" 9332 "int one = 1;\n" 9333 "\n" 9334 "unsigned oneTwoThree = 123;\n" 9335 "int oneTwo = 12;", 9336 format("float a = 5;\n" 9337 "int one = 1;\n" 9338 "\n" 9339 "unsigned oneTwoThree = 123;\n" 9340 "int oneTwo = 12;", 9341 Alignment)); 9342 Alignment.AlignConsecutiveAssignments = true; 9343 verifyFormat("float something = 2000;\n" 9344 "double another = 911;\n" 9345 "int i = 1, j = 10;\n" 9346 "const int *oneMore = 1;\n" 9347 "unsigned i = 2;", 9348 Alignment); 9349 verifyFormat("int oneTwoThree = {0}; // comment\n" 9350 "unsigned oneTwo = 0; // comment", 9351 Alignment); 9352 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9353 " int const i = 1;\n" 9354 " int * j = 2;\n" 9355 " int big = 10000;\n" 9356 "\n" 9357 " unsigned oneTwoThree = 123;\n" 9358 " int oneTwo = 12;\n" 9359 " method();\n" 9360 " float k = 2;\n" 9361 " int ll = 10000;\n" 9362 "}", 9363 format("void SomeFunction(int parameter= 0) {\n" 9364 " int const i= 1;\n" 9365 " int *j=2;\n" 9366 " int big = 10000;\n" 9367 "\n" 9368 "unsigned oneTwoThree =123;\n" 9369 "int oneTwo = 12;\n" 9370 " method();\n" 9371 "float k= 2;\n" 9372 "int ll=10000;\n" 9373 "}", 9374 Alignment)); 9375 Alignment.AlignConsecutiveAssignments = false; 9376 Alignment.AlignEscapedNewlinesLeft = true; 9377 verifyFormat("#define A \\\n" 9378 " int aaaa = 12; \\\n" 9379 " float b = 23; \\\n" 9380 " const int ccc = 234; \\\n" 9381 " unsigned dddddddddd = 2345;", 9382 Alignment); 9383 Alignment.AlignEscapedNewlinesLeft = false; 9384 Alignment.ColumnLimit = 30; 9385 verifyFormat("#define A \\\n" 9386 " int aaaa = 12; \\\n" 9387 " float b = 23; \\\n" 9388 " const int ccc = 234; \\\n" 9389 " int dddddddddd = 2345;", 9390 Alignment); 9391 Alignment.ColumnLimit = 80; 9392 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9393 "k = 4, int l = 5,\n" 9394 " int m = 6) {\n" 9395 " const int j = 10;\n" 9396 " otherThing = 1;\n" 9397 "}", 9398 Alignment); 9399 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9400 " int const i = 1;\n" 9401 " int * j = 2;\n" 9402 " int big = 10000;\n" 9403 "}", 9404 Alignment); 9405 verifyFormat("class C {\n" 9406 "public:\n" 9407 " int i = 1;\n" 9408 " virtual void f() = 0;\n" 9409 "};", 9410 Alignment); 9411 verifyFormat("float i = 1;\n" 9412 "if (SomeType t = getSomething()) {\n" 9413 "}\n" 9414 "const unsigned j = 2;\n" 9415 "int big = 10000;", 9416 Alignment); 9417 verifyFormat("float j = 7;\n" 9418 "for (int k = 0; k < N; ++k) {\n" 9419 "}\n" 9420 "unsigned j = 2;\n" 9421 "int big = 10000;\n" 9422 "}", 9423 Alignment); 9424 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9425 verifyFormat("float i = 1;\n" 9426 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9427 " = someLooooooooooooooooongFunction();\n" 9428 "int j = 2;", 9429 Alignment); 9430 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9431 verifyFormat("int i = 1;\n" 9432 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9433 " someLooooooooooooooooongFunction();\n" 9434 "int j = 2;", 9435 Alignment); 9436 9437 Alignment.AlignConsecutiveAssignments = true; 9438 verifyFormat("auto lambda = []() {\n" 9439 " auto ii = 0;\n" 9440 " float j = 0;\n" 9441 " return 0;\n" 9442 "};\n" 9443 "int i = 0;\n" 9444 "float i2 = 0;\n" 9445 "auto v = type{\n" 9446 " i = 1, //\n" 9447 " (i = 2), //\n" 9448 " i = 3 //\n" 9449 "};", 9450 Alignment); 9451 Alignment.AlignConsecutiveAssignments = false; 9452 9453 // FIXME: Should align all three declarations 9454 verifyFormat( 9455 "int i = 1;\n" 9456 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9457 " loooooooooooooooooooooongParameterB);\n" 9458 "int j = 2;", 9459 Alignment); 9460 9461 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9462 // We expect declarations and assignments to align, as long as it doesn't 9463 // exceed the column limit, starting a new alignemnt sequence whenever it 9464 // happens. 9465 Alignment.AlignConsecutiveAssignments = true; 9466 Alignment.ColumnLimit = 30; 9467 verifyFormat("float ii = 1;\n" 9468 "unsigned j = 2;\n" 9469 "int someVerylongVariable = 1;\n" 9470 "AnotherLongType ll = 123456;\n" 9471 "VeryVeryLongType k = 2;\n" 9472 "int myvar = 1;", 9473 Alignment); 9474 Alignment.ColumnLimit = 80; 9475 Alignment.AlignConsecutiveAssignments = false; 9476 9477 verifyFormat( 9478 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9479 " typename LongType, typename B>\n" 9480 "auto foo() {}\n", 9481 Alignment); 9482 verifyFormat("float a, b = 1;\n" 9483 "int c = 2;\n" 9484 "int dd = 3;\n", 9485 Alignment); 9486 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9487 "float b[1][] = {{3.f}};\n", 9488 Alignment); 9489 Alignment.AlignConsecutiveAssignments = true; 9490 verifyFormat("float a, b = 1;\n" 9491 "int c = 2;\n" 9492 "int dd = 3;\n", 9493 Alignment); 9494 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9495 "float b[1][] = {{3.f}};\n", 9496 Alignment); 9497 Alignment.AlignConsecutiveAssignments = false; 9498 9499 Alignment.ColumnLimit = 30; 9500 Alignment.BinPackParameters = false; 9501 verifyFormat("void foo(float a,\n" 9502 " float b,\n" 9503 " int c,\n" 9504 " uint32_t *d) {\n" 9505 " int * e = 0;\n" 9506 " float f = 0;\n" 9507 " double g = 0;\n" 9508 "}\n" 9509 "void bar(ino_t a,\n" 9510 " int b,\n" 9511 " uint32_t *c,\n" 9512 " bool d) {}\n", 9513 Alignment); 9514 Alignment.BinPackParameters = true; 9515 Alignment.ColumnLimit = 80; 9516 } 9517 9518 TEST_F(FormatTest, LinuxBraceBreaking) { 9519 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9520 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9521 verifyFormat("namespace a\n" 9522 "{\n" 9523 "class A\n" 9524 "{\n" 9525 " void f()\n" 9526 " {\n" 9527 " if (true) {\n" 9528 " a();\n" 9529 " b();\n" 9530 " } else {\n" 9531 " a();\n" 9532 " }\n" 9533 " }\n" 9534 " void g() { return; }\n" 9535 "};\n" 9536 "struct B {\n" 9537 " int x;\n" 9538 "};\n" 9539 "}\n", 9540 LinuxBraceStyle); 9541 verifyFormat("enum X {\n" 9542 " Y = 0,\n" 9543 "}\n", 9544 LinuxBraceStyle); 9545 verifyFormat("struct S {\n" 9546 " int Type;\n" 9547 " union {\n" 9548 " int x;\n" 9549 " double y;\n" 9550 " } Value;\n" 9551 " class C\n" 9552 " {\n" 9553 " MyFavoriteType Value;\n" 9554 " } Class;\n" 9555 "}\n", 9556 LinuxBraceStyle); 9557 } 9558 9559 TEST_F(FormatTest, MozillaBraceBreaking) { 9560 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9561 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9562 verifyFormat("namespace a {\n" 9563 "class A\n" 9564 "{\n" 9565 " void f()\n" 9566 " {\n" 9567 " if (true) {\n" 9568 " a();\n" 9569 " b();\n" 9570 " }\n" 9571 " }\n" 9572 " void g() { return; }\n" 9573 "};\n" 9574 "enum E\n" 9575 "{\n" 9576 " A,\n" 9577 " // foo\n" 9578 " B,\n" 9579 " C\n" 9580 "};\n" 9581 "struct B\n" 9582 "{\n" 9583 " int x;\n" 9584 "};\n" 9585 "}\n", 9586 MozillaBraceStyle); 9587 verifyFormat("struct S\n" 9588 "{\n" 9589 " int Type;\n" 9590 " union\n" 9591 " {\n" 9592 " int x;\n" 9593 " double y;\n" 9594 " } Value;\n" 9595 " class C\n" 9596 " {\n" 9597 " MyFavoriteType Value;\n" 9598 " } Class;\n" 9599 "}\n", 9600 MozillaBraceStyle); 9601 } 9602 9603 TEST_F(FormatTest, StroustrupBraceBreaking) { 9604 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9605 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9606 verifyFormat("namespace a {\n" 9607 "class A {\n" 9608 " void f()\n" 9609 " {\n" 9610 " if (true) {\n" 9611 " a();\n" 9612 " b();\n" 9613 " }\n" 9614 " }\n" 9615 " void g() { return; }\n" 9616 "};\n" 9617 "struct B {\n" 9618 " int x;\n" 9619 "};\n" 9620 "}\n", 9621 StroustrupBraceStyle); 9622 9623 verifyFormat("void foo()\n" 9624 "{\n" 9625 " if (a) {\n" 9626 " a();\n" 9627 " }\n" 9628 " else {\n" 9629 " b();\n" 9630 " }\n" 9631 "}\n", 9632 StroustrupBraceStyle); 9633 9634 verifyFormat("#ifdef _DEBUG\n" 9635 "int foo(int i = 0)\n" 9636 "#else\n" 9637 "int foo(int i = 5)\n" 9638 "#endif\n" 9639 "{\n" 9640 " return i;\n" 9641 "}", 9642 StroustrupBraceStyle); 9643 9644 verifyFormat("void foo() {}\n" 9645 "void bar()\n" 9646 "#ifdef _DEBUG\n" 9647 "{\n" 9648 " foo();\n" 9649 "}\n" 9650 "#else\n" 9651 "{\n" 9652 "}\n" 9653 "#endif", 9654 StroustrupBraceStyle); 9655 9656 verifyFormat("void foobar() { int i = 5; }\n" 9657 "#ifdef _DEBUG\n" 9658 "void bar() {}\n" 9659 "#else\n" 9660 "void bar() { foobar(); }\n" 9661 "#endif", 9662 StroustrupBraceStyle); 9663 } 9664 9665 TEST_F(FormatTest, AllmanBraceBreaking) { 9666 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9667 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9668 verifyFormat("namespace a\n" 9669 "{\n" 9670 "class A\n" 9671 "{\n" 9672 " void f()\n" 9673 " {\n" 9674 " if (true)\n" 9675 " {\n" 9676 " a();\n" 9677 " b();\n" 9678 " }\n" 9679 " }\n" 9680 " void g() { return; }\n" 9681 "};\n" 9682 "struct B\n" 9683 "{\n" 9684 " int x;\n" 9685 "};\n" 9686 "}", 9687 AllmanBraceStyle); 9688 9689 verifyFormat("void f()\n" 9690 "{\n" 9691 " if (true)\n" 9692 " {\n" 9693 " a();\n" 9694 " }\n" 9695 " else if (false)\n" 9696 " {\n" 9697 " b();\n" 9698 " }\n" 9699 " else\n" 9700 " {\n" 9701 " c();\n" 9702 " }\n" 9703 "}\n", 9704 AllmanBraceStyle); 9705 9706 verifyFormat("void f()\n" 9707 "{\n" 9708 " for (int i = 0; i < 10; ++i)\n" 9709 " {\n" 9710 " a();\n" 9711 " }\n" 9712 " while (false)\n" 9713 " {\n" 9714 " b();\n" 9715 " }\n" 9716 " do\n" 9717 " {\n" 9718 " c();\n" 9719 " } while (false)\n" 9720 "}\n", 9721 AllmanBraceStyle); 9722 9723 verifyFormat("void f(int a)\n" 9724 "{\n" 9725 " switch (a)\n" 9726 " {\n" 9727 " case 0:\n" 9728 " break;\n" 9729 " case 1:\n" 9730 " {\n" 9731 " break;\n" 9732 " }\n" 9733 " case 2:\n" 9734 " {\n" 9735 " }\n" 9736 " break;\n" 9737 " default:\n" 9738 " break;\n" 9739 " }\n" 9740 "}\n", 9741 AllmanBraceStyle); 9742 9743 verifyFormat("enum X\n" 9744 "{\n" 9745 " Y = 0,\n" 9746 "}\n", 9747 AllmanBraceStyle); 9748 verifyFormat("enum X\n" 9749 "{\n" 9750 " Y = 0\n" 9751 "}\n", 9752 AllmanBraceStyle); 9753 9754 verifyFormat("@interface BSApplicationController ()\n" 9755 "{\n" 9756 "@private\n" 9757 " id _extraIvar;\n" 9758 "}\n" 9759 "@end\n", 9760 AllmanBraceStyle); 9761 9762 verifyFormat("#ifdef _DEBUG\n" 9763 "int foo(int i = 0)\n" 9764 "#else\n" 9765 "int foo(int i = 5)\n" 9766 "#endif\n" 9767 "{\n" 9768 " return i;\n" 9769 "}", 9770 AllmanBraceStyle); 9771 9772 verifyFormat("void foo() {}\n" 9773 "void bar()\n" 9774 "#ifdef _DEBUG\n" 9775 "{\n" 9776 " foo();\n" 9777 "}\n" 9778 "#else\n" 9779 "{\n" 9780 "}\n" 9781 "#endif", 9782 AllmanBraceStyle); 9783 9784 verifyFormat("void foobar() { int i = 5; }\n" 9785 "#ifdef _DEBUG\n" 9786 "void bar() {}\n" 9787 "#else\n" 9788 "void bar() { foobar(); }\n" 9789 "#endif", 9790 AllmanBraceStyle); 9791 9792 // This shouldn't affect ObjC blocks.. 9793 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9794 " // ...\n" 9795 " int i;\n" 9796 "}];", 9797 AllmanBraceStyle); 9798 verifyFormat("void (^block)(void) = ^{\n" 9799 " // ...\n" 9800 " int i;\n" 9801 "};", 9802 AllmanBraceStyle); 9803 // .. or dict literals. 9804 verifyFormat("void f()\n" 9805 "{\n" 9806 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9807 "}", 9808 AllmanBraceStyle); 9809 verifyFormat("int f()\n" 9810 "{ // comment\n" 9811 " return 42;\n" 9812 "}", 9813 AllmanBraceStyle); 9814 9815 AllmanBraceStyle.ColumnLimit = 19; 9816 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9817 AllmanBraceStyle.ColumnLimit = 18; 9818 verifyFormat("void f()\n" 9819 "{\n" 9820 " int i;\n" 9821 "}", 9822 AllmanBraceStyle); 9823 AllmanBraceStyle.ColumnLimit = 80; 9824 9825 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9826 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9827 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9828 verifyFormat("void f(bool b)\n" 9829 "{\n" 9830 " if (b)\n" 9831 " {\n" 9832 " return;\n" 9833 " }\n" 9834 "}\n", 9835 BreakBeforeBraceShortIfs); 9836 verifyFormat("void f(bool b)\n" 9837 "{\n" 9838 " if (b) return;\n" 9839 "}\n", 9840 BreakBeforeBraceShortIfs); 9841 verifyFormat("void f(bool b)\n" 9842 "{\n" 9843 " while (b)\n" 9844 " {\n" 9845 " return;\n" 9846 " }\n" 9847 "}\n", 9848 BreakBeforeBraceShortIfs); 9849 } 9850 9851 TEST_F(FormatTest, GNUBraceBreaking) { 9852 FormatStyle GNUBraceStyle = getLLVMStyle(); 9853 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9854 verifyFormat("namespace a\n" 9855 "{\n" 9856 "class A\n" 9857 "{\n" 9858 " void f()\n" 9859 " {\n" 9860 " int a;\n" 9861 " {\n" 9862 " int b;\n" 9863 " }\n" 9864 " if (true)\n" 9865 " {\n" 9866 " a();\n" 9867 " b();\n" 9868 " }\n" 9869 " }\n" 9870 " void g() { return; }\n" 9871 "}\n" 9872 "}", 9873 GNUBraceStyle); 9874 9875 verifyFormat("void f()\n" 9876 "{\n" 9877 " if (true)\n" 9878 " {\n" 9879 " a();\n" 9880 " }\n" 9881 " else if (false)\n" 9882 " {\n" 9883 " b();\n" 9884 " }\n" 9885 " else\n" 9886 " {\n" 9887 " c();\n" 9888 " }\n" 9889 "}\n", 9890 GNUBraceStyle); 9891 9892 verifyFormat("void f()\n" 9893 "{\n" 9894 " for (int i = 0; i < 10; ++i)\n" 9895 " {\n" 9896 " a();\n" 9897 " }\n" 9898 " while (false)\n" 9899 " {\n" 9900 " b();\n" 9901 " }\n" 9902 " do\n" 9903 " {\n" 9904 " c();\n" 9905 " }\n" 9906 " while (false);\n" 9907 "}\n", 9908 GNUBraceStyle); 9909 9910 verifyFormat("void f(int a)\n" 9911 "{\n" 9912 " switch (a)\n" 9913 " {\n" 9914 " case 0:\n" 9915 " break;\n" 9916 " case 1:\n" 9917 " {\n" 9918 " break;\n" 9919 " }\n" 9920 " case 2:\n" 9921 " {\n" 9922 " }\n" 9923 " break;\n" 9924 " default:\n" 9925 " break;\n" 9926 " }\n" 9927 "}\n", 9928 GNUBraceStyle); 9929 9930 verifyFormat("enum X\n" 9931 "{\n" 9932 " Y = 0,\n" 9933 "}\n", 9934 GNUBraceStyle); 9935 9936 verifyFormat("@interface BSApplicationController ()\n" 9937 "{\n" 9938 "@private\n" 9939 " id _extraIvar;\n" 9940 "}\n" 9941 "@end\n", 9942 GNUBraceStyle); 9943 9944 verifyFormat("#ifdef _DEBUG\n" 9945 "int foo(int i = 0)\n" 9946 "#else\n" 9947 "int foo(int i = 5)\n" 9948 "#endif\n" 9949 "{\n" 9950 " return i;\n" 9951 "}", 9952 GNUBraceStyle); 9953 9954 verifyFormat("void foo() {}\n" 9955 "void bar()\n" 9956 "#ifdef _DEBUG\n" 9957 "{\n" 9958 " foo();\n" 9959 "}\n" 9960 "#else\n" 9961 "{\n" 9962 "}\n" 9963 "#endif", 9964 GNUBraceStyle); 9965 9966 verifyFormat("void foobar() { int i = 5; }\n" 9967 "#ifdef _DEBUG\n" 9968 "void bar() {}\n" 9969 "#else\n" 9970 "void bar() { foobar(); }\n" 9971 "#endif", 9972 GNUBraceStyle); 9973 } 9974 9975 TEST_F(FormatTest, WebKitBraceBreaking) { 9976 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9977 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9978 verifyFormat("namespace a {\n" 9979 "class A {\n" 9980 " void f()\n" 9981 " {\n" 9982 " if (true) {\n" 9983 " a();\n" 9984 " b();\n" 9985 " }\n" 9986 " }\n" 9987 " void g() { return; }\n" 9988 "};\n" 9989 "enum E {\n" 9990 " A,\n" 9991 " // foo\n" 9992 " B,\n" 9993 " C\n" 9994 "};\n" 9995 "struct B {\n" 9996 " int x;\n" 9997 "};\n" 9998 "}\n", 9999 WebKitBraceStyle); 10000 verifyFormat("struct S {\n" 10001 " int Type;\n" 10002 " union {\n" 10003 " int x;\n" 10004 " double y;\n" 10005 " } Value;\n" 10006 " class C {\n" 10007 " MyFavoriteType Value;\n" 10008 " } Class;\n" 10009 "};\n", 10010 WebKitBraceStyle); 10011 } 10012 10013 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10014 verifyFormat("void f() {\n" 10015 " try {\n" 10016 " } catch (const Exception &e) {\n" 10017 " }\n" 10018 "}\n", 10019 getLLVMStyle()); 10020 } 10021 10022 TEST_F(FormatTest, UnderstandsPragmas) { 10023 verifyFormat("#pragma omp reduction(| : var)"); 10024 verifyFormat("#pragma omp reduction(+ : var)"); 10025 10026 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10027 "(including parentheses).", 10028 format("#pragma mark Any non-hyphenated or hyphenated string " 10029 "(including parentheses).")); 10030 } 10031 10032 TEST_F(FormatTest, UnderstandPragmaOption) { 10033 verifyFormat("#pragma option -C -A"); 10034 10035 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10036 } 10037 10038 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10039 for (size_t i = 1; i < Styles.size(); ++i) \ 10040 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10041 << " differs from Style #0" 10042 10043 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10044 SmallVector<FormatStyle, 3> Styles; 10045 Styles.resize(3); 10046 10047 Styles[0] = getLLVMStyle(); 10048 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10049 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10050 EXPECT_ALL_STYLES_EQUAL(Styles); 10051 10052 Styles[0] = getGoogleStyle(); 10053 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10054 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10055 EXPECT_ALL_STYLES_EQUAL(Styles); 10056 10057 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10058 EXPECT_TRUE( 10059 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10060 EXPECT_TRUE( 10061 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10062 EXPECT_ALL_STYLES_EQUAL(Styles); 10063 10064 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10065 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10066 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10067 EXPECT_ALL_STYLES_EQUAL(Styles); 10068 10069 Styles[0] = getMozillaStyle(); 10070 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10071 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10072 EXPECT_ALL_STYLES_EQUAL(Styles); 10073 10074 Styles[0] = getWebKitStyle(); 10075 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10076 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10077 EXPECT_ALL_STYLES_EQUAL(Styles); 10078 10079 Styles[0] = getGNUStyle(); 10080 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10081 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10082 EXPECT_ALL_STYLES_EQUAL(Styles); 10083 10084 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10085 } 10086 10087 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10088 SmallVector<FormatStyle, 8> Styles; 10089 Styles.resize(2); 10090 10091 Styles[0] = getGoogleStyle(); 10092 Styles[1] = getLLVMStyle(); 10093 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10094 EXPECT_ALL_STYLES_EQUAL(Styles); 10095 10096 Styles.resize(5); 10097 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10098 Styles[1] = getLLVMStyle(); 10099 Styles[1].Language = FormatStyle::LK_JavaScript; 10100 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10101 10102 Styles[2] = getLLVMStyle(); 10103 Styles[2].Language = FormatStyle::LK_JavaScript; 10104 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10105 "BasedOnStyle: Google", 10106 &Styles[2]) 10107 .value()); 10108 10109 Styles[3] = getLLVMStyle(); 10110 Styles[3].Language = FormatStyle::LK_JavaScript; 10111 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10112 "Language: JavaScript", 10113 &Styles[3]) 10114 .value()); 10115 10116 Styles[4] = getLLVMStyle(); 10117 Styles[4].Language = FormatStyle::LK_JavaScript; 10118 EXPECT_EQ(0, parseConfiguration("---\n" 10119 "BasedOnStyle: LLVM\n" 10120 "IndentWidth: 123\n" 10121 "---\n" 10122 "BasedOnStyle: Google\n" 10123 "Language: JavaScript", 10124 &Styles[4]) 10125 .value()); 10126 EXPECT_ALL_STYLES_EQUAL(Styles); 10127 } 10128 10129 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10130 Style.FIELD = false; \ 10131 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10132 EXPECT_TRUE(Style.FIELD); \ 10133 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10134 EXPECT_FALSE(Style.FIELD); 10135 10136 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10137 10138 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10139 Style.STRUCT.FIELD = false; \ 10140 EXPECT_EQ(0, \ 10141 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10142 .value()); \ 10143 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10144 EXPECT_EQ(0, \ 10145 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10146 .value()); \ 10147 EXPECT_FALSE(Style.STRUCT.FIELD); 10148 10149 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10150 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10151 10152 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10153 EXPECT_NE(VALUE, Style.FIELD); \ 10154 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10155 EXPECT_EQ(VALUE, Style.FIELD) 10156 10157 TEST_F(FormatTest, ParsesConfigurationBools) { 10158 FormatStyle Style = {}; 10159 Style.Language = FormatStyle::LK_Cpp; 10160 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 10161 CHECK_PARSE_BOOL(AlignOperands); 10162 CHECK_PARSE_BOOL(AlignTrailingComments); 10163 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10164 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10165 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10166 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10167 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10168 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10169 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10170 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10171 CHECK_PARSE_BOOL(BinPackArguments); 10172 CHECK_PARSE_BOOL(BinPackParameters); 10173 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10174 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10175 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 10176 CHECK_PARSE_BOOL(BreakStringLiterals); 10177 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10178 CHECK_PARSE_BOOL(DerivePointerAlignment); 10179 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10180 CHECK_PARSE_BOOL(DisableFormat); 10181 CHECK_PARSE_BOOL(IndentCaseLabels); 10182 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10183 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10184 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10185 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10186 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10187 CHECK_PARSE_BOOL(ReflowComments); 10188 CHECK_PARSE_BOOL(SortIncludes); 10189 CHECK_PARSE_BOOL(SpacesInParentheses); 10190 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10191 CHECK_PARSE_BOOL(SpacesInAngles); 10192 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10193 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10194 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10195 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10196 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10197 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10198 10199 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10200 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10201 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10202 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10203 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10204 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10205 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10206 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10207 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10208 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10209 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10210 } 10211 10212 #undef CHECK_PARSE_BOOL 10213 10214 TEST_F(FormatTest, ParsesConfiguration) { 10215 FormatStyle Style = {}; 10216 Style.Language = FormatStyle::LK_Cpp; 10217 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10218 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10219 ConstructorInitializerIndentWidth, 1234u); 10220 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10221 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10222 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10223 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10224 PenaltyBreakBeforeFirstCallParameter, 1234u); 10225 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10226 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10227 PenaltyReturnTypeOnItsOwnLine, 1234u); 10228 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10229 SpacesBeforeTrailingComments, 1234u); 10230 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10231 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10232 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10233 10234 Style.PointerAlignment = FormatStyle::PAS_Middle; 10235 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10236 FormatStyle::PAS_Left); 10237 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10238 FormatStyle::PAS_Right); 10239 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10240 FormatStyle::PAS_Middle); 10241 // For backward compatibility: 10242 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10243 FormatStyle::PAS_Left); 10244 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10245 FormatStyle::PAS_Right); 10246 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10247 FormatStyle::PAS_Middle); 10248 10249 Style.Standard = FormatStyle::LS_Auto; 10250 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10251 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10252 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10253 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10254 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10255 10256 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10257 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10258 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10259 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10260 FormatStyle::BOS_None); 10261 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10262 FormatStyle::BOS_All); 10263 // For backward compatibility: 10264 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10265 FormatStyle::BOS_None); 10266 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10267 FormatStyle::BOS_All); 10268 10269 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10270 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10271 FormatStyle::BAS_Align); 10272 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10273 FormatStyle::BAS_DontAlign); 10274 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10275 FormatStyle::BAS_AlwaysBreak); 10276 // For backward compatibility: 10277 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10278 FormatStyle::BAS_DontAlign); 10279 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10280 FormatStyle::BAS_Align); 10281 10282 Style.UseTab = FormatStyle::UT_ForIndentation; 10283 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10284 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10285 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10286 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10287 FormatStyle::UT_ForContinuationAndIndentation); 10288 // For backward compatibility: 10289 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10290 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10291 10292 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10293 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10294 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10295 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10296 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10297 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10298 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10299 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10300 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10301 // For backward compatibility: 10302 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10303 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10304 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10305 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10306 10307 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10308 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10309 FormatStyle::SBPO_Never); 10310 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10311 FormatStyle::SBPO_Always); 10312 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10313 FormatStyle::SBPO_ControlStatements); 10314 // For backward compatibility: 10315 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10316 FormatStyle::SBPO_Never); 10317 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10318 FormatStyle::SBPO_ControlStatements); 10319 10320 Style.ColumnLimit = 123; 10321 FormatStyle BaseStyle = getLLVMStyle(); 10322 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10323 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10324 10325 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10326 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10327 FormatStyle::BS_Attach); 10328 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10329 FormatStyle::BS_Linux); 10330 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10331 FormatStyle::BS_Mozilla); 10332 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10333 FormatStyle::BS_Stroustrup); 10334 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10335 FormatStyle::BS_Allman); 10336 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10337 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10338 FormatStyle::BS_WebKit); 10339 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10340 FormatStyle::BS_Custom); 10341 10342 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10343 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10344 FormatStyle::RTBS_None); 10345 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10346 FormatStyle::RTBS_All); 10347 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10348 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10349 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10350 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10351 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10352 AlwaysBreakAfterReturnType, 10353 FormatStyle::RTBS_TopLevelDefinitions); 10354 10355 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10356 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10357 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10358 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10359 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10360 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10361 AlwaysBreakAfterDefinitionReturnType, 10362 FormatStyle::DRTBS_TopLevel); 10363 10364 Style.NamespaceIndentation = FormatStyle::NI_All; 10365 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10366 FormatStyle::NI_None); 10367 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10368 FormatStyle::NI_Inner); 10369 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10370 FormatStyle::NI_All); 10371 10372 // FIXME: This is required because parsing a configuration simply overwrites 10373 // the first N elements of the list instead of resetting it. 10374 Style.ForEachMacros.clear(); 10375 std::vector<std::string> BoostForeach; 10376 BoostForeach.push_back("BOOST_FOREACH"); 10377 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10378 std::vector<std::string> BoostAndQForeach; 10379 BoostAndQForeach.push_back("BOOST_FOREACH"); 10380 BoostAndQForeach.push_back("Q_FOREACH"); 10381 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10382 BoostAndQForeach); 10383 10384 Style.IncludeCategories.clear(); 10385 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10386 {".*", 1}}; 10387 CHECK_PARSE("IncludeCategories:\n" 10388 " - Regex: abc/.*\n" 10389 " Priority: 2\n" 10390 " - Regex: .*\n" 10391 " Priority: 1", 10392 IncludeCategories, ExpectedCategories); 10393 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10394 } 10395 10396 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10397 FormatStyle Style = {}; 10398 Style.Language = FormatStyle::LK_Cpp; 10399 CHECK_PARSE("Language: Cpp\n" 10400 "IndentWidth: 12", 10401 IndentWidth, 12u); 10402 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10403 "IndentWidth: 34", 10404 &Style), 10405 ParseError::Unsuitable); 10406 EXPECT_EQ(12u, Style.IndentWidth); 10407 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10408 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10409 10410 Style.Language = FormatStyle::LK_JavaScript; 10411 CHECK_PARSE("Language: JavaScript\n" 10412 "IndentWidth: 12", 10413 IndentWidth, 12u); 10414 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10415 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10416 "IndentWidth: 34", 10417 &Style), 10418 ParseError::Unsuitable); 10419 EXPECT_EQ(23u, Style.IndentWidth); 10420 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10421 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10422 10423 CHECK_PARSE("BasedOnStyle: LLVM\n" 10424 "IndentWidth: 67", 10425 IndentWidth, 67u); 10426 10427 CHECK_PARSE("---\n" 10428 "Language: JavaScript\n" 10429 "IndentWidth: 12\n" 10430 "---\n" 10431 "Language: Cpp\n" 10432 "IndentWidth: 34\n" 10433 "...\n", 10434 IndentWidth, 12u); 10435 10436 Style.Language = FormatStyle::LK_Cpp; 10437 CHECK_PARSE("---\n" 10438 "Language: JavaScript\n" 10439 "IndentWidth: 12\n" 10440 "---\n" 10441 "Language: Cpp\n" 10442 "IndentWidth: 34\n" 10443 "...\n", 10444 IndentWidth, 34u); 10445 CHECK_PARSE("---\n" 10446 "IndentWidth: 78\n" 10447 "---\n" 10448 "Language: JavaScript\n" 10449 "IndentWidth: 56\n" 10450 "...\n", 10451 IndentWidth, 78u); 10452 10453 Style.ColumnLimit = 123; 10454 Style.IndentWidth = 234; 10455 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10456 Style.TabWidth = 345; 10457 EXPECT_FALSE(parseConfiguration("---\n" 10458 "IndentWidth: 456\n" 10459 "BreakBeforeBraces: Allman\n" 10460 "---\n" 10461 "Language: JavaScript\n" 10462 "IndentWidth: 111\n" 10463 "TabWidth: 111\n" 10464 "---\n" 10465 "Language: Cpp\n" 10466 "BreakBeforeBraces: Stroustrup\n" 10467 "TabWidth: 789\n" 10468 "...\n", 10469 &Style)); 10470 EXPECT_EQ(123u, Style.ColumnLimit); 10471 EXPECT_EQ(456u, Style.IndentWidth); 10472 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10473 EXPECT_EQ(789u, Style.TabWidth); 10474 10475 EXPECT_EQ(parseConfiguration("---\n" 10476 "Language: JavaScript\n" 10477 "IndentWidth: 56\n" 10478 "---\n" 10479 "IndentWidth: 78\n" 10480 "...\n", 10481 &Style), 10482 ParseError::Error); 10483 EXPECT_EQ(parseConfiguration("---\n" 10484 "Language: JavaScript\n" 10485 "IndentWidth: 56\n" 10486 "---\n" 10487 "Language: JavaScript\n" 10488 "IndentWidth: 78\n" 10489 "...\n", 10490 &Style), 10491 ParseError::Error); 10492 10493 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10494 } 10495 10496 #undef CHECK_PARSE 10497 10498 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10499 FormatStyle Style = {}; 10500 Style.Language = FormatStyle::LK_JavaScript; 10501 Style.BreakBeforeTernaryOperators = true; 10502 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10503 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10504 10505 Style.BreakBeforeTernaryOperators = true; 10506 EXPECT_EQ(0, parseConfiguration("---\n" 10507 "BasedOnStyle: Google\n" 10508 "---\n" 10509 "Language: JavaScript\n" 10510 "IndentWidth: 76\n" 10511 "...\n", 10512 &Style) 10513 .value()); 10514 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10515 EXPECT_EQ(76u, Style.IndentWidth); 10516 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10517 } 10518 10519 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10520 FormatStyle Style = getLLVMStyle(); 10521 std::string YAML = configurationAsText(Style); 10522 FormatStyle ParsedStyle = {}; 10523 ParsedStyle.Language = FormatStyle::LK_Cpp; 10524 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10525 EXPECT_EQ(Style, ParsedStyle); 10526 } 10527 10528 TEST_F(FormatTest, WorksFor8bitEncodings) { 10529 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10530 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10531 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10532 "\"\xef\xee\xf0\xf3...\"", 10533 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10534 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10535 "\xef\xee\xf0\xf3...\"", 10536 getLLVMStyleWithColumns(12))); 10537 } 10538 10539 TEST_F(FormatTest, HandlesUTF8BOM) { 10540 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10541 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10542 format("\xef\xbb\xbf#include <iostream>")); 10543 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10544 format("\xef\xbb\xbf\n#include <iostream>")); 10545 } 10546 10547 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10548 #if !defined(_MSC_VER) 10549 10550 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10551 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10552 getLLVMStyleWithColumns(35)); 10553 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10554 getLLVMStyleWithColumns(31)); 10555 verifyFormat("// Однажды в студёную зимнюю пору...", 10556 getLLVMStyleWithColumns(36)); 10557 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10558 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10559 getLLVMStyleWithColumns(39)); 10560 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10561 getLLVMStyleWithColumns(35)); 10562 } 10563 10564 TEST_F(FormatTest, SplitsUTF8Strings) { 10565 // Non-printable characters' width is currently considered to be the length in 10566 // bytes in UTF8. The characters can be displayed in very different manner 10567 // (zero-width, single width with a substitution glyph, expanded to their code 10568 // (e.g. "<8d>"), so there's no single correct way to handle them. 10569 EXPECT_EQ("\"aaaaÄ\"\n" 10570 "\"\xc2\x8d\";", 10571 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10572 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10573 "\"\xc2\x8d\";", 10574 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10575 EXPECT_EQ("\"Однажды, в \"\n" 10576 "\"студёную \"\n" 10577 "\"зимнюю \"\n" 10578 "\"пору,\"", 10579 format("\"Однажды, в студёную зимнюю пору,\"", 10580 getLLVMStyleWithColumns(13))); 10581 EXPECT_EQ( 10582 "\"一 二 三 \"\n" 10583 "\"四 五六 \"\n" 10584 "\"七 八 九 \"\n" 10585 "\"十\"", 10586 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10587 EXPECT_EQ("\"一\t二 \"\n" 10588 "\"\t三 \"\n" 10589 "\"四 五\t六 \"\n" 10590 "\"\t七 \"\n" 10591 "\"八九十\tqq\"", 10592 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10593 getLLVMStyleWithColumns(11))); 10594 10595 // UTF8 character in an escape sequence. 10596 EXPECT_EQ("\"aaaaaa\"\n" 10597 "\"\\\xC2\x8D\"", 10598 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10599 } 10600 10601 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10602 EXPECT_EQ("const char *sssss =\n" 10603 " \"一二三四五六七八\\\n" 10604 " 九 十\";", 10605 format("const char *sssss = \"一二三四五六七八\\\n" 10606 " 九 十\";", 10607 getLLVMStyleWithColumns(30))); 10608 } 10609 10610 TEST_F(FormatTest, SplitsUTF8LineComments) { 10611 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10612 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10613 EXPECT_EQ("// Я из лесу\n" 10614 "// вышел; был\n" 10615 "// сильный\n" 10616 "// мороз.", 10617 format("// Я из лесу вышел; был сильный мороз.", 10618 getLLVMStyleWithColumns(13))); 10619 EXPECT_EQ("// 一二三\n" 10620 "// 四五六七\n" 10621 "// 八 九\n" 10622 "// 十", 10623 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10624 } 10625 10626 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10627 EXPECT_EQ("/* Гляжу,\n" 10628 " * поднимается\n" 10629 " * медленно в\n" 10630 " * гору\n" 10631 " * Лошадка,\n" 10632 " * везущая\n" 10633 " * хворосту\n" 10634 " * воз. */", 10635 format("/* Гляжу, поднимается медленно в гору\n" 10636 " * Лошадка, везущая хворосту воз. */", 10637 getLLVMStyleWithColumns(13))); 10638 EXPECT_EQ( 10639 "/* 一二三\n" 10640 " * 四五六七\n" 10641 " * 八 九\n" 10642 " * 十 */", 10643 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10644 EXPECT_EQ("/* \n" 10645 " * \n" 10646 " * - */", 10647 format("/* - */", getLLVMStyleWithColumns(12))); 10648 } 10649 10650 #endif // _MSC_VER 10651 10652 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10653 FormatStyle Style = getLLVMStyle(); 10654 10655 Style.ConstructorInitializerIndentWidth = 4; 10656 verifyFormat( 10657 "SomeClass::Constructor()\n" 10658 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10659 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10660 Style); 10661 10662 Style.ConstructorInitializerIndentWidth = 2; 10663 verifyFormat( 10664 "SomeClass::Constructor()\n" 10665 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10666 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10667 Style); 10668 10669 Style.ConstructorInitializerIndentWidth = 0; 10670 verifyFormat( 10671 "SomeClass::Constructor()\n" 10672 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10673 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10674 Style); 10675 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10676 verifyFormat( 10677 "SomeLongTemplateVariableName<\n" 10678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10679 Style); 10680 verifyFormat( 10681 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10683 Style); 10684 } 10685 10686 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10687 FormatStyle Style = getLLVMStyle(); 10688 Style.BreakConstructorInitializersBeforeComma = true; 10689 Style.ConstructorInitializerIndentWidth = 4; 10690 verifyFormat("SomeClass::Constructor()\n" 10691 " : a(a)\n" 10692 " , b(b)\n" 10693 " , c(c) {}", 10694 Style); 10695 verifyFormat("SomeClass::Constructor()\n" 10696 " : a(a) {}", 10697 Style); 10698 10699 Style.ColumnLimit = 0; 10700 verifyFormat("SomeClass::Constructor()\n" 10701 " : a(a) {}", 10702 Style); 10703 verifyFormat("SomeClass::Constructor() noexcept\n" 10704 " : a(a) {}", 10705 Style); 10706 verifyFormat("SomeClass::Constructor()\n" 10707 " : a(a)\n" 10708 " , b(b)\n" 10709 " , c(c) {}", 10710 Style); 10711 verifyFormat("SomeClass::Constructor()\n" 10712 " : a(a) {\n" 10713 " foo();\n" 10714 " bar();\n" 10715 "}", 10716 Style); 10717 10718 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10719 verifyFormat("SomeClass::Constructor()\n" 10720 " : a(a)\n" 10721 " , b(b)\n" 10722 " , c(c) {\n}", 10723 Style); 10724 verifyFormat("SomeClass::Constructor()\n" 10725 " : a(a) {\n}", 10726 Style); 10727 10728 Style.ColumnLimit = 80; 10729 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10730 Style.ConstructorInitializerIndentWidth = 2; 10731 verifyFormat("SomeClass::Constructor()\n" 10732 " : a(a)\n" 10733 " , b(b)\n" 10734 " , c(c) {}", 10735 Style); 10736 10737 Style.ConstructorInitializerIndentWidth = 0; 10738 verifyFormat("SomeClass::Constructor()\n" 10739 ": a(a)\n" 10740 ", b(b)\n" 10741 ", c(c) {}", 10742 Style); 10743 10744 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10745 Style.ConstructorInitializerIndentWidth = 4; 10746 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10747 verifyFormat( 10748 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10749 Style); 10750 verifyFormat( 10751 "SomeClass::Constructor()\n" 10752 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10753 Style); 10754 Style.ConstructorInitializerIndentWidth = 4; 10755 Style.ColumnLimit = 60; 10756 verifyFormat("SomeClass::Constructor()\n" 10757 " : aaaaaaaa(aaaaaaaa)\n" 10758 " , aaaaaaaa(aaaaaaaa)\n" 10759 " , aaaaaaaa(aaaaaaaa) {}", 10760 Style); 10761 } 10762 10763 TEST_F(FormatTest, Destructors) { 10764 verifyFormat("void F(int &i) { i.~int(); }"); 10765 verifyFormat("void F(int &i) { i->~int(); }"); 10766 } 10767 10768 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10769 FormatStyle Style = getWebKitStyle(); 10770 10771 // Don't indent in outer namespaces. 10772 verifyFormat("namespace outer {\n" 10773 "int i;\n" 10774 "namespace inner {\n" 10775 " int i;\n" 10776 "} // namespace inner\n" 10777 "} // namespace outer\n" 10778 "namespace other_outer {\n" 10779 "int i;\n" 10780 "}", 10781 Style); 10782 10783 // Don't indent case labels. 10784 verifyFormat("switch (variable) {\n" 10785 "case 1:\n" 10786 "case 2:\n" 10787 " doSomething();\n" 10788 " break;\n" 10789 "default:\n" 10790 " ++variable;\n" 10791 "}", 10792 Style); 10793 10794 // Wrap before binary operators. 10795 EXPECT_EQ("void f()\n" 10796 "{\n" 10797 " if (aaaaaaaaaaaaaaaa\n" 10798 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10799 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10800 " return;\n" 10801 "}", 10802 format("void f() {\n" 10803 "if (aaaaaaaaaaaaaaaa\n" 10804 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10805 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10806 "return;\n" 10807 "}", 10808 Style)); 10809 10810 // Allow functions on a single line. 10811 verifyFormat("void f() { return; }", Style); 10812 10813 // Constructor initializers are formatted one per line with the "," on the 10814 // new line. 10815 verifyFormat("Constructor()\n" 10816 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10817 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10818 " aaaaaaaaaaaaaa)\n" 10819 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10820 "{\n" 10821 "}", 10822 Style); 10823 verifyFormat("SomeClass::Constructor()\n" 10824 " : a(a)\n" 10825 "{\n" 10826 "}", 10827 Style); 10828 EXPECT_EQ("SomeClass::Constructor()\n" 10829 " : a(a)\n" 10830 "{\n" 10831 "}", 10832 format("SomeClass::Constructor():a(a){}", Style)); 10833 verifyFormat("SomeClass::Constructor()\n" 10834 " : a(a)\n" 10835 " , b(b)\n" 10836 " , c(c)\n" 10837 "{\n" 10838 "}", 10839 Style); 10840 verifyFormat("SomeClass::Constructor()\n" 10841 " : a(a)\n" 10842 "{\n" 10843 " foo();\n" 10844 " bar();\n" 10845 "}", 10846 Style); 10847 10848 // Access specifiers should be aligned left. 10849 verifyFormat("class C {\n" 10850 "public:\n" 10851 " int i;\n" 10852 "};", 10853 Style); 10854 10855 // Do not align comments. 10856 verifyFormat("int a; // Do not\n" 10857 "double b; // align comments.", 10858 Style); 10859 10860 // Do not align operands. 10861 EXPECT_EQ("ASSERT(aaaa\n" 10862 " || bbbb);", 10863 format("ASSERT ( aaaa\n||bbbb);", Style)); 10864 10865 // Accept input's line breaks. 10866 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10867 " || bbbbbbbbbbbbbbb) {\n" 10868 " i++;\n" 10869 "}", 10870 format("if (aaaaaaaaaaaaaaa\n" 10871 "|| bbbbbbbbbbbbbbb) { i++; }", 10872 Style)); 10873 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10874 " i++;\n" 10875 "}", 10876 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10877 10878 // Don't automatically break all macro definitions (llvm.org/PR17842). 10879 verifyFormat("#define aNumber 10", Style); 10880 // However, generally keep the line breaks that the user authored. 10881 EXPECT_EQ("#define aNumber \\\n" 10882 " 10", 10883 format("#define aNumber \\\n" 10884 " 10", 10885 Style)); 10886 10887 // Keep empty and one-element array literals on a single line. 10888 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10889 " copyItems:YES];", 10890 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10891 "copyItems:YES];", 10892 Style)); 10893 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10894 " copyItems:YES];", 10895 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10896 " copyItems:YES];", 10897 Style)); 10898 // FIXME: This does not seem right, there should be more indentation before 10899 // the array literal's entries. Nested blocks have the same problem. 10900 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10901 " @\"a\",\n" 10902 " @\"a\"\n" 10903 "]\n" 10904 " copyItems:YES];", 10905 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10906 " @\"a\",\n" 10907 " @\"a\"\n" 10908 " ]\n" 10909 " copyItems:YES];", 10910 Style)); 10911 EXPECT_EQ( 10912 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10913 " copyItems:YES];", 10914 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10915 " copyItems:YES];", 10916 Style)); 10917 10918 verifyFormat("[self.a b:c c:d];", Style); 10919 EXPECT_EQ("[self.a b:c\n" 10920 " c:d];", 10921 format("[self.a b:c\n" 10922 "c:d];", 10923 Style)); 10924 } 10925 10926 TEST_F(FormatTest, FormatsLambdas) { 10927 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10928 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10929 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10930 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10931 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10932 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10933 verifyFormat("void f() {\n" 10934 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10935 "}\n"); 10936 verifyFormat("void f() {\n" 10937 " other(x.begin(), //\n" 10938 " x.end(), //\n" 10939 " [&](int, int) { return 1; });\n" 10940 "}\n"); 10941 verifyFormat("SomeFunction([]() { // A cool function...\n" 10942 " return 43;\n" 10943 "});"); 10944 EXPECT_EQ("SomeFunction([]() {\n" 10945 "#define A a\n" 10946 " return 43;\n" 10947 "});", 10948 format("SomeFunction([](){\n" 10949 "#define A a\n" 10950 "return 43;\n" 10951 "});")); 10952 verifyFormat("void f() {\n" 10953 " SomeFunction([](decltype(x), A *a) {});\n" 10954 "}"); 10955 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10956 " [](const aaaaaaaaaa &a) { return a; });"); 10957 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10958 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10959 "});"); 10960 verifyFormat("Constructor()\n" 10961 " : Field([] { // comment\n" 10962 " int i;\n" 10963 " }) {}"); 10964 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10965 " return some_parameter.size();\n" 10966 "};"); 10967 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10968 " [](const string &s) { return s; };"); 10969 verifyFormat("int i = aaaaaa ? 1 //\n" 10970 " : [] {\n" 10971 " return 2; //\n" 10972 " }();"); 10973 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10974 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10975 " return x == 2; // force break\n" 10976 " });"); 10977 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10978 " int iiiiiiiiiiii) {\n" 10979 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10980 "});", 10981 getLLVMStyleWithColumns(60)); 10982 verifyFormat("SomeFunction({[&] {\n" 10983 " // comment\n" 10984 " },\n" 10985 " [&] {\n" 10986 " // comment\n" 10987 " }});"); 10988 verifyFormat("SomeFunction({[&] {\n" 10989 " // comment\n" 10990 "}});"); 10991 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10992 " [&]() { return true; },\n" 10993 " aaaaa aaaaaaaaa);"); 10994 10995 // Lambdas with return types. 10996 verifyFormat("int c = []() -> int { return 2; }();\n"); 10997 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10998 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10999 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11000 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11001 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11002 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11003 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11004 verifyFormat("[a, a]() -> a<1> {};"); 11005 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11006 " int j) -> int {\n" 11007 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11008 "};"); 11009 verifyFormat( 11010 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11011 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11012 " return aaaaaaaaaaaaaaaaa;\n" 11013 " });", 11014 getLLVMStyleWithColumns(70)); 11015 verifyFormat("[]() //\n" 11016 " -> int {\n" 11017 " return 1; //\n" 11018 "};"); 11019 11020 // Multiple lambdas in the same parentheses change indentation rules. 11021 verifyFormat("SomeFunction(\n" 11022 " []() {\n" 11023 " int i = 42;\n" 11024 " return i;\n" 11025 " },\n" 11026 " []() {\n" 11027 " int j = 43;\n" 11028 " return j;\n" 11029 " });"); 11030 11031 // More complex introducers. 11032 verifyFormat("return [i, args...] {};"); 11033 11034 // Not lambdas. 11035 verifyFormat("constexpr char hello[]{\"hello\"};"); 11036 verifyFormat("double &operator[](int i) { return 0; }\n" 11037 "int i;"); 11038 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11039 verifyFormat("int i = a[a][a]->f();"); 11040 verifyFormat("int i = (*b)[a]->f();"); 11041 11042 // Other corner cases. 11043 verifyFormat("void f() {\n" 11044 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11045 " );\n" 11046 "}"); 11047 11048 // Lambdas created through weird macros. 11049 verifyFormat("void f() {\n" 11050 " MACRO((const AA &a) { return 1; });\n" 11051 " MACRO((AA &a) { return 1; });\n" 11052 "}"); 11053 11054 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11055 " doo_dah();\n" 11056 " doo_dah();\n" 11057 " })) {\n" 11058 "}"); 11059 verifyFormat("auto lambda = []() {\n" 11060 " int a = 2\n" 11061 "#if A\n" 11062 " + 2\n" 11063 "#endif\n" 11064 " ;\n" 11065 "};"); 11066 } 11067 11068 TEST_F(FormatTest, FormatsBlocks) { 11069 FormatStyle ShortBlocks = getLLVMStyle(); 11070 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11071 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11072 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11073 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11074 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11075 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11076 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11077 11078 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11079 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11080 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11081 11082 verifyFormat("[operation setCompletionBlock:^{\n" 11083 " [self onOperationDone];\n" 11084 "}];"); 11085 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11086 " [self onOperationDone];\n" 11087 "}]};"); 11088 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11089 " f();\n" 11090 "}];"); 11091 verifyFormat("int a = [operation block:^int(int *i) {\n" 11092 " return 1;\n" 11093 "}];"); 11094 verifyFormat("[myObject doSomethingWith:arg1\n" 11095 " aaa:^int(int *a) {\n" 11096 " return 1;\n" 11097 " }\n" 11098 " bbb:f(a * bbbbbbbb)];"); 11099 11100 verifyFormat("[operation setCompletionBlock:^{\n" 11101 " [self.delegate newDataAvailable];\n" 11102 "}];", 11103 getLLVMStyleWithColumns(60)); 11104 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11105 " NSString *path = [self sessionFilePath];\n" 11106 " if (path) {\n" 11107 " // ...\n" 11108 " }\n" 11109 "});"); 11110 verifyFormat("[[SessionService sharedService]\n" 11111 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11112 " if (window) {\n" 11113 " [self windowDidLoad:window];\n" 11114 " } else {\n" 11115 " [self errorLoadingWindow];\n" 11116 " }\n" 11117 " }];"); 11118 verifyFormat("void (^largeBlock)(void) = ^{\n" 11119 " // ...\n" 11120 "};\n", 11121 getLLVMStyleWithColumns(40)); 11122 verifyFormat("[[SessionService sharedService]\n" 11123 " loadWindowWithCompletionBlock: //\n" 11124 " ^(SessionWindow *window) {\n" 11125 " if (window) {\n" 11126 " [self windowDidLoad:window];\n" 11127 " } else {\n" 11128 " [self errorLoadingWindow];\n" 11129 " }\n" 11130 " }];", 11131 getLLVMStyleWithColumns(60)); 11132 verifyFormat("[myObject doSomethingWith:arg1\n" 11133 " firstBlock:^(Foo *a) {\n" 11134 " // ...\n" 11135 " int i;\n" 11136 " }\n" 11137 " secondBlock:^(Bar *b) {\n" 11138 " // ...\n" 11139 " int i;\n" 11140 " }\n" 11141 " thirdBlock:^Foo(Bar *b) {\n" 11142 " // ...\n" 11143 " int i;\n" 11144 " }];"); 11145 verifyFormat("[myObject doSomethingWith:arg1\n" 11146 " firstBlock:-1\n" 11147 " secondBlock:^(Bar *b) {\n" 11148 " // ...\n" 11149 " int i;\n" 11150 " }];"); 11151 11152 verifyFormat("f(^{\n" 11153 " @autoreleasepool {\n" 11154 " if (a) {\n" 11155 " g();\n" 11156 " }\n" 11157 " }\n" 11158 "});"); 11159 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11160 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11161 "};"); 11162 11163 FormatStyle FourIndent = getLLVMStyle(); 11164 FourIndent.ObjCBlockIndentWidth = 4; 11165 verifyFormat("[operation setCompletionBlock:^{\n" 11166 " [self onOperationDone];\n" 11167 "}];", 11168 FourIndent); 11169 } 11170 11171 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11172 FormatStyle ZeroColumn = getLLVMStyle(); 11173 ZeroColumn.ColumnLimit = 0; 11174 11175 verifyFormat("[[SessionService sharedService] " 11176 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11177 " if (window) {\n" 11178 " [self windowDidLoad:window];\n" 11179 " } else {\n" 11180 " [self errorLoadingWindow];\n" 11181 " }\n" 11182 "}];", 11183 ZeroColumn); 11184 EXPECT_EQ("[[SessionService sharedService]\n" 11185 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11186 " if (window) {\n" 11187 " [self windowDidLoad:window];\n" 11188 " } else {\n" 11189 " [self errorLoadingWindow];\n" 11190 " }\n" 11191 " }];", 11192 format("[[SessionService sharedService]\n" 11193 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11194 " if (window) {\n" 11195 " [self windowDidLoad:window];\n" 11196 " } else {\n" 11197 " [self errorLoadingWindow];\n" 11198 " }\n" 11199 "}];", 11200 ZeroColumn)); 11201 verifyFormat("[myObject doSomethingWith:arg1\n" 11202 " firstBlock:^(Foo *a) {\n" 11203 " // ...\n" 11204 " int i;\n" 11205 " }\n" 11206 " secondBlock:^(Bar *b) {\n" 11207 " // ...\n" 11208 " int i;\n" 11209 " }\n" 11210 " thirdBlock:^Foo(Bar *b) {\n" 11211 " // ...\n" 11212 " int i;\n" 11213 " }];", 11214 ZeroColumn); 11215 verifyFormat("f(^{\n" 11216 " @autoreleasepool {\n" 11217 " if (a) {\n" 11218 " g();\n" 11219 " }\n" 11220 " }\n" 11221 "});", 11222 ZeroColumn); 11223 verifyFormat("void (^largeBlock)(void) = ^{\n" 11224 " // ...\n" 11225 "};", 11226 ZeroColumn); 11227 11228 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11229 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11230 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11231 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11232 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11233 " int i;\n" 11234 "};", 11235 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11236 } 11237 11238 TEST_F(FormatTest, SupportsCRLF) { 11239 EXPECT_EQ("int a;\r\n" 11240 "int b;\r\n" 11241 "int c;\r\n", 11242 format("int a;\r\n" 11243 " int b;\r\n" 11244 " int c;\r\n", 11245 getLLVMStyle())); 11246 EXPECT_EQ("int a;\r\n" 11247 "int b;\r\n" 11248 "int c;\r\n", 11249 format("int a;\r\n" 11250 " int b;\n" 11251 " int c;\r\n", 11252 getLLVMStyle())); 11253 EXPECT_EQ("int a;\n" 11254 "int b;\n" 11255 "int c;\n", 11256 format("int a;\r\n" 11257 " int b;\n" 11258 " int c;\n", 11259 getLLVMStyle())); 11260 EXPECT_EQ("\"aaaaaaa \"\r\n" 11261 "\"bbbbbbb\";\r\n", 11262 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11263 EXPECT_EQ("#define A \\\r\n" 11264 " b; \\\r\n" 11265 " c; \\\r\n" 11266 " d;\r\n", 11267 format("#define A \\\r\n" 11268 " b; \\\r\n" 11269 " c; d; \r\n", 11270 getGoogleStyle())); 11271 11272 EXPECT_EQ("/*\r\n" 11273 "multi line block comments\r\n" 11274 "should not introduce\r\n" 11275 "an extra carriage return\r\n" 11276 "*/\r\n", 11277 format("/*\r\n" 11278 "multi line block comments\r\n" 11279 "should not introduce\r\n" 11280 "an extra carriage return\r\n" 11281 "*/\r\n")); 11282 } 11283 11284 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11285 verifyFormat("MY_CLASS(C) {\n" 11286 " int i;\n" 11287 " int j;\n" 11288 "};"); 11289 } 11290 11291 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11292 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11293 TwoIndent.ContinuationIndentWidth = 2; 11294 11295 EXPECT_EQ("int i =\n" 11296 " longFunction(\n" 11297 " arg);", 11298 format("int i = longFunction(arg);", TwoIndent)); 11299 11300 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11301 SixIndent.ContinuationIndentWidth = 6; 11302 11303 EXPECT_EQ("int i =\n" 11304 " longFunction(\n" 11305 " arg);", 11306 format("int i = longFunction(arg);", SixIndent)); 11307 } 11308 11309 TEST_F(FormatTest, SpacesInAngles) { 11310 FormatStyle Spaces = getLLVMStyle(); 11311 Spaces.SpacesInAngles = true; 11312 11313 verifyFormat("static_cast< int >(arg);", Spaces); 11314 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11315 verifyFormat("f< int, float >();", Spaces); 11316 verifyFormat("template <> g() {}", Spaces); 11317 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11318 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11319 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11320 Spaces); 11321 11322 Spaces.Standard = FormatStyle::LS_Cpp03; 11323 Spaces.SpacesInAngles = true; 11324 verifyFormat("A< A< int > >();", Spaces); 11325 11326 Spaces.SpacesInAngles = false; 11327 verifyFormat("A<A<int> >();", Spaces); 11328 11329 Spaces.Standard = FormatStyle::LS_Cpp11; 11330 Spaces.SpacesInAngles = true; 11331 verifyFormat("A< A< int > >();", Spaces); 11332 11333 Spaces.SpacesInAngles = false; 11334 verifyFormat("A<A<int>>();", Spaces); 11335 } 11336 11337 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11338 FormatStyle Style = getLLVMStyle(); 11339 Style.SpaceAfterTemplateKeyword = false; 11340 verifyFormat("template<int> void foo();", Style); 11341 } 11342 11343 TEST_F(FormatTest, TripleAngleBrackets) { 11344 verifyFormat("f<<<1, 1>>>();"); 11345 verifyFormat("f<<<1, 1, 1, s>>>();"); 11346 verifyFormat("f<<<a, b, c, d>>>();"); 11347 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11348 verifyFormat("f<param><<<1, 1>>>();"); 11349 verifyFormat("f<1><<<1, 1>>>();"); 11350 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11351 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11352 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11353 } 11354 11355 TEST_F(FormatTest, MergeLessLessAtEnd) { 11356 verifyFormat("<<"); 11357 EXPECT_EQ("< < <", format("\\\n<<<")); 11358 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11359 "aaallvm::outs() <<"); 11360 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11361 "aaaallvm::outs()\n <<"); 11362 } 11363 11364 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11365 std::string code = "#if A\n" 11366 "#if B\n" 11367 "a.\n" 11368 "#endif\n" 11369 " a = 1;\n" 11370 "#else\n" 11371 "#endif\n" 11372 "#if C\n" 11373 "#else\n" 11374 "#endif\n"; 11375 EXPECT_EQ(code, format(code)); 11376 } 11377 11378 TEST_F(FormatTest, HandleConflictMarkers) { 11379 // Git/SVN conflict markers. 11380 EXPECT_EQ("int a;\n" 11381 "void f() {\n" 11382 " callme(some(parameter1,\n" 11383 "<<<<<<< text by the vcs\n" 11384 " parameter2),\n" 11385 "||||||| text by the vcs\n" 11386 " parameter2),\n" 11387 " parameter3,\n" 11388 "======= text by the vcs\n" 11389 " parameter2, parameter3),\n" 11390 ">>>>>>> text by the vcs\n" 11391 " otherparameter);\n", 11392 format("int a;\n" 11393 "void f() {\n" 11394 " callme(some(parameter1,\n" 11395 "<<<<<<< text by the vcs\n" 11396 " parameter2),\n" 11397 "||||||| text by the vcs\n" 11398 " parameter2),\n" 11399 " parameter3,\n" 11400 "======= text by the vcs\n" 11401 " parameter2,\n" 11402 " parameter3),\n" 11403 ">>>>>>> text by the vcs\n" 11404 " otherparameter);\n")); 11405 11406 // Perforce markers. 11407 EXPECT_EQ("void f() {\n" 11408 " function(\n" 11409 ">>>> text by the vcs\n" 11410 " parameter,\n" 11411 "==== text by the vcs\n" 11412 " parameter,\n" 11413 "==== text by the vcs\n" 11414 " parameter,\n" 11415 "<<<< text by the vcs\n" 11416 " parameter);\n", 11417 format("void f() {\n" 11418 " function(\n" 11419 ">>>> text by the vcs\n" 11420 " parameter,\n" 11421 "==== text by the vcs\n" 11422 " parameter,\n" 11423 "==== text by the vcs\n" 11424 " parameter,\n" 11425 "<<<< text by the vcs\n" 11426 " parameter);\n")); 11427 11428 EXPECT_EQ("<<<<<<<\n" 11429 "|||||||\n" 11430 "=======\n" 11431 ">>>>>>>", 11432 format("<<<<<<<\n" 11433 "|||||||\n" 11434 "=======\n" 11435 ">>>>>>>")); 11436 11437 EXPECT_EQ("<<<<<<<\n" 11438 "|||||||\n" 11439 "int i;\n" 11440 "=======\n" 11441 ">>>>>>>", 11442 format("<<<<<<<\n" 11443 "|||||||\n" 11444 "int i;\n" 11445 "=======\n" 11446 ">>>>>>>")); 11447 11448 // FIXME: Handle parsing of macros around conflict markers correctly: 11449 EXPECT_EQ("#define Macro \\\n" 11450 "<<<<<<<\n" 11451 "Something \\\n" 11452 "|||||||\n" 11453 "Else \\\n" 11454 "=======\n" 11455 "Other \\\n" 11456 ">>>>>>>\n" 11457 " End int i;\n", 11458 format("#define Macro \\\n" 11459 "<<<<<<<\n" 11460 " Something \\\n" 11461 "|||||||\n" 11462 " Else \\\n" 11463 "=======\n" 11464 " Other \\\n" 11465 ">>>>>>>\n" 11466 " End\n" 11467 "int i;\n")); 11468 } 11469 11470 TEST_F(FormatTest, DisableRegions) { 11471 EXPECT_EQ("int i;\n" 11472 "// clang-format off\n" 11473 " int j;\n" 11474 "// clang-format on\n" 11475 "int k;", 11476 format(" int i;\n" 11477 " // clang-format off\n" 11478 " int j;\n" 11479 " // clang-format on\n" 11480 " int k;")); 11481 EXPECT_EQ("int i;\n" 11482 "/* clang-format off */\n" 11483 " int j;\n" 11484 "/* clang-format on */\n" 11485 "int k;", 11486 format(" int i;\n" 11487 " /* clang-format off */\n" 11488 " int j;\n" 11489 " /* clang-format on */\n" 11490 " int k;")); 11491 } 11492 11493 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11494 format("? ) ="); 11495 verifyNoCrash("#define a\\\n /**/}"); 11496 } 11497 11498 TEST_F(FormatTest, FormatsTableGenCode) { 11499 FormatStyle Style = getLLVMStyle(); 11500 Style.Language = FormatStyle::LK_TableGen; 11501 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11502 } 11503 11504 // Since this test case uses UNIX-style file path. We disable it for MS 11505 // compiler. 11506 #if !defined(_MSC_VER) && !defined(__MINGW32__) 11507 11508 TEST(FormatStyle, GetStyleOfFile) { 11509 vfs::InMemoryFileSystem FS; 11510 // Test 1: format file in the same directory. 11511 ASSERT_TRUE( 11512 FS.addFile("/a/.clang-format", 0, 11513 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11514 ASSERT_TRUE( 11515 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11516 auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS); 11517 ASSERT_EQ(Style1, getLLVMStyle()); 11518 11519 // Test 2: fallback to default. 11520 ASSERT_TRUE( 11521 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11522 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS); 11523 ASSERT_EQ(Style2, getMozillaStyle()); 11524 11525 // Test 3: format file in parent directory. 11526 ASSERT_TRUE( 11527 FS.addFile("/c/.clang-format", 0, 11528 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11529 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11530 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11531 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS); 11532 ASSERT_EQ(Style3, getGoogleStyle()); 11533 } 11534 11535 #endif // _MSC_VER 11536 11537 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11538 // Column limit is 20. 11539 std::string Code = "Type *a =\n" 11540 " new Type();\n" 11541 "g(iiiii, 0, jjjjj,\n" 11542 " 0, kkkkk, 0, mm);\n" 11543 "int bad = format ;"; 11544 std::string Expected = "auto a = new Type();\n" 11545 "g(iiiii, nullptr,\n" 11546 " jjjjj, nullptr,\n" 11547 " kkkkk, nullptr,\n" 11548 " mm);\n" 11549 "int bad = format ;"; 11550 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11551 tooling::Replacements Replaces = toReplacements( 11552 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11553 "auto "), 11554 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11555 "nullptr"), 11556 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11557 "nullptr"), 11558 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11559 "nullptr")}); 11560 11561 format::FormatStyle Style = format::getLLVMStyle(); 11562 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11563 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11564 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11565 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11566 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11567 EXPECT_TRUE(static_cast<bool>(Result)); 11568 EXPECT_EQ(Expected, *Result); 11569 } 11570 11571 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11572 std::string Code = "#include \"a.h\"\n" 11573 "#include \"c.h\"\n" 11574 "\n" 11575 "int main() {\n" 11576 " return 0;\n" 11577 "}"; 11578 std::string Expected = "#include \"a.h\"\n" 11579 "#include \"b.h\"\n" 11580 "#include \"c.h\"\n" 11581 "\n" 11582 "int main() {\n" 11583 " return 0;\n" 11584 "}"; 11585 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11586 tooling::Replacements Replaces = toReplacements( 11587 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11588 "#include \"b.h\"\n")}); 11589 11590 format::FormatStyle Style = format::getLLVMStyle(); 11591 Style.SortIncludes = true; 11592 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11593 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11594 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11595 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11596 EXPECT_TRUE(static_cast<bool>(Result)); 11597 EXPECT_EQ(Expected, *Result); 11598 } 11599 11600 } // end namespace 11601 } // end namespace format 11602 } // end namespace clang 11603