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 && fffff(\n" 3357 "#define A Just forcing a new line\n" 3358 " ddd);"); 3359 } 3360 3361 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3362 verifyFormat( 3363 "bool aaaaaaa =\n" 3364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3365 " bbbbbbbb();"); 3366 verifyFormat( 3367 "bool aaaaaaa =\n" 3368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3369 " bbbbbbbb();"); 3370 3371 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3373 " ccccccccc == ddddddddddd;"); 3374 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3375 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3376 " ccccccccc == ddddddddddd;"); 3377 verifyFormat( 3378 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3380 " ccccccccc == ddddddddddd;"); 3381 3382 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3383 " aaaaaa) &&\n" 3384 " bbbbbb && cccccc;"); 3385 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3386 " aaaaaa) >>\n" 3387 " bbbbbb;"); 3388 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3389 " SourceMgr.getSpellingColumnNumber(\n" 3390 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3391 " 1);"); 3392 3393 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3394 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3395 " cccccc) {\n}"); 3396 verifyFormat("b = a &&\n" 3397 " // Comment\n" 3398 " b.c && d;"); 3399 3400 // If the LHS of a comparison is not a binary expression itself, the 3401 // additional linebreak confuses many people. 3402 verifyFormat( 3403 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3405 "}"); 3406 verifyFormat( 3407 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3409 "}"); 3410 verifyFormat( 3411 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3413 "}"); 3414 // Even explicit parentheses stress the precedence enough to make the 3415 // additional break unnecessary. 3416 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3418 "}"); 3419 // This cases is borderline, but with the indentation it is still readable. 3420 verifyFormat( 3421 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3422 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3424 "}", 3425 getLLVMStyleWithColumns(75)); 3426 3427 // If the LHS is a binary expression, we should still use the additional break 3428 // as otherwise the formatting hides the operator precedence. 3429 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3431 " 5) {\n" 3432 "}"); 3433 3434 FormatStyle OnePerLine = getLLVMStyle(); 3435 OnePerLine.BinPackParameters = false; 3436 verifyFormat( 3437 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3440 OnePerLine); 3441 } 3442 3443 TEST_F(FormatTest, ExpressionIndentation) { 3444 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3448 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3449 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3452 " ccccccccccccccccccccccccccccccccccccccccc;"); 3453 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3456 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3457 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3460 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3461 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3464 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3465 verifyFormat("if () {\n" 3466 "} else if (aaaaa &&\n" 3467 " bbbbb > // break\n" 3468 " ccccc) {\n" 3469 "}"); 3470 3471 // Presence of a trailing comment used to change indentation of b. 3472 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3473 " b;\n" 3474 "return aaaaaaaaaaaaaaaaaaa +\n" 3475 " b; //", 3476 getLLVMStyleWithColumns(30)); 3477 } 3478 3479 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3480 // Not sure what the best system is here. Like this, the LHS can be found 3481 // immediately above an operator (everything with the same or a higher 3482 // indent). The RHS is aligned right of the operator and so compasses 3483 // everything until something with the same indent as the operator is found. 3484 // FIXME: Is this a good system? 3485 FormatStyle Style = getLLVMStyle(); 3486 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3487 verifyFormat( 3488 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3489 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3490 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3491 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3492 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3493 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3494 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3495 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3496 " > ccccccccccccccccccccccccccccccccccccccccc;", 3497 Style); 3498 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3499 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3500 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3501 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3502 Style); 3503 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3504 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3505 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3506 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3507 Style); 3508 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3509 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3510 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3511 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3512 Style); 3513 verifyFormat("if () {\n" 3514 "} else if (aaaaa\n" 3515 " && bbbbb // break\n" 3516 " > ccccc) {\n" 3517 "}", 3518 Style); 3519 verifyFormat("return (a)\n" 3520 " // comment\n" 3521 " + b;", 3522 Style); 3523 verifyFormat( 3524 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3525 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3526 " + cc;", 3527 Style); 3528 3529 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3530 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3531 Style); 3532 3533 // Forced by comments. 3534 verifyFormat( 3535 "unsigned ContentSize =\n" 3536 " sizeof(int16_t) // DWARF ARange version number\n" 3537 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3538 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3539 " + sizeof(int8_t); // Segment Size (in bytes)"); 3540 3541 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3542 " == boost::fusion::at_c<1>(iiii).second;", 3543 Style); 3544 3545 Style.ColumnLimit = 60; 3546 verifyFormat("zzzzzzzzzz\n" 3547 " = bbbbbbbbbbbbbbbbb\n" 3548 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3549 Style); 3550 } 3551 3552 TEST_F(FormatTest, NoOperandAlignment) { 3553 FormatStyle Style = getLLVMStyle(); 3554 Style.AlignOperands = false; 3555 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3556 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3557 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3558 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3559 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3560 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3561 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3562 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3563 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3564 " > ccccccccccccccccccccccccccccccccccccccccc;", 3565 Style); 3566 3567 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3568 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3569 " + cc;", 3570 Style); 3571 verifyFormat("int a = aa\n" 3572 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3573 " * cccccccccccccccccccccccccccccccccccc;", 3574 Style); 3575 3576 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3577 verifyFormat("return (a > b\n" 3578 " // comment1\n" 3579 " // comment2\n" 3580 " || c);", 3581 Style); 3582 } 3583 3584 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3585 FormatStyle Style = getLLVMStyle(); 3586 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3587 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3589 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3590 Style); 3591 } 3592 3593 TEST_F(FormatTest, ConstructorInitializers) { 3594 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3595 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3596 getLLVMStyleWithColumns(45)); 3597 verifyFormat("Constructor()\n" 3598 " : Inttializer(FitsOnTheLine) {}", 3599 getLLVMStyleWithColumns(44)); 3600 verifyFormat("Constructor()\n" 3601 " : Inttializer(FitsOnTheLine) {}", 3602 getLLVMStyleWithColumns(43)); 3603 3604 verifyFormat("template <typename T>\n" 3605 "Constructor() : Initializer(FitsOnTheLine) {}", 3606 getLLVMStyleWithColumns(45)); 3607 3608 verifyFormat( 3609 "SomeClass::Constructor()\n" 3610 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3611 3612 verifyFormat( 3613 "SomeClass::Constructor()\n" 3614 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3615 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3616 verifyFormat( 3617 "SomeClass::Constructor()\n" 3618 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3619 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3620 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3621 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3622 " : aaaaaaaaaa(aaaaaa) {}"); 3623 3624 verifyFormat("Constructor()\n" 3625 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3626 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3627 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3628 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3629 3630 verifyFormat("Constructor()\n" 3631 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3633 3634 verifyFormat("Constructor(int Parameter = 0)\n" 3635 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3636 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3637 verifyFormat("Constructor()\n" 3638 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3639 "}", 3640 getLLVMStyleWithColumns(60)); 3641 verifyFormat("Constructor()\n" 3642 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3643 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3644 3645 // Here a line could be saved by splitting the second initializer onto two 3646 // lines, but that is not desirable. 3647 verifyFormat("Constructor()\n" 3648 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3649 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3650 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3651 3652 FormatStyle OnePerLine = getLLVMStyle(); 3653 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3654 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3655 verifyFormat("SomeClass::Constructor()\n" 3656 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3657 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3658 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3659 OnePerLine); 3660 verifyFormat("SomeClass::Constructor()\n" 3661 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3662 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3663 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3664 OnePerLine); 3665 verifyFormat("MyClass::MyClass(int var)\n" 3666 " : some_var_(var), // 4 space indent\n" 3667 " some_other_var_(var + 1) { // lined up\n" 3668 "}", 3669 OnePerLine); 3670 verifyFormat("Constructor()\n" 3671 " : aaaaa(aaaaaa),\n" 3672 " aaaaa(aaaaaa),\n" 3673 " aaaaa(aaaaaa),\n" 3674 " aaaaa(aaaaaa),\n" 3675 " aaaaa(aaaaaa) {}", 3676 OnePerLine); 3677 verifyFormat("Constructor()\n" 3678 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3679 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3680 OnePerLine); 3681 OnePerLine.BinPackParameters = false; 3682 verifyFormat( 3683 "Constructor()\n" 3684 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3685 " aaaaaaaaaaa().aaa(),\n" 3686 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3687 OnePerLine); 3688 OnePerLine.ColumnLimit = 60; 3689 verifyFormat("Constructor()\n" 3690 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3691 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3692 OnePerLine); 3693 3694 EXPECT_EQ("Constructor()\n" 3695 " : // Comment forcing unwanted break.\n" 3696 " aaaa(aaaa) {}", 3697 format("Constructor() :\n" 3698 " // Comment forcing unwanted break.\n" 3699 " aaaa(aaaa) {}")); 3700 } 3701 3702 TEST_F(FormatTest, MemoizationTests) { 3703 // This breaks if the memoization lookup does not take \c Indent and 3704 // \c LastSpace into account. 3705 verifyFormat( 3706 "extern CFRunLoopTimerRef\n" 3707 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3708 " CFTimeInterval interval, CFOptionFlags flags,\n" 3709 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3710 " CFRunLoopTimerContext *context) {}"); 3711 3712 // Deep nesting somewhat works around our memoization. 3713 verifyFormat( 3714 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3715 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3716 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3717 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3718 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3719 getLLVMStyleWithColumns(65)); 3720 verifyFormat( 3721 "aaaaa(\n" 3722 " aaaaa,\n" 3723 " aaaaa(\n" 3724 " aaaaa,\n" 3725 " aaaaa(\n" 3726 " aaaaa,\n" 3727 " aaaaa(\n" 3728 " aaaaa,\n" 3729 " aaaaa(\n" 3730 " aaaaa,\n" 3731 " aaaaa(\n" 3732 " aaaaa,\n" 3733 " aaaaa(\n" 3734 " aaaaa,\n" 3735 " aaaaa(\n" 3736 " aaaaa,\n" 3737 " aaaaa(\n" 3738 " aaaaa,\n" 3739 " aaaaa(\n" 3740 " aaaaa,\n" 3741 " aaaaa(\n" 3742 " aaaaa,\n" 3743 " aaaaa(\n" 3744 " aaaaa,\n" 3745 " aaaaa))))))))))));", 3746 getLLVMStyleWithColumns(65)); 3747 verifyFormat( 3748 "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" 3749 " a),\n" 3750 " a),\n" 3751 " a),\n" 3752 " a),\n" 3753 " a),\n" 3754 " a),\n" 3755 " a),\n" 3756 " a),\n" 3757 " a),\n" 3758 " a),\n" 3759 " a),\n" 3760 " a),\n" 3761 " a),\n" 3762 " a),\n" 3763 " a),\n" 3764 " a),\n" 3765 " a)", 3766 getLLVMStyleWithColumns(65)); 3767 3768 // This test takes VERY long when memoization is broken. 3769 FormatStyle OnePerLine = getLLVMStyle(); 3770 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3771 OnePerLine.BinPackParameters = false; 3772 std::string input = "Constructor()\n" 3773 " : aaaa(a,\n"; 3774 for (unsigned i = 0, e = 80; i != e; ++i) { 3775 input += " a,\n"; 3776 } 3777 input += " a) {}"; 3778 verifyFormat(input, OnePerLine); 3779 } 3780 3781 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3782 verifyFormat( 3783 "void f() {\n" 3784 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3785 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3786 " f();\n" 3787 "}"); 3788 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3789 " Intervals[i - 1].getRange().getLast()) {\n}"); 3790 } 3791 3792 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3793 // Principially, we break function declarations in a certain order: 3794 // 1) break amongst arguments. 3795 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3796 " Cccccccccccccc cccccccccccccc);"); 3797 verifyFormat("template <class TemplateIt>\n" 3798 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3799 " TemplateIt *stop) {}"); 3800 3801 // 2) break after return type. 3802 verifyFormat( 3803 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3804 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3805 getGoogleStyle()); 3806 3807 // 3) break after (. 3808 verifyFormat( 3809 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3810 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3811 getGoogleStyle()); 3812 3813 // 4) break before after nested name specifiers. 3814 verifyFormat( 3815 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3816 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3817 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3818 getGoogleStyle()); 3819 3820 // However, there are exceptions, if a sufficient amount of lines can be 3821 // saved. 3822 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3823 // more adjusting. 3824 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3825 " Cccccccccccccc cccccccccc,\n" 3826 " Cccccccccccccc cccccccccc,\n" 3827 " Cccccccccccccc cccccccccc,\n" 3828 " Cccccccccccccc cccccccccc);"); 3829 verifyFormat( 3830 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3831 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3832 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3833 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3834 getGoogleStyle()); 3835 verifyFormat( 3836 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3837 " Cccccccccccccc cccccccccc,\n" 3838 " Cccccccccccccc cccccccccc,\n" 3839 " Cccccccccccccc cccccccccc,\n" 3840 " Cccccccccccccc cccccccccc,\n" 3841 " Cccccccccccccc cccccccccc,\n" 3842 " Cccccccccccccc cccccccccc);"); 3843 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3844 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3845 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3846 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3847 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3848 3849 // Break after multi-line parameters. 3850 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3853 " bbbb bbbb);"); 3854 verifyFormat("void SomeLoooooooooooongFunction(\n" 3855 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3856 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3857 " int bbbbbbbbbbbbb);"); 3858 3859 // Treat overloaded operators like other functions. 3860 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3861 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3862 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3863 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3864 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3865 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3866 verifyGoogleFormat( 3867 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3868 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3869 verifyGoogleFormat( 3870 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3871 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3872 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3873 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3874 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3875 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3876 verifyGoogleFormat( 3877 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3878 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3879 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3880 verifyGoogleFormat( 3881 "template <typename T>\n" 3882 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3883 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3884 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3885 3886 FormatStyle Style = getLLVMStyle(); 3887 Style.PointerAlignment = FormatStyle::PAS_Left; 3888 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3889 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3890 Style); 3891 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3893 Style); 3894 } 3895 3896 TEST_F(FormatTest, TrailingReturnType) { 3897 verifyFormat("auto foo() -> int;\n"); 3898 verifyFormat("struct S {\n" 3899 " auto bar() const -> int;\n" 3900 "};"); 3901 verifyFormat("template <size_t Order, typename T>\n" 3902 "auto load_img(const std::string &filename)\n" 3903 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3904 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3905 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3906 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3907 verifyFormat("template <typename T>\n" 3908 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3909 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3910 3911 // Not trailing return types. 3912 verifyFormat("void f() { auto a = b->c(); }"); 3913 } 3914 3915 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3916 // Avoid breaking before trailing 'const' or other trailing annotations, if 3917 // they are not function-like. 3918 FormatStyle Style = getGoogleStyle(); 3919 Style.ColumnLimit = 47; 3920 verifyFormat("void someLongFunction(\n" 3921 " int someLoooooooooooooongParameter) const {\n}", 3922 getLLVMStyleWithColumns(47)); 3923 verifyFormat("LoooooongReturnType\n" 3924 "someLoooooooongFunction() const {}", 3925 getLLVMStyleWithColumns(47)); 3926 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3927 " const {}", 3928 Style); 3929 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3930 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3931 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3932 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3933 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3934 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3935 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3936 " aaaaaaaaaaa aaaaa) const override;"); 3937 verifyGoogleFormat( 3938 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3939 " const override;"); 3940 3941 // Even if the first parameter has to be wrapped. 3942 verifyFormat("void someLongFunction(\n" 3943 " int someLongParameter) const {}", 3944 getLLVMStyleWithColumns(46)); 3945 verifyFormat("void someLongFunction(\n" 3946 " int someLongParameter) const {}", 3947 Style); 3948 verifyFormat("void someLongFunction(\n" 3949 " int someLongParameter) override {}", 3950 Style); 3951 verifyFormat("void someLongFunction(\n" 3952 " int someLongParameter) OVERRIDE {}", 3953 Style); 3954 verifyFormat("void someLongFunction(\n" 3955 " int someLongParameter) final {}", 3956 Style); 3957 verifyFormat("void someLongFunction(\n" 3958 " int someLongParameter) FINAL {}", 3959 Style); 3960 verifyFormat("void someLongFunction(\n" 3961 " int parameter) const override {}", 3962 Style); 3963 3964 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3965 verifyFormat("void someLongFunction(\n" 3966 " int someLongParameter) const\n" 3967 "{\n" 3968 "}", 3969 Style); 3970 3971 // Unless these are unknown annotations. 3972 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3973 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3974 " LONG_AND_UGLY_ANNOTATION;"); 3975 3976 // Breaking before function-like trailing annotations is fine to keep them 3977 // close to their arguments. 3978 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3979 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3980 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3981 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3982 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3983 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3984 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3985 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3986 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3987 3988 verifyFormat( 3989 "void aaaaaaaaaaaaaaaaaa()\n" 3990 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3991 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3992 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3993 " __attribute__((unused));"); 3994 verifyGoogleFormat( 3995 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3996 " GUARDED_BY(aaaaaaaaaaaa);"); 3997 verifyGoogleFormat( 3998 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3999 " GUARDED_BY(aaaaaaaaaaaa);"); 4000 verifyGoogleFormat( 4001 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4002 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4003 verifyGoogleFormat( 4004 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4005 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4006 } 4007 4008 TEST_F(FormatTest, FunctionAnnotations) { 4009 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4010 "int OldFunction(const string ¶meter) {}"); 4011 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4012 "string OldFunction(const string ¶meter) {}"); 4013 verifyFormat("template <typename T>\n" 4014 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4015 "string OldFunction(const string ¶meter) {}"); 4016 4017 // Not function annotations. 4018 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4019 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4020 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4021 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4022 verifyFormat("MACRO(abc).function() // wrap\n" 4023 " << abc;"); 4024 verifyFormat("MACRO(abc)->function() // wrap\n" 4025 " << abc;"); 4026 verifyFormat("MACRO(abc)::function() // wrap\n" 4027 " << abc;"); 4028 } 4029 4030 TEST_F(FormatTest, BreaksDesireably) { 4031 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4032 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4033 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4034 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4036 "}"); 4037 4038 verifyFormat( 4039 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4041 4042 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4045 4046 verifyFormat( 4047 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4049 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4051 4052 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4053 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4054 4055 verifyFormat( 4056 "void f() {\n" 4057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4059 "}"); 4060 verifyFormat( 4061 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4063 verifyFormat( 4064 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4066 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4069 4070 // Indent consistently independent of call expression and unary operator. 4071 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4072 " dddddddddddddddddddddddddddddd));"); 4073 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4074 " dddddddddddddddddddddddddddddd));"); 4075 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4076 " dddddddddddddddddddddddddddddd));"); 4077 4078 // This test case breaks on an incorrect memoization, i.e. an optimization not 4079 // taking into account the StopAt value. 4080 verifyFormat( 4081 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4082 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4083 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4084 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4085 4086 verifyFormat("{\n {\n {\n" 4087 " Annotation.SpaceRequiredBefore =\n" 4088 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4089 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4090 " }\n }\n}"); 4091 4092 // Break on an outer level if there was a break on an inner level. 4093 EXPECT_EQ("f(g(h(a, // comment\n" 4094 " b, c),\n" 4095 " d, e),\n" 4096 " x, y);", 4097 format("f(g(h(a, // comment\n" 4098 " b, c), d, e), x, y);")); 4099 4100 // Prefer breaking similar line breaks. 4101 verifyFormat( 4102 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4103 " NSTrackingMouseEnteredAndExited |\n" 4104 " NSTrackingActiveAlways;"); 4105 } 4106 4107 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4108 FormatStyle NoBinPacking = getGoogleStyle(); 4109 NoBinPacking.BinPackParameters = false; 4110 NoBinPacking.BinPackArguments = true; 4111 verifyFormat("void f() {\n" 4112 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4114 "}", 4115 NoBinPacking); 4116 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4117 " int aaaaaaaaaaaaaaaaaaaa,\n" 4118 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4119 NoBinPacking); 4120 4121 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4122 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4123 " vector<int> bbbbbbbbbbbbbbb);", 4124 NoBinPacking); 4125 // FIXME: This behavior difference is probably not wanted. However, currently 4126 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4127 // template arguments from BreakBeforeParameter being set because of the 4128 // one-per-line formatting. 4129 verifyFormat( 4130 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4131 " aaaaaaaaaa> aaaaaaaaaa);", 4132 NoBinPacking); 4133 verifyFormat( 4134 "void fffffffffff(\n" 4135 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4136 " aaaaaaaaaa);"); 4137 } 4138 4139 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4140 FormatStyle NoBinPacking = getGoogleStyle(); 4141 NoBinPacking.BinPackParameters = false; 4142 NoBinPacking.BinPackArguments = false; 4143 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4144 " aaaaaaaaaaaaaaaaaaaa,\n" 4145 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4146 NoBinPacking); 4147 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4148 " aaaaaaaaaaaaa,\n" 4149 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4150 NoBinPacking); 4151 verifyFormat( 4152 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4153 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4155 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4157 NoBinPacking); 4158 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4159 " .aaaaaaaaaaaaaaaaaa();", 4160 NoBinPacking); 4161 verifyFormat("void f() {\n" 4162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4163 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4164 "}", 4165 NoBinPacking); 4166 4167 verifyFormat( 4168 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4169 " aaaaaaaaaaaa,\n" 4170 " aaaaaaaaaaaa);", 4171 NoBinPacking); 4172 verifyFormat( 4173 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4174 " ddddddddddddddddddddddddddddd),\n" 4175 " test);", 4176 NoBinPacking); 4177 4178 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4179 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4180 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4181 " aaaaaaaaaaaaaaaaaa;", 4182 NoBinPacking); 4183 verifyFormat("a(\"a\"\n" 4184 " \"a\",\n" 4185 " a);"); 4186 4187 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4188 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4189 " aaaaaaaaa,\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4191 NoBinPacking); 4192 verifyFormat( 4193 "void f() {\n" 4194 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4195 " .aaaaaaa();\n" 4196 "}", 4197 NoBinPacking); 4198 verifyFormat( 4199 "template <class SomeType, class SomeOtherType>\n" 4200 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4201 NoBinPacking); 4202 } 4203 4204 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4205 FormatStyle Style = getLLVMStyleWithColumns(15); 4206 Style.ExperimentalAutoDetectBinPacking = true; 4207 EXPECT_EQ("aaa(aaaa,\n" 4208 " aaaa,\n" 4209 " aaaa);\n" 4210 "aaa(aaaa,\n" 4211 " aaaa,\n" 4212 " aaaa);", 4213 format("aaa(aaaa,\n" // one-per-line 4214 " aaaa,\n" 4215 " aaaa );\n" 4216 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4217 Style)); 4218 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4219 " aaaa);\n" 4220 "aaa(aaaa, aaaa,\n" 4221 " aaaa);", 4222 format("aaa(aaaa, aaaa,\n" // bin-packed 4223 " aaaa );\n" 4224 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4225 Style)); 4226 } 4227 4228 TEST_F(FormatTest, FormatsBuilderPattern) { 4229 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4230 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4231 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4232 " .StartsWith(\".init\", ORDER_INIT)\n" 4233 " .StartsWith(\".fini\", ORDER_FINI)\n" 4234 " .StartsWith(\".hash\", ORDER_HASH)\n" 4235 " .Default(ORDER_TEXT);\n"); 4236 4237 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4238 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4239 verifyFormat( 4240 "aaaaaaa->aaaaaaa\n" 4241 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4243 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4244 verifyFormat( 4245 "aaaaaaa->aaaaaaa\n" 4246 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4247 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4248 verifyFormat( 4249 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4250 " aaaaaaaaaaaaaa);"); 4251 verifyFormat( 4252 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4253 " aaaaaa->aaaaaaaaaaaa()\n" 4254 " ->aaaaaaaaaaaaaaaa(\n" 4255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4256 " ->aaaaaaaaaaaaaaaaa();"); 4257 verifyGoogleFormat( 4258 "void f() {\n" 4259 " someo->Add((new util::filetools::Handler(dir))\n" 4260 " ->OnEvent1(NewPermanentCallback(\n" 4261 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4262 " ->OnEvent2(NewPermanentCallback(\n" 4263 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4264 " ->OnEvent3(NewPermanentCallback(\n" 4265 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4266 " ->OnEvent5(NewPermanentCallback(\n" 4267 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4268 " ->OnEvent6(NewPermanentCallback(\n" 4269 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4270 "}"); 4271 4272 verifyFormat( 4273 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4274 verifyFormat("aaaaaaaaaaaaaaa()\n" 4275 " .aaaaaaaaaaaaaaa()\n" 4276 " .aaaaaaaaaaaaaaa()\n" 4277 " .aaaaaaaaaaaaaaa()\n" 4278 " .aaaaaaaaaaaaaaa();"); 4279 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4280 " .aaaaaaaaaaaaaaa()\n" 4281 " .aaaaaaaaaaaaaaa()\n" 4282 " .aaaaaaaaaaaaaaa();"); 4283 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4284 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4285 " .aaaaaaaaaaaaaaa();"); 4286 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4287 " ->aaaaaaaaaaaaaae(0)\n" 4288 " ->aaaaaaaaaaaaaaa();"); 4289 4290 // Don't linewrap after very short segments. 4291 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4292 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4293 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4294 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4295 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4296 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4297 verifyFormat("aaa()\n" 4298 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4299 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4300 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4301 4302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4303 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4304 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4305 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4308 4309 // Prefer not to break after empty parentheses. 4310 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4311 " First->LastNewlineOffset);"); 4312 4313 // Prefer not to create "hanging" indents. 4314 verifyFormat( 4315 "return !soooooooooooooome_map\n" 4316 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4317 " .second;"); 4318 verifyFormat( 4319 "return aaaaaaaaaaaaaaaa\n" 4320 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4321 " .aaaa(aaaaaaaaaaaaaa);"); 4322 // No hanging indent here. 4323 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4325 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4327 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4328 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4329 getLLVMStyleWithColumns(60)); 4330 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4331 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4332 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4333 getLLVMStyleWithColumns(59)); 4334 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4336 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4337 } 4338 4339 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4340 verifyFormat( 4341 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4342 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4343 verifyFormat( 4344 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4345 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4346 4347 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4348 " ccccccccccccccccccccccccc) {\n}"); 4349 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4350 " ccccccccccccccccccccccccc) {\n}"); 4351 4352 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4353 " ccccccccccccccccccccccccc) {\n}"); 4354 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4355 " ccccccccccccccccccccccccc) {\n}"); 4356 4357 verifyFormat( 4358 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4359 " ccccccccccccccccccccccccc) {\n}"); 4360 verifyFormat( 4361 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4362 " ccccccccccccccccccccccccc) {\n}"); 4363 4364 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4365 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4366 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4367 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4368 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4369 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4370 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4371 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4372 4373 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4374 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4375 " aaaaaaaaaaaaaaa != aa) {\n}"); 4376 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4377 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4378 " aaaaaaaaaaaaaaa != aa) {\n}"); 4379 } 4380 4381 TEST_F(FormatTest, BreaksAfterAssignments) { 4382 verifyFormat( 4383 "unsigned Cost =\n" 4384 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4385 " SI->getPointerAddressSpaceee());\n"); 4386 verifyFormat( 4387 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4388 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4389 4390 verifyFormat( 4391 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4392 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4393 verifyFormat("unsigned OriginalStartColumn =\n" 4394 " SourceMgr.getSpellingColumnNumber(\n" 4395 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4396 " 1;"); 4397 } 4398 4399 TEST_F(FormatTest, AlignsAfterAssignments) { 4400 verifyFormat( 4401 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4402 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4403 verifyFormat( 4404 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4405 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4406 verifyFormat( 4407 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4408 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4409 verifyFormat( 4410 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4411 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4412 verifyFormat( 4413 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4414 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4415 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4416 } 4417 4418 TEST_F(FormatTest, AlignsAfterReturn) { 4419 verifyFormat( 4420 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4421 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4422 verifyFormat( 4423 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4424 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4425 verifyFormat( 4426 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4427 " aaaaaaaaaaaaaaaaaaaaaa();"); 4428 verifyFormat( 4429 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4430 " aaaaaaaaaaaaaaaaaaaaaa());"); 4431 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4433 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4436 verifyFormat("return\n" 4437 " // true if code is one of a or b.\n" 4438 " code == a || code == b;"); 4439 } 4440 4441 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4442 verifyFormat( 4443 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4444 " aaaaaaaaa aaaaaaa) {}"); 4445 verifyFormat( 4446 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4447 " aaaaaaaaaaa aaaaaaaaa);"); 4448 verifyFormat( 4449 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4450 " aaaaaaaaaaaaaaaaaaaaa));"); 4451 FormatStyle Style = getLLVMStyle(); 4452 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4453 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4454 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4455 Style); 4456 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4457 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4458 Style); 4459 verifyFormat("SomeLongVariableName->someFunction(\n" 4460 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4461 Style); 4462 verifyFormat( 4463 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4464 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4465 Style); 4466 verifyFormat( 4467 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4468 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4469 Style); 4470 verifyFormat( 4471 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4472 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4473 Style); 4474 4475 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4476 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4477 " b));", 4478 Style); 4479 4480 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4481 Style.BinPackArguments = false; 4482 Style.BinPackParameters = false; 4483 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4484 " aaaaaaaaaaa aaaaaaaa,\n" 4485 " aaaaaaaaa aaaaaaa,\n" 4486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4487 Style); 4488 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4489 " aaaaaaaaaaa aaaaaaaaa,\n" 4490 " aaaaaaaaaaa aaaaaaaaa,\n" 4491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4492 Style); 4493 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4494 " aaaaaaaaaaaaaaa,\n" 4495 " aaaaaaaaaaaaaaaaaaaaa,\n" 4496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4497 Style); 4498 verifyFormat( 4499 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4500 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4501 Style); 4502 verifyFormat( 4503 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4504 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4505 Style); 4506 verifyFormat( 4507 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4508 " aaaaaaaaaaaaaaaaaaaaa(\n" 4509 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4510 " aaaaaaaaaaaaaaaa);", 4511 Style); 4512 verifyFormat( 4513 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4514 " aaaaaaaaaaaaaaaaaaaaa(\n" 4515 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4516 " aaaaaaaaaaaaaaaa);", 4517 Style); 4518 } 4519 4520 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4521 FormatStyle Style = getLLVMStyleWithColumns(40); 4522 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4523 " bbbbbbbbbbbbbbbbbbbbbb);", 4524 Style); 4525 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4526 Style.AlignOperands = false; 4527 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4528 " bbbbbbbbbbbbbbbbbbbbbb);", 4529 Style); 4530 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4531 Style.AlignOperands = true; 4532 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4533 " bbbbbbbbbbbbbbbbbbbbbb);", 4534 Style); 4535 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4536 Style.AlignOperands = false; 4537 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4538 " bbbbbbbbbbbbbbbbbbbbbb);", 4539 Style); 4540 } 4541 4542 TEST_F(FormatTest, BreaksConditionalExpressions) { 4543 verifyFormat( 4544 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4545 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4546 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4547 verifyFormat( 4548 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4549 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4550 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4551 verifyFormat( 4552 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4553 " : aaaaaaaaaaaaa);"); 4554 verifyFormat( 4555 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4556 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4557 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4558 " aaaaaaaaaaaaa);"); 4559 verifyFormat( 4560 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4561 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4562 " aaaaaaaaaaaaa);"); 4563 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4564 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4566 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4568 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4570 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4572 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4574 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4575 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4577 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4579 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4580 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4581 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4582 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4583 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4585 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " : aaaaaaaaaaaaaaaa;"); 4587 verifyFormat( 4588 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4589 " ? aaaaaaaaaaaaaaa\n" 4590 " : aaaaaaaaaaaaaaa;"); 4591 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4592 " aaaaaaaaa\n" 4593 " ? b\n" 4594 " : c);"); 4595 verifyFormat("return aaaa == bbbb\n" 4596 " // comment\n" 4597 " ? aaaa\n" 4598 " : bbbb;"); 4599 verifyFormat("unsigned Indent =\n" 4600 " format(TheLine.First,\n" 4601 " IndentForLevel[TheLine.Level] >= 0\n" 4602 " ? IndentForLevel[TheLine.Level]\n" 4603 " : TheLine * 2,\n" 4604 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4605 getLLVMStyleWithColumns(60)); 4606 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4607 " ? aaaaaaaaaaaaaaa\n" 4608 " : bbbbbbbbbbbbbbb //\n" 4609 " ? ccccccccccccccc\n" 4610 " : ddddddddddddddd;"); 4611 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4612 " ? aaaaaaaaaaaaaaa\n" 4613 " : (bbbbbbbbbbbbbbb //\n" 4614 " ? ccccccccccccccc\n" 4615 " : ddddddddddddddd);"); 4616 verifyFormat( 4617 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4618 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4619 " aaaaaaaaaaaaaaaaaaaaa +\n" 4620 " aaaaaaaaaaaaaaaaaaaaa\n" 4621 " : aaaaaaaaaa;"); 4622 verifyFormat( 4623 "aaaaaa = aaaaaaaaaaaa\n" 4624 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4625 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4626 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4627 4628 FormatStyle NoBinPacking = getLLVMStyle(); 4629 NoBinPacking.BinPackArguments = false; 4630 verifyFormat( 4631 "void f() {\n" 4632 " g(aaa,\n" 4633 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4635 " ? aaaaaaaaaaaaaaa\n" 4636 " : aaaaaaaaaaaaaaa);\n" 4637 "}", 4638 NoBinPacking); 4639 verifyFormat( 4640 "void f() {\n" 4641 " g(aaa,\n" 4642 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4644 " ?: aaaaaaaaaaaaaaa);\n" 4645 "}", 4646 NoBinPacking); 4647 4648 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4649 " // comment.\n" 4650 " ccccccccccccccccccccccccccccccccccccccc\n" 4651 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4652 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4653 4654 // Assignments in conditional expressions. Apparently not uncommon :-(. 4655 verifyFormat("return a != b\n" 4656 " // comment\n" 4657 " ? a = b\n" 4658 " : a = b;"); 4659 verifyFormat("return a != b\n" 4660 " // comment\n" 4661 " ? a = a != b\n" 4662 " // comment\n" 4663 " ? a = b\n" 4664 " : a\n" 4665 " : a;\n"); 4666 verifyFormat("return a != b\n" 4667 " // comment\n" 4668 " ? a\n" 4669 " : a = a != b\n" 4670 " // comment\n" 4671 " ? a = b\n" 4672 " : a;"); 4673 } 4674 4675 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4676 FormatStyle Style = getLLVMStyle(); 4677 Style.BreakBeforeTernaryOperators = false; 4678 Style.ColumnLimit = 70; 4679 verifyFormat( 4680 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4681 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4683 Style); 4684 verifyFormat( 4685 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 4686 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4688 Style); 4689 verifyFormat( 4690 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4691 " aaaaaaaaaaaaa);", 4692 Style); 4693 verifyFormat( 4694 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4695 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4697 " aaaaaaaaaaaaa);", 4698 Style); 4699 verifyFormat( 4700 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4701 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4702 " aaaaaaaaaaaaa);", 4703 Style); 4704 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4709 Style); 4710 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4716 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4717 Style); 4718 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4723 Style); 4724 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4725 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4726 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4727 Style); 4728 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4732 Style); 4733 verifyFormat( 4734 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4735 " aaaaaaaaaaaaaaa :\n" 4736 " aaaaaaaaaaaaaaa;", 4737 Style); 4738 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4739 " aaaaaaaaa ?\n" 4740 " b :\n" 4741 " c);", 4742 Style); 4743 verifyFormat("unsigned Indent =\n" 4744 " format(TheLine.First,\n" 4745 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4746 " IndentForLevel[TheLine.Level] :\n" 4747 " TheLine * 2,\n" 4748 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4749 Style); 4750 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4751 " aaaaaaaaaaaaaaa :\n" 4752 " bbbbbbbbbbbbbbb ? //\n" 4753 " ccccccccccccccc :\n" 4754 " ddddddddddddddd;", 4755 Style); 4756 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4757 " aaaaaaaaaaaaaaa :\n" 4758 " (bbbbbbbbbbbbbbb ? //\n" 4759 " ccccccccccccccc :\n" 4760 " ddddddddddddddd);", 4761 Style); 4762 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4763 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4764 " ccccccccccccccccccccccccccc;", 4765 Style); 4766 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4767 " aaaaa :\n" 4768 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4769 Style); 4770 } 4771 4772 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4773 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4774 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4775 verifyFormat("bool a = true, b = false;"); 4776 4777 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4779 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4780 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4781 verifyFormat( 4782 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4783 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4784 " d = e && f;"); 4785 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4786 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4787 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4788 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4789 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4790 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4791 4792 FormatStyle Style = getGoogleStyle(); 4793 Style.PointerAlignment = FormatStyle::PAS_Left; 4794 Style.DerivePointerAlignment = false; 4795 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4796 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4797 " *b = bbbbbbbbbbbbbbbbbbb;", 4798 Style); 4799 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4800 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4801 Style); 4802 verifyFormat("vector<int*> a, b;", Style); 4803 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4804 } 4805 4806 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4807 verifyFormat("arr[foo ? bar : baz];"); 4808 verifyFormat("f()[foo ? bar : baz];"); 4809 verifyFormat("(a + b)[foo ? bar : baz];"); 4810 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4811 } 4812 4813 TEST_F(FormatTest, AlignsStringLiterals) { 4814 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4815 " \"short literal\");"); 4816 verifyFormat( 4817 "looooooooooooooooooooooooongFunction(\n" 4818 " \"short literal\"\n" 4819 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4820 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4821 " \" string literals\",\n" 4822 " and, other, parameters);"); 4823 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4824 " \"5678\";", 4825 format("fun + \"1243\" /* comment */\n" 4826 " \"5678\";", 4827 getLLVMStyleWithColumns(28))); 4828 EXPECT_EQ( 4829 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4830 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4831 " \"aaaaaaaaaaaaaaaa\";", 4832 format("aaaaaa =" 4833 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4834 "aaaaaaaaaaaaaaaaaaaaa\" " 4835 "\"aaaaaaaaaaaaaaaa\";")); 4836 verifyFormat("a = a + \"a\"\n" 4837 " \"a\"\n" 4838 " \"a\";"); 4839 verifyFormat("f(\"a\", \"b\"\n" 4840 " \"c\");"); 4841 4842 verifyFormat( 4843 "#define LL_FORMAT \"ll\"\n" 4844 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4845 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4846 4847 verifyFormat("#define A(X) \\\n" 4848 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4849 " \"ccccc\"", 4850 getLLVMStyleWithColumns(23)); 4851 verifyFormat("#define A \"def\"\n" 4852 "f(\"abc\" A \"ghi\"\n" 4853 " \"jkl\");"); 4854 4855 verifyFormat("f(L\"a\"\n" 4856 " L\"b\");"); 4857 verifyFormat("#define A(X) \\\n" 4858 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4859 " L\"ccccc\"", 4860 getLLVMStyleWithColumns(25)); 4861 4862 verifyFormat("f(@\"a\"\n" 4863 " @\"b\");"); 4864 verifyFormat("NSString s = @\"a\"\n" 4865 " @\"b\"\n" 4866 " @\"c\";"); 4867 verifyFormat("NSString s = @\"a\"\n" 4868 " \"b\"\n" 4869 " \"c\";"); 4870 } 4871 4872 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4873 FormatStyle Style = getLLVMStyle(); 4874 // No declarations or definitions should be moved to own line. 4875 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4876 verifyFormat("class A {\n" 4877 " int f() { return 1; }\n" 4878 " int g();\n" 4879 "};\n" 4880 "int f() { return 1; }\n" 4881 "int g();\n", 4882 Style); 4883 4884 // All declarations and definitions should have the return type moved to its 4885 // own 4886 // line. 4887 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4888 verifyFormat("class E {\n" 4889 " int\n" 4890 " f() {\n" 4891 " return 1;\n" 4892 " }\n" 4893 " int\n" 4894 " g();\n" 4895 "};\n" 4896 "int\n" 4897 "f() {\n" 4898 " return 1;\n" 4899 "}\n" 4900 "int\n" 4901 "g();\n", 4902 Style); 4903 4904 // Top-level definitions, and no kinds of declarations should have the 4905 // return type moved to its own line. 4906 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4907 verifyFormat("class B {\n" 4908 " int f() { return 1; }\n" 4909 " int g();\n" 4910 "};\n" 4911 "int\n" 4912 "f() {\n" 4913 " return 1;\n" 4914 "}\n" 4915 "int g();\n", 4916 Style); 4917 4918 // Top-level definitions and declarations should have the return type moved 4919 // to its own line. 4920 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4921 verifyFormat("class C {\n" 4922 " int f() { return 1; }\n" 4923 " int g();\n" 4924 "};\n" 4925 "int\n" 4926 "f() {\n" 4927 " return 1;\n" 4928 "}\n" 4929 "int\n" 4930 "g();\n", 4931 Style); 4932 4933 // All definitions should have the return type moved to its own line, but no 4934 // kinds of declarations. 4935 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4936 verifyFormat("class D {\n" 4937 " int\n" 4938 " f() {\n" 4939 " return 1;\n" 4940 " }\n" 4941 " int g();\n" 4942 "};\n" 4943 "int\n" 4944 "f() {\n" 4945 " return 1;\n" 4946 "}\n" 4947 "int g();\n", 4948 Style); 4949 verifyFormat("const char *\n" 4950 "f(void) {\n" // Break here. 4951 " return \"\";\n" 4952 "}\n" 4953 "const char *bar(void);\n", // No break here. 4954 Style); 4955 verifyFormat("template <class T>\n" 4956 "T *\n" 4957 "f(T &c) {\n" // Break here. 4958 " return NULL;\n" 4959 "}\n" 4960 "template <class T> T *f(T &c);\n", // No break here. 4961 Style); 4962 verifyFormat("class C {\n" 4963 " int\n" 4964 " operator+() {\n" 4965 " return 1;\n" 4966 " }\n" 4967 " int\n" 4968 " operator()() {\n" 4969 " return 1;\n" 4970 " }\n" 4971 "};\n", 4972 Style); 4973 verifyFormat("void\n" 4974 "A::operator()() {}\n" 4975 "void\n" 4976 "A::operator>>() {}\n" 4977 "void\n" 4978 "A::operator+() {}\n", 4979 Style); 4980 verifyFormat("void *operator new(std::size_t s);", // No break here. 4981 Style); 4982 verifyFormat("void *\n" 4983 "operator new(std::size_t s) {}", 4984 Style); 4985 verifyFormat("void *\n" 4986 "operator delete[](void *ptr) {}", 4987 Style); 4988 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4989 verifyFormat("const char *\n" 4990 "f(void)\n" // Break here. 4991 "{\n" 4992 " return \"\";\n" 4993 "}\n" 4994 "const char *bar(void);\n", // No break here. 4995 Style); 4996 verifyFormat("template <class T>\n" 4997 "T *\n" // Problem here: no line break 4998 "f(T &c)\n" // Break here. 4999 "{\n" 5000 " return NULL;\n" 5001 "}\n" 5002 "template <class T> T *f(T &c);\n", // No break here. 5003 Style); 5004 } 5005 5006 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5007 FormatStyle NoBreak = getLLVMStyle(); 5008 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5009 FormatStyle Break = getLLVMStyle(); 5010 Break.AlwaysBreakBeforeMultilineStrings = true; 5011 verifyFormat("aaaa = \"bbbb\"\n" 5012 " \"cccc\";", 5013 NoBreak); 5014 verifyFormat("aaaa =\n" 5015 " \"bbbb\"\n" 5016 " \"cccc\";", 5017 Break); 5018 verifyFormat("aaaa(\"bbbb\"\n" 5019 " \"cccc\");", 5020 NoBreak); 5021 verifyFormat("aaaa(\n" 5022 " \"bbbb\"\n" 5023 " \"cccc\");", 5024 Break); 5025 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5026 " \"cccc\");", 5027 NoBreak); 5028 verifyFormat("aaaa(qqq,\n" 5029 " \"bbbb\"\n" 5030 " \"cccc\");", 5031 Break); 5032 verifyFormat("aaaa(qqq,\n" 5033 " L\"bbbb\"\n" 5034 " L\"cccc\");", 5035 Break); 5036 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5037 " \"bbbb\"));", 5038 Break); 5039 verifyFormat("string s = someFunction(\n" 5040 " \"abc\"\n" 5041 " \"abc\");", 5042 Break); 5043 5044 // As we break before unary operators, breaking right after them is bad. 5045 verifyFormat("string foo = abc ? \"x\"\n" 5046 " \"blah blah blah blah blah blah\"\n" 5047 " : \"y\";", 5048 Break); 5049 5050 // Don't break if there is no column gain. 5051 verifyFormat("f(\"aaaa\"\n" 5052 " \"bbbb\");", 5053 Break); 5054 5055 // Treat literals with escaped newlines like multi-line string literals. 5056 EXPECT_EQ("x = \"a\\\n" 5057 "b\\\n" 5058 "c\";", 5059 format("x = \"a\\\n" 5060 "b\\\n" 5061 "c\";", 5062 NoBreak)); 5063 EXPECT_EQ("xxxx =\n" 5064 " \"a\\\n" 5065 "b\\\n" 5066 "c\";", 5067 format("xxxx = \"a\\\n" 5068 "b\\\n" 5069 "c\";", 5070 Break)); 5071 5072 // Exempt ObjC strings for now. 5073 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5074 " @\"bbbb\";", 5075 format("NSString *const kString = @\"aaaa\"\n" 5076 "@\"bbbb\";", 5077 Break)); 5078 5079 Break.ColumnLimit = 0; 5080 verifyFormat("const char *hello = \"hello llvm\";", Break); 5081 } 5082 5083 TEST_F(FormatTest, AlignsPipes) { 5084 verifyFormat( 5085 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5086 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5087 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5088 verifyFormat( 5089 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5090 " << aaaaaaaaaaaaaaaaaaaa;"); 5091 verifyFormat( 5092 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5093 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5094 verifyFormat( 5095 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5096 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5097 verifyFormat( 5098 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5099 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5100 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5101 verifyFormat( 5102 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5103 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5104 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5105 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5108 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5109 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5110 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5111 verifyFormat( 5112 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5114 5115 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5116 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5117 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5119 " aaaaaaaaaaaaaaaaaaaaa)\n" 5120 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5121 verifyFormat("LOG_IF(aaa == //\n" 5122 " bbb)\n" 5123 " << a << b;"); 5124 5125 // But sometimes, breaking before the first "<<" is desirable. 5126 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5127 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5128 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5129 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5130 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5131 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5132 " << BEF << IsTemplate << Description << E->getType();"); 5133 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5134 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5136 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5137 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5139 " << aaa;"); 5140 5141 verifyFormat( 5142 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5143 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5144 5145 // Incomplete string literal. 5146 EXPECT_EQ("llvm::errs() << \"\n" 5147 " << a;", 5148 format("llvm::errs() << \"\n<<a;")); 5149 5150 verifyFormat("void f() {\n" 5151 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5152 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5153 "}"); 5154 5155 // Handle 'endl'. 5156 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5157 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5158 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5159 5160 // Handle '\n'. 5161 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5162 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5163 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5164 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5165 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5166 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5167 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5168 } 5169 5170 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5171 verifyFormat("return out << \"somepacket = {\\n\"\n" 5172 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5173 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5174 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5175 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5176 " << \"}\";"); 5177 5178 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5179 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5180 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5181 verifyFormat( 5182 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5183 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5184 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5185 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5186 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5187 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5188 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5189 verifyFormat( 5190 "void f() {\n" 5191 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5192 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5193 "}"); 5194 5195 // Breaking before the first "<<" is generally not desirable. 5196 verifyFormat( 5197 "llvm::errs()\n" 5198 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5199 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5200 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5201 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5202 getLLVMStyleWithColumns(70)); 5203 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5204 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5205 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5206 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5207 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5208 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5209 getLLVMStyleWithColumns(70)); 5210 5211 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5212 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5213 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5214 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5215 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5216 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5217 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5218 " (aaaa + aaaa);", 5219 getLLVMStyleWithColumns(40)); 5220 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5221 " (aaaaaaa + aaaaa));", 5222 getLLVMStyleWithColumns(40)); 5223 verifyFormat( 5224 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5225 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5226 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5227 } 5228 5229 TEST_F(FormatTest, UnderstandsEquals) { 5230 verifyFormat( 5231 "aaaaaaaaaaaaaaaaa =\n" 5232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5233 verifyFormat( 5234 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5236 verifyFormat( 5237 "if (a) {\n" 5238 " f();\n" 5239 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5241 "}"); 5242 5243 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5244 " 100000000 + 10000000) {\n}"); 5245 } 5246 5247 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5248 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5249 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5250 5251 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5252 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5253 5254 verifyFormat( 5255 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5256 " Parameter2);"); 5257 5258 verifyFormat( 5259 "ShortObject->shortFunction(\n" 5260 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5261 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5262 5263 verifyFormat("loooooooooooooongFunction(\n" 5264 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5265 5266 verifyFormat( 5267 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5268 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5269 5270 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5271 " .WillRepeatedly(Return(SomeValue));"); 5272 verifyFormat("void f() {\n" 5273 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5274 " .Times(2)\n" 5275 " .WillRepeatedly(Return(SomeValue));\n" 5276 "}"); 5277 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5278 " ccccccccccccccccccccccc);"); 5279 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5281 " .aaaaa(aaaaa),\n" 5282 " aaaaaaaaaaaaaaaaaaaaa);"); 5283 verifyFormat("void f() {\n" 5284 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5285 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5286 "}"); 5287 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5289 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5290 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5291 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5292 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5293 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5294 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5295 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5296 "}"); 5297 5298 // Here, it is not necessary to wrap at "." or "->". 5299 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5300 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5301 verifyFormat( 5302 "aaaaaaaaaaa->aaaaaaaaa(\n" 5303 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5304 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5305 5306 verifyFormat( 5307 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5308 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5309 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5310 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5311 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5312 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5313 5314 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5316 " .a();"); 5317 5318 FormatStyle NoBinPacking = getLLVMStyle(); 5319 NoBinPacking.BinPackParameters = false; 5320 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5321 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5322 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5323 " aaaaaaaaaaaaaaaaaaa,\n" 5324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5325 NoBinPacking); 5326 5327 // If there is a subsequent call, change to hanging indentation. 5328 verifyFormat( 5329 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5330 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5331 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5332 verifyFormat( 5333 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5334 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5335 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5337 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5338 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5340 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5341 } 5342 5343 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5344 verifyFormat("template <typename T>\n" 5345 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5346 verifyFormat("template <typename T>\n" 5347 "// T should be one of {A, B}.\n" 5348 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5349 verifyFormat( 5350 "template <typename T>\n" 5351 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5352 verifyFormat("template <typename T>\n" 5353 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5354 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5355 verifyFormat( 5356 "template <typename T>\n" 5357 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5358 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5359 verifyFormat( 5360 "template <typename T>\n" 5361 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5362 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5364 verifyFormat("template <typename T>\n" 5365 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5366 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5367 verifyFormat( 5368 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5369 " typename T4 = char>\n" 5370 "void f();"); 5371 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5372 " template <typename> class cccccccccccccccccccccc,\n" 5373 " typename ddddddddddddd>\n" 5374 "class C {};"); 5375 verifyFormat( 5376 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5378 5379 verifyFormat("void f() {\n" 5380 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5381 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5382 "}"); 5383 5384 verifyFormat("template <typename T> class C {};"); 5385 verifyFormat("template <typename T> void f();"); 5386 verifyFormat("template <typename T> void f() {}"); 5387 verifyFormat( 5388 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5391 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5394 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5395 getLLVMStyleWithColumns(72)); 5396 EXPECT_EQ("static_cast<A< //\n" 5397 " B> *>(\n" 5398 "\n" 5399 " );", 5400 format("static_cast<A<//\n" 5401 " B>*>(\n" 5402 "\n" 5403 " );")); 5404 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5405 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5406 5407 FormatStyle AlwaysBreak = getLLVMStyle(); 5408 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5409 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5410 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5411 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5412 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5413 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5414 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5415 verifyFormat("template <template <typename> class Fooooooo,\n" 5416 " template <typename> class Baaaaaaar>\n" 5417 "struct C {};", 5418 AlwaysBreak); 5419 verifyFormat("template <typename T> // T can be A, B or C.\n" 5420 "struct C {};", 5421 AlwaysBreak); 5422 verifyFormat("template <enum E> class A {\n" 5423 "public:\n" 5424 " E *f();\n" 5425 "};"); 5426 } 5427 5428 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5429 verifyFormat( 5430 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5432 verifyFormat( 5433 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5436 5437 // FIXME: Should we have the extra indent after the second break? 5438 verifyFormat( 5439 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5442 5443 verifyFormat( 5444 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5445 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5446 5447 // Breaking at nested name specifiers is generally not desirable. 5448 verifyFormat( 5449 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5450 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5451 5452 verifyFormat( 5453 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5455 " aaaaaaaaaaaaaaaaaaaaa);", 5456 getLLVMStyleWithColumns(74)); 5457 5458 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5460 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5461 } 5462 5463 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5464 verifyFormat("A<int> a;"); 5465 verifyFormat("A<A<A<int>>> a;"); 5466 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5467 verifyFormat("bool x = a < 1 || 2 > a;"); 5468 verifyFormat("bool x = 5 < f<int>();"); 5469 verifyFormat("bool x = f<int>() > 5;"); 5470 verifyFormat("bool x = 5 < a<int>::x;"); 5471 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5472 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5473 5474 verifyGoogleFormat("A<A<int>> a;"); 5475 verifyGoogleFormat("A<A<A<int>>> a;"); 5476 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5477 verifyGoogleFormat("A<A<int> > a;"); 5478 verifyGoogleFormat("A<A<A<int> > > a;"); 5479 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5480 verifyGoogleFormat("A<::A<int>> a;"); 5481 verifyGoogleFormat("A<::A> a;"); 5482 verifyGoogleFormat("A< ::A> a;"); 5483 verifyGoogleFormat("A< ::A<int> > a;"); 5484 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5485 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5486 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5487 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5488 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5489 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5490 5491 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5492 5493 verifyFormat("test >> a >> b;"); 5494 verifyFormat("test << a >> b;"); 5495 5496 verifyFormat("f<int>();"); 5497 verifyFormat("template <typename T> void f() {}"); 5498 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5499 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5500 "sizeof(char)>::type>;"); 5501 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5502 verifyFormat("f(a.operator()<A>());"); 5503 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5504 " .template operator()<A>());", 5505 getLLVMStyleWithColumns(35)); 5506 5507 // Not template parameters. 5508 verifyFormat("return a < b && c > d;"); 5509 verifyFormat("void f() {\n" 5510 " while (a < b && c > d) {\n" 5511 " }\n" 5512 "}"); 5513 verifyFormat("template <typename... Types>\n" 5514 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5515 5516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5518 getLLVMStyleWithColumns(60)); 5519 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5520 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5521 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5522 } 5523 5524 TEST_F(FormatTest, BitshiftOperatorWidth) { 5525 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5526 " bar */", 5527 format("int a=1<<2; /* foo\n" 5528 " bar */")); 5529 5530 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5531 " bar */", 5532 format("int b =256>>1 ; /* foo\n" 5533 " bar */")); 5534 } 5535 5536 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5537 verifyFormat("COMPARE(a, ==, b);"); 5538 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5539 } 5540 5541 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5542 verifyFormat("int A::*x;"); 5543 verifyFormat("int (S::*func)(void *);"); 5544 verifyFormat("void f() { int (S::*func)(void *); }"); 5545 verifyFormat("typedef bool *(Class::*Member)() const;"); 5546 verifyFormat("void f() {\n" 5547 " (a->*f)();\n" 5548 " a->*x;\n" 5549 " (a.*f)();\n" 5550 " ((*a).*f)();\n" 5551 " a.*x;\n" 5552 "}"); 5553 verifyFormat("void f() {\n" 5554 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5555 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5556 "}"); 5557 verifyFormat( 5558 "(aaaaaaaaaa->*bbbbbbb)(\n" 5559 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5560 FormatStyle Style = getLLVMStyle(); 5561 Style.PointerAlignment = FormatStyle::PAS_Left; 5562 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5563 } 5564 5565 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5566 verifyFormat("int a = -2;"); 5567 verifyFormat("f(-1, -2, -3);"); 5568 verifyFormat("a[-1] = 5;"); 5569 verifyFormat("int a = 5 + -2;"); 5570 verifyFormat("if (i == -1) {\n}"); 5571 verifyFormat("if (i != -1) {\n}"); 5572 verifyFormat("if (i > -1) {\n}"); 5573 verifyFormat("if (i < -1) {\n}"); 5574 verifyFormat("++(a->f());"); 5575 verifyFormat("--(a->f());"); 5576 verifyFormat("(a->f())++;"); 5577 verifyFormat("a[42]++;"); 5578 verifyFormat("if (!(a->f())) {\n}"); 5579 5580 verifyFormat("a-- > b;"); 5581 verifyFormat("b ? -a : c;"); 5582 verifyFormat("n * sizeof char16;"); 5583 verifyFormat("n * alignof char16;", getGoogleStyle()); 5584 verifyFormat("sizeof(char);"); 5585 verifyFormat("alignof(char);", getGoogleStyle()); 5586 5587 verifyFormat("return -1;"); 5588 verifyFormat("switch (a) {\n" 5589 "case -1:\n" 5590 " break;\n" 5591 "}"); 5592 verifyFormat("#define X -1"); 5593 verifyFormat("#define X -kConstant"); 5594 5595 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5596 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5597 5598 verifyFormat("int a = /* confusing comment */ -1;"); 5599 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5600 verifyFormat("int a = i /* confusing comment */++;"); 5601 } 5602 5603 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5604 verifyFormat("if (!aaaaaaaaaa( // break\n" 5605 " aaaaa)) {\n" 5606 "}"); 5607 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5608 " aaaaa));"); 5609 verifyFormat("*aaa = aaaaaaa( // break\n" 5610 " bbbbbb);"); 5611 } 5612 5613 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5614 verifyFormat("bool operator<();"); 5615 verifyFormat("bool operator>();"); 5616 verifyFormat("bool operator=();"); 5617 verifyFormat("bool operator==();"); 5618 verifyFormat("bool operator!=();"); 5619 verifyFormat("int operator+();"); 5620 verifyFormat("int operator++();"); 5621 verifyFormat("bool operator,();"); 5622 verifyFormat("bool operator();"); 5623 verifyFormat("bool operator()();"); 5624 verifyFormat("bool operator[]();"); 5625 verifyFormat("operator bool();"); 5626 verifyFormat("operator int();"); 5627 verifyFormat("operator void *();"); 5628 verifyFormat("operator SomeType<int>();"); 5629 verifyFormat("operator SomeType<int, int>();"); 5630 verifyFormat("operator SomeType<SomeType<int>>();"); 5631 verifyFormat("void *operator new(std::size_t size);"); 5632 verifyFormat("void *operator new[](std::size_t size);"); 5633 verifyFormat("void operator delete(void *ptr);"); 5634 verifyFormat("void operator delete[](void *ptr);"); 5635 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5636 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5637 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5638 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5639 5640 verifyFormat( 5641 "ostream &operator<<(ostream &OutputStream,\n" 5642 " SomeReallyLongType WithSomeReallyLongValue);"); 5643 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5644 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5645 " return left.group < right.group;\n" 5646 "}"); 5647 verifyFormat("SomeType &operator=(const SomeType &S);"); 5648 verifyFormat("f.template operator()<int>();"); 5649 5650 verifyGoogleFormat("operator void*();"); 5651 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5652 verifyGoogleFormat("operator ::A();"); 5653 5654 verifyFormat("using A::operator+;"); 5655 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5656 "int i;"); 5657 } 5658 5659 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5660 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5661 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5662 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5663 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5664 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5665 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5666 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5667 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5668 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5669 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5670 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5671 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5672 verifyFormat("template <typename T>\n" 5673 "void F(T) && = delete;", 5674 getGoogleStyle()); 5675 5676 FormatStyle AlignLeft = getLLVMStyle(); 5677 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5678 verifyFormat("void A::b() && {}", AlignLeft); 5679 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5680 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5681 AlignLeft); 5682 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5683 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5684 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5685 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5686 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5687 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5688 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5689 5690 FormatStyle Spaces = getLLVMStyle(); 5691 Spaces.SpacesInCStyleCastParentheses = true; 5692 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5693 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5694 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5695 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5696 5697 Spaces.SpacesInCStyleCastParentheses = false; 5698 Spaces.SpacesInParentheses = true; 5699 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5700 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5701 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5702 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5703 } 5704 5705 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5706 verifyFormat("void f() {\n" 5707 " A *a = new A;\n" 5708 " A *a = new (placement) A;\n" 5709 " delete a;\n" 5710 " delete (A *)a;\n" 5711 "}"); 5712 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5713 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5714 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5715 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5716 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5717 verifyFormat("delete[] h->p;"); 5718 } 5719 5720 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5721 verifyFormat("int *f(int *a) {}"); 5722 verifyFormat("int main(int argc, char **argv) {}"); 5723 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5724 verifyIndependentOfContext("f(a, *a);"); 5725 verifyFormat("void g() { f(*a); }"); 5726 verifyIndependentOfContext("int a = b * 10;"); 5727 verifyIndependentOfContext("int a = 10 * b;"); 5728 verifyIndependentOfContext("int a = b * c;"); 5729 verifyIndependentOfContext("int a += b * c;"); 5730 verifyIndependentOfContext("int a -= b * c;"); 5731 verifyIndependentOfContext("int a *= b * c;"); 5732 verifyIndependentOfContext("int a /= b * c;"); 5733 verifyIndependentOfContext("int a = *b;"); 5734 verifyIndependentOfContext("int a = *b * c;"); 5735 verifyIndependentOfContext("int a = b * *c;"); 5736 verifyIndependentOfContext("int a = b * (10);"); 5737 verifyIndependentOfContext("S << b * (10);"); 5738 verifyIndependentOfContext("return 10 * b;"); 5739 verifyIndependentOfContext("return *b * *c;"); 5740 verifyIndependentOfContext("return a & ~b;"); 5741 verifyIndependentOfContext("f(b ? *c : *d);"); 5742 verifyIndependentOfContext("int a = b ? *c : *d;"); 5743 verifyIndependentOfContext("*b = a;"); 5744 verifyIndependentOfContext("a * ~b;"); 5745 verifyIndependentOfContext("a * !b;"); 5746 verifyIndependentOfContext("a * +b;"); 5747 verifyIndependentOfContext("a * -b;"); 5748 verifyIndependentOfContext("a * ++b;"); 5749 verifyIndependentOfContext("a * --b;"); 5750 verifyIndependentOfContext("a[4] * b;"); 5751 verifyIndependentOfContext("a[a * a] = 1;"); 5752 verifyIndependentOfContext("f() * b;"); 5753 verifyIndependentOfContext("a * [self dostuff];"); 5754 verifyIndependentOfContext("int x = a * (a + b);"); 5755 verifyIndependentOfContext("(a *)(a + b);"); 5756 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5757 verifyIndependentOfContext("int *pa = (int *)&a;"); 5758 verifyIndependentOfContext("return sizeof(int **);"); 5759 verifyIndependentOfContext("return sizeof(int ******);"); 5760 verifyIndependentOfContext("return (int **&)a;"); 5761 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5762 verifyFormat("void f(Type (*parameter)[10]) {}"); 5763 verifyFormat("void f(Type (¶meter)[10]) {}"); 5764 verifyGoogleFormat("return sizeof(int**);"); 5765 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5766 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5767 verifyFormat("auto a = [](int **&, int ***) {};"); 5768 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5769 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5770 verifyFormat("[](const decltype(*a) &value) {}"); 5771 verifyFormat("decltype(a * b) F();"); 5772 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5773 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5774 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5775 verifyIndependentOfContext("int i{a * b};"); 5776 verifyIndependentOfContext("aaa && aaa->f();"); 5777 verifyIndependentOfContext("int x = ~*p;"); 5778 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5779 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5780 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5781 verifyFormat("void f() { f(a, c * d); }"); 5782 verifyFormat("void f() { f(new a(), c * d); }"); 5783 5784 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5785 5786 verifyIndependentOfContext("A<int *> a;"); 5787 verifyIndependentOfContext("A<int **> a;"); 5788 verifyIndependentOfContext("A<int *, int *> a;"); 5789 verifyIndependentOfContext("A<int *[]> a;"); 5790 verifyIndependentOfContext( 5791 "const char *const p = reinterpret_cast<const char *const>(q);"); 5792 verifyIndependentOfContext("A<int **, int **> a;"); 5793 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5794 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5795 verifyFormat("for (; a && b;) {\n}"); 5796 verifyFormat("bool foo = true && [] { return false; }();"); 5797 5798 verifyFormat( 5799 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5801 5802 verifyGoogleFormat("int const* a = &b;"); 5803 verifyGoogleFormat("**outparam = 1;"); 5804 verifyGoogleFormat("*outparam = a * b;"); 5805 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5806 verifyGoogleFormat("A<int*> a;"); 5807 verifyGoogleFormat("A<int**> a;"); 5808 verifyGoogleFormat("A<int*, int*> a;"); 5809 verifyGoogleFormat("A<int**, int**> a;"); 5810 verifyGoogleFormat("f(b ? *c : *d);"); 5811 verifyGoogleFormat("int a = b ? *c : *d;"); 5812 verifyGoogleFormat("Type* t = **x;"); 5813 verifyGoogleFormat("Type* t = *++*x;"); 5814 verifyGoogleFormat("*++*x;"); 5815 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5816 verifyGoogleFormat("Type* t = x++ * y;"); 5817 verifyGoogleFormat( 5818 "const char* const p = reinterpret_cast<const char* const>(q);"); 5819 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5820 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5821 verifyGoogleFormat("template <typename T>\n" 5822 "void f(int i = 0, SomeType** temps = NULL);"); 5823 5824 FormatStyle Left = getLLVMStyle(); 5825 Left.PointerAlignment = FormatStyle::PAS_Left; 5826 verifyFormat("x = *a(x) = *a(y);", Left); 5827 verifyFormat("for (;; *a = b) {\n}", Left); 5828 verifyFormat("return *this += 1;", Left); 5829 5830 verifyIndependentOfContext("a = *(x + y);"); 5831 verifyIndependentOfContext("a = &(x + y);"); 5832 verifyIndependentOfContext("*(x + y).call();"); 5833 verifyIndependentOfContext("&(x + y)->call();"); 5834 verifyFormat("void f() { &(*I).first; }"); 5835 5836 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5837 verifyFormat( 5838 "int *MyValues = {\n" 5839 " *A, // Operator detection might be confused by the '{'\n" 5840 " *BB // Operator detection might be confused by previous comment\n" 5841 "};"); 5842 5843 verifyIndependentOfContext("if (int *a = &b)"); 5844 verifyIndependentOfContext("if (int &a = *b)"); 5845 verifyIndependentOfContext("if (a & b[i])"); 5846 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5847 verifyIndependentOfContext("if (*b[i])"); 5848 verifyIndependentOfContext("if (int *a = (&b))"); 5849 verifyIndependentOfContext("while (int *a = &b)"); 5850 verifyIndependentOfContext("size = sizeof *a;"); 5851 verifyIndependentOfContext("if (a && (b = c))"); 5852 verifyFormat("void f() {\n" 5853 " for (const int &v : Values) {\n" 5854 " }\n" 5855 "}"); 5856 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5857 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5858 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5859 5860 verifyFormat("#define A (!a * b)"); 5861 verifyFormat("#define MACRO \\\n" 5862 " int *i = a * b; \\\n" 5863 " void f(a *b);", 5864 getLLVMStyleWithColumns(19)); 5865 5866 verifyIndependentOfContext("A = new SomeType *[Length];"); 5867 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5868 verifyIndependentOfContext("T **t = new T *;"); 5869 verifyIndependentOfContext("T **t = new T *();"); 5870 verifyGoogleFormat("A = new SomeType*[Length]();"); 5871 verifyGoogleFormat("A = new SomeType*[Length];"); 5872 verifyGoogleFormat("T** t = new T*;"); 5873 verifyGoogleFormat("T** t = new T*();"); 5874 5875 FormatStyle PointerLeft = getLLVMStyle(); 5876 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5877 verifyFormat("delete *x;", PointerLeft); 5878 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5879 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5880 verifyFormat("template <bool a, bool b> " 5881 "typename t::if<x && y>::type f() {}"); 5882 verifyFormat("template <int *y> f() {}"); 5883 verifyFormat("vector<int *> v;"); 5884 verifyFormat("vector<int *const> v;"); 5885 verifyFormat("vector<int *const **const *> v;"); 5886 verifyFormat("vector<int *volatile> v;"); 5887 verifyFormat("vector<a * b> v;"); 5888 verifyFormat("foo<b && false>();"); 5889 verifyFormat("foo<b & 1>();"); 5890 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5891 verifyFormat( 5892 "template <class T,\n" 5893 " class = typename std::enable_if<\n" 5894 " std::is_integral<T>::value &&\n" 5895 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5896 "void F();", 5897 getLLVMStyleWithColumns(70)); 5898 verifyFormat( 5899 "template <class T,\n" 5900 " class = typename ::std::enable_if<\n" 5901 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5902 "void F();", 5903 getGoogleStyleWithColumns(68)); 5904 5905 verifyIndependentOfContext("MACRO(int *i);"); 5906 verifyIndependentOfContext("MACRO(auto *a);"); 5907 verifyIndependentOfContext("MACRO(const A *a);"); 5908 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5909 verifyFormat("void f() { f(float{1}, a * a); }"); 5910 // FIXME: Is there a way to make this work? 5911 // verifyIndependentOfContext("MACRO(A *a);"); 5912 5913 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5914 verifyFormat("return options != nullptr && operator==(*options);"); 5915 5916 EXPECT_EQ("#define OP(x) \\\n" 5917 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5918 " return s << a.DebugString(); \\\n" 5919 " }", 5920 format("#define OP(x) \\\n" 5921 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5922 " return s << a.DebugString(); \\\n" 5923 " }", 5924 getLLVMStyleWithColumns(50))); 5925 5926 // FIXME: We cannot handle this case yet; we might be able to figure out that 5927 // foo<x> d > v; doesn't make sense. 5928 verifyFormat("foo<a<b && c> d> v;"); 5929 5930 FormatStyle PointerMiddle = getLLVMStyle(); 5931 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5932 verifyFormat("delete *x;", PointerMiddle); 5933 verifyFormat("int * x;", PointerMiddle); 5934 verifyFormat("template <int * y> f() {}", PointerMiddle); 5935 verifyFormat("int * f(int * a) {}", PointerMiddle); 5936 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5937 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5938 verifyFormat("A<int *> a;", PointerMiddle); 5939 verifyFormat("A<int **> a;", PointerMiddle); 5940 verifyFormat("A<int *, int *> a;", PointerMiddle); 5941 verifyFormat("A<int * []> a;", PointerMiddle); 5942 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5943 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5944 verifyFormat("T ** t = new T *;", PointerMiddle); 5945 5946 // Member function reference qualifiers aren't binary operators. 5947 verifyFormat("string // break\n" 5948 "operator()() & {}"); 5949 verifyFormat("string // break\n" 5950 "operator()() && {}"); 5951 verifyGoogleFormat("template <typename T>\n" 5952 "auto x() & -> int {}"); 5953 } 5954 5955 TEST_F(FormatTest, UnderstandsAttributes) { 5956 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5957 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5958 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5959 FormatStyle AfterType = getLLVMStyle(); 5960 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5961 verifyFormat("__attribute__((nodebug)) void\n" 5962 "foo() {}\n", 5963 AfterType); 5964 } 5965 5966 TEST_F(FormatTest, UnderstandsEllipsis) { 5967 verifyFormat("int printf(const char *fmt, ...);"); 5968 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5969 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5970 5971 FormatStyle PointersLeft = getLLVMStyle(); 5972 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5973 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5974 } 5975 5976 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5977 EXPECT_EQ("int *a;\n" 5978 "int *a;\n" 5979 "int *a;", 5980 format("int *a;\n" 5981 "int* a;\n" 5982 "int *a;", 5983 getGoogleStyle())); 5984 EXPECT_EQ("int* a;\n" 5985 "int* a;\n" 5986 "int* a;", 5987 format("int* a;\n" 5988 "int* a;\n" 5989 "int *a;", 5990 getGoogleStyle())); 5991 EXPECT_EQ("int *a;\n" 5992 "int *a;\n" 5993 "int *a;", 5994 format("int *a;\n" 5995 "int * a;\n" 5996 "int * a;", 5997 getGoogleStyle())); 5998 EXPECT_EQ("auto x = [] {\n" 5999 " int *a;\n" 6000 " int *a;\n" 6001 " int *a;\n" 6002 "};", 6003 format("auto x=[]{int *a;\n" 6004 "int * a;\n" 6005 "int * a;};", 6006 getGoogleStyle())); 6007 } 6008 6009 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6010 verifyFormat("int f(int &&a) {}"); 6011 verifyFormat("int f(int a, char &&b) {}"); 6012 verifyFormat("void f() { int &&a = b; }"); 6013 verifyGoogleFormat("int f(int a, char&& b) {}"); 6014 verifyGoogleFormat("void f() { int&& a = b; }"); 6015 6016 verifyIndependentOfContext("A<int &&> a;"); 6017 verifyIndependentOfContext("A<int &&, int &&> a;"); 6018 verifyGoogleFormat("A<int&&> a;"); 6019 verifyGoogleFormat("A<int&&, int&&> a;"); 6020 6021 // Not rvalue references: 6022 verifyFormat("template <bool B, bool C> class A {\n" 6023 " static_assert(B && C, \"Something is wrong\");\n" 6024 "};"); 6025 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6026 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6027 verifyFormat("#define A(a, b) (a && b)"); 6028 } 6029 6030 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6031 verifyFormat("void f() {\n" 6032 " x[aaaaaaaaa -\n" 6033 " b] = 23;\n" 6034 "}", 6035 getLLVMStyleWithColumns(15)); 6036 } 6037 6038 TEST_F(FormatTest, FormatsCasts) { 6039 verifyFormat("Type *A = static_cast<Type *>(P);"); 6040 verifyFormat("Type *A = (Type *)P;"); 6041 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6042 verifyFormat("int a = (int)(2.0f);"); 6043 verifyFormat("int a = (int)2.0f;"); 6044 verifyFormat("x[(int32)y];"); 6045 verifyFormat("x = (int32)y;"); 6046 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6047 verifyFormat("int a = (int)*b;"); 6048 verifyFormat("int a = (int)2.0f;"); 6049 verifyFormat("int a = (int)~0;"); 6050 verifyFormat("int a = (int)++a;"); 6051 verifyFormat("int a = (int)sizeof(int);"); 6052 verifyFormat("int a = (int)+2;"); 6053 verifyFormat("my_int a = (my_int)2.0f;"); 6054 verifyFormat("my_int a = (my_int)sizeof(int);"); 6055 verifyFormat("return (my_int)aaa;"); 6056 verifyFormat("#define x ((int)-1)"); 6057 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6058 verifyFormat("#define p(q) ((int *)&q)"); 6059 verifyFormat("fn(a)(b) + 1;"); 6060 6061 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6062 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6063 verifyFormat("my_int a = (my_int)~0;"); 6064 verifyFormat("my_int a = (my_int)++a;"); 6065 verifyFormat("my_int a = (my_int)-2;"); 6066 verifyFormat("my_int a = (my_int)1;"); 6067 verifyFormat("my_int a = (my_int *)1;"); 6068 verifyFormat("my_int a = (const my_int)-1;"); 6069 verifyFormat("my_int a = (const my_int *)-1;"); 6070 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6071 verifyFormat("my_int a = (ns::my_int)-2;"); 6072 verifyFormat("case (my_int)ONE:"); 6073 verifyFormat("auto x = (X)this;"); 6074 6075 // FIXME: single value wrapped with paren will be treated as cast. 6076 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6077 6078 verifyFormat("{ (void)F; }"); 6079 6080 // Don't break after a cast's 6081 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6082 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6083 " bbbbbbbbbbbbbbbbbbbbbb);"); 6084 6085 // These are not casts. 6086 verifyFormat("void f(int *) {}"); 6087 verifyFormat("f(foo)->b;"); 6088 verifyFormat("f(foo).b;"); 6089 verifyFormat("f(foo)(b);"); 6090 verifyFormat("f(foo)[b];"); 6091 verifyFormat("[](foo) { return 4; }(bar);"); 6092 verifyFormat("(*funptr)(foo)[4];"); 6093 verifyFormat("funptrs[4](foo)[4];"); 6094 verifyFormat("void f(int *);"); 6095 verifyFormat("void f(int *) = 0;"); 6096 verifyFormat("void f(SmallVector<int>) {}"); 6097 verifyFormat("void f(SmallVector<int>);"); 6098 verifyFormat("void f(SmallVector<int>) = 0;"); 6099 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6100 verifyFormat("int a = sizeof(int) * b;"); 6101 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6102 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6103 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6104 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6105 6106 // These are not casts, but at some point were confused with casts. 6107 verifyFormat("virtual void foo(int *) override;"); 6108 verifyFormat("virtual void foo(char &) const;"); 6109 verifyFormat("virtual void foo(int *a, char *) const;"); 6110 verifyFormat("int a = sizeof(int *) + b;"); 6111 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6112 verifyFormat("bool b = f(g<int>) && c;"); 6113 verifyFormat("typedef void (*f)(int i) func;"); 6114 6115 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6116 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6117 // FIXME: The indentation here is not ideal. 6118 verifyFormat( 6119 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6120 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6121 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6122 } 6123 6124 TEST_F(FormatTest, FormatsFunctionTypes) { 6125 verifyFormat("A<bool()> a;"); 6126 verifyFormat("A<SomeType()> a;"); 6127 verifyFormat("A<void (*)(int, std::string)> a;"); 6128 verifyFormat("A<void *(int)>;"); 6129 verifyFormat("void *(*a)(int *, SomeType *);"); 6130 verifyFormat("int (*func)(void *);"); 6131 verifyFormat("void f() { int (*func)(void *); }"); 6132 verifyFormat("template <class CallbackClass>\n" 6133 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6134 6135 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6136 verifyGoogleFormat("void* (*a)(int);"); 6137 verifyGoogleFormat( 6138 "template <class CallbackClass>\n" 6139 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6140 6141 // Other constructs can look somewhat like function types: 6142 verifyFormat("A<sizeof(*x)> a;"); 6143 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6144 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6145 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6146 verifyFormat("int x = f(&h)();"); 6147 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6148 } 6149 6150 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6151 verifyFormat("A (*foo_)[6];"); 6152 verifyFormat("vector<int> (*foo_)[6];"); 6153 } 6154 6155 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6156 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6157 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6158 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6159 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6160 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6161 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6162 6163 // Different ways of ()-initializiation. 6164 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6165 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6166 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6167 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6168 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6169 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6170 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6171 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6172 } 6173 6174 TEST_F(FormatTest, BreaksLongDeclarations) { 6175 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6176 " AnotherNameForTheLongType;"); 6177 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6179 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6180 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6181 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6182 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6183 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6184 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6185 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6186 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6187 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6188 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6189 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6190 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6191 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6192 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6193 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6194 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6195 FormatStyle Indented = getLLVMStyle(); 6196 Indented.IndentWrappedFunctionNames = true; 6197 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6198 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6199 Indented); 6200 verifyFormat( 6201 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6202 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6203 Indented); 6204 verifyFormat( 6205 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6206 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6207 Indented); 6208 verifyFormat( 6209 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6210 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6211 Indented); 6212 6213 // FIXME: Without the comment, this breaks after "(". 6214 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6215 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6216 getGoogleStyle()); 6217 6218 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6219 " int LoooooooooooooooooooongParam2) {}"); 6220 verifyFormat( 6221 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6222 " SourceLocation L, IdentifierIn *II,\n" 6223 " Type *T) {}"); 6224 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6225 "ReallyReaaallyLongFunctionName(\n" 6226 " const std::string &SomeParameter,\n" 6227 " const SomeType<string, SomeOtherTemplateParameter>\n" 6228 " &ReallyReallyLongParameterName,\n" 6229 " const SomeType<string, SomeOtherTemplateParameter>\n" 6230 " &AnotherLongParameterName) {}"); 6231 verifyFormat("template <typename A>\n" 6232 "SomeLoooooooooooooooooooooongType<\n" 6233 " typename some_namespace::SomeOtherType<A>::Type>\n" 6234 "Function() {}"); 6235 6236 verifyGoogleFormat( 6237 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6238 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6239 verifyGoogleFormat( 6240 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6241 " SourceLocation L) {}"); 6242 verifyGoogleFormat( 6243 "some_namespace::LongReturnType\n" 6244 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6245 " int first_long_parameter, int second_parameter) {}"); 6246 6247 verifyGoogleFormat("template <typename T>\n" 6248 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6249 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6250 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6251 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6252 6253 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6254 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6255 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6256 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6257 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6258 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6259 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6260 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6262 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6263 } 6264 6265 TEST_F(FormatTest, FormatsArrays) { 6266 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6267 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6268 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6269 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6270 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6271 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6272 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6273 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6274 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6275 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6276 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6277 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6278 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6279 verifyFormat( 6280 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6281 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6282 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6283 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6284 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6285 6286 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6288 verifyFormat( 6289 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6290 " .aaaaaaa[0]\n" 6291 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6292 verifyFormat("a[::b::c];"); 6293 6294 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6295 6296 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6297 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6298 } 6299 6300 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6301 verifyFormat("(a)->b();"); 6302 verifyFormat("--a;"); 6303 } 6304 6305 TEST_F(FormatTest, HandlesIncludeDirectives) { 6306 verifyFormat("#include <string>\n" 6307 "#include <a/b/c.h>\n" 6308 "#include \"a/b/string\"\n" 6309 "#include \"string.h\"\n" 6310 "#include \"string.h\"\n" 6311 "#include <a-a>\n" 6312 "#include < path with space >\n" 6313 "#include_next <test.h>" 6314 "#include \"abc.h\" // this is included for ABC\n" 6315 "#include \"some long include\" // with a comment\n" 6316 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6317 getLLVMStyleWithColumns(35)); 6318 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6319 EXPECT_EQ("#include <a>", format("#include<a>")); 6320 6321 verifyFormat("#import <string>"); 6322 verifyFormat("#import <a/b/c.h>"); 6323 verifyFormat("#import \"a/b/string\""); 6324 verifyFormat("#import \"string.h\""); 6325 verifyFormat("#import \"string.h\""); 6326 verifyFormat("#if __has_include(<strstream>)\n" 6327 "#include <strstream>\n" 6328 "#endif"); 6329 6330 verifyFormat("#define MY_IMPORT <a/b>"); 6331 6332 // Protocol buffer definition or missing "#". 6333 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6334 getLLVMStyleWithColumns(30)); 6335 6336 FormatStyle Style = getLLVMStyle(); 6337 Style.AlwaysBreakBeforeMultilineStrings = true; 6338 Style.ColumnLimit = 0; 6339 verifyFormat("#import \"abc.h\"", Style); 6340 6341 // But 'import' might also be a regular C++ namespace. 6342 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6344 } 6345 6346 //===----------------------------------------------------------------------===// 6347 // Error recovery tests. 6348 //===----------------------------------------------------------------------===// 6349 6350 TEST_F(FormatTest, IncompleteParameterLists) { 6351 FormatStyle NoBinPacking = getLLVMStyle(); 6352 NoBinPacking.BinPackParameters = false; 6353 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6354 " double *min_x,\n" 6355 " double *max_x,\n" 6356 " double *min_y,\n" 6357 " double *max_y,\n" 6358 " double *min_z,\n" 6359 " double *max_z, ) {}", 6360 NoBinPacking); 6361 } 6362 6363 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6364 verifyFormat("void f() { return; }\n42"); 6365 verifyFormat("void f() {\n" 6366 " if (0)\n" 6367 " return;\n" 6368 "}\n" 6369 "42"); 6370 verifyFormat("void f() { return }\n42"); 6371 verifyFormat("void f() {\n" 6372 " if (0)\n" 6373 " return\n" 6374 "}\n" 6375 "42"); 6376 } 6377 6378 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6379 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6380 EXPECT_EQ("void f() {\n" 6381 " if (a)\n" 6382 " return\n" 6383 "}", 6384 format("void f ( ) { if ( a ) return }")); 6385 EXPECT_EQ("namespace N {\n" 6386 "void f()\n" 6387 "}", 6388 format("namespace N { void f() }")); 6389 EXPECT_EQ("namespace N {\n" 6390 "void f() {}\n" 6391 "void g()\n" 6392 "}", 6393 format("namespace N { void f( ) { } void g( ) }")); 6394 } 6395 6396 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6397 verifyFormat("int aaaaaaaa =\n" 6398 " // Overlylongcomment\n" 6399 " b;", 6400 getLLVMStyleWithColumns(20)); 6401 verifyFormat("function(\n" 6402 " ShortArgument,\n" 6403 " LoooooooooooongArgument);\n", 6404 getLLVMStyleWithColumns(20)); 6405 } 6406 6407 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6408 verifyFormat("public:"); 6409 verifyFormat("class A {\n" 6410 "public\n" 6411 " void f() {}\n" 6412 "};"); 6413 verifyFormat("public\n" 6414 "int qwerty;"); 6415 verifyFormat("public\n" 6416 "B {}"); 6417 verifyFormat("public\n" 6418 "{}"); 6419 verifyFormat("public\n" 6420 "B { int x; }"); 6421 } 6422 6423 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6424 verifyFormat("{"); 6425 verifyFormat("#})"); 6426 verifyNoCrash("(/**/[:!] ?[)."); 6427 } 6428 6429 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6430 verifyFormat("do {\n}"); 6431 verifyFormat("do {\n}\n" 6432 "f();"); 6433 verifyFormat("do {\n}\n" 6434 "wheeee(fun);"); 6435 verifyFormat("do {\n" 6436 " f();\n" 6437 "}"); 6438 } 6439 6440 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6441 verifyFormat("if {\n foo;\n foo();\n}"); 6442 verifyFormat("switch {\n foo;\n foo();\n}"); 6443 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6444 verifyFormat("while {\n foo;\n foo();\n}"); 6445 verifyFormat("do {\n foo;\n foo();\n} while;"); 6446 } 6447 6448 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6449 verifyIncompleteFormat("namespace {\n" 6450 "class Foo { Foo (\n" 6451 "};\n" 6452 "} // comment"); 6453 } 6454 6455 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6456 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6457 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6458 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6459 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6460 6461 EXPECT_EQ("{\n" 6462 " {\n" 6463 " breakme(\n" 6464 " qwe);\n" 6465 " }\n", 6466 format("{\n" 6467 " {\n" 6468 " breakme(qwe);\n" 6469 "}\n", 6470 getLLVMStyleWithColumns(10))); 6471 } 6472 6473 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6474 verifyFormat("int x = {\n" 6475 " avariable,\n" 6476 " b(alongervariable)};", 6477 getLLVMStyleWithColumns(25)); 6478 } 6479 6480 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6481 verifyFormat("return (a)(b){1, 2, 3};"); 6482 } 6483 6484 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6485 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6486 verifyFormat("vector<int> x{\n" 6487 " 1, 2, 3, 4,\n" 6488 "};"); 6489 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6490 verifyFormat("f({1, 2});"); 6491 verifyFormat("auto v = Foo{-1};"); 6492 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6493 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6494 verifyFormat("new vector<int>{1, 2, 3};"); 6495 verifyFormat("new int[3]{1, 2, 3};"); 6496 verifyFormat("new int{1};"); 6497 verifyFormat("return {arg1, arg2};"); 6498 verifyFormat("return {arg1, SomeType{parameter}};"); 6499 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6500 verifyFormat("new T{arg1, arg2};"); 6501 verifyFormat("f(MyMap[{composite, key}]);"); 6502 verifyFormat("class Class {\n" 6503 " T member = {arg1, arg2};\n" 6504 "};"); 6505 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6506 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6507 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6508 6509 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6510 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6511 verifyFormat("auto i = decltype(x){};"); 6512 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6513 verifyFormat("Node n{1, Node{1000}, //\n" 6514 " 2};"); 6515 verifyFormat("Aaaa aaaaaaa{\n" 6516 " {\n" 6517 " aaaa,\n" 6518 " },\n" 6519 "};"); 6520 verifyFormat("class C : public D {\n" 6521 " SomeClass SC{2};\n" 6522 "};"); 6523 verifyFormat("class C : public A {\n" 6524 " class D : public B {\n" 6525 " void f() { int i{2}; }\n" 6526 " };\n" 6527 "};"); 6528 verifyFormat("#define A {a, a},"); 6529 6530 // Cases where distinguising braced lists and blocks is hard. 6531 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6532 verifyFormat("void f() {\n" 6533 " return; // comment\n" 6534 "}\n" 6535 "SomeType t;"); 6536 verifyFormat("void f() {\n" 6537 " if (a) {\n" 6538 " f();\n" 6539 " }\n" 6540 "}\n" 6541 "SomeType t;"); 6542 6543 // In combination with BinPackArguments = false. 6544 FormatStyle NoBinPacking = getLLVMStyle(); 6545 NoBinPacking.BinPackArguments = false; 6546 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6547 " bbbbb,\n" 6548 " ccccc,\n" 6549 " ddddd,\n" 6550 " eeeee,\n" 6551 " ffffff,\n" 6552 " ggggg,\n" 6553 " hhhhhh,\n" 6554 " iiiiii,\n" 6555 " jjjjjj,\n" 6556 " kkkkkk};", 6557 NoBinPacking); 6558 verifyFormat("const Aaaaaa aaaaa = {\n" 6559 " aaaaa,\n" 6560 " bbbbb,\n" 6561 " ccccc,\n" 6562 " ddddd,\n" 6563 " eeeee,\n" 6564 " ffffff,\n" 6565 " ggggg,\n" 6566 " hhhhhh,\n" 6567 " iiiiii,\n" 6568 " jjjjjj,\n" 6569 " kkkkkk,\n" 6570 "};", 6571 NoBinPacking); 6572 verifyFormat( 6573 "const Aaaaaa aaaaa = {\n" 6574 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6575 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6576 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6577 "};", 6578 NoBinPacking); 6579 6580 // FIXME: The alignment of these trailing comments might be bad. Then again, 6581 // this might be utterly useless in real code. 6582 verifyFormat("Constructor::Constructor()\n" 6583 " : some_value{ //\n" 6584 " aaaaaaa, //\n" 6585 " bbbbbbb} {}"); 6586 6587 // In braced lists, the first comment is always assumed to belong to the 6588 // first element. Thus, it can be moved to the next or previous line as 6589 // appropriate. 6590 EXPECT_EQ("function({// First element:\n" 6591 " 1,\n" 6592 " // Second element:\n" 6593 " 2});", 6594 format("function({\n" 6595 " // First element:\n" 6596 " 1,\n" 6597 " // Second element:\n" 6598 " 2});")); 6599 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6600 " // First element:\n" 6601 " 1,\n" 6602 " // Second element:\n" 6603 " 2};", 6604 format("std::vector<int> MyNumbers{// First element:\n" 6605 " 1,\n" 6606 " // Second element:\n" 6607 " 2};", 6608 getLLVMStyleWithColumns(30))); 6609 // A trailing comma should still lead to an enforced line break. 6610 EXPECT_EQ("vector<int> SomeVector = {\n" 6611 " // aaa\n" 6612 " 1, 2,\n" 6613 "};", 6614 format("vector<int> SomeVector = { // aaa\n" 6615 " 1, 2, };")); 6616 6617 FormatStyle ExtraSpaces = getLLVMStyle(); 6618 ExtraSpaces.Cpp11BracedListStyle = false; 6619 ExtraSpaces.ColumnLimit = 75; 6620 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6621 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6622 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6623 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6624 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6625 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6626 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6627 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6628 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6629 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6630 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6631 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6632 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6633 verifyFormat("class Class {\n" 6634 " T member = { arg1, arg2 };\n" 6635 "};", 6636 ExtraSpaces); 6637 verifyFormat( 6638 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6639 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6640 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6641 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6642 ExtraSpaces); 6643 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6644 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6645 ExtraSpaces); 6646 verifyFormat( 6647 "someFunction(OtherParam,\n" 6648 " BracedList{ // comment 1 (Forcing interesting break)\n" 6649 " param1, param2,\n" 6650 " // comment 2\n" 6651 " param3, param4 });", 6652 ExtraSpaces); 6653 verifyFormat( 6654 "std::this_thread::sleep_for(\n" 6655 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6656 ExtraSpaces); 6657 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6658 " aaaaaaa,\n" 6659 " aaaaaaaaaa,\n" 6660 " aaaaa,\n" 6661 " aaaaaaaaaaaaaaa,\n" 6662 " aaa,\n" 6663 " aaaaaaaaaa,\n" 6664 " a,\n" 6665 " aaaaaaaaaaaaaaaaaaaaa,\n" 6666 " aaaaaaaaaaaa,\n" 6667 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6668 " aaaaaaa,\n" 6669 " a};"); 6670 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6671 } 6672 6673 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6674 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6675 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6676 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6677 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6678 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6679 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6680 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6681 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6682 " 1, 22, 333, 4444, 55555, //\n" 6683 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6684 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6685 verifyFormat( 6686 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6687 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6688 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6689 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6690 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6691 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6692 " 7777777};"); 6693 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6694 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6695 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6696 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6697 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6698 " // Separating comment.\n" 6699 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6700 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6701 " // Leading comment\n" 6702 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6703 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6704 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6705 " 1, 1, 1, 1};", 6706 getLLVMStyleWithColumns(39)); 6707 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6708 " 1, 1, 1, 1};", 6709 getLLVMStyleWithColumns(38)); 6710 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6711 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6712 getLLVMStyleWithColumns(43)); 6713 verifyFormat( 6714 "static unsigned SomeValues[10][3] = {\n" 6715 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6716 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6717 verifyFormat("static auto fields = new vector<string>{\n" 6718 " \"aaaaaaaaaaaaa\",\n" 6719 " \"aaaaaaaaaaaaa\",\n" 6720 " \"aaaaaaaaaaaa\",\n" 6721 " \"aaaaaaaaaaaaaa\",\n" 6722 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6723 " \"aaaaaaaaaaaa\",\n" 6724 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6725 "};"); 6726 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6727 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6728 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6729 " 3, cccccccccccccccccccccc};", 6730 getLLVMStyleWithColumns(60)); 6731 6732 // Trailing commas. 6733 verifyFormat("vector<int> x = {\n" 6734 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6735 "};", 6736 getLLVMStyleWithColumns(39)); 6737 verifyFormat("vector<int> x = {\n" 6738 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6739 "};", 6740 getLLVMStyleWithColumns(39)); 6741 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6742 " 1, 1, 1, 1,\n" 6743 " /**/ /**/};", 6744 getLLVMStyleWithColumns(39)); 6745 6746 // Trailing comment in the first line. 6747 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6748 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6749 " 111111111, 222222222, 3333333333, 444444444, //\n" 6750 " 11111111, 22222222, 333333333, 44444444};"); 6751 // Trailing comment in the last line. 6752 verifyFormat("int aaaaa[] = {\n" 6753 " 1, 2, 3, // comment\n" 6754 " 4, 5, 6 // comment\n" 6755 "};"); 6756 6757 // With nested lists, we should either format one item per line or all nested 6758 // lists one on line. 6759 // FIXME: For some nested lists, we can do better. 6760 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6761 " {aaaaaaaaaaaaaaaaaaa},\n" 6762 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6763 " {aaaaaaaaaaaaaaaaa}};", 6764 getLLVMStyleWithColumns(60)); 6765 verifyFormat( 6766 "SomeStruct my_struct_array = {\n" 6767 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6768 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6769 " {aaa, aaa},\n" 6770 " {aaa, aaa},\n" 6771 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6772 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6773 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6774 6775 // No column layout should be used here. 6776 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6777 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6778 6779 verifyNoCrash("a<,"); 6780 6781 // No braced initializer here. 6782 verifyFormat("void f() {\n" 6783 " struct Dummy {};\n" 6784 " f(v);\n" 6785 "}"); 6786 6787 // Long lists should be formatted in columns even if they are nested. 6788 verifyFormat( 6789 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6790 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6791 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6792 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6793 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6794 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6795 6796 // Allow "single-column" layout even if that violates the column limit. There 6797 // isn't going to be a better way. 6798 verifyFormat("std::vector<int> a = {\n" 6799 " aaaaaaaa,\n" 6800 " aaaaaaaa,\n" 6801 " aaaaaaaa,\n" 6802 " aaaaaaaa,\n" 6803 " aaaaaaaaaa,\n" 6804 " aaaaaaaa,\n" 6805 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6806 getLLVMStyleWithColumns(30)); 6807 verifyFormat("vector<int> aaaa = {\n" 6808 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6809 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6810 " aaaaaa.aaaaaaa,\n" 6811 " aaaaaa.aaaaaaa,\n" 6812 " aaaaaa.aaaaaaa,\n" 6813 " aaaaaa.aaaaaaa,\n" 6814 "};"); 6815 } 6816 6817 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6818 FormatStyle DoNotMerge = getLLVMStyle(); 6819 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6820 6821 verifyFormat("void f() { return 42; }"); 6822 verifyFormat("void f() {\n" 6823 " return 42;\n" 6824 "}", 6825 DoNotMerge); 6826 verifyFormat("void f() {\n" 6827 " // Comment\n" 6828 "}"); 6829 verifyFormat("{\n" 6830 "#error {\n" 6831 " int a;\n" 6832 "}"); 6833 verifyFormat("{\n" 6834 " int a;\n" 6835 "#error {\n" 6836 "}"); 6837 verifyFormat("void f() {} // comment"); 6838 verifyFormat("void f() { int a; } // comment"); 6839 verifyFormat("void f() {\n" 6840 "} // comment", 6841 DoNotMerge); 6842 verifyFormat("void f() {\n" 6843 " int a;\n" 6844 "} // comment", 6845 DoNotMerge); 6846 verifyFormat("void f() {\n" 6847 "} // comment", 6848 getLLVMStyleWithColumns(15)); 6849 6850 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6851 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6852 6853 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6854 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6855 verifyFormat("class C {\n" 6856 " C()\n" 6857 " : iiiiiiii(nullptr),\n" 6858 " kkkkkkk(nullptr),\n" 6859 " mmmmmmm(nullptr),\n" 6860 " nnnnnnn(nullptr) {}\n" 6861 "};", 6862 getGoogleStyle()); 6863 6864 FormatStyle NoColumnLimit = getLLVMStyle(); 6865 NoColumnLimit.ColumnLimit = 0; 6866 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6867 EXPECT_EQ("class C {\n" 6868 " A() : b(0) {}\n" 6869 "};", 6870 format("class C{A():b(0){}};", NoColumnLimit)); 6871 EXPECT_EQ("A()\n" 6872 " : b(0) {\n" 6873 "}", 6874 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6875 6876 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6877 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6878 FormatStyle::SFS_None; 6879 EXPECT_EQ("A()\n" 6880 " : b(0) {\n" 6881 "}", 6882 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6883 EXPECT_EQ("A()\n" 6884 " : b(0) {\n" 6885 "}", 6886 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6887 6888 verifyFormat("#define A \\\n" 6889 " void f() { \\\n" 6890 " int i; \\\n" 6891 " }", 6892 getLLVMStyleWithColumns(20)); 6893 verifyFormat("#define A \\\n" 6894 " void f() { int i; }", 6895 getLLVMStyleWithColumns(21)); 6896 verifyFormat("#define A \\\n" 6897 " void f() { \\\n" 6898 " int i; \\\n" 6899 " } \\\n" 6900 " int j;", 6901 getLLVMStyleWithColumns(22)); 6902 verifyFormat("#define A \\\n" 6903 " void f() { int i; } \\\n" 6904 " int j;", 6905 getLLVMStyleWithColumns(23)); 6906 } 6907 6908 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6909 FormatStyle MergeInlineOnly = getLLVMStyle(); 6910 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6911 verifyFormat("class C {\n" 6912 " int f() { return 42; }\n" 6913 "};", 6914 MergeInlineOnly); 6915 verifyFormat("int f() {\n" 6916 " return 42;\n" 6917 "}", 6918 MergeInlineOnly); 6919 } 6920 6921 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6922 // Elaborate type variable declarations. 6923 verifyFormat("struct foo a = {bar};\nint n;"); 6924 verifyFormat("class foo a = {bar};\nint n;"); 6925 verifyFormat("union foo a = {bar};\nint n;"); 6926 6927 // Elaborate types inside function definitions. 6928 verifyFormat("struct foo f() {}\nint n;"); 6929 verifyFormat("class foo f() {}\nint n;"); 6930 verifyFormat("union foo f() {}\nint n;"); 6931 6932 // Templates. 6933 verifyFormat("template <class X> void f() {}\nint n;"); 6934 verifyFormat("template <struct X> void f() {}\nint n;"); 6935 verifyFormat("template <union X> void f() {}\nint n;"); 6936 6937 // Actual definitions... 6938 verifyFormat("struct {\n} n;"); 6939 verifyFormat( 6940 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6941 verifyFormat("union Z {\n int n;\n} x;"); 6942 verifyFormat("class MACRO Z {\n} n;"); 6943 verifyFormat("class MACRO(X) Z {\n} n;"); 6944 verifyFormat("class __attribute__(X) Z {\n} n;"); 6945 verifyFormat("class __declspec(X) Z {\n} n;"); 6946 verifyFormat("class A##B##C {\n} n;"); 6947 verifyFormat("class alignas(16) Z {\n} n;"); 6948 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6949 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6950 6951 // Redefinition from nested context: 6952 verifyFormat("class A::B::C {\n} n;"); 6953 6954 // Template definitions. 6955 verifyFormat( 6956 "template <typename F>\n" 6957 "Matcher(const Matcher<F> &Other,\n" 6958 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6959 " !is_same<F, T>::value>::type * = 0)\n" 6960 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6961 6962 // FIXME: This is still incorrectly handled at the formatter side. 6963 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6964 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6965 6966 // FIXME: 6967 // This now gets parsed incorrectly as class definition. 6968 // verifyFormat("class A<int> f() {\n}\nint n;"); 6969 6970 // Elaborate types where incorrectly parsing the structural element would 6971 // break the indent. 6972 verifyFormat("if (true)\n" 6973 " class X x;\n" 6974 "else\n" 6975 " f();\n"); 6976 6977 // This is simply incomplete. Formatting is not important, but must not crash. 6978 verifyFormat("class A:"); 6979 } 6980 6981 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6982 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6983 format("#error Leave all white!!!!! space* alone!\n")); 6984 EXPECT_EQ( 6985 "#warning Leave all white!!!!! space* alone!\n", 6986 format("#warning Leave all white!!!!! space* alone!\n")); 6987 EXPECT_EQ("#error 1", format(" # error 1")); 6988 EXPECT_EQ("#warning 1", format(" # warning 1")); 6989 } 6990 6991 TEST_F(FormatTest, FormatHashIfExpressions) { 6992 verifyFormat("#if AAAA && BBBB"); 6993 verifyFormat("#if (AAAA && BBBB)"); 6994 verifyFormat("#elif (AAAA && BBBB)"); 6995 // FIXME: Come up with a better indentation for #elif. 6996 verifyFormat( 6997 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6998 " defined(BBBBBBBB)\n" 6999 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7000 " defined(BBBBBBBB)\n" 7001 "#endif", 7002 getLLVMStyleWithColumns(65)); 7003 } 7004 7005 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7006 FormatStyle AllowsMergedIf = getGoogleStyle(); 7007 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7008 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7009 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7010 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7011 EXPECT_EQ("if (true) return 42;", 7012 format("if (true)\nreturn 42;", AllowsMergedIf)); 7013 FormatStyle ShortMergedIf = AllowsMergedIf; 7014 ShortMergedIf.ColumnLimit = 25; 7015 verifyFormat("#define A \\\n" 7016 " if (true) return 42;", 7017 ShortMergedIf); 7018 verifyFormat("#define A \\\n" 7019 " f(); \\\n" 7020 " if (true)\n" 7021 "#define B", 7022 ShortMergedIf); 7023 verifyFormat("#define A \\\n" 7024 " f(); \\\n" 7025 " if (true)\n" 7026 "g();", 7027 ShortMergedIf); 7028 verifyFormat("{\n" 7029 "#ifdef A\n" 7030 " // Comment\n" 7031 " if (true) continue;\n" 7032 "#endif\n" 7033 " // Comment\n" 7034 " if (true) continue;\n" 7035 "}", 7036 ShortMergedIf); 7037 ShortMergedIf.ColumnLimit = 29; 7038 verifyFormat("#define A \\\n" 7039 " if (aaaaaaaaaa) return 1; \\\n" 7040 " return 2;", 7041 ShortMergedIf); 7042 ShortMergedIf.ColumnLimit = 28; 7043 verifyFormat("#define A \\\n" 7044 " if (aaaaaaaaaa) \\\n" 7045 " return 1; \\\n" 7046 " return 2;", 7047 ShortMergedIf); 7048 } 7049 7050 TEST_F(FormatTest, BlockCommentsInControlLoops) { 7051 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7052 " f();\n" 7053 "}"); 7054 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7055 " f();\n" 7056 "} /* another comment */ else /* comment #3 */ {\n" 7057 " g();\n" 7058 "}"); 7059 verifyFormat("while (0) /* a comment in a strange place */ {\n" 7060 " f();\n" 7061 "}"); 7062 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 7063 " f();\n" 7064 "}"); 7065 verifyFormat("do /* a comment in a strange place */ {\n" 7066 " f();\n" 7067 "} /* another comment */ while (0);"); 7068 } 7069 7070 TEST_F(FormatTest, BlockComments) { 7071 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 7072 format("/* *//* */ /* */\n/* *//* */ /* */")); 7073 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 7074 EXPECT_EQ("#define A /*123*/ \\\n" 7075 " b\n" 7076 "/* */\n" 7077 "someCall(\n" 7078 " parameter);", 7079 format("#define A /*123*/ b\n" 7080 "/* */\n" 7081 "someCall(parameter);", 7082 getLLVMStyleWithColumns(15))); 7083 7084 EXPECT_EQ("#define A\n" 7085 "/* */ someCall(\n" 7086 " parameter);", 7087 format("#define A\n" 7088 "/* */someCall(parameter);", 7089 getLLVMStyleWithColumns(15))); 7090 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7091 EXPECT_EQ("/*\n" 7092 "*\n" 7093 " * aaaaaa\n" 7094 " * aaaaaa\n" 7095 "*/", 7096 format("/*\n" 7097 "*\n" 7098 " * aaaaaa aaaaaa\n" 7099 "*/", 7100 getLLVMStyleWithColumns(10))); 7101 EXPECT_EQ("/*\n" 7102 "**\n" 7103 "* aaaaaa\n" 7104 "*aaaaaa\n" 7105 "*/", 7106 format("/*\n" 7107 "**\n" 7108 "* aaaaaa aaaaaa\n" 7109 "*/", 7110 getLLVMStyleWithColumns(10))); 7111 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7112 " /* line 1\n" 7113 " bbbbbbbbbbbb */\n" 7114 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7115 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7116 " /* line 1\n" 7117 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7118 getLLVMStyleWithColumns(50))); 7119 7120 FormatStyle NoBinPacking = getLLVMStyle(); 7121 NoBinPacking.BinPackParameters = false; 7122 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7123 " 2, /* comment 2 */\n" 7124 " 3, /* comment 3 */\n" 7125 " aaaa,\n" 7126 " bbbb);", 7127 format("someFunction (1, /* comment 1 */\n" 7128 " 2, /* comment 2 */ \n" 7129 " 3, /* comment 3 */\n" 7130 "aaaa, bbbb );", 7131 NoBinPacking)); 7132 verifyFormat( 7133 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7135 EXPECT_EQ( 7136 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7137 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7139 format( 7140 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7141 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7143 EXPECT_EQ( 7144 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7145 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7146 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7147 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7148 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7149 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7150 7151 verifyFormat("void f(int * /* unused */) {}"); 7152 7153 EXPECT_EQ("/*\n" 7154 " **\n" 7155 " */", 7156 format("/*\n" 7157 " **\n" 7158 " */")); 7159 EXPECT_EQ("/*\n" 7160 " *q\n" 7161 " */", 7162 format("/*\n" 7163 " *q\n" 7164 " */")); 7165 EXPECT_EQ("/*\n" 7166 " * q\n" 7167 " */", 7168 format("/*\n" 7169 " * q\n" 7170 " */")); 7171 EXPECT_EQ("/*\n" 7172 " **/", 7173 format("/*\n" 7174 " **/")); 7175 EXPECT_EQ("/*\n" 7176 " ***/", 7177 format("/*\n" 7178 " ***/")); 7179 } 7180 7181 TEST_F(FormatTest, BlockCommentsInMacros) { 7182 EXPECT_EQ("#define A \\\n" 7183 " { \\\n" 7184 " /* one line */ \\\n" 7185 " someCall();", 7186 format("#define A { \\\n" 7187 " /* one line */ \\\n" 7188 " someCall();", 7189 getLLVMStyleWithColumns(20))); 7190 EXPECT_EQ("#define A \\\n" 7191 " { \\\n" 7192 " /* previous */ \\\n" 7193 " /* one line */ \\\n" 7194 " someCall();", 7195 format("#define A { \\\n" 7196 " /* previous */ \\\n" 7197 " /* one line */ \\\n" 7198 " someCall();", 7199 getLLVMStyleWithColumns(20))); 7200 } 7201 7202 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7203 EXPECT_EQ("a = {\n" 7204 " 1111 /* */\n" 7205 "};", 7206 format("a = {1111 /* */\n" 7207 "};", 7208 getLLVMStyleWithColumns(15))); 7209 EXPECT_EQ("a = {\n" 7210 " 1111 /* */\n" 7211 "};", 7212 format("a = {1111 /* */\n" 7213 "};", 7214 getLLVMStyleWithColumns(15))); 7215 7216 // FIXME: The formatting is still wrong here. 7217 EXPECT_EQ("a = {\n" 7218 " 1111 /* a\n" 7219 " */\n" 7220 "};", 7221 format("a = {1111 /* a */\n" 7222 "};", 7223 getLLVMStyleWithColumns(15))); 7224 } 7225 7226 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7227 verifyFormat("{\n" 7228 " // a\n" 7229 " // b"); 7230 } 7231 7232 TEST_F(FormatTest, FormatStarDependingOnContext) { 7233 verifyFormat("void f(int *a);"); 7234 verifyFormat("void f() { f(fint * b); }"); 7235 verifyFormat("class A {\n void f(int *a);\n};"); 7236 verifyFormat("class A {\n int *a;\n};"); 7237 verifyFormat("namespace a {\n" 7238 "namespace b {\n" 7239 "class A {\n" 7240 " void f() {}\n" 7241 " int *a;\n" 7242 "};\n" 7243 "}\n" 7244 "}"); 7245 } 7246 7247 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7248 verifyFormat("while"); 7249 verifyFormat("operator"); 7250 } 7251 7252 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7253 // This code would be painfully slow to format if we didn't skip it. 7254 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 7255 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7256 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7257 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7258 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7259 "A(1, 1)\n" 7260 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7261 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7262 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7263 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7264 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7265 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7266 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7267 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7268 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7269 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7270 // Deeply nested part is untouched, rest is formatted. 7271 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7272 format(std::string("int i;\n") + Code + "int j;\n", 7273 getLLVMStyle(), IC_ExpectIncomplete)); 7274 } 7275 7276 //===----------------------------------------------------------------------===// 7277 // Objective-C tests. 7278 //===----------------------------------------------------------------------===// 7279 7280 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7281 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7282 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7283 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7284 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7285 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7286 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7287 format("-(NSInteger)Method3:(id)anObject;")); 7288 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7289 format("-(NSInteger)Method4:(id)anObject;")); 7290 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7291 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7292 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7293 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7294 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7295 "forAllCells:(BOOL)flag;", 7296 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7297 "forAllCells:(BOOL)flag;")); 7298 7299 // Very long objectiveC method declaration. 7300 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7301 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7302 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7303 " inRange:(NSRange)range\n" 7304 " outRange:(NSRange)out_range\n" 7305 " outRange1:(NSRange)out_range1\n" 7306 " outRange2:(NSRange)out_range2\n" 7307 " outRange3:(NSRange)out_range3\n" 7308 " outRange4:(NSRange)out_range4\n" 7309 " outRange5:(NSRange)out_range5\n" 7310 " outRange6:(NSRange)out_range6\n" 7311 " outRange7:(NSRange)out_range7\n" 7312 " outRange8:(NSRange)out_range8\n" 7313 " outRange9:(NSRange)out_range9;"); 7314 7315 // When the function name has to be wrapped. 7316 FormatStyle Style = getLLVMStyle(); 7317 Style.IndentWrappedFunctionNames = false; 7318 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7319 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7320 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7321 "}", 7322 Style); 7323 Style.IndentWrappedFunctionNames = true; 7324 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7325 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7326 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7327 "}", 7328 Style); 7329 7330 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7331 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7332 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7333 // protocol lists (but not for template classes): 7334 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7335 7336 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7337 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7338 7339 // If there's no return type (very rare in practice!), LLVM and Google style 7340 // agree. 7341 verifyFormat("- foo;"); 7342 verifyFormat("- foo:(int)f;"); 7343 verifyGoogleFormat("- foo:(int)foo;"); 7344 } 7345 7346 7347 TEST_F(FormatTest, BreaksStringLiterals) { 7348 EXPECT_EQ("\"some text \"\n" 7349 "\"other\";", 7350 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7351 EXPECT_EQ("\"some text \"\n" 7352 "\"other\";", 7353 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7354 EXPECT_EQ( 7355 "#define A \\\n" 7356 " \"some \" \\\n" 7357 " \"text \" \\\n" 7358 " \"other\";", 7359 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7360 EXPECT_EQ( 7361 "#define A \\\n" 7362 " \"so \" \\\n" 7363 " \"text \" \\\n" 7364 " \"other\";", 7365 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7366 7367 EXPECT_EQ("\"some text\"", 7368 format("\"some text\"", getLLVMStyleWithColumns(1))); 7369 EXPECT_EQ("\"some text\"", 7370 format("\"some text\"", getLLVMStyleWithColumns(11))); 7371 EXPECT_EQ("\"some \"\n" 7372 "\"text\"", 7373 format("\"some text\"", getLLVMStyleWithColumns(10))); 7374 EXPECT_EQ("\"some \"\n" 7375 "\"text\"", 7376 format("\"some text\"", getLLVMStyleWithColumns(7))); 7377 EXPECT_EQ("\"some\"\n" 7378 "\" tex\"\n" 7379 "\"t\"", 7380 format("\"some text\"", getLLVMStyleWithColumns(6))); 7381 EXPECT_EQ("\"some\"\n" 7382 "\" tex\"\n" 7383 "\" and\"", 7384 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7385 EXPECT_EQ("\"some\"\n" 7386 "\"/tex\"\n" 7387 "\"/and\"", 7388 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7389 7390 EXPECT_EQ("variable =\n" 7391 " \"long string \"\n" 7392 " \"literal\";", 7393 format("variable = \"long string literal\";", 7394 getLLVMStyleWithColumns(20))); 7395 7396 EXPECT_EQ("variable = f(\n" 7397 " \"long string \"\n" 7398 " \"literal\",\n" 7399 " short,\n" 7400 " loooooooooooooooooooong);", 7401 format("variable = f(\"long string literal\", short, " 7402 "loooooooooooooooooooong);", 7403 getLLVMStyleWithColumns(20))); 7404 7405 EXPECT_EQ( 7406 "f(g(\"long string \"\n" 7407 " \"literal\"),\n" 7408 " b);", 7409 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7410 EXPECT_EQ("f(g(\"long string \"\n" 7411 " \"literal\",\n" 7412 " a),\n" 7413 " b);", 7414 format("f(g(\"long string literal\", a), b);", 7415 getLLVMStyleWithColumns(20))); 7416 EXPECT_EQ( 7417 "f(\"one two\".split(\n" 7418 " variable));", 7419 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7420 EXPECT_EQ("f(\"one two three four five six \"\n" 7421 " \"seven\".split(\n" 7422 " really_looooong_variable));", 7423 format("f(\"one two three four five six seven\"." 7424 "split(really_looooong_variable));", 7425 getLLVMStyleWithColumns(33))); 7426 7427 EXPECT_EQ("f(\"some \"\n" 7428 " \"text\",\n" 7429 " other);", 7430 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7431 7432 // Only break as a last resort. 7433 verifyFormat( 7434 "aaaaaaaaaaaaaaaaaaaa(\n" 7435 " aaaaaaaaaaaaaaaaaaaa,\n" 7436 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7437 7438 EXPECT_EQ("\"splitmea\"\n" 7439 "\"trandomp\"\n" 7440 "\"oint\"", 7441 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7442 7443 EXPECT_EQ("\"split/\"\n" 7444 "\"pathat/\"\n" 7445 "\"slashes\"", 7446 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7447 7448 EXPECT_EQ("\"split/\"\n" 7449 "\"pathat/\"\n" 7450 "\"slashes\"", 7451 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7452 EXPECT_EQ("\"split at \"\n" 7453 "\"spaces/at/\"\n" 7454 "\"slashes.at.any$\"\n" 7455 "\"non-alphanumeric%\"\n" 7456 "\"1111111111characte\"\n" 7457 "\"rs\"", 7458 format("\"split at " 7459 "spaces/at/" 7460 "slashes.at." 7461 "any$non-" 7462 "alphanumeric%" 7463 "1111111111characte" 7464 "rs\"", 7465 getLLVMStyleWithColumns(20))); 7466 7467 // Verify that splitting the strings understands 7468 // Style::AlwaysBreakBeforeMultilineStrings. 7469 EXPECT_EQ( 7470 "aaaaaaaaaaaa(\n" 7471 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7472 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7473 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7474 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7475 "aaaaaaaaaaaaaaaaaaaaaa\");", 7476 getGoogleStyle())); 7477 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7478 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7479 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7480 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7481 "aaaaaaaaaaaaaaaaaaaaaa\";", 7482 getGoogleStyle())); 7483 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7484 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7485 format("llvm::outs() << " 7486 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7487 "aaaaaaaaaaaaaaaaaaa\";")); 7488 EXPECT_EQ("ffff(\n" 7489 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7490 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7491 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7492 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7493 getGoogleStyle())); 7494 7495 FormatStyle Style = getLLVMStyleWithColumns(12); 7496 Style.BreakStringLiterals = false; 7497 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7498 7499 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7500 AlignLeft.AlignEscapedNewlinesLeft = true; 7501 EXPECT_EQ("#define A \\\n" 7502 " \"some \" \\\n" 7503 " \"text \" \\\n" 7504 " \"other\";", 7505 format("#define A \"some text other\";", AlignLeft)); 7506 } 7507 7508 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7509 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7510 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7511 EXPECT_EQ("int i = a(b());", 7512 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7513 } 7514 7515 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7516 EXPECT_EQ( 7517 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7518 "(\n" 7519 " \"x\t\");", 7520 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7521 "aaaaaaa(" 7522 "\"x\t\");")); 7523 } 7524 7525 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7526 EXPECT_EQ( 7527 "u8\"utf8 string \"\n" 7528 "u8\"literal\";", 7529 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7530 EXPECT_EQ( 7531 "u\"utf16 string \"\n" 7532 "u\"literal\";", 7533 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7534 EXPECT_EQ( 7535 "U\"utf32 string \"\n" 7536 "U\"literal\";", 7537 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7538 EXPECT_EQ("L\"wide string \"\n" 7539 "L\"literal\";", 7540 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7541 EXPECT_EQ("@\"NSString \"\n" 7542 "@\"literal\";", 7543 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7544 7545 // This input makes clang-format try to split the incomplete unicode escape 7546 // sequence, which used to lead to a crasher. 7547 verifyNoCrash( 7548 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7549 getLLVMStyleWithColumns(60)); 7550 } 7551 7552 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7553 FormatStyle Style = getGoogleStyleWithColumns(15); 7554 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7555 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7556 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7557 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7558 EXPECT_EQ("u8R\"x(raw literal)x\";", 7559 format("u8R\"x(raw literal)x\";", Style)); 7560 } 7561 7562 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7563 FormatStyle Style = getLLVMStyleWithColumns(20); 7564 EXPECT_EQ( 7565 "_T(\"aaaaaaaaaaaaaa\")\n" 7566 "_T(\"aaaaaaaaaaaaaa\")\n" 7567 "_T(\"aaaaaaaaaaaa\")", 7568 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7569 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 7570 " _T(\"aaaaaa\"),\n" 7571 " z);", 7572 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7573 7574 // FIXME: Handle embedded spaces in one iteration. 7575 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7576 // "_T(\"aaaaaaaaaaaaa\")\n" 7577 // "_T(\"aaaaaaaaaaaaa\")\n" 7578 // "_T(\"a\")", 7579 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7580 // getLLVMStyleWithColumns(20))); 7581 EXPECT_EQ( 7582 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7583 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7584 EXPECT_EQ("f(\n" 7585 "#if !TEST\n" 7586 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7587 "#endif\n" 7588 " );", 7589 format("f(\n" 7590 "#if !TEST\n" 7591 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7592 "#endif\n" 7593 ");")); 7594 EXPECT_EQ("f(\n" 7595 "\n" 7596 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7597 format("f(\n" 7598 "\n" 7599 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7600 } 7601 7602 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7603 EXPECT_EQ( 7604 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7607 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7610 } 7611 7612 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7613 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7614 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7615 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7616 "multiline raw string literal xxxxxxxxxxxxxx\n" 7617 ")x\",\n" 7618 " a),\n" 7619 " b);", 7620 format("fffffffffff(g(R\"x(\n" 7621 "multiline raw string literal xxxxxxxxxxxxxx\n" 7622 ")x\", a), b);", 7623 getGoogleStyleWithColumns(20))); 7624 EXPECT_EQ("fffffffffff(\n" 7625 " g(R\"x(qqq\n" 7626 "multiline raw string literal xxxxxxxxxxxxxx\n" 7627 ")x\",\n" 7628 " a),\n" 7629 " b);", 7630 format("fffffffffff(g(R\"x(qqq\n" 7631 "multiline raw string literal xxxxxxxxxxxxxx\n" 7632 ")x\", a), b);", 7633 getGoogleStyleWithColumns(20))); 7634 7635 EXPECT_EQ("fffffffffff(R\"x(\n" 7636 "multiline raw string literal xxxxxxxxxxxxxx\n" 7637 ")x\");", 7638 format("fffffffffff(R\"x(\n" 7639 "multiline raw string literal xxxxxxxxxxxxxx\n" 7640 ")x\");", 7641 getGoogleStyleWithColumns(20))); 7642 EXPECT_EQ("fffffffffff(R\"x(\n" 7643 "multiline raw string literal xxxxxxxxxxxxxx\n" 7644 ")x\" + bbbbbb);", 7645 format("fffffffffff(R\"x(\n" 7646 "multiline raw string literal xxxxxxxxxxxxxx\n" 7647 ")x\" + bbbbbb);", 7648 getGoogleStyleWithColumns(20))); 7649 EXPECT_EQ("fffffffffff(\n" 7650 " R\"x(\n" 7651 "multiline raw string literal xxxxxxxxxxxxxx\n" 7652 ")x\" +\n" 7653 " bbbbbb);", 7654 format("fffffffffff(\n" 7655 " R\"x(\n" 7656 "multiline raw string literal xxxxxxxxxxxxxx\n" 7657 ")x\" + bbbbbb);", 7658 getGoogleStyleWithColumns(20))); 7659 } 7660 7661 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7662 verifyFormat("string a = \"unterminated;"); 7663 EXPECT_EQ("function(\"unterminated,\n" 7664 " OtherParameter);", 7665 format("function( \"unterminated,\n" 7666 " OtherParameter);")); 7667 } 7668 7669 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7670 FormatStyle Style = getLLVMStyle(); 7671 Style.Standard = FormatStyle::LS_Cpp03; 7672 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7673 format("#define x(_a) printf(\"foo\"_a);", Style)); 7674 } 7675 7676 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7677 7678 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7679 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7680 " \"ddeeefff\");", 7681 format("someFunction(\"aaabbbcccdddeeefff\");", 7682 getLLVMStyleWithColumns(25))); 7683 EXPECT_EQ("someFunction1234567890(\n" 7684 " \"aaabbbcccdddeeefff\");", 7685 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7686 getLLVMStyleWithColumns(26))); 7687 EXPECT_EQ("someFunction1234567890(\n" 7688 " \"aaabbbcccdddeeeff\"\n" 7689 " \"f\");", 7690 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7691 getLLVMStyleWithColumns(25))); 7692 EXPECT_EQ("someFunction1234567890(\n" 7693 " \"aaabbbcccdddeeeff\"\n" 7694 " \"f\");", 7695 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7696 getLLVMStyleWithColumns(24))); 7697 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7698 " \"ddde \"\n" 7699 " \"efff\");", 7700 format("someFunction(\"aaabbbcc ddde efff\");", 7701 getLLVMStyleWithColumns(25))); 7702 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7703 " \"ddeeefff\");", 7704 format("someFunction(\"aaabbbccc ddeeefff\");", 7705 getLLVMStyleWithColumns(25))); 7706 EXPECT_EQ("someFunction1234567890(\n" 7707 " \"aaabb \"\n" 7708 " \"cccdddeeefff\");", 7709 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7710 getLLVMStyleWithColumns(25))); 7711 EXPECT_EQ("#define A \\\n" 7712 " string s = \\\n" 7713 " \"123456789\" \\\n" 7714 " \"0\"; \\\n" 7715 " int i;", 7716 format("#define A string s = \"1234567890\"; int i;", 7717 getLLVMStyleWithColumns(20))); 7718 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7719 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7720 " \"dddeeeff\"\n" 7721 " \"f\");", 7722 format("someFunction(\"aaabbbcc dddeeefff\");", 7723 getLLVMStyleWithColumns(25))); 7724 } 7725 7726 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7727 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7728 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7729 EXPECT_EQ("\"test\"\n" 7730 "\"\\n\"", 7731 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7732 EXPECT_EQ("\"tes\\\\\"\n" 7733 "\"n\"", 7734 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7735 EXPECT_EQ("\"\\\\\\\\\"\n" 7736 "\"\\n\"", 7737 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7738 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7739 EXPECT_EQ("\"\\uff01\"\n" 7740 "\"test\"", 7741 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7742 EXPECT_EQ("\"\\Uff01ff02\"", 7743 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7744 EXPECT_EQ("\"\\x000000000001\"\n" 7745 "\"next\"", 7746 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7747 EXPECT_EQ("\"\\x000000000001next\"", 7748 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7749 EXPECT_EQ("\"\\x000000000001\"", 7750 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7751 EXPECT_EQ("\"test\"\n" 7752 "\"\\000000\"\n" 7753 "\"000001\"", 7754 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7755 EXPECT_EQ("\"test\\000\"\n" 7756 "\"00000000\"\n" 7757 "\"1\"", 7758 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7759 } 7760 7761 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7762 verifyFormat("void f() {\n" 7763 " return g() {}\n" 7764 " void h() {}"); 7765 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7766 "g();\n" 7767 "}"); 7768 } 7769 7770 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7771 verifyFormat( 7772 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7773 } 7774 7775 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7776 verifyFormat("class X {\n" 7777 " void f() {\n" 7778 " }\n" 7779 "};", 7780 getLLVMStyleWithColumns(12)); 7781 } 7782 7783 TEST_F(FormatTest, ConfigurableIndentWidth) { 7784 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7785 EightIndent.IndentWidth = 8; 7786 EightIndent.ContinuationIndentWidth = 8; 7787 verifyFormat("void f() {\n" 7788 " someFunction();\n" 7789 " if (true) {\n" 7790 " f();\n" 7791 " }\n" 7792 "}", 7793 EightIndent); 7794 verifyFormat("class X {\n" 7795 " void f() {\n" 7796 " }\n" 7797 "};", 7798 EightIndent); 7799 verifyFormat("int x[] = {\n" 7800 " call(),\n" 7801 " call()};", 7802 EightIndent); 7803 } 7804 7805 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7806 verifyFormat("double\n" 7807 "f();", 7808 getLLVMStyleWithColumns(8)); 7809 } 7810 7811 TEST_F(FormatTest, ConfigurableUseOfTab) { 7812 FormatStyle Tab = getLLVMStyleWithColumns(42); 7813 Tab.IndentWidth = 8; 7814 Tab.UseTab = FormatStyle::UT_Always; 7815 Tab.AlignEscapedNewlinesLeft = true; 7816 7817 EXPECT_EQ("if (aaaaaaaa && // q\n" 7818 " bb)\t\t// w\n" 7819 "\t;", 7820 format("if (aaaaaaaa &&// q\n" 7821 "bb)// w\n" 7822 ";", 7823 Tab)); 7824 EXPECT_EQ("if (aaa && bbb) // w\n" 7825 "\t;", 7826 format("if(aaa&&bbb)// w\n" 7827 ";", 7828 Tab)); 7829 7830 verifyFormat("class X {\n" 7831 "\tvoid f() {\n" 7832 "\t\tsomeFunction(parameter1,\n" 7833 "\t\t\t parameter2);\n" 7834 "\t}\n" 7835 "};", 7836 Tab); 7837 verifyFormat("#define A \\\n" 7838 "\tvoid f() { \\\n" 7839 "\t\tsomeFunction( \\\n" 7840 "\t\t parameter1, \\\n" 7841 "\t\t parameter2); \\\n" 7842 "\t}", 7843 Tab); 7844 7845 Tab.TabWidth = 4; 7846 Tab.IndentWidth = 8; 7847 verifyFormat("class TabWidth4Indent8 {\n" 7848 "\t\tvoid f() {\n" 7849 "\t\t\t\tsomeFunction(parameter1,\n" 7850 "\t\t\t\t\t\t\t parameter2);\n" 7851 "\t\t}\n" 7852 "};", 7853 Tab); 7854 7855 Tab.TabWidth = 4; 7856 Tab.IndentWidth = 4; 7857 verifyFormat("class TabWidth4Indent4 {\n" 7858 "\tvoid f() {\n" 7859 "\t\tsomeFunction(parameter1,\n" 7860 "\t\t\t\t\t parameter2);\n" 7861 "\t}\n" 7862 "};", 7863 Tab); 7864 7865 Tab.TabWidth = 8; 7866 Tab.IndentWidth = 4; 7867 verifyFormat("class TabWidth8Indent4 {\n" 7868 " void f() {\n" 7869 "\tsomeFunction(parameter1,\n" 7870 "\t\t parameter2);\n" 7871 " }\n" 7872 "};", 7873 Tab); 7874 7875 Tab.TabWidth = 8; 7876 Tab.IndentWidth = 8; 7877 EXPECT_EQ("/*\n" 7878 "\t a\t\tcomment\n" 7879 "\t in multiple lines\n" 7880 " */", 7881 format(" /*\t \t \n" 7882 " \t \t a\t\tcomment\t \t\n" 7883 " \t \t in multiple lines\t\n" 7884 " \t */", 7885 Tab)); 7886 7887 Tab.UseTab = FormatStyle::UT_ForIndentation; 7888 verifyFormat("{\n" 7889 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7890 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7891 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7892 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7893 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7894 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7895 "};", 7896 Tab); 7897 verifyFormat("enum AA {\n" 7898 "\ta1, // Force multiple lines\n" 7899 "\ta2,\n" 7900 "\ta3\n" 7901 "};", 7902 Tab); 7903 EXPECT_EQ("if (aaaaaaaa && // q\n" 7904 " bb) // w\n" 7905 "\t;", 7906 format("if (aaaaaaaa &&// q\n" 7907 "bb)// w\n" 7908 ";", 7909 Tab)); 7910 verifyFormat("class X {\n" 7911 "\tvoid f() {\n" 7912 "\t\tsomeFunction(parameter1,\n" 7913 "\t\t parameter2);\n" 7914 "\t}\n" 7915 "};", 7916 Tab); 7917 verifyFormat("{\n" 7918 "\tQ(\n" 7919 "\t {\n" 7920 "\t\t int a;\n" 7921 "\t\t someFunction(aaaaaaaa,\n" 7922 "\t\t bbbbbbb);\n" 7923 "\t },\n" 7924 "\t p);\n" 7925 "}", 7926 Tab); 7927 EXPECT_EQ("{\n" 7928 "\t/* aaaa\n" 7929 "\t bbbb */\n" 7930 "}", 7931 format("{\n" 7932 "/* aaaa\n" 7933 " bbbb */\n" 7934 "}", 7935 Tab)); 7936 EXPECT_EQ("{\n" 7937 "\t/*\n" 7938 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7939 "\t bbbbbbbbbbbbb\n" 7940 "\t*/\n" 7941 "}", 7942 format("{\n" 7943 "/*\n" 7944 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7945 "*/\n" 7946 "}", 7947 Tab)); 7948 EXPECT_EQ("{\n" 7949 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7950 "\t// bbbbbbbbbbbbb\n" 7951 "}", 7952 format("{\n" 7953 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7954 "}", 7955 Tab)); 7956 EXPECT_EQ("{\n" 7957 "\t/*\n" 7958 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7959 "\t bbbbbbbbbbbbb\n" 7960 "\t*/\n" 7961 "}", 7962 format("{\n" 7963 "\t/*\n" 7964 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7965 "\t*/\n" 7966 "}", 7967 Tab)); 7968 EXPECT_EQ("{\n" 7969 "\t/*\n" 7970 "\n" 7971 "\t*/\n" 7972 "}", 7973 format("{\n" 7974 "\t/*\n" 7975 "\n" 7976 "\t*/\n" 7977 "}", 7978 Tab)); 7979 EXPECT_EQ("{\n" 7980 "\t/*\n" 7981 " asdf\n" 7982 "\t*/\n" 7983 "}", 7984 format("{\n" 7985 "\t/*\n" 7986 " asdf\n" 7987 "\t*/\n" 7988 "}", 7989 Tab)); 7990 7991 Tab.UseTab = FormatStyle::UT_Never; 7992 EXPECT_EQ("/*\n" 7993 " a\t\tcomment\n" 7994 " in multiple lines\n" 7995 " */", 7996 format(" /*\t \t \n" 7997 " \t \t a\t\tcomment\t \t\n" 7998 " \t \t in multiple lines\t\n" 7999 " \t */", 8000 Tab)); 8001 EXPECT_EQ("/* some\n" 8002 " comment */", 8003 format(" \t \t /* some\n" 8004 " \t \t comment */", 8005 Tab)); 8006 EXPECT_EQ("int a; /* some\n" 8007 " comment */", 8008 format(" \t \t int a; /* some\n" 8009 " \t \t comment */", 8010 Tab)); 8011 8012 EXPECT_EQ("int a; /* some\n" 8013 "comment */", 8014 format(" \t \t int\ta; /* some\n" 8015 " \t \t comment */", 8016 Tab)); 8017 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8018 " comment */", 8019 format(" \t \t f(\"\t\t\"); /* some\n" 8020 " \t \t comment */", 8021 Tab)); 8022 EXPECT_EQ("{\n" 8023 " /*\n" 8024 " * Comment\n" 8025 " */\n" 8026 " int i;\n" 8027 "}", 8028 format("{\n" 8029 "\t/*\n" 8030 "\t * Comment\n" 8031 "\t */\n" 8032 "\t int i;\n" 8033 "}")); 8034 8035 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8036 Tab.TabWidth = 8; 8037 Tab.IndentWidth = 8; 8038 EXPECT_EQ("if (aaaaaaaa && // q\n" 8039 " bb) // w\n" 8040 "\t;", 8041 format("if (aaaaaaaa &&// q\n" 8042 "bb)// w\n" 8043 ";", 8044 Tab)); 8045 EXPECT_EQ("if (aaa && bbb) // w\n" 8046 "\t;", 8047 format("if(aaa&&bbb)// w\n" 8048 ";", 8049 Tab)); 8050 verifyFormat("class X {\n" 8051 "\tvoid f() {\n" 8052 "\t\tsomeFunction(parameter1,\n" 8053 "\t\t\t parameter2);\n" 8054 "\t}\n" 8055 "};", 8056 Tab); 8057 verifyFormat("#define A \\\n" 8058 "\tvoid f() { \\\n" 8059 "\t\tsomeFunction( \\\n" 8060 "\t\t parameter1, \\\n" 8061 "\t\t parameter2); \\\n" 8062 "\t}", 8063 Tab); 8064 Tab.TabWidth = 4; 8065 Tab.IndentWidth = 8; 8066 verifyFormat("class TabWidth4Indent8 {\n" 8067 "\t\tvoid f() {\n" 8068 "\t\t\t\tsomeFunction(parameter1,\n" 8069 "\t\t\t\t\t\t\t parameter2);\n" 8070 "\t\t}\n" 8071 "};", 8072 Tab); 8073 Tab.TabWidth = 4; 8074 Tab.IndentWidth = 4; 8075 verifyFormat("class TabWidth4Indent4 {\n" 8076 "\tvoid f() {\n" 8077 "\t\tsomeFunction(parameter1,\n" 8078 "\t\t\t\t\t parameter2);\n" 8079 "\t}\n" 8080 "};", 8081 Tab); 8082 Tab.TabWidth = 8; 8083 Tab.IndentWidth = 4; 8084 verifyFormat("class TabWidth8Indent4 {\n" 8085 " void f() {\n" 8086 "\tsomeFunction(parameter1,\n" 8087 "\t\t parameter2);\n" 8088 " }\n" 8089 "};", 8090 Tab); 8091 Tab.TabWidth = 8; 8092 Tab.IndentWidth = 8; 8093 EXPECT_EQ("/*\n" 8094 "\t a\t\tcomment\n" 8095 "\t in multiple lines\n" 8096 " */", 8097 format(" /*\t \t \n" 8098 " \t \t a\t\tcomment\t \t\n" 8099 " \t \t in multiple lines\t\n" 8100 " \t */", 8101 Tab)); 8102 verifyFormat("{\n" 8103 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8104 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8105 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8106 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8107 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8108 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8109 "};", 8110 Tab); 8111 verifyFormat("enum AA {\n" 8112 "\ta1, // Force multiple lines\n" 8113 "\ta2,\n" 8114 "\ta3\n" 8115 "};", 8116 Tab); 8117 EXPECT_EQ("if (aaaaaaaa && // q\n" 8118 " bb) // w\n" 8119 "\t;", 8120 format("if (aaaaaaaa &&// q\n" 8121 "bb)// w\n" 8122 ";", 8123 Tab)); 8124 verifyFormat("class X {\n" 8125 "\tvoid f() {\n" 8126 "\t\tsomeFunction(parameter1,\n" 8127 "\t\t\t parameter2);\n" 8128 "\t}\n" 8129 "};", 8130 Tab); 8131 verifyFormat("{\n" 8132 "\tQ(\n" 8133 "\t {\n" 8134 "\t\t int a;\n" 8135 "\t\t someFunction(aaaaaaaa,\n" 8136 "\t\t\t\t bbbbbbb);\n" 8137 "\t },\n" 8138 "\t p);\n" 8139 "}", 8140 Tab); 8141 EXPECT_EQ("{\n" 8142 "\t/* aaaa\n" 8143 "\t bbbb */\n" 8144 "}", 8145 format("{\n" 8146 "/* aaaa\n" 8147 " bbbb */\n" 8148 "}", 8149 Tab)); 8150 EXPECT_EQ("{\n" 8151 "\t/*\n" 8152 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8153 "\t bbbbbbbbbbbbb\n" 8154 "\t*/\n" 8155 "}", 8156 format("{\n" 8157 "/*\n" 8158 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8159 "*/\n" 8160 "}", 8161 Tab)); 8162 EXPECT_EQ("{\n" 8163 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8164 "\t// bbbbbbbbbbbbb\n" 8165 "}", 8166 format("{\n" 8167 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8168 "}", 8169 Tab)); 8170 EXPECT_EQ("{\n" 8171 "\t/*\n" 8172 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8173 "\t bbbbbbbbbbbbb\n" 8174 "\t*/\n" 8175 "}", 8176 format("{\n" 8177 "\t/*\n" 8178 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8179 "\t*/\n" 8180 "}", 8181 Tab)); 8182 EXPECT_EQ("{\n" 8183 "\t/*\n" 8184 "\n" 8185 "\t*/\n" 8186 "}", 8187 format("{\n" 8188 "\t/*\n" 8189 "\n" 8190 "\t*/\n" 8191 "}", 8192 Tab)); 8193 EXPECT_EQ("{\n" 8194 "\t/*\n" 8195 " asdf\n" 8196 "\t*/\n" 8197 "}", 8198 format("{\n" 8199 "\t/*\n" 8200 " asdf\n" 8201 "\t*/\n" 8202 "}", 8203 Tab)); 8204 EXPECT_EQ("/*\n" 8205 "\t a\t\tcomment\n" 8206 "\t in multiple lines\n" 8207 " */", 8208 format(" /*\t \t \n" 8209 " \t \t a\t\tcomment\t \t\n" 8210 " \t \t in multiple lines\t\n" 8211 " \t */", 8212 Tab)); 8213 EXPECT_EQ("/* some\n" 8214 " comment */", 8215 format(" \t \t /* some\n" 8216 " \t \t comment */", 8217 Tab)); 8218 EXPECT_EQ("int a; /* some\n" 8219 " comment */", 8220 format(" \t \t int a; /* some\n" 8221 " \t \t comment */", 8222 Tab)); 8223 EXPECT_EQ("int a; /* some\n" 8224 "comment */", 8225 format(" \t \t int\ta; /* some\n" 8226 " \t \t comment */", 8227 Tab)); 8228 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8229 " comment */", 8230 format(" \t \t f(\"\t\t\"); /* some\n" 8231 " \t \t comment */", 8232 Tab)); 8233 EXPECT_EQ("{\n" 8234 " /*\n" 8235 " * Comment\n" 8236 " */\n" 8237 " int i;\n" 8238 "}", 8239 format("{\n" 8240 "\t/*\n" 8241 "\t * Comment\n" 8242 "\t */\n" 8243 "\t int i;\n" 8244 "}")); 8245 Tab.AlignConsecutiveAssignments = true; 8246 Tab.AlignConsecutiveDeclarations = true; 8247 Tab.TabWidth = 4; 8248 Tab.IndentWidth = 4; 8249 verifyFormat("class Assign {\n" 8250 "\tvoid f() {\n" 8251 "\t\tint x = 123;\n" 8252 "\t\tint random = 4;\n" 8253 "\t\tstd::string alphabet =\n" 8254 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8255 "\t}\n" 8256 "};", 8257 Tab); 8258 } 8259 8260 TEST_F(FormatTest, CalculatesOriginalColumn) { 8261 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8262 "q\"; /* some\n" 8263 " comment */", 8264 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8265 "q\"; /* some\n" 8266 " comment */", 8267 getLLVMStyle())); 8268 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8269 "/* some\n" 8270 " comment */", 8271 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8272 " /* some\n" 8273 " comment */", 8274 getLLVMStyle())); 8275 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8276 "qqq\n" 8277 "/* some\n" 8278 " comment */", 8279 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8280 "qqq\n" 8281 " /* some\n" 8282 " comment */", 8283 getLLVMStyle())); 8284 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8285 "wwww; /* some\n" 8286 " comment */", 8287 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8288 "wwww; /* some\n" 8289 " comment */", 8290 getLLVMStyle())); 8291 } 8292 8293 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8294 FormatStyle NoSpace = getLLVMStyle(); 8295 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8296 8297 verifyFormat("while(true)\n" 8298 " continue;", 8299 NoSpace); 8300 verifyFormat("for(;;)\n" 8301 " continue;", 8302 NoSpace); 8303 verifyFormat("if(true)\n" 8304 " f();\n" 8305 "else if(true)\n" 8306 " f();", 8307 NoSpace); 8308 verifyFormat("do {\n" 8309 " do_something();\n" 8310 "} while(something());", 8311 NoSpace); 8312 verifyFormat("switch(x) {\n" 8313 "default:\n" 8314 " break;\n" 8315 "}", 8316 NoSpace); 8317 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8318 verifyFormat("size_t x = sizeof(x);", NoSpace); 8319 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8320 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8321 verifyFormat("alignas(128) char a[128];", NoSpace); 8322 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8323 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8324 verifyFormat("int f() throw(Deprecated);", NoSpace); 8325 verifyFormat("typedef void (*cb)(int);", NoSpace); 8326 verifyFormat("T A::operator()();", NoSpace); 8327 verifyFormat("X A::operator++(T);", NoSpace); 8328 8329 FormatStyle Space = getLLVMStyle(); 8330 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8331 8332 verifyFormat("int f ();", Space); 8333 verifyFormat("void f (int a, T b) {\n" 8334 " while (true)\n" 8335 " continue;\n" 8336 "}", 8337 Space); 8338 verifyFormat("if (true)\n" 8339 " f ();\n" 8340 "else if (true)\n" 8341 " f ();", 8342 Space); 8343 verifyFormat("do {\n" 8344 " do_something ();\n" 8345 "} while (something ());", 8346 Space); 8347 verifyFormat("switch (x) {\n" 8348 "default:\n" 8349 " break;\n" 8350 "}", 8351 Space); 8352 verifyFormat("A::A () : a (1) {}", Space); 8353 verifyFormat("void f () __attribute__ ((asdf));", Space); 8354 verifyFormat("*(&a + 1);\n" 8355 "&((&a)[1]);\n" 8356 "a[(b + c) * d];\n" 8357 "(((a + 1) * 2) + 3) * 4;", 8358 Space); 8359 verifyFormat("#define A(x) x", Space); 8360 verifyFormat("#define A (x) x", Space); 8361 verifyFormat("#if defined(x)\n" 8362 "#endif", 8363 Space); 8364 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8365 verifyFormat("size_t x = sizeof (x);", Space); 8366 verifyFormat("auto f (int x) -> decltype (x);", Space); 8367 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8368 verifyFormat("alignas (128) char a[128];", Space); 8369 verifyFormat("size_t x = alignof (MyType);", Space); 8370 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8371 verifyFormat("int f () throw (Deprecated);", Space); 8372 verifyFormat("typedef void (*cb) (int);", Space); 8373 verifyFormat("T A::operator() ();", Space); 8374 verifyFormat("X A::operator++ (T);", Space); 8375 } 8376 8377 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8378 FormatStyle Spaces = getLLVMStyle(); 8379 8380 Spaces.SpacesInParentheses = true; 8381 verifyFormat("call( x, y, z );", Spaces); 8382 verifyFormat("call();", Spaces); 8383 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8384 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8385 Spaces); 8386 verifyFormat("while ( (bool)1 )\n" 8387 " continue;", 8388 Spaces); 8389 verifyFormat("for ( ;; )\n" 8390 " continue;", 8391 Spaces); 8392 verifyFormat("if ( true )\n" 8393 " f();\n" 8394 "else if ( true )\n" 8395 " f();", 8396 Spaces); 8397 verifyFormat("do {\n" 8398 " do_something( (int)i );\n" 8399 "} while ( something() );", 8400 Spaces); 8401 verifyFormat("switch ( x ) {\n" 8402 "default:\n" 8403 " break;\n" 8404 "}", 8405 Spaces); 8406 8407 Spaces.SpacesInParentheses = false; 8408 Spaces.SpacesInCStyleCastParentheses = true; 8409 verifyFormat("Type *A = ( Type * )P;", Spaces); 8410 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8411 verifyFormat("x = ( int32 )y;", Spaces); 8412 verifyFormat("int a = ( int )(2.0f);", Spaces); 8413 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8414 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8415 verifyFormat("#define x (( int )-1)", Spaces); 8416 8417 // Run the first set of tests again with: 8418 Spaces.SpacesInParentheses = false; 8419 Spaces.SpaceInEmptyParentheses = true; 8420 Spaces.SpacesInCStyleCastParentheses = true; 8421 verifyFormat("call(x, y, z);", Spaces); 8422 verifyFormat("call( );", Spaces); 8423 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8424 verifyFormat("while (( bool )1)\n" 8425 " continue;", 8426 Spaces); 8427 verifyFormat("for (;;)\n" 8428 " continue;", 8429 Spaces); 8430 verifyFormat("if (true)\n" 8431 " f( );\n" 8432 "else if (true)\n" 8433 " f( );", 8434 Spaces); 8435 verifyFormat("do {\n" 8436 " do_something(( int )i);\n" 8437 "} while (something( ));", 8438 Spaces); 8439 verifyFormat("switch (x) {\n" 8440 "default:\n" 8441 " break;\n" 8442 "}", 8443 Spaces); 8444 8445 // Run the first set of tests again with: 8446 Spaces.SpaceAfterCStyleCast = 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 subset of tests again with: 8472 Spaces.SpacesInCStyleCastParentheses = false; 8473 Spaces.SpaceAfterCStyleCast = true; 8474 verifyFormat("while ((bool) 1)\n" 8475 " continue;", 8476 Spaces); 8477 verifyFormat("do {\n" 8478 " do_something((int) i);\n" 8479 "} while (something( ));", 8480 Spaces); 8481 } 8482 8483 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8484 verifyFormat("int a[5];"); 8485 verifyFormat("a[3] += 42;"); 8486 8487 FormatStyle Spaces = getLLVMStyle(); 8488 Spaces.SpacesInSquareBrackets = true; 8489 // Lambdas unchanged. 8490 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8491 verifyFormat("return [i, args...] {};", Spaces); 8492 8493 // Not lambdas. 8494 verifyFormat("int a[ 5 ];", Spaces); 8495 verifyFormat("a[ 3 ] += 42;", Spaces); 8496 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8497 verifyFormat("double &operator[](int i) { return 0; }\n" 8498 "int i;", 8499 Spaces); 8500 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8501 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8502 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8503 } 8504 8505 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8506 verifyFormat("int a = 5;"); 8507 verifyFormat("a += 42;"); 8508 verifyFormat("a or_eq 8;"); 8509 8510 FormatStyle Spaces = getLLVMStyle(); 8511 Spaces.SpaceBeforeAssignmentOperators = false; 8512 verifyFormat("int a= 5;", Spaces); 8513 verifyFormat("a+= 42;", Spaces); 8514 verifyFormat("a or_eq 8;", Spaces); 8515 } 8516 8517 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8518 FormatStyle Alignment = getLLVMStyle(); 8519 Alignment.AlignConsecutiveAssignments = false; 8520 verifyFormat("int a = 5;\n" 8521 "int oneTwoThree = 123;", 8522 Alignment); 8523 verifyFormat("int a = 5;\n" 8524 "int oneTwoThree = 123;", 8525 Alignment); 8526 8527 Alignment.AlignConsecutiveAssignments = true; 8528 verifyFormat("int a = 5;\n" 8529 "int oneTwoThree = 123;", 8530 Alignment); 8531 verifyFormat("int a = method();\n" 8532 "int oneTwoThree = 133;", 8533 Alignment); 8534 verifyFormat("a &= 5;\n" 8535 "bcd *= 5;\n" 8536 "ghtyf += 5;\n" 8537 "dvfvdb -= 5;\n" 8538 "a /= 5;\n" 8539 "vdsvsv %= 5;\n" 8540 "sfdbddfbdfbb ^= 5;\n" 8541 "dvsdsv |= 5;\n" 8542 "int dsvvdvsdvvv = 123;", 8543 Alignment); 8544 verifyFormat("int i = 1, j = 10;\n" 8545 "something = 2000;", 8546 Alignment); 8547 verifyFormat("something = 2000;\n" 8548 "int i = 1, j = 10;\n", 8549 Alignment); 8550 verifyFormat("something = 2000;\n" 8551 "another = 911;\n" 8552 "int i = 1, j = 10;\n" 8553 "oneMore = 1;\n" 8554 "i = 2;", 8555 Alignment); 8556 verifyFormat("int a = 5;\n" 8557 "int one = 1;\n" 8558 "method();\n" 8559 "int oneTwoThree = 123;\n" 8560 "int oneTwo = 12;", 8561 Alignment); 8562 verifyFormat("int oneTwoThree = 123;\n" 8563 "int oneTwo = 12;\n" 8564 "method();\n", 8565 Alignment); 8566 verifyFormat("int oneTwoThree = 123; // comment\n" 8567 "int oneTwo = 12; // comment", 8568 Alignment); 8569 EXPECT_EQ("int a = 5;\n" 8570 "\n" 8571 "int oneTwoThree = 123;", 8572 format("int a = 5;\n" 8573 "\n" 8574 "int oneTwoThree= 123;", 8575 Alignment)); 8576 EXPECT_EQ("int a = 5;\n" 8577 "int one = 1;\n" 8578 "\n" 8579 "int oneTwoThree = 123;", 8580 format("int a = 5;\n" 8581 "int one = 1;\n" 8582 "\n" 8583 "int oneTwoThree = 123;", 8584 Alignment)); 8585 EXPECT_EQ("int a = 5;\n" 8586 "int one = 1;\n" 8587 "\n" 8588 "int oneTwoThree = 123;\n" 8589 "int oneTwo = 12;", 8590 format("int a = 5;\n" 8591 "int one = 1;\n" 8592 "\n" 8593 "int oneTwoThree = 123;\n" 8594 "int oneTwo = 12;", 8595 Alignment)); 8596 Alignment.AlignEscapedNewlinesLeft = true; 8597 verifyFormat("#define A \\\n" 8598 " int aaaa = 12; \\\n" 8599 " int b = 23; \\\n" 8600 " int ccc = 234; \\\n" 8601 " int dddddddddd = 2345;", 8602 Alignment); 8603 Alignment.AlignEscapedNewlinesLeft = false; 8604 verifyFormat("#define A " 8605 " \\\n" 8606 " int aaaa = 12; " 8607 " \\\n" 8608 " int b = 23; " 8609 " \\\n" 8610 " int ccc = 234; " 8611 " \\\n" 8612 " int dddddddddd = 2345;", 8613 Alignment); 8614 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8615 "k = 4, int l = 5,\n" 8616 " int m = 6) {\n" 8617 " int j = 10;\n" 8618 " otherThing = 1;\n" 8619 "}", 8620 Alignment); 8621 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8622 " int i = 1;\n" 8623 " int j = 2;\n" 8624 " int big = 10000;\n" 8625 "}", 8626 Alignment); 8627 verifyFormat("class C {\n" 8628 "public:\n" 8629 " int i = 1;\n" 8630 " virtual void f() = 0;\n" 8631 "};", 8632 Alignment); 8633 verifyFormat("int i = 1;\n" 8634 "if (SomeType t = getSomething()) {\n" 8635 "}\n" 8636 "int j = 2;\n" 8637 "int big = 10000;", 8638 Alignment); 8639 verifyFormat("int j = 7;\n" 8640 "for (int k = 0; k < N; ++k) {\n" 8641 "}\n" 8642 "int j = 2;\n" 8643 "int big = 10000;\n" 8644 "}", 8645 Alignment); 8646 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8647 verifyFormat("int i = 1;\n" 8648 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8649 " = someLooooooooooooooooongFunction();\n" 8650 "int j = 2;", 8651 Alignment); 8652 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8653 verifyFormat("int i = 1;\n" 8654 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8655 " someLooooooooooooooooongFunction();\n" 8656 "int j = 2;", 8657 Alignment); 8658 8659 verifyFormat("auto lambda = []() {\n" 8660 " auto i = 0;\n" 8661 " return 0;\n" 8662 "};\n" 8663 "int i = 0;\n" 8664 "auto v = type{\n" 8665 " i = 1, //\n" 8666 " (i = 2), //\n" 8667 " i = 3 //\n" 8668 "};", 8669 Alignment); 8670 8671 // FIXME: Should align all three assignments 8672 verifyFormat( 8673 "int i = 1;\n" 8674 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8675 " loooooooooooooooooooooongParameterB);\n" 8676 "int j = 2;", 8677 Alignment); 8678 8679 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8680 " typename B = very_long_type_name_1,\n" 8681 " typename T_2 = very_long_type_name_2>\n" 8682 "auto foo() {}\n", 8683 Alignment); 8684 verifyFormat("int a, b = 1;\n" 8685 "int c = 2;\n" 8686 "int dd = 3;\n", 8687 Alignment); 8688 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8689 "float b[1][] = {{3.f}};\n", 8690 Alignment); 8691 } 8692 8693 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8694 FormatStyle Alignment = getLLVMStyle(); 8695 Alignment.AlignConsecutiveDeclarations = false; 8696 verifyFormat("float const a = 5;\n" 8697 "int oneTwoThree = 123;", 8698 Alignment); 8699 verifyFormat("int a = 5;\n" 8700 "float const oneTwoThree = 123;", 8701 Alignment); 8702 8703 Alignment.AlignConsecutiveDeclarations = true; 8704 verifyFormat("float const a = 5;\n" 8705 "int oneTwoThree = 123;", 8706 Alignment); 8707 verifyFormat("int a = method();\n" 8708 "float const oneTwoThree = 133;", 8709 Alignment); 8710 verifyFormat("int i = 1, j = 10;\n" 8711 "something = 2000;", 8712 Alignment); 8713 verifyFormat("something = 2000;\n" 8714 "int i = 1, j = 10;\n", 8715 Alignment); 8716 verifyFormat("float something = 2000;\n" 8717 "double another = 911;\n" 8718 "int i = 1, j = 10;\n" 8719 "const int *oneMore = 1;\n" 8720 "unsigned i = 2;", 8721 Alignment); 8722 verifyFormat("float a = 5;\n" 8723 "int one = 1;\n" 8724 "method();\n" 8725 "const double oneTwoThree = 123;\n" 8726 "const unsigned int oneTwo = 12;", 8727 Alignment); 8728 verifyFormat("int oneTwoThree{0}; // comment\n" 8729 "unsigned oneTwo; // comment", 8730 Alignment); 8731 EXPECT_EQ("float const a = 5;\n" 8732 "\n" 8733 "int oneTwoThree = 123;", 8734 format("float const a = 5;\n" 8735 "\n" 8736 "int oneTwoThree= 123;", 8737 Alignment)); 8738 EXPECT_EQ("float a = 5;\n" 8739 "int one = 1;\n" 8740 "\n" 8741 "unsigned oneTwoThree = 123;", 8742 format("float a = 5;\n" 8743 "int one = 1;\n" 8744 "\n" 8745 "unsigned oneTwoThree = 123;", 8746 Alignment)); 8747 EXPECT_EQ("float a = 5;\n" 8748 "int one = 1;\n" 8749 "\n" 8750 "unsigned oneTwoThree = 123;\n" 8751 "int oneTwo = 12;", 8752 format("float a = 5;\n" 8753 "int one = 1;\n" 8754 "\n" 8755 "unsigned oneTwoThree = 123;\n" 8756 "int oneTwo = 12;", 8757 Alignment)); 8758 Alignment.AlignConsecutiveAssignments = true; 8759 verifyFormat("float something = 2000;\n" 8760 "double another = 911;\n" 8761 "int i = 1, j = 10;\n" 8762 "const int *oneMore = 1;\n" 8763 "unsigned i = 2;", 8764 Alignment); 8765 verifyFormat("int oneTwoThree = {0}; // comment\n" 8766 "unsigned oneTwo = 0; // comment", 8767 Alignment); 8768 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8769 " int const i = 1;\n" 8770 " int * j = 2;\n" 8771 " int big = 10000;\n" 8772 "\n" 8773 " unsigned oneTwoThree = 123;\n" 8774 " int oneTwo = 12;\n" 8775 " method();\n" 8776 " float k = 2;\n" 8777 " int ll = 10000;\n" 8778 "}", 8779 format("void SomeFunction(int parameter= 0) {\n" 8780 " int const i= 1;\n" 8781 " int *j=2;\n" 8782 " int big = 10000;\n" 8783 "\n" 8784 "unsigned oneTwoThree =123;\n" 8785 "int oneTwo = 12;\n" 8786 " method();\n" 8787 "float k= 2;\n" 8788 "int ll=10000;\n" 8789 "}", 8790 Alignment)); 8791 Alignment.AlignConsecutiveAssignments = false; 8792 Alignment.AlignEscapedNewlinesLeft = true; 8793 verifyFormat("#define A \\\n" 8794 " int aaaa = 12; \\\n" 8795 " float b = 23; \\\n" 8796 " const int ccc = 234; \\\n" 8797 " unsigned dddddddddd = 2345;", 8798 Alignment); 8799 Alignment.AlignEscapedNewlinesLeft = false; 8800 Alignment.ColumnLimit = 30; 8801 verifyFormat("#define A \\\n" 8802 " int aaaa = 12; \\\n" 8803 " float b = 23; \\\n" 8804 " const int ccc = 234; \\\n" 8805 " int dddddddddd = 2345;", 8806 Alignment); 8807 Alignment.ColumnLimit = 80; 8808 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8809 "k = 4, int l = 5,\n" 8810 " int m = 6) {\n" 8811 " const int j = 10;\n" 8812 " otherThing = 1;\n" 8813 "}", 8814 Alignment); 8815 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8816 " int const i = 1;\n" 8817 " int * j = 2;\n" 8818 " int big = 10000;\n" 8819 "}", 8820 Alignment); 8821 verifyFormat("class C {\n" 8822 "public:\n" 8823 " int i = 1;\n" 8824 " virtual void f() = 0;\n" 8825 "};", 8826 Alignment); 8827 verifyFormat("float i = 1;\n" 8828 "if (SomeType t = getSomething()) {\n" 8829 "}\n" 8830 "const unsigned j = 2;\n" 8831 "int big = 10000;", 8832 Alignment); 8833 verifyFormat("float j = 7;\n" 8834 "for (int k = 0; k < N; ++k) {\n" 8835 "}\n" 8836 "unsigned j = 2;\n" 8837 "int big = 10000;\n" 8838 "}", 8839 Alignment); 8840 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8841 verifyFormat("float i = 1;\n" 8842 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8843 " = someLooooooooooooooooongFunction();\n" 8844 "int j = 2;", 8845 Alignment); 8846 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8847 verifyFormat("int i = 1;\n" 8848 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8849 " someLooooooooooooooooongFunction();\n" 8850 "int j = 2;", 8851 Alignment); 8852 8853 Alignment.AlignConsecutiveAssignments = true; 8854 verifyFormat("auto lambda = []() {\n" 8855 " auto ii = 0;\n" 8856 " float j = 0;\n" 8857 " return 0;\n" 8858 "};\n" 8859 "int i = 0;\n" 8860 "float i2 = 0;\n" 8861 "auto v = type{\n" 8862 " i = 1, //\n" 8863 " (i = 2), //\n" 8864 " i = 3 //\n" 8865 "};", 8866 Alignment); 8867 Alignment.AlignConsecutiveAssignments = false; 8868 8869 // FIXME: Should align all three declarations 8870 verifyFormat( 8871 "int i = 1;\n" 8872 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8873 " loooooooooooooooooooooongParameterB);\n" 8874 "int j = 2;", 8875 Alignment); 8876 8877 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8878 // We expect declarations and assignments to align, as long as it doesn't 8879 // exceed the column limit, starting a new alignemnt sequence whenever it 8880 // happens. 8881 Alignment.AlignConsecutiveAssignments = true; 8882 Alignment.ColumnLimit = 30; 8883 verifyFormat("float ii = 1;\n" 8884 "unsigned j = 2;\n" 8885 "int someVerylongVariable = 1;\n" 8886 "AnotherLongType ll = 123456;\n" 8887 "VeryVeryLongType k = 2;\n" 8888 "int myvar = 1;", 8889 Alignment); 8890 Alignment.ColumnLimit = 80; 8891 Alignment.AlignConsecutiveAssignments = false; 8892 8893 verifyFormat( 8894 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8895 " typename LongType, typename B>\n" 8896 "auto foo() {}\n", 8897 Alignment); 8898 verifyFormat("float a, b = 1;\n" 8899 "int c = 2;\n" 8900 "int dd = 3;\n", 8901 Alignment); 8902 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8903 "float b[1][] = {{3.f}};\n", 8904 Alignment); 8905 Alignment.AlignConsecutiveAssignments = true; 8906 verifyFormat("float a, b = 1;\n" 8907 "int c = 2;\n" 8908 "int dd = 3;\n", 8909 Alignment); 8910 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8911 "float b[1][] = {{3.f}};\n", 8912 Alignment); 8913 Alignment.AlignConsecutiveAssignments = false; 8914 8915 Alignment.ColumnLimit = 30; 8916 Alignment.BinPackParameters = false; 8917 verifyFormat("void foo(float a,\n" 8918 " float b,\n" 8919 " int c,\n" 8920 " uint32_t *d) {\n" 8921 " int * e = 0;\n" 8922 " float f = 0;\n" 8923 " double g = 0;\n" 8924 "}\n" 8925 "void bar(ino_t a,\n" 8926 " int b,\n" 8927 " uint32_t *c,\n" 8928 " bool d) {}\n", 8929 Alignment); 8930 Alignment.BinPackParameters = true; 8931 Alignment.ColumnLimit = 80; 8932 } 8933 8934 TEST_F(FormatTest, LinuxBraceBreaking) { 8935 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8936 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8937 verifyFormat("namespace a\n" 8938 "{\n" 8939 "class A\n" 8940 "{\n" 8941 " void f()\n" 8942 " {\n" 8943 " if (true) {\n" 8944 " a();\n" 8945 " b();\n" 8946 " } else {\n" 8947 " a();\n" 8948 " }\n" 8949 " }\n" 8950 " void g() { return; }\n" 8951 "};\n" 8952 "struct B {\n" 8953 " int x;\n" 8954 "};\n" 8955 "}\n", 8956 LinuxBraceStyle); 8957 verifyFormat("enum X {\n" 8958 " Y = 0,\n" 8959 "}\n", 8960 LinuxBraceStyle); 8961 verifyFormat("struct S {\n" 8962 " int Type;\n" 8963 " union {\n" 8964 " int x;\n" 8965 " double y;\n" 8966 " } Value;\n" 8967 " class C\n" 8968 " {\n" 8969 " MyFavoriteType Value;\n" 8970 " } Class;\n" 8971 "}\n", 8972 LinuxBraceStyle); 8973 } 8974 8975 TEST_F(FormatTest, MozillaBraceBreaking) { 8976 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8977 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8978 verifyFormat("namespace a {\n" 8979 "class A\n" 8980 "{\n" 8981 " void f()\n" 8982 " {\n" 8983 " if (true) {\n" 8984 " a();\n" 8985 " b();\n" 8986 " }\n" 8987 " }\n" 8988 " void g() { return; }\n" 8989 "};\n" 8990 "enum E\n" 8991 "{\n" 8992 " A,\n" 8993 " // foo\n" 8994 " B,\n" 8995 " C\n" 8996 "};\n" 8997 "struct B\n" 8998 "{\n" 8999 " int x;\n" 9000 "};\n" 9001 "}\n", 9002 MozillaBraceStyle); 9003 verifyFormat("struct S\n" 9004 "{\n" 9005 " int Type;\n" 9006 " union\n" 9007 " {\n" 9008 " int x;\n" 9009 " double y;\n" 9010 " } Value;\n" 9011 " class C\n" 9012 " {\n" 9013 " MyFavoriteType Value;\n" 9014 " } Class;\n" 9015 "}\n", 9016 MozillaBraceStyle); 9017 } 9018 9019 TEST_F(FormatTest, StroustrupBraceBreaking) { 9020 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9021 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9022 verifyFormat("namespace a {\n" 9023 "class A {\n" 9024 " void f()\n" 9025 " {\n" 9026 " if (true) {\n" 9027 " a();\n" 9028 " b();\n" 9029 " }\n" 9030 " }\n" 9031 " void g() { return; }\n" 9032 "};\n" 9033 "struct B {\n" 9034 " int x;\n" 9035 "};\n" 9036 "}\n", 9037 StroustrupBraceStyle); 9038 9039 verifyFormat("void foo()\n" 9040 "{\n" 9041 " if (a) {\n" 9042 " a();\n" 9043 " }\n" 9044 " else {\n" 9045 " b();\n" 9046 " }\n" 9047 "}\n", 9048 StroustrupBraceStyle); 9049 9050 verifyFormat("#ifdef _DEBUG\n" 9051 "int foo(int i = 0)\n" 9052 "#else\n" 9053 "int foo(int i = 5)\n" 9054 "#endif\n" 9055 "{\n" 9056 " return i;\n" 9057 "}", 9058 StroustrupBraceStyle); 9059 9060 verifyFormat("void foo() {}\n" 9061 "void bar()\n" 9062 "#ifdef _DEBUG\n" 9063 "{\n" 9064 " foo();\n" 9065 "}\n" 9066 "#else\n" 9067 "{\n" 9068 "}\n" 9069 "#endif", 9070 StroustrupBraceStyle); 9071 9072 verifyFormat("void foobar() { int i = 5; }\n" 9073 "#ifdef _DEBUG\n" 9074 "void bar() {}\n" 9075 "#else\n" 9076 "void bar() { foobar(); }\n" 9077 "#endif", 9078 StroustrupBraceStyle); 9079 } 9080 9081 TEST_F(FormatTest, AllmanBraceBreaking) { 9082 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9083 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9084 verifyFormat("namespace a\n" 9085 "{\n" 9086 "class A\n" 9087 "{\n" 9088 " void f()\n" 9089 " {\n" 9090 " if (true)\n" 9091 " {\n" 9092 " a();\n" 9093 " b();\n" 9094 " }\n" 9095 " }\n" 9096 " void g() { return; }\n" 9097 "};\n" 9098 "struct B\n" 9099 "{\n" 9100 " int x;\n" 9101 "};\n" 9102 "}", 9103 AllmanBraceStyle); 9104 9105 verifyFormat("void f()\n" 9106 "{\n" 9107 " if (true)\n" 9108 " {\n" 9109 " a();\n" 9110 " }\n" 9111 " else if (false)\n" 9112 " {\n" 9113 " b();\n" 9114 " }\n" 9115 " else\n" 9116 " {\n" 9117 " c();\n" 9118 " }\n" 9119 "}\n", 9120 AllmanBraceStyle); 9121 9122 verifyFormat("void f()\n" 9123 "{\n" 9124 " for (int i = 0; i < 10; ++i)\n" 9125 " {\n" 9126 " a();\n" 9127 " }\n" 9128 " while (false)\n" 9129 " {\n" 9130 " b();\n" 9131 " }\n" 9132 " do\n" 9133 " {\n" 9134 " c();\n" 9135 " } while (false)\n" 9136 "}\n", 9137 AllmanBraceStyle); 9138 9139 verifyFormat("void f(int a)\n" 9140 "{\n" 9141 " switch (a)\n" 9142 " {\n" 9143 " case 0:\n" 9144 " break;\n" 9145 " case 1:\n" 9146 " {\n" 9147 " break;\n" 9148 " }\n" 9149 " case 2:\n" 9150 " {\n" 9151 " }\n" 9152 " break;\n" 9153 " default:\n" 9154 " break;\n" 9155 " }\n" 9156 "}\n", 9157 AllmanBraceStyle); 9158 9159 verifyFormat("enum X\n" 9160 "{\n" 9161 " Y = 0,\n" 9162 "}\n", 9163 AllmanBraceStyle); 9164 verifyFormat("enum X\n" 9165 "{\n" 9166 " Y = 0\n" 9167 "}\n", 9168 AllmanBraceStyle); 9169 9170 verifyFormat("@interface BSApplicationController ()\n" 9171 "{\n" 9172 "@private\n" 9173 " id _extraIvar;\n" 9174 "}\n" 9175 "@end\n", 9176 AllmanBraceStyle); 9177 9178 verifyFormat("#ifdef _DEBUG\n" 9179 "int foo(int i = 0)\n" 9180 "#else\n" 9181 "int foo(int i = 5)\n" 9182 "#endif\n" 9183 "{\n" 9184 " return i;\n" 9185 "}", 9186 AllmanBraceStyle); 9187 9188 verifyFormat("void foo() {}\n" 9189 "void bar()\n" 9190 "#ifdef _DEBUG\n" 9191 "{\n" 9192 " foo();\n" 9193 "}\n" 9194 "#else\n" 9195 "{\n" 9196 "}\n" 9197 "#endif", 9198 AllmanBraceStyle); 9199 9200 verifyFormat("void foobar() { int i = 5; }\n" 9201 "#ifdef _DEBUG\n" 9202 "void bar() {}\n" 9203 "#else\n" 9204 "void bar() { foobar(); }\n" 9205 "#endif", 9206 AllmanBraceStyle); 9207 9208 // This shouldn't affect ObjC blocks.. 9209 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9210 " // ...\n" 9211 " int i;\n" 9212 "}];", 9213 AllmanBraceStyle); 9214 verifyFormat("void (^block)(void) = ^{\n" 9215 " // ...\n" 9216 " int i;\n" 9217 "};", 9218 AllmanBraceStyle); 9219 // .. or dict literals. 9220 verifyFormat("void f()\n" 9221 "{\n" 9222 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9223 "}", 9224 AllmanBraceStyle); 9225 verifyFormat("int f()\n" 9226 "{ // comment\n" 9227 " return 42;\n" 9228 "}", 9229 AllmanBraceStyle); 9230 9231 AllmanBraceStyle.ColumnLimit = 19; 9232 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9233 AllmanBraceStyle.ColumnLimit = 18; 9234 verifyFormat("void f()\n" 9235 "{\n" 9236 " int i;\n" 9237 "}", 9238 AllmanBraceStyle); 9239 AllmanBraceStyle.ColumnLimit = 80; 9240 9241 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9242 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9243 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9244 verifyFormat("void f(bool b)\n" 9245 "{\n" 9246 " if (b)\n" 9247 " {\n" 9248 " return;\n" 9249 " }\n" 9250 "}\n", 9251 BreakBeforeBraceShortIfs); 9252 verifyFormat("void f(bool b)\n" 9253 "{\n" 9254 " if (b) return;\n" 9255 "}\n", 9256 BreakBeforeBraceShortIfs); 9257 verifyFormat("void f(bool b)\n" 9258 "{\n" 9259 " while (b)\n" 9260 " {\n" 9261 " return;\n" 9262 " }\n" 9263 "}\n", 9264 BreakBeforeBraceShortIfs); 9265 } 9266 9267 TEST_F(FormatTest, GNUBraceBreaking) { 9268 FormatStyle GNUBraceStyle = getLLVMStyle(); 9269 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9270 verifyFormat("namespace a\n" 9271 "{\n" 9272 "class A\n" 9273 "{\n" 9274 " void f()\n" 9275 " {\n" 9276 " int a;\n" 9277 " {\n" 9278 " int b;\n" 9279 " }\n" 9280 " if (true)\n" 9281 " {\n" 9282 " a();\n" 9283 " b();\n" 9284 " }\n" 9285 " }\n" 9286 " void g() { return; }\n" 9287 "}\n" 9288 "}", 9289 GNUBraceStyle); 9290 9291 verifyFormat("void f()\n" 9292 "{\n" 9293 " if (true)\n" 9294 " {\n" 9295 " a();\n" 9296 " }\n" 9297 " else if (false)\n" 9298 " {\n" 9299 " b();\n" 9300 " }\n" 9301 " else\n" 9302 " {\n" 9303 " c();\n" 9304 " }\n" 9305 "}\n", 9306 GNUBraceStyle); 9307 9308 verifyFormat("void f()\n" 9309 "{\n" 9310 " for (int i = 0; i < 10; ++i)\n" 9311 " {\n" 9312 " a();\n" 9313 " }\n" 9314 " while (false)\n" 9315 " {\n" 9316 " b();\n" 9317 " }\n" 9318 " do\n" 9319 " {\n" 9320 " c();\n" 9321 " }\n" 9322 " while (false);\n" 9323 "}\n", 9324 GNUBraceStyle); 9325 9326 verifyFormat("void f(int a)\n" 9327 "{\n" 9328 " switch (a)\n" 9329 " {\n" 9330 " case 0:\n" 9331 " break;\n" 9332 " case 1:\n" 9333 " {\n" 9334 " break;\n" 9335 " }\n" 9336 " case 2:\n" 9337 " {\n" 9338 " }\n" 9339 " break;\n" 9340 " default:\n" 9341 " break;\n" 9342 " }\n" 9343 "}\n", 9344 GNUBraceStyle); 9345 9346 verifyFormat("enum X\n" 9347 "{\n" 9348 " Y = 0,\n" 9349 "}\n", 9350 GNUBraceStyle); 9351 9352 verifyFormat("@interface BSApplicationController ()\n" 9353 "{\n" 9354 "@private\n" 9355 " id _extraIvar;\n" 9356 "}\n" 9357 "@end\n", 9358 GNUBraceStyle); 9359 9360 verifyFormat("#ifdef _DEBUG\n" 9361 "int foo(int i = 0)\n" 9362 "#else\n" 9363 "int foo(int i = 5)\n" 9364 "#endif\n" 9365 "{\n" 9366 " return i;\n" 9367 "}", 9368 GNUBraceStyle); 9369 9370 verifyFormat("void foo() {}\n" 9371 "void bar()\n" 9372 "#ifdef _DEBUG\n" 9373 "{\n" 9374 " foo();\n" 9375 "}\n" 9376 "#else\n" 9377 "{\n" 9378 "}\n" 9379 "#endif", 9380 GNUBraceStyle); 9381 9382 verifyFormat("void foobar() { int i = 5; }\n" 9383 "#ifdef _DEBUG\n" 9384 "void bar() {}\n" 9385 "#else\n" 9386 "void bar() { foobar(); }\n" 9387 "#endif", 9388 GNUBraceStyle); 9389 } 9390 9391 TEST_F(FormatTest, WebKitBraceBreaking) { 9392 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9393 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9394 verifyFormat("namespace a {\n" 9395 "class A {\n" 9396 " void f()\n" 9397 " {\n" 9398 " if (true) {\n" 9399 " a();\n" 9400 " b();\n" 9401 " }\n" 9402 " }\n" 9403 " void g() { return; }\n" 9404 "};\n" 9405 "enum E {\n" 9406 " A,\n" 9407 " // foo\n" 9408 " B,\n" 9409 " C\n" 9410 "};\n" 9411 "struct B {\n" 9412 " int x;\n" 9413 "};\n" 9414 "}\n", 9415 WebKitBraceStyle); 9416 verifyFormat("struct S {\n" 9417 " int Type;\n" 9418 " union {\n" 9419 " int x;\n" 9420 " double y;\n" 9421 " } Value;\n" 9422 " class C {\n" 9423 " MyFavoriteType Value;\n" 9424 " } Class;\n" 9425 "};\n", 9426 WebKitBraceStyle); 9427 } 9428 9429 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9430 verifyFormat("void f() {\n" 9431 " try {\n" 9432 " } catch (const Exception &e) {\n" 9433 " }\n" 9434 "}\n", 9435 getLLVMStyle()); 9436 } 9437 9438 TEST_F(FormatTest, UnderstandsPragmas) { 9439 verifyFormat("#pragma omp reduction(| : var)"); 9440 verifyFormat("#pragma omp reduction(+ : var)"); 9441 9442 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9443 "(including parentheses).", 9444 format("#pragma mark Any non-hyphenated or hyphenated string " 9445 "(including parentheses).")); 9446 } 9447 9448 TEST_F(FormatTest, UnderstandPragmaOption) { 9449 verifyFormat("#pragma option -C -A"); 9450 9451 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9452 } 9453 9454 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9455 for (size_t i = 1; i < Styles.size(); ++i) \ 9456 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9457 << " differs from Style #0" 9458 9459 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9460 SmallVector<FormatStyle, 3> Styles; 9461 Styles.resize(3); 9462 9463 Styles[0] = getLLVMStyle(); 9464 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9465 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9466 EXPECT_ALL_STYLES_EQUAL(Styles); 9467 9468 Styles[0] = getGoogleStyle(); 9469 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9470 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9471 EXPECT_ALL_STYLES_EQUAL(Styles); 9472 9473 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9474 EXPECT_TRUE( 9475 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9476 EXPECT_TRUE( 9477 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9478 EXPECT_ALL_STYLES_EQUAL(Styles); 9479 9480 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9481 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9482 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9483 EXPECT_ALL_STYLES_EQUAL(Styles); 9484 9485 Styles[0] = getMozillaStyle(); 9486 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9487 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9488 EXPECT_ALL_STYLES_EQUAL(Styles); 9489 9490 Styles[0] = getWebKitStyle(); 9491 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9492 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9493 EXPECT_ALL_STYLES_EQUAL(Styles); 9494 9495 Styles[0] = getGNUStyle(); 9496 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9497 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9498 EXPECT_ALL_STYLES_EQUAL(Styles); 9499 9500 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9501 } 9502 9503 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9504 SmallVector<FormatStyle, 8> Styles; 9505 Styles.resize(2); 9506 9507 Styles[0] = getGoogleStyle(); 9508 Styles[1] = getLLVMStyle(); 9509 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9510 EXPECT_ALL_STYLES_EQUAL(Styles); 9511 9512 Styles.resize(5); 9513 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9514 Styles[1] = getLLVMStyle(); 9515 Styles[1].Language = FormatStyle::LK_JavaScript; 9516 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9517 9518 Styles[2] = getLLVMStyle(); 9519 Styles[2].Language = FormatStyle::LK_JavaScript; 9520 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9521 "BasedOnStyle: Google", 9522 &Styles[2]) 9523 .value()); 9524 9525 Styles[3] = getLLVMStyle(); 9526 Styles[3].Language = FormatStyle::LK_JavaScript; 9527 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9528 "Language: JavaScript", 9529 &Styles[3]) 9530 .value()); 9531 9532 Styles[4] = getLLVMStyle(); 9533 Styles[4].Language = FormatStyle::LK_JavaScript; 9534 EXPECT_EQ(0, parseConfiguration("---\n" 9535 "BasedOnStyle: LLVM\n" 9536 "IndentWidth: 123\n" 9537 "---\n" 9538 "BasedOnStyle: Google\n" 9539 "Language: JavaScript", 9540 &Styles[4]) 9541 .value()); 9542 EXPECT_ALL_STYLES_EQUAL(Styles); 9543 } 9544 9545 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9546 Style.FIELD = false; \ 9547 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9548 EXPECT_TRUE(Style.FIELD); \ 9549 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9550 EXPECT_FALSE(Style.FIELD); 9551 9552 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9553 9554 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9555 Style.STRUCT.FIELD = false; \ 9556 EXPECT_EQ(0, \ 9557 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9558 .value()); \ 9559 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9560 EXPECT_EQ(0, \ 9561 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9562 .value()); \ 9563 EXPECT_FALSE(Style.STRUCT.FIELD); 9564 9565 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9566 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9567 9568 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9569 EXPECT_NE(VALUE, Style.FIELD); \ 9570 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9571 EXPECT_EQ(VALUE, Style.FIELD) 9572 9573 TEST_F(FormatTest, ParsesConfigurationBools) { 9574 FormatStyle Style = {}; 9575 Style.Language = FormatStyle::LK_Cpp; 9576 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9577 CHECK_PARSE_BOOL(AlignOperands); 9578 CHECK_PARSE_BOOL(AlignTrailingComments); 9579 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9580 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9581 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9582 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9583 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9584 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9585 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9586 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9587 CHECK_PARSE_BOOL(BinPackArguments); 9588 CHECK_PARSE_BOOL(BinPackParameters); 9589 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9590 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9591 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9592 CHECK_PARSE_BOOL(BreakStringLiterals); 9593 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9594 CHECK_PARSE_BOOL(DerivePointerAlignment); 9595 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9596 CHECK_PARSE_BOOL(DisableFormat); 9597 CHECK_PARSE_BOOL(IndentCaseLabels); 9598 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9599 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9600 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9601 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9602 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9603 CHECK_PARSE_BOOL(ReflowComments); 9604 CHECK_PARSE_BOOL(SortIncludes); 9605 CHECK_PARSE_BOOL(SpacesInParentheses); 9606 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9607 CHECK_PARSE_BOOL(SpacesInAngles); 9608 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9609 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9610 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9611 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9612 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9613 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9614 9615 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9616 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9617 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9618 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9619 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9620 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9621 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9622 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9623 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9624 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9625 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9626 } 9627 9628 #undef CHECK_PARSE_BOOL 9629 9630 TEST_F(FormatTest, ParsesConfiguration) { 9631 FormatStyle Style = {}; 9632 Style.Language = FormatStyle::LK_Cpp; 9633 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9634 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9635 ConstructorInitializerIndentWidth, 1234u); 9636 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9637 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9638 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9639 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9640 PenaltyBreakBeforeFirstCallParameter, 1234u); 9641 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9642 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9643 PenaltyReturnTypeOnItsOwnLine, 1234u); 9644 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9645 SpacesBeforeTrailingComments, 1234u); 9646 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9647 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9648 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9649 9650 Style.PointerAlignment = FormatStyle::PAS_Middle; 9651 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9652 FormatStyle::PAS_Left); 9653 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9654 FormatStyle::PAS_Right); 9655 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9656 FormatStyle::PAS_Middle); 9657 // For backward compatibility: 9658 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9659 FormatStyle::PAS_Left); 9660 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9661 FormatStyle::PAS_Right); 9662 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9663 FormatStyle::PAS_Middle); 9664 9665 Style.Standard = FormatStyle::LS_Auto; 9666 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9667 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9668 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9669 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9670 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9671 9672 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9673 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9674 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9675 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9676 FormatStyle::BOS_None); 9677 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9678 FormatStyle::BOS_All); 9679 // For backward compatibility: 9680 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9681 FormatStyle::BOS_None); 9682 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9683 FormatStyle::BOS_All); 9684 9685 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9686 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9687 FormatStyle::BAS_Align); 9688 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9689 FormatStyle::BAS_DontAlign); 9690 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9691 FormatStyle::BAS_AlwaysBreak); 9692 // For backward compatibility: 9693 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9694 FormatStyle::BAS_DontAlign); 9695 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9696 FormatStyle::BAS_Align); 9697 9698 Style.UseTab = FormatStyle::UT_ForIndentation; 9699 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9700 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9701 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9702 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9703 FormatStyle::UT_ForContinuationAndIndentation); 9704 // For backward compatibility: 9705 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9706 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9707 9708 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9709 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9710 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9711 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9712 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9713 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9714 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9715 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9716 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9717 // For backward compatibility: 9718 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9719 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9720 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9721 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9722 9723 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9724 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9725 FormatStyle::SBPO_Never); 9726 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9727 FormatStyle::SBPO_Always); 9728 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9729 FormatStyle::SBPO_ControlStatements); 9730 // For backward compatibility: 9731 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9732 FormatStyle::SBPO_Never); 9733 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9734 FormatStyle::SBPO_ControlStatements); 9735 9736 Style.ColumnLimit = 123; 9737 FormatStyle BaseStyle = getLLVMStyle(); 9738 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9739 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9740 9741 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9742 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9743 FormatStyle::BS_Attach); 9744 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9745 FormatStyle::BS_Linux); 9746 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9747 FormatStyle::BS_Mozilla); 9748 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9749 FormatStyle::BS_Stroustrup); 9750 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9751 FormatStyle::BS_Allman); 9752 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9753 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9754 FormatStyle::BS_WebKit); 9755 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9756 FormatStyle::BS_Custom); 9757 9758 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9759 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9760 FormatStyle::RTBS_None); 9761 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9762 FormatStyle::RTBS_All); 9763 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9764 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9765 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9766 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9767 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9768 AlwaysBreakAfterReturnType, 9769 FormatStyle::RTBS_TopLevelDefinitions); 9770 9771 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9772 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9773 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9774 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9775 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9776 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9777 AlwaysBreakAfterDefinitionReturnType, 9778 FormatStyle::DRTBS_TopLevel); 9779 9780 Style.NamespaceIndentation = FormatStyle::NI_All; 9781 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9782 FormatStyle::NI_None); 9783 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9784 FormatStyle::NI_Inner); 9785 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9786 FormatStyle::NI_All); 9787 9788 // FIXME: This is required because parsing a configuration simply overwrites 9789 // the first N elements of the list instead of resetting it. 9790 Style.ForEachMacros.clear(); 9791 std::vector<std::string> BoostForeach; 9792 BoostForeach.push_back("BOOST_FOREACH"); 9793 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9794 std::vector<std::string> BoostAndQForeach; 9795 BoostAndQForeach.push_back("BOOST_FOREACH"); 9796 BoostAndQForeach.push_back("Q_FOREACH"); 9797 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9798 BoostAndQForeach); 9799 9800 Style.IncludeCategories.clear(); 9801 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9802 {".*", 1}}; 9803 CHECK_PARSE("IncludeCategories:\n" 9804 " - Regex: abc/.*\n" 9805 " Priority: 2\n" 9806 " - Regex: .*\n" 9807 " Priority: 1", 9808 IncludeCategories, ExpectedCategories); 9809 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9810 } 9811 9812 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9813 FormatStyle Style = {}; 9814 Style.Language = FormatStyle::LK_Cpp; 9815 CHECK_PARSE("Language: Cpp\n" 9816 "IndentWidth: 12", 9817 IndentWidth, 12u); 9818 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9819 "IndentWidth: 34", 9820 &Style), 9821 ParseError::Unsuitable); 9822 EXPECT_EQ(12u, Style.IndentWidth); 9823 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9824 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9825 9826 Style.Language = FormatStyle::LK_JavaScript; 9827 CHECK_PARSE("Language: JavaScript\n" 9828 "IndentWidth: 12", 9829 IndentWidth, 12u); 9830 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9831 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9832 "IndentWidth: 34", 9833 &Style), 9834 ParseError::Unsuitable); 9835 EXPECT_EQ(23u, Style.IndentWidth); 9836 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9837 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9838 9839 CHECK_PARSE("BasedOnStyle: LLVM\n" 9840 "IndentWidth: 67", 9841 IndentWidth, 67u); 9842 9843 CHECK_PARSE("---\n" 9844 "Language: JavaScript\n" 9845 "IndentWidth: 12\n" 9846 "---\n" 9847 "Language: Cpp\n" 9848 "IndentWidth: 34\n" 9849 "...\n", 9850 IndentWidth, 12u); 9851 9852 Style.Language = FormatStyle::LK_Cpp; 9853 CHECK_PARSE("---\n" 9854 "Language: JavaScript\n" 9855 "IndentWidth: 12\n" 9856 "---\n" 9857 "Language: Cpp\n" 9858 "IndentWidth: 34\n" 9859 "...\n", 9860 IndentWidth, 34u); 9861 CHECK_PARSE("---\n" 9862 "IndentWidth: 78\n" 9863 "---\n" 9864 "Language: JavaScript\n" 9865 "IndentWidth: 56\n" 9866 "...\n", 9867 IndentWidth, 78u); 9868 9869 Style.ColumnLimit = 123; 9870 Style.IndentWidth = 234; 9871 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9872 Style.TabWidth = 345; 9873 EXPECT_FALSE(parseConfiguration("---\n" 9874 "IndentWidth: 456\n" 9875 "BreakBeforeBraces: Allman\n" 9876 "---\n" 9877 "Language: JavaScript\n" 9878 "IndentWidth: 111\n" 9879 "TabWidth: 111\n" 9880 "---\n" 9881 "Language: Cpp\n" 9882 "BreakBeforeBraces: Stroustrup\n" 9883 "TabWidth: 789\n" 9884 "...\n", 9885 &Style)); 9886 EXPECT_EQ(123u, Style.ColumnLimit); 9887 EXPECT_EQ(456u, Style.IndentWidth); 9888 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9889 EXPECT_EQ(789u, Style.TabWidth); 9890 9891 EXPECT_EQ(parseConfiguration("---\n" 9892 "Language: JavaScript\n" 9893 "IndentWidth: 56\n" 9894 "---\n" 9895 "IndentWidth: 78\n" 9896 "...\n", 9897 &Style), 9898 ParseError::Error); 9899 EXPECT_EQ(parseConfiguration("---\n" 9900 "Language: JavaScript\n" 9901 "IndentWidth: 56\n" 9902 "---\n" 9903 "Language: JavaScript\n" 9904 "IndentWidth: 78\n" 9905 "...\n", 9906 &Style), 9907 ParseError::Error); 9908 9909 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9910 } 9911 9912 #undef CHECK_PARSE 9913 9914 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9915 FormatStyle Style = {}; 9916 Style.Language = FormatStyle::LK_JavaScript; 9917 Style.BreakBeforeTernaryOperators = true; 9918 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9919 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9920 9921 Style.BreakBeforeTernaryOperators = true; 9922 EXPECT_EQ(0, parseConfiguration("---\n" 9923 "BasedOnStyle: Google\n" 9924 "---\n" 9925 "Language: JavaScript\n" 9926 "IndentWidth: 76\n" 9927 "...\n", 9928 &Style) 9929 .value()); 9930 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9931 EXPECT_EQ(76u, Style.IndentWidth); 9932 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9933 } 9934 9935 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9936 FormatStyle Style = getLLVMStyle(); 9937 std::string YAML = configurationAsText(Style); 9938 FormatStyle ParsedStyle = {}; 9939 ParsedStyle.Language = FormatStyle::LK_Cpp; 9940 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9941 EXPECT_EQ(Style, ParsedStyle); 9942 } 9943 9944 TEST_F(FormatTest, WorksFor8bitEncodings) { 9945 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9946 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9947 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9948 "\"\xef\xee\xf0\xf3...\"", 9949 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9950 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9951 "\xef\xee\xf0\xf3...\"", 9952 getLLVMStyleWithColumns(12))); 9953 } 9954 9955 TEST_F(FormatTest, HandlesUTF8BOM) { 9956 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9957 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9958 format("\xef\xbb\xbf#include <iostream>")); 9959 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9960 format("\xef\xbb\xbf\n#include <iostream>")); 9961 } 9962 9963 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9964 #if !defined(_MSC_VER) 9965 9966 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9967 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9968 getLLVMStyleWithColumns(35)); 9969 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9970 getLLVMStyleWithColumns(31)); 9971 verifyFormat("// Однажды в студёную зимнюю пору...", 9972 getLLVMStyleWithColumns(36)); 9973 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9974 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9975 getLLVMStyleWithColumns(39)); 9976 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9977 getLLVMStyleWithColumns(35)); 9978 } 9979 9980 TEST_F(FormatTest, SplitsUTF8Strings) { 9981 // Non-printable characters' width is currently considered to be the length in 9982 // bytes in UTF8. The characters can be displayed in very different manner 9983 // (zero-width, single width with a substitution glyph, expanded to their code 9984 // (e.g. "<8d>"), so there's no single correct way to handle them. 9985 EXPECT_EQ("\"aaaaÄ\"\n" 9986 "\"\xc2\x8d\";", 9987 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9988 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9989 "\"\xc2\x8d\";", 9990 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9991 EXPECT_EQ("\"Однажды, в \"\n" 9992 "\"студёную \"\n" 9993 "\"зимнюю \"\n" 9994 "\"пору,\"", 9995 format("\"Однажды, в студёную зимнюю пору,\"", 9996 getLLVMStyleWithColumns(13))); 9997 EXPECT_EQ( 9998 "\"一 二 三 \"\n" 9999 "\"四 五六 \"\n" 10000 "\"七 八 九 \"\n" 10001 "\"十\"", 10002 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10003 EXPECT_EQ("\"一\t二 \"\n" 10004 "\"\t三 \"\n" 10005 "\"四 五\t六 \"\n" 10006 "\"\t七 \"\n" 10007 "\"八九十\tqq\"", 10008 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10009 getLLVMStyleWithColumns(11))); 10010 10011 // UTF8 character in an escape sequence. 10012 EXPECT_EQ("\"aaaaaa\"\n" 10013 "\"\\\xC2\x8D\"", 10014 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10015 } 10016 10017 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10018 EXPECT_EQ("const char *sssss =\n" 10019 " \"一二三四五六七八\\\n" 10020 " 九 十\";", 10021 format("const char *sssss = \"一二三四五六七八\\\n" 10022 " 九 十\";", 10023 getLLVMStyleWithColumns(30))); 10024 } 10025 10026 TEST_F(FormatTest, SplitsUTF8LineComments) { 10027 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10028 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10029 EXPECT_EQ("// Я из лесу\n" 10030 "// вышел; был\n" 10031 "// сильный\n" 10032 "// мороз.", 10033 format("// Я из лесу вышел; был сильный мороз.", 10034 getLLVMStyleWithColumns(13))); 10035 EXPECT_EQ("// 一二三\n" 10036 "// 四五六七\n" 10037 "// 八 九\n" 10038 "// 十", 10039 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10040 } 10041 10042 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10043 EXPECT_EQ("/* Гляжу,\n" 10044 " * поднимается\n" 10045 " * медленно в\n" 10046 " * гору\n" 10047 " * Лошадка,\n" 10048 " * везущая\n" 10049 " * хворосту\n" 10050 " * воз. */", 10051 format("/* Гляжу, поднимается медленно в гору\n" 10052 " * Лошадка, везущая хворосту воз. */", 10053 getLLVMStyleWithColumns(13))); 10054 EXPECT_EQ( 10055 "/* 一二三\n" 10056 " * 四五六七\n" 10057 " * 八 九\n" 10058 " * 十 */", 10059 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10060 EXPECT_EQ("/* \n" 10061 " * \n" 10062 " * - */", 10063 format("/* - */", getLLVMStyleWithColumns(12))); 10064 } 10065 10066 #endif // _MSC_VER 10067 10068 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10069 FormatStyle Style = getLLVMStyle(); 10070 10071 Style.ConstructorInitializerIndentWidth = 4; 10072 verifyFormat( 10073 "SomeClass::Constructor()\n" 10074 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10075 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10076 Style); 10077 10078 Style.ConstructorInitializerIndentWidth = 2; 10079 verifyFormat( 10080 "SomeClass::Constructor()\n" 10081 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10082 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10083 Style); 10084 10085 Style.ConstructorInitializerIndentWidth = 0; 10086 verifyFormat( 10087 "SomeClass::Constructor()\n" 10088 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10089 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10090 Style); 10091 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10092 verifyFormat( 10093 "SomeLongTemplateVariableName<\n" 10094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10095 Style); 10096 verifyFormat( 10097 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10099 Style); 10100 } 10101 10102 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10103 FormatStyle Style = getLLVMStyle(); 10104 Style.BreakConstructorInitializersBeforeComma = true; 10105 Style.ConstructorInitializerIndentWidth = 4; 10106 verifyFormat("SomeClass::Constructor()\n" 10107 " : a(a)\n" 10108 " , b(b)\n" 10109 " , c(c) {}", 10110 Style); 10111 verifyFormat("SomeClass::Constructor()\n" 10112 " : a(a) {}", 10113 Style); 10114 10115 Style.ColumnLimit = 0; 10116 verifyFormat("SomeClass::Constructor()\n" 10117 " : a(a) {}", 10118 Style); 10119 verifyFormat("SomeClass::Constructor() noexcept\n" 10120 " : a(a) {}", 10121 Style); 10122 verifyFormat("SomeClass::Constructor()\n" 10123 " : a(a)\n" 10124 " , b(b)\n" 10125 " , c(c) {}", 10126 Style); 10127 verifyFormat("SomeClass::Constructor()\n" 10128 " : a(a) {\n" 10129 " foo();\n" 10130 " bar();\n" 10131 "}", 10132 Style); 10133 10134 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10135 verifyFormat("SomeClass::Constructor()\n" 10136 " : a(a)\n" 10137 " , b(b)\n" 10138 " , c(c) {\n}", 10139 Style); 10140 verifyFormat("SomeClass::Constructor()\n" 10141 " : a(a) {\n}", 10142 Style); 10143 10144 Style.ColumnLimit = 80; 10145 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10146 Style.ConstructorInitializerIndentWidth = 2; 10147 verifyFormat("SomeClass::Constructor()\n" 10148 " : a(a)\n" 10149 " , b(b)\n" 10150 " , c(c) {}", 10151 Style); 10152 10153 Style.ConstructorInitializerIndentWidth = 0; 10154 verifyFormat("SomeClass::Constructor()\n" 10155 ": a(a)\n" 10156 ", b(b)\n" 10157 ", c(c) {}", 10158 Style); 10159 10160 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10161 Style.ConstructorInitializerIndentWidth = 4; 10162 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10163 verifyFormat( 10164 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10165 Style); 10166 verifyFormat( 10167 "SomeClass::Constructor()\n" 10168 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10169 Style); 10170 Style.ConstructorInitializerIndentWidth = 4; 10171 Style.ColumnLimit = 60; 10172 verifyFormat("SomeClass::Constructor()\n" 10173 " : aaaaaaaa(aaaaaaaa)\n" 10174 " , aaaaaaaa(aaaaaaaa)\n" 10175 " , aaaaaaaa(aaaaaaaa) {}", 10176 Style); 10177 } 10178 10179 TEST_F(FormatTest, Destructors) { 10180 verifyFormat("void F(int &i) { i.~int(); }"); 10181 verifyFormat("void F(int &i) { i->~int(); }"); 10182 } 10183 10184 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10185 FormatStyle Style = getWebKitStyle(); 10186 10187 // Don't indent in outer namespaces. 10188 verifyFormat("namespace outer {\n" 10189 "int i;\n" 10190 "namespace inner {\n" 10191 " int i;\n" 10192 "} // namespace inner\n" 10193 "} // namespace outer\n" 10194 "namespace other_outer {\n" 10195 "int i;\n" 10196 "}", 10197 Style); 10198 10199 // Don't indent case labels. 10200 verifyFormat("switch (variable) {\n" 10201 "case 1:\n" 10202 "case 2:\n" 10203 " doSomething();\n" 10204 " break;\n" 10205 "default:\n" 10206 " ++variable;\n" 10207 "}", 10208 Style); 10209 10210 // Wrap before binary operators. 10211 EXPECT_EQ("void f()\n" 10212 "{\n" 10213 " if (aaaaaaaaaaaaaaaa\n" 10214 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10215 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10216 " return;\n" 10217 "}", 10218 format("void f() {\n" 10219 "if (aaaaaaaaaaaaaaaa\n" 10220 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10221 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10222 "return;\n" 10223 "}", 10224 Style)); 10225 10226 // Allow functions on a single line. 10227 verifyFormat("void f() { return; }", Style); 10228 10229 // Constructor initializers are formatted one per line with the "," on the 10230 // new line. 10231 verifyFormat("Constructor()\n" 10232 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10233 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10234 " aaaaaaaaaaaaaa)\n" 10235 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10236 "{\n" 10237 "}", 10238 Style); 10239 verifyFormat("SomeClass::Constructor()\n" 10240 " : a(a)\n" 10241 "{\n" 10242 "}", 10243 Style); 10244 EXPECT_EQ("SomeClass::Constructor()\n" 10245 " : a(a)\n" 10246 "{\n" 10247 "}", 10248 format("SomeClass::Constructor():a(a){}", Style)); 10249 verifyFormat("SomeClass::Constructor()\n" 10250 " : a(a)\n" 10251 " , b(b)\n" 10252 " , c(c)\n" 10253 "{\n" 10254 "}", 10255 Style); 10256 verifyFormat("SomeClass::Constructor()\n" 10257 " : a(a)\n" 10258 "{\n" 10259 " foo();\n" 10260 " bar();\n" 10261 "}", 10262 Style); 10263 10264 // Access specifiers should be aligned left. 10265 verifyFormat("class C {\n" 10266 "public:\n" 10267 " int i;\n" 10268 "};", 10269 Style); 10270 10271 // Do not align comments. 10272 verifyFormat("int a; // Do not\n" 10273 "double b; // align comments.", 10274 Style); 10275 10276 // Do not align operands. 10277 EXPECT_EQ("ASSERT(aaaa\n" 10278 " || bbbb);", 10279 format("ASSERT ( aaaa\n||bbbb);", Style)); 10280 10281 // Accept input's line breaks. 10282 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10283 " || bbbbbbbbbbbbbbb) {\n" 10284 " i++;\n" 10285 "}", 10286 format("if (aaaaaaaaaaaaaaa\n" 10287 "|| bbbbbbbbbbbbbbb) { i++; }", 10288 Style)); 10289 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10290 " i++;\n" 10291 "}", 10292 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10293 10294 // Don't automatically break all macro definitions (llvm.org/PR17842). 10295 verifyFormat("#define aNumber 10", Style); 10296 // However, generally keep the line breaks that the user authored. 10297 EXPECT_EQ("#define aNumber \\\n" 10298 " 10", 10299 format("#define aNumber \\\n" 10300 " 10", 10301 Style)); 10302 10303 // Keep empty and one-element array literals on a single line. 10304 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10305 " copyItems:YES];", 10306 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10307 "copyItems:YES];", 10308 Style)); 10309 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10310 " copyItems:YES];", 10311 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10312 " copyItems:YES];", 10313 Style)); 10314 // FIXME: This does not seem right, there should be more indentation before 10315 // the array literal's entries. Nested blocks have the same problem. 10316 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10317 " @\"a\",\n" 10318 " @\"a\"\n" 10319 "]\n" 10320 " copyItems:YES];", 10321 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10322 " @\"a\",\n" 10323 " @\"a\"\n" 10324 " ]\n" 10325 " copyItems:YES];", 10326 Style)); 10327 EXPECT_EQ( 10328 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10329 " copyItems:YES];", 10330 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10331 " copyItems:YES];", 10332 Style)); 10333 10334 verifyFormat("[self.a b:c c:d];", Style); 10335 EXPECT_EQ("[self.a b:c\n" 10336 " c:d];", 10337 format("[self.a b:c\n" 10338 "c:d];", 10339 Style)); 10340 } 10341 10342 TEST_F(FormatTest, FormatsLambdas) { 10343 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10344 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10345 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10346 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10347 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10348 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10349 verifyFormat("int x = f(*+[] {});"); 10350 verifyFormat("void f() {\n" 10351 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10352 "}\n"); 10353 verifyFormat("void f() {\n" 10354 " other(x.begin(), //\n" 10355 " x.end(), //\n" 10356 " [&](int, int) { return 1; });\n" 10357 "}\n"); 10358 verifyFormat("SomeFunction([]() { // A cool function...\n" 10359 " return 43;\n" 10360 "});"); 10361 EXPECT_EQ("SomeFunction([]() {\n" 10362 "#define A a\n" 10363 " return 43;\n" 10364 "});", 10365 format("SomeFunction([](){\n" 10366 "#define A a\n" 10367 "return 43;\n" 10368 "});")); 10369 verifyFormat("void f() {\n" 10370 " SomeFunction([](decltype(x), A *a) {});\n" 10371 "}"); 10372 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10373 " [](const aaaaaaaaaa &a) { return a; });"); 10374 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10375 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10376 "});"); 10377 verifyFormat("Constructor()\n" 10378 " : Field([] { // comment\n" 10379 " int i;\n" 10380 " }) {}"); 10381 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10382 " return some_parameter.size();\n" 10383 "};"); 10384 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10385 " [](const string &s) { return s; };"); 10386 verifyFormat("int i = aaaaaa ? 1 //\n" 10387 " : [] {\n" 10388 " return 2; //\n" 10389 " }();"); 10390 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10391 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10392 " return x == 2; // force break\n" 10393 " });"); 10394 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10395 " int iiiiiiiiiiii) {\n" 10396 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10397 "});", 10398 getLLVMStyleWithColumns(60)); 10399 verifyFormat("SomeFunction({[&] {\n" 10400 " // comment\n" 10401 " },\n" 10402 " [&] {\n" 10403 " // comment\n" 10404 " }});"); 10405 verifyFormat("SomeFunction({[&] {\n" 10406 " // comment\n" 10407 "}});"); 10408 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10409 " [&]() { return true; },\n" 10410 " aaaaa aaaaaaaaa);"); 10411 10412 // Lambdas with return types. 10413 verifyFormat("int c = []() -> int { return 2; }();\n"); 10414 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10415 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10416 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10417 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10418 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10419 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10420 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10421 verifyFormat("[a, a]() -> a<1> {};"); 10422 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10423 " int j) -> int {\n" 10424 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10425 "};"); 10426 verifyFormat( 10427 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10428 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10429 " return aaaaaaaaaaaaaaaaa;\n" 10430 " });", 10431 getLLVMStyleWithColumns(70)); 10432 verifyFormat("[]() //\n" 10433 " -> int {\n" 10434 " return 1; //\n" 10435 "};"); 10436 10437 // Multiple lambdas in the same parentheses change indentation rules. 10438 verifyFormat("SomeFunction(\n" 10439 " []() {\n" 10440 " int i = 42;\n" 10441 " return i;\n" 10442 " },\n" 10443 " []() {\n" 10444 " int j = 43;\n" 10445 " return j;\n" 10446 " });"); 10447 10448 // More complex introducers. 10449 verifyFormat("return [i, args...] {};"); 10450 10451 // Not lambdas. 10452 verifyFormat("constexpr char hello[]{\"hello\"};"); 10453 verifyFormat("double &operator[](int i) { return 0; }\n" 10454 "int i;"); 10455 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10456 verifyFormat("int i = a[a][a]->f();"); 10457 verifyFormat("int i = (*b)[a]->f();"); 10458 10459 // Other corner cases. 10460 verifyFormat("void f() {\n" 10461 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10462 " );\n" 10463 "}"); 10464 10465 // Lambdas created through weird macros. 10466 verifyFormat("void f() {\n" 10467 " MACRO((const AA &a) { return 1; });\n" 10468 " MACRO((AA &a) { return 1; });\n" 10469 "}"); 10470 10471 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10472 " doo_dah();\n" 10473 " doo_dah();\n" 10474 " })) {\n" 10475 "}"); 10476 verifyFormat("auto lambda = []() {\n" 10477 " int a = 2\n" 10478 "#if A\n" 10479 " + 2\n" 10480 "#endif\n" 10481 " ;\n" 10482 "};"); 10483 } 10484 10485 TEST_F(FormatTest, FormatsBlocks) { 10486 FormatStyle ShortBlocks = getLLVMStyle(); 10487 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10488 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10489 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10490 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10491 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10492 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10493 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10494 10495 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10496 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10497 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10498 10499 verifyFormat("[operation setCompletionBlock:^{\n" 10500 " [self onOperationDone];\n" 10501 "}];"); 10502 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10503 " [self onOperationDone];\n" 10504 "}]};"); 10505 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10506 " f();\n" 10507 "}];"); 10508 verifyFormat("int a = [operation block:^int(int *i) {\n" 10509 " return 1;\n" 10510 "}];"); 10511 verifyFormat("[myObject doSomethingWith:arg1\n" 10512 " aaa:^int(int *a) {\n" 10513 " return 1;\n" 10514 " }\n" 10515 " bbb:f(a * bbbbbbbb)];"); 10516 10517 verifyFormat("[operation setCompletionBlock:^{\n" 10518 " [self.delegate newDataAvailable];\n" 10519 "}];", 10520 getLLVMStyleWithColumns(60)); 10521 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10522 " NSString *path = [self sessionFilePath];\n" 10523 " if (path) {\n" 10524 " // ...\n" 10525 " }\n" 10526 "});"); 10527 verifyFormat("[[SessionService sharedService]\n" 10528 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10529 " if (window) {\n" 10530 " [self windowDidLoad:window];\n" 10531 " } else {\n" 10532 " [self errorLoadingWindow];\n" 10533 " }\n" 10534 " }];"); 10535 verifyFormat("void (^largeBlock)(void) = ^{\n" 10536 " // ...\n" 10537 "};\n", 10538 getLLVMStyleWithColumns(40)); 10539 verifyFormat("[[SessionService sharedService]\n" 10540 " loadWindowWithCompletionBlock: //\n" 10541 " ^(SessionWindow *window) {\n" 10542 " if (window) {\n" 10543 " [self windowDidLoad:window];\n" 10544 " } else {\n" 10545 " [self errorLoadingWindow];\n" 10546 " }\n" 10547 " }];", 10548 getLLVMStyleWithColumns(60)); 10549 verifyFormat("[myObject doSomethingWith:arg1\n" 10550 " firstBlock:^(Foo *a) {\n" 10551 " // ...\n" 10552 " int i;\n" 10553 " }\n" 10554 " secondBlock:^(Bar *b) {\n" 10555 " // ...\n" 10556 " int i;\n" 10557 " }\n" 10558 " thirdBlock:^Foo(Bar *b) {\n" 10559 " // ...\n" 10560 " int i;\n" 10561 " }];"); 10562 verifyFormat("[myObject doSomethingWith:arg1\n" 10563 " firstBlock:-1\n" 10564 " secondBlock:^(Bar *b) {\n" 10565 " // ...\n" 10566 " int i;\n" 10567 " }];"); 10568 10569 verifyFormat("f(^{\n" 10570 " @autoreleasepool {\n" 10571 " if (a) {\n" 10572 " g();\n" 10573 " }\n" 10574 " }\n" 10575 "});"); 10576 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10577 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10578 "};"); 10579 10580 FormatStyle FourIndent = getLLVMStyle(); 10581 FourIndent.ObjCBlockIndentWidth = 4; 10582 verifyFormat("[operation setCompletionBlock:^{\n" 10583 " [self onOperationDone];\n" 10584 "}];", 10585 FourIndent); 10586 } 10587 10588 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10589 FormatStyle ZeroColumn = getLLVMStyle(); 10590 ZeroColumn.ColumnLimit = 0; 10591 10592 verifyFormat("[[SessionService sharedService] " 10593 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10594 " if (window) {\n" 10595 " [self windowDidLoad:window];\n" 10596 " } else {\n" 10597 " [self errorLoadingWindow];\n" 10598 " }\n" 10599 "}];", 10600 ZeroColumn); 10601 EXPECT_EQ("[[SessionService sharedService]\n" 10602 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10603 " if (window) {\n" 10604 " [self windowDidLoad:window];\n" 10605 " } else {\n" 10606 " [self errorLoadingWindow];\n" 10607 " }\n" 10608 " }];", 10609 format("[[SessionService sharedService]\n" 10610 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10611 " if (window) {\n" 10612 " [self windowDidLoad:window];\n" 10613 " } else {\n" 10614 " [self errorLoadingWindow];\n" 10615 " }\n" 10616 "}];", 10617 ZeroColumn)); 10618 verifyFormat("[myObject doSomethingWith:arg1\n" 10619 " firstBlock:^(Foo *a) {\n" 10620 " // ...\n" 10621 " int i;\n" 10622 " }\n" 10623 " secondBlock:^(Bar *b) {\n" 10624 " // ...\n" 10625 " int i;\n" 10626 " }\n" 10627 " thirdBlock:^Foo(Bar *b) {\n" 10628 " // ...\n" 10629 " int i;\n" 10630 " }];", 10631 ZeroColumn); 10632 verifyFormat("f(^{\n" 10633 " @autoreleasepool {\n" 10634 " if (a) {\n" 10635 " g();\n" 10636 " }\n" 10637 " }\n" 10638 "});", 10639 ZeroColumn); 10640 verifyFormat("void (^largeBlock)(void) = ^{\n" 10641 " // ...\n" 10642 "};", 10643 ZeroColumn); 10644 10645 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10646 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10647 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10648 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10649 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10650 " int i;\n" 10651 "};", 10652 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10653 } 10654 10655 TEST_F(FormatTest, SupportsCRLF) { 10656 EXPECT_EQ("int a;\r\n" 10657 "int b;\r\n" 10658 "int c;\r\n", 10659 format("int a;\r\n" 10660 " int b;\r\n" 10661 " int c;\r\n", 10662 getLLVMStyle())); 10663 EXPECT_EQ("int a;\r\n" 10664 "int b;\r\n" 10665 "int c;\r\n", 10666 format("int a;\r\n" 10667 " int b;\n" 10668 " int c;\r\n", 10669 getLLVMStyle())); 10670 EXPECT_EQ("int a;\n" 10671 "int b;\n" 10672 "int c;\n", 10673 format("int a;\r\n" 10674 " int b;\n" 10675 " int c;\n", 10676 getLLVMStyle())); 10677 EXPECT_EQ("\"aaaaaaa \"\r\n" 10678 "\"bbbbbbb\";\r\n", 10679 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10680 EXPECT_EQ("#define A \\\r\n" 10681 " b; \\\r\n" 10682 " c; \\\r\n" 10683 " d;\r\n", 10684 format("#define A \\\r\n" 10685 " b; \\\r\n" 10686 " c; d; \r\n", 10687 getGoogleStyle())); 10688 10689 EXPECT_EQ("/*\r\n" 10690 "multi line block comments\r\n" 10691 "should not introduce\r\n" 10692 "an extra carriage return\r\n" 10693 "*/\r\n", 10694 format("/*\r\n" 10695 "multi line block comments\r\n" 10696 "should not introduce\r\n" 10697 "an extra carriage return\r\n" 10698 "*/\r\n")); 10699 } 10700 10701 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10702 verifyFormat("MY_CLASS(C) {\n" 10703 " int i;\n" 10704 " int j;\n" 10705 "};"); 10706 } 10707 10708 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10709 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10710 TwoIndent.ContinuationIndentWidth = 2; 10711 10712 EXPECT_EQ("int i =\n" 10713 " longFunction(\n" 10714 " arg);", 10715 format("int i = longFunction(arg);", TwoIndent)); 10716 10717 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10718 SixIndent.ContinuationIndentWidth = 6; 10719 10720 EXPECT_EQ("int i =\n" 10721 " longFunction(\n" 10722 " arg);", 10723 format("int i = longFunction(arg);", SixIndent)); 10724 } 10725 10726 TEST_F(FormatTest, SpacesInAngles) { 10727 FormatStyle Spaces = getLLVMStyle(); 10728 Spaces.SpacesInAngles = true; 10729 10730 verifyFormat("static_cast< int >(arg);", Spaces); 10731 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10732 verifyFormat("f< int, float >();", Spaces); 10733 verifyFormat("template <> g() {}", Spaces); 10734 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10735 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10736 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10737 Spaces); 10738 10739 Spaces.Standard = FormatStyle::LS_Cpp03; 10740 Spaces.SpacesInAngles = true; 10741 verifyFormat("A< A< int > >();", Spaces); 10742 10743 Spaces.SpacesInAngles = false; 10744 verifyFormat("A<A<int> >();", Spaces); 10745 10746 Spaces.Standard = FormatStyle::LS_Cpp11; 10747 Spaces.SpacesInAngles = true; 10748 verifyFormat("A< A< int > >();", Spaces); 10749 10750 Spaces.SpacesInAngles = false; 10751 verifyFormat("A<A<int>>();", Spaces); 10752 } 10753 10754 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10755 FormatStyle Style = getLLVMStyle(); 10756 Style.SpaceAfterTemplateKeyword = false; 10757 verifyFormat("template<int> void foo();", Style); 10758 } 10759 10760 TEST_F(FormatTest, TripleAngleBrackets) { 10761 verifyFormat("f<<<1, 1>>>();"); 10762 verifyFormat("f<<<1, 1, 1, s>>>();"); 10763 verifyFormat("f<<<a, b, c, d>>>();"); 10764 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10765 verifyFormat("f<param><<<1, 1>>>();"); 10766 verifyFormat("f<1><<<1, 1>>>();"); 10767 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10768 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10769 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10770 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10771 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10772 } 10773 10774 TEST_F(FormatTest, MergeLessLessAtEnd) { 10775 verifyFormat("<<"); 10776 EXPECT_EQ("< < <", format("\\\n<<<")); 10777 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10778 "aaallvm::outs() <<"); 10779 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10780 "aaaallvm::outs()\n <<"); 10781 } 10782 10783 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10784 std::string code = "#if A\n" 10785 "#if B\n" 10786 "a.\n" 10787 "#endif\n" 10788 " a = 1;\n" 10789 "#else\n" 10790 "#endif\n" 10791 "#if C\n" 10792 "#else\n" 10793 "#endif\n"; 10794 EXPECT_EQ(code, format(code)); 10795 } 10796 10797 TEST_F(FormatTest, HandleConflictMarkers) { 10798 // Git/SVN conflict markers. 10799 EXPECT_EQ("int a;\n" 10800 "void f() {\n" 10801 " callme(some(parameter1,\n" 10802 "<<<<<<< text by the vcs\n" 10803 " parameter2),\n" 10804 "||||||| text by the vcs\n" 10805 " parameter2),\n" 10806 " parameter3,\n" 10807 "======= text by the vcs\n" 10808 " parameter2, parameter3),\n" 10809 ">>>>>>> text by the vcs\n" 10810 " otherparameter);\n", 10811 format("int a;\n" 10812 "void f() {\n" 10813 " callme(some(parameter1,\n" 10814 "<<<<<<< text by the vcs\n" 10815 " parameter2),\n" 10816 "||||||| text by the vcs\n" 10817 " parameter2),\n" 10818 " parameter3,\n" 10819 "======= text by the vcs\n" 10820 " parameter2,\n" 10821 " parameter3),\n" 10822 ">>>>>>> text by the vcs\n" 10823 " otherparameter);\n")); 10824 10825 // Perforce markers. 10826 EXPECT_EQ("void f() {\n" 10827 " function(\n" 10828 ">>>> text by the vcs\n" 10829 " parameter,\n" 10830 "==== text by the vcs\n" 10831 " parameter,\n" 10832 "==== text by the vcs\n" 10833 " parameter,\n" 10834 "<<<< text by the vcs\n" 10835 " parameter);\n", 10836 format("void f() {\n" 10837 " function(\n" 10838 ">>>> text by the vcs\n" 10839 " parameter,\n" 10840 "==== text by the vcs\n" 10841 " parameter,\n" 10842 "==== text by the vcs\n" 10843 " parameter,\n" 10844 "<<<< text by the vcs\n" 10845 " parameter);\n")); 10846 10847 EXPECT_EQ("<<<<<<<\n" 10848 "|||||||\n" 10849 "=======\n" 10850 ">>>>>>>", 10851 format("<<<<<<<\n" 10852 "|||||||\n" 10853 "=======\n" 10854 ">>>>>>>")); 10855 10856 EXPECT_EQ("<<<<<<<\n" 10857 "|||||||\n" 10858 "int i;\n" 10859 "=======\n" 10860 ">>>>>>>", 10861 format("<<<<<<<\n" 10862 "|||||||\n" 10863 "int i;\n" 10864 "=======\n" 10865 ">>>>>>>")); 10866 10867 // FIXME: Handle parsing of macros around conflict markers correctly: 10868 EXPECT_EQ("#define Macro \\\n" 10869 "<<<<<<<\n" 10870 "Something \\\n" 10871 "|||||||\n" 10872 "Else \\\n" 10873 "=======\n" 10874 "Other \\\n" 10875 ">>>>>>>\n" 10876 " End int i;\n", 10877 format("#define Macro \\\n" 10878 "<<<<<<<\n" 10879 " Something \\\n" 10880 "|||||||\n" 10881 " Else \\\n" 10882 "=======\n" 10883 " Other \\\n" 10884 ">>>>>>>\n" 10885 " End\n" 10886 "int i;\n")); 10887 } 10888 10889 TEST_F(FormatTest, DisableRegions) { 10890 EXPECT_EQ("int i;\n" 10891 "// clang-format off\n" 10892 " int j;\n" 10893 "// clang-format on\n" 10894 "int k;", 10895 format(" int i;\n" 10896 " // clang-format off\n" 10897 " int j;\n" 10898 " // clang-format on\n" 10899 " int k;")); 10900 EXPECT_EQ("int i;\n" 10901 "/* clang-format off */\n" 10902 " int j;\n" 10903 "/* clang-format on */\n" 10904 "int k;", 10905 format(" int i;\n" 10906 " /* clang-format off */\n" 10907 " int j;\n" 10908 " /* clang-format on */\n" 10909 " int k;")); 10910 } 10911 10912 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10913 format("? ) ="); 10914 verifyNoCrash("#define a\\\n /**/}"); 10915 } 10916 10917 TEST_F(FormatTest, FormatsTableGenCode) { 10918 FormatStyle Style = getLLVMStyle(); 10919 Style.Language = FormatStyle::LK_TableGen; 10920 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10921 } 10922 10923 TEST_F(FormatTest, ArrayOfTemplates) { 10924 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10925 format("auto a = new unique_ptr<int > [ 10];")); 10926 10927 FormatStyle Spaces = getLLVMStyle(); 10928 Spaces.SpacesInSquareBrackets = true; 10929 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10930 format("auto a = new unique_ptr<int > [10];", Spaces)); 10931 } 10932 10933 TEST_F(FormatTest, ArrayAsTemplateType) { 10934 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10935 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10936 10937 FormatStyle Spaces = getLLVMStyle(); 10938 Spaces.SpacesInSquareBrackets = true; 10939 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10940 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10941 } 10942 10943 TEST(FormatStyle, GetStyleOfFile) { 10944 vfs::InMemoryFileSystem FS; 10945 // Test 1: format file in the same directory. 10946 ASSERT_TRUE( 10947 FS.addFile("/a/.clang-format", 0, 10948 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10949 ASSERT_TRUE( 10950 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10951 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10952 ASSERT_EQ(Style1, getLLVMStyle()); 10953 10954 // Test 2: fallback to default. 10955 ASSERT_TRUE( 10956 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10957 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10958 ASSERT_EQ(Style2, getMozillaStyle()); 10959 10960 // Test 3: format file in parent directory. 10961 ASSERT_TRUE( 10962 FS.addFile("/c/.clang-format", 0, 10963 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10964 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10965 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10966 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10967 ASSERT_EQ(Style3, getGoogleStyle()); 10968 } 10969 10970 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 10971 // Column limit is 20. 10972 std::string Code = "Type *a =\n" 10973 " new Type();\n" 10974 "g(iiiii, 0, jjjjj,\n" 10975 " 0, kkkkk, 0, mm);\n" 10976 "int bad = format ;"; 10977 std::string Expected = "auto a = new Type();\n" 10978 "g(iiiii, nullptr,\n" 10979 " jjjjj, nullptr,\n" 10980 " kkkkk, nullptr,\n" 10981 " mm);\n" 10982 "int bad = format ;"; 10983 FileID ID = Context.createInMemoryFile("format.cpp", Code); 10984 tooling::Replacements Replaces = toReplacements( 10985 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 10986 "auto "), 10987 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 10988 "nullptr"), 10989 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 10990 "nullptr"), 10991 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 10992 "nullptr")}); 10993 10994 format::FormatStyle Style = format::getLLVMStyle(); 10995 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 10996 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10997 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10998 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10999 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11000 EXPECT_TRUE(static_cast<bool>(Result)); 11001 EXPECT_EQ(Expected, *Result); 11002 } 11003 11004 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11005 std::string Code = "#include \"a.h\"\n" 11006 "#include \"c.h\"\n" 11007 "\n" 11008 "int main() {\n" 11009 " return 0;\n" 11010 "}"; 11011 std::string Expected = "#include \"a.h\"\n" 11012 "#include \"b.h\"\n" 11013 "#include \"c.h\"\n" 11014 "\n" 11015 "int main() {\n" 11016 " return 0;\n" 11017 "}"; 11018 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11019 tooling::Replacements Replaces = toReplacements( 11020 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11021 "#include \"b.h\"\n")}); 11022 11023 format::FormatStyle Style = format::getLLVMStyle(); 11024 Style.SortIncludes = true; 11025 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11026 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11027 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11028 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11029 EXPECT_TRUE(static_cast<bool>(Result)); 11030 EXPECT_EQ(Expected, *Result); 11031 } 11032 11033 TEST_F(FormatTest, AllignTrailingComments) { 11034 EXPECT_EQ("#define MACRO(V) \\\n" 11035 " V(Rt2) /* one more char */ \\\n" 11036 " V(Rs) /* than here */ \\\n" 11037 "/* comment 3 */\n", 11038 format("#define MACRO(V)\\\n" 11039 "V(Rt2) /* one more char */ \\\n" 11040 "V(Rs) /* than here */ \\\n" 11041 "/* comment 3 */ \\\n", 11042 getLLVMStyleWithColumns(40))); 11043 } 11044 } // end namespace 11045 } // end namespace format 11046 } // end namespace clang 11047