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 FormatStyle Style = getLLVMStyle(); 281 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 282 Style.MaxEmptyLinesToKeep = 2; 283 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 284 Style.BraceWrapping.AfterClass = true; 285 Style.BraceWrapping.AfterFunction = true; 286 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 287 288 EXPECT_EQ("class Foo\n" 289 "{\n" 290 " Foo() {}\n" 291 "\n" 292 " void funk() {}\n" 293 "};", 294 format("class Foo\n" 295 "{\n" 296 " Foo()\n" 297 " {\n" 298 " }\n" 299 "\n" 300 " void funk() {}\n" 301 "};", 302 Style)); 303 } 304 305 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 306 verifyFormat("x = (a) and (b);"); 307 verifyFormat("x = (a) or (b);"); 308 verifyFormat("x = (a) bitand (b);"); 309 verifyFormat("x = (a) bitor (b);"); 310 verifyFormat("x = (a) not_eq (b);"); 311 verifyFormat("x = (a) and_eq (b);"); 312 verifyFormat("x = (a) or_eq (b);"); 313 verifyFormat("x = (a) xor (b);"); 314 } 315 316 //===----------------------------------------------------------------------===// 317 // Tests for control statements. 318 //===----------------------------------------------------------------------===// 319 320 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 321 verifyFormat("if (true)\n f();\ng();"); 322 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 323 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 324 325 FormatStyle AllowsMergedIf = getLLVMStyle(); 326 AllowsMergedIf.AlignEscapedNewlinesLeft = true; 327 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 328 verifyFormat("if (a)\n" 329 " // comment\n" 330 " f();", 331 AllowsMergedIf); 332 verifyFormat("{\n" 333 " if (a)\n" 334 " label:\n" 335 " f();\n" 336 "}", 337 AllowsMergedIf); 338 verifyFormat("#define A \\\n" 339 " if (a) \\\n" 340 " label: \\\n" 341 " f()", 342 AllowsMergedIf); 343 verifyFormat("if (a)\n" 344 " ;", 345 AllowsMergedIf); 346 verifyFormat("if (a)\n" 347 " if (b) return;", 348 AllowsMergedIf); 349 350 verifyFormat("if (a) // Can't merge this\n" 351 " f();\n", 352 AllowsMergedIf); 353 verifyFormat("if (a) /* still don't merge */\n" 354 " f();", 355 AllowsMergedIf); 356 verifyFormat("if (a) { // Never merge this\n" 357 " f();\n" 358 "}", 359 AllowsMergedIf); 360 verifyFormat("if (a) { /* Never merge this */\n" 361 " f();\n" 362 "}", 363 AllowsMergedIf); 364 365 AllowsMergedIf.ColumnLimit = 14; 366 verifyFormat("if (a) return;", AllowsMergedIf); 367 verifyFormat("if (aaaaaaaaa)\n" 368 " return;", 369 AllowsMergedIf); 370 371 AllowsMergedIf.ColumnLimit = 13; 372 verifyFormat("if (a)\n return;", AllowsMergedIf); 373 } 374 375 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 376 FormatStyle AllowsMergedLoops = getLLVMStyle(); 377 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 378 verifyFormat("while (true) continue;", AllowsMergedLoops); 379 verifyFormat("for (;;) continue;", AllowsMergedLoops); 380 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 381 verifyFormat("while (true)\n" 382 " ;", 383 AllowsMergedLoops); 384 verifyFormat("for (;;)\n" 385 " ;", 386 AllowsMergedLoops); 387 verifyFormat("for (;;)\n" 388 " for (;;) continue;", 389 AllowsMergedLoops); 390 verifyFormat("for (;;) // Can't merge this\n" 391 " continue;", 392 AllowsMergedLoops); 393 verifyFormat("for (;;) /* still don't merge */\n" 394 " continue;", 395 AllowsMergedLoops); 396 } 397 398 TEST_F(FormatTest, FormatShortBracedStatements) { 399 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 400 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 401 402 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 403 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 404 405 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 406 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 407 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 408 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 409 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 410 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 411 verifyFormat("if (true) { //\n" 412 " f();\n" 413 "}", 414 AllowSimpleBracedStatements); 415 verifyFormat("if (true) {\n" 416 " f();\n" 417 " f();\n" 418 "}", 419 AllowSimpleBracedStatements); 420 verifyFormat("if (true) {\n" 421 " f();\n" 422 "} else {\n" 423 " f();\n" 424 "}", 425 AllowSimpleBracedStatements); 426 427 verifyFormat("template <int> struct A2 {\n" 428 " struct B {};\n" 429 "};", 430 AllowSimpleBracedStatements); 431 432 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 433 verifyFormat("if (true) {\n" 434 " f();\n" 435 "}", 436 AllowSimpleBracedStatements); 437 verifyFormat("if (true) {\n" 438 " f();\n" 439 "} else {\n" 440 " f();\n" 441 "}", 442 AllowSimpleBracedStatements); 443 444 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 445 verifyFormat("while (true) {\n" 446 " f();\n" 447 "}", 448 AllowSimpleBracedStatements); 449 verifyFormat("for (;;) {\n" 450 " f();\n" 451 "}", 452 AllowSimpleBracedStatements); 453 } 454 455 TEST_F(FormatTest, ParseIfElse) { 456 verifyFormat("if (true)\n" 457 " if (true)\n" 458 " if (true)\n" 459 " f();\n" 460 " else\n" 461 " g();\n" 462 " else\n" 463 " h();\n" 464 "else\n" 465 " i();"); 466 verifyFormat("if (true)\n" 467 " if (true)\n" 468 " if (true) {\n" 469 " if (true)\n" 470 " f();\n" 471 " } else {\n" 472 " g();\n" 473 " }\n" 474 " else\n" 475 " h();\n" 476 "else {\n" 477 " i();\n" 478 "}"); 479 verifyFormat("void f() {\n" 480 " if (a) {\n" 481 " } else {\n" 482 " }\n" 483 "}"); 484 } 485 486 TEST_F(FormatTest, ElseIf) { 487 verifyFormat("if (a) {\n} else if (b) {\n}"); 488 verifyFormat("if (a)\n" 489 " f();\n" 490 "else if (b)\n" 491 " g();\n" 492 "else\n" 493 " h();"); 494 verifyFormat("if (a) {\n" 495 " f();\n" 496 "}\n" 497 "// or else ..\n" 498 "else {\n" 499 " g()\n" 500 "}"); 501 502 verifyFormat("if (a) {\n" 503 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 505 "}"); 506 verifyFormat("if (a) {\n" 507 "} else if (\n" 508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 509 "}", 510 getLLVMStyleWithColumns(62)); 511 } 512 513 TEST_F(FormatTest, FormatsForLoop) { 514 verifyFormat( 515 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 516 " ++VeryVeryLongLoopVariable)\n" 517 " ;"); 518 verifyFormat("for (;;)\n" 519 " f();"); 520 verifyFormat("for (;;) {\n}"); 521 verifyFormat("for (;;) {\n" 522 " f();\n" 523 "}"); 524 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 525 526 verifyFormat( 527 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 528 " E = UnwrappedLines.end();\n" 529 " I != E; ++I) {\n}"); 530 531 verifyFormat( 532 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 533 " ++IIIII) {\n}"); 534 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 535 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 536 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 537 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 538 " I = FD->getDeclsInPrototypeScope().begin(),\n" 539 " E = FD->getDeclsInPrototypeScope().end();\n" 540 " I != E; ++I) {\n}"); 541 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 542 " I = Container.begin(),\n" 543 " E = Container.end();\n" 544 " I != E; ++I) {\n}", 545 getLLVMStyleWithColumns(76)); 546 547 verifyFormat( 548 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 552 " ++aaaaaaaaaaa) {\n}"); 553 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 554 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 555 " ++i) {\n}"); 556 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 557 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 558 "}"); 559 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 560 " aaaaaaaaaa);\n" 561 " iter; ++iter) {\n" 562 "}"); 563 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 565 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 566 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 567 568 FormatStyle NoBinPacking = getLLVMStyle(); 569 NoBinPacking.BinPackParameters = false; 570 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 571 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 572 " aaaaaaaaaaaaaaaa,\n" 573 " aaaaaaaaaaaaaaaa,\n" 574 " aaaaaaaaaaaaaaaa);\n" 575 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 576 "}", 577 NoBinPacking); 578 verifyFormat( 579 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 580 " E = UnwrappedLines.end();\n" 581 " I != E;\n" 582 " ++I) {\n}", 583 NoBinPacking); 584 } 585 586 TEST_F(FormatTest, RangeBasedForLoops) { 587 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 589 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 590 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 591 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 593 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 594 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 595 } 596 597 TEST_F(FormatTest, ForEachLoops) { 598 verifyFormat("void f() {\n" 599 " foreach (Item *item, itemlist) {}\n" 600 " Q_FOREACH (Item *item, itemlist) {}\n" 601 " BOOST_FOREACH (Item *item, itemlist) {}\n" 602 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 603 "}"); 604 605 // As function-like macros. 606 verifyFormat("#define foreach(x, y)\n" 607 "#define Q_FOREACH(x, y)\n" 608 "#define BOOST_FOREACH(x, y)\n" 609 "#define UNKNOWN_FOREACH(x, y)\n"); 610 611 // Not as function-like macros. 612 verifyFormat("#define foreach (x, y)\n" 613 "#define Q_FOREACH (x, y)\n" 614 "#define BOOST_FOREACH (x, y)\n" 615 "#define UNKNOWN_FOREACH (x, y)\n"); 616 } 617 618 TEST_F(FormatTest, FormatsWhileLoop) { 619 verifyFormat("while (true) {\n}"); 620 verifyFormat("while (true)\n" 621 " f();"); 622 verifyFormat("while () {\n}"); 623 verifyFormat("while () {\n" 624 " f();\n" 625 "}"); 626 } 627 628 TEST_F(FormatTest, FormatsDoWhile) { 629 verifyFormat("do {\n" 630 " do_something();\n" 631 "} while (something());"); 632 verifyFormat("do\n" 633 " do_something();\n" 634 "while (something());"); 635 } 636 637 TEST_F(FormatTest, FormatsSwitchStatement) { 638 verifyFormat("switch (x) {\n" 639 "case 1:\n" 640 " f();\n" 641 " break;\n" 642 "case kFoo:\n" 643 "case ns::kBar:\n" 644 "case kBaz:\n" 645 " break;\n" 646 "default:\n" 647 " g();\n" 648 " break;\n" 649 "}"); 650 verifyFormat("switch (x) {\n" 651 "case 1: {\n" 652 " f();\n" 653 " break;\n" 654 "}\n" 655 "case 2: {\n" 656 " break;\n" 657 "}\n" 658 "}"); 659 verifyFormat("switch (x) {\n" 660 "case 1: {\n" 661 " f();\n" 662 " {\n" 663 " g();\n" 664 " h();\n" 665 " }\n" 666 " break;\n" 667 "}\n" 668 "}"); 669 verifyFormat("switch (x) {\n" 670 "case 1: {\n" 671 " f();\n" 672 " if (foo) {\n" 673 " g();\n" 674 " h();\n" 675 " }\n" 676 " break;\n" 677 "}\n" 678 "}"); 679 verifyFormat("switch (x) {\n" 680 "case 1: {\n" 681 " f();\n" 682 " g();\n" 683 "} break;\n" 684 "}"); 685 verifyFormat("switch (test)\n" 686 " ;"); 687 verifyFormat("switch (x) {\n" 688 "default: {\n" 689 " // Do nothing.\n" 690 "}\n" 691 "}"); 692 verifyFormat("switch (x) {\n" 693 "// comment\n" 694 "// if 1, do f()\n" 695 "case 1:\n" 696 " f();\n" 697 "}"); 698 verifyFormat("switch (x) {\n" 699 "case 1:\n" 700 " // Do amazing stuff\n" 701 " {\n" 702 " f();\n" 703 " g();\n" 704 " }\n" 705 " break;\n" 706 "}"); 707 verifyFormat("#define A \\\n" 708 " switch (x) { \\\n" 709 " case a: \\\n" 710 " foo = b; \\\n" 711 " }", 712 getLLVMStyleWithColumns(20)); 713 verifyFormat("#define OPERATION_CASE(name) \\\n" 714 " case OP_name: \\\n" 715 " return operations::Operation##name\n", 716 getLLVMStyleWithColumns(40)); 717 verifyFormat("switch (x) {\n" 718 "case 1:;\n" 719 "default:;\n" 720 " int i;\n" 721 "}"); 722 723 verifyGoogleFormat("switch (x) {\n" 724 " case 1:\n" 725 " f();\n" 726 " break;\n" 727 " case kFoo:\n" 728 " case ns::kBar:\n" 729 " case kBaz:\n" 730 " break;\n" 731 " default:\n" 732 " g();\n" 733 " break;\n" 734 "}"); 735 verifyGoogleFormat("switch (x) {\n" 736 " case 1: {\n" 737 " f();\n" 738 " break;\n" 739 " }\n" 740 "}"); 741 verifyGoogleFormat("switch (test)\n" 742 " ;"); 743 744 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 745 " case OP_name: \\\n" 746 " return operations::Operation##name\n"); 747 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 748 " // Get the correction operation class.\n" 749 " switch (OpCode) {\n" 750 " CASE(Add);\n" 751 " CASE(Subtract);\n" 752 " default:\n" 753 " return operations::Unknown;\n" 754 " }\n" 755 "#undef OPERATION_CASE\n" 756 "}"); 757 verifyFormat("DEBUG({\n" 758 " switch (x) {\n" 759 " case A:\n" 760 " f();\n" 761 " break;\n" 762 " // On B:\n" 763 " case B:\n" 764 " g();\n" 765 " break;\n" 766 " }\n" 767 "});"); 768 verifyFormat("switch (a) {\n" 769 "case (b):\n" 770 " return;\n" 771 "}"); 772 773 verifyFormat("switch (a) {\n" 774 "case some_namespace::\n" 775 " some_constant:\n" 776 " return;\n" 777 "}", 778 getLLVMStyleWithColumns(34)); 779 } 780 781 TEST_F(FormatTest, CaseRanges) { 782 verifyFormat("switch (x) {\n" 783 "case 'A' ... 'Z':\n" 784 "case 1 ... 5:\n" 785 "case a ... b:\n" 786 " break;\n" 787 "}"); 788 } 789 790 TEST_F(FormatTest, ShortCaseLabels) { 791 FormatStyle Style = getLLVMStyle(); 792 Style.AllowShortCaseLabelsOnASingleLine = true; 793 verifyFormat("switch (a) {\n" 794 "case 1: x = 1; break;\n" 795 "case 2: return;\n" 796 "case 3:\n" 797 "case 4:\n" 798 "case 5: return;\n" 799 "case 6: // comment\n" 800 " return;\n" 801 "case 7:\n" 802 " // comment\n" 803 " return;\n" 804 "case 8:\n" 805 " x = 8; // comment\n" 806 " break;\n" 807 "default: y = 1; break;\n" 808 "}", 809 Style); 810 verifyFormat("switch (a) {\n" 811 "#if FOO\n" 812 "case 0: return 0;\n" 813 "#endif\n" 814 "}", 815 Style); 816 verifyFormat("switch (a) {\n" 817 "case 1: {\n" 818 "}\n" 819 "case 2: {\n" 820 " return;\n" 821 "}\n" 822 "case 3: {\n" 823 " x = 1;\n" 824 " return;\n" 825 "}\n" 826 "case 4:\n" 827 " if (x)\n" 828 " return;\n" 829 "}", 830 Style); 831 Style.ColumnLimit = 21; 832 verifyFormat("switch (a) {\n" 833 "case 1: x = 1; break;\n" 834 "case 2: return;\n" 835 "case 3:\n" 836 "case 4:\n" 837 "case 5: return;\n" 838 "default:\n" 839 " y = 1;\n" 840 " break;\n" 841 "}", 842 Style); 843 } 844 845 TEST_F(FormatTest, FormatsLabels) { 846 verifyFormat("void f() {\n" 847 " some_code();\n" 848 "test_label:\n" 849 " some_other_code();\n" 850 " {\n" 851 " some_more_code();\n" 852 " another_label:\n" 853 " some_more_code();\n" 854 " }\n" 855 "}"); 856 verifyFormat("{\n" 857 " some_code();\n" 858 "test_label:\n" 859 " some_other_code();\n" 860 "}"); 861 verifyFormat("{\n" 862 " some_code();\n" 863 "test_label:;\n" 864 " int i = 0;\n" 865 "}"); 866 } 867 868 //===----------------------------------------------------------------------===// 869 // Tests for comments. 870 //===----------------------------------------------------------------------===// 871 872 TEST_F(FormatTest, UnderstandsSingleLineComments) { 873 verifyFormat("//* */"); 874 verifyFormat("// line 1\n" 875 "// line 2\n" 876 "void f() {}\n"); 877 878 verifyFormat("void f() {\n" 879 " // Doesn't do anything\n" 880 "}"); 881 verifyFormat("SomeObject\n" 882 " // Calling someFunction on SomeObject\n" 883 " .someFunction();"); 884 verifyFormat("auto result = SomeObject\n" 885 " // Calling someFunction on SomeObject\n" 886 " .someFunction();"); 887 verifyFormat("void f(int i, // some comment (probably for i)\n" 888 " int j, // some comment (probably for j)\n" 889 " int k); // some comment (probably for k)"); 890 verifyFormat("void f(int i,\n" 891 " // some comment (probably for j)\n" 892 " int j,\n" 893 " // some comment (probably for k)\n" 894 " int k);"); 895 896 verifyFormat("int i // This is a fancy variable\n" 897 " = 5; // with nicely aligned comment."); 898 899 verifyFormat("// Leading comment.\n" 900 "int a; // Trailing comment."); 901 verifyFormat("int a; // Trailing comment\n" 902 " // on 2\n" 903 " // or 3 lines.\n" 904 "int b;"); 905 verifyFormat("int a; // Trailing comment\n" 906 "\n" 907 "// Leading comment.\n" 908 "int b;"); 909 verifyFormat("int a; // Comment.\n" 910 " // More details.\n" 911 "int bbbb; // Another comment."); 912 verifyFormat( 913 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 914 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 915 "int cccccccccccccccccccccccccccccc; // comment\n" 916 "int ddd; // looooooooooooooooooooooooong comment\n" 917 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 918 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 919 "int ccccccccccccccccccc; // comment"); 920 921 verifyFormat("#include \"a\" // comment\n" 922 "#include \"a/b/c\" // comment"); 923 verifyFormat("#include <a> // comment\n" 924 "#include <a/b/c> // comment"); 925 EXPECT_EQ("#include \"a\" // comment\n" 926 "#include \"a/b/c\" // comment", 927 format("#include \\\n" 928 " \"a\" // comment\n" 929 "#include \"a/b/c\" // comment")); 930 931 verifyFormat("enum E {\n" 932 " // comment\n" 933 " VAL_A, // comment\n" 934 " VAL_B\n" 935 "};"); 936 937 verifyFormat( 938 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 939 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 940 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 941 " // Comment inside a statement.\n" 942 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 943 verifyFormat("SomeFunction(a,\n" 944 " // comment\n" 945 " b + x);"); 946 verifyFormat("SomeFunction(a, a,\n" 947 " // comment\n" 948 " b + x);"); 949 verifyFormat( 950 "bool aaaaaaaaaaaaa = // comment\n" 951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 953 954 verifyFormat("int aaaa; // aaaaa\n" 955 "int aa; // aaaaaaa", 956 getLLVMStyleWithColumns(20)); 957 958 EXPECT_EQ("void f() { // This does something ..\n" 959 "}\n" 960 "int a; // This is unrelated", 961 format("void f() { // This does something ..\n" 962 " }\n" 963 "int a; // This is unrelated")); 964 EXPECT_EQ("class C {\n" 965 " void f() { // This does something ..\n" 966 " } // awesome..\n" 967 "\n" 968 " int a; // This is unrelated\n" 969 "};", 970 format("class C{void f() { // This does something ..\n" 971 " } // awesome..\n" 972 " \n" 973 "int a; // This is unrelated\n" 974 "};")); 975 976 EXPECT_EQ("int i; // single line trailing comment", 977 format("int i;\\\n// single line trailing comment")); 978 979 verifyGoogleFormat("int a; // Trailing comment."); 980 981 verifyFormat("someFunction(anotherFunction( // Force break.\n" 982 " parameter));"); 983 984 verifyGoogleFormat("#endif // HEADER_GUARD"); 985 986 verifyFormat("const char *test[] = {\n" 987 " // A\n" 988 " \"aaaa\",\n" 989 " // B\n" 990 " \"aaaaa\"};"); 991 verifyGoogleFormat( 992 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 993 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 994 EXPECT_EQ("D(a, {\n" 995 " // test\n" 996 " int a;\n" 997 "});", 998 format("D(a, {\n" 999 "// test\n" 1000 "int a;\n" 1001 "});")); 1002 1003 EXPECT_EQ("lineWith(); // comment\n" 1004 "// at start\n" 1005 "otherLine();", 1006 format("lineWith(); // comment\n" 1007 "// at start\n" 1008 "otherLine();")); 1009 EXPECT_EQ("lineWith(); // comment\n" 1010 "/*\n" 1011 " * at start */\n" 1012 "otherLine();", 1013 format("lineWith(); // comment\n" 1014 "/*\n" 1015 " * at start */\n" 1016 "otherLine();")); 1017 EXPECT_EQ("lineWith(); // comment\n" 1018 " // at start\n" 1019 "otherLine();", 1020 format("lineWith(); // comment\n" 1021 " // at start\n" 1022 "otherLine();")); 1023 1024 EXPECT_EQ("lineWith(); // comment\n" 1025 "// at start\n" 1026 "otherLine(); // comment", 1027 format("lineWith(); // comment\n" 1028 "// at start\n" 1029 "otherLine(); // comment")); 1030 EXPECT_EQ("lineWith();\n" 1031 "// at start\n" 1032 "otherLine(); // comment", 1033 format("lineWith();\n" 1034 " // at start\n" 1035 "otherLine(); // comment")); 1036 EXPECT_EQ("// first\n" 1037 "// at start\n" 1038 "otherLine(); // comment", 1039 format("// first\n" 1040 " // at start\n" 1041 "otherLine(); // comment")); 1042 EXPECT_EQ("f();\n" 1043 "// first\n" 1044 "// at start\n" 1045 "otherLine(); // comment", 1046 format("f();\n" 1047 "// first\n" 1048 " // at start\n" 1049 "otherLine(); // comment")); 1050 verifyFormat("f(); // comment\n" 1051 "// first\n" 1052 "// at start\n" 1053 "otherLine();"); 1054 EXPECT_EQ("f(); // comment\n" 1055 "// first\n" 1056 "// at start\n" 1057 "otherLine();", 1058 format("f(); // comment\n" 1059 "// first\n" 1060 " // at start\n" 1061 "otherLine();")); 1062 EXPECT_EQ("f(); // comment\n" 1063 " // first\n" 1064 "// at start\n" 1065 "otherLine();", 1066 format("f(); // comment\n" 1067 " // first\n" 1068 "// at start\n" 1069 "otherLine();")); 1070 EXPECT_EQ("void f() {\n" 1071 " lineWith(); // comment\n" 1072 " // at start\n" 1073 "}", 1074 format("void f() {\n" 1075 " lineWith(); // comment\n" 1076 " // at start\n" 1077 "}")); 1078 EXPECT_EQ("int xy; // a\n" 1079 "int z; // b", 1080 format("int xy; // a\n" 1081 "int z; //b")); 1082 EXPECT_EQ("int xy; // a\n" 1083 "int z; // bb", 1084 format("int xy; // a\n" 1085 "int z; //bb", 1086 getLLVMStyleWithColumns(12))); 1087 1088 verifyFormat("#define A \\\n" 1089 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1090 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1091 getLLVMStyleWithColumns(60)); 1092 verifyFormat( 1093 "#define A \\\n" 1094 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1095 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1096 getLLVMStyleWithColumns(61)); 1097 1098 verifyFormat("if ( // This is some comment\n" 1099 " x + 3) {\n" 1100 "}"); 1101 EXPECT_EQ("if ( // This is some comment\n" 1102 " // spanning two lines\n" 1103 " x + 3) {\n" 1104 "}", 1105 format("if( // This is some comment\n" 1106 " // spanning two lines\n" 1107 " x + 3) {\n" 1108 "}")); 1109 1110 verifyNoCrash("/\\\n/"); 1111 verifyNoCrash("/\\\n* */"); 1112 // The 0-character somehow makes the lexer return a proper comment. 1113 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1114 } 1115 1116 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1117 EXPECT_EQ("SomeFunction(a,\n" 1118 " b, // comment\n" 1119 " c);", 1120 format("SomeFunction(a,\n" 1121 " b, // comment\n" 1122 " c);")); 1123 EXPECT_EQ("SomeFunction(a, b,\n" 1124 " // comment\n" 1125 " c);", 1126 format("SomeFunction(a,\n" 1127 " b,\n" 1128 " // comment\n" 1129 " c);")); 1130 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1131 " c);", 1132 format("SomeFunction(a, b, // comment (unclear relation)\n" 1133 " c);")); 1134 EXPECT_EQ("SomeFunction(a, // comment\n" 1135 " b,\n" 1136 " c); // comment", 1137 format("SomeFunction(a, // comment\n" 1138 " b,\n" 1139 " c); // comment")); 1140 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" 1141 " aaaa), //\n" 1142 " aaaa, bbbbb);", 1143 format("aaaaaaaaaa(aaaa(aaaa,\n" 1144 "aaaa), //\n" 1145 "aaaa, bbbbb);")); 1146 } 1147 1148 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1149 EXPECT_EQ("// comment", format("// comment ")); 1150 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1151 format("int aaaaaaa, bbbbbbb; // comment ", 1152 getLLVMStyleWithColumns(33))); 1153 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1154 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1155 } 1156 1157 TEST_F(FormatTest, UnderstandsBlockComments) { 1158 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1159 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 1160 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1161 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1162 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1163 "/* Trailing comment for aa... */\n" 1164 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1165 EXPECT_EQ( 1166 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1167 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1168 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1169 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1170 EXPECT_EQ( 1171 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1172 " aaaaaaaaaaaaaaaaaa,\n" 1173 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1174 "}", 1175 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1176 " aaaaaaaaaaaaaaaaaa ,\n" 1177 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1178 "}")); 1179 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 1180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1181 1182 FormatStyle NoBinPacking = getLLVMStyle(); 1183 NoBinPacking.BinPackParameters = false; 1184 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1185 " /* parameter 2 */ aaaaaa,\n" 1186 " /* parameter 3 */ aaaaaa,\n" 1187 " /* parameter 4 */ aaaaaa);", 1188 NoBinPacking); 1189 1190 // Aligning block comments in macros. 1191 verifyGoogleFormat("#define A \\\n" 1192 " int i; /*a*/ \\\n" 1193 " int jjj; /*b*/"); 1194 } 1195 1196 TEST_F(FormatTest, AlignsBlockComments) { 1197 EXPECT_EQ("/*\n" 1198 " * Really multi-line\n" 1199 " * comment.\n" 1200 " */\n" 1201 "void f() {}", 1202 format(" /*\n" 1203 " * Really multi-line\n" 1204 " * comment.\n" 1205 " */\n" 1206 " void f() {}")); 1207 EXPECT_EQ("class C {\n" 1208 " /*\n" 1209 " * Another multi-line\n" 1210 " * comment.\n" 1211 " */\n" 1212 " void f() {}\n" 1213 "};", 1214 format("class C {\n" 1215 "/*\n" 1216 " * Another multi-line\n" 1217 " * comment.\n" 1218 " */\n" 1219 "void f() {}\n" 1220 "};")); 1221 EXPECT_EQ("/*\n" 1222 " 1. This is a comment with non-trivial formatting.\n" 1223 " 1.1. We have to indent/outdent all lines equally\n" 1224 " 1.1.1. to keep the formatting.\n" 1225 " */", 1226 format(" /*\n" 1227 " 1. This is a comment with non-trivial formatting.\n" 1228 " 1.1. We have to indent/outdent all lines equally\n" 1229 " 1.1.1. to keep the formatting.\n" 1230 " */")); 1231 EXPECT_EQ("/*\n" 1232 "Don't try to outdent if there's not enough indentation.\n" 1233 "*/", 1234 format(" /*\n" 1235 " Don't try to outdent if there's not enough indentation.\n" 1236 " */")); 1237 1238 EXPECT_EQ("int i; /* Comment with empty...\n" 1239 " *\n" 1240 " * line. */", 1241 format("int i; /* Comment with empty...\n" 1242 " *\n" 1243 " * line. */")); 1244 EXPECT_EQ("int foobar = 0; /* comment */\n" 1245 "int bar = 0; /* multiline\n" 1246 " comment 1 */\n" 1247 "int baz = 0; /* multiline\n" 1248 " comment 2 */\n" 1249 "int bzz = 0; /* multiline\n" 1250 " comment 3 */", 1251 format("int foobar = 0; /* comment */\n" 1252 "int bar = 0; /* multiline\n" 1253 " comment 1 */\n" 1254 "int baz = 0; /* multiline\n" 1255 " comment 2 */\n" 1256 "int bzz = 0; /* multiline\n" 1257 " comment 3 */")); 1258 EXPECT_EQ("int foobar = 0; /* comment */\n" 1259 "int bar = 0; /* multiline\n" 1260 " comment */\n" 1261 "int baz = 0; /* multiline\n" 1262 "comment */", 1263 format("int foobar = 0; /* comment */\n" 1264 "int bar = 0; /* multiline\n" 1265 "comment */\n" 1266 "int baz = 0; /* multiline\n" 1267 "comment */")); 1268 } 1269 1270 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1271 FormatStyle Style = getLLVMStyleWithColumns(20); 1272 Style.ReflowComments = false; 1273 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1274 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1275 } 1276 1277 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1278 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1280 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1282 EXPECT_EQ( 1283 "void ffffffffffff(\n" 1284 " int aaaaaaaa, int bbbbbbbb,\n" 1285 " int cccccccccccc) { /*\n" 1286 " aaaaaaaaaa\n" 1287 " aaaaaaaaaaaaa\n" 1288 " bbbbbbbbbbbbbb\n" 1289 " bbbbbbbbbb\n" 1290 " */\n" 1291 "}", 1292 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1293 "{ /*\n" 1294 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1295 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1296 " */\n" 1297 "}", 1298 getLLVMStyleWithColumns(40))); 1299 } 1300 1301 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1302 EXPECT_EQ("void ffffffffff(\n" 1303 " int aaaaa /* test */);", 1304 format("void ffffffffff(int aaaaa /* test */);", 1305 getLLVMStyleWithColumns(35))); 1306 } 1307 1308 TEST_F(FormatTest, SplitsLongCxxComments) { 1309 EXPECT_EQ("// A comment that\n" 1310 "// doesn't fit on\n" 1311 "// one line", 1312 format("// A comment that doesn't fit on one line", 1313 getLLVMStyleWithColumns(20))); 1314 EXPECT_EQ("/// A comment that\n" 1315 "/// doesn't fit on\n" 1316 "/// one line", 1317 format("/// A comment that doesn't fit on one line", 1318 getLLVMStyleWithColumns(20))); 1319 EXPECT_EQ("//! A comment that\n" 1320 "//! doesn't fit on\n" 1321 "//! one line", 1322 format("//! A comment that doesn't fit on one line", 1323 getLLVMStyleWithColumns(20))); 1324 EXPECT_EQ("// a b c d\n" 1325 "// e f g\n" 1326 "// h i j k", 1327 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1328 EXPECT_EQ( 1329 "// a b c d\n" 1330 "// e f g\n" 1331 "// h i j k", 1332 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1333 EXPECT_EQ("if (true) // A comment that\n" 1334 " // doesn't fit on\n" 1335 " // one line", 1336 format("if (true) // A comment that doesn't fit on one line ", 1337 getLLVMStyleWithColumns(30))); 1338 EXPECT_EQ("// Don't_touch_leading_whitespace", 1339 format("// Don't_touch_leading_whitespace", 1340 getLLVMStyleWithColumns(20))); 1341 EXPECT_EQ("// Add leading\n" 1342 "// whitespace", 1343 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1344 EXPECT_EQ("/// Add leading\n" 1345 "/// whitespace", 1346 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1347 EXPECT_EQ("//! Add leading\n" 1348 "//! whitespace", 1349 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1350 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1351 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1352 "// limit", 1353 format("//Even if it makes the line exceed the column limit", 1354 getLLVMStyleWithColumns(51))); 1355 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1356 1357 EXPECT_EQ("// aa bb cc dd", 1358 format("// aa bb cc dd ", 1359 getLLVMStyleWithColumns(15))); 1360 1361 EXPECT_EQ("// A comment before\n" 1362 "// a macro\n" 1363 "// definition\n" 1364 "#define a b", 1365 format("// A comment before a macro definition\n" 1366 "#define a b", 1367 getLLVMStyleWithColumns(20))); 1368 EXPECT_EQ("void ffffff(\n" 1369 " int aaaaaaaaa, // wwww\n" 1370 " int bbbbbbbbbb, // xxxxxxx\n" 1371 " // yyyyyyyyyy\n" 1372 " int c, int d, int e) {}", 1373 format("void ffffff(\n" 1374 " int aaaaaaaaa, // wwww\n" 1375 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1376 " int c, int d, int e) {}", 1377 getLLVMStyleWithColumns(40))); 1378 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1379 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1380 getLLVMStyleWithColumns(20))); 1381 EXPECT_EQ( 1382 "#define XXX // a b c d\n" 1383 " // e f g h", 1384 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1385 EXPECT_EQ( 1386 "#define XXX // q w e r\n" 1387 " // t y u i", 1388 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1389 } 1390 1391 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1392 EXPECT_EQ("// A comment\n" 1393 "// that doesn't\n" 1394 "// fit on one\n" 1395 "// line", 1396 format("// A comment that doesn't fit on one line", 1397 getLLVMStyleWithColumns(20))); 1398 EXPECT_EQ("/// A comment\n" 1399 "/// that doesn't\n" 1400 "/// fit on one\n" 1401 "/// line", 1402 format("/// A comment that doesn't fit on one line", 1403 getLLVMStyleWithColumns(20))); 1404 } 1405 1406 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1407 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1408 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1409 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1410 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1411 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1412 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1413 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1414 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1415 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1416 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1417 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1418 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1419 getLLVMStyleWithColumns(50))); 1420 // FIXME: One day we might want to implement adjustment of leading whitespace 1421 // of the consecutive lines in this kind of comment: 1422 EXPECT_EQ("double\n" 1423 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1424 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1425 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1426 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1427 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1428 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1429 getLLVMStyleWithColumns(49))); 1430 } 1431 1432 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1433 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1434 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1435 EXPECT_EQ( 1436 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1437 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1438 EXPECT_EQ( 1439 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1440 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1441 } 1442 1443 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1444 EXPECT_EQ("if (xxx ==\n" 1445 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1446 " zzz)\n" 1447 " q();", 1448 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1449 " zzz) q();", 1450 getLLVMStyleWithColumns(40))); 1451 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1452 " yyy && // aaaaaa bbbbbbbb cccc\n" 1453 " zzz)\n" 1454 " q();", 1455 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1456 " zzz) q();", 1457 getLLVMStyleWithColumns(40))); 1458 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1459 " yyy || // aaaaaa bbbbbbbb cccc\n" 1460 " zzz)\n" 1461 " q();", 1462 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1463 " zzz) q();", 1464 getLLVMStyleWithColumns(40))); 1465 EXPECT_EQ("fffffffff(\n" 1466 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1467 " zzz);", 1468 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1469 " zzz);", 1470 getLLVMStyleWithColumns(40))); 1471 } 1472 1473 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1474 EXPECT_EQ("#define A(x) /* \\\n" 1475 " a comment \\\n" 1476 " inside */ \\\n" 1477 " f();", 1478 format("#define A(x) /* \\\n" 1479 " a comment \\\n" 1480 " inside */ \\\n" 1481 " f();", 1482 getLLVMStyleWithColumns(17))); 1483 EXPECT_EQ("#define A( \\\n" 1484 " x) /* \\\n" 1485 " a comment \\\n" 1486 " inside */ \\\n" 1487 " f();", 1488 format("#define A( \\\n" 1489 " x) /* \\\n" 1490 " a comment \\\n" 1491 " inside */ \\\n" 1492 " f();", 1493 getLLVMStyleWithColumns(17))); 1494 } 1495 1496 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1497 EXPECT_EQ("namespace {}\n// Test\n#define A", 1498 format("namespace {}\n // Test\n#define A")); 1499 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1500 format("namespace {}\n /* Test */\n#define A")); 1501 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1502 format("namespace {}\n /* Test */ #define A")); 1503 } 1504 1505 TEST_F(FormatTest, SplitsLongLinesInComments) { 1506 EXPECT_EQ("/* This is a long\n" 1507 " * comment that\n" 1508 " * doesn't\n" 1509 " * fit on one line.\n" 1510 " */", 1511 format("/* " 1512 "This is a long " 1513 "comment that " 1514 "doesn't " 1515 "fit on one line. */", 1516 getLLVMStyleWithColumns(20))); 1517 EXPECT_EQ( 1518 "/* a b c d\n" 1519 " * e f g\n" 1520 " * h i j k\n" 1521 " */", 1522 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1523 EXPECT_EQ( 1524 "/* a b c d\n" 1525 " * e f g\n" 1526 " * h i j k\n" 1527 " */", 1528 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1529 EXPECT_EQ("/*\n" 1530 "This is a long\n" 1531 "comment that doesn't\n" 1532 "fit on one line.\n" 1533 "*/", 1534 format("/*\n" 1535 "This is a long " 1536 "comment that doesn't " 1537 "fit on one line. \n" 1538 "*/", 1539 getLLVMStyleWithColumns(20))); 1540 EXPECT_EQ("/*\n" 1541 " * This is a long\n" 1542 " * comment that\n" 1543 " * doesn't fit on\n" 1544 " * one line.\n" 1545 " */", 1546 format("/* \n" 1547 " * This is a long " 1548 " comment that " 1549 " doesn't fit on " 1550 " one line. \n" 1551 " */", 1552 getLLVMStyleWithColumns(20))); 1553 EXPECT_EQ("/*\n" 1554 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1555 " * so_it_should_be_broken\n" 1556 " * wherever_a_space_occurs\n" 1557 " */", 1558 format("/*\n" 1559 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1560 " so_it_should_be_broken " 1561 " wherever_a_space_occurs \n" 1562 " */", 1563 getLLVMStyleWithColumns(20))); 1564 EXPECT_EQ("/*\n" 1565 " * This_comment_can_not_be_broken_into_lines\n" 1566 " */", 1567 format("/*\n" 1568 " * This_comment_can_not_be_broken_into_lines\n" 1569 " */", 1570 getLLVMStyleWithColumns(20))); 1571 EXPECT_EQ("{\n" 1572 " /*\n" 1573 " This is another\n" 1574 " long comment that\n" 1575 " doesn't fit on one\n" 1576 " line 1234567890\n" 1577 " */\n" 1578 "}", 1579 format("{\n" 1580 "/*\n" 1581 "This is another " 1582 " long comment that " 1583 " doesn't fit on one" 1584 " line 1234567890\n" 1585 "*/\n" 1586 "}", 1587 getLLVMStyleWithColumns(20))); 1588 EXPECT_EQ("{\n" 1589 " /*\n" 1590 " * This i s\n" 1591 " * another comment\n" 1592 " * t hat doesn' t\n" 1593 " * fit on one l i\n" 1594 " * n e\n" 1595 " */\n" 1596 "}", 1597 format("{\n" 1598 "/*\n" 1599 " * This i s" 1600 " another comment" 1601 " t hat doesn' t" 1602 " fit on one l i" 1603 " n e\n" 1604 " */\n" 1605 "}", 1606 getLLVMStyleWithColumns(20))); 1607 EXPECT_EQ("/*\n" 1608 " * This is a long\n" 1609 " * comment that\n" 1610 " * doesn't fit on\n" 1611 " * one line\n" 1612 " */", 1613 format(" /*\n" 1614 " * This is a long comment that doesn't fit on one line\n" 1615 " */", 1616 getLLVMStyleWithColumns(20))); 1617 EXPECT_EQ("{\n" 1618 " if (something) /* This is a\n" 1619 " long\n" 1620 " comment */\n" 1621 " ;\n" 1622 "}", 1623 format("{\n" 1624 " if (something) /* This is a long comment */\n" 1625 " ;\n" 1626 "}", 1627 getLLVMStyleWithColumns(30))); 1628 1629 EXPECT_EQ("/* A comment before\n" 1630 " * a macro\n" 1631 " * definition */\n" 1632 "#define a b", 1633 format("/* A comment before a macro definition */\n" 1634 "#define a b", 1635 getLLVMStyleWithColumns(20))); 1636 1637 EXPECT_EQ("/* some comment\n" 1638 " * a comment\n" 1639 "* that we break\n" 1640 " * another comment\n" 1641 "* we have to break\n" 1642 "* a left comment\n" 1643 " */", 1644 format(" /* some comment\n" 1645 " * a comment that we break\n" 1646 " * another comment we have to break\n" 1647 "* a left comment\n" 1648 " */", 1649 getLLVMStyleWithColumns(20))); 1650 1651 EXPECT_EQ("/**\n" 1652 " * multiline block\n" 1653 " * comment\n" 1654 " *\n" 1655 " */", 1656 format("/**\n" 1657 " * multiline block comment\n" 1658 " *\n" 1659 " */", 1660 getLLVMStyleWithColumns(20))); 1661 1662 EXPECT_EQ("/*\n" 1663 "\n" 1664 "\n" 1665 " */\n", 1666 format(" /* \n" 1667 " \n" 1668 " \n" 1669 " */\n")); 1670 1671 EXPECT_EQ("/* a a */", 1672 format("/* a a */", getLLVMStyleWithColumns(15))); 1673 EXPECT_EQ("/* a a bc */", 1674 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1675 EXPECT_EQ("/* aaa aaa\n" 1676 " * aaaaa */", 1677 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1678 EXPECT_EQ("/* aaa aaa\n" 1679 " * aaaaa */", 1680 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1681 } 1682 1683 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1684 EXPECT_EQ("#define X \\\n" 1685 " /* \\\n" 1686 " Test \\\n" 1687 " Macro comment \\\n" 1688 " with a long \\\n" 1689 " line \\\n" 1690 " */ \\\n" 1691 " A + B", 1692 format("#define X \\\n" 1693 " /*\n" 1694 " Test\n" 1695 " Macro comment with a long line\n" 1696 " */ \\\n" 1697 " A + B", 1698 getLLVMStyleWithColumns(20))); 1699 EXPECT_EQ("#define X \\\n" 1700 " /* Macro comment \\\n" 1701 " with a long \\\n" 1702 " line */ \\\n" 1703 " A + B", 1704 format("#define X \\\n" 1705 " /* Macro comment with a long\n" 1706 " line */ \\\n" 1707 " A + B", 1708 getLLVMStyleWithColumns(20))); 1709 EXPECT_EQ("#define X \\\n" 1710 " /* Macro comment \\\n" 1711 " * with a long \\\n" 1712 " * line */ \\\n" 1713 " A + B", 1714 format("#define X \\\n" 1715 " /* Macro comment with a long line */ \\\n" 1716 " A + B", 1717 getLLVMStyleWithColumns(20))); 1718 } 1719 1720 TEST_F(FormatTest, CommentsInStaticInitializers) { 1721 EXPECT_EQ( 1722 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1723 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1724 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1725 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1726 " aaaaaaaaaaaaaaaaaaaa};", 1727 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1728 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1729 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1730 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1731 " aaaaaaaaaaaaaaaaaaaa };")); 1732 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1733 " bbbbbbbbbbb, ccccccccccc};"); 1734 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1735 " // comment for bb....\n" 1736 " bbbbbbbbbbb, ccccccccccc};"); 1737 verifyGoogleFormat( 1738 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1739 " bbbbbbbbbbb, ccccccccccc};"); 1740 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1741 " // comment for bb....\n" 1742 " bbbbbbbbbbb, ccccccccccc};"); 1743 1744 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1745 " {d, e, f}, // Group #2\n" 1746 " {g, h, i}}; // Group #3"); 1747 verifyFormat("S s = {{// Group #1\n" 1748 " a, b, c},\n" 1749 " {// Group #2\n" 1750 " d, e, f},\n" 1751 " {// Group #3\n" 1752 " g, h, i}};"); 1753 1754 EXPECT_EQ("S s = {\n" 1755 " // Some comment\n" 1756 " a,\n" 1757 "\n" 1758 " // Comment after empty line\n" 1759 " b}", 1760 format("S s = {\n" 1761 " // Some comment\n" 1762 " a,\n" 1763 " \n" 1764 " // Comment after empty line\n" 1765 " b\n" 1766 "}")); 1767 EXPECT_EQ("S s = {\n" 1768 " /* Some comment */\n" 1769 " a,\n" 1770 "\n" 1771 " /* Comment after empty line */\n" 1772 " b}", 1773 format("S s = {\n" 1774 " /* Some comment */\n" 1775 " a,\n" 1776 " \n" 1777 " /* Comment after empty line */\n" 1778 " b\n" 1779 "}")); 1780 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1781 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1782 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1783 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1784 } 1785 1786 TEST_F(FormatTest, IgnoresIf0Contents) { 1787 EXPECT_EQ("#if 0\n" 1788 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1789 "#endif\n" 1790 "void f() {}", 1791 format("#if 0\n" 1792 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1793 "#endif\n" 1794 "void f( ) { }")); 1795 EXPECT_EQ("#if false\n" 1796 "void f( ) { }\n" 1797 "#endif\n" 1798 "void g() {}\n", 1799 format("#if false\n" 1800 "void f( ) { }\n" 1801 "#endif\n" 1802 "void g( ) { }\n")); 1803 EXPECT_EQ("enum E {\n" 1804 " One,\n" 1805 " Two,\n" 1806 "#if 0\n" 1807 "Three,\n" 1808 " Four,\n" 1809 "#endif\n" 1810 " Five\n" 1811 "};", 1812 format("enum E {\n" 1813 " One,Two,\n" 1814 "#if 0\n" 1815 "Three,\n" 1816 " Four,\n" 1817 "#endif\n" 1818 " Five};")); 1819 EXPECT_EQ("enum F {\n" 1820 " One,\n" 1821 "#if 1\n" 1822 " Two,\n" 1823 "#if 0\n" 1824 "Three,\n" 1825 " Four,\n" 1826 "#endif\n" 1827 " Five\n" 1828 "#endif\n" 1829 "};", 1830 format("enum F {\n" 1831 "One,\n" 1832 "#if 1\n" 1833 "Two,\n" 1834 "#if 0\n" 1835 "Three,\n" 1836 " Four,\n" 1837 "#endif\n" 1838 "Five\n" 1839 "#endif\n" 1840 "};")); 1841 EXPECT_EQ("enum G {\n" 1842 " One,\n" 1843 "#if 0\n" 1844 "Two,\n" 1845 "#else\n" 1846 " Three,\n" 1847 "#endif\n" 1848 " Four\n" 1849 "};", 1850 format("enum G {\n" 1851 "One,\n" 1852 "#if 0\n" 1853 "Two,\n" 1854 "#else\n" 1855 "Three,\n" 1856 "#endif\n" 1857 "Four\n" 1858 "};")); 1859 EXPECT_EQ("enum H {\n" 1860 " One,\n" 1861 "#if 0\n" 1862 "#ifdef Q\n" 1863 "Two,\n" 1864 "#else\n" 1865 "Three,\n" 1866 "#endif\n" 1867 "#endif\n" 1868 " Four\n" 1869 "};", 1870 format("enum H {\n" 1871 "One,\n" 1872 "#if 0\n" 1873 "#ifdef Q\n" 1874 "Two,\n" 1875 "#else\n" 1876 "Three,\n" 1877 "#endif\n" 1878 "#endif\n" 1879 "Four\n" 1880 "};")); 1881 EXPECT_EQ("enum I {\n" 1882 " One,\n" 1883 "#if /* test */ 0 || 1\n" 1884 "Two,\n" 1885 "Three,\n" 1886 "#endif\n" 1887 " Four\n" 1888 "};", 1889 format("enum I {\n" 1890 "One,\n" 1891 "#if /* test */ 0 || 1\n" 1892 "Two,\n" 1893 "Three,\n" 1894 "#endif\n" 1895 "Four\n" 1896 "};")); 1897 EXPECT_EQ("enum J {\n" 1898 " One,\n" 1899 "#if 0\n" 1900 "#if 0\n" 1901 "Two,\n" 1902 "#else\n" 1903 "Three,\n" 1904 "#endif\n" 1905 "Four,\n" 1906 "#endif\n" 1907 " Five\n" 1908 "};", 1909 format("enum J {\n" 1910 "One,\n" 1911 "#if 0\n" 1912 "#if 0\n" 1913 "Two,\n" 1914 "#else\n" 1915 "Three,\n" 1916 "#endif\n" 1917 "Four,\n" 1918 "#endif\n" 1919 "Five\n" 1920 "};")); 1921 } 1922 1923 //===----------------------------------------------------------------------===// 1924 // Tests for classes, namespaces, etc. 1925 //===----------------------------------------------------------------------===// 1926 1927 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1928 verifyFormat("class A {};"); 1929 } 1930 1931 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1932 verifyFormat("class A {\n" 1933 "public:\n" 1934 "public: // comment\n" 1935 "protected:\n" 1936 "private:\n" 1937 " void f() {}\n" 1938 "};"); 1939 verifyGoogleFormat("class A {\n" 1940 " public:\n" 1941 " protected:\n" 1942 " private:\n" 1943 " void f() {}\n" 1944 "};"); 1945 verifyFormat("class A {\n" 1946 "public slots:\n" 1947 " void f1() {}\n" 1948 "public Q_SLOTS:\n" 1949 " void f2() {}\n" 1950 "protected slots:\n" 1951 " void f3() {}\n" 1952 "protected Q_SLOTS:\n" 1953 " void f4() {}\n" 1954 "private slots:\n" 1955 " void f5() {}\n" 1956 "private Q_SLOTS:\n" 1957 " void f6() {}\n" 1958 "signals:\n" 1959 " void g1();\n" 1960 "Q_SIGNALS:\n" 1961 " void g2();\n" 1962 "};"); 1963 1964 // Don't interpret 'signals' the wrong way. 1965 verifyFormat("signals.set();"); 1966 verifyFormat("for (Signals signals : f()) {\n}"); 1967 verifyFormat("{\n" 1968 " signals.set(); // This needs indentation.\n" 1969 "}"); 1970 verifyFormat("void f() {\n" 1971 "label:\n" 1972 " signals.baz();\n" 1973 "}"); 1974 } 1975 1976 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1977 EXPECT_EQ("class A {\n" 1978 "public:\n" 1979 " void f();\n" 1980 "\n" 1981 "private:\n" 1982 " void g() {}\n" 1983 " // test\n" 1984 "protected:\n" 1985 " int h;\n" 1986 "};", 1987 format("class A {\n" 1988 "public:\n" 1989 "void f();\n" 1990 "private:\n" 1991 "void g() {}\n" 1992 "// test\n" 1993 "protected:\n" 1994 "int h;\n" 1995 "};")); 1996 EXPECT_EQ("class A {\n" 1997 "protected:\n" 1998 "public:\n" 1999 " void f();\n" 2000 "};", 2001 format("class A {\n" 2002 "protected:\n" 2003 "\n" 2004 "public:\n" 2005 "\n" 2006 " void f();\n" 2007 "};")); 2008 2009 // Even ensure proper spacing inside macros. 2010 EXPECT_EQ("#define B \\\n" 2011 " class A { \\\n" 2012 " protected: \\\n" 2013 " public: \\\n" 2014 " void f(); \\\n" 2015 " };", 2016 format("#define B \\\n" 2017 " class A { \\\n" 2018 " protected: \\\n" 2019 " \\\n" 2020 " public: \\\n" 2021 " \\\n" 2022 " void f(); \\\n" 2023 " };", 2024 getGoogleStyle())); 2025 // But don't remove empty lines after macros ending in access specifiers. 2026 EXPECT_EQ("#define A private:\n" 2027 "\n" 2028 "int i;", 2029 format("#define A private:\n" 2030 "\n" 2031 "int i;")); 2032 } 2033 2034 TEST_F(FormatTest, FormatsClasses) { 2035 verifyFormat("class A : public B {};"); 2036 verifyFormat("class A : public ::B {};"); 2037 2038 verifyFormat( 2039 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2040 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2041 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2042 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2043 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2044 verifyFormat( 2045 "class A : public B, public C, public D, public E, public F {};"); 2046 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2047 " public C,\n" 2048 " public D,\n" 2049 " public E,\n" 2050 " public F,\n" 2051 " public G {};"); 2052 2053 verifyFormat("class\n" 2054 " ReallyReallyLongClassName {\n" 2055 " int i;\n" 2056 "};", 2057 getLLVMStyleWithColumns(32)); 2058 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2059 " aaaaaaaaaaaaaaaa> {};"); 2060 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2061 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2062 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2063 verifyFormat("template <class R, class C>\n" 2064 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2065 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2066 verifyFormat("class ::A::B {};"); 2067 } 2068 2069 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2070 verifyFormat("class A {\n} a, b;"); 2071 verifyFormat("struct A {\n} a, b;"); 2072 verifyFormat("union A {\n} a;"); 2073 } 2074 2075 TEST_F(FormatTest, FormatsEnum) { 2076 verifyFormat("enum {\n" 2077 " Zero,\n" 2078 " One = 1,\n" 2079 " Two = One + 1,\n" 2080 " Three = (One + Two),\n" 2081 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2082 " Five = (One, Two, Three, Four, 5)\n" 2083 "};"); 2084 verifyGoogleFormat("enum {\n" 2085 " Zero,\n" 2086 " One = 1,\n" 2087 " Two = One + 1,\n" 2088 " Three = (One + Two),\n" 2089 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2090 " Five = (One, Two, Three, Four, 5)\n" 2091 "};"); 2092 verifyFormat("enum Enum {};"); 2093 verifyFormat("enum {};"); 2094 verifyFormat("enum X E {} d;"); 2095 verifyFormat("enum __attribute__((...)) E {} d;"); 2096 verifyFormat("enum __declspec__((...)) E {} d;"); 2097 verifyFormat("enum {\n" 2098 " Bar = Foo<int, int>::value\n" 2099 "};", 2100 getLLVMStyleWithColumns(30)); 2101 2102 verifyFormat("enum ShortEnum { A, B, C };"); 2103 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2104 2105 EXPECT_EQ("enum KeepEmptyLines {\n" 2106 " ONE,\n" 2107 "\n" 2108 " TWO,\n" 2109 "\n" 2110 " THREE\n" 2111 "}", 2112 format("enum KeepEmptyLines {\n" 2113 " ONE,\n" 2114 "\n" 2115 " TWO,\n" 2116 "\n" 2117 "\n" 2118 " THREE\n" 2119 "}")); 2120 verifyFormat("enum E { // comment\n" 2121 " ONE,\n" 2122 " TWO\n" 2123 "};\n" 2124 "int i;"); 2125 // Not enums. 2126 verifyFormat("enum X f() {\n" 2127 " a();\n" 2128 " return 42;\n" 2129 "}"); 2130 verifyFormat("enum X Type::f() {\n" 2131 " a();\n" 2132 " return 42;\n" 2133 "}"); 2134 verifyFormat("enum ::X f() {\n" 2135 " a();\n" 2136 " return 42;\n" 2137 "}"); 2138 verifyFormat("enum ns::X f() {\n" 2139 " a();\n" 2140 " return 42;\n" 2141 "}"); 2142 } 2143 2144 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2145 verifyFormat("enum Type {\n" 2146 " One = 0; // These semicolons should be commas.\n" 2147 " Two = 1;\n" 2148 "};"); 2149 verifyFormat("namespace n {\n" 2150 "enum Type {\n" 2151 " One,\n" 2152 " Two, // missing };\n" 2153 " int i;\n" 2154 "}\n" 2155 "void g() {}"); 2156 } 2157 2158 TEST_F(FormatTest, FormatsEnumStruct) { 2159 verifyFormat("enum struct {\n" 2160 " Zero,\n" 2161 " One = 1,\n" 2162 " Two = One + 1,\n" 2163 " Three = (One + Two),\n" 2164 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2165 " Five = (One, Two, Three, Four, 5)\n" 2166 "};"); 2167 verifyFormat("enum struct Enum {};"); 2168 verifyFormat("enum struct {};"); 2169 verifyFormat("enum struct X E {} d;"); 2170 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2171 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2172 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2173 } 2174 2175 TEST_F(FormatTest, FormatsEnumClass) { 2176 verifyFormat("enum class {\n" 2177 " Zero,\n" 2178 " One = 1,\n" 2179 " Two = One + 1,\n" 2180 " Three = (One + Two),\n" 2181 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2182 " Five = (One, Two, Three, Four, 5)\n" 2183 "};"); 2184 verifyFormat("enum class Enum {};"); 2185 verifyFormat("enum class {};"); 2186 verifyFormat("enum class X E {} d;"); 2187 verifyFormat("enum class __attribute__((...)) E {} d;"); 2188 verifyFormat("enum class __declspec__((...)) E {} d;"); 2189 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2190 } 2191 2192 TEST_F(FormatTest, FormatsEnumTypes) { 2193 verifyFormat("enum X : int {\n" 2194 " A, // Force multiple lines.\n" 2195 " B\n" 2196 "};"); 2197 verifyFormat("enum X : int { A, B };"); 2198 verifyFormat("enum X : std::uint32_t { A, B };"); 2199 } 2200 2201 TEST_F(FormatTest, FormatsNSEnums) { 2202 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2203 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2204 " // Information about someDecentlyLongValue.\n" 2205 " someDecentlyLongValue,\n" 2206 " // Information about anotherDecentlyLongValue.\n" 2207 " anotherDecentlyLongValue,\n" 2208 " // Information about aThirdDecentlyLongValue.\n" 2209 " aThirdDecentlyLongValue\n" 2210 "};"); 2211 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2212 " a = 1,\n" 2213 " b = 2,\n" 2214 " c = 3,\n" 2215 "};"); 2216 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2217 " a = 1,\n" 2218 " b = 2,\n" 2219 " c = 3,\n" 2220 "};"); 2221 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2222 " a = 1,\n" 2223 " b = 2,\n" 2224 " c = 3,\n" 2225 "};"); 2226 } 2227 2228 TEST_F(FormatTest, FormatsBitfields) { 2229 verifyFormat("struct Bitfields {\n" 2230 " unsigned sClass : 8;\n" 2231 " unsigned ValueKind : 2;\n" 2232 "};"); 2233 verifyFormat("struct A {\n" 2234 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2235 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2236 "};"); 2237 verifyFormat("struct MyStruct {\n" 2238 " uchar data;\n" 2239 " uchar : 8;\n" 2240 " uchar : 8;\n" 2241 " uchar other;\n" 2242 "};"); 2243 } 2244 2245 TEST_F(FormatTest, FormatsNamespaces) { 2246 verifyFormat("namespace some_namespace {\n" 2247 "class A {};\n" 2248 "void f() { f(); }\n" 2249 "}"); 2250 verifyFormat("namespace {\n" 2251 "class A {};\n" 2252 "void f() { f(); }\n" 2253 "}"); 2254 verifyFormat("inline namespace X {\n" 2255 "class A {};\n" 2256 "void f() { f(); }\n" 2257 "}"); 2258 verifyFormat("using namespace some_namespace;\n" 2259 "class A {};\n" 2260 "void f() { f(); }"); 2261 2262 // This code is more common than we thought; if we 2263 // layout this correctly the semicolon will go into 2264 // its own line, which is undesirable. 2265 verifyFormat("namespace {};"); 2266 verifyFormat("namespace {\n" 2267 "class A {};\n" 2268 "};"); 2269 2270 verifyFormat("namespace {\n" 2271 "int SomeVariable = 0; // comment\n" 2272 "} // namespace"); 2273 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2274 "#define HEADER_GUARD\n" 2275 "namespace my_namespace {\n" 2276 "int i;\n" 2277 "} // my_namespace\n" 2278 "#endif // HEADER_GUARD", 2279 format("#ifndef HEADER_GUARD\n" 2280 " #define HEADER_GUARD\n" 2281 " namespace my_namespace {\n" 2282 "int i;\n" 2283 "} // my_namespace\n" 2284 "#endif // HEADER_GUARD")); 2285 2286 EXPECT_EQ("namespace A::B {\n" 2287 "class C {};\n" 2288 "}", 2289 format("namespace A::B {\n" 2290 "class C {};\n" 2291 "}")); 2292 2293 FormatStyle Style = getLLVMStyle(); 2294 Style.NamespaceIndentation = FormatStyle::NI_All; 2295 EXPECT_EQ("namespace out {\n" 2296 " int i;\n" 2297 " namespace in {\n" 2298 " int i;\n" 2299 " } // namespace\n" 2300 "} // namespace", 2301 format("namespace out {\n" 2302 "int i;\n" 2303 "namespace in {\n" 2304 "int i;\n" 2305 "} // namespace\n" 2306 "} // namespace", 2307 Style)); 2308 2309 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2310 EXPECT_EQ("namespace out {\n" 2311 "int i;\n" 2312 "namespace in {\n" 2313 " int i;\n" 2314 "} // namespace\n" 2315 "} // namespace", 2316 format("namespace out {\n" 2317 "int i;\n" 2318 "namespace in {\n" 2319 "int i;\n" 2320 "} // namespace\n" 2321 "} // namespace", 2322 Style)); 2323 } 2324 2325 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2326 2327 TEST_F(FormatTest, FormatsInlineASM) { 2328 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2329 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2330 verifyFormat( 2331 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2332 " \"cpuid\\n\\t\"\n" 2333 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2334 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2335 " : \"a\"(value));"); 2336 EXPECT_EQ( 2337 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2338 " __asm {\n" 2339 " mov edx,[that] // vtable in edx\n" 2340 " mov eax,methodIndex\n" 2341 " call [edx][eax*4] // stdcall\n" 2342 " }\n" 2343 "}", 2344 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2345 " __asm {\n" 2346 " mov edx,[that] // vtable in edx\n" 2347 " mov eax,methodIndex\n" 2348 " call [edx][eax*4] // stdcall\n" 2349 " }\n" 2350 "}")); 2351 EXPECT_EQ("_asm {\n" 2352 " xor eax, eax;\n" 2353 " cpuid;\n" 2354 "}", 2355 format("_asm {\n" 2356 " xor eax, eax;\n" 2357 " cpuid;\n" 2358 "}")); 2359 verifyFormat("void function() {\n" 2360 " // comment\n" 2361 " asm(\"\");\n" 2362 "}"); 2363 EXPECT_EQ("__asm {\n" 2364 "}\n" 2365 "int i;", 2366 format("__asm {\n" 2367 "}\n" 2368 "int i;")); 2369 } 2370 2371 TEST_F(FormatTest, FormatTryCatch) { 2372 verifyFormat("try {\n" 2373 " throw a * b;\n" 2374 "} catch (int a) {\n" 2375 " // Do nothing.\n" 2376 "} catch (...) {\n" 2377 " exit(42);\n" 2378 "}"); 2379 2380 // Function-level try statements. 2381 verifyFormat("int f() try { return 4; } catch (...) {\n" 2382 " return 5;\n" 2383 "}"); 2384 verifyFormat("class A {\n" 2385 " int a;\n" 2386 " A() try : a(0) {\n" 2387 " } catch (...) {\n" 2388 " throw;\n" 2389 " }\n" 2390 "};\n"); 2391 2392 // Incomplete try-catch blocks. 2393 verifyIncompleteFormat("try {} catch ("); 2394 } 2395 2396 TEST_F(FormatTest, FormatSEHTryCatch) { 2397 verifyFormat("__try {\n" 2398 " int a = b * c;\n" 2399 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2400 " // Do nothing.\n" 2401 "}"); 2402 2403 verifyFormat("__try {\n" 2404 " int a = b * c;\n" 2405 "} __finally {\n" 2406 " // Do nothing.\n" 2407 "}"); 2408 2409 verifyFormat("DEBUG({\n" 2410 " __try {\n" 2411 " } __finally {\n" 2412 " }\n" 2413 "});\n"); 2414 } 2415 2416 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2417 verifyFormat("try {\n" 2418 " f();\n" 2419 "} catch {\n" 2420 " g();\n" 2421 "}"); 2422 verifyFormat("try {\n" 2423 " f();\n" 2424 "} catch (A a) MACRO(x) {\n" 2425 " g();\n" 2426 "} catch (B b) MACRO(x) {\n" 2427 " g();\n" 2428 "}"); 2429 } 2430 2431 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2432 FormatStyle Style = getLLVMStyle(); 2433 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2434 FormatStyle::BS_WebKit}) { 2435 Style.BreakBeforeBraces = BraceStyle; 2436 verifyFormat("try {\n" 2437 " // something\n" 2438 "} catch (...) {\n" 2439 " // something\n" 2440 "}", 2441 Style); 2442 } 2443 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2444 verifyFormat("try {\n" 2445 " // something\n" 2446 "}\n" 2447 "catch (...) {\n" 2448 " // something\n" 2449 "}", 2450 Style); 2451 verifyFormat("__try {\n" 2452 " // something\n" 2453 "}\n" 2454 "__finally {\n" 2455 " // something\n" 2456 "}", 2457 Style); 2458 verifyFormat("@try {\n" 2459 " // something\n" 2460 "}\n" 2461 "@finally {\n" 2462 " // something\n" 2463 "}", 2464 Style); 2465 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2466 verifyFormat("try\n" 2467 "{\n" 2468 " // something\n" 2469 "}\n" 2470 "catch (...)\n" 2471 "{\n" 2472 " // something\n" 2473 "}", 2474 Style); 2475 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2476 verifyFormat("try\n" 2477 " {\n" 2478 " // something\n" 2479 " }\n" 2480 "catch (...)\n" 2481 " {\n" 2482 " // something\n" 2483 " }", 2484 Style); 2485 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2486 Style.BraceWrapping.BeforeCatch = true; 2487 verifyFormat("try {\n" 2488 " // something\n" 2489 "}\n" 2490 "catch (...) {\n" 2491 " // something\n" 2492 "}", 2493 Style); 2494 } 2495 2496 TEST_F(FormatTest, StaticInitializers) { 2497 verifyFormat("static SomeClass SC = {1, 'a'};"); 2498 2499 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2500 " 100000000, " 2501 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2502 2503 // Here, everything other than the "}" would fit on a line. 2504 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2505 " 10000000000000000000000000};"); 2506 EXPECT_EQ("S s = {a,\n" 2507 "\n" 2508 " b};", 2509 format("S s = {\n" 2510 " a,\n" 2511 "\n" 2512 " b\n" 2513 "};")); 2514 2515 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2516 // line. However, the formatting looks a bit off and this probably doesn't 2517 // happen often in practice. 2518 verifyFormat("static int Variable[1] = {\n" 2519 " {1000000000000000000000000000000000000}};", 2520 getLLVMStyleWithColumns(40)); 2521 } 2522 2523 TEST_F(FormatTest, DesignatedInitializers) { 2524 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2525 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2526 " .bbbbbbbbbb = 2,\n" 2527 " .cccccccccc = 3,\n" 2528 " .dddddddddd = 4,\n" 2529 " .eeeeeeeeee = 5};"); 2530 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2531 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2532 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2533 " .ccccccccccccccccccccccccccc = 3,\n" 2534 " .ddddddddddddddddddddddddddd = 4,\n" 2535 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2536 2537 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2538 } 2539 2540 TEST_F(FormatTest, NestedStaticInitializers) { 2541 verifyFormat("static A x = {{{}}};\n"); 2542 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2543 " {init1, init2, init3, init4}}};", 2544 getLLVMStyleWithColumns(50)); 2545 2546 verifyFormat("somes Status::global_reps[3] = {\n" 2547 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2548 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2549 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2550 getLLVMStyleWithColumns(60)); 2551 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2552 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2553 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2554 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2555 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2556 " {rect.fRight - rect.fLeft, rect.fBottom - " 2557 "rect.fTop}};"); 2558 2559 verifyFormat( 2560 "SomeArrayOfSomeType a = {\n" 2561 " {{1, 2, 3},\n" 2562 " {1, 2, 3},\n" 2563 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2564 " 333333333333333333333333333333},\n" 2565 " {1, 2, 3},\n" 2566 " {1, 2, 3}}};"); 2567 verifyFormat( 2568 "SomeArrayOfSomeType a = {\n" 2569 " {{1, 2, 3}},\n" 2570 " {{1, 2, 3}},\n" 2571 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2572 " 333333333333333333333333333333}},\n" 2573 " {{1, 2, 3}},\n" 2574 " {{1, 2, 3}}};"); 2575 2576 verifyFormat("struct {\n" 2577 " unsigned bit;\n" 2578 " const char *const name;\n" 2579 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2580 " {kOsWin, \"Windows\"},\n" 2581 " {kOsLinux, \"Linux\"},\n" 2582 " {kOsCrOS, \"Chrome OS\"}};"); 2583 verifyFormat("struct {\n" 2584 " unsigned bit;\n" 2585 " const char *const name;\n" 2586 "} kBitsToOs[] = {\n" 2587 " {kOsMac, \"Mac\"},\n" 2588 " {kOsWin, \"Windows\"},\n" 2589 " {kOsLinux, \"Linux\"},\n" 2590 " {kOsCrOS, \"Chrome OS\"},\n" 2591 "};"); 2592 } 2593 2594 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2595 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2596 " \\\n" 2597 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2598 } 2599 2600 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2601 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2602 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2603 2604 // Do break defaulted and deleted functions. 2605 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2606 " default;", 2607 getLLVMStyleWithColumns(40)); 2608 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2609 " delete;", 2610 getLLVMStyleWithColumns(40)); 2611 } 2612 2613 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2614 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2615 getLLVMStyleWithColumns(40)); 2616 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2617 getLLVMStyleWithColumns(40)); 2618 EXPECT_EQ("#define Q \\\n" 2619 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2620 " \"aaaaaaaa.cpp\"", 2621 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2622 getLLVMStyleWithColumns(40))); 2623 } 2624 2625 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2626 EXPECT_EQ("# 123 \"A string literal\"", 2627 format(" # 123 \"A string literal\"")); 2628 } 2629 2630 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2631 EXPECT_EQ("#;", format("#;")); 2632 verifyFormat("#\n;\n;\n;"); 2633 } 2634 2635 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2636 EXPECT_EQ("#line 42 \"test\"\n", 2637 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2638 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2639 getLLVMStyleWithColumns(12))); 2640 } 2641 2642 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2643 EXPECT_EQ("#line 42 \"test\"", 2644 format("# \\\n line \\\n 42 \\\n \"test\"")); 2645 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2646 } 2647 2648 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2649 verifyFormat("#define A \\x20"); 2650 verifyFormat("#define A \\ x20"); 2651 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2652 verifyFormat("#define A ''"); 2653 verifyFormat("#define A ''qqq"); 2654 verifyFormat("#define A `qqq"); 2655 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2656 EXPECT_EQ("const char *c = STRINGIFY(\n" 2657 "\\na : b);", 2658 format("const char * c = STRINGIFY(\n" 2659 "\\na : b);")); 2660 2661 verifyFormat("a\r\\"); 2662 verifyFormat("a\v\\"); 2663 verifyFormat("a\f\\"); 2664 } 2665 2666 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2667 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2668 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2669 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2670 // FIXME: We never break before the macro name. 2671 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2672 2673 verifyFormat("#define A A\n#define A A"); 2674 verifyFormat("#define A(X) A\n#define A A"); 2675 2676 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2677 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2678 } 2679 2680 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2681 EXPECT_EQ("// somecomment\n" 2682 "#include \"a.h\"\n" 2683 "#define A( \\\n" 2684 " A, B)\n" 2685 "#include \"b.h\"\n" 2686 "// somecomment\n", 2687 format(" // somecomment\n" 2688 " #include \"a.h\"\n" 2689 "#define A(A,\\\n" 2690 " B)\n" 2691 " #include \"b.h\"\n" 2692 " // somecomment\n", 2693 getLLVMStyleWithColumns(13))); 2694 } 2695 2696 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2697 2698 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2699 EXPECT_EQ("#define A \\\n" 2700 " c; \\\n" 2701 " e;\n" 2702 "f;", 2703 format("#define A c; e;\n" 2704 "f;", 2705 getLLVMStyleWithColumns(14))); 2706 } 2707 2708 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2709 2710 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2711 EXPECT_EQ("int x,\n" 2712 "#define A\n" 2713 " y;", 2714 format("int x,\n#define A\ny;")); 2715 } 2716 2717 TEST_F(FormatTest, HashInMacroDefinition) { 2718 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2719 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2720 verifyFormat("#define A \\\n" 2721 " { \\\n" 2722 " f(#c); \\\n" 2723 " }", 2724 getLLVMStyleWithColumns(11)); 2725 2726 verifyFormat("#define A(X) \\\n" 2727 " void function##X()", 2728 getLLVMStyleWithColumns(22)); 2729 2730 verifyFormat("#define A(a, b, c) \\\n" 2731 " void a##b##c()", 2732 getLLVMStyleWithColumns(22)); 2733 2734 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2735 } 2736 2737 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2738 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2739 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2740 } 2741 2742 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2743 EXPECT_EQ("#define A b;", format("#define A \\\n" 2744 " \\\n" 2745 " b;", 2746 getLLVMStyleWithColumns(25))); 2747 EXPECT_EQ("#define A \\\n" 2748 " \\\n" 2749 " a; \\\n" 2750 " b;", 2751 format("#define A \\\n" 2752 " \\\n" 2753 " a; \\\n" 2754 " b;", 2755 getLLVMStyleWithColumns(11))); 2756 EXPECT_EQ("#define A \\\n" 2757 " a; \\\n" 2758 " \\\n" 2759 " b;", 2760 format("#define A \\\n" 2761 " a; \\\n" 2762 " \\\n" 2763 " b;", 2764 getLLVMStyleWithColumns(11))); 2765 } 2766 2767 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2768 verifyIncompleteFormat("#define A :"); 2769 verifyFormat("#define SOMECASES \\\n" 2770 " case 1: \\\n" 2771 " case 2\n", 2772 getLLVMStyleWithColumns(20)); 2773 verifyFormat("#define MACRO(a) \\\n" 2774 " if (a) \\\n" 2775 " f(); \\\n" 2776 " else \\\n" 2777 " g()", 2778 getLLVMStyleWithColumns(18)); 2779 verifyFormat("#define A template <typename T>"); 2780 verifyIncompleteFormat("#define STR(x) #x\n" 2781 "f(STR(this_is_a_string_literal{));"); 2782 verifyFormat("#pragma omp threadprivate( \\\n" 2783 " y)), // expected-warning", 2784 getLLVMStyleWithColumns(28)); 2785 verifyFormat("#d, = };"); 2786 verifyFormat("#if \"a"); 2787 verifyIncompleteFormat("({\n" 2788 "#define b \\\n" 2789 " } \\\n" 2790 " a\n" 2791 "a", 2792 getLLVMStyleWithColumns(15)); 2793 verifyFormat("#define A \\\n" 2794 " { \\\n" 2795 " {\n" 2796 "#define B \\\n" 2797 " } \\\n" 2798 " }", 2799 getLLVMStyleWithColumns(15)); 2800 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2801 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2802 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2803 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2804 } 2805 2806 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2807 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2808 EXPECT_EQ("class A : public QObject {\n" 2809 " Q_OBJECT\n" 2810 "\n" 2811 " A() {}\n" 2812 "};", 2813 format("class A : public QObject {\n" 2814 " Q_OBJECT\n" 2815 "\n" 2816 " A() {\n}\n" 2817 "} ;")); 2818 EXPECT_EQ("MACRO\n" 2819 "/*static*/ int i;", 2820 format("MACRO\n" 2821 " /*static*/ int i;")); 2822 EXPECT_EQ("SOME_MACRO\n" 2823 "namespace {\n" 2824 "void f();\n" 2825 "}", 2826 format("SOME_MACRO\n" 2827 " namespace {\n" 2828 "void f( );\n" 2829 "}")); 2830 // Only if the identifier contains at least 5 characters. 2831 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2832 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2833 // Only if everything is upper case. 2834 EXPECT_EQ("class A : public QObject {\n" 2835 " Q_Object A() {}\n" 2836 "};", 2837 format("class A : public QObject {\n" 2838 " Q_Object\n" 2839 " A() {\n}\n" 2840 "} ;")); 2841 2842 // Only if the next line can actually start an unwrapped line. 2843 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2844 format("SOME_WEIRD_LOG_MACRO\n" 2845 "<< SomeThing;")); 2846 2847 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2848 "(n, buffers))\n", 2849 getChromiumStyle(FormatStyle::LK_Cpp)); 2850 } 2851 2852 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2853 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2854 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2855 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2856 "class X {};\n" 2857 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2858 "int *createScopDetectionPass() { return 0; }", 2859 format(" 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 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2866 // braces, so that inner block is indented one level more. 2867 EXPECT_EQ("int q() {\n" 2868 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2869 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2870 " IPC_END_MESSAGE_MAP()\n" 2871 "}", 2872 format("int q() {\n" 2873 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2874 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2875 " IPC_END_MESSAGE_MAP()\n" 2876 "}")); 2877 2878 // Same inside macros. 2879 EXPECT_EQ("#define LIST(L) \\\n" 2880 " L(A) \\\n" 2881 " L(B) \\\n" 2882 " L(C)", 2883 format("#define LIST(L) \\\n" 2884 " L(A) \\\n" 2885 " L(B) \\\n" 2886 " L(C)", 2887 getGoogleStyle())); 2888 2889 // These must not be recognized as macros. 2890 EXPECT_EQ("int q() {\n" 2891 " f(x);\n" 2892 " f(x) {}\n" 2893 " f(x)->g();\n" 2894 " f(x)->*g();\n" 2895 " f(x).g();\n" 2896 " f(x) = x;\n" 2897 " f(x) += x;\n" 2898 " f(x) -= x;\n" 2899 " f(x) *= x;\n" 2900 " f(x) /= x;\n" 2901 " f(x) %= x;\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)[y].z();\n" 2908 " LOG(INFO) << x;\n" 2909 " ifstream(x) >> x;\n" 2910 "}\n", 2911 format("int q() {\n" 2912 " f(x)\n;\n" 2913 " f(x)\n {}\n" 2914 " f(x)\n->g();\n" 2915 " f(x)\n->*g();\n" 2916 " f(x)\n.g();\n" 2917 " f(x)\n = x;\n" 2918 " f(x)\n += x;\n" 2919 " f(x)\n -= x;\n" 2920 " f(x)\n *= x;\n" 2921 " f(x)\n /= x;\n" 2922 " f(x)\n %= x;\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[y].z();\n" 2929 " LOG(INFO)\n << x;\n" 2930 " ifstream(x)\n >> x;\n" 2931 "}\n")); 2932 EXPECT_EQ("int q() {\n" 2933 " F(x)\n" 2934 " if (1) {\n" 2935 " }\n" 2936 " F(x)\n" 2937 " while (1) {\n" 2938 " }\n" 2939 " F(x)\n" 2940 " G(x);\n" 2941 " F(x)\n" 2942 " try {\n" 2943 " Q();\n" 2944 " } catch (...) {\n" 2945 " }\n" 2946 "}\n", 2947 format("int q() {\n" 2948 "F(x)\n" 2949 "if (1) {}\n" 2950 "F(x)\n" 2951 "while (1) {}\n" 2952 "F(x)\n" 2953 "G(x);\n" 2954 "F(x)\n" 2955 "try { Q(); } catch (...) {}\n" 2956 "}\n")); 2957 EXPECT_EQ("class A {\n" 2958 " A() : t(0) {}\n" 2959 " A(int i) noexcept() : {}\n" 2960 " A(X x)\n" // FIXME: function-level try blocks are broken. 2961 " try : t(0) {\n" 2962 " } catch (...) {\n" 2963 " }\n" 2964 "};", 2965 format("class A {\n" 2966 " A()\n : t(0) {}\n" 2967 " A(int i)\n noexcept() : {}\n" 2968 " A(X x)\n" 2969 " try : t(0) {} catch (...) {}\n" 2970 "};")); 2971 EXPECT_EQ("class SomeClass {\n" 2972 "public:\n" 2973 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2974 "};", 2975 format("class SomeClass {\n" 2976 "public:\n" 2977 " SomeClass()\n" 2978 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2979 "};")); 2980 EXPECT_EQ("class SomeClass {\n" 2981 "public:\n" 2982 " SomeClass()\n" 2983 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2984 "};", 2985 format("class SomeClass {\n" 2986 "public:\n" 2987 " SomeClass()\n" 2988 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2989 "};", 2990 getLLVMStyleWithColumns(40))); 2991 2992 verifyFormat("MACRO(>)"); 2993 } 2994 2995 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2996 verifyFormat("#define A \\\n" 2997 " f({ \\\n" 2998 " g(); \\\n" 2999 " });", 3000 getLLVMStyleWithColumns(11)); 3001 } 3002 3003 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 3004 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 3005 } 3006 3007 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3008 verifyFormat("{\n { a #c; }\n}"); 3009 } 3010 3011 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3012 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3013 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3014 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3015 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3016 } 3017 3018 TEST_F(FormatTest, EscapedNewlines) { 3019 EXPECT_EQ( 3020 "#define A \\\n int i; \\\n int j;", 3021 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3022 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3023 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3024 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3025 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3026 } 3027 3028 TEST_F(FormatTest, DontCrashOnBlockComments) { 3029 EXPECT_EQ( 3030 "int xxxxxxxxx; /* " 3031 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3032 "zzzzzz\n" 3033 "0*/", 3034 format("int xxxxxxxxx; /* " 3035 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3036 "0*/")); 3037 } 3038 3039 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3040 verifyFormat("#define A \\\n" 3041 " int v( \\\n" 3042 " a); \\\n" 3043 " int i;", 3044 getLLVMStyleWithColumns(11)); 3045 } 3046 3047 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3048 EXPECT_EQ( 3049 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3050 " \\\n" 3051 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3052 "\n" 3053 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3054 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3055 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3056 "\\\n" 3057 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3058 " \n" 3059 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3060 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3061 } 3062 3063 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3064 EXPECT_EQ("int\n" 3065 "#define A\n" 3066 " a;", 3067 format("int\n#define A\na;")); 3068 verifyFormat("functionCallTo(\n" 3069 " someOtherFunction(\n" 3070 " withSomeParameters, whichInSequence,\n" 3071 " areLongerThanALine(andAnotherCall,\n" 3072 "#define A B\n" 3073 " withMoreParamters,\n" 3074 " whichStronglyInfluenceTheLayout),\n" 3075 " andMoreParameters),\n" 3076 " trailing);", 3077 getLLVMStyleWithColumns(69)); 3078 verifyFormat("Foo::Foo()\n" 3079 "#ifdef BAR\n" 3080 " : baz(0)\n" 3081 "#endif\n" 3082 "{\n" 3083 "}"); 3084 verifyFormat("void f() {\n" 3085 " if (true)\n" 3086 "#ifdef A\n" 3087 " f(42);\n" 3088 " x();\n" 3089 "#else\n" 3090 " g();\n" 3091 " x();\n" 3092 "#endif\n" 3093 "}"); 3094 verifyFormat("void f(param1, param2,\n" 3095 " param3,\n" 3096 "#ifdef A\n" 3097 " param4(param5,\n" 3098 "#ifdef A1\n" 3099 " param6,\n" 3100 "#ifdef A2\n" 3101 " param7),\n" 3102 "#else\n" 3103 " param8),\n" 3104 " param9,\n" 3105 "#endif\n" 3106 " param10,\n" 3107 "#endif\n" 3108 " param11)\n" 3109 "#else\n" 3110 " param12)\n" 3111 "#endif\n" 3112 "{\n" 3113 " x();\n" 3114 "}", 3115 getLLVMStyleWithColumns(28)); 3116 verifyFormat("#if 1\n" 3117 "int i;"); 3118 verifyFormat("#if 1\n" 3119 "#endif\n" 3120 "#if 1\n" 3121 "#else\n" 3122 "#endif\n"); 3123 verifyFormat("DEBUG({\n" 3124 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3126 "});\n" 3127 "#if a\n" 3128 "#else\n" 3129 "#endif"); 3130 3131 verifyIncompleteFormat("void f(\n" 3132 "#if A\n" 3133 " );\n" 3134 "#else\n" 3135 "#endif"); 3136 } 3137 3138 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3139 verifyFormat("#endif\n" 3140 "#if B"); 3141 } 3142 3143 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3144 FormatStyle SingleLine = getLLVMStyle(); 3145 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3146 verifyFormat("#if 0\n" 3147 "#elif 1\n" 3148 "#endif\n" 3149 "void foo() {\n" 3150 " if (test) foo2();\n" 3151 "}", 3152 SingleLine); 3153 } 3154 3155 TEST_F(FormatTest, LayoutBlockInsideParens) { 3156 verifyFormat("functionCall({ int i; });"); 3157 verifyFormat("functionCall({\n" 3158 " int i;\n" 3159 " int j;\n" 3160 "});"); 3161 verifyFormat("functionCall(\n" 3162 " {\n" 3163 " int i;\n" 3164 " int j;\n" 3165 " },\n" 3166 " aaaa, bbbb, cccc);"); 3167 verifyFormat("functionA(functionB({\n" 3168 " int i;\n" 3169 " int j;\n" 3170 " }),\n" 3171 " aaaa, bbbb, cccc);"); 3172 verifyFormat("functionCall(\n" 3173 " {\n" 3174 " int i;\n" 3175 " int j;\n" 3176 " },\n" 3177 " aaaa, bbbb, // comment\n" 3178 " cccc);"); 3179 verifyFormat("functionA(functionB({\n" 3180 " int i;\n" 3181 " int j;\n" 3182 " }),\n" 3183 " aaaa, bbbb, // comment\n" 3184 " cccc);"); 3185 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3186 verifyFormat("functionCall(aaaa, bbbb, {\n" 3187 " int i;\n" 3188 " int j;\n" 3189 "});"); 3190 verifyFormat( 3191 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3192 " {\n" 3193 " int i; // break\n" 3194 " },\n" 3195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3196 " ccccccccccccccccc));"); 3197 verifyFormat("DEBUG({\n" 3198 " if (a)\n" 3199 " f();\n" 3200 "});"); 3201 } 3202 3203 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3204 EXPECT_EQ("SOME_MACRO { int i; }\n" 3205 "int i;", 3206 format(" SOME_MACRO {int i;} int i;")); 3207 } 3208 3209 TEST_F(FormatTest, LayoutNestedBlocks) { 3210 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3211 " struct s {\n" 3212 " int i;\n" 3213 " };\n" 3214 " s kBitsToOs[] = {{10}};\n" 3215 " for (int i = 0; i < 10; ++i)\n" 3216 " return;\n" 3217 "}"); 3218 verifyFormat("call(parameter, {\n" 3219 " something();\n" 3220 " // Comment using all columns.\n" 3221 " somethingelse();\n" 3222 "});", 3223 getLLVMStyleWithColumns(40)); 3224 verifyFormat("DEBUG( //\n" 3225 " { f(); }, a);"); 3226 verifyFormat("DEBUG( //\n" 3227 " {\n" 3228 " f(); //\n" 3229 " },\n" 3230 " a);"); 3231 3232 EXPECT_EQ("call(parameter, {\n" 3233 " something();\n" 3234 " // Comment too\n" 3235 " // looooooooooong.\n" 3236 " somethingElse();\n" 3237 "});", 3238 format("call(parameter, {\n" 3239 " something();\n" 3240 " // Comment too looooooooooong.\n" 3241 " somethingElse();\n" 3242 "});", 3243 getLLVMStyleWithColumns(29))); 3244 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3245 EXPECT_EQ("DEBUG({ // comment\n" 3246 " int i;\n" 3247 "});", 3248 format("DEBUG({ // comment\n" 3249 "int i;\n" 3250 "});")); 3251 EXPECT_EQ("DEBUG({\n" 3252 " int i;\n" 3253 "\n" 3254 " // comment\n" 3255 " int j;\n" 3256 "});", 3257 format("DEBUG({\n" 3258 " int i;\n" 3259 "\n" 3260 " // comment\n" 3261 " int j;\n" 3262 "});")); 3263 3264 verifyFormat("DEBUG({\n" 3265 " if (a)\n" 3266 " return;\n" 3267 "});"); 3268 verifyGoogleFormat("DEBUG({\n" 3269 " if (a) return;\n" 3270 "});"); 3271 FormatStyle Style = getGoogleStyle(); 3272 Style.ColumnLimit = 45; 3273 verifyFormat("Debug(aaaaa,\n" 3274 " {\n" 3275 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3276 " },\n" 3277 " a);", 3278 Style); 3279 3280 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3281 3282 verifyNoCrash("^{v^{a}}"); 3283 } 3284 3285 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3286 EXPECT_EQ("#define MACRO() \\\n" 3287 " Debug(aaa, /* force line break */ \\\n" 3288 " { \\\n" 3289 " int i; \\\n" 3290 " int j; \\\n" 3291 " })", 3292 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3293 " { int i; int j; })", 3294 getGoogleStyle())); 3295 3296 EXPECT_EQ("#define A \\\n" 3297 " [] { \\\n" 3298 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3299 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3300 " }", 3301 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3302 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3303 getGoogleStyle())); 3304 } 3305 3306 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3307 EXPECT_EQ("{}", format("{}")); 3308 verifyFormat("enum E {};"); 3309 verifyFormat("enum E {}"); 3310 } 3311 3312 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3313 FormatStyle Style = getLLVMStyle(); 3314 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3315 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3316 verifyFormat("FOO_BEGIN\n" 3317 " FOO_ENTRY\n" 3318 "FOO_END", Style); 3319 verifyFormat("FOO_BEGIN\n" 3320 " NESTED_FOO_BEGIN\n" 3321 " NESTED_FOO_ENTRY\n" 3322 " NESTED_FOO_END\n" 3323 "FOO_END", Style); 3324 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3325 " int x;\n" 3326 " x = 1;\n" 3327 "FOO_END(Baz)", Style); 3328 } 3329 3330 //===----------------------------------------------------------------------===// 3331 // Line break tests. 3332 //===----------------------------------------------------------------------===// 3333 3334 TEST_F(FormatTest, PreventConfusingIndents) { 3335 verifyFormat( 3336 "void f() {\n" 3337 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3338 " parameter, parameter, parameter)),\n" 3339 " SecondLongCall(parameter));\n" 3340 "}"); 3341 verifyFormat( 3342 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3343 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3345 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3346 verifyFormat( 3347 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3348 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3349 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3350 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3351 verifyFormat( 3352 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3353 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3355 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3356 verifyFormat("int a = bbbb && ccc &&\n" 3357 " fffff(\n" 3358 "#define A Just forcing a new line\n" 3359 " ddd);"); 3360 } 3361 3362 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3363 verifyFormat( 3364 "bool aaaaaaa =\n" 3365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3366 " bbbbbbbb();"); 3367 verifyFormat( 3368 "bool aaaaaaa =\n" 3369 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3370 " bbbbbbbb();"); 3371 3372 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3374 " ccccccccc == ddddddddddd;"); 3375 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3377 " ccccccccc == ddddddddddd;"); 3378 verifyFormat( 3379 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3381 " ccccccccc == ddddddddddd;"); 3382 3383 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3384 " aaaaaa) &&\n" 3385 " bbbbbb && cccccc;"); 3386 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3387 " aaaaaa) >>\n" 3388 " bbbbbb;"); 3389 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3390 " SourceMgr.getSpellingColumnNumber(\n" 3391 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3392 " 1);"); 3393 3394 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3395 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3396 " cccccc) {\n}"); 3397 verifyFormat("b = a &&\n" 3398 " // Comment\n" 3399 " b.c && d;"); 3400 3401 // If the LHS of a comparison is not a binary expression itself, the 3402 // additional linebreak confuses many people. 3403 verifyFormat( 3404 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3406 "}"); 3407 verifyFormat( 3408 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3410 "}"); 3411 verifyFormat( 3412 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3414 "}"); 3415 // Even explicit parentheses stress the precedence enough to make the 3416 // additional break unnecessary. 3417 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3419 "}"); 3420 // This cases is borderline, but with the indentation it is still readable. 3421 verifyFormat( 3422 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3423 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3425 "}", 3426 getLLVMStyleWithColumns(75)); 3427 3428 // If the LHS is a binary expression, we should still use the additional break 3429 // as otherwise the formatting hides the operator precedence. 3430 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3432 " 5) {\n" 3433 "}"); 3434 3435 FormatStyle OnePerLine = getLLVMStyle(); 3436 OnePerLine.BinPackParameters = false; 3437 verifyFormat( 3438 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3441 OnePerLine); 3442 3443 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3444 " .aaa(aaaaaaaaaaaaa) *\n" 3445 " aaaaaaa +\n" 3446 " aaaaaaa;", 3447 getLLVMStyleWithColumns(40)); 3448 } 3449 3450 TEST_F(FormatTest, ExpressionIndentation) { 3451 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3455 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3456 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3459 " ccccccccccccccccccccccccccccccccccccccccc;"); 3460 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3463 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3464 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3467 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3468 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3471 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3472 verifyFormat("if () {\n" 3473 "} else if (aaaaa &&\n" 3474 " bbbbb > // break\n" 3475 " ccccc) {\n" 3476 "}"); 3477 3478 // Presence of a trailing comment used to change indentation of b. 3479 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3480 " b;\n" 3481 "return aaaaaaaaaaaaaaaaaaa +\n" 3482 " b; //", 3483 getLLVMStyleWithColumns(30)); 3484 } 3485 3486 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3487 // Not sure what the best system is here. Like this, the LHS can be found 3488 // immediately above an operator (everything with the same or a higher 3489 // indent). The RHS is aligned right of the operator and so compasses 3490 // everything until something with the same indent as the operator is found. 3491 // FIXME: Is this a good system? 3492 FormatStyle Style = getLLVMStyle(); 3493 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3494 verifyFormat( 3495 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3496 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3497 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3498 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3499 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3500 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3501 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3502 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3503 " > ccccccccccccccccccccccccccccccccccccccccc;", 3504 Style); 3505 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3506 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3507 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3508 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3509 Style); 3510 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3511 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3512 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3513 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3514 Style); 3515 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3516 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3517 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3518 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3519 Style); 3520 verifyFormat("if () {\n" 3521 "} else if (aaaaa\n" 3522 " && bbbbb // break\n" 3523 " > ccccc) {\n" 3524 "}", 3525 Style); 3526 verifyFormat("return (a)\n" 3527 " // comment\n" 3528 " + b;", 3529 Style); 3530 verifyFormat( 3531 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3532 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3533 " + cc;", 3534 Style); 3535 3536 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3537 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3538 Style); 3539 3540 // Forced by comments. 3541 verifyFormat( 3542 "unsigned ContentSize =\n" 3543 " sizeof(int16_t) // DWARF ARange version number\n" 3544 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3545 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3546 " + sizeof(int8_t); // Segment Size (in bytes)"); 3547 3548 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3549 " == boost::fusion::at_c<1>(iiii).second;", 3550 Style); 3551 3552 Style.ColumnLimit = 60; 3553 verifyFormat("zzzzzzzzzz\n" 3554 " = bbbbbbbbbbbbbbbbb\n" 3555 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3556 Style); 3557 } 3558 3559 TEST_F(FormatTest, NoOperandAlignment) { 3560 FormatStyle Style = getLLVMStyle(); 3561 Style.AlignOperands = false; 3562 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3563 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3564 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3565 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3566 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3567 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3568 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3569 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3570 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3571 " > ccccccccccccccccccccccccccccccccccccccccc;", 3572 Style); 3573 3574 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3575 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3576 " + cc;", 3577 Style); 3578 verifyFormat("int a = aa\n" 3579 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3580 " * cccccccccccccccccccccccccccccccccccc;", 3581 Style); 3582 3583 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3584 verifyFormat("return (a > b\n" 3585 " // comment1\n" 3586 " // comment2\n" 3587 " || c);", 3588 Style); 3589 } 3590 3591 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3592 FormatStyle Style = getLLVMStyle(); 3593 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3594 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3596 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3597 Style); 3598 } 3599 3600 TEST_F(FormatTest, ConstructorInitializers) { 3601 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3602 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3603 getLLVMStyleWithColumns(45)); 3604 verifyFormat("Constructor()\n" 3605 " : Inttializer(FitsOnTheLine) {}", 3606 getLLVMStyleWithColumns(44)); 3607 verifyFormat("Constructor()\n" 3608 " : Inttializer(FitsOnTheLine) {}", 3609 getLLVMStyleWithColumns(43)); 3610 3611 verifyFormat("template <typename T>\n" 3612 "Constructor() : Initializer(FitsOnTheLine) {}", 3613 getLLVMStyleWithColumns(45)); 3614 3615 verifyFormat( 3616 "SomeClass::Constructor()\n" 3617 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3618 3619 verifyFormat( 3620 "SomeClass::Constructor()\n" 3621 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3622 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3623 verifyFormat( 3624 "SomeClass::Constructor()\n" 3625 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3626 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3627 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3628 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3629 " : aaaaaaaaaa(aaaaaa) {}"); 3630 3631 verifyFormat("Constructor()\n" 3632 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3633 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3634 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3635 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3636 3637 verifyFormat("Constructor()\n" 3638 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3640 3641 verifyFormat("Constructor(int Parameter = 0)\n" 3642 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3643 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3644 verifyFormat("Constructor()\n" 3645 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3646 "}", 3647 getLLVMStyleWithColumns(60)); 3648 verifyFormat("Constructor()\n" 3649 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3650 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3651 3652 // Here a line could be saved by splitting the second initializer onto two 3653 // lines, but that is not desirable. 3654 verifyFormat("Constructor()\n" 3655 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3656 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3657 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3658 3659 FormatStyle OnePerLine = getLLVMStyle(); 3660 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3661 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3662 verifyFormat("SomeClass::Constructor()\n" 3663 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3664 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3665 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3666 OnePerLine); 3667 verifyFormat("SomeClass::Constructor()\n" 3668 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3669 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3670 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3671 OnePerLine); 3672 verifyFormat("MyClass::MyClass(int var)\n" 3673 " : some_var_(var), // 4 space indent\n" 3674 " some_other_var_(var + 1) { // lined up\n" 3675 "}", 3676 OnePerLine); 3677 verifyFormat("Constructor()\n" 3678 " : aaaaa(aaaaaa),\n" 3679 " aaaaa(aaaaaa),\n" 3680 " aaaaa(aaaaaa),\n" 3681 " aaaaa(aaaaaa),\n" 3682 " aaaaa(aaaaaa) {}", 3683 OnePerLine); 3684 verifyFormat("Constructor()\n" 3685 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3686 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3687 OnePerLine); 3688 OnePerLine.BinPackParameters = false; 3689 verifyFormat( 3690 "Constructor()\n" 3691 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3692 " aaaaaaaaaaa().aaa(),\n" 3693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3694 OnePerLine); 3695 OnePerLine.ColumnLimit = 60; 3696 verifyFormat("Constructor()\n" 3697 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3698 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3699 OnePerLine); 3700 3701 EXPECT_EQ("Constructor()\n" 3702 " : // Comment forcing unwanted break.\n" 3703 " aaaa(aaaa) {}", 3704 format("Constructor() :\n" 3705 " // Comment forcing unwanted break.\n" 3706 " aaaa(aaaa) {}")); 3707 } 3708 3709 TEST_F(FormatTest, MemoizationTests) { 3710 // This breaks if the memoization lookup does not take \c Indent and 3711 // \c LastSpace into account. 3712 verifyFormat( 3713 "extern CFRunLoopTimerRef\n" 3714 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3715 " CFTimeInterval interval, CFOptionFlags flags,\n" 3716 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3717 " CFRunLoopTimerContext *context) {}"); 3718 3719 // Deep nesting somewhat works around our memoization. 3720 verifyFormat( 3721 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3722 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3723 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3724 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3725 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3726 getLLVMStyleWithColumns(65)); 3727 verifyFormat( 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,\n" 3752 " aaaaa))))))))))));", 3753 getLLVMStyleWithColumns(65)); 3754 verifyFormat( 3755 "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" 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),\n" 3772 " a)", 3773 getLLVMStyleWithColumns(65)); 3774 3775 // This test takes VERY long when memoization is broken. 3776 FormatStyle OnePerLine = getLLVMStyle(); 3777 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3778 OnePerLine.BinPackParameters = false; 3779 std::string input = "Constructor()\n" 3780 " : aaaa(a,\n"; 3781 for (unsigned i = 0, e = 80; i != e; ++i) { 3782 input += " a,\n"; 3783 } 3784 input += " a) {}"; 3785 verifyFormat(input, OnePerLine); 3786 } 3787 3788 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3789 verifyFormat( 3790 "void f() {\n" 3791 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3792 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3793 " f();\n" 3794 "}"); 3795 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3796 " Intervals[i - 1].getRange().getLast()) {\n}"); 3797 } 3798 3799 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3800 // Principially, we break function declarations in a certain order: 3801 // 1) break amongst arguments. 3802 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3803 " Cccccccccccccc cccccccccccccc);"); 3804 verifyFormat("template <class TemplateIt>\n" 3805 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3806 " TemplateIt *stop) {}"); 3807 3808 // 2) break after return type. 3809 verifyFormat( 3810 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3811 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3812 getGoogleStyle()); 3813 3814 // 3) break after (. 3815 verifyFormat( 3816 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3817 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3818 getGoogleStyle()); 3819 3820 // 4) break before after nested name specifiers. 3821 verifyFormat( 3822 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3823 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3824 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3825 getGoogleStyle()); 3826 3827 // However, there are exceptions, if a sufficient amount of lines can be 3828 // saved. 3829 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3830 // more adjusting. 3831 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3832 " Cccccccccccccc cccccccccc,\n" 3833 " Cccccccccccccc cccccccccc,\n" 3834 " Cccccccccccccc cccccccccc,\n" 3835 " Cccccccccccccc cccccccccc);"); 3836 verifyFormat( 3837 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3838 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3839 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3840 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3841 getGoogleStyle()); 3842 verifyFormat( 3843 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3844 " Cccccccccccccc cccccccccc,\n" 3845 " Cccccccccccccc cccccccccc,\n" 3846 " Cccccccccccccc cccccccccc,\n" 3847 " Cccccccccccccc cccccccccc,\n" 3848 " Cccccccccccccc cccccccccc,\n" 3849 " Cccccccccccccc cccccccccc);"); 3850 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3851 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3852 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3853 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3854 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3855 3856 // Break after multi-line parameters. 3857 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3860 " bbbb bbbb);"); 3861 verifyFormat("void SomeLoooooooooooongFunction(\n" 3862 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3863 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3864 " int bbbbbbbbbbbbb);"); 3865 3866 // Treat overloaded operators like other functions. 3867 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3868 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3869 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3870 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3871 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3872 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3873 verifyGoogleFormat( 3874 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3875 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3876 verifyGoogleFormat( 3877 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3878 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3879 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3880 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3881 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3882 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3883 verifyGoogleFormat( 3884 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3885 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3886 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3887 verifyGoogleFormat( 3888 "template <typename T>\n" 3889 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3890 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3891 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3892 3893 FormatStyle Style = getLLVMStyle(); 3894 Style.PointerAlignment = FormatStyle::PAS_Left; 3895 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3896 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3897 Style); 3898 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3900 Style); 3901 } 3902 3903 TEST_F(FormatTest, TrailingReturnType) { 3904 verifyFormat("auto foo() -> int;\n"); 3905 verifyFormat("struct S {\n" 3906 " auto bar() const -> int;\n" 3907 "};"); 3908 verifyFormat("template <size_t Order, typename T>\n" 3909 "auto load_img(const std::string &filename)\n" 3910 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3911 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3912 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3913 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3914 verifyFormat("template <typename T>\n" 3915 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3916 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3917 3918 // Not trailing return types. 3919 verifyFormat("void f() { auto a = b->c(); }"); 3920 } 3921 3922 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3923 // Avoid breaking before trailing 'const' or other trailing annotations, if 3924 // they are not function-like. 3925 FormatStyle Style = getGoogleStyle(); 3926 Style.ColumnLimit = 47; 3927 verifyFormat("void someLongFunction(\n" 3928 " int someLoooooooooooooongParameter) const {\n}", 3929 getLLVMStyleWithColumns(47)); 3930 verifyFormat("LoooooongReturnType\n" 3931 "someLoooooooongFunction() const {}", 3932 getLLVMStyleWithColumns(47)); 3933 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3934 " const {}", 3935 Style); 3936 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3937 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3938 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3939 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3940 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3941 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3942 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3943 " aaaaaaaaaaa aaaaa) const override;"); 3944 verifyGoogleFormat( 3945 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3946 " const override;"); 3947 3948 // Even if the first parameter has to be wrapped. 3949 verifyFormat("void someLongFunction(\n" 3950 " int someLongParameter) const {}", 3951 getLLVMStyleWithColumns(46)); 3952 verifyFormat("void someLongFunction(\n" 3953 " int someLongParameter) const {}", 3954 Style); 3955 verifyFormat("void someLongFunction(\n" 3956 " int someLongParameter) override {}", 3957 Style); 3958 verifyFormat("void someLongFunction(\n" 3959 " int someLongParameter) OVERRIDE {}", 3960 Style); 3961 verifyFormat("void someLongFunction(\n" 3962 " int someLongParameter) final {}", 3963 Style); 3964 verifyFormat("void someLongFunction(\n" 3965 " int someLongParameter) FINAL {}", 3966 Style); 3967 verifyFormat("void someLongFunction(\n" 3968 " int parameter) const override {}", 3969 Style); 3970 3971 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3972 verifyFormat("void someLongFunction(\n" 3973 " int someLongParameter) const\n" 3974 "{\n" 3975 "}", 3976 Style); 3977 3978 // Unless these are unknown annotations. 3979 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3980 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3981 " LONG_AND_UGLY_ANNOTATION;"); 3982 3983 // Breaking before function-like trailing annotations is fine to keep them 3984 // close to their arguments. 3985 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3986 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3987 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3988 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3989 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3990 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3991 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3992 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3993 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3994 3995 verifyFormat( 3996 "void aaaaaaaaaaaaaaaaaa()\n" 3997 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3998 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3999 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4000 " __attribute__((unused));"); 4001 verifyGoogleFormat( 4002 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4003 " GUARDED_BY(aaaaaaaaaaaa);"); 4004 verifyGoogleFormat( 4005 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4006 " GUARDED_BY(aaaaaaaaaaaa);"); 4007 verifyGoogleFormat( 4008 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4009 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4010 verifyGoogleFormat( 4011 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4012 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4013 } 4014 4015 TEST_F(FormatTest, FunctionAnnotations) { 4016 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4017 "int OldFunction(const string ¶meter) {}"); 4018 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4019 "string OldFunction(const string ¶meter) {}"); 4020 verifyFormat("template <typename T>\n" 4021 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4022 "string OldFunction(const string ¶meter) {}"); 4023 4024 // Not function annotations. 4025 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4026 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4027 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4028 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4029 verifyFormat("MACRO(abc).function() // wrap\n" 4030 " << abc;"); 4031 verifyFormat("MACRO(abc)->function() // wrap\n" 4032 " << abc;"); 4033 verifyFormat("MACRO(abc)::function() // wrap\n" 4034 " << abc;"); 4035 } 4036 4037 TEST_F(FormatTest, BreaksDesireably) { 4038 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4039 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4040 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4041 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4043 "}"); 4044 4045 verifyFormat( 4046 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4048 4049 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4052 4053 verifyFormat( 4054 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4055 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4057 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4059 4060 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4061 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4062 4063 verifyFormat( 4064 "void f() {\n" 4065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4067 "}"); 4068 verifyFormat( 4069 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4071 verifyFormat( 4072 "aaaaaa(aaa,\n" 4073 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4075 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4078 4079 // Indent consistently independent of call expression and unary operator. 4080 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4081 " dddddddddddddddddddddddddddddd));"); 4082 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4083 " dddddddddddddddddddddddddddddd));"); 4084 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4085 " dddddddddddddddddddddddddddddd));"); 4086 4087 // This test case breaks on an incorrect memoization, i.e. an optimization not 4088 // taking into account the StopAt value. 4089 verifyFormat( 4090 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4091 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4092 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4093 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4094 4095 verifyFormat("{\n {\n {\n" 4096 " Annotation.SpaceRequiredBefore =\n" 4097 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4098 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4099 " }\n }\n}"); 4100 4101 // Break on an outer level if there was a break on an inner level. 4102 EXPECT_EQ("f(g(h(a, // comment\n" 4103 " b, c),\n" 4104 " d, e),\n" 4105 " x, y);", 4106 format("f(g(h(a, // comment\n" 4107 " b, c), d, e), x, y);")); 4108 4109 // Prefer breaking similar line breaks. 4110 verifyFormat( 4111 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4112 " NSTrackingMouseEnteredAndExited |\n" 4113 " NSTrackingActiveAlways;"); 4114 } 4115 4116 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4117 FormatStyle NoBinPacking = getGoogleStyle(); 4118 NoBinPacking.BinPackParameters = false; 4119 NoBinPacking.BinPackArguments = true; 4120 verifyFormat("void f() {\n" 4121 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4123 "}", 4124 NoBinPacking); 4125 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4126 " int aaaaaaaaaaaaaaaaaaaa,\n" 4127 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4128 NoBinPacking); 4129 4130 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4131 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4132 " vector<int> bbbbbbbbbbbbbbb);", 4133 NoBinPacking); 4134 // FIXME: This behavior difference is probably not wanted. However, currently 4135 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4136 // template arguments from BreakBeforeParameter being set because of the 4137 // one-per-line formatting. 4138 verifyFormat( 4139 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4140 " aaaaaaaaaa> aaaaaaaaaa);", 4141 NoBinPacking); 4142 verifyFormat( 4143 "void fffffffffff(\n" 4144 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4145 " aaaaaaaaaa);"); 4146 } 4147 4148 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4149 FormatStyle NoBinPacking = getGoogleStyle(); 4150 NoBinPacking.BinPackParameters = false; 4151 NoBinPacking.BinPackArguments = false; 4152 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4153 " aaaaaaaaaaaaaaaaaaaa,\n" 4154 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4155 NoBinPacking); 4156 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4157 " aaaaaaaaaaaaa,\n" 4158 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4159 NoBinPacking); 4160 verifyFormat( 4161 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4162 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4164 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4166 NoBinPacking); 4167 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4168 " .aaaaaaaaaaaaaaaaaa();", 4169 NoBinPacking); 4170 verifyFormat("void f() {\n" 4171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4172 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4173 "}", 4174 NoBinPacking); 4175 4176 verifyFormat( 4177 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4178 " aaaaaaaaaaaa,\n" 4179 " aaaaaaaaaaaa);", 4180 NoBinPacking); 4181 verifyFormat( 4182 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4183 " ddddddddddddddddddddddddddddd),\n" 4184 " test);", 4185 NoBinPacking); 4186 4187 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4188 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4189 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4190 " aaaaaaaaaaaaaaaaaa;", 4191 NoBinPacking); 4192 verifyFormat("a(\"a\"\n" 4193 " \"a\",\n" 4194 " a);"); 4195 4196 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4197 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4198 " aaaaaaaaa,\n" 4199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4200 NoBinPacking); 4201 verifyFormat( 4202 "void f() {\n" 4203 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4204 " .aaaaaaa();\n" 4205 "}", 4206 NoBinPacking); 4207 verifyFormat( 4208 "template <class SomeType, class SomeOtherType>\n" 4209 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4210 NoBinPacking); 4211 } 4212 4213 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4214 FormatStyle Style = getLLVMStyleWithColumns(15); 4215 Style.ExperimentalAutoDetectBinPacking = true; 4216 EXPECT_EQ("aaa(aaaa,\n" 4217 " aaaa,\n" 4218 " aaaa);\n" 4219 "aaa(aaaa,\n" 4220 " aaaa,\n" 4221 " aaaa);", 4222 format("aaa(aaaa,\n" // one-per-line 4223 " aaaa,\n" 4224 " aaaa );\n" 4225 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4226 Style)); 4227 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4228 " aaaa);\n" 4229 "aaa(aaaa, aaaa,\n" 4230 " aaaa);", 4231 format("aaa(aaaa, aaaa,\n" // bin-packed 4232 " aaaa );\n" 4233 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4234 Style)); 4235 } 4236 4237 TEST_F(FormatTest, FormatsBuilderPattern) { 4238 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4239 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4240 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4241 " .StartsWith(\".init\", ORDER_INIT)\n" 4242 " .StartsWith(\".fini\", ORDER_FINI)\n" 4243 " .StartsWith(\".hash\", ORDER_HASH)\n" 4244 " .Default(ORDER_TEXT);\n"); 4245 4246 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4247 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4248 verifyFormat( 4249 "aaaaaaa->aaaaaaa\n" 4250 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4252 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4253 verifyFormat( 4254 "aaaaaaa->aaaaaaa\n" 4255 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4256 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4257 verifyFormat( 4258 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4259 " aaaaaaaaaaaaaa);"); 4260 verifyFormat( 4261 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4262 " aaaaaa->aaaaaaaaaaaa()\n" 4263 " ->aaaaaaaaaaaaaaaa(\n" 4264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4265 " ->aaaaaaaaaaaaaaaaa();"); 4266 verifyGoogleFormat( 4267 "void f() {\n" 4268 " someo->Add((new util::filetools::Handler(dir))\n" 4269 " ->OnEvent1(NewPermanentCallback(\n" 4270 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4271 " ->OnEvent2(NewPermanentCallback(\n" 4272 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4273 " ->OnEvent3(NewPermanentCallback(\n" 4274 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4275 " ->OnEvent5(NewPermanentCallback(\n" 4276 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4277 " ->OnEvent6(NewPermanentCallback(\n" 4278 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4279 "}"); 4280 4281 verifyFormat( 4282 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4283 verifyFormat("aaaaaaaaaaaaaaa()\n" 4284 " .aaaaaaaaaaaaaaa()\n" 4285 " .aaaaaaaaaaaaaaa()\n" 4286 " .aaaaaaaaaaaaaaa()\n" 4287 " .aaaaaaaaaaaaaaa();"); 4288 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4289 " .aaaaaaaaaaaaaaa()\n" 4290 " .aaaaaaaaaaaaaaa()\n" 4291 " .aaaaaaaaaaaaaaa();"); 4292 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4293 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4294 " .aaaaaaaaaaaaaaa();"); 4295 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4296 " ->aaaaaaaaaaaaaae(0)\n" 4297 " ->aaaaaaaaaaaaaaa();"); 4298 4299 // Don't linewrap after very short segments. 4300 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4301 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4303 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4304 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4306 verifyFormat("aaa()\n" 4307 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4309 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4310 4311 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4312 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4313 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4314 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4315 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4317 4318 // Prefer not to break after empty parentheses. 4319 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4320 " First->LastNewlineOffset);"); 4321 4322 // Prefer not to create "hanging" indents. 4323 verifyFormat( 4324 "return !soooooooooooooome_map\n" 4325 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4326 " .second;"); 4327 verifyFormat( 4328 "return aaaaaaaaaaaaaaaa\n" 4329 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4330 " .aaaa(aaaaaaaaaaaaaa);"); 4331 // No hanging indent here. 4332 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4334 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4336 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4337 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4338 getLLVMStyleWithColumns(60)); 4339 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4340 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4341 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4342 getLLVMStyleWithColumns(59)); 4343 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4345 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4346 } 4347 4348 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4349 verifyFormat( 4350 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4351 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4352 verifyFormat( 4353 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4354 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4355 4356 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4357 " ccccccccccccccccccccccccc) {\n}"); 4358 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4359 " ccccccccccccccccccccccccc) {\n}"); 4360 4361 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4362 " ccccccccccccccccccccccccc) {\n}"); 4363 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4364 " ccccccccccccccccccccccccc) {\n}"); 4365 4366 verifyFormat( 4367 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4368 " ccccccccccccccccccccccccc) {\n}"); 4369 verifyFormat( 4370 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4371 " ccccccccccccccccccccccccc) {\n}"); 4372 4373 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4374 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4375 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4376 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4377 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4378 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4379 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4380 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4381 4382 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4383 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4384 " aaaaaaaaaaaaaaa != aa) {\n}"); 4385 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4387 " aaaaaaaaaaaaaaa != aa) {\n}"); 4388 } 4389 4390 TEST_F(FormatTest, BreaksAfterAssignments) { 4391 verifyFormat( 4392 "unsigned Cost =\n" 4393 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4394 " SI->getPointerAddressSpaceee());\n"); 4395 verifyFormat( 4396 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4397 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4398 4399 verifyFormat( 4400 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4401 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4402 verifyFormat("unsigned OriginalStartColumn =\n" 4403 " SourceMgr.getSpellingColumnNumber(\n" 4404 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4405 " 1;"); 4406 } 4407 4408 TEST_F(FormatTest, AlignsAfterAssignments) { 4409 verifyFormat( 4410 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4411 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4412 verifyFormat( 4413 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4414 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4415 verifyFormat( 4416 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4417 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4418 verifyFormat( 4419 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4420 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4421 verifyFormat( 4422 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4423 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4424 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4425 } 4426 4427 TEST_F(FormatTest, AlignsAfterReturn) { 4428 verifyFormat( 4429 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4430 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4431 verifyFormat( 4432 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4433 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4434 verifyFormat( 4435 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4436 " aaaaaaaaaaaaaaaaaaaaaa();"); 4437 verifyFormat( 4438 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4439 " aaaaaaaaaaaaaaaaaaaaaa());"); 4440 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4442 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4445 verifyFormat("return\n" 4446 " // true if code is one of a or b.\n" 4447 " code == a || code == b;"); 4448 } 4449 4450 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4451 verifyFormat( 4452 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4453 " aaaaaaaaa aaaaaaa) {}"); 4454 verifyFormat( 4455 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4456 " aaaaaaaaaaa aaaaaaaaa);"); 4457 verifyFormat( 4458 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4459 " aaaaaaaaaaaaaaaaaaaaa));"); 4460 FormatStyle Style = getLLVMStyle(); 4461 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4462 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4463 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4464 Style); 4465 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4466 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4467 Style); 4468 verifyFormat("SomeLongVariableName->someFunction(\n" 4469 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4470 Style); 4471 verifyFormat( 4472 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4473 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4474 Style); 4475 verifyFormat( 4476 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4477 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4478 Style); 4479 verifyFormat( 4480 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4481 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4482 Style); 4483 4484 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4485 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4486 " b));", 4487 Style); 4488 4489 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4490 Style.BinPackArguments = false; 4491 Style.BinPackParameters = false; 4492 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4493 " aaaaaaaaaaa aaaaaaaa,\n" 4494 " aaaaaaaaa aaaaaaa,\n" 4495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4496 Style); 4497 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4498 " aaaaaaaaaaa aaaaaaaaa,\n" 4499 " aaaaaaaaaaa aaaaaaaaa,\n" 4500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4501 Style); 4502 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4503 " aaaaaaaaaaaaaaa,\n" 4504 " aaaaaaaaaaaaaaaaaaaaa,\n" 4505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4506 Style); 4507 verifyFormat( 4508 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4509 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4510 Style); 4511 verifyFormat( 4512 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4513 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4514 Style); 4515 verifyFormat( 4516 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4517 " aaaaaaaaaaaaaaaaaaaaa(\n" 4518 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4519 " aaaaaaaaaaaaaaaa);", 4520 Style); 4521 verifyFormat( 4522 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4523 " aaaaaaaaaaaaaaaaaaaaa(\n" 4524 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4525 " aaaaaaaaaaaaaaaa);", 4526 Style); 4527 } 4528 4529 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4530 FormatStyle Style = getLLVMStyleWithColumns(40); 4531 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4532 " bbbbbbbbbbbbbbbbbbbbbb);", 4533 Style); 4534 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4535 Style.AlignOperands = false; 4536 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4537 " bbbbbbbbbbbbbbbbbbbbbb);", 4538 Style); 4539 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4540 Style.AlignOperands = true; 4541 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4542 " bbbbbbbbbbbbbbbbbbbbbb);", 4543 Style); 4544 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4545 Style.AlignOperands = false; 4546 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4547 " bbbbbbbbbbbbbbbbbbbbbb);", 4548 Style); 4549 } 4550 4551 TEST_F(FormatTest, BreaksConditionalExpressions) { 4552 verifyFormat( 4553 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4554 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4556 verifyFormat( 4557 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4558 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4559 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4560 verifyFormat( 4561 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4562 " : aaaaaaaaaaaaa);"); 4563 verifyFormat( 4564 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4565 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4566 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4567 " aaaaaaaaaaaaa);"); 4568 verifyFormat( 4569 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4570 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4571 " aaaaaaaaaaaaa);"); 4572 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4573 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4575 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4577 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4579 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4581 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4583 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4584 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4588 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4589 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4590 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4591 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4592 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4594 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4595 " : aaaaaaaaaaaaaaaa;"); 4596 verifyFormat( 4597 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4598 " ? aaaaaaaaaaaaaaa\n" 4599 " : aaaaaaaaaaaaaaa;"); 4600 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4601 " aaaaaaaaa\n" 4602 " ? b\n" 4603 " : c);"); 4604 verifyFormat("return aaaa == bbbb\n" 4605 " // comment\n" 4606 " ? aaaa\n" 4607 " : bbbb;"); 4608 verifyFormat("unsigned Indent =\n" 4609 " format(TheLine.First,\n" 4610 " IndentForLevel[TheLine.Level] >= 0\n" 4611 " ? IndentForLevel[TheLine.Level]\n" 4612 " : TheLine * 2,\n" 4613 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4614 getLLVMStyleWithColumns(60)); 4615 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4616 " ? aaaaaaaaaaaaaaa\n" 4617 " : bbbbbbbbbbbbbbb //\n" 4618 " ? ccccccccccccccc\n" 4619 " : ddddddddddddddd;"); 4620 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4621 " ? aaaaaaaaaaaaaaa\n" 4622 " : (bbbbbbbbbbbbbbb //\n" 4623 " ? ccccccccccccccc\n" 4624 " : ddddddddddddddd);"); 4625 verifyFormat( 4626 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4627 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4628 " aaaaaaaaaaaaaaaaaaaaa +\n" 4629 " aaaaaaaaaaaaaaaaaaaaa\n" 4630 " : aaaaaaaaaa;"); 4631 verifyFormat( 4632 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4633 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4634 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4635 4636 FormatStyle NoBinPacking = getLLVMStyle(); 4637 NoBinPacking.BinPackArguments = false; 4638 verifyFormat( 4639 "void f() {\n" 4640 " g(aaa,\n" 4641 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4643 " ? aaaaaaaaaaaaaaa\n" 4644 " : aaaaaaaaaaaaaaa);\n" 4645 "}", 4646 NoBinPacking); 4647 verifyFormat( 4648 "void f() {\n" 4649 " g(aaa,\n" 4650 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4652 " ?: aaaaaaaaaaaaaaa);\n" 4653 "}", 4654 NoBinPacking); 4655 4656 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4657 " // comment.\n" 4658 " ccccccccccccccccccccccccccccccccccccccc\n" 4659 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4660 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4661 4662 // Assignments in conditional expressions. Apparently not uncommon :-(. 4663 verifyFormat("return a != b\n" 4664 " // comment\n" 4665 " ? a = b\n" 4666 " : a = b;"); 4667 verifyFormat("return a != b\n" 4668 " // comment\n" 4669 " ? a = a != b\n" 4670 " // comment\n" 4671 " ? a = b\n" 4672 " : a\n" 4673 " : a;\n"); 4674 verifyFormat("return a != b\n" 4675 " // comment\n" 4676 " ? a\n" 4677 " : a = a != b\n" 4678 " // comment\n" 4679 " ? a = b\n" 4680 " : a;"); 4681 } 4682 4683 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4684 FormatStyle Style = getLLVMStyle(); 4685 Style.BreakBeforeTernaryOperators = false; 4686 Style.ColumnLimit = 70; 4687 verifyFormat( 4688 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4691 Style); 4692 verifyFormat( 4693 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4694 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4696 Style); 4697 verifyFormat( 4698 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4699 " aaaaaaaaaaaaa);", 4700 Style); 4701 verifyFormat( 4702 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4703 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4705 " aaaaaaaaaaaaa);", 4706 Style); 4707 verifyFormat( 4708 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4709 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4710 " aaaaaaaaaaaaa);", 4711 Style); 4712 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4716 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4717 Style); 4718 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4724 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4725 Style); 4726 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4730 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4731 Style); 4732 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4733 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4734 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4735 Style); 4736 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4740 Style); 4741 verifyFormat( 4742 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4743 " aaaaaaaaaaaaaaa :\n" 4744 " aaaaaaaaaaaaaaa;", 4745 Style); 4746 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4747 " aaaaaaaaa ?\n" 4748 " b :\n" 4749 " c);", 4750 Style); 4751 verifyFormat("unsigned Indent =\n" 4752 " format(TheLine.First,\n" 4753 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4754 " IndentForLevel[TheLine.Level] :\n" 4755 " TheLine * 2,\n" 4756 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4757 Style); 4758 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4759 " aaaaaaaaaaaaaaa :\n" 4760 " bbbbbbbbbbbbbbb ? //\n" 4761 " ccccccccccccccc :\n" 4762 " ddddddddddddddd;", 4763 Style); 4764 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4765 " aaaaaaaaaaaaaaa :\n" 4766 " (bbbbbbbbbbbbbbb ? //\n" 4767 " ccccccccccccccc :\n" 4768 " ddddddddddddddd);", 4769 Style); 4770 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4771 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4772 " ccccccccccccccccccccccccccc;", 4773 Style); 4774 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4775 " aaaaa :\n" 4776 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4777 Style); 4778 } 4779 4780 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4781 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4782 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4783 verifyFormat("bool a = true, b = false;"); 4784 4785 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4787 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4788 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4789 verifyFormat( 4790 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4791 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4792 " d = e && f;"); 4793 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4794 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4795 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4796 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4797 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4798 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4799 4800 FormatStyle Style = getGoogleStyle(); 4801 Style.PointerAlignment = FormatStyle::PAS_Left; 4802 Style.DerivePointerAlignment = false; 4803 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4804 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4805 " *b = bbbbbbbbbbbbbbbbbbb;", 4806 Style); 4807 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4808 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4809 Style); 4810 verifyFormat("vector<int*> a, b;", Style); 4811 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4812 } 4813 4814 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4815 verifyFormat("arr[foo ? bar : baz];"); 4816 verifyFormat("f()[foo ? bar : baz];"); 4817 verifyFormat("(a + b)[foo ? bar : baz];"); 4818 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4819 } 4820 4821 TEST_F(FormatTest, AlignsStringLiterals) { 4822 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4823 " \"short literal\");"); 4824 verifyFormat( 4825 "looooooooooooooooooooooooongFunction(\n" 4826 " \"short literal\"\n" 4827 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4828 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4829 " \" string literals\",\n" 4830 " and, other, parameters);"); 4831 EXPECT_EQ("fun +\n" 4832 " \"1243\" /* comment */\n" 4833 " \"5678\";", 4834 format("fun + \"1243\" /* comment */\n" 4835 " \"5678\";", 4836 getLLVMStyleWithColumns(28))); 4837 EXPECT_EQ( 4838 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4839 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4840 " \"aaaaaaaaaaaaaaaa\";", 4841 format("aaaaaa =" 4842 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4843 "aaaaaaaaaaaaaaaaaaaaa\" " 4844 "\"aaaaaaaaaaaaaaaa\";")); 4845 verifyFormat("a = a +\n" 4846 " \"a\"\n" 4847 " \"a\"\n" 4848 " \"a\";"); 4849 verifyFormat("f(\"a\",\n" 4850 " \"b\"\n" 4851 " \"c\");"); 4852 4853 verifyFormat( 4854 "#define LL_FORMAT \"ll\"\n" 4855 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4856 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4857 4858 verifyFormat("#define A(X) \\\n" 4859 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4860 " \"ccccc\"", 4861 getLLVMStyleWithColumns(23)); 4862 verifyFormat("#define A \"def\"\n" 4863 "f(\"abc\" A \"ghi\"\n" 4864 " \"jkl\");"); 4865 4866 verifyFormat("f(L\"a\"\n" 4867 " L\"b\");"); 4868 verifyFormat("#define A(X) \\\n" 4869 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4870 " L\"ccccc\"", 4871 getLLVMStyleWithColumns(25)); 4872 4873 verifyFormat("f(@\"a\"\n" 4874 " @\"b\");"); 4875 verifyFormat("NSString s = @\"a\"\n" 4876 " @\"b\"\n" 4877 " @\"c\";"); 4878 verifyFormat("NSString s = @\"a\"\n" 4879 " \"b\"\n" 4880 " \"c\";"); 4881 } 4882 4883 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4884 FormatStyle Style = getLLVMStyle(); 4885 // No declarations or definitions should be moved to own line. 4886 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4887 verifyFormat("class A {\n" 4888 " int f() { return 1; }\n" 4889 " int g();\n" 4890 "};\n" 4891 "int f() { return 1; }\n" 4892 "int g();\n", 4893 Style); 4894 4895 // All declarations and definitions should have the return type moved to its 4896 // own 4897 // line. 4898 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4899 verifyFormat("class E {\n" 4900 " int\n" 4901 " f() {\n" 4902 " return 1;\n" 4903 " }\n" 4904 " int\n" 4905 " g();\n" 4906 "};\n" 4907 "int\n" 4908 "f() {\n" 4909 " return 1;\n" 4910 "}\n" 4911 "int\n" 4912 "g();\n", 4913 Style); 4914 4915 // Top-level definitions, and no kinds of declarations should have the 4916 // return type moved to its own line. 4917 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4918 verifyFormat("class B {\n" 4919 " int f() { return 1; }\n" 4920 " int g();\n" 4921 "};\n" 4922 "int\n" 4923 "f() {\n" 4924 " return 1;\n" 4925 "}\n" 4926 "int g();\n", 4927 Style); 4928 4929 // Top-level definitions and declarations should have the return type moved 4930 // to its own line. 4931 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4932 verifyFormat("class C {\n" 4933 " int f() { return 1; }\n" 4934 " int g();\n" 4935 "};\n" 4936 "int\n" 4937 "f() {\n" 4938 " return 1;\n" 4939 "}\n" 4940 "int\n" 4941 "g();\n", 4942 Style); 4943 4944 // All definitions should have the return type moved to its own line, but no 4945 // kinds of declarations. 4946 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4947 verifyFormat("class D {\n" 4948 " int\n" 4949 " f() {\n" 4950 " return 1;\n" 4951 " }\n" 4952 " int g();\n" 4953 "};\n" 4954 "int\n" 4955 "f() {\n" 4956 " return 1;\n" 4957 "}\n" 4958 "int g();\n", 4959 Style); 4960 verifyFormat("const char *\n" 4961 "f(void) {\n" // Break here. 4962 " return \"\";\n" 4963 "}\n" 4964 "const char *bar(void);\n", // No break here. 4965 Style); 4966 verifyFormat("template <class T>\n" 4967 "T *\n" 4968 "f(T &c) {\n" // Break here. 4969 " return NULL;\n" 4970 "}\n" 4971 "template <class T> T *f(T &c);\n", // No break here. 4972 Style); 4973 verifyFormat("class C {\n" 4974 " int\n" 4975 " operator+() {\n" 4976 " return 1;\n" 4977 " }\n" 4978 " int\n" 4979 " operator()() {\n" 4980 " return 1;\n" 4981 " }\n" 4982 "};\n", 4983 Style); 4984 verifyFormat("void\n" 4985 "A::operator()() {}\n" 4986 "void\n" 4987 "A::operator>>() {}\n" 4988 "void\n" 4989 "A::operator+() {}\n", 4990 Style); 4991 verifyFormat("void *operator new(std::size_t s);", // No break here. 4992 Style); 4993 verifyFormat("void *\n" 4994 "operator new(std::size_t s) {}", 4995 Style); 4996 verifyFormat("void *\n" 4997 "operator delete[](void *ptr) {}", 4998 Style); 4999 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5000 verifyFormat("const char *\n" 5001 "f(void)\n" // Break here. 5002 "{\n" 5003 " return \"\";\n" 5004 "}\n" 5005 "const char *bar(void);\n", // No break here. 5006 Style); 5007 verifyFormat("template <class T>\n" 5008 "T *\n" // Problem here: no line break 5009 "f(T &c)\n" // Break here. 5010 "{\n" 5011 " return NULL;\n" 5012 "}\n" 5013 "template <class T> T *f(T &c);\n", // No break here. 5014 Style); 5015 } 5016 5017 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5018 FormatStyle NoBreak = getLLVMStyle(); 5019 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5020 FormatStyle Break = getLLVMStyle(); 5021 Break.AlwaysBreakBeforeMultilineStrings = true; 5022 verifyFormat("aaaa = \"bbbb\"\n" 5023 " \"cccc\";", 5024 NoBreak); 5025 verifyFormat("aaaa =\n" 5026 " \"bbbb\"\n" 5027 " \"cccc\";", 5028 Break); 5029 verifyFormat("aaaa(\"bbbb\"\n" 5030 " \"cccc\");", 5031 NoBreak); 5032 verifyFormat("aaaa(\n" 5033 " \"bbbb\"\n" 5034 " \"cccc\");", 5035 Break); 5036 verifyFormat("aaaa(qqq,\n" 5037 " \"bbbb\"\n" 5038 " \"cccc\");", 5039 NoBreak); 5040 verifyFormat("aaaa(qqq,\n" 5041 " \"bbbb\"\n" 5042 " \"cccc\");", 5043 Break); 5044 verifyFormat("aaaa(qqq,\n" 5045 " L\"bbbb\"\n" 5046 " L\"cccc\");", 5047 Break); 5048 verifyFormat("aaaaa(aaaaaa,\n" 5049 " aaaaaaa(\"aaaa\"\n" 5050 " \"bbbb\"));", 5051 Break); 5052 verifyFormat("string s = someFunction(\n" 5053 " \"abc\"\n" 5054 " \"abc\");", 5055 Break); 5056 5057 // As we break before unary operators, breaking right after them is bad. 5058 verifyFormat("string foo = abc ? \"x\"\n" 5059 " \"blah blah blah blah blah blah\"\n" 5060 " : \"y\";", 5061 Break); 5062 5063 // Don't break if there is no column gain. 5064 verifyFormat("f(\"aaaa\"\n" 5065 " \"bbbb\");", 5066 Break); 5067 5068 // Treat literals with escaped newlines like multi-line string literals. 5069 EXPECT_EQ("x = \"a\\\n" 5070 "b\\\n" 5071 "c\";", 5072 format("x = \"a\\\n" 5073 "b\\\n" 5074 "c\";", 5075 NoBreak)); 5076 EXPECT_EQ("xxxx =\n" 5077 " \"a\\\n" 5078 "b\\\n" 5079 "c\";", 5080 format("xxxx = \"a\\\n" 5081 "b\\\n" 5082 "c\";", 5083 Break)); 5084 5085 // Exempt ObjC strings for now. 5086 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5087 " @\"bbbb\";", 5088 format("NSString *const kString = @\"aaaa\"\n" 5089 "@\"bbbb\";", 5090 Break)); 5091 5092 Break.ColumnLimit = 0; 5093 verifyFormat("const char *hello = \"hello llvm\";", Break); 5094 } 5095 5096 TEST_F(FormatTest, AlignsPipes) { 5097 verifyFormat( 5098 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5099 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5100 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5101 verifyFormat( 5102 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5103 " << aaaaaaaaaaaaaaaaaaaa;"); 5104 verifyFormat( 5105 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5106 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5107 verifyFormat( 5108 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5109 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5110 verifyFormat( 5111 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5112 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5113 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5114 verifyFormat( 5115 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5116 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5117 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5118 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5121 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5122 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5123 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5124 verifyFormat( 5125 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5127 5128 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5129 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5130 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5132 " aaaaaaaaaaaaaaaaaaaaa)\n" 5133 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5134 verifyFormat("LOG_IF(aaa == //\n" 5135 " bbb)\n" 5136 " << a << b;"); 5137 5138 // But sometimes, breaking before the first "<<" is desirable. 5139 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5140 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5141 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5142 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5143 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5144 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5145 " << BEF << IsTemplate << Description << E->getType();"); 5146 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5147 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5149 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5150 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5152 " << aaa;"); 5153 5154 verifyFormat( 5155 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5156 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5157 5158 // Incomplete string literal. 5159 EXPECT_EQ("llvm::errs() << \"\n" 5160 " << a;", 5161 format("llvm::errs() << \"\n<<a;")); 5162 5163 verifyFormat("void f() {\n" 5164 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5165 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5166 "}"); 5167 5168 // Handle 'endl'. 5169 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5170 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5171 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5172 5173 // Handle '\n'. 5174 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5175 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5176 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5177 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5178 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5179 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5180 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5181 } 5182 5183 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5184 verifyFormat("return out << \"somepacket = {\\n\"\n" 5185 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5186 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5187 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5188 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5189 " << \"}\";"); 5190 5191 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5192 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5193 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5194 verifyFormat( 5195 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5196 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5197 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5198 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5199 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5200 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5201 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5202 verifyFormat( 5203 "void f() {\n" 5204 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5205 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5206 "}"); 5207 5208 // Breaking before the first "<<" is generally not desirable. 5209 verifyFormat( 5210 "llvm::errs()\n" 5211 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5212 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5213 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5214 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5215 getLLVMStyleWithColumns(70)); 5216 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5217 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5218 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5219 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5220 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5221 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5222 getLLVMStyleWithColumns(70)); 5223 5224 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5225 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5226 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5227 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5228 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5229 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5230 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5231 " (aaaa + aaaa);", 5232 getLLVMStyleWithColumns(40)); 5233 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5234 " (aaaaaaa + aaaaa));", 5235 getLLVMStyleWithColumns(40)); 5236 verifyFormat( 5237 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5238 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5239 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5240 } 5241 5242 TEST_F(FormatTest, UnderstandsEquals) { 5243 verifyFormat( 5244 "aaaaaaaaaaaaaaaaa =\n" 5245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5246 verifyFormat( 5247 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5249 verifyFormat( 5250 "if (a) {\n" 5251 " f();\n" 5252 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5254 "}"); 5255 5256 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5257 " 100000000 + 10000000) {\n}"); 5258 } 5259 5260 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5261 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5262 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5263 5264 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5265 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5266 5267 verifyFormat( 5268 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5269 " Parameter2);"); 5270 5271 verifyFormat( 5272 "ShortObject->shortFunction(\n" 5273 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5274 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5275 5276 verifyFormat("loooooooooooooongFunction(\n" 5277 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5278 5279 verifyFormat( 5280 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5281 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5282 5283 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5284 " .WillRepeatedly(Return(SomeValue));"); 5285 verifyFormat("void f() {\n" 5286 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5287 " .Times(2)\n" 5288 " .WillRepeatedly(Return(SomeValue));\n" 5289 "}"); 5290 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5291 " ccccccccccccccccccccccc);"); 5292 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5294 " .aaaaa(aaaaa),\n" 5295 " aaaaaaaaaaaaaaaaaaaaa);"); 5296 verifyFormat("void f() {\n" 5297 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5298 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5299 "}"); 5300 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5301 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5302 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5303 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5304 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5305 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5307 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5309 "}"); 5310 5311 // Here, it is not necessary to wrap at "." or "->". 5312 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5313 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5314 verifyFormat( 5315 "aaaaaaaaaaa->aaaaaaaaa(\n" 5316 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5317 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5318 5319 verifyFormat( 5320 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5322 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5323 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5324 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5325 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5326 5327 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5329 " .a();"); 5330 5331 FormatStyle NoBinPacking = getLLVMStyle(); 5332 NoBinPacking.BinPackParameters = false; 5333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5334 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5335 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5336 " aaaaaaaaaaaaaaaaaaa,\n" 5337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5338 NoBinPacking); 5339 5340 // If there is a subsequent call, change to hanging indentation. 5341 verifyFormat( 5342 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5343 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5344 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5345 verifyFormat( 5346 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5347 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5348 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5350 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5351 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5353 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5354 } 5355 5356 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5357 verifyFormat("template <typename T>\n" 5358 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5359 verifyFormat("template <typename T>\n" 5360 "// T should be one of {A, B}.\n" 5361 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5362 verifyFormat( 5363 "template <typename T>\n" 5364 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5365 verifyFormat("template <typename T>\n" 5366 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5367 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5368 verifyFormat( 5369 "template <typename T>\n" 5370 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5371 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5372 verifyFormat( 5373 "template <typename T>\n" 5374 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5375 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5377 verifyFormat("template <typename T>\n" 5378 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5379 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5380 verifyFormat( 5381 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5382 " typename T4 = char>\n" 5383 "void f();"); 5384 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5385 " template <typename> class cccccccccccccccccccccc,\n" 5386 " typename ddddddddddddd>\n" 5387 "class C {};"); 5388 verifyFormat( 5389 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5391 5392 verifyFormat("void f() {\n" 5393 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5394 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5395 "}"); 5396 5397 verifyFormat("template <typename T> class C {};"); 5398 verifyFormat("template <typename T> void f();"); 5399 verifyFormat("template <typename T> void f() {}"); 5400 verifyFormat( 5401 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5404 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5407 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5408 getLLVMStyleWithColumns(72)); 5409 EXPECT_EQ("static_cast<A< //\n" 5410 " B> *>(\n" 5411 "\n" 5412 " );", 5413 format("static_cast<A<//\n" 5414 " B>*>(\n" 5415 "\n" 5416 " );")); 5417 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5418 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5419 5420 FormatStyle AlwaysBreak = getLLVMStyle(); 5421 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5422 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5423 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5424 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5425 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5426 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5427 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5428 verifyFormat("template <template <typename> class Fooooooo,\n" 5429 " template <typename> class Baaaaaaar>\n" 5430 "struct C {};", 5431 AlwaysBreak); 5432 verifyFormat("template <typename T> // T can be A, B or C.\n" 5433 "struct C {};", 5434 AlwaysBreak); 5435 verifyFormat("template <enum E> class A {\n" 5436 "public:\n" 5437 " E *f();\n" 5438 "};"); 5439 } 5440 5441 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5442 verifyFormat( 5443 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5445 verifyFormat( 5446 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5449 5450 // FIXME: Should we have the extra indent after the second break? 5451 verifyFormat( 5452 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5455 5456 verifyFormat( 5457 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5458 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5459 5460 // Breaking at nested name specifiers is generally not desirable. 5461 verifyFormat( 5462 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5463 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5464 5465 verifyFormat( 5466 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5467 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5469 " aaaaaaaaaaaaaaaaaaaaa);", 5470 getLLVMStyleWithColumns(74)); 5471 5472 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5473 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5474 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5475 } 5476 5477 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5478 verifyFormat("A<int> a;"); 5479 verifyFormat("A<A<A<int>>> a;"); 5480 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5481 verifyFormat("bool x = a < 1 || 2 > a;"); 5482 verifyFormat("bool x = 5 < f<int>();"); 5483 verifyFormat("bool x = f<int>() > 5;"); 5484 verifyFormat("bool x = 5 < a<int>::x;"); 5485 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5486 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5487 5488 verifyGoogleFormat("A<A<int>> a;"); 5489 verifyGoogleFormat("A<A<A<int>>> a;"); 5490 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5491 verifyGoogleFormat("A<A<int> > a;"); 5492 verifyGoogleFormat("A<A<A<int> > > a;"); 5493 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5494 verifyGoogleFormat("A<::A<int>> a;"); 5495 verifyGoogleFormat("A<::A> a;"); 5496 verifyGoogleFormat("A< ::A> a;"); 5497 verifyGoogleFormat("A< ::A<int> > a;"); 5498 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5499 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5500 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5501 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5502 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5503 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5504 5505 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5506 5507 verifyFormat("test >> a >> b;"); 5508 verifyFormat("test << a >> b;"); 5509 5510 verifyFormat("f<int>();"); 5511 verifyFormat("template <typename T> void f() {}"); 5512 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5513 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5514 "sizeof(char)>::type>;"); 5515 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5516 verifyFormat("f(a.operator()<A>());"); 5517 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5518 " .template operator()<A>());", 5519 getLLVMStyleWithColumns(35)); 5520 5521 // Not template parameters. 5522 verifyFormat("return a < b && c > d;"); 5523 verifyFormat("void f() {\n" 5524 " while (a < b && c > d) {\n" 5525 " }\n" 5526 "}"); 5527 verifyFormat("template <typename... Types>\n" 5528 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5529 5530 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5532 getLLVMStyleWithColumns(60)); 5533 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5534 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5535 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5536 } 5537 5538 TEST_F(FormatTest, BitshiftOperatorWidth) { 5539 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5540 " bar */", 5541 format("int a=1<<2; /* foo\n" 5542 " bar */")); 5543 5544 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5545 " bar */", 5546 format("int b =256>>1 ; /* foo\n" 5547 " bar */")); 5548 } 5549 5550 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5551 verifyFormat("COMPARE(a, ==, b);"); 5552 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5553 } 5554 5555 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5556 verifyFormat("int A::*x;"); 5557 verifyFormat("int (S::*func)(void *);"); 5558 verifyFormat("void f() { int (S::*func)(void *); }"); 5559 verifyFormat("typedef bool *(Class::*Member)() const;"); 5560 verifyFormat("void f() {\n" 5561 " (a->*f)();\n" 5562 " a->*x;\n" 5563 " (a.*f)();\n" 5564 " ((*a).*f)();\n" 5565 " a.*x;\n" 5566 "}"); 5567 verifyFormat("void f() {\n" 5568 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5569 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5570 "}"); 5571 verifyFormat( 5572 "(aaaaaaaaaa->*bbbbbbb)(\n" 5573 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5574 FormatStyle Style = getLLVMStyle(); 5575 Style.PointerAlignment = FormatStyle::PAS_Left; 5576 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5577 } 5578 5579 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5580 verifyFormat("int a = -2;"); 5581 verifyFormat("f(-1, -2, -3);"); 5582 verifyFormat("a[-1] = 5;"); 5583 verifyFormat("int a = 5 + -2;"); 5584 verifyFormat("if (i == -1) {\n}"); 5585 verifyFormat("if (i != -1) {\n}"); 5586 verifyFormat("if (i > -1) {\n}"); 5587 verifyFormat("if (i < -1) {\n}"); 5588 verifyFormat("++(a->f());"); 5589 verifyFormat("--(a->f());"); 5590 verifyFormat("(a->f())++;"); 5591 verifyFormat("a[42]++;"); 5592 verifyFormat("if (!(a->f())) {\n}"); 5593 5594 verifyFormat("a-- > b;"); 5595 verifyFormat("b ? -a : c;"); 5596 verifyFormat("n * sizeof char16;"); 5597 verifyFormat("n * alignof char16;", getGoogleStyle()); 5598 verifyFormat("sizeof(char);"); 5599 verifyFormat("alignof(char);", getGoogleStyle()); 5600 5601 verifyFormat("return -1;"); 5602 verifyFormat("switch (a) {\n" 5603 "case -1:\n" 5604 " break;\n" 5605 "}"); 5606 verifyFormat("#define X -1"); 5607 verifyFormat("#define X -kConstant"); 5608 5609 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5610 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5611 5612 verifyFormat("int a = /* confusing comment */ -1;"); 5613 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5614 verifyFormat("int a = i /* confusing comment */++;"); 5615 } 5616 5617 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5618 verifyFormat("if (!aaaaaaaaaa( // break\n" 5619 " aaaaa)) {\n" 5620 "}"); 5621 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5622 " aaaaa));"); 5623 verifyFormat("*aaa = aaaaaaa( // break\n" 5624 " bbbbbb);"); 5625 } 5626 5627 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5628 verifyFormat("bool operator<();"); 5629 verifyFormat("bool operator>();"); 5630 verifyFormat("bool operator=();"); 5631 verifyFormat("bool operator==();"); 5632 verifyFormat("bool operator!=();"); 5633 verifyFormat("int operator+();"); 5634 verifyFormat("int operator++();"); 5635 verifyFormat("bool operator,();"); 5636 verifyFormat("bool operator();"); 5637 verifyFormat("bool operator()();"); 5638 verifyFormat("bool operator[]();"); 5639 verifyFormat("operator bool();"); 5640 verifyFormat("operator int();"); 5641 verifyFormat("operator void *();"); 5642 verifyFormat("operator SomeType<int>();"); 5643 verifyFormat("operator SomeType<int, int>();"); 5644 verifyFormat("operator SomeType<SomeType<int>>();"); 5645 verifyFormat("void *operator new(std::size_t size);"); 5646 verifyFormat("void *operator new[](std::size_t size);"); 5647 verifyFormat("void operator delete(void *ptr);"); 5648 verifyFormat("void operator delete[](void *ptr);"); 5649 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5650 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5651 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5652 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5653 5654 verifyFormat( 5655 "ostream &operator<<(ostream &OutputStream,\n" 5656 " SomeReallyLongType WithSomeReallyLongValue);"); 5657 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5658 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5659 " return left.group < right.group;\n" 5660 "}"); 5661 verifyFormat("SomeType &operator=(const SomeType &S);"); 5662 verifyFormat("f.template operator()<int>();"); 5663 5664 verifyGoogleFormat("operator void*();"); 5665 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5666 verifyGoogleFormat("operator ::A();"); 5667 5668 verifyFormat("using A::operator+;"); 5669 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5670 "int i;"); 5671 } 5672 5673 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5674 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5675 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5676 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5677 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5678 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5679 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5680 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5681 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5682 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5683 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5684 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5685 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5686 verifyFormat("template <typename T>\n" 5687 "void F(T) && = delete;", 5688 getGoogleStyle()); 5689 5690 FormatStyle AlignLeft = getLLVMStyle(); 5691 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5692 verifyFormat("void A::b() && {}", AlignLeft); 5693 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5694 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5695 AlignLeft); 5696 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5697 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5698 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5699 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5700 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5701 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5702 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5703 5704 FormatStyle Spaces = getLLVMStyle(); 5705 Spaces.SpacesInCStyleCastParentheses = true; 5706 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5707 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5708 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5709 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5710 5711 Spaces.SpacesInCStyleCastParentheses = false; 5712 Spaces.SpacesInParentheses = true; 5713 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5714 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5715 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5716 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5717 } 5718 5719 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5720 verifyFormat("void f() {\n" 5721 " A *a = new A;\n" 5722 " A *a = new (placement) A;\n" 5723 " delete a;\n" 5724 " delete (A *)a;\n" 5725 "}"); 5726 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5727 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5728 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5729 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5730 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5731 verifyFormat("delete[] h->p;"); 5732 } 5733 5734 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5735 verifyFormat("int *f(int *a) {}"); 5736 verifyFormat("int main(int argc, char **argv) {}"); 5737 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5738 verifyIndependentOfContext("f(a, *a);"); 5739 verifyFormat("void g() { f(*a); }"); 5740 verifyIndependentOfContext("int a = b * 10;"); 5741 verifyIndependentOfContext("int a = 10 * b;"); 5742 verifyIndependentOfContext("int a = b * c;"); 5743 verifyIndependentOfContext("int a += b * c;"); 5744 verifyIndependentOfContext("int a -= b * c;"); 5745 verifyIndependentOfContext("int a *= b * c;"); 5746 verifyIndependentOfContext("int a /= b * c;"); 5747 verifyIndependentOfContext("int a = *b;"); 5748 verifyIndependentOfContext("int a = *b * c;"); 5749 verifyIndependentOfContext("int a = b * *c;"); 5750 verifyIndependentOfContext("int a = b * (10);"); 5751 verifyIndependentOfContext("S << b * (10);"); 5752 verifyIndependentOfContext("return 10 * b;"); 5753 verifyIndependentOfContext("return *b * *c;"); 5754 verifyIndependentOfContext("return a & ~b;"); 5755 verifyIndependentOfContext("f(b ? *c : *d);"); 5756 verifyIndependentOfContext("int a = b ? *c : *d;"); 5757 verifyIndependentOfContext("*b = a;"); 5758 verifyIndependentOfContext("a * ~b;"); 5759 verifyIndependentOfContext("a * !b;"); 5760 verifyIndependentOfContext("a * +b;"); 5761 verifyIndependentOfContext("a * -b;"); 5762 verifyIndependentOfContext("a * ++b;"); 5763 verifyIndependentOfContext("a * --b;"); 5764 verifyIndependentOfContext("a[4] * b;"); 5765 verifyIndependentOfContext("a[a * a] = 1;"); 5766 verifyIndependentOfContext("f() * b;"); 5767 verifyIndependentOfContext("a * [self dostuff];"); 5768 verifyIndependentOfContext("int x = a * (a + b);"); 5769 verifyIndependentOfContext("(a *)(a + b);"); 5770 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5771 verifyIndependentOfContext("int *pa = (int *)&a;"); 5772 verifyIndependentOfContext("return sizeof(int **);"); 5773 verifyIndependentOfContext("return sizeof(int ******);"); 5774 verifyIndependentOfContext("return (int **&)a;"); 5775 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5776 verifyFormat("void f(Type (*parameter)[10]) {}"); 5777 verifyFormat("void f(Type (¶meter)[10]) {}"); 5778 verifyGoogleFormat("return sizeof(int**);"); 5779 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5780 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5781 verifyFormat("auto a = [](int **&, int ***) {};"); 5782 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5783 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5784 verifyFormat("[](const decltype(*a) &value) {}"); 5785 verifyFormat("decltype(a * b) F();"); 5786 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5787 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5788 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5789 verifyIndependentOfContext("int i{a * b};"); 5790 verifyIndependentOfContext("aaa && aaa->f();"); 5791 verifyIndependentOfContext("int x = ~*p;"); 5792 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5793 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5794 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5795 verifyFormat("void f() { f(a, c * d); }"); 5796 verifyFormat("void f() { f(new a(), c * d); }"); 5797 verifyFormat("void f(const MyOverride &override);"); 5798 verifyFormat("void f(const MyFinal &final);"); 5799 verifyIndependentOfContext("bool a = f() && override.f();"); 5800 verifyIndependentOfContext("bool a = f() && final.f();"); 5801 5802 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5803 5804 verifyIndependentOfContext("A<int *> a;"); 5805 verifyIndependentOfContext("A<int **> a;"); 5806 verifyIndependentOfContext("A<int *, int *> a;"); 5807 verifyIndependentOfContext("A<int *[]> a;"); 5808 verifyIndependentOfContext( 5809 "const char *const p = reinterpret_cast<const char *const>(q);"); 5810 verifyIndependentOfContext("A<int **, int **> a;"); 5811 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5812 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5813 verifyFormat("for (; a && b;) {\n}"); 5814 verifyFormat("bool foo = true && [] { return false; }();"); 5815 5816 verifyFormat( 5817 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5818 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5819 5820 verifyGoogleFormat("int const* a = &b;"); 5821 verifyGoogleFormat("**outparam = 1;"); 5822 verifyGoogleFormat("*outparam = a * b;"); 5823 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5824 verifyGoogleFormat("A<int*> a;"); 5825 verifyGoogleFormat("A<int**> a;"); 5826 verifyGoogleFormat("A<int*, int*> a;"); 5827 verifyGoogleFormat("A<int**, int**> a;"); 5828 verifyGoogleFormat("f(b ? *c : *d);"); 5829 verifyGoogleFormat("int a = b ? *c : *d;"); 5830 verifyGoogleFormat("Type* t = **x;"); 5831 verifyGoogleFormat("Type* t = *++*x;"); 5832 verifyGoogleFormat("*++*x;"); 5833 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5834 verifyGoogleFormat("Type* t = x++ * y;"); 5835 verifyGoogleFormat( 5836 "const char* const p = reinterpret_cast<const char* const>(q);"); 5837 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5838 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5839 verifyGoogleFormat("template <typename T>\n" 5840 "void f(int i = 0, SomeType** temps = NULL);"); 5841 5842 FormatStyle Left = getLLVMStyle(); 5843 Left.PointerAlignment = FormatStyle::PAS_Left; 5844 verifyFormat("x = *a(x) = *a(y);", Left); 5845 verifyFormat("for (;; *a = b) {\n}", Left); 5846 verifyFormat("return *this += 1;", Left); 5847 5848 verifyIndependentOfContext("a = *(x + y);"); 5849 verifyIndependentOfContext("a = &(x + y);"); 5850 verifyIndependentOfContext("*(x + y).call();"); 5851 verifyIndependentOfContext("&(x + y)->call();"); 5852 verifyFormat("void f() { &(*I).first; }"); 5853 5854 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5855 verifyFormat( 5856 "int *MyValues = {\n" 5857 " *A, // Operator detection might be confused by the '{'\n" 5858 " *BB // Operator detection might be confused by previous comment\n" 5859 "};"); 5860 5861 verifyIndependentOfContext("if (int *a = &b)"); 5862 verifyIndependentOfContext("if (int &a = *b)"); 5863 verifyIndependentOfContext("if (a & b[i])"); 5864 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5865 verifyIndependentOfContext("if (*b[i])"); 5866 verifyIndependentOfContext("if (int *a = (&b))"); 5867 verifyIndependentOfContext("while (int *a = &b)"); 5868 verifyIndependentOfContext("size = sizeof *a;"); 5869 verifyIndependentOfContext("if (a && (b = c))"); 5870 verifyFormat("void f() {\n" 5871 " for (const int &v : Values) {\n" 5872 " }\n" 5873 "}"); 5874 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5875 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5876 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5877 5878 verifyFormat("#define A (!a * b)"); 5879 verifyFormat("#define MACRO \\\n" 5880 " int *i = a * b; \\\n" 5881 " void f(a *b);", 5882 getLLVMStyleWithColumns(19)); 5883 5884 verifyIndependentOfContext("A = new SomeType *[Length];"); 5885 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5886 verifyIndependentOfContext("T **t = new T *;"); 5887 verifyIndependentOfContext("T **t = new T *();"); 5888 verifyGoogleFormat("A = new SomeType*[Length]();"); 5889 verifyGoogleFormat("A = new SomeType*[Length];"); 5890 verifyGoogleFormat("T** t = new T*;"); 5891 verifyGoogleFormat("T** t = new T*();"); 5892 5893 FormatStyle PointerLeft = getLLVMStyle(); 5894 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5895 verifyFormat("delete *x;", PointerLeft); 5896 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5897 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5898 verifyFormat("template <bool a, bool b> " 5899 "typename t::if<x && y>::type f() {}"); 5900 verifyFormat("template <int *y> f() {}"); 5901 verifyFormat("vector<int *> v;"); 5902 verifyFormat("vector<int *const> v;"); 5903 verifyFormat("vector<int *const **const *> v;"); 5904 verifyFormat("vector<int *volatile> v;"); 5905 verifyFormat("vector<a * b> v;"); 5906 verifyFormat("foo<b && false>();"); 5907 verifyFormat("foo<b & 1>();"); 5908 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5909 verifyFormat( 5910 "template <class T,\n" 5911 " class = typename std::enable_if<\n" 5912 " std::is_integral<T>::value &&\n" 5913 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5914 "void F();", 5915 getLLVMStyleWithColumns(70)); 5916 verifyFormat( 5917 "template <class T,\n" 5918 " class = typename ::std::enable_if<\n" 5919 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5920 "void F();", 5921 getGoogleStyleWithColumns(68)); 5922 5923 verifyIndependentOfContext("MACRO(int *i);"); 5924 verifyIndependentOfContext("MACRO(auto *a);"); 5925 verifyIndependentOfContext("MACRO(const A *a);"); 5926 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5927 verifyFormat("void f() { f(float{1}, a * a); }"); 5928 // FIXME: Is there a way to make this work? 5929 // verifyIndependentOfContext("MACRO(A *a);"); 5930 5931 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5932 verifyFormat("return options != nullptr && operator==(*options);"); 5933 5934 EXPECT_EQ("#define OP(x) \\\n" 5935 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5936 " return s << a.DebugString(); \\\n" 5937 " }", 5938 format("#define OP(x) \\\n" 5939 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5940 " return s << a.DebugString(); \\\n" 5941 " }", 5942 getLLVMStyleWithColumns(50))); 5943 5944 // FIXME: We cannot handle this case yet; we might be able to figure out that 5945 // foo<x> d > v; doesn't make sense. 5946 verifyFormat("foo<a<b && c> d> v;"); 5947 5948 FormatStyle PointerMiddle = getLLVMStyle(); 5949 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5950 verifyFormat("delete *x;", PointerMiddle); 5951 verifyFormat("int * x;", PointerMiddle); 5952 verifyFormat("template <int * y> f() {}", PointerMiddle); 5953 verifyFormat("int * f(int * a) {}", PointerMiddle); 5954 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5955 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5956 verifyFormat("A<int *> a;", PointerMiddle); 5957 verifyFormat("A<int **> a;", PointerMiddle); 5958 verifyFormat("A<int *, int *> a;", PointerMiddle); 5959 verifyFormat("A<int * []> a;", PointerMiddle); 5960 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5961 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5962 verifyFormat("T ** t = new T *;", PointerMiddle); 5963 5964 // Member function reference qualifiers aren't binary operators. 5965 verifyFormat("string // break\n" 5966 "operator()() & {}"); 5967 verifyFormat("string // break\n" 5968 "operator()() && {}"); 5969 verifyGoogleFormat("template <typename T>\n" 5970 "auto x() & -> int {}"); 5971 } 5972 5973 TEST_F(FormatTest, UnderstandsAttributes) { 5974 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5975 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5976 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5977 FormatStyle AfterType = getLLVMStyle(); 5978 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5979 verifyFormat("__attribute__((nodebug)) void\n" 5980 "foo() {}\n", 5981 AfterType); 5982 } 5983 5984 TEST_F(FormatTest, UnderstandsEllipsis) { 5985 verifyFormat("int printf(const char *fmt, ...);"); 5986 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5987 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5988 5989 FormatStyle PointersLeft = getLLVMStyle(); 5990 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5991 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5992 } 5993 5994 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5995 EXPECT_EQ("int *a;\n" 5996 "int *a;\n" 5997 "int *a;", 5998 format("int *a;\n" 5999 "int* a;\n" 6000 "int *a;", 6001 getGoogleStyle())); 6002 EXPECT_EQ("int* a;\n" 6003 "int* a;\n" 6004 "int* a;", 6005 format("int* a;\n" 6006 "int* a;\n" 6007 "int *a;", 6008 getGoogleStyle())); 6009 EXPECT_EQ("int *a;\n" 6010 "int *a;\n" 6011 "int *a;", 6012 format("int *a;\n" 6013 "int * a;\n" 6014 "int * a;", 6015 getGoogleStyle())); 6016 EXPECT_EQ("auto x = [] {\n" 6017 " int *a;\n" 6018 " int *a;\n" 6019 " int *a;\n" 6020 "};", 6021 format("auto x=[]{int *a;\n" 6022 "int * a;\n" 6023 "int * a;};", 6024 getGoogleStyle())); 6025 } 6026 6027 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6028 verifyFormat("int f(int &&a) {}"); 6029 verifyFormat("int f(int a, char &&b) {}"); 6030 verifyFormat("void f() { int &&a = b; }"); 6031 verifyGoogleFormat("int f(int a, char&& b) {}"); 6032 verifyGoogleFormat("void f() { int&& a = b; }"); 6033 6034 verifyIndependentOfContext("A<int &&> a;"); 6035 verifyIndependentOfContext("A<int &&, int &&> a;"); 6036 verifyGoogleFormat("A<int&&> a;"); 6037 verifyGoogleFormat("A<int&&, int&&> a;"); 6038 6039 // Not rvalue references: 6040 verifyFormat("template <bool B, bool C> class A {\n" 6041 " static_assert(B && C, \"Something is wrong\");\n" 6042 "};"); 6043 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6044 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6045 verifyFormat("#define A(a, b) (a && b)"); 6046 } 6047 6048 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6049 verifyFormat("void f() {\n" 6050 " x[aaaaaaaaa -\n" 6051 " b] = 23;\n" 6052 "}", 6053 getLLVMStyleWithColumns(15)); 6054 } 6055 6056 TEST_F(FormatTest, FormatsCasts) { 6057 verifyFormat("Type *A = static_cast<Type *>(P);"); 6058 verifyFormat("Type *A = (Type *)P;"); 6059 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6060 verifyFormat("int a = (int)(2.0f);"); 6061 verifyFormat("int a = (int)2.0f;"); 6062 verifyFormat("x[(int32)y];"); 6063 verifyFormat("x = (int32)y;"); 6064 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6065 verifyFormat("int a = (int)*b;"); 6066 verifyFormat("int a = (int)2.0f;"); 6067 verifyFormat("int a = (int)~0;"); 6068 verifyFormat("int a = (int)++a;"); 6069 verifyFormat("int a = (int)sizeof(int);"); 6070 verifyFormat("int a = (int)+2;"); 6071 verifyFormat("my_int a = (my_int)2.0f;"); 6072 verifyFormat("my_int a = (my_int)sizeof(int);"); 6073 verifyFormat("return (my_int)aaa;"); 6074 verifyFormat("#define x ((int)-1)"); 6075 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6076 verifyFormat("#define p(q) ((int *)&q)"); 6077 verifyFormat("fn(a)(b) + 1;"); 6078 6079 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6080 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6081 verifyFormat("my_int a = (my_int)~0;"); 6082 verifyFormat("my_int a = (my_int)++a;"); 6083 verifyFormat("my_int a = (my_int)-2;"); 6084 verifyFormat("my_int a = (my_int)1;"); 6085 verifyFormat("my_int a = (my_int *)1;"); 6086 verifyFormat("my_int a = (const my_int)-1;"); 6087 verifyFormat("my_int a = (const my_int *)-1;"); 6088 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6089 verifyFormat("my_int a = (ns::my_int)-2;"); 6090 verifyFormat("case (my_int)ONE:"); 6091 verifyFormat("auto x = (X)this;"); 6092 6093 // FIXME: single value wrapped with paren will be treated as cast. 6094 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6095 6096 verifyFormat("{ (void)F; }"); 6097 6098 // Don't break after a cast's 6099 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6100 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6101 " bbbbbbbbbbbbbbbbbbbbbb);"); 6102 6103 // These are not casts. 6104 verifyFormat("void f(int *) {}"); 6105 verifyFormat("f(foo)->b;"); 6106 verifyFormat("f(foo).b;"); 6107 verifyFormat("f(foo)(b);"); 6108 verifyFormat("f(foo)[b];"); 6109 verifyFormat("[](foo) { return 4; }(bar);"); 6110 verifyFormat("(*funptr)(foo)[4];"); 6111 verifyFormat("funptrs[4](foo)[4];"); 6112 verifyFormat("void f(int *);"); 6113 verifyFormat("void f(int *) = 0;"); 6114 verifyFormat("void f(SmallVector<int>) {}"); 6115 verifyFormat("void f(SmallVector<int>);"); 6116 verifyFormat("void f(SmallVector<int>) = 0;"); 6117 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6118 verifyFormat("int a = sizeof(int) * b;"); 6119 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6120 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6121 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6122 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6123 6124 // These are not casts, but at some point were confused with casts. 6125 verifyFormat("virtual void foo(int *) override;"); 6126 verifyFormat("virtual void foo(char &) const;"); 6127 verifyFormat("virtual void foo(int *a, char *) const;"); 6128 verifyFormat("int a = sizeof(int *) + b;"); 6129 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6130 verifyFormat("bool b = f(g<int>) && c;"); 6131 verifyFormat("typedef void (*f)(int i) func;"); 6132 6133 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6134 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6135 // FIXME: The indentation here is not ideal. 6136 verifyFormat( 6137 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6138 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6139 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6140 } 6141 6142 TEST_F(FormatTest, FormatsFunctionTypes) { 6143 verifyFormat("A<bool()> a;"); 6144 verifyFormat("A<SomeType()> a;"); 6145 verifyFormat("A<void (*)(int, std::string)> a;"); 6146 verifyFormat("A<void *(int)>;"); 6147 verifyFormat("void *(*a)(int *, SomeType *);"); 6148 verifyFormat("int (*func)(void *);"); 6149 verifyFormat("void f() { int (*func)(void *); }"); 6150 verifyFormat("template <class CallbackClass>\n" 6151 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6152 6153 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6154 verifyGoogleFormat("void* (*a)(int);"); 6155 verifyGoogleFormat( 6156 "template <class CallbackClass>\n" 6157 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6158 6159 // Other constructs can look somewhat like function types: 6160 verifyFormat("A<sizeof(*x)> a;"); 6161 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6162 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6163 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6164 verifyFormat("int x = f(&h)();"); 6165 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6166 } 6167 6168 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6169 verifyFormat("A (*foo_)[6];"); 6170 verifyFormat("vector<int> (*foo_)[6];"); 6171 } 6172 6173 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6174 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6175 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6176 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6177 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6178 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6179 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6180 6181 // Different ways of ()-initializiation. 6182 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6183 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6184 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6185 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6186 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6187 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6188 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6189 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6190 } 6191 6192 TEST_F(FormatTest, BreaksLongDeclarations) { 6193 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6194 " AnotherNameForTheLongType;"); 6195 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6197 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6198 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6199 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6200 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6201 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6202 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6203 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6204 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6205 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6206 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6207 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6208 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6209 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6210 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6211 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6212 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6213 FormatStyle Indented = getLLVMStyle(); 6214 Indented.IndentWrappedFunctionNames = true; 6215 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6216 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6217 Indented); 6218 verifyFormat( 6219 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6220 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6221 Indented); 6222 verifyFormat( 6223 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6224 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6225 Indented); 6226 verifyFormat( 6227 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6228 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6229 Indented); 6230 6231 // FIXME: Without the comment, this breaks after "(". 6232 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6233 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6234 getGoogleStyle()); 6235 6236 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6237 " int LoooooooooooooooooooongParam2) {}"); 6238 verifyFormat( 6239 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6240 " SourceLocation L, IdentifierIn *II,\n" 6241 " Type *T) {}"); 6242 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6243 "ReallyReaaallyLongFunctionName(\n" 6244 " const std::string &SomeParameter,\n" 6245 " const SomeType<string, SomeOtherTemplateParameter>\n" 6246 " &ReallyReallyLongParameterName,\n" 6247 " const SomeType<string, SomeOtherTemplateParameter>\n" 6248 " &AnotherLongParameterName) {}"); 6249 verifyFormat("template <typename A>\n" 6250 "SomeLoooooooooooooooooooooongType<\n" 6251 " typename some_namespace::SomeOtherType<A>::Type>\n" 6252 "Function() {}"); 6253 6254 verifyGoogleFormat( 6255 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6256 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6257 verifyGoogleFormat( 6258 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6259 " SourceLocation L) {}"); 6260 verifyGoogleFormat( 6261 "some_namespace::LongReturnType\n" 6262 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6263 " int first_long_parameter, int second_parameter) {}"); 6264 6265 verifyGoogleFormat("template <typename T>\n" 6266 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6267 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6268 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6269 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6270 6271 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6272 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6273 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6274 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6275 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6276 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6277 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6278 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6281 } 6282 6283 TEST_F(FormatTest, FormatsArrays) { 6284 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6285 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6286 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6287 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6288 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6289 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6290 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6291 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6292 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6293 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6294 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6295 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6296 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6297 verifyFormat( 6298 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6299 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6300 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6301 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6302 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6303 6304 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6305 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6306 verifyFormat( 6307 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6308 " .aaaaaaa[0]\n" 6309 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6310 verifyFormat("a[::b::c];"); 6311 6312 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6313 6314 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6315 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6316 } 6317 6318 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6319 verifyFormat("(a)->b();"); 6320 verifyFormat("--a;"); 6321 } 6322 6323 TEST_F(FormatTest, HandlesIncludeDirectives) { 6324 verifyFormat("#include <string>\n" 6325 "#include <a/b/c.h>\n" 6326 "#include \"a/b/string\"\n" 6327 "#include \"string.h\"\n" 6328 "#include \"string.h\"\n" 6329 "#include <a-a>\n" 6330 "#include < path with space >\n" 6331 "#include_next <test.h>" 6332 "#include \"abc.h\" // this is included for ABC\n" 6333 "#include \"some long include\" // with a comment\n" 6334 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6335 getLLVMStyleWithColumns(35)); 6336 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6337 EXPECT_EQ("#include <a>", format("#include<a>")); 6338 6339 verifyFormat("#import <string>"); 6340 verifyFormat("#import <a/b/c.h>"); 6341 verifyFormat("#import \"a/b/string\""); 6342 verifyFormat("#import \"string.h\""); 6343 verifyFormat("#import \"string.h\""); 6344 verifyFormat("#if __has_include(<strstream>)\n" 6345 "#include <strstream>\n" 6346 "#endif"); 6347 6348 verifyFormat("#define MY_IMPORT <a/b>"); 6349 6350 // Protocol buffer definition or missing "#". 6351 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6352 getLLVMStyleWithColumns(30)); 6353 6354 FormatStyle Style = getLLVMStyle(); 6355 Style.AlwaysBreakBeforeMultilineStrings = true; 6356 Style.ColumnLimit = 0; 6357 verifyFormat("#import \"abc.h\"", Style); 6358 6359 // But 'import' might also be a regular C++ namespace. 6360 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6362 } 6363 6364 //===----------------------------------------------------------------------===// 6365 // Error recovery tests. 6366 //===----------------------------------------------------------------------===// 6367 6368 TEST_F(FormatTest, IncompleteParameterLists) { 6369 FormatStyle NoBinPacking = getLLVMStyle(); 6370 NoBinPacking.BinPackParameters = false; 6371 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6372 " double *min_x,\n" 6373 " double *max_x,\n" 6374 " double *min_y,\n" 6375 " double *max_y,\n" 6376 " double *min_z,\n" 6377 " double *max_z, ) {}", 6378 NoBinPacking); 6379 } 6380 6381 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6382 verifyFormat("void f() { return; }\n42"); 6383 verifyFormat("void f() {\n" 6384 " if (0)\n" 6385 " return;\n" 6386 "}\n" 6387 "42"); 6388 verifyFormat("void f() { return }\n42"); 6389 verifyFormat("void f() {\n" 6390 " if (0)\n" 6391 " return\n" 6392 "}\n" 6393 "42"); 6394 } 6395 6396 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6397 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6398 EXPECT_EQ("void f() {\n" 6399 " if (a)\n" 6400 " return\n" 6401 "}", 6402 format("void f ( ) { if ( a ) return }")); 6403 EXPECT_EQ("namespace N {\n" 6404 "void f()\n" 6405 "}", 6406 format("namespace N { void f() }")); 6407 EXPECT_EQ("namespace N {\n" 6408 "void f() {}\n" 6409 "void g()\n" 6410 "}", 6411 format("namespace N { void f( ) { } void g( ) }")); 6412 } 6413 6414 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6415 verifyFormat("int aaaaaaaa =\n" 6416 " // Overlylongcomment\n" 6417 " b;", 6418 getLLVMStyleWithColumns(20)); 6419 verifyFormat("function(\n" 6420 " ShortArgument,\n" 6421 " LoooooooooooongArgument);\n", 6422 getLLVMStyleWithColumns(20)); 6423 } 6424 6425 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6426 verifyFormat("public:"); 6427 verifyFormat("class A {\n" 6428 "public\n" 6429 " void f() {}\n" 6430 "};"); 6431 verifyFormat("public\n" 6432 "int qwerty;"); 6433 verifyFormat("public\n" 6434 "B {}"); 6435 verifyFormat("public\n" 6436 "{}"); 6437 verifyFormat("public\n" 6438 "B { int x; }"); 6439 } 6440 6441 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6442 verifyFormat("{"); 6443 verifyFormat("#})"); 6444 verifyNoCrash("(/**/[:!] ?[)."); 6445 } 6446 6447 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6448 verifyFormat("do {\n}"); 6449 verifyFormat("do {\n}\n" 6450 "f();"); 6451 verifyFormat("do {\n}\n" 6452 "wheeee(fun);"); 6453 verifyFormat("do {\n" 6454 " f();\n" 6455 "}"); 6456 } 6457 6458 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6459 verifyFormat("if {\n foo;\n foo();\n}"); 6460 verifyFormat("switch {\n foo;\n foo();\n}"); 6461 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6462 verifyFormat("while {\n foo;\n foo();\n}"); 6463 verifyFormat("do {\n foo;\n foo();\n} while;"); 6464 } 6465 6466 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6467 verifyIncompleteFormat("namespace {\n" 6468 "class Foo { Foo (\n" 6469 "};\n" 6470 "} // comment"); 6471 } 6472 6473 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6474 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6475 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6476 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6477 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6478 6479 EXPECT_EQ("{\n" 6480 " {\n" 6481 " breakme(\n" 6482 " qwe);\n" 6483 " }\n", 6484 format("{\n" 6485 " {\n" 6486 " breakme(qwe);\n" 6487 "}\n", 6488 getLLVMStyleWithColumns(10))); 6489 } 6490 6491 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6492 verifyFormat("int x = {\n" 6493 " avariable,\n" 6494 " b(alongervariable)};", 6495 getLLVMStyleWithColumns(25)); 6496 } 6497 6498 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6499 verifyFormat("return (a)(b){1, 2, 3};"); 6500 } 6501 6502 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6503 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6504 verifyFormat("vector<int> x{\n" 6505 " 1, 2, 3, 4,\n" 6506 "};"); 6507 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6508 verifyFormat("f({1, 2});"); 6509 verifyFormat("auto v = Foo{-1};"); 6510 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6511 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6512 verifyFormat("new vector<int>{1, 2, 3};"); 6513 verifyFormat("new int[3]{1, 2, 3};"); 6514 verifyFormat("new int{1};"); 6515 verifyFormat("return {arg1, arg2};"); 6516 verifyFormat("return {arg1, SomeType{parameter}};"); 6517 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6518 verifyFormat("new T{arg1, arg2};"); 6519 verifyFormat("f(MyMap[{composite, key}]);"); 6520 verifyFormat("class Class {\n" 6521 " T member = {arg1, arg2};\n" 6522 "};"); 6523 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6524 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6525 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6526 6527 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6528 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6529 verifyFormat("auto i = decltype(x){};"); 6530 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6531 verifyFormat("Node n{1, Node{1000}, //\n" 6532 " 2};"); 6533 verifyFormat("Aaaa aaaaaaa{\n" 6534 " {\n" 6535 " aaaa,\n" 6536 " },\n" 6537 "};"); 6538 verifyFormat("class C : public D {\n" 6539 " SomeClass SC{2};\n" 6540 "};"); 6541 verifyFormat("class C : public A {\n" 6542 " class D : public B {\n" 6543 " void f() { int i{2}; }\n" 6544 " };\n" 6545 "};"); 6546 verifyFormat("#define A {a, a},"); 6547 6548 // Cases where distinguising braced lists and blocks is hard. 6549 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6550 verifyFormat("void f() {\n" 6551 " return; // comment\n" 6552 "}\n" 6553 "SomeType t;"); 6554 verifyFormat("void f() {\n" 6555 " if (a) {\n" 6556 " f();\n" 6557 " }\n" 6558 "}\n" 6559 "SomeType t;"); 6560 6561 // In combination with BinPackArguments = false. 6562 FormatStyle NoBinPacking = getLLVMStyle(); 6563 NoBinPacking.BinPackArguments = false; 6564 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6565 " bbbbb,\n" 6566 " ccccc,\n" 6567 " ddddd,\n" 6568 " eeeee,\n" 6569 " ffffff,\n" 6570 " ggggg,\n" 6571 " hhhhhh,\n" 6572 " iiiiii,\n" 6573 " jjjjjj,\n" 6574 " kkkkkk};", 6575 NoBinPacking); 6576 verifyFormat("const Aaaaaa aaaaa = {\n" 6577 " aaaaa,\n" 6578 " bbbbb,\n" 6579 " ccccc,\n" 6580 " ddddd,\n" 6581 " eeeee,\n" 6582 " ffffff,\n" 6583 " ggggg,\n" 6584 " hhhhhh,\n" 6585 " iiiiii,\n" 6586 " jjjjjj,\n" 6587 " kkkkkk,\n" 6588 "};", 6589 NoBinPacking); 6590 verifyFormat( 6591 "const Aaaaaa aaaaa = {\n" 6592 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6593 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6594 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6595 "};", 6596 NoBinPacking); 6597 6598 // FIXME: The alignment of these trailing comments might be bad. Then again, 6599 // this might be utterly useless in real code. 6600 verifyFormat("Constructor::Constructor()\n" 6601 " : some_value{ //\n" 6602 " aaaaaaa, //\n" 6603 " bbbbbbb} {}"); 6604 6605 // In braced lists, the first comment is always assumed to belong to the 6606 // first element. Thus, it can be moved to the next or previous line as 6607 // appropriate. 6608 EXPECT_EQ("function({// First element:\n" 6609 " 1,\n" 6610 " // Second element:\n" 6611 " 2});", 6612 format("function({\n" 6613 " // First element:\n" 6614 " 1,\n" 6615 " // Second element:\n" 6616 " 2});")); 6617 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6618 " // First element:\n" 6619 " 1,\n" 6620 " // Second element:\n" 6621 " 2};", 6622 format("std::vector<int> MyNumbers{// First element:\n" 6623 " 1,\n" 6624 " // Second element:\n" 6625 " 2};", 6626 getLLVMStyleWithColumns(30))); 6627 // A trailing comma should still lead to an enforced line break. 6628 EXPECT_EQ("vector<int> SomeVector = {\n" 6629 " // aaa\n" 6630 " 1, 2,\n" 6631 "};", 6632 format("vector<int> SomeVector = { // aaa\n" 6633 " 1, 2, };")); 6634 6635 FormatStyle ExtraSpaces = getLLVMStyle(); 6636 ExtraSpaces.Cpp11BracedListStyle = false; 6637 ExtraSpaces.ColumnLimit = 75; 6638 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6639 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6640 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6641 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6642 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6643 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6644 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6645 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6646 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6647 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6648 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6649 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6650 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6651 verifyFormat("class Class {\n" 6652 " T member = { arg1, arg2 };\n" 6653 "};", 6654 ExtraSpaces); 6655 verifyFormat( 6656 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6657 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6658 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6659 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6660 ExtraSpaces); 6661 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6662 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6663 ExtraSpaces); 6664 verifyFormat( 6665 "someFunction(OtherParam,\n" 6666 " BracedList{ // comment 1 (Forcing interesting break)\n" 6667 " param1, param2,\n" 6668 " // comment 2\n" 6669 " param3, param4 });", 6670 ExtraSpaces); 6671 verifyFormat( 6672 "std::this_thread::sleep_for(\n" 6673 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6674 ExtraSpaces); 6675 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6676 " aaaaaaa,\n" 6677 " aaaaaaaaaa,\n" 6678 " aaaaa,\n" 6679 " aaaaaaaaaaaaaaa,\n" 6680 " aaa,\n" 6681 " aaaaaaaaaa,\n" 6682 " a,\n" 6683 " aaaaaaaaaaaaaaaaaaaaa,\n" 6684 " aaaaaaaaaaaa,\n" 6685 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6686 " aaaaaaa,\n" 6687 " a};"); 6688 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6689 } 6690 6691 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6692 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6693 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6694 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6695 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6696 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6697 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6698 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6699 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6700 " 1, 22, 333, 4444, 55555, //\n" 6701 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6702 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6703 verifyFormat( 6704 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6705 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6706 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6707 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6708 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6709 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6710 " 7777777};"); 6711 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6712 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6713 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6714 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6715 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6716 " // Separating comment.\n" 6717 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6718 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6719 " // Leading comment\n" 6720 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6721 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6722 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6723 " 1, 1, 1, 1};", 6724 getLLVMStyleWithColumns(39)); 6725 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6726 " 1, 1, 1, 1};", 6727 getLLVMStyleWithColumns(38)); 6728 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6729 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6730 getLLVMStyleWithColumns(43)); 6731 verifyFormat( 6732 "static unsigned SomeValues[10][3] = {\n" 6733 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6734 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6735 verifyFormat("static auto fields = new vector<string>{\n" 6736 " \"aaaaaaaaaaaaa\",\n" 6737 " \"aaaaaaaaaaaaa\",\n" 6738 " \"aaaaaaaaaaaa\",\n" 6739 " \"aaaaaaaaaaaaaa\",\n" 6740 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6741 " \"aaaaaaaaaaaa\",\n" 6742 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6743 "};"); 6744 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6745 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6746 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6747 " 3, cccccccccccccccccccccc};", 6748 getLLVMStyleWithColumns(60)); 6749 6750 // Trailing commas. 6751 verifyFormat("vector<int> x = {\n" 6752 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6753 "};", 6754 getLLVMStyleWithColumns(39)); 6755 verifyFormat("vector<int> x = {\n" 6756 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6757 "};", 6758 getLLVMStyleWithColumns(39)); 6759 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6760 " 1, 1, 1, 1,\n" 6761 " /**/ /**/};", 6762 getLLVMStyleWithColumns(39)); 6763 6764 // Trailing comment in the first line. 6765 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6766 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6767 " 111111111, 222222222, 3333333333, 444444444, //\n" 6768 " 11111111, 22222222, 333333333, 44444444};"); 6769 // Trailing comment in the last line. 6770 verifyFormat("int aaaaa[] = {\n" 6771 " 1, 2, 3, // comment\n" 6772 " 4, 5, 6 // comment\n" 6773 "};"); 6774 6775 // With nested lists, we should either format one item per line or all nested 6776 // lists one on line. 6777 // FIXME: For some nested lists, we can do better. 6778 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6779 " {aaaaaaaaaaaaaaaaaaa},\n" 6780 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6781 " {aaaaaaaaaaaaaaaaa}};", 6782 getLLVMStyleWithColumns(60)); 6783 verifyFormat( 6784 "SomeStruct my_struct_array = {\n" 6785 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6786 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6787 " {aaa, aaa},\n" 6788 " {aaa, aaa},\n" 6789 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6790 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6791 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6792 6793 // No column layout should be used here. 6794 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6795 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6796 6797 verifyNoCrash("a<,"); 6798 6799 // No braced initializer here. 6800 verifyFormat("void f() {\n" 6801 " struct Dummy {};\n" 6802 " f(v);\n" 6803 "}"); 6804 6805 // Long lists should be formatted in columns even if they are nested. 6806 verifyFormat( 6807 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6808 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6809 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6810 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6811 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6812 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6813 6814 // Allow "single-column" layout even if that violates the column limit. There 6815 // isn't going to be a better way. 6816 verifyFormat("std::vector<int> a = {\n" 6817 " aaaaaaaa,\n" 6818 " aaaaaaaa,\n" 6819 " aaaaaaaa,\n" 6820 " aaaaaaaa,\n" 6821 " aaaaaaaaaa,\n" 6822 " aaaaaaaa,\n" 6823 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6824 getLLVMStyleWithColumns(30)); 6825 verifyFormat("vector<int> aaaa = {\n" 6826 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6827 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6828 " aaaaaa.aaaaaaa,\n" 6829 " aaaaaa.aaaaaaa,\n" 6830 " aaaaaa.aaaaaaa,\n" 6831 " aaaaaa.aaaaaaa,\n" 6832 "};"); 6833 6834 // Don't create hanging lists. 6835 verifyFormat("someFunction(Param,\n" 6836 " {List1, List2,\n" 6837 " List3});", 6838 getLLVMStyleWithColumns(35)); 6839 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6840 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6841 } 6842 6843 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6844 FormatStyle DoNotMerge = getLLVMStyle(); 6845 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6846 6847 verifyFormat("void f() { return 42; }"); 6848 verifyFormat("void f() {\n" 6849 " return 42;\n" 6850 "}", 6851 DoNotMerge); 6852 verifyFormat("void f() {\n" 6853 " // Comment\n" 6854 "}"); 6855 verifyFormat("{\n" 6856 "#error {\n" 6857 " int a;\n" 6858 "}"); 6859 verifyFormat("{\n" 6860 " int a;\n" 6861 "#error {\n" 6862 "}"); 6863 verifyFormat("void f() {} // comment"); 6864 verifyFormat("void f() { int a; } // comment"); 6865 verifyFormat("void f() {\n" 6866 "} // comment", 6867 DoNotMerge); 6868 verifyFormat("void f() {\n" 6869 " int a;\n" 6870 "} // comment", 6871 DoNotMerge); 6872 verifyFormat("void f() {\n" 6873 "} // comment", 6874 getLLVMStyleWithColumns(15)); 6875 6876 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6877 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6878 6879 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6880 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6881 verifyFormat("class C {\n" 6882 " C()\n" 6883 " : iiiiiiii(nullptr),\n" 6884 " kkkkkkk(nullptr),\n" 6885 " mmmmmmm(nullptr),\n" 6886 " nnnnnnn(nullptr) {}\n" 6887 "};", 6888 getGoogleStyle()); 6889 6890 FormatStyle NoColumnLimit = getLLVMStyle(); 6891 NoColumnLimit.ColumnLimit = 0; 6892 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6893 EXPECT_EQ("class C {\n" 6894 " A() : b(0) {}\n" 6895 "};", 6896 format("class C{A():b(0){}};", NoColumnLimit)); 6897 EXPECT_EQ("A()\n" 6898 " : b(0) {\n" 6899 "}", 6900 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6901 6902 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6903 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6904 FormatStyle::SFS_None; 6905 EXPECT_EQ("A()\n" 6906 " : b(0) {\n" 6907 "}", 6908 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6909 EXPECT_EQ("A()\n" 6910 " : b(0) {\n" 6911 "}", 6912 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6913 6914 verifyFormat("#define A \\\n" 6915 " void f() { \\\n" 6916 " int i; \\\n" 6917 " }", 6918 getLLVMStyleWithColumns(20)); 6919 verifyFormat("#define A \\\n" 6920 " void f() { int i; }", 6921 getLLVMStyleWithColumns(21)); 6922 verifyFormat("#define A \\\n" 6923 " void f() { \\\n" 6924 " int i; \\\n" 6925 " } \\\n" 6926 " int j;", 6927 getLLVMStyleWithColumns(22)); 6928 verifyFormat("#define A \\\n" 6929 " void f() { int i; } \\\n" 6930 " int j;", 6931 getLLVMStyleWithColumns(23)); 6932 } 6933 6934 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6935 FormatStyle MergeInlineOnly = getLLVMStyle(); 6936 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6937 verifyFormat("class C {\n" 6938 " int f() { return 42; }\n" 6939 "};", 6940 MergeInlineOnly); 6941 verifyFormat("int f() {\n" 6942 " return 42;\n" 6943 "}", 6944 MergeInlineOnly); 6945 } 6946 6947 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6948 // Elaborate type variable declarations. 6949 verifyFormat("struct foo a = {bar};\nint n;"); 6950 verifyFormat("class foo a = {bar};\nint n;"); 6951 verifyFormat("union foo a = {bar};\nint n;"); 6952 6953 // Elaborate types inside function definitions. 6954 verifyFormat("struct foo f() {}\nint n;"); 6955 verifyFormat("class foo f() {}\nint n;"); 6956 verifyFormat("union foo f() {}\nint n;"); 6957 6958 // Templates. 6959 verifyFormat("template <class X> void f() {}\nint n;"); 6960 verifyFormat("template <struct X> void f() {}\nint n;"); 6961 verifyFormat("template <union X> void f() {}\nint n;"); 6962 6963 // Actual definitions... 6964 verifyFormat("struct {\n} n;"); 6965 verifyFormat( 6966 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6967 verifyFormat("union Z {\n int n;\n} x;"); 6968 verifyFormat("class MACRO Z {\n} n;"); 6969 verifyFormat("class MACRO(X) Z {\n} n;"); 6970 verifyFormat("class __attribute__(X) Z {\n} n;"); 6971 verifyFormat("class __declspec(X) Z {\n} n;"); 6972 verifyFormat("class A##B##C {\n} n;"); 6973 verifyFormat("class alignas(16) Z {\n} n;"); 6974 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6975 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6976 6977 // Redefinition from nested context: 6978 verifyFormat("class A::B::C {\n} n;"); 6979 6980 // Template definitions. 6981 verifyFormat( 6982 "template <typename F>\n" 6983 "Matcher(const Matcher<F> &Other,\n" 6984 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6985 " !is_same<F, T>::value>::type * = 0)\n" 6986 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6987 6988 // FIXME: This is still incorrectly handled at the formatter side. 6989 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6990 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6991 6992 // FIXME: 6993 // This now gets parsed incorrectly as class definition. 6994 // verifyFormat("class A<int> f() {\n}\nint n;"); 6995 6996 // Elaborate types where incorrectly parsing the structural element would 6997 // break the indent. 6998 verifyFormat("if (true)\n" 6999 " class X x;\n" 7000 "else\n" 7001 " f();\n"); 7002 7003 // This is simply incomplete. Formatting is not important, but must not crash. 7004 verifyFormat("class A:"); 7005 } 7006 7007 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7008 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7009 format("#error Leave all white!!!!! space* alone!\n")); 7010 EXPECT_EQ( 7011 "#warning Leave all white!!!!! space* alone!\n", 7012 format("#warning Leave all white!!!!! space* alone!\n")); 7013 EXPECT_EQ("#error 1", format(" # error 1")); 7014 EXPECT_EQ("#warning 1", format(" # warning 1")); 7015 } 7016 7017 TEST_F(FormatTest, FormatHashIfExpressions) { 7018 verifyFormat("#if AAAA && BBBB"); 7019 verifyFormat("#if (AAAA && BBBB)"); 7020 verifyFormat("#elif (AAAA && BBBB)"); 7021 // FIXME: Come up with a better indentation for #elif. 7022 verifyFormat( 7023 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7024 " defined(BBBBBBBB)\n" 7025 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7026 " defined(BBBBBBBB)\n" 7027 "#endif", 7028 getLLVMStyleWithColumns(65)); 7029 } 7030 7031 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7032 FormatStyle AllowsMergedIf = getGoogleStyle(); 7033 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7034 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7035 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7036 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7037 EXPECT_EQ("if (true) return 42;", 7038 format("if (true)\nreturn 42;", AllowsMergedIf)); 7039 FormatStyle ShortMergedIf = AllowsMergedIf; 7040 ShortMergedIf.ColumnLimit = 25; 7041 verifyFormat("#define A \\\n" 7042 " if (true) return 42;", 7043 ShortMergedIf); 7044 verifyFormat("#define A \\\n" 7045 " f(); \\\n" 7046 " if (true)\n" 7047 "#define B", 7048 ShortMergedIf); 7049 verifyFormat("#define A \\\n" 7050 " f(); \\\n" 7051 " if (true)\n" 7052 "g();", 7053 ShortMergedIf); 7054 verifyFormat("{\n" 7055 "#ifdef A\n" 7056 " // Comment\n" 7057 " if (true) continue;\n" 7058 "#endif\n" 7059 " // Comment\n" 7060 " if (true) continue;\n" 7061 "}", 7062 ShortMergedIf); 7063 ShortMergedIf.ColumnLimit = 29; 7064 verifyFormat("#define A \\\n" 7065 " if (aaaaaaaaaa) return 1; \\\n" 7066 " return 2;", 7067 ShortMergedIf); 7068 ShortMergedIf.ColumnLimit = 28; 7069 verifyFormat("#define A \\\n" 7070 " if (aaaaaaaaaa) \\\n" 7071 " return 1; \\\n" 7072 " return 2;", 7073 ShortMergedIf); 7074 } 7075 7076 TEST_F(FormatTest, BlockCommentsInControlLoops) { 7077 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7078 " f();\n" 7079 "}"); 7080 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7081 " f();\n" 7082 "} /* another comment */ else /* comment #3 */ {\n" 7083 " g();\n" 7084 "}"); 7085 verifyFormat("while (0) /* a comment in a strange place */ {\n" 7086 " f();\n" 7087 "}"); 7088 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 7089 " f();\n" 7090 "}"); 7091 verifyFormat("do /* a comment in a strange place */ {\n" 7092 " f();\n" 7093 "} /* another comment */ while (0);"); 7094 } 7095 7096 TEST_F(FormatTest, BlockComments) { 7097 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 7098 format("/* *//* */ /* */\n/* *//* */ /* */")); 7099 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 7100 EXPECT_EQ("#define A /*123*/ \\\n" 7101 " b\n" 7102 "/* */\n" 7103 "someCall(\n" 7104 " parameter);", 7105 format("#define A /*123*/ b\n" 7106 "/* */\n" 7107 "someCall(parameter);", 7108 getLLVMStyleWithColumns(15))); 7109 7110 EXPECT_EQ("#define A\n" 7111 "/* */ someCall(\n" 7112 " parameter);", 7113 format("#define A\n" 7114 "/* */someCall(parameter);", 7115 getLLVMStyleWithColumns(15))); 7116 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7117 EXPECT_EQ("/*\n" 7118 "*\n" 7119 " * aaaaaa\n" 7120 " * aaaaaa\n" 7121 "*/", 7122 format("/*\n" 7123 "*\n" 7124 " * aaaaaa aaaaaa\n" 7125 "*/", 7126 getLLVMStyleWithColumns(10))); 7127 EXPECT_EQ("/*\n" 7128 "**\n" 7129 "* aaaaaa\n" 7130 "*aaaaaa\n" 7131 "*/", 7132 format("/*\n" 7133 "**\n" 7134 "* aaaaaa aaaaaa\n" 7135 "*/", 7136 getLLVMStyleWithColumns(10))); 7137 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7138 " /* line 1\n" 7139 " bbbbbbbbbbbb */\n" 7140 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7141 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7142 " /* line 1\n" 7143 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7144 getLLVMStyleWithColumns(50))); 7145 7146 FormatStyle NoBinPacking = getLLVMStyle(); 7147 NoBinPacking.BinPackParameters = false; 7148 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7149 " 2, /* comment 2 */\n" 7150 " 3, /* comment 3 */\n" 7151 " aaaa,\n" 7152 " bbbb);", 7153 format("someFunction (1, /* comment 1 */\n" 7154 " 2, /* comment 2 */ \n" 7155 " 3, /* comment 3 */\n" 7156 "aaaa, bbbb );", 7157 NoBinPacking)); 7158 verifyFormat( 7159 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7161 EXPECT_EQ( 7162 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7165 format( 7166 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7167 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7169 EXPECT_EQ( 7170 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7171 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7172 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7173 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7174 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7175 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7176 7177 verifyFormat("void f(int * /* unused */) {}"); 7178 7179 EXPECT_EQ("/*\n" 7180 " **\n" 7181 " */", 7182 format("/*\n" 7183 " **\n" 7184 " */")); 7185 EXPECT_EQ("/*\n" 7186 " *q\n" 7187 " */", 7188 format("/*\n" 7189 " *q\n" 7190 " */")); 7191 EXPECT_EQ("/*\n" 7192 " * q\n" 7193 " */", 7194 format("/*\n" 7195 " * q\n" 7196 " */")); 7197 EXPECT_EQ("/*\n" 7198 " **/", 7199 format("/*\n" 7200 " **/")); 7201 EXPECT_EQ("/*\n" 7202 " ***/", 7203 format("/*\n" 7204 " ***/")); 7205 } 7206 7207 TEST_F(FormatTest, BlockCommentsInMacros) { 7208 EXPECT_EQ("#define A \\\n" 7209 " { \\\n" 7210 " /* one line */ \\\n" 7211 " someCall();", 7212 format("#define A { \\\n" 7213 " /* one line */ \\\n" 7214 " someCall();", 7215 getLLVMStyleWithColumns(20))); 7216 EXPECT_EQ("#define A \\\n" 7217 " { \\\n" 7218 " /* previous */ \\\n" 7219 " /* one line */ \\\n" 7220 " someCall();", 7221 format("#define A { \\\n" 7222 " /* previous */ \\\n" 7223 " /* one line */ \\\n" 7224 " someCall();", 7225 getLLVMStyleWithColumns(20))); 7226 } 7227 7228 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7229 EXPECT_EQ("a = {\n" 7230 " 1111 /* */\n" 7231 "};", 7232 format("a = {1111 /* */\n" 7233 "};", 7234 getLLVMStyleWithColumns(15))); 7235 EXPECT_EQ("a = {\n" 7236 " 1111 /* */\n" 7237 "};", 7238 format("a = {1111 /* */\n" 7239 "};", 7240 getLLVMStyleWithColumns(15))); 7241 7242 // FIXME: The formatting is still wrong here. 7243 EXPECT_EQ("a = {\n" 7244 " 1111 /* a\n" 7245 " */\n" 7246 "};", 7247 format("a = {1111 /* a */\n" 7248 "};", 7249 getLLVMStyleWithColumns(15))); 7250 } 7251 7252 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7253 verifyFormat("{\n" 7254 " // a\n" 7255 " // b"); 7256 } 7257 7258 TEST_F(FormatTest, FormatStarDependingOnContext) { 7259 verifyFormat("void f(int *a);"); 7260 verifyFormat("void f() { f(fint * b); }"); 7261 verifyFormat("class A {\n void f(int *a);\n};"); 7262 verifyFormat("class A {\n int *a;\n};"); 7263 verifyFormat("namespace a {\n" 7264 "namespace b {\n" 7265 "class A {\n" 7266 " void f() {}\n" 7267 " int *a;\n" 7268 "};\n" 7269 "}\n" 7270 "}"); 7271 } 7272 7273 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7274 verifyFormat("while"); 7275 verifyFormat("operator"); 7276 } 7277 7278 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7279 // This code would be painfully slow to format if we didn't skip it. 7280 std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x 7281 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7282 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7283 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7284 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7285 "A(1, 1)\n" 7286 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7287 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7288 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7289 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7290 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7291 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7292 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7293 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7294 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7295 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7296 // Deeply nested part is untouched, rest is formatted. 7297 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7298 format(std::string("int i;\n") + Code + "int j;\n", 7299 getLLVMStyle(), IC_ExpectIncomplete)); 7300 } 7301 7302 //===----------------------------------------------------------------------===// 7303 // Objective-C tests. 7304 //===----------------------------------------------------------------------===// 7305 7306 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7307 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7308 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7309 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7310 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7311 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7312 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7313 format("-(NSInteger)Method3:(id)anObject;")); 7314 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7315 format("-(NSInteger)Method4:(id)anObject;")); 7316 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7317 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7318 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7319 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7320 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7321 "forAllCells:(BOOL)flag;", 7322 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7323 "forAllCells:(BOOL)flag;")); 7324 7325 // Very long objectiveC method declaration. 7326 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7327 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7328 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7329 " inRange:(NSRange)range\n" 7330 " outRange:(NSRange)out_range\n" 7331 " outRange1:(NSRange)out_range1\n" 7332 " outRange2:(NSRange)out_range2\n" 7333 " outRange3:(NSRange)out_range3\n" 7334 " outRange4:(NSRange)out_range4\n" 7335 " outRange5:(NSRange)out_range5\n" 7336 " outRange6:(NSRange)out_range6\n" 7337 " outRange7:(NSRange)out_range7\n" 7338 " outRange8:(NSRange)out_range8\n" 7339 " outRange9:(NSRange)out_range9;"); 7340 7341 // When the function name has to be wrapped. 7342 FormatStyle Style = getLLVMStyle(); 7343 Style.IndentWrappedFunctionNames = false; 7344 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7345 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7346 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7347 "}", 7348 Style); 7349 Style.IndentWrappedFunctionNames = true; 7350 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7351 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7352 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7353 "}", 7354 Style); 7355 7356 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7357 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7358 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7359 // protocol lists (but not for template classes): 7360 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7361 7362 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7363 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7364 7365 // If there's no return type (very rare in practice!), LLVM and Google style 7366 // agree. 7367 verifyFormat("- foo;"); 7368 verifyFormat("- foo:(int)f;"); 7369 verifyGoogleFormat("- foo:(int)foo;"); 7370 } 7371 7372 7373 TEST_F(FormatTest, BreaksStringLiterals) { 7374 EXPECT_EQ("\"some text \"\n" 7375 "\"other\";", 7376 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7377 EXPECT_EQ("\"some text \"\n" 7378 "\"other\";", 7379 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7380 EXPECT_EQ( 7381 "#define A \\\n" 7382 " \"some \" \\\n" 7383 " \"text \" \\\n" 7384 " \"other\";", 7385 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7386 EXPECT_EQ( 7387 "#define A \\\n" 7388 " \"so \" \\\n" 7389 " \"text \" \\\n" 7390 " \"other\";", 7391 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7392 7393 EXPECT_EQ("\"some text\"", 7394 format("\"some text\"", getLLVMStyleWithColumns(1))); 7395 EXPECT_EQ("\"some text\"", 7396 format("\"some text\"", getLLVMStyleWithColumns(11))); 7397 EXPECT_EQ("\"some \"\n" 7398 "\"text\"", 7399 format("\"some text\"", getLLVMStyleWithColumns(10))); 7400 EXPECT_EQ("\"some \"\n" 7401 "\"text\"", 7402 format("\"some text\"", getLLVMStyleWithColumns(7))); 7403 EXPECT_EQ("\"some\"\n" 7404 "\" tex\"\n" 7405 "\"t\"", 7406 format("\"some text\"", getLLVMStyleWithColumns(6))); 7407 EXPECT_EQ("\"some\"\n" 7408 "\" tex\"\n" 7409 "\" and\"", 7410 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7411 EXPECT_EQ("\"some\"\n" 7412 "\"/tex\"\n" 7413 "\"/and\"", 7414 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7415 7416 EXPECT_EQ("variable =\n" 7417 " \"long string \"\n" 7418 " \"literal\";", 7419 format("variable = \"long string literal\";", 7420 getLLVMStyleWithColumns(20))); 7421 7422 EXPECT_EQ("variable = f(\n" 7423 " \"long string \"\n" 7424 " \"literal\",\n" 7425 " short,\n" 7426 " loooooooooooooooooooong);", 7427 format("variable = f(\"long string literal\", short, " 7428 "loooooooooooooooooooong);", 7429 getLLVMStyleWithColumns(20))); 7430 7431 EXPECT_EQ( 7432 "f(g(\"long string \"\n" 7433 " \"literal\"),\n" 7434 " b);", 7435 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7436 EXPECT_EQ("f(g(\"long string \"\n" 7437 " \"literal\",\n" 7438 " a),\n" 7439 " b);", 7440 format("f(g(\"long string literal\", a), b);", 7441 getLLVMStyleWithColumns(20))); 7442 EXPECT_EQ( 7443 "f(\"one two\".split(\n" 7444 " variable));", 7445 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7446 EXPECT_EQ("f(\"one two three four five six \"\n" 7447 " \"seven\".split(\n" 7448 " really_looooong_variable));", 7449 format("f(\"one two three four five six seven\"." 7450 "split(really_looooong_variable));", 7451 getLLVMStyleWithColumns(33))); 7452 7453 EXPECT_EQ("f(\"some \"\n" 7454 " \"text\",\n" 7455 " other);", 7456 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7457 7458 // Only break as a last resort. 7459 verifyFormat( 7460 "aaaaaaaaaaaaaaaaaaaa(\n" 7461 " aaaaaaaaaaaaaaaaaaaa,\n" 7462 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7463 7464 EXPECT_EQ("\"splitmea\"\n" 7465 "\"trandomp\"\n" 7466 "\"oint\"", 7467 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7468 7469 EXPECT_EQ("\"split/\"\n" 7470 "\"pathat/\"\n" 7471 "\"slashes\"", 7472 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7473 7474 EXPECT_EQ("\"split/\"\n" 7475 "\"pathat/\"\n" 7476 "\"slashes\"", 7477 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7478 EXPECT_EQ("\"split at \"\n" 7479 "\"spaces/at/\"\n" 7480 "\"slashes.at.any$\"\n" 7481 "\"non-alphanumeric%\"\n" 7482 "\"1111111111characte\"\n" 7483 "\"rs\"", 7484 format("\"split at " 7485 "spaces/at/" 7486 "slashes.at." 7487 "any$non-" 7488 "alphanumeric%" 7489 "1111111111characte" 7490 "rs\"", 7491 getLLVMStyleWithColumns(20))); 7492 7493 // Verify that splitting the strings understands 7494 // Style::AlwaysBreakBeforeMultilineStrings. 7495 EXPECT_EQ( 7496 "aaaaaaaaaaaa(\n" 7497 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7498 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7499 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7500 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7501 "aaaaaaaaaaaaaaaaaaaaaa\");", 7502 getGoogleStyle())); 7503 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7504 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7505 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7506 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7507 "aaaaaaaaaaaaaaaaaaaaaa\";", 7508 getGoogleStyle())); 7509 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7510 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7511 format("llvm::outs() << " 7512 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7513 "aaaaaaaaaaaaaaaaaaa\";")); 7514 EXPECT_EQ("ffff(\n" 7515 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7516 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7517 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7518 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7519 getGoogleStyle())); 7520 7521 FormatStyle Style = getLLVMStyleWithColumns(12); 7522 Style.BreakStringLiterals = false; 7523 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7524 7525 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7526 AlignLeft.AlignEscapedNewlinesLeft = true; 7527 EXPECT_EQ("#define A \\\n" 7528 " \"some \" \\\n" 7529 " \"text \" \\\n" 7530 " \"other\";", 7531 format("#define A \"some text other\";", AlignLeft)); 7532 } 7533 7534 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7535 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7536 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7537 EXPECT_EQ("int i = a(b());", 7538 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7539 } 7540 7541 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7542 EXPECT_EQ( 7543 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7544 "(\n" 7545 " \"x\t\");", 7546 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7547 "aaaaaaa(" 7548 "\"x\t\");")); 7549 } 7550 7551 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7552 EXPECT_EQ( 7553 "u8\"utf8 string \"\n" 7554 "u8\"literal\";", 7555 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7556 EXPECT_EQ( 7557 "u\"utf16 string \"\n" 7558 "u\"literal\";", 7559 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7560 EXPECT_EQ( 7561 "U\"utf32 string \"\n" 7562 "U\"literal\";", 7563 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7564 EXPECT_EQ("L\"wide string \"\n" 7565 "L\"literal\";", 7566 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7567 EXPECT_EQ("@\"NSString \"\n" 7568 "@\"literal\";", 7569 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7570 7571 // This input makes clang-format try to split the incomplete unicode escape 7572 // sequence, which used to lead to a crasher. 7573 verifyNoCrash( 7574 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7575 getLLVMStyleWithColumns(60)); 7576 } 7577 7578 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7579 FormatStyle Style = getGoogleStyleWithColumns(15); 7580 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7581 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7582 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7583 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7584 EXPECT_EQ("u8R\"x(raw literal)x\";", 7585 format("u8R\"x(raw literal)x\";", Style)); 7586 } 7587 7588 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7589 FormatStyle Style = getLLVMStyleWithColumns(20); 7590 EXPECT_EQ( 7591 "_T(\"aaaaaaaaaaaaaa\")\n" 7592 "_T(\"aaaaaaaaaaaaaa\")\n" 7593 "_T(\"aaaaaaaaaaaa\")", 7594 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7595 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 7596 " _T(\"aaaaaa\"),\n" 7597 " z);", 7598 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7599 7600 // FIXME: Handle embedded spaces in one iteration. 7601 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7602 // "_T(\"aaaaaaaaaaaaa\")\n" 7603 // "_T(\"aaaaaaaaaaaaa\")\n" 7604 // "_T(\"a\")", 7605 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7606 // getLLVMStyleWithColumns(20))); 7607 EXPECT_EQ( 7608 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7609 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7610 EXPECT_EQ("f(\n" 7611 "#if !TEST\n" 7612 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7613 "#endif\n" 7614 " );", 7615 format("f(\n" 7616 "#if !TEST\n" 7617 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7618 "#endif\n" 7619 ");")); 7620 EXPECT_EQ("f(\n" 7621 "\n" 7622 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7623 format("f(\n" 7624 "\n" 7625 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7626 } 7627 7628 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7629 EXPECT_EQ( 7630 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7633 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7636 } 7637 7638 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7639 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7640 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7641 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7642 "multiline raw string literal xxxxxxxxxxxxxx\n" 7643 ")x\",\n" 7644 " a),\n" 7645 " b);", 7646 format("fffffffffff(g(R\"x(\n" 7647 "multiline raw string literal xxxxxxxxxxxxxx\n" 7648 ")x\", a), b);", 7649 getGoogleStyleWithColumns(20))); 7650 EXPECT_EQ("fffffffffff(\n" 7651 " g(R\"x(qqq\n" 7652 "multiline raw string literal xxxxxxxxxxxxxx\n" 7653 ")x\",\n" 7654 " a),\n" 7655 " b);", 7656 format("fffffffffff(g(R\"x(qqq\n" 7657 "multiline raw string literal xxxxxxxxxxxxxx\n" 7658 ")x\", a), b);", 7659 getGoogleStyleWithColumns(20))); 7660 7661 EXPECT_EQ("fffffffffff(R\"x(\n" 7662 "multiline raw string literal xxxxxxxxxxxxxx\n" 7663 ")x\");", 7664 format("fffffffffff(R\"x(\n" 7665 "multiline raw string literal xxxxxxxxxxxxxx\n" 7666 ")x\");", 7667 getGoogleStyleWithColumns(20))); 7668 EXPECT_EQ("fffffffffff(R\"x(\n" 7669 "multiline raw string literal xxxxxxxxxxxxxx\n" 7670 ")x\" + bbbbbb);", 7671 format("fffffffffff(R\"x(\n" 7672 "multiline raw string literal xxxxxxxxxxxxxx\n" 7673 ")x\" + bbbbbb);", 7674 getGoogleStyleWithColumns(20))); 7675 EXPECT_EQ("fffffffffff(\n" 7676 " R\"x(\n" 7677 "multiline raw string literal xxxxxxxxxxxxxx\n" 7678 ")x\" +\n" 7679 " bbbbbb);", 7680 format("fffffffffff(\n" 7681 " R\"x(\n" 7682 "multiline raw string literal xxxxxxxxxxxxxx\n" 7683 ")x\" + bbbbbb);", 7684 getGoogleStyleWithColumns(20))); 7685 } 7686 7687 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7688 verifyFormat("string a = \"unterminated;"); 7689 EXPECT_EQ("function(\"unterminated,\n" 7690 " OtherParameter);", 7691 format("function( \"unterminated,\n" 7692 " OtherParameter);")); 7693 } 7694 7695 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7696 FormatStyle Style = getLLVMStyle(); 7697 Style.Standard = FormatStyle::LS_Cpp03; 7698 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7699 format("#define x(_a) printf(\"foo\"_a);", Style)); 7700 } 7701 7702 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7703 7704 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7705 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7706 " \"ddeeefff\");", 7707 format("someFunction(\"aaabbbcccdddeeefff\");", 7708 getLLVMStyleWithColumns(25))); 7709 EXPECT_EQ("someFunction1234567890(\n" 7710 " \"aaabbbcccdddeeefff\");", 7711 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7712 getLLVMStyleWithColumns(26))); 7713 EXPECT_EQ("someFunction1234567890(\n" 7714 " \"aaabbbcccdddeeeff\"\n" 7715 " \"f\");", 7716 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7717 getLLVMStyleWithColumns(25))); 7718 EXPECT_EQ("someFunction1234567890(\n" 7719 " \"aaabbbcccdddeeeff\"\n" 7720 " \"f\");", 7721 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7722 getLLVMStyleWithColumns(24))); 7723 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7724 " \"ddde \"\n" 7725 " \"efff\");", 7726 format("someFunction(\"aaabbbcc ddde efff\");", 7727 getLLVMStyleWithColumns(25))); 7728 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7729 " \"ddeeefff\");", 7730 format("someFunction(\"aaabbbccc ddeeefff\");", 7731 getLLVMStyleWithColumns(25))); 7732 EXPECT_EQ("someFunction1234567890(\n" 7733 " \"aaabb \"\n" 7734 " \"cccdddeeefff\");", 7735 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7736 getLLVMStyleWithColumns(25))); 7737 EXPECT_EQ("#define A \\\n" 7738 " string s = \\\n" 7739 " \"123456789\" \\\n" 7740 " \"0\"; \\\n" 7741 " int i;", 7742 format("#define A string s = \"1234567890\"; int i;", 7743 getLLVMStyleWithColumns(20))); 7744 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7745 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7746 " \"dddeeeff\"\n" 7747 " \"f\");", 7748 format("someFunction(\"aaabbbcc dddeeefff\");", 7749 getLLVMStyleWithColumns(25))); 7750 } 7751 7752 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7753 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7754 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7755 EXPECT_EQ("\"test\"\n" 7756 "\"\\n\"", 7757 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7758 EXPECT_EQ("\"tes\\\\\"\n" 7759 "\"n\"", 7760 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7761 EXPECT_EQ("\"\\\\\\\\\"\n" 7762 "\"\\n\"", 7763 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7764 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7765 EXPECT_EQ("\"\\uff01\"\n" 7766 "\"test\"", 7767 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7768 EXPECT_EQ("\"\\Uff01ff02\"", 7769 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7770 EXPECT_EQ("\"\\x000000000001\"\n" 7771 "\"next\"", 7772 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7773 EXPECT_EQ("\"\\x000000000001next\"", 7774 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7775 EXPECT_EQ("\"\\x000000000001\"", 7776 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7777 EXPECT_EQ("\"test\"\n" 7778 "\"\\000000\"\n" 7779 "\"000001\"", 7780 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7781 EXPECT_EQ("\"test\\000\"\n" 7782 "\"00000000\"\n" 7783 "\"1\"", 7784 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7785 } 7786 7787 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7788 verifyFormat("void f() {\n" 7789 " return g() {}\n" 7790 " void h() {}"); 7791 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7792 "g();\n" 7793 "}"); 7794 } 7795 7796 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7797 verifyFormat( 7798 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7799 } 7800 7801 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7802 verifyFormat("class X {\n" 7803 " void f() {\n" 7804 " }\n" 7805 "};", 7806 getLLVMStyleWithColumns(12)); 7807 } 7808 7809 TEST_F(FormatTest, ConfigurableIndentWidth) { 7810 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7811 EightIndent.IndentWidth = 8; 7812 EightIndent.ContinuationIndentWidth = 8; 7813 verifyFormat("void f() {\n" 7814 " someFunction();\n" 7815 " if (true) {\n" 7816 " f();\n" 7817 " }\n" 7818 "}", 7819 EightIndent); 7820 verifyFormat("class X {\n" 7821 " void f() {\n" 7822 " }\n" 7823 "};", 7824 EightIndent); 7825 verifyFormat("int x[] = {\n" 7826 " call(),\n" 7827 " call()};", 7828 EightIndent); 7829 } 7830 7831 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7832 verifyFormat("double\n" 7833 "f();", 7834 getLLVMStyleWithColumns(8)); 7835 } 7836 7837 TEST_F(FormatTest, ConfigurableUseOfTab) { 7838 FormatStyle Tab = getLLVMStyleWithColumns(42); 7839 Tab.IndentWidth = 8; 7840 Tab.UseTab = FormatStyle::UT_Always; 7841 Tab.AlignEscapedNewlinesLeft = true; 7842 7843 EXPECT_EQ("if (aaaaaaaa && // q\n" 7844 " bb)\t\t// w\n" 7845 "\t;", 7846 format("if (aaaaaaaa &&// q\n" 7847 "bb)// w\n" 7848 ";", 7849 Tab)); 7850 EXPECT_EQ("if (aaa && bbb) // w\n" 7851 "\t;", 7852 format("if(aaa&&bbb)// w\n" 7853 ";", 7854 Tab)); 7855 7856 verifyFormat("class X {\n" 7857 "\tvoid f() {\n" 7858 "\t\tsomeFunction(parameter1,\n" 7859 "\t\t\t parameter2);\n" 7860 "\t}\n" 7861 "};", 7862 Tab); 7863 verifyFormat("#define A \\\n" 7864 "\tvoid f() { \\\n" 7865 "\t\tsomeFunction( \\\n" 7866 "\t\t parameter1, \\\n" 7867 "\t\t parameter2); \\\n" 7868 "\t}", 7869 Tab); 7870 7871 Tab.TabWidth = 4; 7872 Tab.IndentWidth = 8; 7873 verifyFormat("class TabWidth4Indent8 {\n" 7874 "\t\tvoid f() {\n" 7875 "\t\t\t\tsomeFunction(parameter1,\n" 7876 "\t\t\t\t\t\t\t parameter2);\n" 7877 "\t\t}\n" 7878 "};", 7879 Tab); 7880 7881 Tab.TabWidth = 4; 7882 Tab.IndentWidth = 4; 7883 verifyFormat("class TabWidth4Indent4 {\n" 7884 "\tvoid f() {\n" 7885 "\t\tsomeFunction(parameter1,\n" 7886 "\t\t\t\t\t parameter2);\n" 7887 "\t}\n" 7888 "};", 7889 Tab); 7890 7891 Tab.TabWidth = 8; 7892 Tab.IndentWidth = 4; 7893 verifyFormat("class TabWidth8Indent4 {\n" 7894 " void f() {\n" 7895 "\tsomeFunction(parameter1,\n" 7896 "\t\t parameter2);\n" 7897 " }\n" 7898 "};", 7899 Tab); 7900 7901 Tab.TabWidth = 8; 7902 Tab.IndentWidth = 8; 7903 EXPECT_EQ("/*\n" 7904 "\t a\t\tcomment\n" 7905 "\t in multiple lines\n" 7906 " */", 7907 format(" /*\t \t \n" 7908 " \t \t a\t\tcomment\t \t\n" 7909 " \t \t in multiple lines\t\n" 7910 " \t */", 7911 Tab)); 7912 7913 Tab.UseTab = FormatStyle::UT_ForIndentation; 7914 verifyFormat("{\n" 7915 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7916 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7917 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7918 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7919 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7920 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7921 "};", 7922 Tab); 7923 verifyFormat("enum AA {\n" 7924 "\ta1, // Force multiple lines\n" 7925 "\ta2,\n" 7926 "\ta3\n" 7927 "};", 7928 Tab); 7929 EXPECT_EQ("if (aaaaaaaa && // q\n" 7930 " bb) // w\n" 7931 "\t;", 7932 format("if (aaaaaaaa &&// q\n" 7933 "bb)// w\n" 7934 ";", 7935 Tab)); 7936 verifyFormat("class X {\n" 7937 "\tvoid f() {\n" 7938 "\t\tsomeFunction(parameter1,\n" 7939 "\t\t parameter2);\n" 7940 "\t}\n" 7941 "};", 7942 Tab); 7943 verifyFormat("{\n" 7944 "\tQ(\n" 7945 "\t {\n" 7946 "\t\t int a;\n" 7947 "\t\t someFunction(aaaaaaaa,\n" 7948 "\t\t bbbbbbb);\n" 7949 "\t },\n" 7950 "\t p);\n" 7951 "}", 7952 Tab); 7953 EXPECT_EQ("{\n" 7954 "\t/* aaaa\n" 7955 "\t bbbb */\n" 7956 "}", 7957 format("{\n" 7958 "/* aaaa\n" 7959 " bbbb */\n" 7960 "}", 7961 Tab)); 7962 EXPECT_EQ("{\n" 7963 "\t/*\n" 7964 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7965 "\t bbbbbbbbbbbbb\n" 7966 "\t*/\n" 7967 "}", 7968 format("{\n" 7969 "/*\n" 7970 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7971 "*/\n" 7972 "}", 7973 Tab)); 7974 EXPECT_EQ("{\n" 7975 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7976 "\t// bbbbbbbbbbbbb\n" 7977 "}", 7978 format("{\n" 7979 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7980 "}", 7981 Tab)); 7982 EXPECT_EQ("{\n" 7983 "\t/*\n" 7984 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7985 "\t bbbbbbbbbbbbb\n" 7986 "\t*/\n" 7987 "}", 7988 format("{\n" 7989 "\t/*\n" 7990 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7991 "\t*/\n" 7992 "}", 7993 Tab)); 7994 EXPECT_EQ("{\n" 7995 "\t/*\n" 7996 "\n" 7997 "\t*/\n" 7998 "}", 7999 format("{\n" 8000 "\t/*\n" 8001 "\n" 8002 "\t*/\n" 8003 "}", 8004 Tab)); 8005 EXPECT_EQ("{\n" 8006 "\t/*\n" 8007 " asdf\n" 8008 "\t*/\n" 8009 "}", 8010 format("{\n" 8011 "\t/*\n" 8012 " asdf\n" 8013 "\t*/\n" 8014 "}", 8015 Tab)); 8016 8017 Tab.UseTab = FormatStyle::UT_Never; 8018 EXPECT_EQ("/*\n" 8019 " a\t\tcomment\n" 8020 " in multiple lines\n" 8021 " */", 8022 format(" /*\t \t \n" 8023 " \t \t a\t\tcomment\t \t\n" 8024 " \t \t in multiple lines\t\n" 8025 " \t */", 8026 Tab)); 8027 EXPECT_EQ("/* some\n" 8028 " comment */", 8029 format(" \t \t /* some\n" 8030 " \t \t comment */", 8031 Tab)); 8032 EXPECT_EQ("int a; /* some\n" 8033 " comment */", 8034 format(" \t \t int a; /* some\n" 8035 " \t \t comment */", 8036 Tab)); 8037 8038 EXPECT_EQ("int a; /* some\n" 8039 "comment */", 8040 format(" \t \t int\ta; /* some\n" 8041 " \t \t comment */", 8042 Tab)); 8043 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8044 " comment */", 8045 format(" \t \t f(\"\t\t\"); /* some\n" 8046 " \t \t comment */", 8047 Tab)); 8048 EXPECT_EQ("{\n" 8049 " /*\n" 8050 " * Comment\n" 8051 " */\n" 8052 " int i;\n" 8053 "}", 8054 format("{\n" 8055 "\t/*\n" 8056 "\t * Comment\n" 8057 "\t */\n" 8058 "\t int i;\n" 8059 "}")); 8060 8061 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8062 Tab.TabWidth = 8; 8063 Tab.IndentWidth = 8; 8064 EXPECT_EQ("if (aaaaaaaa && // q\n" 8065 " bb) // w\n" 8066 "\t;", 8067 format("if (aaaaaaaa &&// q\n" 8068 "bb)// w\n" 8069 ";", 8070 Tab)); 8071 EXPECT_EQ("if (aaa && bbb) // w\n" 8072 "\t;", 8073 format("if(aaa&&bbb)// w\n" 8074 ";", 8075 Tab)); 8076 verifyFormat("class X {\n" 8077 "\tvoid f() {\n" 8078 "\t\tsomeFunction(parameter1,\n" 8079 "\t\t\t parameter2);\n" 8080 "\t}\n" 8081 "};", 8082 Tab); 8083 verifyFormat("#define A \\\n" 8084 "\tvoid f() { \\\n" 8085 "\t\tsomeFunction( \\\n" 8086 "\t\t parameter1, \\\n" 8087 "\t\t parameter2); \\\n" 8088 "\t}", 8089 Tab); 8090 Tab.TabWidth = 4; 8091 Tab.IndentWidth = 8; 8092 verifyFormat("class TabWidth4Indent8 {\n" 8093 "\t\tvoid f() {\n" 8094 "\t\t\t\tsomeFunction(parameter1,\n" 8095 "\t\t\t\t\t\t\t parameter2);\n" 8096 "\t\t}\n" 8097 "};", 8098 Tab); 8099 Tab.TabWidth = 4; 8100 Tab.IndentWidth = 4; 8101 verifyFormat("class TabWidth4Indent4 {\n" 8102 "\tvoid f() {\n" 8103 "\t\tsomeFunction(parameter1,\n" 8104 "\t\t\t\t\t parameter2);\n" 8105 "\t}\n" 8106 "};", 8107 Tab); 8108 Tab.TabWidth = 8; 8109 Tab.IndentWidth = 4; 8110 verifyFormat("class TabWidth8Indent4 {\n" 8111 " void f() {\n" 8112 "\tsomeFunction(parameter1,\n" 8113 "\t\t parameter2);\n" 8114 " }\n" 8115 "};", 8116 Tab); 8117 Tab.TabWidth = 8; 8118 Tab.IndentWidth = 8; 8119 EXPECT_EQ("/*\n" 8120 "\t a\t\tcomment\n" 8121 "\t in multiple lines\n" 8122 " */", 8123 format(" /*\t \t \n" 8124 " \t \t a\t\tcomment\t \t\n" 8125 " \t \t in multiple lines\t\n" 8126 " \t */", 8127 Tab)); 8128 verifyFormat("{\n" 8129 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8130 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8131 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8132 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8133 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8134 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8135 "};", 8136 Tab); 8137 verifyFormat("enum AA {\n" 8138 "\ta1, // Force multiple lines\n" 8139 "\ta2,\n" 8140 "\ta3\n" 8141 "};", 8142 Tab); 8143 EXPECT_EQ("if (aaaaaaaa && // q\n" 8144 " bb) // w\n" 8145 "\t;", 8146 format("if (aaaaaaaa &&// q\n" 8147 "bb)// w\n" 8148 ";", 8149 Tab)); 8150 verifyFormat("class X {\n" 8151 "\tvoid f() {\n" 8152 "\t\tsomeFunction(parameter1,\n" 8153 "\t\t\t parameter2);\n" 8154 "\t}\n" 8155 "};", 8156 Tab); 8157 verifyFormat("{\n" 8158 "\tQ(\n" 8159 "\t {\n" 8160 "\t\t int a;\n" 8161 "\t\t someFunction(aaaaaaaa,\n" 8162 "\t\t\t\t bbbbbbb);\n" 8163 "\t },\n" 8164 "\t p);\n" 8165 "}", 8166 Tab); 8167 EXPECT_EQ("{\n" 8168 "\t/* aaaa\n" 8169 "\t bbbb */\n" 8170 "}", 8171 format("{\n" 8172 "/* aaaa\n" 8173 " bbbb */\n" 8174 "}", 8175 Tab)); 8176 EXPECT_EQ("{\n" 8177 "\t/*\n" 8178 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8179 "\t bbbbbbbbbbbbb\n" 8180 "\t*/\n" 8181 "}", 8182 format("{\n" 8183 "/*\n" 8184 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8185 "*/\n" 8186 "}", 8187 Tab)); 8188 EXPECT_EQ("{\n" 8189 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8190 "\t// bbbbbbbbbbbbb\n" 8191 "}", 8192 format("{\n" 8193 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8194 "}", 8195 Tab)); 8196 EXPECT_EQ("{\n" 8197 "\t/*\n" 8198 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8199 "\t bbbbbbbbbbbbb\n" 8200 "\t*/\n" 8201 "}", 8202 format("{\n" 8203 "\t/*\n" 8204 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8205 "\t*/\n" 8206 "}", 8207 Tab)); 8208 EXPECT_EQ("{\n" 8209 "\t/*\n" 8210 "\n" 8211 "\t*/\n" 8212 "}", 8213 format("{\n" 8214 "\t/*\n" 8215 "\n" 8216 "\t*/\n" 8217 "}", 8218 Tab)); 8219 EXPECT_EQ("{\n" 8220 "\t/*\n" 8221 " asdf\n" 8222 "\t*/\n" 8223 "}", 8224 format("{\n" 8225 "\t/*\n" 8226 " asdf\n" 8227 "\t*/\n" 8228 "}", 8229 Tab)); 8230 EXPECT_EQ("/*\n" 8231 "\t a\t\tcomment\n" 8232 "\t in multiple lines\n" 8233 " */", 8234 format(" /*\t \t \n" 8235 " \t \t a\t\tcomment\t \t\n" 8236 " \t \t in multiple lines\t\n" 8237 " \t */", 8238 Tab)); 8239 EXPECT_EQ("/* some\n" 8240 " comment */", 8241 format(" \t \t /* some\n" 8242 " \t \t comment */", 8243 Tab)); 8244 EXPECT_EQ("int a; /* some\n" 8245 " comment */", 8246 format(" \t \t int a; /* some\n" 8247 " \t \t comment */", 8248 Tab)); 8249 EXPECT_EQ("int a; /* some\n" 8250 "comment */", 8251 format(" \t \t int\ta; /* some\n" 8252 " \t \t comment */", 8253 Tab)); 8254 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8255 " comment */", 8256 format(" \t \t f(\"\t\t\"); /* some\n" 8257 " \t \t comment */", 8258 Tab)); 8259 EXPECT_EQ("{\n" 8260 " /*\n" 8261 " * Comment\n" 8262 " */\n" 8263 " int i;\n" 8264 "}", 8265 format("{\n" 8266 "\t/*\n" 8267 "\t * Comment\n" 8268 "\t */\n" 8269 "\t int i;\n" 8270 "}")); 8271 Tab.AlignConsecutiveAssignments = true; 8272 Tab.AlignConsecutiveDeclarations = true; 8273 Tab.TabWidth = 4; 8274 Tab.IndentWidth = 4; 8275 verifyFormat("class Assign {\n" 8276 "\tvoid f() {\n" 8277 "\t\tint x = 123;\n" 8278 "\t\tint random = 4;\n" 8279 "\t\tstd::string alphabet =\n" 8280 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8281 "\t}\n" 8282 "};", 8283 Tab); 8284 } 8285 8286 TEST_F(FormatTest, CalculatesOriginalColumn) { 8287 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8288 "q\"; /* some\n" 8289 " comment */", 8290 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8291 "q\"; /* some\n" 8292 " comment */", 8293 getLLVMStyle())); 8294 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8295 "/* some\n" 8296 " comment */", 8297 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8298 " /* some\n" 8299 " comment */", 8300 getLLVMStyle())); 8301 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8302 "qqq\n" 8303 "/* some\n" 8304 " comment */", 8305 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8306 "qqq\n" 8307 " /* some\n" 8308 " comment */", 8309 getLLVMStyle())); 8310 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8311 "wwww; /* some\n" 8312 " comment */", 8313 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8314 "wwww; /* some\n" 8315 " comment */", 8316 getLLVMStyle())); 8317 } 8318 8319 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8320 FormatStyle NoSpace = getLLVMStyle(); 8321 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8322 8323 verifyFormat("while(true)\n" 8324 " continue;", 8325 NoSpace); 8326 verifyFormat("for(;;)\n" 8327 " continue;", 8328 NoSpace); 8329 verifyFormat("if(true)\n" 8330 " f();\n" 8331 "else if(true)\n" 8332 " f();", 8333 NoSpace); 8334 verifyFormat("do {\n" 8335 " do_something();\n" 8336 "} while(something());", 8337 NoSpace); 8338 verifyFormat("switch(x) {\n" 8339 "default:\n" 8340 " break;\n" 8341 "}", 8342 NoSpace); 8343 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8344 verifyFormat("size_t x = sizeof(x);", NoSpace); 8345 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8346 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8347 verifyFormat("alignas(128) char a[128];", NoSpace); 8348 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8349 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8350 verifyFormat("int f() throw(Deprecated);", NoSpace); 8351 verifyFormat("typedef void (*cb)(int);", NoSpace); 8352 verifyFormat("T A::operator()();", NoSpace); 8353 verifyFormat("X A::operator++(T);", NoSpace); 8354 8355 FormatStyle Space = getLLVMStyle(); 8356 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8357 8358 verifyFormat("int f ();", Space); 8359 verifyFormat("void f (int a, T b) {\n" 8360 " while (true)\n" 8361 " continue;\n" 8362 "}", 8363 Space); 8364 verifyFormat("if (true)\n" 8365 " f ();\n" 8366 "else if (true)\n" 8367 " f ();", 8368 Space); 8369 verifyFormat("do {\n" 8370 " do_something ();\n" 8371 "} while (something ());", 8372 Space); 8373 verifyFormat("switch (x) {\n" 8374 "default:\n" 8375 " break;\n" 8376 "}", 8377 Space); 8378 verifyFormat("A::A () : a (1) {}", Space); 8379 verifyFormat("void f () __attribute__ ((asdf));", Space); 8380 verifyFormat("*(&a + 1);\n" 8381 "&((&a)[1]);\n" 8382 "a[(b + c) * d];\n" 8383 "(((a + 1) * 2) + 3) * 4;", 8384 Space); 8385 verifyFormat("#define A(x) x", Space); 8386 verifyFormat("#define A (x) x", Space); 8387 verifyFormat("#if defined(x)\n" 8388 "#endif", 8389 Space); 8390 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8391 verifyFormat("size_t x = sizeof (x);", Space); 8392 verifyFormat("auto f (int x) -> decltype (x);", Space); 8393 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8394 verifyFormat("alignas (128) char a[128];", Space); 8395 verifyFormat("size_t x = alignof (MyType);", Space); 8396 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8397 verifyFormat("int f () throw (Deprecated);", Space); 8398 verifyFormat("typedef void (*cb) (int);", Space); 8399 verifyFormat("T A::operator() ();", Space); 8400 verifyFormat("X A::operator++ (T);", Space); 8401 } 8402 8403 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8404 FormatStyle Spaces = getLLVMStyle(); 8405 8406 Spaces.SpacesInParentheses = true; 8407 verifyFormat("call( x, y, z );", Spaces); 8408 verifyFormat("call();", Spaces); 8409 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8410 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8411 Spaces); 8412 verifyFormat("while ( (bool)1 )\n" 8413 " continue;", 8414 Spaces); 8415 verifyFormat("for ( ;; )\n" 8416 " continue;", 8417 Spaces); 8418 verifyFormat("if ( true )\n" 8419 " f();\n" 8420 "else if ( true )\n" 8421 " f();", 8422 Spaces); 8423 verifyFormat("do {\n" 8424 " do_something( (int)i );\n" 8425 "} while ( something() );", 8426 Spaces); 8427 verifyFormat("switch ( x ) {\n" 8428 "default:\n" 8429 " break;\n" 8430 "}", 8431 Spaces); 8432 8433 Spaces.SpacesInParentheses = false; 8434 Spaces.SpacesInCStyleCastParentheses = true; 8435 verifyFormat("Type *A = ( Type * )P;", Spaces); 8436 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8437 verifyFormat("x = ( int32 )y;", Spaces); 8438 verifyFormat("int a = ( int )(2.0f);", Spaces); 8439 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8440 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8441 verifyFormat("#define x (( int )-1)", Spaces); 8442 8443 // Run the first set of tests again with: 8444 Spaces.SpacesInParentheses = false; 8445 Spaces.SpaceInEmptyParentheses = true; 8446 Spaces.SpacesInCStyleCastParentheses = true; 8447 verifyFormat("call(x, y, z);", Spaces); 8448 verifyFormat("call( );", Spaces); 8449 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8450 verifyFormat("while (( bool )1)\n" 8451 " continue;", 8452 Spaces); 8453 verifyFormat("for (;;)\n" 8454 " continue;", 8455 Spaces); 8456 verifyFormat("if (true)\n" 8457 " f( );\n" 8458 "else if (true)\n" 8459 " f( );", 8460 Spaces); 8461 verifyFormat("do {\n" 8462 " do_something(( int )i);\n" 8463 "} while (something( ));", 8464 Spaces); 8465 verifyFormat("switch (x) {\n" 8466 "default:\n" 8467 " break;\n" 8468 "}", 8469 Spaces); 8470 8471 // Run the first set of tests again with: 8472 Spaces.SpaceAfterCStyleCast = true; 8473 verifyFormat("call(x, y, z);", Spaces); 8474 verifyFormat("call( );", Spaces); 8475 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8476 verifyFormat("while (( bool ) 1)\n" 8477 " continue;", 8478 Spaces); 8479 verifyFormat("for (;;)\n" 8480 " continue;", 8481 Spaces); 8482 verifyFormat("if (true)\n" 8483 " f( );\n" 8484 "else if (true)\n" 8485 " f( );", 8486 Spaces); 8487 verifyFormat("do {\n" 8488 " do_something(( int ) i);\n" 8489 "} while (something( ));", 8490 Spaces); 8491 verifyFormat("switch (x) {\n" 8492 "default:\n" 8493 " break;\n" 8494 "}", 8495 Spaces); 8496 8497 // Run subset of tests again with: 8498 Spaces.SpacesInCStyleCastParentheses = false; 8499 Spaces.SpaceAfterCStyleCast = true; 8500 verifyFormat("while ((bool) 1)\n" 8501 " continue;", 8502 Spaces); 8503 verifyFormat("do {\n" 8504 " do_something((int) i);\n" 8505 "} while (something( ));", 8506 Spaces); 8507 } 8508 8509 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8510 verifyFormat("int a[5];"); 8511 verifyFormat("a[3] += 42;"); 8512 8513 FormatStyle Spaces = getLLVMStyle(); 8514 Spaces.SpacesInSquareBrackets = true; 8515 // Lambdas unchanged. 8516 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8517 verifyFormat("return [i, args...] {};", Spaces); 8518 8519 // Not lambdas. 8520 verifyFormat("int a[ 5 ];", Spaces); 8521 verifyFormat("a[ 3 ] += 42;", Spaces); 8522 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8523 verifyFormat("double &operator[](int i) { return 0; }\n" 8524 "int i;", 8525 Spaces); 8526 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8527 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8528 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8529 } 8530 8531 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8532 verifyFormat("int a = 5;"); 8533 verifyFormat("a += 42;"); 8534 verifyFormat("a or_eq 8;"); 8535 8536 FormatStyle Spaces = getLLVMStyle(); 8537 Spaces.SpaceBeforeAssignmentOperators = false; 8538 verifyFormat("int a= 5;", Spaces); 8539 verifyFormat("a+= 42;", Spaces); 8540 verifyFormat("a or_eq 8;", Spaces); 8541 } 8542 8543 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8544 FormatStyle Alignment = getLLVMStyle(); 8545 Alignment.AlignConsecutiveAssignments = false; 8546 verifyFormat("int a = 5;\n" 8547 "int oneTwoThree = 123;", 8548 Alignment); 8549 verifyFormat("int a = 5;\n" 8550 "int oneTwoThree = 123;", 8551 Alignment); 8552 8553 Alignment.AlignConsecutiveAssignments = true; 8554 verifyFormat("int a = 5;\n" 8555 "int oneTwoThree = 123;", 8556 Alignment); 8557 verifyFormat("int a = method();\n" 8558 "int oneTwoThree = 133;", 8559 Alignment); 8560 verifyFormat("a &= 5;\n" 8561 "bcd *= 5;\n" 8562 "ghtyf += 5;\n" 8563 "dvfvdb -= 5;\n" 8564 "a /= 5;\n" 8565 "vdsvsv %= 5;\n" 8566 "sfdbddfbdfbb ^= 5;\n" 8567 "dvsdsv |= 5;\n" 8568 "int dsvvdvsdvvv = 123;", 8569 Alignment); 8570 verifyFormat("int i = 1, j = 10;\n" 8571 "something = 2000;", 8572 Alignment); 8573 verifyFormat("something = 2000;\n" 8574 "int i = 1, j = 10;\n", 8575 Alignment); 8576 verifyFormat("something = 2000;\n" 8577 "another = 911;\n" 8578 "int i = 1, j = 10;\n" 8579 "oneMore = 1;\n" 8580 "i = 2;", 8581 Alignment); 8582 verifyFormat("int a = 5;\n" 8583 "int one = 1;\n" 8584 "method();\n" 8585 "int oneTwoThree = 123;\n" 8586 "int oneTwo = 12;", 8587 Alignment); 8588 verifyFormat("int oneTwoThree = 123;\n" 8589 "int oneTwo = 12;\n" 8590 "method();\n", 8591 Alignment); 8592 verifyFormat("int oneTwoThree = 123; // comment\n" 8593 "int oneTwo = 12; // comment", 8594 Alignment); 8595 EXPECT_EQ("int a = 5;\n" 8596 "\n" 8597 "int oneTwoThree = 123;", 8598 format("int a = 5;\n" 8599 "\n" 8600 "int oneTwoThree= 123;", 8601 Alignment)); 8602 EXPECT_EQ("int a = 5;\n" 8603 "int one = 1;\n" 8604 "\n" 8605 "int oneTwoThree = 123;", 8606 format("int a = 5;\n" 8607 "int one = 1;\n" 8608 "\n" 8609 "int oneTwoThree = 123;", 8610 Alignment)); 8611 EXPECT_EQ("int a = 5;\n" 8612 "int one = 1;\n" 8613 "\n" 8614 "int oneTwoThree = 123;\n" 8615 "int oneTwo = 12;", 8616 format("int a = 5;\n" 8617 "int one = 1;\n" 8618 "\n" 8619 "int oneTwoThree = 123;\n" 8620 "int oneTwo = 12;", 8621 Alignment)); 8622 Alignment.AlignEscapedNewlinesLeft = true; 8623 verifyFormat("#define A \\\n" 8624 " int aaaa = 12; \\\n" 8625 " int b = 23; \\\n" 8626 " int ccc = 234; \\\n" 8627 " int dddddddddd = 2345;", 8628 Alignment); 8629 Alignment.AlignEscapedNewlinesLeft = false; 8630 verifyFormat("#define A " 8631 " \\\n" 8632 " int aaaa = 12; " 8633 " \\\n" 8634 " int b = 23; " 8635 " \\\n" 8636 " int ccc = 234; " 8637 " \\\n" 8638 " int dddddddddd = 2345;", 8639 Alignment); 8640 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8641 "k = 4, int l = 5,\n" 8642 " int m = 6) {\n" 8643 " int j = 10;\n" 8644 " otherThing = 1;\n" 8645 "}", 8646 Alignment); 8647 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8648 " int i = 1;\n" 8649 " int j = 2;\n" 8650 " int big = 10000;\n" 8651 "}", 8652 Alignment); 8653 verifyFormat("class C {\n" 8654 "public:\n" 8655 " int i = 1;\n" 8656 " virtual void f() = 0;\n" 8657 "};", 8658 Alignment); 8659 verifyFormat("int i = 1;\n" 8660 "if (SomeType t = getSomething()) {\n" 8661 "}\n" 8662 "int j = 2;\n" 8663 "int big = 10000;", 8664 Alignment); 8665 verifyFormat("int j = 7;\n" 8666 "for (int k = 0; k < N; ++k) {\n" 8667 "}\n" 8668 "int j = 2;\n" 8669 "int big = 10000;\n" 8670 "}", 8671 Alignment); 8672 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8673 verifyFormat("int i = 1;\n" 8674 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8675 " = someLooooooooooooooooongFunction();\n" 8676 "int j = 2;", 8677 Alignment); 8678 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8679 verifyFormat("int i = 1;\n" 8680 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8681 " someLooooooooooooooooongFunction();\n" 8682 "int j = 2;", 8683 Alignment); 8684 8685 verifyFormat("auto lambda = []() {\n" 8686 " auto i = 0;\n" 8687 " return 0;\n" 8688 "};\n" 8689 "int i = 0;\n" 8690 "auto v = type{\n" 8691 " i = 1, //\n" 8692 " (i = 2), //\n" 8693 " i = 3 //\n" 8694 "};", 8695 Alignment); 8696 8697 // FIXME: Should align all three assignments 8698 verifyFormat( 8699 "int i = 1;\n" 8700 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8701 " loooooooooooooooooooooongParameterB);\n" 8702 "int j = 2;", 8703 Alignment); 8704 8705 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8706 " typename B = very_long_type_name_1,\n" 8707 " typename T_2 = very_long_type_name_2>\n" 8708 "auto foo() {}\n", 8709 Alignment); 8710 verifyFormat("int a, b = 1;\n" 8711 "int c = 2;\n" 8712 "int dd = 3;\n", 8713 Alignment); 8714 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8715 "float b[1][] = {{3.f}};\n", 8716 Alignment); 8717 } 8718 8719 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8720 FormatStyle Alignment = getLLVMStyle(); 8721 Alignment.AlignConsecutiveDeclarations = false; 8722 verifyFormat("float const a = 5;\n" 8723 "int oneTwoThree = 123;", 8724 Alignment); 8725 verifyFormat("int a = 5;\n" 8726 "float const oneTwoThree = 123;", 8727 Alignment); 8728 8729 Alignment.AlignConsecutiveDeclarations = true; 8730 verifyFormat("float const a = 5;\n" 8731 "int oneTwoThree = 123;", 8732 Alignment); 8733 verifyFormat("int a = method();\n" 8734 "float const oneTwoThree = 133;", 8735 Alignment); 8736 verifyFormat("int i = 1, j = 10;\n" 8737 "something = 2000;", 8738 Alignment); 8739 verifyFormat("something = 2000;\n" 8740 "int i = 1, j = 10;\n", 8741 Alignment); 8742 verifyFormat("float something = 2000;\n" 8743 "double another = 911;\n" 8744 "int i = 1, j = 10;\n" 8745 "const int *oneMore = 1;\n" 8746 "unsigned i = 2;", 8747 Alignment); 8748 verifyFormat("float a = 5;\n" 8749 "int one = 1;\n" 8750 "method();\n" 8751 "const double oneTwoThree = 123;\n" 8752 "const unsigned int oneTwo = 12;", 8753 Alignment); 8754 verifyFormat("int oneTwoThree{0}; // comment\n" 8755 "unsigned oneTwo; // comment", 8756 Alignment); 8757 EXPECT_EQ("float const a = 5;\n" 8758 "\n" 8759 "int oneTwoThree = 123;", 8760 format("float const a = 5;\n" 8761 "\n" 8762 "int oneTwoThree= 123;", 8763 Alignment)); 8764 EXPECT_EQ("float a = 5;\n" 8765 "int one = 1;\n" 8766 "\n" 8767 "unsigned oneTwoThree = 123;", 8768 format("float a = 5;\n" 8769 "int one = 1;\n" 8770 "\n" 8771 "unsigned oneTwoThree = 123;", 8772 Alignment)); 8773 EXPECT_EQ("float a = 5;\n" 8774 "int one = 1;\n" 8775 "\n" 8776 "unsigned oneTwoThree = 123;\n" 8777 "int oneTwo = 12;", 8778 format("float a = 5;\n" 8779 "int one = 1;\n" 8780 "\n" 8781 "unsigned oneTwoThree = 123;\n" 8782 "int oneTwo = 12;", 8783 Alignment)); 8784 Alignment.AlignConsecutiveAssignments = true; 8785 verifyFormat("float something = 2000;\n" 8786 "double another = 911;\n" 8787 "int i = 1, j = 10;\n" 8788 "const int *oneMore = 1;\n" 8789 "unsigned i = 2;", 8790 Alignment); 8791 verifyFormat("int oneTwoThree = {0}; // comment\n" 8792 "unsigned oneTwo = 0; // comment", 8793 Alignment); 8794 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8795 " int const i = 1;\n" 8796 " int * j = 2;\n" 8797 " int big = 10000;\n" 8798 "\n" 8799 " unsigned oneTwoThree = 123;\n" 8800 " int oneTwo = 12;\n" 8801 " method();\n" 8802 " float k = 2;\n" 8803 " int ll = 10000;\n" 8804 "}", 8805 format("void SomeFunction(int parameter= 0) {\n" 8806 " int const i= 1;\n" 8807 " int *j=2;\n" 8808 " int big = 10000;\n" 8809 "\n" 8810 "unsigned oneTwoThree =123;\n" 8811 "int oneTwo = 12;\n" 8812 " method();\n" 8813 "float k= 2;\n" 8814 "int ll=10000;\n" 8815 "}", 8816 Alignment)); 8817 Alignment.AlignConsecutiveAssignments = false; 8818 Alignment.AlignEscapedNewlinesLeft = true; 8819 verifyFormat("#define A \\\n" 8820 " int aaaa = 12; \\\n" 8821 " float b = 23; \\\n" 8822 " const int ccc = 234; \\\n" 8823 " unsigned dddddddddd = 2345;", 8824 Alignment); 8825 Alignment.AlignEscapedNewlinesLeft = false; 8826 Alignment.ColumnLimit = 30; 8827 verifyFormat("#define A \\\n" 8828 " int aaaa = 12; \\\n" 8829 " float b = 23; \\\n" 8830 " const int ccc = 234; \\\n" 8831 " int dddddddddd = 2345;", 8832 Alignment); 8833 Alignment.ColumnLimit = 80; 8834 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8835 "k = 4, int l = 5,\n" 8836 " int m = 6) {\n" 8837 " const int j = 10;\n" 8838 " otherThing = 1;\n" 8839 "}", 8840 Alignment); 8841 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8842 " int const i = 1;\n" 8843 " int * j = 2;\n" 8844 " int big = 10000;\n" 8845 "}", 8846 Alignment); 8847 verifyFormat("class C {\n" 8848 "public:\n" 8849 " int i = 1;\n" 8850 " virtual void f() = 0;\n" 8851 "};", 8852 Alignment); 8853 verifyFormat("float i = 1;\n" 8854 "if (SomeType t = getSomething()) {\n" 8855 "}\n" 8856 "const unsigned j = 2;\n" 8857 "int big = 10000;", 8858 Alignment); 8859 verifyFormat("float j = 7;\n" 8860 "for (int k = 0; k < N; ++k) {\n" 8861 "}\n" 8862 "unsigned j = 2;\n" 8863 "int big = 10000;\n" 8864 "}", 8865 Alignment); 8866 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8867 verifyFormat("float i = 1;\n" 8868 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8869 " = someLooooooooooooooooongFunction();\n" 8870 "int j = 2;", 8871 Alignment); 8872 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8873 verifyFormat("int i = 1;\n" 8874 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8875 " someLooooooooooooooooongFunction();\n" 8876 "int j = 2;", 8877 Alignment); 8878 8879 Alignment.AlignConsecutiveAssignments = true; 8880 verifyFormat("auto lambda = []() {\n" 8881 " auto ii = 0;\n" 8882 " float j = 0;\n" 8883 " return 0;\n" 8884 "};\n" 8885 "int i = 0;\n" 8886 "float i2 = 0;\n" 8887 "auto v = type{\n" 8888 " i = 1, //\n" 8889 " (i = 2), //\n" 8890 " i = 3 //\n" 8891 "};", 8892 Alignment); 8893 Alignment.AlignConsecutiveAssignments = false; 8894 8895 // FIXME: Should align all three declarations 8896 verifyFormat( 8897 "int i = 1;\n" 8898 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8899 " loooooooooooooooooooooongParameterB);\n" 8900 "int j = 2;", 8901 Alignment); 8902 8903 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8904 // We expect declarations and assignments to align, as long as it doesn't 8905 // exceed the column limit, starting a new alignemnt sequence whenever it 8906 // happens. 8907 Alignment.AlignConsecutiveAssignments = true; 8908 Alignment.ColumnLimit = 30; 8909 verifyFormat("float ii = 1;\n" 8910 "unsigned j = 2;\n" 8911 "int someVerylongVariable = 1;\n" 8912 "AnotherLongType ll = 123456;\n" 8913 "VeryVeryLongType k = 2;\n" 8914 "int myvar = 1;", 8915 Alignment); 8916 Alignment.ColumnLimit = 80; 8917 Alignment.AlignConsecutiveAssignments = false; 8918 8919 verifyFormat( 8920 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8921 " typename LongType, typename B>\n" 8922 "auto foo() {}\n", 8923 Alignment); 8924 verifyFormat("float a, b = 1;\n" 8925 "int c = 2;\n" 8926 "int dd = 3;\n", 8927 Alignment); 8928 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8929 "float b[1][] = {{3.f}};\n", 8930 Alignment); 8931 Alignment.AlignConsecutiveAssignments = true; 8932 verifyFormat("float a, b = 1;\n" 8933 "int c = 2;\n" 8934 "int dd = 3;\n", 8935 Alignment); 8936 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8937 "float b[1][] = {{3.f}};\n", 8938 Alignment); 8939 Alignment.AlignConsecutiveAssignments = false; 8940 8941 Alignment.ColumnLimit = 30; 8942 Alignment.BinPackParameters = false; 8943 verifyFormat("void foo(float a,\n" 8944 " float b,\n" 8945 " int c,\n" 8946 " uint32_t *d) {\n" 8947 " int * e = 0;\n" 8948 " float f = 0;\n" 8949 " double g = 0;\n" 8950 "}\n" 8951 "void bar(ino_t a,\n" 8952 " int b,\n" 8953 " uint32_t *c,\n" 8954 " bool d) {}\n", 8955 Alignment); 8956 Alignment.BinPackParameters = true; 8957 Alignment.ColumnLimit = 80; 8958 } 8959 8960 TEST_F(FormatTest, LinuxBraceBreaking) { 8961 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8962 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8963 verifyFormat("namespace a\n" 8964 "{\n" 8965 "class A\n" 8966 "{\n" 8967 " void f()\n" 8968 " {\n" 8969 " if (true) {\n" 8970 " a();\n" 8971 " b();\n" 8972 " } else {\n" 8973 " a();\n" 8974 " }\n" 8975 " }\n" 8976 " void g() { return; }\n" 8977 "};\n" 8978 "struct B {\n" 8979 " int x;\n" 8980 "};\n" 8981 "}\n", 8982 LinuxBraceStyle); 8983 verifyFormat("enum X {\n" 8984 " Y = 0,\n" 8985 "}\n", 8986 LinuxBraceStyle); 8987 verifyFormat("struct S {\n" 8988 " int Type;\n" 8989 " union {\n" 8990 " int x;\n" 8991 " double y;\n" 8992 " } Value;\n" 8993 " class C\n" 8994 " {\n" 8995 " MyFavoriteType Value;\n" 8996 " } Class;\n" 8997 "}\n", 8998 LinuxBraceStyle); 8999 } 9000 9001 TEST_F(FormatTest, MozillaBraceBreaking) { 9002 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9003 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9004 verifyFormat("namespace a {\n" 9005 "class A\n" 9006 "{\n" 9007 " void f()\n" 9008 " {\n" 9009 " if (true) {\n" 9010 " a();\n" 9011 " b();\n" 9012 " }\n" 9013 " }\n" 9014 " void g() { return; }\n" 9015 "};\n" 9016 "enum E\n" 9017 "{\n" 9018 " A,\n" 9019 " // foo\n" 9020 " B,\n" 9021 " C\n" 9022 "};\n" 9023 "struct B\n" 9024 "{\n" 9025 " int x;\n" 9026 "};\n" 9027 "}\n", 9028 MozillaBraceStyle); 9029 verifyFormat("struct S\n" 9030 "{\n" 9031 " int Type;\n" 9032 " union\n" 9033 " {\n" 9034 " int x;\n" 9035 " double y;\n" 9036 " } Value;\n" 9037 " class C\n" 9038 " {\n" 9039 " MyFavoriteType Value;\n" 9040 " } Class;\n" 9041 "}\n", 9042 MozillaBraceStyle); 9043 } 9044 9045 TEST_F(FormatTest, StroustrupBraceBreaking) { 9046 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9047 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9048 verifyFormat("namespace a {\n" 9049 "class A {\n" 9050 " void f()\n" 9051 " {\n" 9052 " if (true) {\n" 9053 " a();\n" 9054 " b();\n" 9055 " }\n" 9056 " }\n" 9057 " void g() { return; }\n" 9058 "};\n" 9059 "struct B {\n" 9060 " int x;\n" 9061 "};\n" 9062 "}\n", 9063 StroustrupBraceStyle); 9064 9065 verifyFormat("void foo()\n" 9066 "{\n" 9067 " if (a) {\n" 9068 " a();\n" 9069 " }\n" 9070 " else {\n" 9071 " b();\n" 9072 " }\n" 9073 "}\n", 9074 StroustrupBraceStyle); 9075 9076 verifyFormat("#ifdef _DEBUG\n" 9077 "int foo(int i = 0)\n" 9078 "#else\n" 9079 "int foo(int i = 5)\n" 9080 "#endif\n" 9081 "{\n" 9082 " return i;\n" 9083 "}", 9084 StroustrupBraceStyle); 9085 9086 verifyFormat("void foo() {}\n" 9087 "void bar()\n" 9088 "#ifdef _DEBUG\n" 9089 "{\n" 9090 " foo();\n" 9091 "}\n" 9092 "#else\n" 9093 "{\n" 9094 "}\n" 9095 "#endif", 9096 StroustrupBraceStyle); 9097 9098 verifyFormat("void foobar() { int i = 5; }\n" 9099 "#ifdef _DEBUG\n" 9100 "void bar() {}\n" 9101 "#else\n" 9102 "void bar() { foobar(); }\n" 9103 "#endif", 9104 StroustrupBraceStyle); 9105 } 9106 9107 TEST_F(FormatTest, AllmanBraceBreaking) { 9108 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9109 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9110 verifyFormat("namespace a\n" 9111 "{\n" 9112 "class A\n" 9113 "{\n" 9114 " void f()\n" 9115 " {\n" 9116 " if (true)\n" 9117 " {\n" 9118 " a();\n" 9119 " b();\n" 9120 " }\n" 9121 " }\n" 9122 " void g() { return; }\n" 9123 "};\n" 9124 "struct B\n" 9125 "{\n" 9126 " int x;\n" 9127 "};\n" 9128 "}", 9129 AllmanBraceStyle); 9130 9131 verifyFormat("void f()\n" 9132 "{\n" 9133 " if (true)\n" 9134 " {\n" 9135 " a();\n" 9136 " }\n" 9137 " else if (false)\n" 9138 " {\n" 9139 " b();\n" 9140 " }\n" 9141 " else\n" 9142 " {\n" 9143 " c();\n" 9144 " }\n" 9145 "}\n", 9146 AllmanBraceStyle); 9147 9148 verifyFormat("void f()\n" 9149 "{\n" 9150 " for (int i = 0; i < 10; ++i)\n" 9151 " {\n" 9152 " a();\n" 9153 " }\n" 9154 " while (false)\n" 9155 " {\n" 9156 " b();\n" 9157 " }\n" 9158 " do\n" 9159 " {\n" 9160 " c();\n" 9161 " } while (false)\n" 9162 "}\n", 9163 AllmanBraceStyle); 9164 9165 verifyFormat("void f(int a)\n" 9166 "{\n" 9167 " switch (a)\n" 9168 " {\n" 9169 " case 0:\n" 9170 " break;\n" 9171 " case 1:\n" 9172 " {\n" 9173 " break;\n" 9174 " }\n" 9175 " case 2:\n" 9176 " {\n" 9177 " }\n" 9178 " break;\n" 9179 " default:\n" 9180 " break;\n" 9181 " }\n" 9182 "}\n", 9183 AllmanBraceStyle); 9184 9185 verifyFormat("enum X\n" 9186 "{\n" 9187 " Y = 0,\n" 9188 "}\n", 9189 AllmanBraceStyle); 9190 verifyFormat("enum X\n" 9191 "{\n" 9192 " Y = 0\n" 9193 "}\n", 9194 AllmanBraceStyle); 9195 9196 verifyFormat("@interface BSApplicationController ()\n" 9197 "{\n" 9198 "@private\n" 9199 " id _extraIvar;\n" 9200 "}\n" 9201 "@end\n", 9202 AllmanBraceStyle); 9203 9204 verifyFormat("#ifdef _DEBUG\n" 9205 "int foo(int i = 0)\n" 9206 "#else\n" 9207 "int foo(int i = 5)\n" 9208 "#endif\n" 9209 "{\n" 9210 " return i;\n" 9211 "}", 9212 AllmanBraceStyle); 9213 9214 verifyFormat("void foo() {}\n" 9215 "void bar()\n" 9216 "#ifdef _DEBUG\n" 9217 "{\n" 9218 " foo();\n" 9219 "}\n" 9220 "#else\n" 9221 "{\n" 9222 "}\n" 9223 "#endif", 9224 AllmanBraceStyle); 9225 9226 verifyFormat("void foobar() { int i = 5; }\n" 9227 "#ifdef _DEBUG\n" 9228 "void bar() {}\n" 9229 "#else\n" 9230 "void bar() { foobar(); }\n" 9231 "#endif", 9232 AllmanBraceStyle); 9233 9234 // This shouldn't affect ObjC blocks.. 9235 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9236 " // ...\n" 9237 " int i;\n" 9238 "}];", 9239 AllmanBraceStyle); 9240 verifyFormat("void (^block)(void) = ^{\n" 9241 " // ...\n" 9242 " int i;\n" 9243 "};", 9244 AllmanBraceStyle); 9245 // .. or dict literals. 9246 verifyFormat("void f()\n" 9247 "{\n" 9248 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9249 "}", 9250 AllmanBraceStyle); 9251 verifyFormat("int f()\n" 9252 "{ // comment\n" 9253 " return 42;\n" 9254 "}", 9255 AllmanBraceStyle); 9256 9257 AllmanBraceStyle.ColumnLimit = 19; 9258 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9259 AllmanBraceStyle.ColumnLimit = 18; 9260 verifyFormat("void f()\n" 9261 "{\n" 9262 " int i;\n" 9263 "}", 9264 AllmanBraceStyle); 9265 AllmanBraceStyle.ColumnLimit = 80; 9266 9267 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9268 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9269 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9270 verifyFormat("void f(bool b)\n" 9271 "{\n" 9272 " if (b)\n" 9273 " {\n" 9274 " return;\n" 9275 " }\n" 9276 "}\n", 9277 BreakBeforeBraceShortIfs); 9278 verifyFormat("void f(bool b)\n" 9279 "{\n" 9280 " if (b) return;\n" 9281 "}\n", 9282 BreakBeforeBraceShortIfs); 9283 verifyFormat("void f(bool b)\n" 9284 "{\n" 9285 " while (b)\n" 9286 " {\n" 9287 " return;\n" 9288 " }\n" 9289 "}\n", 9290 BreakBeforeBraceShortIfs); 9291 } 9292 9293 TEST_F(FormatTest, GNUBraceBreaking) { 9294 FormatStyle GNUBraceStyle = getLLVMStyle(); 9295 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9296 verifyFormat("namespace a\n" 9297 "{\n" 9298 "class A\n" 9299 "{\n" 9300 " void f()\n" 9301 " {\n" 9302 " int a;\n" 9303 " {\n" 9304 " int b;\n" 9305 " }\n" 9306 " if (true)\n" 9307 " {\n" 9308 " a();\n" 9309 " b();\n" 9310 " }\n" 9311 " }\n" 9312 " void g() { return; }\n" 9313 "}\n" 9314 "}", 9315 GNUBraceStyle); 9316 9317 verifyFormat("void f()\n" 9318 "{\n" 9319 " if (true)\n" 9320 " {\n" 9321 " a();\n" 9322 " }\n" 9323 " else if (false)\n" 9324 " {\n" 9325 " b();\n" 9326 " }\n" 9327 " else\n" 9328 " {\n" 9329 " c();\n" 9330 " }\n" 9331 "}\n", 9332 GNUBraceStyle); 9333 9334 verifyFormat("void f()\n" 9335 "{\n" 9336 " for (int i = 0; i < 10; ++i)\n" 9337 " {\n" 9338 " a();\n" 9339 " }\n" 9340 " while (false)\n" 9341 " {\n" 9342 " b();\n" 9343 " }\n" 9344 " do\n" 9345 " {\n" 9346 " c();\n" 9347 " }\n" 9348 " while (false);\n" 9349 "}\n", 9350 GNUBraceStyle); 9351 9352 verifyFormat("void f(int a)\n" 9353 "{\n" 9354 " switch (a)\n" 9355 " {\n" 9356 " case 0:\n" 9357 " break;\n" 9358 " case 1:\n" 9359 " {\n" 9360 " break;\n" 9361 " }\n" 9362 " case 2:\n" 9363 " {\n" 9364 " }\n" 9365 " break;\n" 9366 " default:\n" 9367 " break;\n" 9368 " }\n" 9369 "}\n", 9370 GNUBraceStyle); 9371 9372 verifyFormat("enum X\n" 9373 "{\n" 9374 " Y = 0,\n" 9375 "}\n", 9376 GNUBraceStyle); 9377 9378 verifyFormat("@interface BSApplicationController ()\n" 9379 "{\n" 9380 "@private\n" 9381 " id _extraIvar;\n" 9382 "}\n" 9383 "@end\n", 9384 GNUBraceStyle); 9385 9386 verifyFormat("#ifdef _DEBUG\n" 9387 "int foo(int i = 0)\n" 9388 "#else\n" 9389 "int foo(int i = 5)\n" 9390 "#endif\n" 9391 "{\n" 9392 " return i;\n" 9393 "}", 9394 GNUBraceStyle); 9395 9396 verifyFormat("void foo() {}\n" 9397 "void bar()\n" 9398 "#ifdef _DEBUG\n" 9399 "{\n" 9400 " foo();\n" 9401 "}\n" 9402 "#else\n" 9403 "{\n" 9404 "}\n" 9405 "#endif", 9406 GNUBraceStyle); 9407 9408 verifyFormat("void foobar() { int i = 5; }\n" 9409 "#ifdef _DEBUG\n" 9410 "void bar() {}\n" 9411 "#else\n" 9412 "void bar() { foobar(); }\n" 9413 "#endif", 9414 GNUBraceStyle); 9415 } 9416 9417 TEST_F(FormatTest, WebKitBraceBreaking) { 9418 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9419 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9420 verifyFormat("namespace a {\n" 9421 "class A {\n" 9422 " void f()\n" 9423 " {\n" 9424 " if (true) {\n" 9425 " a();\n" 9426 " b();\n" 9427 " }\n" 9428 " }\n" 9429 " void g() { return; }\n" 9430 "};\n" 9431 "enum E {\n" 9432 " A,\n" 9433 " // foo\n" 9434 " B,\n" 9435 " C\n" 9436 "};\n" 9437 "struct B {\n" 9438 " int x;\n" 9439 "};\n" 9440 "}\n", 9441 WebKitBraceStyle); 9442 verifyFormat("struct S {\n" 9443 " int Type;\n" 9444 " union {\n" 9445 " int x;\n" 9446 " double y;\n" 9447 " } Value;\n" 9448 " class C {\n" 9449 " MyFavoriteType Value;\n" 9450 " } Class;\n" 9451 "};\n", 9452 WebKitBraceStyle); 9453 } 9454 9455 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9456 verifyFormat("void f() {\n" 9457 " try {\n" 9458 " } catch (const Exception &e) {\n" 9459 " }\n" 9460 "}\n", 9461 getLLVMStyle()); 9462 } 9463 9464 TEST_F(FormatTest, UnderstandsPragmas) { 9465 verifyFormat("#pragma omp reduction(| : var)"); 9466 verifyFormat("#pragma omp reduction(+ : var)"); 9467 9468 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9469 "(including parentheses).", 9470 format("#pragma mark Any non-hyphenated or hyphenated string " 9471 "(including parentheses).")); 9472 } 9473 9474 TEST_F(FormatTest, UnderstandPragmaOption) { 9475 verifyFormat("#pragma option -C -A"); 9476 9477 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9478 } 9479 9480 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9481 for (size_t i = 1; i < Styles.size(); ++i) \ 9482 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9483 << " differs from Style #0" 9484 9485 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9486 SmallVector<FormatStyle, 3> Styles; 9487 Styles.resize(3); 9488 9489 Styles[0] = getLLVMStyle(); 9490 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9491 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9492 EXPECT_ALL_STYLES_EQUAL(Styles); 9493 9494 Styles[0] = getGoogleStyle(); 9495 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9496 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9497 EXPECT_ALL_STYLES_EQUAL(Styles); 9498 9499 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9500 EXPECT_TRUE( 9501 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9502 EXPECT_TRUE( 9503 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9504 EXPECT_ALL_STYLES_EQUAL(Styles); 9505 9506 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9507 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9508 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9509 EXPECT_ALL_STYLES_EQUAL(Styles); 9510 9511 Styles[0] = getMozillaStyle(); 9512 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9513 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9514 EXPECT_ALL_STYLES_EQUAL(Styles); 9515 9516 Styles[0] = getWebKitStyle(); 9517 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9518 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9519 EXPECT_ALL_STYLES_EQUAL(Styles); 9520 9521 Styles[0] = getGNUStyle(); 9522 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9523 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9524 EXPECT_ALL_STYLES_EQUAL(Styles); 9525 9526 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9527 } 9528 9529 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9530 SmallVector<FormatStyle, 8> Styles; 9531 Styles.resize(2); 9532 9533 Styles[0] = getGoogleStyle(); 9534 Styles[1] = getLLVMStyle(); 9535 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9536 EXPECT_ALL_STYLES_EQUAL(Styles); 9537 9538 Styles.resize(5); 9539 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9540 Styles[1] = getLLVMStyle(); 9541 Styles[1].Language = FormatStyle::LK_JavaScript; 9542 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9543 9544 Styles[2] = getLLVMStyle(); 9545 Styles[2].Language = FormatStyle::LK_JavaScript; 9546 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9547 "BasedOnStyle: Google", 9548 &Styles[2]) 9549 .value()); 9550 9551 Styles[3] = getLLVMStyle(); 9552 Styles[3].Language = FormatStyle::LK_JavaScript; 9553 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9554 "Language: JavaScript", 9555 &Styles[3]) 9556 .value()); 9557 9558 Styles[4] = getLLVMStyle(); 9559 Styles[4].Language = FormatStyle::LK_JavaScript; 9560 EXPECT_EQ(0, parseConfiguration("---\n" 9561 "BasedOnStyle: LLVM\n" 9562 "IndentWidth: 123\n" 9563 "---\n" 9564 "BasedOnStyle: Google\n" 9565 "Language: JavaScript", 9566 &Styles[4]) 9567 .value()); 9568 EXPECT_ALL_STYLES_EQUAL(Styles); 9569 } 9570 9571 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9572 Style.FIELD = false; \ 9573 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9574 EXPECT_TRUE(Style.FIELD); \ 9575 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9576 EXPECT_FALSE(Style.FIELD); 9577 9578 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9579 9580 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9581 Style.STRUCT.FIELD = false; \ 9582 EXPECT_EQ(0, \ 9583 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9584 .value()); \ 9585 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9586 EXPECT_EQ(0, \ 9587 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9588 .value()); \ 9589 EXPECT_FALSE(Style.STRUCT.FIELD); 9590 9591 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9592 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9593 9594 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9595 EXPECT_NE(VALUE, Style.FIELD); \ 9596 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9597 EXPECT_EQ(VALUE, Style.FIELD) 9598 9599 TEST_F(FormatTest, ParsesConfigurationBools) { 9600 FormatStyle Style = {}; 9601 Style.Language = FormatStyle::LK_Cpp; 9602 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9603 CHECK_PARSE_BOOL(AlignOperands); 9604 CHECK_PARSE_BOOL(AlignTrailingComments); 9605 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9606 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9607 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9608 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9609 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9610 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9611 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9612 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9613 CHECK_PARSE_BOOL(BinPackArguments); 9614 CHECK_PARSE_BOOL(BinPackParameters); 9615 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9616 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9617 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9618 CHECK_PARSE_BOOL(BreakStringLiterals); 9619 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9620 CHECK_PARSE_BOOL(DerivePointerAlignment); 9621 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9622 CHECK_PARSE_BOOL(DisableFormat); 9623 CHECK_PARSE_BOOL(IndentCaseLabels); 9624 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9625 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9626 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9627 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9628 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9629 CHECK_PARSE_BOOL(ReflowComments); 9630 CHECK_PARSE_BOOL(SortIncludes); 9631 CHECK_PARSE_BOOL(SpacesInParentheses); 9632 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9633 CHECK_PARSE_BOOL(SpacesInAngles); 9634 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9635 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9636 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9637 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9638 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9639 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9640 9641 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9642 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9643 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9644 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9645 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9646 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9647 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9648 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9649 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9650 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9651 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9652 } 9653 9654 #undef CHECK_PARSE_BOOL 9655 9656 TEST_F(FormatTest, ParsesConfiguration) { 9657 FormatStyle Style = {}; 9658 Style.Language = FormatStyle::LK_Cpp; 9659 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9660 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9661 ConstructorInitializerIndentWidth, 1234u); 9662 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9663 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9664 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9665 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9666 PenaltyBreakBeforeFirstCallParameter, 1234u); 9667 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9668 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9669 PenaltyReturnTypeOnItsOwnLine, 1234u); 9670 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9671 SpacesBeforeTrailingComments, 1234u); 9672 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9673 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9674 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9675 9676 Style.PointerAlignment = FormatStyle::PAS_Middle; 9677 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9678 FormatStyle::PAS_Left); 9679 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9680 FormatStyle::PAS_Right); 9681 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9682 FormatStyle::PAS_Middle); 9683 // For backward compatibility: 9684 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9685 FormatStyle::PAS_Left); 9686 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9687 FormatStyle::PAS_Right); 9688 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9689 FormatStyle::PAS_Middle); 9690 9691 Style.Standard = FormatStyle::LS_Auto; 9692 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9693 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9694 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9695 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9696 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9697 9698 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9699 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9700 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9701 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9702 FormatStyle::BOS_None); 9703 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9704 FormatStyle::BOS_All); 9705 // For backward compatibility: 9706 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9707 FormatStyle::BOS_None); 9708 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9709 FormatStyle::BOS_All); 9710 9711 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9712 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9713 FormatStyle::BAS_Align); 9714 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9715 FormatStyle::BAS_DontAlign); 9716 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9717 FormatStyle::BAS_AlwaysBreak); 9718 // For backward compatibility: 9719 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9720 FormatStyle::BAS_DontAlign); 9721 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9722 FormatStyle::BAS_Align); 9723 9724 Style.UseTab = FormatStyle::UT_ForIndentation; 9725 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9726 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9727 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9728 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9729 FormatStyle::UT_ForContinuationAndIndentation); 9730 // For backward compatibility: 9731 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9732 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9733 9734 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9735 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9736 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9737 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9738 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9739 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9740 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9741 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9742 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9743 // For backward compatibility: 9744 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9745 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9746 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9747 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9748 9749 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9750 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9751 FormatStyle::SBPO_Never); 9752 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9753 FormatStyle::SBPO_Always); 9754 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9755 FormatStyle::SBPO_ControlStatements); 9756 // For backward compatibility: 9757 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9758 FormatStyle::SBPO_Never); 9759 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9760 FormatStyle::SBPO_ControlStatements); 9761 9762 Style.ColumnLimit = 123; 9763 FormatStyle BaseStyle = getLLVMStyle(); 9764 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9765 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9766 9767 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9768 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9769 FormatStyle::BS_Attach); 9770 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9771 FormatStyle::BS_Linux); 9772 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9773 FormatStyle::BS_Mozilla); 9774 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9775 FormatStyle::BS_Stroustrup); 9776 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9777 FormatStyle::BS_Allman); 9778 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9779 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9780 FormatStyle::BS_WebKit); 9781 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9782 FormatStyle::BS_Custom); 9783 9784 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9785 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9786 FormatStyle::RTBS_None); 9787 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9788 FormatStyle::RTBS_All); 9789 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9790 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9791 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9792 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9793 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9794 AlwaysBreakAfterReturnType, 9795 FormatStyle::RTBS_TopLevelDefinitions); 9796 9797 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9798 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9799 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9800 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9801 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9802 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9803 AlwaysBreakAfterDefinitionReturnType, 9804 FormatStyle::DRTBS_TopLevel); 9805 9806 Style.NamespaceIndentation = FormatStyle::NI_All; 9807 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9808 FormatStyle::NI_None); 9809 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9810 FormatStyle::NI_Inner); 9811 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9812 FormatStyle::NI_All); 9813 9814 // FIXME: This is required because parsing a configuration simply overwrites 9815 // the first N elements of the list instead of resetting it. 9816 Style.ForEachMacros.clear(); 9817 std::vector<std::string> BoostForeach; 9818 BoostForeach.push_back("BOOST_FOREACH"); 9819 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9820 std::vector<std::string> BoostAndQForeach; 9821 BoostAndQForeach.push_back("BOOST_FOREACH"); 9822 BoostAndQForeach.push_back("Q_FOREACH"); 9823 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9824 BoostAndQForeach); 9825 9826 Style.IncludeCategories.clear(); 9827 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9828 {".*", 1}}; 9829 CHECK_PARSE("IncludeCategories:\n" 9830 " - Regex: abc/.*\n" 9831 " Priority: 2\n" 9832 " - Regex: .*\n" 9833 " Priority: 1", 9834 IncludeCategories, ExpectedCategories); 9835 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9836 } 9837 9838 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9839 FormatStyle Style = {}; 9840 Style.Language = FormatStyle::LK_Cpp; 9841 CHECK_PARSE("Language: Cpp\n" 9842 "IndentWidth: 12", 9843 IndentWidth, 12u); 9844 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9845 "IndentWidth: 34", 9846 &Style), 9847 ParseError::Unsuitable); 9848 EXPECT_EQ(12u, Style.IndentWidth); 9849 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9850 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9851 9852 Style.Language = FormatStyle::LK_JavaScript; 9853 CHECK_PARSE("Language: JavaScript\n" 9854 "IndentWidth: 12", 9855 IndentWidth, 12u); 9856 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9857 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9858 "IndentWidth: 34", 9859 &Style), 9860 ParseError::Unsuitable); 9861 EXPECT_EQ(23u, Style.IndentWidth); 9862 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9863 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9864 9865 CHECK_PARSE("BasedOnStyle: LLVM\n" 9866 "IndentWidth: 67", 9867 IndentWidth, 67u); 9868 9869 CHECK_PARSE("---\n" 9870 "Language: JavaScript\n" 9871 "IndentWidth: 12\n" 9872 "---\n" 9873 "Language: Cpp\n" 9874 "IndentWidth: 34\n" 9875 "...\n", 9876 IndentWidth, 12u); 9877 9878 Style.Language = FormatStyle::LK_Cpp; 9879 CHECK_PARSE("---\n" 9880 "Language: JavaScript\n" 9881 "IndentWidth: 12\n" 9882 "---\n" 9883 "Language: Cpp\n" 9884 "IndentWidth: 34\n" 9885 "...\n", 9886 IndentWidth, 34u); 9887 CHECK_PARSE("---\n" 9888 "IndentWidth: 78\n" 9889 "---\n" 9890 "Language: JavaScript\n" 9891 "IndentWidth: 56\n" 9892 "...\n", 9893 IndentWidth, 78u); 9894 9895 Style.ColumnLimit = 123; 9896 Style.IndentWidth = 234; 9897 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9898 Style.TabWidth = 345; 9899 EXPECT_FALSE(parseConfiguration("---\n" 9900 "IndentWidth: 456\n" 9901 "BreakBeforeBraces: Allman\n" 9902 "---\n" 9903 "Language: JavaScript\n" 9904 "IndentWidth: 111\n" 9905 "TabWidth: 111\n" 9906 "---\n" 9907 "Language: Cpp\n" 9908 "BreakBeforeBraces: Stroustrup\n" 9909 "TabWidth: 789\n" 9910 "...\n", 9911 &Style)); 9912 EXPECT_EQ(123u, Style.ColumnLimit); 9913 EXPECT_EQ(456u, Style.IndentWidth); 9914 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9915 EXPECT_EQ(789u, Style.TabWidth); 9916 9917 EXPECT_EQ(parseConfiguration("---\n" 9918 "Language: JavaScript\n" 9919 "IndentWidth: 56\n" 9920 "---\n" 9921 "IndentWidth: 78\n" 9922 "...\n", 9923 &Style), 9924 ParseError::Error); 9925 EXPECT_EQ(parseConfiguration("---\n" 9926 "Language: JavaScript\n" 9927 "IndentWidth: 56\n" 9928 "---\n" 9929 "Language: JavaScript\n" 9930 "IndentWidth: 78\n" 9931 "...\n", 9932 &Style), 9933 ParseError::Error); 9934 9935 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9936 } 9937 9938 #undef CHECK_PARSE 9939 9940 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9941 FormatStyle Style = {}; 9942 Style.Language = FormatStyle::LK_JavaScript; 9943 Style.BreakBeforeTernaryOperators = true; 9944 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9945 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9946 9947 Style.BreakBeforeTernaryOperators = true; 9948 EXPECT_EQ(0, parseConfiguration("---\n" 9949 "BasedOnStyle: Google\n" 9950 "---\n" 9951 "Language: JavaScript\n" 9952 "IndentWidth: 76\n" 9953 "...\n", 9954 &Style) 9955 .value()); 9956 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9957 EXPECT_EQ(76u, Style.IndentWidth); 9958 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9959 } 9960 9961 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9962 FormatStyle Style = getLLVMStyle(); 9963 std::string YAML = configurationAsText(Style); 9964 FormatStyle ParsedStyle = {}; 9965 ParsedStyle.Language = FormatStyle::LK_Cpp; 9966 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9967 EXPECT_EQ(Style, ParsedStyle); 9968 } 9969 9970 TEST_F(FormatTest, WorksFor8bitEncodings) { 9971 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9972 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9973 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9974 "\"\xef\xee\xf0\xf3...\"", 9975 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9976 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9977 "\xef\xee\xf0\xf3...\"", 9978 getLLVMStyleWithColumns(12))); 9979 } 9980 9981 TEST_F(FormatTest, HandlesUTF8BOM) { 9982 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9983 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9984 format("\xef\xbb\xbf#include <iostream>")); 9985 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9986 format("\xef\xbb\xbf\n#include <iostream>")); 9987 } 9988 9989 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9990 #if !defined(_MSC_VER) 9991 9992 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9993 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9994 getLLVMStyleWithColumns(35)); 9995 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9996 getLLVMStyleWithColumns(31)); 9997 verifyFormat("// Однажды в студёную зимнюю пору...", 9998 getLLVMStyleWithColumns(36)); 9999 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10000 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10001 getLLVMStyleWithColumns(39)); 10002 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10003 getLLVMStyleWithColumns(35)); 10004 } 10005 10006 TEST_F(FormatTest, SplitsUTF8Strings) { 10007 // Non-printable characters' width is currently considered to be the length in 10008 // bytes in UTF8. The characters can be displayed in very different manner 10009 // (zero-width, single width with a substitution glyph, expanded to their code 10010 // (e.g. "<8d>"), so there's no single correct way to handle them. 10011 EXPECT_EQ("\"aaaaÄ\"\n" 10012 "\"\xc2\x8d\";", 10013 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10014 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10015 "\"\xc2\x8d\";", 10016 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10017 EXPECT_EQ("\"Однажды, в \"\n" 10018 "\"студёную \"\n" 10019 "\"зимнюю \"\n" 10020 "\"пору,\"", 10021 format("\"Однажды, в студёную зимнюю пору,\"", 10022 getLLVMStyleWithColumns(13))); 10023 EXPECT_EQ( 10024 "\"一 二 三 \"\n" 10025 "\"四 五六 \"\n" 10026 "\"七 八 九 \"\n" 10027 "\"十\"", 10028 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10029 EXPECT_EQ("\"一\t二 \"\n" 10030 "\"\t三 \"\n" 10031 "\"四 五\t六 \"\n" 10032 "\"\t七 \"\n" 10033 "\"八九十\tqq\"", 10034 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10035 getLLVMStyleWithColumns(11))); 10036 10037 // UTF8 character in an escape sequence. 10038 EXPECT_EQ("\"aaaaaa\"\n" 10039 "\"\\\xC2\x8D\"", 10040 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10041 } 10042 10043 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10044 EXPECT_EQ("const char *sssss =\n" 10045 " \"一二三四五六七八\\\n" 10046 " 九 十\";", 10047 format("const char *sssss = \"一二三四五六七八\\\n" 10048 " 九 十\";", 10049 getLLVMStyleWithColumns(30))); 10050 } 10051 10052 TEST_F(FormatTest, SplitsUTF8LineComments) { 10053 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10054 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10055 EXPECT_EQ("// Я из лесу\n" 10056 "// вышел; был\n" 10057 "// сильный\n" 10058 "// мороз.", 10059 format("// Я из лесу вышел; был сильный мороз.", 10060 getLLVMStyleWithColumns(13))); 10061 EXPECT_EQ("// 一二三\n" 10062 "// 四五六七\n" 10063 "// 八 九\n" 10064 "// 十", 10065 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10066 } 10067 10068 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10069 EXPECT_EQ("/* Гляжу,\n" 10070 " * поднимается\n" 10071 " * медленно в\n" 10072 " * гору\n" 10073 " * Лошадка,\n" 10074 " * везущая\n" 10075 " * хворосту\n" 10076 " * воз. */", 10077 format("/* Гляжу, поднимается медленно в гору\n" 10078 " * Лошадка, везущая хворосту воз. */", 10079 getLLVMStyleWithColumns(13))); 10080 EXPECT_EQ( 10081 "/* 一二三\n" 10082 " * 四五六七\n" 10083 " * 八 九\n" 10084 " * 十 */", 10085 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10086 EXPECT_EQ("/* \n" 10087 " * \n" 10088 " * - */", 10089 format("/* - */", getLLVMStyleWithColumns(12))); 10090 } 10091 10092 #endif // _MSC_VER 10093 10094 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10095 FormatStyle Style = getLLVMStyle(); 10096 10097 Style.ConstructorInitializerIndentWidth = 4; 10098 verifyFormat( 10099 "SomeClass::Constructor()\n" 10100 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10101 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10102 Style); 10103 10104 Style.ConstructorInitializerIndentWidth = 2; 10105 verifyFormat( 10106 "SomeClass::Constructor()\n" 10107 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10108 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10109 Style); 10110 10111 Style.ConstructorInitializerIndentWidth = 0; 10112 verifyFormat( 10113 "SomeClass::Constructor()\n" 10114 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10115 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10116 Style); 10117 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10118 verifyFormat( 10119 "SomeLongTemplateVariableName<\n" 10120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10121 Style); 10122 verifyFormat( 10123 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10125 Style); 10126 } 10127 10128 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10129 FormatStyle Style = getLLVMStyle(); 10130 Style.BreakConstructorInitializersBeforeComma = true; 10131 Style.ConstructorInitializerIndentWidth = 4; 10132 verifyFormat("SomeClass::Constructor()\n" 10133 " : a(a)\n" 10134 " , b(b)\n" 10135 " , c(c) {}", 10136 Style); 10137 verifyFormat("SomeClass::Constructor()\n" 10138 " : a(a) {}", 10139 Style); 10140 10141 Style.ColumnLimit = 0; 10142 verifyFormat("SomeClass::Constructor()\n" 10143 " : a(a) {}", 10144 Style); 10145 verifyFormat("SomeClass::Constructor() noexcept\n" 10146 " : a(a) {}", 10147 Style); 10148 verifyFormat("SomeClass::Constructor()\n" 10149 " : a(a)\n" 10150 " , b(b)\n" 10151 " , c(c) {}", 10152 Style); 10153 verifyFormat("SomeClass::Constructor()\n" 10154 " : a(a) {\n" 10155 " foo();\n" 10156 " bar();\n" 10157 "}", 10158 Style); 10159 10160 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10161 verifyFormat("SomeClass::Constructor()\n" 10162 " : a(a)\n" 10163 " , b(b)\n" 10164 " , c(c) {\n}", 10165 Style); 10166 verifyFormat("SomeClass::Constructor()\n" 10167 " : a(a) {\n}", 10168 Style); 10169 10170 Style.ColumnLimit = 80; 10171 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10172 Style.ConstructorInitializerIndentWidth = 2; 10173 verifyFormat("SomeClass::Constructor()\n" 10174 " : a(a)\n" 10175 " , b(b)\n" 10176 " , c(c) {}", 10177 Style); 10178 10179 Style.ConstructorInitializerIndentWidth = 0; 10180 verifyFormat("SomeClass::Constructor()\n" 10181 ": a(a)\n" 10182 ", b(b)\n" 10183 ", c(c) {}", 10184 Style); 10185 10186 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10187 Style.ConstructorInitializerIndentWidth = 4; 10188 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10189 verifyFormat( 10190 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10191 Style); 10192 verifyFormat( 10193 "SomeClass::Constructor()\n" 10194 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10195 Style); 10196 Style.ConstructorInitializerIndentWidth = 4; 10197 Style.ColumnLimit = 60; 10198 verifyFormat("SomeClass::Constructor()\n" 10199 " : aaaaaaaa(aaaaaaaa)\n" 10200 " , aaaaaaaa(aaaaaaaa)\n" 10201 " , aaaaaaaa(aaaaaaaa) {}", 10202 Style); 10203 } 10204 10205 TEST_F(FormatTest, Destructors) { 10206 verifyFormat("void F(int &i) { i.~int(); }"); 10207 verifyFormat("void F(int &i) { i->~int(); }"); 10208 } 10209 10210 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10211 FormatStyle Style = getWebKitStyle(); 10212 10213 // Don't indent in outer namespaces. 10214 verifyFormat("namespace outer {\n" 10215 "int i;\n" 10216 "namespace inner {\n" 10217 " int i;\n" 10218 "} // namespace inner\n" 10219 "} // namespace outer\n" 10220 "namespace other_outer {\n" 10221 "int i;\n" 10222 "}", 10223 Style); 10224 10225 // Don't indent case labels. 10226 verifyFormat("switch (variable) {\n" 10227 "case 1:\n" 10228 "case 2:\n" 10229 " doSomething();\n" 10230 " break;\n" 10231 "default:\n" 10232 " ++variable;\n" 10233 "}", 10234 Style); 10235 10236 // Wrap before binary operators. 10237 EXPECT_EQ("void f()\n" 10238 "{\n" 10239 " if (aaaaaaaaaaaaaaaa\n" 10240 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10241 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10242 " return;\n" 10243 "}", 10244 format("void f() {\n" 10245 "if (aaaaaaaaaaaaaaaa\n" 10246 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10247 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10248 "return;\n" 10249 "}", 10250 Style)); 10251 10252 // Allow functions on a single line. 10253 verifyFormat("void f() { return; }", Style); 10254 10255 // Constructor initializers are formatted one per line with the "," on the 10256 // new line. 10257 verifyFormat("Constructor()\n" 10258 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10259 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10260 " aaaaaaaaaaaaaa)\n" 10261 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10262 "{\n" 10263 "}", 10264 Style); 10265 verifyFormat("SomeClass::Constructor()\n" 10266 " : a(a)\n" 10267 "{\n" 10268 "}", 10269 Style); 10270 EXPECT_EQ("SomeClass::Constructor()\n" 10271 " : a(a)\n" 10272 "{\n" 10273 "}", 10274 format("SomeClass::Constructor():a(a){}", Style)); 10275 verifyFormat("SomeClass::Constructor()\n" 10276 " : a(a)\n" 10277 " , b(b)\n" 10278 " , c(c)\n" 10279 "{\n" 10280 "}", 10281 Style); 10282 verifyFormat("SomeClass::Constructor()\n" 10283 " : a(a)\n" 10284 "{\n" 10285 " foo();\n" 10286 " bar();\n" 10287 "}", 10288 Style); 10289 10290 // Access specifiers should be aligned left. 10291 verifyFormat("class C {\n" 10292 "public:\n" 10293 " int i;\n" 10294 "};", 10295 Style); 10296 10297 // Do not align comments. 10298 verifyFormat("int a; // Do not\n" 10299 "double b; // align comments.", 10300 Style); 10301 10302 // Do not align operands. 10303 EXPECT_EQ("ASSERT(aaaa\n" 10304 " || bbbb);", 10305 format("ASSERT ( aaaa\n||bbbb);", Style)); 10306 10307 // Accept input's line breaks. 10308 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10309 " || bbbbbbbbbbbbbbb) {\n" 10310 " i++;\n" 10311 "}", 10312 format("if (aaaaaaaaaaaaaaa\n" 10313 "|| bbbbbbbbbbbbbbb) { i++; }", 10314 Style)); 10315 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10316 " i++;\n" 10317 "}", 10318 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10319 10320 // Don't automatically break all macro definitions (llvm.org/PR17842). 10321 verifyFormat("#define aNumber 10", Style); 10322 // However, generally keep the line breaks that the user authored. 10323 EXPECT_EQ("#define aNumber \\\n" 10324 " 10", 10325 format("#define aNumber \\\n" 10326 " 10", 10327 Style)); 10328 10329 // Keep empty and one-element array literals on a single line. 10330 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10331 " copyItems:YES];", 10332 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10333 "copyItems:YES];", 10334 Style)); 10335 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10336 " copyItems:YES];", 10337 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10338 " copyItems:YES];", 10339 Style)); 10340 // FIXME: This does not seem right, there should be more indentation before 10341 // the array literal's entries. Nested blocks have the same problem. 10342 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10343 " @\"a\",\n" 10344 " @\"a\"\n" 10345 "]\n" 10346 " copyItems:YES];", 10347 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10348 " @\"a\",\n" 10349 " @\"a\"\n" 10350 " ]\n" 10351 " copyItems:YES];", 10352 Style)); 10353 EXPECT_EQ( 10354 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10355 " copyItems:YES];", 10356 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10357 " copyItems:YES];", 10358 Style)); 10359 10360 verifyFormat("[self.a b:c c:d];", Style); 10361 EXPECT_EQ("[self.a b:c\n" 10362 " c:d];", 10363 format("[self.a b:c\n" 10364 "c:d];", 10365 Style)); 10366 } 10367 10368 TEST_F(FormatTest, FormatsLambdas) { 10369 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10370 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10371 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10372 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10373 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10374 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10375 verifyFormat("int x = f(*+[] {});"); 10376 verifyFormat("void f() {\n" 10377 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10378 "}\n"); 10379 verifyFormat("void f() {\n" 10380 " other(x.begin(), //\n" 10381 " x.end(), //\n" 10382 " [&](int, int) { return 1; });\n" 10383 "}\n"); 10384 verifyFormat("SomeFunction([]() { // A cool function...\n" 10385 " return 43;\n" 10386 "});"); 10387 EXPECT_EQ("SomeFunction([]() {\n" 10388 "#define A a\n" 10389 " return 43;\n" 10390 "});", 10391 format("SomeFunction([](){\n" 10392 "#define A a\n" 10393 "return 43;\n" 10394 "});")); 10395 verifyFormat("void f() {\n" 10396 " SomeFunction([](decltype(x), A *a) {});\n" 10397 "}"); 10398 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10399 " [](const aaaaaaaaaa &a) { return a; });"); 10400 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10401 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10402 "});"); 10403 verifyFormat("Constructor()\n" 10404 " : Field([] { // comment\n" 10405 " int i;\n" 10406 " }) {}"); 10407 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10408 " return some_parameter.size();\n" 10409 "};"); 10410 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10411 " [](const string &s) { return s; };"); 10412 verifyFormat("int i = aaaaaa ? 1 //\n" 10413 " : [] {\n" 10414 " return 2; //\n" 10415 " }();"); 10416 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10417 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10418 " return x == 2; // force break\n" 10419 " });"); 10420 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10421 " int iiiiiiiiiiii) {\n" 10422 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10423 "});", 10424 getLLVMStyleWithColumns(60)); 10425 verifyFormat("SomeFunction({[&] {\n" 10426 " // comment\n" 10427 " },\n" 10428 " [&] {\n" 10429 " // comment\n" 10430 " }});"); 10431 verifyFormat("SomeFunction({[&] {\n" 10432 " // comment\n" 10433 "}});"); 10434 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10435 " [&]() { return true; },\n" 10436 " aaaaa aaaaaaaaa);"); 10437 10438 // Lambdas with return types. 10439 verifyFormat("int c = []() -> int { return 2; }();\n"); 10440 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10441 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10442 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10443 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10444 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10445 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10446 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10447 verifyFormat("[a, a]() -> a<1> {};"); 10448 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10449 " int j) -> int {\n" 10450 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10451 "};"); 10452 verifyFormat( 10453 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10454 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10455 " return aaaaaaaaaaaaaaaaa;\n" 10456 " });", 10457 getLLVMStyleWithColumns(70)); 10458 verifyFormat("[]() //\n" 10459 " -> int {\n" 10460 " return 1; //\n" 10461 "};"); 10462 10463 // Multiple lambdas in the same parentheses change indentation rules. 10464 verifyFormat("SomeFunction(\n" 10465 " []() {\n" 10466 " int i = 42;\n" 10467 " return i;\n" 10468 " },\n" 10469 " []() {\n" 10470 " int j = 43;\n" 10471 " return j;\n" 10472 " });"); 10473 10474 // More complex introducers. 10475 verifyFormat("return [i, args...] {};"); 10476 10477 // Not lambdas. 10478 verifyFormat("constexpr char hello[]{\"hello\"};"); 10479 verifyFormat("double &operator[](int i) { return 0; }\n" 10480 "int i;"); 10481 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10482 verifyFormat("int i = a[a][a]->f();"); 10483 verifyFormat("int i = (*b)[a]->f();"); 10484 10485 // Other corner cases. 10486 verifyFormat("void f() {\n" 10487 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10488 " );\n" 10489 "}"); 10490 10491 // Lambdas created through weird macros. 10492 verifyFormat("void f() {\n" 10493 " MACRO((const AA &a) { return 1; });\n" 10494 " MACRO((AA &a) { return 1; });\n" 10495 "}"); 10496 10497 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10498 " doo_dah();\n" 10499 " doo_dah();\n" 10500 " })) {\n" 10501 "}"); 10502 verifyFormat("auto lambda = []() {\n" 10503 " int a = 2\n" 10504 "#if A\n" 10505 " + 2\n" 10506 "#endif\n" 10507 " ;\n" 10508 "};"); 10509 } 10510 10511 TEST_F(FormatTest, FormatsBlocks) { 10512 FormatStyle ShortBlocks = getLLVMStyle(); 10513 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10514 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10515 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10516 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10517 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10518 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10519 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10520 10521 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10522 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10523 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10524 10525 verifyFormat("[operation setCompletionBlock:^{\n" 10526 " [self onOperationDone];\n" 10527 "}];"); 10528 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10529 " [self onOperationDone];\n" 10530 "}]};"); 10531 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10532 " f();\n" 10533 "}];"); 10534 verifyFormat("int a = [operation block:^int(int *i) {\n" 10535 " return 1;\n" 10536 "}];"); 10537 verifyFormat("[myObject doSomethingWith:arg1\n" 10538 " aaa:^int(int *a) {\n" 10539 " return 1;\n" 10540 " }\n" 10541 " bbb:f(a * bbbbbbbb)];"); 10542 10543 verifyFormat("[operation setCompletionBlock:^{\n" 10544 " [self.delegate newDataAvailable];\n" 10545 "}];", 10546 getLLVMStyleWithColumns(60)); 10547 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10548 " NSString *path = [self sessionFilePath];\n" 10549 " if (path) {\n" 10550 " // ...\n" 10551 " }\n" 10552 "});"); 10553 verifyFormat("[[SessionService sharedService]\n" 10554 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10555 " if (window) {\n" 10556 " [self windowDidLoad:window];\n" 10557 " } else {\n" 10558 " [self errorLoadingWindow];\n" 10559 " }\n" 10560 " }];"); 10561 verifyFormat("void (^largeBlock)(void) = ^{\n" 10562 " // ...\n" 10563 "};\n", 10564 getLLVMStyleWithColumns(40)); 10565 verifyFormat("[[SessionService sharedService]\n" 10566 " loadWindowWithCompletionBlock: //\n" 10567 " ^(SessionWindow *window) {\n" 10568 " if (window) {\n" 10569 " [self windowDidLoad:window];\n" 10570 " } else {\n" 10571 " [self errorLoadingWindow];\n" 10572 " }\n" 10573 " }];", 10574 getLLVMStyleWithColumns(60)); 10575 verifyFormat("[myObject doSomethingWith:arg1\n" 10576 " firstBlock:^(Foo *a) {\n" 10577 " // ...\n" 10578 " int i;\n" 10579 " }\n" 10580 " secondBlock:^(Bar *b) {\n" 10581 " // ...\n" 10582 " int i;\n" 10583 " }\n" 10584 " thirdBlock:^Foo(Bar *b) {\n" 10585 " // ...\n" 10586 " int i;\n" 10587 " }];"); 10588 verifyFormat("[myObject doSomethingWith:arg1\n" 10589 " firstBlock:-1\n" 10590 " secondBlock:^(Bar *b) {\n" 10591 " // ...\n" 10592 " int i;\n" 10593 " }];"); 10594 10595 verifyFormat("f(^{\n" 10596 " @autoreleasepool {\n" 10597 " if (a) {\n" 10598 " g();\n" 10599 " }\n" 10600 " }\n" 10601 "});"); 10602 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10603 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10604 "};"); 10605 10606 FormatStyle FourIndent = getLLVMStyle(); 10607 FourIndent.ObjCBlockIndentWidth = 4; 10608 verifyFormat("[operation setCompletionBlock:^{\n" 10609 " [self onOperationDone];\n" 10610 "}];", 10611 FourIndent); 10612 } 10613 10614 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10615 FormatStyle ZeroColumn = getLLVMStyle(); 10616 ZeroColumn.ColumnLimit = 0; 10617 10618 verifyFormat("[[SessionService sharedService] " 10619 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10620 " if (window) {\n" 10621 " [self windowDidLoad:window];\n" 10622 " } else {\n" 10623 " [self errorLoadingWindow];\n" 10624 " }\n" 10625 "}];", 10626 ZeroColumn); 10627 EXPECT_EQ("[[SessionService sharedService]\n" 10628 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10629 " if (window) {\n" 10630 " [self windowDidLoad:window];\n" 10631 " } else {\n" 10632 " [self errorLoadingWindow];\n" 10633 " }\n" 10634 " }];", 10635 format("[[SessionService sharedService]\n" 10636 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10637 " if (window) {\n" 10638 " [self windowDidLoad:window];\n" 10639 " } else {\n" 10640 " [self errorLoadingWindow];\n" 10641 " }\n" 10642 "}];", 10643 ZeroColumn)); 10644 verifyFormat("[myObject doSomethingWith:arg1\n" 10645 " firstBlock:^(Foo *a) {\n" 10646 " // ...\n" 10647 " int i;\n" 10648 " }\n" 10649 " secondBlock:^(Bar *b) {\n" 10650 " // ...\n" 10651 " int i;\n" 10652 " }\n" 10653 " thirdBlock:^Foo(Bar *b) {\n" 10654 " // ...\n" 10655 " int i;\n" 10656 " }];", 10657 ZeroColumn); 10658 verifyFormat("f(^{\n" 10659 " @autoreleasepool {\n" 10660 " if (a) {\n" 10661 " g();\n" 10662 " }\n" 10663 " }\n" 10664 "});", 10665 ZeroColumn); 10666 verifyFormat("void (^largeBlock)(void) = ^{\n" 10667 " // ...\n" 10668 "};", 10669 ZeroColumn); 10670 10671 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10672 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10673 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10674 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10675 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10676 " int i;\n" 10677 "};", 10678 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10679 } 10680 10681 TEST_F(FormatTest, SupportsCRLF) { 10682 EXPECT_EQ("int a;\r\n" 10683 "int b;\r\n" 10684 "int c;\r\n", 10685 format("int a;\r\n" 10686 " int b;\r\n" 10687 " int c;\r\n", 10688 getLLVMStyle())); 10689 EXPECT_EQ("int a;\r\n" 10690 "int b;\r\n" 10691 "int c;\r\n", 10692 format("int a;\r\n" 10693 " int b;\n" 10694 " int c;\r\n", 10695 getLLVMStyle())); 10696 EXPECT_EQ("int a;\n" 10697 "int b;\n" 10698 "int c;\n", 10699 format("int a;\r\n" 10700 " int b;\n" 10701 " int c;\n", 10702 getLLVMStyle())); 10703 EXPECT_EQ("\"aaaaaaa \"\r\n" 10704 "\"bbbbbbb\";\r\n", 10705 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10706 EXPECT_EQ("#define A \\\r\n" 10707 " b; \\\r\n" 10708 " c; \\\r\n" 10709 " d;\r\n", 10710 format("#define A \\\r\n" 10711 " b; \\\r\n" 10712 " c; d; \r\n", 10713 getGoogleStyle())); 10714 10715 EXPECT_EQ("/*\r\n" 10716 "multi line block comments\r\n" 10717 "should not introduce\r\n" 10718 "an extra carriage return\r\n" 10719 "*/\r\n", 10720 format("/*\r\n" 10721 "multi line block comments\r\n" 10722 "should not introduce\r\n" 10723 "an extra carriage return\r\n" 10724 "*/\r\n")); 10725 } 10726 10727 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10728 verifyFormat("MY_CLASS(C) {\n" 10729 " int i;\n" 10730 " int j;\n" 10731 "};"); 10732 } 10733 10734 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10735 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10736 TwoIndent.ContinuationIndentWidth = 2; 10737 10738 EXPECT_EQ("int i =\n" 10739 " longFunction(\n" 10740 " arg);", 10741 format("int i = longFunction(arg);", TwoIndent)); 10742 10743 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10744 SixIndent.ContinuationIndentWidth = 6; 10745 10746 EXPECT_EQ("int i =\n" 10747 " longFunction(\n" 10748 " arg);", 10749 format("int i = longFunction(arg);", SixIndent)); 10750 } 10751 10752 TEST_F(FormatTest, SpacesInAngles) { 10753 FormatStyle Spaces = getLLVMStyle(); 10754 Spaces.SpacesInAngles = true; 10755 10756 verifyFormat("static_cast< int >(arg);", Spaces); 10757 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10758 verifyFormat("f< int, float >();", Spaces); 10759 verifyFormat("template <> g() {}", Spaces); 10760 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10761 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10762 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10763 Spaces); 10764 10765 Spaces.Standard = FormatStyle::LS_Cpp03; 10766 Spaces.SpacesInAngles = true; 10767 verifyFormat("A< A< int > >();", Spaces); 10768 10769 Spaces.SpacesInAngles = false; 10770 verifyFormat("A<A<int> >();", Spaces); 10771 10772 Spaces.Standard = FormatStyle::LS_Cpp11; 10773 Spaces.SpacesInAngles = true; 10774 verifyFormat("A< A< int > >();", Spaces); 10775 10776 Spaces.SpacesInAngles = false; 10777 verifyFormat("A<A<int>>();", Spaces); 10778 } 10779 10780 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10781 FormatStyle Style = getLLVMStyle(); 10782 Style.SpaceAfterTemplateKeyword = false; 10783 verifyFormat("template<int> void foo();", Style); 10784 } 10785 10786 TEST_F(FormatTest, TripleAngleBrackets) { 10787 verifyFormat("f<<<1, 1>>>();"); 10788 verifyFormat("f<<<1, 1, 1, s>>>();"); 10789 verifyFormat("f<<<a, b, c, d>>>();"); 10790 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10791 verifyFormat("f<param><<<1, 1>>>();"); 10792 verifyFormat("f<1><<<1, 1>>>();"); 10793 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10794 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10795 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10796 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10797 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10798 } 10799 10800 TEST_F(FormatTest, MergeLessLessAtEnd) { 10801 verifyFormat("<<"); 10802 EXPECT_EQ("< < <", format("\\\n<<<")); 10803 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10804 "aaallvm::outs() <<"); 10805 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10806 "aaaallvm::outs()\n <<"); 10807 } 10808 10809 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10810 std::string code = "#if A\n" 10811 "#if B\n" 10812 "a.\n" 10813 "#endif\n" 10814 " a = 1;\n" 10815 "#else\n" 10816 "#endif\n" 10817 "#if C\n" 10818 "#else\n" 10819 "#endif\n"; 10820 EXPECT_EQ(code, format(code)); 10821 } 10822 10823 TEST_F(FormatTest, HandleConflictMarkers) { 10824 // Git/SVN conflict markers. 10825 EXPECT_EQ("int a;\n" 10826 "void f() {\n" 10827 " callme(some(parameter1,\n" 10828 "<<<<<<< text by the vcs\n" 10829 " parameter2),\n" 10830 "||||||| text by the vcs\n" 10831 " parameter2),\n" 10832 " parameter3,\n" 10833 "======= text by the vcs\n" 10834 " parameter2, parameter3),\n" 10835 ">>>>>>> text by the vcs\n" 10836 " otherparameter);\n", 10837 format("int a;\n" 10838 "void f() {\n" 10839 " callme(some(parameter1,\n" 10840 "<<<<<<< text by the vcs\n" 10841 " parameter2),\n" 10842 "||||||| text by the vcs\n" 10843 " parameter2),\n" 10844 " parameter3,\n" 10845 "======= text by the vcs\n" 10846 " parameter2,\n" 10847 " parameter3),\n" 10848 ">>>>>>> text by the vcs\n" 10849 " otherparameter);\n")); 10850 10851 // Perforce markers. 10852 EXPECT_EQ("void f() {\n" 10853 " function(\n" 10854 ">>>> text by the vcs\n" 10855 " parameter,\n" 10856 "==== text by the vcs\n" 10857 " parameter,\n" 10858 "==== text by the vcs\n" 10859 " parameter,\n" 10860 "<<<< text by the vcs\n" 10861 " parameter);\n", 10862 format("void f() {\n" 10863 " function(\n" 10864 ">>>> text by the vcs\n" 10865 " parameter,\n" 10866 "==== text by the vcs\n" 10867 " parameter,\n" 10868 "==== text by the vcs\n" 10869 " parameter,\n" 10870 "<<<< text by the vcs\n" 10871 " parameter);\n")); 10872 10873 EXPECT_EQ("<<<<<<<\n" 10874 "|||||||\n" 10875 "=======\n" 10876 ">>>>>>>", 10877 format("<<<<<<<\n" 10878 "|||||||\n" 10879 "=======\n" 10880 ">>>>>>>")); 10881 10882 EXPECT_EQ("<<<<<<<\n" 10883 "|||||||\n" 10884 "int i;\n" 10885 "=======\n" 10886 ">>>>>>>", 10887 format("<<<<<<<\n" 10888 "|||||||\n" 10889 "int i;\n" 10890 "=======\n" 10891 ">>>>>>>")); 10892 10893 // FIXME: Handle parsing of macros around conflict markers correctly: 10894 EXPECT_EQ("#define Macro \\\n" 10895 "<<<<<<<\n" 10896 "Something \\\n" 10897 "|||||||\n" 10898 "Else \\\n" 10899 "=======\n" 10900 "Other \\\n" 10901 ">>>>>>>\n" 10902 " End int i;\n", 10903 format("#define Macro \\\n" 10904 "<<<<<<<\n" 10905 " Something \\\n" 10906 "|||||||\n" 10907 " Else \\\n" 10908 "=======\n" 10909 " Other \\\n" 10910 ">>>>>>>\n" 10911 " End\n" 10912 "int i;\n")); 10913 } 10914 10915 TEST_F(FormatTest, DisableRegions) { 10916 EXPECT_EQ("int i;\n" 10917 "// clang-format off\n" 10918 " int j;\n" 10919 "// clang-format on\n" 10920 "int k;", 10921 format(" int i;\n" 10922 " // clang-format off\n" 10923 " int j;\n" 10924 " // clang-format on\n" 10925 " int k;")); 10926 EXPECT_EQ("int i;\n" 10927 "/* clang-format off */\n" 10928 " int j;\n" 10929 "/* clang-format on */\n" 10930 "int k;", 10931 format(" int i;\n" 10932 " /* clang-format off */\n" 10933 " int j;\n" 10934 " /* clang-format on */\n" 10935 " int k;")); 10936 } 10937 10938 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10939 format("? ) ="); 10940 verifyNoCrash("#define a\\\n /**/}"); 10941 } 10942 10943 TEST_F(FormatTest, FormatsTableGenCode) { 10944 FormatStyle Style = getLLVMStyle(); 10945 Style.Language = FormatStyle::LK_TableGen; 10946 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10947 } 10948 10949 TEST_F(FormatTest, ArrayOfTemplates) { 10950 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10951 format("auto a = new unique_ptr<int > [ 10];")); 10952 10953 FormatStyle Spaces = getLLVMStyle(); 10954 Spaces.SpacesInSquareBrackets = true; 10955 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10956 format("auto a = new unique_ptr<int > [10];", Spaces)); 10957 } 10958 10959 TEST_F(FormatTest, ArrayAsTemplateType) { 10960 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10961 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10962 10963 FormatStyle Spaces = getLLVMStyle(); 10964 Spaces.SpacesInSquareBrackets = true; 10965 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10966 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10967 } 10968 10969 TEST(FormatStyle, GetStyleOfFile) { 10970 vfs::InMemoryFileSystem FS; 10971 // Test 1: format file in the same directory. 10972 ASSERT_TRUE( 10973 FS.addFile("/a/.clang-format", 0, 10974 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10975 ASSERT_TRUE( 10976 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10977 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10978 ASSERT_TRUE((bool)Style1); 10979 ASSERT_EQ(*Style1, getLLVMStyle()); 10980 10981 // Test 2: fallback to default. 10982 ASSERT_TRUE( 10983 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10984 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10985 ASSERT_TRUE((bool)Style2); 10986 ASSERT_EQ(*Style2, getMozillaStyle()); 10987 10988 // Test 3: format file in parent directory. 10989 ASSERT_TRUE( 10990 FS.addFile("/c/.clang-format", 0, 10991 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10992 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10993 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10994 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10995 ASSERT_TRUE((bool)Style3); 10996 ASSERT_EQ(*Style3, getGoogleStyle()); 10997 10998 // Test 4: error on invalid fallback style 10999 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11000 ASSERT_FALSE((bool)Style4); 11001 llvm::consumeError(Style4.takeError()); 11002 11003 // Test 5: error on invalid yaml on command line 11004 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11005 ASSERT_FALSE((bool)Style5); 11006 llvm::consumeError(Style5.takeError()); 11007 11008 // Test 6: error on invalid style 11009 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11010 ASSERT_FALSE((bool)Style6); 11011 llvm::consumeError(Style6.takeError()); 11012 11013 // Test 7: found config file, error on parsing it 11014 ASSERT_TRUE( 11015 FS.addFile("/d/.clang-format", 0, 11016 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11017 "InvalidKey: InvalidValue"))); 11018 ASSERT_TRUE( 11019 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11020 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11021 ASSERT_FALSE((bool)Style7); 11022 llvm::consumeError(Style7.takeError()); 11023 } 11024 11025 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11026 // Column limit is 20. 11027 std::string Code = "Type *a =\n" 11028 " new Type();\n" 11029 "g(iiiii, 0, jjjjj,\n" 11030 " 0, kkkkk, 0, mm);\n" 11031 "int bad = format ;"; 11032 std::string Expected = "auto a = new Type();\n" 11033 "g(iiiii, nullptr,\n" 11034 " jjjjj, nullptr,\n" 11035 " kkkkk, nullptr,\n" 11036 " mm);\n" 11037 "int bad = format ;"; 11038 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11039 tooling::Replacements Replaces = toReplacements( 11040 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11041 "auto "), 11042 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11043 "nullptr"), 11044 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11045 "nullptr"), 11046 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11047 "nullptr")}); 11048 11049 format::FormatStyle Style = format::getLLVMStyle(); 11050 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11051 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11052 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11053 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11054 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11055 EXPECT_TRUE(static_cast<bool>(Result)); 11056 EXPECT_EQ(Expected, *Result); 11057 } 11058 11059 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11060 std::string Code = "#include \"a.h\"\n" 11061 "#include \"c.h\"\n" 11062 "\n" 11063 "int main() {\n" 11064 " return 0;\n" 11065 "}"; 11066 std::string Expected = "#include \"a.h\"\n" 11067 "#include \"b.h\"\n" 11068 "#include \"c.h\"\n" 11069 "\n" 11070 "int main() {\n" 11071 " return 0;\n" 11072 "}"; 11073 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11074 tooling::Replacements Replaces = toReplacements( 11075 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11076 "#include \"b.h\"\n")}); 11077 11078 format::FormatStyle Style = format::getLLVMStyle(); 11079 Style.SortIncludes = true; 11080 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11081 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11082 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11083 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11084 EXPECT_TRUE(static_cast<bool>(Result)); 11085 EXPECT_EQ(Expected, *Result); 11086 } 11087 11088 TEST_F(FormatTest, AllignTrailingComments) { 11089 EXPECT_EQ("#define MACRO(V) \\\n" 11090 " V(Rt2) /* one more char */ \\\n" 11091 " V(Rs) /* than here */ \\\n" 11092 "/* comment 3 */\n", 11093 format("#define MACRO(V)\\\n" 11094 "V(Rt2) /* one more char */ \\\n" 11095 "V(Rs) /* than here */ \\\n" 11096 "/* comment 3 */ \\\n", 11097 getLLVMStyleWithColumns(40))); 11098 } 11099 } // end namespace 11100 } // end namespace format 11101 } // end namespace clang 11102