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 } 5218 5219 TEST_F(FormatTest, UnderstandsEquals) { 5220 verifyFormat( 5221 "aaaaaaaaaaaaaaaaa =\n" 5222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5223 verifyFormat( 5224 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5226 verifyFormat( 5227 "if (a) {\n" 5228 " f();\n" 5229 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5231 "}"); 5232 5233 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5234 " 100000000 + 10000000) {\n}"); 5235 } 5236 5237 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5238 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5239 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5240 5241 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5242 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5243 5244 verifyFormat( 5245 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5246 " Parameter2);"); 5247 5248 verifyFormat( 5249 "ShortObject->shortFunction(\n" 5250 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5251 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5252 5253 verifyFormat("loooooooooooooongFunction(\n" 5254 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5255 5256 verifyFormat( 5257 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5258 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5259 5260 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5261 " .WillRepeatedly(Return(SomeValue));"); 5262 verifyFormat("void f() {\n" 5263 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5264 " .Times(2)\n" 5265 " .WillRepeatedly(Return(SomeValue));\n" 5266 "}"); 5267 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5268 " ccccccccccccccccccccccc);"); 5269 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5271 " .aaaaa(aaaaa),\n" 5272 " aaaaaaaaaaaaaaaaaaaaa);"); 5273 verifyFormat("void f() {\n" 5274 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5275 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5276 "}"); 5277 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5279 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5281 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5282 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5283 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5284 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5285 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5286 "}"); 5287 5288 // Here, it is not necessary to wrap at "." or "->". 5289 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5290 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5291 verifyFormat( 5292 "aaaaaaaaaaa->aaaaaaaaa(\n" 5293 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5294 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5295 5296 verifyFormat( 5297 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5299 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5300 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5301 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5302 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5303 5304 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5305 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5306 " .a();"); 5307 5308 FormatStyle NoBinPacking = getLLVMStyle(); 5309 NoBinPacking.BinPackParameters = false; 5310 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5311 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5312 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5313 " aaaaaaaaaaaaaaaaaaa,\n" 5314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5315 NoBinPacking); 5316 5317 // If there is a subsequent call, change to hanging indentation. 5318 verifyFormat( 5319 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5320 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5321 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5322 verifyFormat( 5323 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5324 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5325 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5327 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5328 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5330 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5331 } 5332 5333 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5334 verifyFormat("template <typename T>\n" 5335 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5336 verifyFormat("template <typename T>\n" 5337 "// T should be one of {A, B}.\n" 5338 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5339 verifyFormat( 5340 "template <typename T>\n" 5341 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5342 verifyFormat("template <typename T>\n" 5343 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5344 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5345 verifyFormat( 5346 "template <typename T>\n" 5347 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5348 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5349 verifyFormat( 5350 "template <typename T>\n" 5351 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5352 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5354 verifyFormat("template <typename T>\n" 5355 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5356 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5357 verifyFormat( 5358 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5359 " typename T4 = char>\n" 5360 "void f();"); 5361 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5362 " template <typename> class cccccccccccccccccccccc,\n" 5363 " typename ddddddddddddd>\n" 5364 "class C {};"); 5365 verifyFormat( 5366 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5367 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5368 5369 verifyFormat("void f() {\n" 5370 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5371 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5372 "}"); 5373 5374 verifyFormat("template <typename T> class C {};"); 5375 verifyFormat("template <typename T> void f();"); 5376 verifyFormat("template <typename T> void f() {}"); 5377 verifyFormat( 5378 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5381 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5383 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5384 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5385 getLLVMStyleWithColumns(72)); 5386 EXPECT_EQ("static_cast<A< //\n" 5387 " B> *>(\n" 5388 "\n" 5389 " );", 5390 format("static_cast<A<//\n" 5391 " B>*>(\n" 5392 "\n" 5393 " );")); 5394 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5395 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5396 5397 FormatStyle AlwaysBreak = getLLVMStyle(); 5398 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5399 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5400 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5401 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5402 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5403 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5404 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5405 verifyFormat("template <template <typename> class Fooooooo,\n" 5406 " template <typename> class Baaaaaaar>\n" 5407 "struct C {};", 5408 AlwaysBreak); 5409 verifyFormat("template <typename T> // T can be A, B or C.\n" 5410 "struct C {};", 5411 AlwaysBreak); 5412 verifyFormat("template <enum E> class A {\n" 5413 "public:\n" 5414 " E *f();\n" 5415 "};"); 5416 } 5417 5418 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5419 verifyFormat( 5420 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5422 verifyFormat( 5423 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5426 5427 // FIXME: Should we have the extra indent after the second break? 5428 verifyFormat( 5429 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5432 5433 verifyFormat( 5434 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5435 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5436 5437 // Breaking at nested name specifiers is generally not desirable. 5438 verifyFormat( 5439 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5440 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5441 5442 verifyFormat( 5443 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5445 " aaaaaaaaaaaaaaaaaaaaa);", 5446 getLLVMStyleWithColumns(74)); 5447 5448 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5450 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5451 } 5452 5453 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5454 verifyFormat("A<int> a;"); 5455 verifyFormat("A<A<A<int>>> a;"); 5456 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5457 verifyFormat("bool x = a < 1 || 2 > a;"); 5458 verifyFormat("bool x = 5 < f<int>();"); 5459 verifyFormat("bool x = f<int>() > 5;"); 5460 verifyFormat("bool x = 5 < a<int>::x;"); 5461 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5462 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5463 5464 verifyGoogleFormat("A<A<int>> a;"); 5465 verifyGoogleFormat("A<A<A<int>>> a;"); 5466 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5467 verifyGoogleFormat("A<A<int> > a;"); 5468 verifyGoogleFormat("A<A<A<int> > > a;"); 5469 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5470 verifyGoogleFormat("A<::A<int>> a;"); 5471 verifyGoogleFormat("A<::A> a;"); 5472 verifyGoogleFormat("A< ::A> a;"); 5473 verifyGoogleFormat("A< ::A<int> > a;"); 5474 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5475 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5476 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5477 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5478 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5479 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5480 5481 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5482 5483 verifyFormat("test >> a >> b;"); 5484 verifyFormat("test << a >> b;"); 5485 5486 verifyFormat("f<int>();"); 5487 verifyFormat("template <typename T> void f() {}"); 5488 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5489 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5490 "sizeof(char)>::type>;"); 5491 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5492 verifyFormat("f(a.operator()<A>());"); 5493 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5494 " .template operator()<A>());", 5495 getLLVMStyleWithColumns(35)); 5496 5497 // Not template parameters. 5498 verifyFormat("return a < b && c > d;"); 5499 verifyFormat("void f() {\n" 5500 " while (a < b && c > d) {\n" 5501 " }\n" 5502 "}"); 5503 verifyFormat("template <typename... Types>\n" 5504 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5505 5506 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5508 getLLVMStyleWithColumns(60)); 5509 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5510 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5511 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5512 } 5513 5514 TEST_F(FormatTest, BitshiftOperatorWidth) { 5515 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5516 " bar */", 5517 format("int a=1<<2; /* foo\n" 5518 " bar */")); 5519 5520 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5521 " bar */", 5522 format("int b =256>>1 ; /* foo\n" 5523 " bar */")); 5524 } 5525 5526 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5527 verifyFormat("COMPARE(a, ==, b);"); 5528 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5529 } 5530 5531 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5532 verifyFormat("int A::*x;"); 5533 verifyFormat("int (S::*func)(void *);"); 5534 verifyFormat("void f() { int (S::*func)(void *); }"); 5535 verifyFormat("typedef bool *(Class::*Member)() const;"); 5536 verifyFormat("void f() {\n" 5537 " (a->*f)();\n" 5538 " a->*x;\n" 5539 " (a.*f)();\n" 5540 " ((*a).*f)();\n" 5541 " a.*x;\n" 5542 "}"); 5543 verifyFormat("void f() {\n" 5544 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5545 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5546 "}"); 5547 verifyFormat( 5548 "(aaaaaaaaaa->*bbbbbbb)(\n" 5549 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5550 FormatStyle Style = getLLVMStyle(); 5551 Style.PointerAlignment = FormatStyle::PAS_Left; 5552 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5553 } 5554 5555 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5556 verifyFormat("int a = -2;"); 5557 verifyFormat("f(-1, -2, -3);"); 5558 verifyFormat("a[-1] = 5;"); 5559 verifyFormat("int a = 5 + -2;"); 5560 verifyFormat("if (i == -1) {\n}"); 5561 verifyFormat("if (i != -1) {\n}"); 5562 verifyFormat("if (i > -1) {\n}"); 5563 verifyFormat("if (i < -1) {\n}"); 5564 verifyFormat("++(a->f());"); 5565 verifyFormat("--(a->f());"); 5566 verifyFormat("(a->f())++;"); 5567 verifyFormat("a[42]++;"); 5568 verifyFormat("if (!(a->f())) {\n}"); 5569 5570 verifyFormat("a-- > b;"); 5571 verifyFormat("b ? -a : c;"); 5572 verifyFormat("n * sizeof char16;"); 5573 verifyFormat("n * alignof char16;", getGoogleStyle()); 5574 verifyFormat("sizeof(char);"); 5575 verifyFormat("alignof(char);", getGoogleStyle()); 5576 5577 verifyFormat("return -1;"); 5578 verifyFormat("switch (a) {\n" 5579 "case -1:\n" 5580 " break;\n" 5581 "}"); 5582 verifyFormat("#define X -1"); 5583 verifyFormat("#define X -kConstant"); 5584 5585 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5586 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5587 5588 verifyFormat("int a = /* confusing comment */ -1;"); 5589 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5590 verifyFormat("int a = i /* confusing comment */++;"); 5591 } 5592 5593 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5594 verifyFormat("if (!aaaaaaaaaa( // break\n" 5595 " aaaaa)) {\n" 5596 "}"); 5597 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5598 " aaaaa));"); 5599 verifyFormat("*aaa = aaaaaaa( // break\n" 5600 " bbbbbb);"); 5601 } 5602 5603 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5604 verifyFormat("bool operator<();"); 5605 verifyFormat("bool operator>();"); 5606 verifyFormat("bool operator=();"); 5607 verifyFormat("bool operator==();"); 5608 verifyFormat("bool operator!=();"); 5609 verifyFormat("int operator+();"); 5610 verifyFormat("int operator++();"); 5611 verifyFormat("bool operator,();"); 5612 verifyFormat("bool operator();"); 5613 verifyFormat("bool operator()();"); 5614 verifyFormat("bool operator[]();"); 5615 verifyFormat("operator bool();"); 5616 verifyFormat("operator int();"); 5617 verifyFormat("operator void *();"); 5618 verifyFormat("operator SomeType<int>();"); 5619 verifyFormat("operator SomeType<int, int>();"); 5620 verifyFormat("operator SomeType<SomeType<int>>();"); 5621 verifyFormat("void *operator new(std::size_t size);"); 5622 verifyFormat("void *operator new[](std::size_t size);"); 5623 verifyFormat("void operator delete(void *ptr);"); 5624 verifyFormat("void operator delete[](void *ptr);"); 5625 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5626 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5627 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5628 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5629 5630 verifyFormat( 5631 "ostream &operator<<(ostream &OutputStream,\n" 5632 " SomeReallyLongType WithSomeReallyLongValue);"); 5633 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5634 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5635 " return left.group < right.group;\n" 5636 "}"); 5637 verifyFormat("SomeType &operator=(const SomeType &S);"); 5638 verifyFormat("f.template operator()<int>();"); 5639 5640 verifyGoogleFormat("operator void*();"); 5641 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5642 verifyGoogleFormat("operator ::A();"); 5643 5644 verifyFormat("using A::operator+;"); 5645 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5646 "int i;"); 5647 } 5648 5649 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5650 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5651 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5652 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5653 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5654 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5655 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5656 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5657 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5658 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5659 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5660 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5661 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5662 verifyFormat("template <typename T>\n" 5663 "void F(T) && = delete;", 5664 getGoogleStyle()); 5665 5666 FormatStyle AlignLeft = getLLVMStyle(); 5667 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5668 verifyFormat("void A::b() && {}", AlignLeft); 5669 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5670 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5671 AlignLeft); 5672 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5673 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5674 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5675 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5676 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5677 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5678 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5679 5680 FormatStyle Spaces = getLLVMStyle(); 5681 Spaces.SpacesInCStyleCastParentheses = true; 5682 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5683 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5684 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5685 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5686 5687 Spaces.SpacesInCStyleCastParentheses = false; 5688 Spaces.SpacesInParentheses = true; 5689 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5690 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5691 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5692 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5693 } 5694 5695 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5696 verifyFormat("void f() {\n" 5697 " A *a = new A;\n" 5698 " A *a = new (placement) A;\n" 5699 " delete a;\n" 5700 " delete (A *)a;\n" 5701 "}"); 5702 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5703 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5704 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5705 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5706 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5707 verifyFormat("delete[] h->p;"); 5708 } 5709 5710 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5711 verifyFormat("int *f(int *a) {}"); 5712 verifyFormat("int main(int argc, char **argv) {}"); 5713 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5714 verifyIndependentOfContext("f(a, *a);"); 5715 verifyFormat("void g() { f(*a); }"); 5716 verifyIndependentOfContext("int a = b * 10;"); 5717 verifyIndependentOfContext("int a = 10 * b;"); 5718 verifyIndependentOfContext("int a = b * c;"); 5719 verifyIndependentOfContext("int a += b * c;"); 5720 verifyIndependentOfContext("int a -= b * c;"); 5721 verifyIndependentOfContext("int a *= b * c;"); 5722 verifyIndependentOfContext("int a /= b * c;"); 5723 verifyIndependentOfContext("int a = *b;"); 5724 verifyIndependentOfContext("int a = *b * c;"); 5725 verifyIndependentOfContext("int a = b * *c;"); 5726 verifyIndependentOfContext("int a = b * (10);"); 5727 verifyIndependentOfContext("S << b * (10);"); 5728 verifyIndependentOfContext("return 10 * b;"); 5729 verifyIndependentOfContext("return *b * *c;"); 5730 verifyIndependentOfContext("return a & ~b;"); 5731 verifyIndependentOfContext("f(b ? *c : *d);"); 5732 verifyIndependentOfContext("int a = b ? *c : *d;"); 5733 verifyIndependentOfContext("*b = a;"); 5734 verifyIndependentOfContext("a * ~b;"); 5735 verifyIndependentOfContext("a * !b;"); 5736 verifyIndependentOfContext("a * +b;"); 5737 verifyIndependentOfContext("a * -b;"); 5738 verifyIndependentOfContext("a * ++b;"); 5739 verifyIndependentOfContext("a * --b;"); 5740 verifyIndependentOfContext("a[4] * b;"); 5741 verifyIndependentOfContext("a[a * a] = 1;"); 5742 verifyIndependentOfContext("f() * b;"); 5743 verifyIndependentOfContext("a * [self dostuff];"); 5744 verifyIndependentOfContext("int x = a * (a + b);"); 5745 verifyIndependentOfContext("(a *)(a + b);"); 5746 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5747 verifyIndependentOfContext("int *pa = (int *)&a;"); 5748 verifyIndependentOfContext("return sizeof(int **);"); 5749 verifyIndependentOfContext("return sizeof(int ******);"); 5750 verifyIndependentOfContext("return (int **&)a;"); 5751 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5752 verifyFormat("void f(Type (*parameter)[10]) {}"); 5753 verifyFormat("void f(Type (¶meter)[10]) {}"); 5754 verifyGoogleFormat("return sizeof(int**);"); 5755 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5756 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5757 verifyFormat("auto a = [](int **&, int ***) {};"); 5758 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5759 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5760 verifyFormat("[](const decltype(*a) &value) {}"); 5761 verifyFormat("decltype(a * b) F();"); 5762 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5763 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5764 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5765 verifyIndependentOfContext("int i{a * b};"); 5766 verifyIndependentOfContext("aaa && aaa->f();"); 5767 verifyIndependentOfContext("int x = ~*p;"); 5768 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5769 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5770 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5771 verifyFormat("void f() { f(a, c * d); }"); 5772 verifyFormat("void f() { f(new a(), c * d); }"); 5773 5774 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5775 5776 verifyIndependentOfContext("A<int *> a;"); 5777 verifyIndependentOfContext("A<int **> a;"); 5778 verifyIndependentOfContext("A<int *, int *> a;"); 5779 verifyIndependentOfContext("A<int *[]> a;"); 5780 verifyIndependentOfContext( 5781 "const char *const p = reinterpret_cast<const char *const>(q);"); 5782 verifyIndependentOfContext("A<int **, int **> a;"); 5783 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5784 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5785 verifyFormat("for (; a && b;) {\n}"); 5786 verifyFormat("bool foo = true && [] { return false; }();"); 5787 5788 verifyFormat( 5789 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5791 5792 verifyGoogleFormat("int const* a = &b;"); 5793 verifyGoogleFormat("**outparam = 1;"); 5794 verifyGoogleFormat("*outparam = a * b;"); 5795 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5796 verifyGoogleFormat("A<int*> a;"); 5797 verifyGoogleFormat("A<int**> a;"); 5798 verifyGoogleFormat("A<int*, int*> a;"); 5799 verifyGoogleFormat("A<int**, int**> a;"); 5800 verifyGoogleFormat("f(b ? *c : *d);"); 5801 verifyGoogleFormat("int a = b ? *c : *d;"); 5802 verifyGoogleFormat("Type* t = **x;"); 5803 verifyGoogleFormat("Type* t = *++*x;"); 5804 verifyGoogleFormat("*++*x;"); 5805 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5806 verifyGoogleFormat("Type* t = x++ * y;"); 5807 verifyGoogleFormat( 5808 "const char* const p = reinterpret_cast<const char* const>(q);"); 5809 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5810 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5811 verifyGoogleFormat("template <typename T>\n" 5812 "void f(int i = 0, SomeType** temps = NULL);"); 5813 5814 FormatStyle Left = getLLVMStyle(); 5815 Left.PointerAlignment = FormatStyle::PAS_Left; 5816 verifyFormat("x = *a(x) = *a(y);", Left); 5817 verifyFormat("for (;; *a = b) {\n}", Left); 5818 verifyFormat("return *this += 1;", Left); 5819 5820 verifyIndependentOfContext("a = *(x + y);"); 5821 verifyIndependentOfContext("a = &(x + y);"); 5822 verifyIndependentOfContext("*(x + y).call();"); 5823 verifyIndependentOfContext("&(x + y)->call();"); 5824 verifyFormat("void f() { &(*I).first; }"); 5825 5826 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5827 verifyFormat( 5828 "int *MyValues = {\n" 5829 " *A, // Operator detection might be confused by the '{'\n" 5830 " *BB // Operator detection might be confused by previous comment\n" 5831 "};"); 5832 5833 verifyIndependentOfContext("if (int *a = &b)"); 5834 verifyIndependentOfContext("if (int &a = *b)"); 5835 verifyIndependentOfContext("if (a & b[i])"); 5836 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5837 verifyIndependentOfContext("if (*b[i])"); 5838 verifyIndependentOfContext("if (int *a = (&b))"); 5839 verifyIndependentOfContext("while (int *a = &b)"); 5840 verifyIndependentOfContext("size = sizeof *a;"); 5841 verifyIndependentOfContext("if (a && (b = c))"); 5842 verifyFormat("void f() {\n" 5843 " for (const int &v : Values) {\n" 5844 " }\n" 5845 "}"); 5846 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5847 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5848 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5849 5850 verifyFormat("#define A (!a * b)"); 5851 verifyFormat("#define MACRO \\\n" 5852 " int *i = a * b; \\\n" 5853 " void f(a *b);", 5854 getLLVMStyleWithColumns(19)); 5855 5856 verifyIndependentOfContext("A = new SomeType *[Length];"); 5857 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5858 verifyIndependentOfContext("T **t = new T *;"); 5859 verifyIndependentOfContext("T **t = new T *();"); 5860 verifyGoogleFormat("A = new SomeType*[Length]();"); 5861 verifyGoogleFormat("A = new SomeType*[Length];"); 5862 verifyGoogleFormat("T** t = new T*;"); 5863 verifyGoogleFormat("T** t = new T*();"); 5864 5865 FormatStyle PointerLeft = getLLVMStyle(); 5866 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5867 verifyFormat("delete *x;", PointerLeft); 5868 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5869 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5870 verifyFormat("template <bool a, bool b> " 5871 "typename t::if<x && y>::type f() {}"); 5872 verifyFormat("template <int *y> f() {}"); 5873 verifyFormat("vector<int *> v;"); 5874 verifyFormat("vector<int *const> v;"); 5875 verifyFormat("vector<int *const **const *> v;"); 5876 verifyFormat("vector<int *volatile> v;"); 5877 verifyFormat("vector<a * b> v;"); 5878 verifyFormat("foo<b && false>();"); 5879 verifyFormat("foo<b & 1>();"); 5880 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5881 verifyFormat( 5882 "template <class T,\n" 5883 " class = typename std::enable_if<\n" 5884 " std::is_integral<T>::value &&\n" 5885 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5886 "void F();", 5887 getLLVMStyleWithColumns(70)); 5888 verifyFormat( 5889 "template <class T,\n" 5890 " class = typename ::std::enable_if<\n" 5891 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5892 "void F();", 5893 getGoogleStyleWithColumns(68)); 5894 5895 verifyIndependentOfContext("MACRO(int *i);"); 5896 verifyIndependentOfContext("MACRO(auto *a);"); 5897 verifyIndependentOfContext("MACRO(const A *a);"); 5898 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5899 verifyFormat("void f() { f(float{1}, a * a); }"); 5900 // FIXME: Is there a way to make this work? 5901 // verifyIndependentOfContext("MACRO(A *a);"); 5902 5903 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5904 verifyFormat("return options != nullptr && operator==(*options);"); 5905 5906 EXPECT_EQ("#define OP(x) \\\n" 5907 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5908 " return s << a.DebugString(); \\\n" 5909 " }", 5910 format("#define OP(x) \\\n" 5911 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5912 " return s << a.DebugString(); \\\n" 5913 " }", 5914 getLLVMStyleWithColumns(50))); 5915 5916 // FIXME: We cannot handle this case yet; we might be able to figure out that 5917 // foo<x> d > v; doesn't make sense. 5918 verifyFormat("foo<a<b && c> d> v;"); 5919 5920 FormatStyle PointerMiddle = getLLVMStyle(); 5921 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5922 verifyFormat("delete *x;", PointerMiddle); 5923 verifyFormat("int * x;", PointerMiddle); 5924 verifyFormat("template <int * y> f() {}", PointerMiddle); 5925 verifyFormat("int * f(int * a) {}", PointerMiddle); 5926 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5927 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5928 verifyFormat("A<int *> a;", PointerMiddle); 5929 verifyFormat("A<int **> a;", PointerMiddle); 5930 verifyFormat("A<int *, int *> a;", PointerMiddle); 5931 verifyFormat("A<int * []> a;", PointerMiddle); 5932 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5933 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5934 verifyFormat("T ** t = new T *;", PointerMiddle); 5935 5936 // Member function reference qualifiers aren't binary operators. 5937 verifyFormat("string // break\n" 5938 "operator()() & {}"); 5939 verifyFormat("string // break\n" 5940 "operator()() && {}"); 5941 verifyGoogleFormat("template <typename T>\n" 5942 "auto x() & -> int {}"); 5943 } 5944 5945 TEST_F(FormatTest, UnderstandsAttributes) { 5946 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5947 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5948 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5949 FormatStyle AfterType = getLLVMStyle(); 5950 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5951 verifyFormat("__attribute__((nodebug)) void\n" 5952 "foo() {}\n", 5953 AfterType); 5954 } 5955 5956 TEST_F(FormatTest, UnderstandsEllipsis) { 5957 verifyFormat("int printf(const char *fmt, ...);"); 5958 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5959 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5960 5961 FormatStyle PointersLeft = getLLVMStyle(); 5962 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5963 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5964 } 5965 5966 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5967 EXPECT_EQ("int *a;\n" 5968 "int *a;\n" 5969 "int *a;", 5970 format("int *a;\n" 5971 "int* a;\n" 5972 "int *a;", 5973 getGoogleStyle())); 5974 EXPECT_EQ("int* a;\n" 5975 "int* a;\n" 5976 "int* a;", 5977 format("int* a;\n" 5978 "int* a;\n" 5979 "int *a;", 5980 getGoogleStyle())); 5981 EXPECT_EQ("int *a;\n" 5982 "int *a;\n" 5983 "int *a;", 5984 format("int *a;\n" 5985 "int * a;\n" 5986 "int * a;", 5987 getGoogleStyle())); 5988 EXPECT_EQ("auto x = [] {\n" 5989 " int *a;\n" 5990 " int *a;\n" 5991 " int *a;\n" 5992 "};", 5993 format("auto x=[]{int *a;\n" 5994 "int * a;\n" 5995 "int * a;};", 5996 getGoogleStyle())); 5997 } 5998 5999 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6000 verifyFormat("int f(int &&a) {}"); 6001 verifyFormat("int f(int a, char &&b) {}"); 6002 verifyFormat("void f() { int &&a = b; }"); 6003 verifyGoogleFormat("int f(int a, char&& b) {}"); 6004 verifyGoogleFormat("void f() { int&& a = b; }"); 6005 6006 verifyIndependentOfContext("A<int &&> a;"); 6007 verifyIndependentOfContext("A<int &&, int &&> a;"); 6008 verifyGoogleFormat("A<int&&> a;"); 6009 verifyGoogleFormat("A<int&&, int&&> a;"); 6010 6011 // Not rvalue references: 6012 verifyFormat("template <bool B, bool C> class A {\n" 6013 " static_assert(B && C, \"Something is wrong\");\n" 6014 "};"); 6015 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6016 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6017 verifyFormat("#define A(a, b) (a && b)"); 6018 } 6019 6020 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6021 verifyFormat("void f() {\n" 6022 " x[aaaaaaaaa -\n" 6023 " b] = 23;\n" 6024 "}", 6025 getLLVMStyleWithColumns(15)); 6026 } 6027 6028 TEST_F(FormatTest, FormatsCasts) { 6029 verifyFormat("Type *A = static_cast<Type *>(P);"); 6030 verifyFormat("Type *A = (Type *)P;"); 6031 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6032 verifyFormat("int a = (int)(2.0f);"); 6033 verifyFormat("int a = (int)2.0f;"); 6034 verifyFormat("x[(int32)y];"); 6035 verifyFormat("x = (int32)y;"); 6036 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6037 verifyFormat("int a = (int)*b;"); 6038 verifyFormat("int a = (int)2.0f;"); 6039 verifyFormat("int a = (int)~0;"); 6040 verifyFormat("int a = (int)++a;"); 6041 verifyFormat("int a = (int)sizeof(int);"); 6042 verifyFormat("int a = (int)+2;"); 6043 verifyFormat("my_int a = (my_int)2.0f;"); 6044 verifyFormat("my_int a = (my_int)sizeof(int);"); 6045 verifyFormat("return (my_int)aaa;"); 6046 verifyFormat("#define x ((int)-1)"); 6047 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6048 verifyFormat("#define p(q) ((int *)&q)"); 6049 verifyFormat("fn(a)(b) + 1;"); 6050 6051 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6052 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6053 verifyFormat("my_int a = (my_int)~0;"); 6054 verifyFormat("my_int a = (my_int)++a;"); 6055 verifyFormat("my_int a = (my_int)-2;"); 6056 verifyFormat("my_int a = (my_int)1;"); 6057 verifyFormat("my_int a = (my_int *)1;"); 6058 verifyFormat("my_int a = (const my_int)-1;"); 6059 verifyFormat("my_int a = (const my_int *)-1;"); 6060 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6061 verifyFormat("my_int a = (ns::my_int)-2;"); 6062 verifyFormat("case (my_int)ONE:"); 6063 verifyFormat("auto x = (X)this;"); 6064 6065 // FIXME: single value wrapped with paren will be treated as cast. 6066 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6067 6068 verifyFormat("{ (void)F; }"); 6069 6070 // Don't break after a cast's 6071 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6072 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6073 " bbbbbbbbbbbbbbbbbbbbbb);"); 6074 6075 // These are not casts. 6076 verifyFormat("void f(int *) {}"); 6077 verifyFormat("f(foo)->b;"); 6078 verifyFormat("f(foo).b;"); 6079 verifyFormat("f(foo)(b);"); 6080 verifyFormat("f(foo)[b];"); 6081 verifyFormat("[](foo) { return 4; }(bar);"); 6082 verifyFormat("(*funptr)(foo)[4];"); 6083 verifyFormat("funptrs[4](foo)[4];"); 6084 verifyFormat("void f(int *);"); 6085 verifyFormat("void f(int *) = 0;"); 6086 verifyFormat("void f(SmallVector<int>) {}"); 6087 verifyFormat("void f(SmallVector<int>);"); 6088 verifyFormat("void f(SmallVector<int>) = 0;"); 6089 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6090 verifyFormat("int a = sizeof(int) * b;"); 6091 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6092 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6093 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6094 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6095 6096 // These are not casts, but at some point were confused with casts. 6097 verifyFormat("virtual void foo(int *) override;"); 6098 verifyFormat("virtual void foo(char &) const;"); 6099 verifyFormat("virtual void foo(int *a, char *) const;"); 6100 verifyFormat("int a = sizeof(int *) + b;"); 6101 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6102 verifyFormat("bool b = f(g<int>) && c;"); 6103 verifyFormat("typedef void (*f)(int i) func;"); 6104 6105 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6106 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6107 // FIXME: The indentation here is not ideal. 6108 verifyFormat( 6109 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6110 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6111 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6112 } 6113 6114 TEST_F(FormatTest, FormatsFunctionTypes) { 6115 verifyFormat("A<bool()> a;"); 6116 verifyFormat("A<SomeType()> a;"); 6117 verifyFormat("A<void (*)(int, std::string)> a;"); 6118 verifyFormat("A<void *(int)>;"); 6119 verifyFormat("void *(*a)(int *, SomeType *);"); 6120 verifyFormat("int (*func)(void *);"); 6121 verifyFormat("void f() { int (*func)(void *); }"); 6122 verifyFormat("template <class CallbackClass>\n" 6123 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6124 6125 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6126 verifyGoogleFormat("void* (*a)(int);"); 6127 verifyGoogleFormat( 6128 "template <class CallbackClass>\n" 6129 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6130 6131 // Other constructs can look somewhat like function types: 6132 verifyFormat("A<sizeof(*x)> a;"); 6133 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6134 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6135 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6136 verifyFormat("int x = f(&h)();"); 6137 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6138 } 6139 6140 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6141 verifyFormat("A (*foo_)[6];"); 6142 verifyFormat("vector<int> (*foo_)[6];"); 6143 } 6144 6145 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6146 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6147 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6148 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6149 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6150 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6151 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6152 6153 // Different ways of ()-initializiation. 6154 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6155 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6156 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6157 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6158 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6159 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6160 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6161 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6162 } 6163 6164 TEST_F(FormatTest, BreaksLongDeclarations) { 6165 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6166 " AnotherNameForTheLongType;"); 6167 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6169 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6170 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6171 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6172 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6173 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6174 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6175 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6176 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6177 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6178 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6179 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6180 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6181 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6182 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6183 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6184 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6185 FormatStyle Indented = getLLVMStyle(); 6186 Indented.IndentWrappedFunctionNames = true; 6187 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6188 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6189 Indented); 6190 verifyFormat( 6191 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6192 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6193 Indented); 6194 verifyFormat( 6195 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6196 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6197 Indented); 6198 verifyFormat( 6199 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6200 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6201 Indented); 6202 6203 // FIXME: Without the comment, this breaks after "(". 6204 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6205 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6206 getGoogleStyle()); 6207 6208 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6209 " int LoooooooooooooooooooongParam2) {}"); 6210 verifyFormat( 6211 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6212 " SourceLocation L, IdentifierIn *II,\n" 6213 " Type *T) {}"); 6214 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6215 "ReallyReaaallyLongFunctionName(\n" 6216 " const std::string &SomeParameter,\n" 6217 " const SomeType<string, SomeOtherTemplateParameter>\n" 6218 " &ReallyReallyLongParameterName,\n" 6219 " const SomeType<string, SomeOtherTemplateParameter>\n" 6220 " &AnotherLongParameterName) {}"); 6221 verifyFormat("template <typename A>\n" 6222 "SomeLoooooooooooooooooooooongType<\n" 6223 " typename some_namespace::SomeOtherType<A>::Type>\n" 6224 "Function() {}"); 6225 6226 verifyGoogleFormat( 6227 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6228 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6229 verifyGoogleFormat( 6230 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6231 " SourceLocation L) {}"); 6232 verifyGoogleFormat( 6233 "some_namespace::LongReturnType\n" 6234 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6235 " int first_long_parameter, int second_parameter) {}"); 6236 6237 verifyGoogleFormat("template <typename T>\n" 6238 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6239 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6240 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6241 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6242 6243 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6244 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6245 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6246 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6247 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6248 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6249 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6250 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6253 } 6254 6255 TEST_F(FormatTest, FormatsArrays) { 6256 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6257 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6258 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6259 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6260 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6261 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6262 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6263 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6265 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6266 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6267 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6268 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6269 verifyFormat( 6270 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6271 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6272 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6273 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6274 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6275 6276 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6278 verifyFormat( 6279 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6280 " .aaaaaaa[0]\n" 6281 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6282 verifyFormat("a[::b::c];"); 6283 6284 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6285 6286 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6287 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6288 } 6289 6290 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6291 verifyFormat("(a)->b();"); 6292 verifyFormat("--a;"); 6293 } 6294 6295 TEST_F(FormatTest, HandlesIncludeDirectives) { 6296 verifyFormat("#include <string>\n" 6297 "#include <a/b/c.h>\n" 6298 "#include \"a/b/string\"\n" 6299 "#include \"string.h\"\n" 6300 "#include \"string.h\"\n" 6301 "#include <a-a>\n" 6302 "#include < path with space >\n" 6303 "#include_next <test.h>" 6304 "#include \"abc.h\" // this is included for ABC\n" 6305 "#include \"some long include\" // with a comment\n" 6306 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6307 getLLVMStyleWithColumns(35)); 6308 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6309 EXPECT_EQ("#include <a>", format("#include<a>")); 6310 6311 verifyFormat("#import <string>"); 6312 verifyFormat("#import <a/b/c.h>"); 6313 verifyFormat("#import \"a/b/string\""); 6314 verifyFormat("#import \"string.h\""); 6315 verifyFormat("#import \"string.h\""); 6316 verifyFormat("#if __has_include(<strstream>)\n" 6317 "#include <strstream>\n" 6318 "#endif"); 6319 6320 verifyFormat("#define MY_IMPORT <a/b>"); 6321 6322 // Protocol buffer definition or missing "#". 6323 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6324 getLLVMStyleWithColumns(30)); 6325 6326 FormatStyle Style = getLLVMStyle(); 6327 Style.AlwaysBreakBeforeMultilineStrings = true; 6328 Style.ColumnLimit = 0; 6329 verifyFormat("#import \"abc.h\"", Style); 6330 6331 // But 'import' might also be a regular C++ namespace. 6332 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6334 } 6335 6336 //===----------------------------------------------------------------------===// 6337 // Error recovery tests. 6338 //===----------------------------------------------------------------------===// 6339 6340 TEST_F(FormatTest, IncompleteParameterLists) { 6341 FormatStyle NoBinPacking = getLLVMStyle(); 6342 NoBinPacking.BinPackParameters = false; 6343 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6344 " double *min_x,\n" 6345 " double *max_x,\n" 6346 " double *min_y,\n" 6347 " double *max_y,\n" 6348 " double *min_z,\n" 6349 " double *max_z, ) {}", 6350 NoBinPacking); 6351 } 6352 6353 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6354 verifyFormat("void f() { return; }\n42"); 6355 verifyFormat("void f() {\n" 6356 " if (0)\n" 6357 " return;\n" 6358 "}\n" 6359 "42"); 6360 verifyFormat("void f() { return }\n42"); 6361 verifyFormat("void f() {\n" 6362 " if (0)\n" 6363 " return\n" 6364 "}\n" 6365 "42"); 6366 } 6367 6368 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6369 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6370 EXPECT_EQ("void f() {\n" 6371 " if (a)\n" 6372 " return\n" 6373 "}", 6374 format("void f ( ) { if ( a ) return }")); 6375 EXPECT_EQ("namespace N {\n" 6376 "void f()\n" 6377 "}", 6378 format("namespace N { void f() }")); 6379 EXPECT_EQ("namespace N {\n" 6380 "void f() {}\n" 6381 "void g()\n" 6382 "}", 6383 format("namespace N { void f( ) { } void g( ) }")); 6384 } 6385 6386 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6387 verifyFormat("int aaaaaaaa =\n" 6388 " // Overlylongcomment\n" 6389 " b;", 6390 getLLVMStyleWithColumns(20)); 6391 verifyFormat("function(\n" 6392 " ShortArgument,\n" 6393 " LoooooooooooongArgument);\n", 6394 getLLVMStyleWithColumns(20)); 6395 } 6396 6397 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6398 verifyFormat("public:"); 6399 verifyFormat("class A {\n" 6400 "public\n" 6401 " void f() {}\n" 6402 "};"); 6403 verifyFormat("public\n" 6404 "int qwerty;"); 6405 verifyFormat("public\n" 6406 "B {}"); 6407 verifyFormat("public\n" 6408 "{}"); 6409 verifyFormat("public\n" 6410 "B { int x; }"); 6411 } 6412 6413 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6414 verifyFormat("{"); 6415 verifyFormat("#})"); 6416 verifyNoCrash("(/**/[:!] ?[)."); 6417 } 6418 6419 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6420 verifyFormat("do {\n}"); 6421 verifyFormat("do {\n}\n" 6422 "f();"); 6423 verifyFormat("do {\n}\n" 6424 "wheeee(fun);"); 6425 verifyFormat("do {\n" 6426 " f();\n" 6427 "}"); 6428 } 6429 6430 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6431 verifyFormat("if {\n foo;\n foo();\n}"); 6432 verifyFormat("switch {\n foo;\n foo();\n}"); 6433 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6434 verifyFormat("while {\n foo;\n foo();\n}"); 6435 verifyFormat("do {\n foo;\n foo();\n} while;"); 6436 } 6437 6438 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6439 verifyIncompleteFormat("namespace {\n" 6440 "class Foo { Foo (\n" 6441 "};\n" 6442 "} // comment"); 6443 } 6444 6445 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6446 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6447 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6448 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6449 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6450 6451 EXPECT_EQ("{\n" 6452 " {\n" 6453 " breakme(\n" 6454 " qwe);\n" 6455 " }\n", 6456 format("{\n" 6457 " {\n" 6458 " breakme(qwe);\n" 6459 "}\n", 6460 getLLVMStyleWithColumns(10))); 6461 } 6462 6463 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6464 verifyFormat("int x = {\n" 6465 " avariable,\n" 6466 " b(alongervariable)};", 6467 getLLVMStyleWithColumns(25)); 6468 } 6469 6470 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6471 verifyFormat("return (a)(b){1, 2, 3};"); 6472 } 6473 6474 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6475 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6476 verifyFormat("vector<int> x{\n" 6477 " 1, 2, 3, 4,\n" 6478 "};"); 6479 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6480 verifyFormat("f({1, 2});"); 6481 verifyFormat("auto v = Foo{-1};"); 6482 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6483 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6484 verifyFormat("new vector<int>{1, 2, 3};"); 6485 verifyFormat("new int[3]{1, 2, 3};"); 6486 verifyFormat("new int{1};"); 6487 verifyFormat("return {arg1, arg2};"); 6488 verifyFormat("return {arg1, SomeType{parameter}};"); 6489 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6490 verifyFormat("new T{arg1, arg2};"); 6491 verifyFormat("f(MyMap[{composite, key}]);"); 6492 verifyFormat("class Class {\n" 6493 " T member = {arg1, arg2};\n" 6494 "};"); 6495 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6496 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6497 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6498 6499 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6500 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6501 verifyFormat("auto i = decltype(x){};"); 6502 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6503 verifyFormat("Node n{1, Node{1000}, //\n" 6504 " 2};"); 6505 verifyFormat("Aaaa aaaaaaa{\n" 6506 " {\n" 6507 " aaaa,\n" 6508 " },\n" 6509 "};"); 6510 verifyFormat("class C : public D {\n" 6511 " SomeClass SC{2};\n" 6512 "};"); 6513 verifyFormat("class C : public A {\n" 6514 " class D : public B {\n" 6515 " void f() { int i{2}; }\n" 6516 " };\n" 6517 "};"); 6518 verifyFormat("#define A {a, a},"); 6519 6520 // Cases where distinguising braced lists and blocks is hard. 6521 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6522 verifyFormat("void f() {\n" 6523 " return; // comment\n" 6524 "}\n" 6525 "SomeType t;"); 6526 verifyFormat("void f() {\n" 6527 " if (a) {\n" 6528 " f();\n" 6529 " }\n" 6530 "}\n" 6531 "SomeType t;"); 6532 6533 // In combination with BinPackArguments = false. 6534 FormatStyle NoBinPacking = getLLVMStyle(); 6535 NoBinPacking.BinPackArguments = false; 6536 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6537 " bbbbb,\n" 6538 " ccccc,\n" 6539 " ddddd,\n" 6540 " eeeee,\n" 6541 " ffffff,\n" 6542 " ggggg,\n" 6543 " hhhhhh,\n" 6544 " iiiiii,\n" 6545 " jjjjjj,\n" 6546 " kkkkkk};", 6547 NoBinPacking); 6548 verifyFormat("const Aaaaaa aaaaa = {\n" 6549 " aaaaa,\n" 6550 " bbbbb,\n" 6551 " ccccc,\n" 6552 " ddddd,\n" 6553 " eeeee,\n" 6554 " ffffff,\n" 6555 " ggggg,\n" 6556 " hhhhhh,\n" 6557 " iiiiii,\n" 6558 " jjjjjj,\n" 6559 " kkkkkk,\n" 6560 "};", 6561 NoBinPacking); 6562 verifyFormat( 6563 "const Aaaaaa aaaaa = {\n" 6564 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6565 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6566 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6567 "};", 6568 NoBinPacking); 6569 6570 // FIXME: The alignment of these trailing comments might be bad. Then again, 6571 // this might be utterly useless in real code. 6572 verifyFormat("Constructor::Constructor()\n" 6573 " : some_value{ //\n" 6574 " aaaaaaa, //\n" 6575 " bbbbbbb} {}"); 6576 6577 // In braced lists, the first comment is always assumed to belong to the 6578 // first element. Thus, it can be moved to the next or previous line as 6579 // appropriate. 6580 EXPECT_EQ("function({// First element:\n" 6581 " 1,\n" 6582 " // Second element:\n" 6583 " 2});", 6584 format("function({\n" 6585 " // First element:\n" 6586 " 1,\n" 6587 " // Second element:\n" 6588 " 2});")); 6589 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6590 " // First element:\n" 6591 " 1,\n" 6592 " // Second element:\n" 6593 " 2};", 6594 format("std::vector<int> MyNumbers{// First element:\n" 6595 " 1,\n" 6596 " // Second element:\n" 6597 " 2};", 6598 getLLVMStyleWithColumns(30))); 6599 // A trailing comma should still lead to an enforced line break. 6600 EXPECT_EQ("vector<int> SomeVector = {\n" 6601 " // aaa\n" 6602 " 1, 2,\n" 6603 "};", 6604 format("vector<int> SomeVector = { // aaa\n" 6605 " 1, 2, };")); 6606 6607 FormatStyle ExtraSpaces = getLLVMStyle(); 6608 ExtraSpaces.Cpp11BracedListStyle = false; 6609 ExtraSpaces.ColumnLimit = 75; 6610 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6611 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6612 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6613 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6614 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6615 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6616 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6617 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6618 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6619 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6620 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6621 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6622 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6623 verifyFormat("class Class {\n" 6624 " T member = { arg1, arg2 };\n" 6625 "};", 6626 ExtraSpaces); 6627 verifyFormat( 6628 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6629 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6630 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6631 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6632 ExtraSpaces); 6633 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6634 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6635 ExtraSpaces); 6636 verifyFormat( 6637 "someFunction(OtherParam,\n" 6638 " BracedList{ // comment 1 (Forcing interesting break)\n" 6639 " param1, param2,\n" 6640 " // comment 2\n" 6641 " param3, param4 });", 6642 ExtraSpaces); 6643 verifyFormat( 6644 "std::this_thread::sleep_for(\n" 6645 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6646 ExtraSpaces); 6647 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6648 " aaaaaaa,\n" 6649 " aaaaaaaaaa,\n" 6650 " aaaaa,\n" 6651 " aaaaaaaaaaaaaaa,\n" 6652 " aaa,\n" 6653 " aaaaaaaaaa,\n" 6654 " a,\n" 6655 " aaaaaaaaaaaaaaaaaaaaa,\n" 6656 " aaaaaaaaaaaa,\n" 6657 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6658 " aaaaaaa,\n" 6659 " a};"); 6660 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6661 } 6662 6663 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6664 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6665 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6666 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6667 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6668 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6669 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6670 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6671 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6672 " 1, 22, 333, 4444, 55555, //\n" 6673 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6674 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6675 verifyFormat( 6676 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6677 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6678 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6679 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6680 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6681 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6682 " 7777777};"); 6683 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6684 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6685 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6686 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6687 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6688 " // Separating comment.\n" 6689 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6690 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6691 " // Leading comment\n" 6692 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6693 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6694 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6695 " 1, 1, 1, 1};", 6696 getLLVMStyleWithColumns(39)); 6697 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6698 " 1, 1, 1, 1};", 6699 getLLVMStyleWithColumns(38)); 6700 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6701 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6702 getLLVMStyleWithColumns(43)); 6703 verifyFormat( 6704 "static unsigned SomeValues[10][3] = {\n" 6705 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6706 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6707 verifyFormat("static auto fields = new vector<string>{\n" 6708 " \"aaaaaaaaaaaaa\",\n" 6709 " \"aaaaaaaaaaaaa\",\n" 6710 " \"aaaaaaaaaaaa\",\n" 6711 " \"aaaaaaaaaaaaaa\",\n" 6712 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6713 " \"aaaaaaaaaaaa\",\n" 6714 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6715 "};"); 6716 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6717 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6718 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6719 " 3, cccccccccccccccccccccc};", 6720 getLLVMStyleWithColumns(60)); 6721 6722 // Trailing commas. 6723 verifyFormat("vector<int> x = {\n" 6724 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6725 "};", 6726 getLLVMStyleWithColumns(39)); 6727 verifyFormat("vector<int> x = {\n" 6728 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6729 "};", 6730 getLLVMStyleWithColumns(39)); 6731 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6732 " 1, 1, 1, 1,\n" 6733 " /**/ /**/};", 6734 getLLVMStyleWithColumns(39)); 6735 6736 // Trailing comment in the first line. 6737 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6738 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6739 " 111111111, 222222222, 3333333333, 444444444, //\n" 6740 " 11111111, 22222222, 333333333, 44444444};"); 6741 // Trailing comment in the last line. 6742 verifyFormat("int aaaaa[] = {\n" 6743 " 1, 2, 3, // comment\n" 6744 " 4, 5, 6 // comment\n" 6745 "};"); 6746 6747 // With nested lists, we should either format one item per line or all nested 6748 // lists one on line. 6749 // FIXME: For some nested lists, we can do better. 6750 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6751 " {aaaaaaaaaaaaaaaaaaa},\n" 6752 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6753 " {aaaaaaaaaaaaaaaaa}};", 6754 getLLVMStyleWithColumns(60)); 6755 verifyFormat( 6756 "SomeStruct my_struct_array = {\n" 6757 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6758 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6759 " {aaa, aaa},\n" 6760 " {aaa, aaa},\n" 6761 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6762 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6763 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6764 6765 // No column layout should be used here. 6766 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6767 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6768 6769 verifyNoCrash("a<,"); 6770 6771 // No braced initializer here. 6772 verifyFormat("void f() {\n" 6773 " struct Dummy {};\n" 6774 " f(v);\n" 6775 "}"); 6776 6777 // Long lists should be formatted in columns even if they are nested. 6778 verifyFormat( 6779 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6780 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6781 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6782 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6783 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6784 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6785 6786 // Allow "single-column" layout even if that violates the column limit. There 6787 // isn't going to be a better way. 6788 verifyFormat("std::vector<int> a = {\n" 6789 " aaaaaaaa,\n" 6790 " aaaaaaaa,\n" 6791 " aaaaaaaa,\n" 6792 " aaaaaaaa,\n" 6793 " aaaaaaaaaa,\n" 6794 " aaaaaaaa,\n" 6795 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6796 getLLVMStyleWithColumns(30)); 6797 } 6798 6799 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6800 FormatStyle DoNotMerge = getLLVMStyle(); 6801 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6802 6803 verifyFormat("void f() { return 42; }"); 6804 verifyFormat("void f() {\n" 6805 " return 42;\n" 6806 "}", 6807 DoNotMerge); 6808 verifyFormat("void f() {\n" 6809 " // Comment\n" 6810 "}"); 6811 verifyFormat("{\n" 6812 "#error {\n" 6813 " int a;\n" 6814 "}"); 6815 verifyFormat("{\n" 6816 " int a;\n" 6817 "#error {\n" 6818 "}"); 6819 verifyFormat("void f() {} // comment"); 6820 verifyFormat("void f() { int a; } // comment"); 6821 verifyFormat("void f() {\n" 6822 "} // comment", 6823 DoNotMerge); 6824 verifyFormat("void f() {\n" 6825 " int a;\n" 6826 "} // comment", 6827 DoNotMerge); 6828 verifyFormat("void f() {\n" 6829 "} // comment", 6830 getLLVMStyleWithColumns(15)); 6831 6832 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6833 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6834 6835 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6836 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6837 verifyFormat("class C {\n" 6838 " C()\n" 6839 " : iiiiiiii(nullptr),\n" 6840 " kkkkkkk(nullptr),\n" 6841 " mmmmmmm(nullptr),\n" 6842 " nnnnnnn(nullptr) {}\n" 6843 "};", 6844 getGoogleStyle()); 6845 6846 FormatStyle NoColumnLimit = getLLVMStyle(); 6847 NoColumnLimit.ColumnLimit = 0; 6848 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6849 EXPECT_EQ("class C {\n" 6850 " A() : b(0) {}\n" 6851 "};", 6852 format("class C{A():b(0){}};", NoColumnLimit)); 6853 EXPECT_EQ("A()\n" 6854 " : b(0) {\n" 6855 "}", 6856 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6857 6858 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6859 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6860 FormatStyle::SFS_None; 6861 EXPECT_EQ("A()\n" 6862 " : b(0) {\n" 6863 "}", 6864 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6865 EXPECT_EQ("A()\n" 6866 " : b(0) {\n" 6867 "}", 6868 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6869 6870 verifyFormat("#define A \\\n" 6871 " void f() { \\\n" 6872 " int i; \\\n" 6873 " }", 6874 getLLVMStyleWithColumns(20)); 6875 verifyFormat("#define A \\\n" 6876 " void f() { int i; }", 6877 getLLVMStyleWithColumns(21)); 6878 verifyFormat("#define A \\\n" 6879 " void f() { \\\n" 6880 " int i; \\\n" 6881 " } \\\n" 6882 " int j;", 6883 getLLVMStyleWithColumns(22)); 6884 verifyFormat("#define A \\\n" 6885 " void f() { int i; } \\\n" 6886 " int j;", 6887 getLLVMStyleWithColumns(23)); 6888 } 6889 6890 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6891 FormatStyle MergeInlineOnly = getLLVMStyle(); 6892 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6893 verifyFormat("class C {\n" 6894 " int f() { return 42; }\n" 6895 "};", 6896 MergeInlineOnly); 6897 verifyFormat("int f() {\n" 6898 " return 42;\n" 6899 "}", 6900 MergeInlineOnly); 6901 } 6902 6903 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6904 // Elaborate type variable declarations. 6905 verifyFormat("struct foo a = {bar};\nint n;"); 6906 verifyFormat("class foo a = {bar};\nint n;"); 6907 verifyFormat("union foo a = {bar};\nint n;"); 6908 6909 // Elaborate types inside function definitions. 6910 verifyFormat("struct foo f() {}\nint n;"); 6911 verifyFormat("class foo f() {}\nint n;"); 6912 verifyFormat("union foo f() {}\nint n;"); 6913 6914 // Templates. 6915 verifyFormat("template <class X> void f() {}\nint n;"); 6916 verifyFormat("template <struct X> void f() {}\nint n;"); 6917 verifyFormat("template <union X> void f() {}\nint n;"); 6918 6919 // Actual definitions... 6920 verifyFormat("struct {\n} n;"); 6921 verifyFormat( 6922 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6923 verifyFormat("union Z {\n int n;\n} x;"); 6924 verifyFormat("class MACRO Z {\n} n;"); 6925 verifyFormat("class MACRO(X) Z {\n} n;"); 6926 verifyFormat("class __attribute__(X) Z {\n} n;"); 6927 verifyFormat("class __declspec(X) Z {\n} n;"); 6928 verifyFormat("class A##B##C {\n} n;"); 6929 verifyFormat("class alignas(16) Z {\n} n;"); 6930 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6931 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6932 6933 // Redefinition from nested context: 6934 verifyFormat("class A::B::C {\n} n;"); 6935 6936 // Template definitions. 6937 verifyFormat( 6938 "template <typename F>\n" 6939 "Matcher(const Matcher<F> &Other,\n" 6940 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6941 " !is_same<F, T>::value>::type * = 0)\n" 6942 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6943 6944 // FIXME: This is still incorrectly handled at the formatter side. 6945 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6946 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6947 6948 // FIXME: 6949 // This now gets parsed incorrectly as class definition. 6950 // verifyFormat("class A<int> f() {\n}\nint n;"); 6951 6952 // Elaborate types where incorrectly parsing the structural element would 6953 // break the indent. 6954 verifyFormat("if (true)\n" 6955 " class X x;\n" 6956 "else\n" 6957 " f();\n"); 6958 6959 // This is simply incomplete. Formatting is not important, but must not crash. 6960 verifyFormat("class A:"); 6961 } 6962 6963 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6964 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6965 format("#error Leave all white!!!!! space* alone!\n")); 6966 EXPECT_EQ( 6967 "#warning Leave all white!!!!! space* alone!\n", 6968 format("#warning Leave all white!!!!! space* alone!\n")); 6969 EXPECT_EQ("#error 1", format(" # error 1")); 6970 EXPECT_EQ("#warning 1", format(" # warning 1")); 6971 } 6972 6973 TEST_F(FormatTest, FormatHashIfExpressions) { 6974 verifyFormat("#if AAAA && BBBB"); 6975 verifyFormat("#if (AAAA && BBBB)"); 6976 verifyFormat("#elif (AAAA && BBBB)"); 6977 // FIXME: Come up with a better indentation for #elif. 6978 verifyFormat( 6979 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6980 " defined(BBBBBBBB)\n" 6981 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6982 " defined(BBBBBBBB)\n" 6983 "#endif", 6984 getLLVMStyleWithColumns(65)); 6985 } 6986 6987 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6988 FormatStyle AllowsMergedIf = getGoogleStyle(); 6989 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6990 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6991 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6992 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6993 EXPECT_EQ("if (true) return 42;", 6994 format("if (true)\nreturn 42;", AllowsMergedIf)); 6995 FormatStyle ShortMergedIf = AllowsMergedIf; 6996 ShortMergedIf.ColumnLimit = 25; 6997 verifyFormat("#define A \\\n" 6998 " if (true) return 42;", 6999 ShortMergedIf); 7000 verifyFormat("#define A \\\n" 7001 " f(); \\\n" 7002 " if (true)\n" 7003 "#define B", 7004 ShortMergedIf); 7005 verifyFormat("#define A \\\n" 7006 " f(); \\\n" 7007 " if (true)\n" 7008 "g();", 7009 ShortMergedIf); 7010 verifyFormat("{\n" 7011 "#ifdef A\n" 7012 " // Comment\n" 7013 " if (true) continue;\n" 7014 "#endif\n" 7015 " // Comment\n" 7016 " if (true) continue;\n" 7017 "}", 7018 ShortMergedIf); 7019 ShortMergedIf.ColumnLimit = 29; 7020 verifyFormat("#define A \\\n" 7021 " if (aaaaaaaaaa) return 1; \\\n" 7022 " return 2;", 7023 ShortMergedIf); 7024 ShortMergedIf.ColumnLimit = 28; 7025 verifyFormat("#define A \\\n" 7026 " if (aaaaaaaaaa) \\\n" 7027 " return 1; \\\n" 7028 " return 2;", 7029 ShortMergedIf); 7030 } 7031 7032 TEST_F(FormatTest, BlockCommentsInControlLoops) { 7033 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7034 " f();\n" 7035 "}"); 7036 verifyFormat("if (0) /* a comment in a strange place */ {\n" 7037 " f();\n" 7038 "} /* another comment */ else /* comment #3 */ {\n" 7039 " g();\n" 7040 "}"); 7041 verifyFormat("while (0) /* a comment in a strange place */ {\n" 7042 " f();\n" 7043 "}"); 7044 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 7045 " f();\n" 7046 "}"); 7047 verifyFormat("do /* a comment in a strange place */ {\n" 7048 " f();\n" 7049 "} /* another comment */ while (0);"); 7050 } 7051 7052 TEST_F(FormatTest, BlockComments) { 7053 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 7054 format("/* *//* */ /* */\n/* *//* */ /* */")); 7055 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 7056 EXPECT_EQ("#define A /*123*/ \\\n" 7057 " b\n" 7058 "/* */\n" 7059 "someCall(\n" 7060 " parameter);", 7061 format("#define A /*123*/ b\n" 7062 "/* */\n" 7063 "someCall(parameter);", 7064 getLLVMStyleWithColumns(15))); 7065 7066 EXPECT_EQ("#define A\n" 7067 "/* */ someCall(\n" 7068 " parameter);", 7069 format("#define A\n" 7070 "/* */someCall(parameter);", 7071 getLLVMStyleWithColumns(15))); 7072 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7073 EXPECT_EQ("/*\n" 7074 "*\n" 7075 " * aaaaaa\n" 7076 " * aaaaaa\n" 7077 "*/", 7078 format("/*\n" 7079 "*\n" 7080 " * aaaaaa aaaaaa\n" 7081 "*/", 7082 getLLVMStyleWithColumns(10))); 7083 EXPECT_EQ("/*\n" 7084 "**\n" 7085 "* aaaaaa\n" 7086 "*aaaaaa\n" 7087 "*/", 7088 format("/*\n" 7089 "**\n" 7090 "* aaaaaa aaaaaa\n" 7091 "*/", 7092 getLLVMStyleWithColumns(10))); 7093 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7094 " /* line 1\n" 7095 " bbbbbbbbbbbb */\n" 7096 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7097 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7098 " /* line 1\n" 7099 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7100 getLLVMStyleWithColumns(50))); 7101 7102 FormatStyle NoBinPacking = getLLVMStyle(); 7103 NoBinPacking.BinPackParameters = false; 7104 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7105 " 2, /* comment 2 */\n" 7106 " 3, /* comment 3 */\n" 7107 " aaaa,\n" 7108 " bbbb);", 7109 format("someFunction (1, /* comment 1 */\n" 7110 " 2, /* comment 2 */ \n" 7111 " 3, /* comment 3 */\n" 7112 "aaaa, bbbb );", 7113 NoBinPacking)); 7114 verifyFormat( 7115 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7117 EXPECT_EQ( 7118 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7119 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7121 format( 7122 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7123 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7125 EXPECT_EQ( 7126 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7127 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7128 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7129 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7130 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7131 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7132 7133 verifyFormat("void f(int * /* unused */) {}"); 7134 7135 EXPECT_EQ("/*\n" 7136 " **\n" 7137 " */", 7138 format("/*\n" 7139 " **\n" 7140 " */")); 7141 EXPECT_EQ("/*\n" 7142 " *q\n" 7143 " */", 7144 format("/*\n" 7145 " *q\n" 7146 " */")); 7147 EXPECT_EQ("/*\n" 7148 " * q\n" 7149 " */", 7150 format("/*\n" 7151 " * q\n" 7152 " */")); 7153 EXPECT_EQ("/*\n" 7154 " **/", 7155 format("/*\n" 7156 " **/")); 7157 EXPECT_EQ("/*\n" 7158 " ***/", 7159 format("/*\n" 7160 " ***/")); 7161 } 7162 7163 TEST_F(FormatTest, BlockCommentsInMacros) { 7164 EXPECT_EQ("#define A \\\n" 7165 " { \\\n" 7166 " /* one line */ \\\n" 7167 " someCall();", 7168 format("#define A { \\\n" 7169 " /* one line */ \\\n" 7170 " someCall();", 7171 getLLVMStyleWithColumns(20))); 7172 EXPECT_EQ("#define A \\\n" 7173 " { \\\n" 7174 " /* previous */ \\\n" 7175 " /* one line */ \\\n" 7176 " someCall();", 7177 format("#define A { \\\n" 7178 " /* previous */ \\\n" 7179 " /* one line */ \\\n" 7180 " someCall();", 7181 getLLVMStyleWithColumns(20))); 7182 } 7183 7184 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7185 EXPECT_EQ("a = {\n" 7186 " 1111 /* */\n" 7187 "};", 7188 format("a = {1111 /* */\n" 7189 "};", 7190 getLLVMStyleWithColumns(15))); 7191 EXPECT_EQ("a = {\n" 7192 " 1111 /* */\n" 7193 "};", 7194 format("a = {1111 /* */\n" 7195 "};", 7196 getLLVMStyleWithColumns(15))); 7197 7198 // FIXME: The formatting is still wrong here. 7199 EXPECT_EQ("a = {\n" 7200 " 1111 /* a\n" 7201 " */\n" 7202 "};", 7203 format("a = {1111 /* a */\n" 7204 "};", 7205 getLLVMStyleWithColumns(15))); 7206 } 7207 7208 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7209 verifyFormat("{\n" 7210 " // a\n" 7211 " // b"); 7212 } 7213 7214 TEST_F(FormatTest, FormatStarDependingOnContext) { 7215 verifyFormat("void f(int *a);"); 7216 verifyFormat("void f() { f(fint * b); }"); 7217 verifyFormat("class A {\n void f(int *a);\n};"); 7218 verifyFormat("class A {\n int *a;\n};"); 7219 verifyFormat("namespace a {\n" 7220 "namespace b {\n" 7221 "class A {\n" 7222 " void f() {}\n" 7223 " int *a;\n" 7224 "};\n" 7225 "}\n" 7226 "}"); 7227 } 7228 7229 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7230 verifyFormat("while"); 7231 verifyFormat("operator"); 7232 } 7233 7234 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7235 // This code would be painfully slow to format if we didn't skip it. 7236 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 7237 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7238 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7239 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7240 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7241 "A(1, 1)\n" 7242 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7243 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7244 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7245 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7246 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7247 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7248 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7249 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7250 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7251 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7252 // Deeply nested part is untouched, rest is formatted. 7253 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7254 format(std::string("int i;\n") + Code + "int j;\n", 7255 getLLVMStyle(), IC_ExpectIncomplete)); 7256 } 7257 7258 //===----------------------------------------------------------------------===// 7259 // Objective-C tests. 7260 //===----------------------------------------------------------------------===// 7261 7262 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7263 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7264 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7265 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7266 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7267 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7268 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7269 format("-(NSInteger)Method3:(id)anObject;")); 7270 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7271 format("-(NSInteger)Method4:(id)anObject;")); 7272 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7273 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7274 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7275 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7276 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7277 "forAllCells:(BOOL)flag;", 7278 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7279 "forAllCells:(BOOL)flag;")); 7280 7281 // Very long objectiveC method declaration. 7282 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7283 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7284 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7285 " inRange:(NSRange)range\n" 7286 " outRange:(NSRange)out_range\n" 7287 " outRange1:(NSRange)out_range1\n" 7288 " outRange2:(NSRange)out_range2\n" 7289 " outRange3:(NSRange)out_range3\n" 7290 " outRange4:(NSRange)out_range4\n" 7291 " outRange5:(NSRange)out_range5\n" 7292 " outRange6:(NSRange)out_range6\n" 7293 " outRange7:(NSRange)out_range7\n" 7294 " outRange8:(NSRange)out_range8\n" 7295 " outRange9:(NSRange)out_range9;"); 7296 7297 // When the function name has to be wrapped. 7298 FormatStyle Style = getLLVMStyle(); 7299 Style.IndentWrappedFunctionNames = false; 7300 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7301 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7302 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7303 "}", 7304 Style); 7305 Style.IndentWrappedFunctionNames = true; 7306 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7307 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7308 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7309 "}", 7310 Style); 7311 7312 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7313 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7314 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7315 // protocol lists (but not for template classes): 7316 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7317 7318 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7319 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7320 7321 // If there's no return type (very rare in practice!), LLVM and Google style 7322 // agree. 7323 verifyFormat("- foo;"); 7324 verifyFormat("- foo:(int)f;"); 7325 verifyGoogleFormat("- foo:(int)foo;"); 7326 } 7327 7328 7329 TEST_F(FormatTest, BreaksStringLiterals) { 7330 EXPECT_EQ("\"some text \"\n" 7331 "\"other\";", 7332 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7333 EXPECT_EQ("\"some text \"\n" 7334 "\"other\";", 7335 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7336 EXPECT_EQ( 7337 "#define A \\\n" 7338 " \"some \" \\\n" 7339 " \"text \" \\\n" 7340 " \"other\";", 7341 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7342 EXPECT_EQ( 7343 "#define A \\\n" 7344 " \"so \" \\\n" 7345 " \"text \" \\\n" 7346 " \"other\";", 7347 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7348 7349 EXPECT_EQ("\"some text\"", 7350 format("\"some text\"", getLLVMStyleWithColumns(1))); 7351 EXPECT_EQ("\"some text\"", 7352 format("\"some text\"", getLLVMStyleWithColumns(11))); 7353 EXPECT_EQ("\"some \"\n" 7354 "\"text\"", 7355 format("\"some text\"", getLLVMStyleWithColumns(10))); 7356 EXPECT_EQ("\"some \"\n" 7357 "\"text\"", 7358 format("\"some text\"", getLLVMStyleWithColumns(7))); 7359 EXPECT_EQ("\"some\"\n" 7360 "\" tex\"\n" 7361 "\"t\"", 7362 format("\"some text\"", getLLVMStyleWithColumns(6))); 7363 EXPECT_EQ("\"some\"\n" 7364 "\" tex\"\n" 7365 "\" and\"", 7366 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7367 EXPECT_EQ("\"some\"\n" 7368 "\"/tex\"\n" 7369 "\"/and\"", 7370 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7371 7372 EXPECT_EQ("variable =\n" 7373 " \"long string \"\n" 7374 " \"literal\";", 7375 format("variable = \"long string literal\";", 7376 getLLVMStyleWithColumns(20))); 7377 7378 EXPECT_EQ("variable = f(\n" 7379 " \"long string \"\n" 7380 " \"literal\",\n" 7381 " short,\n" 7382 " loooooooooooooooooooong);", 7383 format("variable = f(\"long string literal\", short, " 7384 "loooooooooooooooooooong);", 7385 getLLVMStyleWithColumns(20))); 7386 7387 EXPECT_EQ( 7388 "f(g(\"long string \"\n" 7389 " \"literal\"),\n" 7390 " b);", 7391 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7392 EXPECT_EQ("f(g(\"long string \"\n" 7393 " \"literal\",\n" 7394 " a),\n" 7395 " b);", 7396 format("f(g(\"long string literal\", a), b);", 7397 getLLVMStyleWithColumns(20))); 7398 EXPECT_EQ( 7399 "f(\"one two\".split(\n" 7400 " variable));", 7401 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7402 EXPECT_EQ("f(\"one two three four five six \"\n" 7403 " \"seven\".split(\n" 7404 " really_looooong_variable));", 7405 format("f(\"one two three four five six seven\"." 7406 "split(really_looooong_variable));", 7407 getLLVMStyleWithColumns(33))); 7408 7409 EXPECT_EQ("f(\"some \"\n" 7410 " \"text\",\n" 7411 " other);", 7412 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7413 7414 // Only break as a last resort. 7415 verifyFormat( 7416 "aaaaaaaaaaaaaaaaaaaa(\n" 7417 " aaaaaaaaaaaaaaaaaaaa,\n" 7418 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7419 7420 EXPECT_EQ("\"splitmea\"\n" 7421 "\"trandomp\"\n" 7422 "\"oint\"", 7423 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7424 7425 EXPECT_EQ("\"split/\"\n" 7426 "\"pathat/\"\n" 7427 "\"slashes\"", 7428 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7429 7430 EXPECT_EQ("\"split/\"\n" 7431 "\"pathat/\"\n" 7432 "\"slashes\"", 7433 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7434 EXPECT_EQ("\"split at \"\n" 7435 "\"spaces/at/\"\n" 7436 "\"slashes.at.any$\"\n" 7437 "\"non-alphanumeric%\"\n" 7438 "\"1111111111characte\"\n" 7439 "\"rs\"", 7440 format("\"split at " 7441 "spaces/at/" 7442 "slashes.at." 7443 "any$non-" 7444 "alphanumeric%" 7445 "1111111111characte" 7446 "rs\"", 7447 getLLVMStyleWithColumns(20))); 7448 7449 // Verify that splitting the strings understands 7450 // Style::AlwaysBreakBeforeMultilineStrings. 7451 EXPECT_EQ( 7452 "aaaaaaaaaaaa(\n" 7453 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7454 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7455 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7456 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7457 "aaaaaaaaaaaaaaaaaaaaaa\");", 7458 getGoogleStyle())); 7459 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7460 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7461 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7462 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7463 "aaaaaaaaaaaaaaaaaaaaaa\";", 7464 getGoogleStyle())); 7465 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7466 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7467 format("llvm::outs() << " 7468 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7469 "aaaaaaaaaaaaaaaaaaa\";")); 7470 EXPECT_EQ("ffff(\n" 7471 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7472 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7473 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7474 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7475 getGoogleStyle())); 7476 7477 FormatStyle Style = getLLVMStyleWithColumns(12); 7478 Style.BreakStringLiterals = false; 7479 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7480 7481 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7482 AlignLeft.AlignEscapedNewlinesLeft = true; 7483 EXPECT_EQ("#define A \\\n" 7484 " \"some \" \\\n" 7485 " \"text \" \\\n" 7486 " \"other\";", 7487 format("#define A \"some text other\";", AlignLeft)); 7488 } 7489 7490 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7491 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7492 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7493 EXPECT_EQ("int i = a(b());", 7494 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7495 } 7496 7497 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7498 EXPECT_EQ( 7499 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7500 "(\n" 7501 " \"x\t\");", 7502 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7503 "aaaaaaa(" 7504 "\"x\t\");")); 7505 } 7506 7507 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7508 EXPECT_EQ( 7509 "u8\"utf8 string \"\n" 7510 "u8\"literal\";", 7511 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7512 EXPECT_EQ( 7513 "u\"utf16 string \"\n" 7514 "u\"literal\";", 7515 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7516 EXPECT_EQ( 7517 "U\"utf32 string \"\n" 7518 "U\"literal\";", 7519 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7520 EXPECT_EQ("L\"wide string \"\n" 7521 "L\"literal\";", 7522 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7523 EXPECT_EQ("@\"NSString \"\n" 7524 "@\"literal\";", 7525 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7526 7527 // This input makes clang-format try to split the incomplete unicode escape 7528 // sequence, which used to lead to a crasher. 7529 verifyNoCrash( 7530 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7531 getLLVMStyleWithColumns(60)); 7532 } 7533 7534 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7535 FormatStyle Style = getGoogleStyleWithColumns(15); 7536 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7537 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7538 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7539 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7540 EXPECT_EQ("u8R\"x(raw literal)x\";", 7541 format("u8R\"x(raw literal)x\";", Style)); 7542 } 7543 7544 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7545 FormatStyle Style = getLLVMStyleWithColumns(20); 7546 EXPECT_EQ( 7547 "_T(\"aaaaaaaaaaaaaa\")\n" 7548 "_T(\"aaaaaaaaaaaaaa\")\n" 7549 "_T(\"aaaaaaaaaaaa\")", 7550 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7551 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 7552 " _T(\"aaaaaa\"),\n" 7553 " z);", 7554 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7555 7556 // FIXME: Handle embedded spaces in one iteration. 7557 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7558 // "_T(\"aaaaaaaaaaaaa\")\n" 7559 // "_T(\"aaaaaaaaaaaaa\")\n" 7560 // "_T(\"a\")", 7561 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7562 // getLLVMStyleWithColumns(20))); 7563 EXPECT_EQ( 7564 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7565 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7566 EXPECT_EQ("f(\n" 7567 "#if !TEST\n" 7568 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7569 "#endif\n" 7570 " );", 7571 format("f(\n" 7572 "#if !TEST\n" 7573 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7574 "#endif\n" 7575 ");")); 7576 EXPECT_EQ("f(\n" 7577 "\n" 7578 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7579 format("f(\n" 7580 "\n" 7581 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7582 } 7583 7584 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7585 EXPECT_EQ( 7586 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7589 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7592 } 7593 7594 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7595 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7596 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7597 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7598 "multiline raw string literal xxxxxxxxxxxxxx\n" 7599 ")x\",\n" 7600 " a),\n" 7601 " b);", 7602 format("fffffffffff(g(R\"x(\n" 7603 "multiline raw string literal xxxxxxxxxxxxxx\n" 7604 ")x\", a), b);", 7605 getGoogleStyleWithColumns(20))); 7606 EXPECT_EQ("fffffffffff(\n" 7607 " g(R\"x(qqq\n" 7608 "multiline raw string literal xxxxxxxxxxxxxx\n" 7609 ")x\",\n" 7610 " a),\n" 7611 " b);", 7612 format("fffffffffff(g(R\"x(qqq\n" 7613 "multiline raw string literal xxxxxxxxxxxxxx\n" 7614 ")x\", a), b);", 7615 getGoogleStyleWithColumns(20))); 7616 7617 EXPECT_EQ("fffffffffff(R\"x(\n" 7618 "multiline raw string literal xxxxxxxxxxxxxx\n" 7619 ")x\");", 7620 format("fffffffffff(R\"x(\n" 7621 "multiline raw string literal xxxxxxxxxxxxxx\n" 7622 ")x\");", 7623 getGoogleStyleWithColumns(20))); 7624 EXPECT_EQ("fffffffffff(R\"x(\n" 7625 "multiline raw string literal xxxxxxxxxxxxxx\n" 7626 ")x\" + bbbbbb);", 7627 format("fffffffffff(R\"x(\n" 7628 "multiline raw string literal xxxxxxxxxxxxxx\n" 7629 ")x\" + bbbbbb);", 7630 getGoogleStyleWithColumns(20))); 7631 EXPECT_EQ("fffffffffff(\n" 7632 " R\"x(\n" 7633 "multiline raw string literal xxxxxxxxxxxxxx\n" 7634 ")x\" +\n" 7635 " bbbbbb);", 7636 format("fffffffffff(\n" 7637 " R\"x(\n" 7638 "multiline raw string literal xxxxxxxxxxxxxx\n" 7639 ")x\" + bbbbbb);", 7640 getGoogleStyleWithColumns(20))); 7641 } 7642 7643 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7644 verifyFormat("string a = \"unterminated;"); 7645 EXPECT_EQ("function(\"unterminated,\n" 7646 " OtherParameter);", 7647 format("function( \"unterminated,\n" 7648 " OtherParameter);")); 7649 } 7650 7651 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7652 FormatStyle Style = getLLVMStyle(); 7653 Style.Standard = FormatStyle::LS_Cpp03; 7654 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7655 format("#define x(_a) printf(\"foo\"_a);", Style)); 7656 } 7657 7658 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7659 7660 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7661 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7662 " \"ddeeefff\");", 7663 format("someFunction(\"aaabbbcccdddeeefff\");", 7664 getLLVMStyleWithColumns(25))); 7665 EXPECT_EQ("someFunction1234567890(\n" 7666 " \"aaabbbcccdddeeefff\");", 7667 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7668 getLLVMStyleWithColumns(26))); 7669 EXPECT_EQ("someFunction1234567890(\n" 7670 " \"aaabbbcccdddeeeff\"\n" 7671 " \"f\");", 7672 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7673 getLLVMStyleWithColumns(25))); 7674 EXPECT_EQ("someFunction1234567890(\n" 7675 " \"aaabbbcccdddeeeff\"\n" 7676 " \"f\");", 7677 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7678 getLLVMStyleWithColumns(24))); 7679 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7680 " \"ddde \"\n" 7681 " \"efff\");", 7682 format("someFunction(\"aaabbbcc ddde efff\");", 7683 getLLVMStyleWithColumns(25))); 7684 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7685 " \"ddeeefff\");", 7686 format("someFunction(\"aaabbbccc ddeeefff\");", 7687 getLLVMStyleWithColumns(25))); 7688 EXPECT_EQ("someFunction1234567890(\n" 7689 " \"aaabb \"\n" 7690 " \"cccdddeeefff\");", 7691 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7692 getLLVMStyleWithColumns(25))); 7693 EXPECT_EQ("#define A \\\n" 7694 " string s = \\\n" 7695 " \"123456789\" \\\n" 7696 " \"0\"; \\\n" 7697 " int i;", 7698 format("#define A string s = \"1234567890\"; int i;", 7699 getLLVMStyleWithColumns(20))); 7700 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7701 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7702 " \"dddeeeff\"\n" 7703 " \"f\");", 7704 format("someFunction(\"aaabbbcc dddeeefff\");", 7705 getLLVMStyleWithColumns(25))); 7706 } 7707 7708 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7709 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7710 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7711 EXPECT_EQ("\"test\"\n" 7712 "\"\\n\"", 7713 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7714 EXPECT_EQ("\"tes\\\\\"\n" 7715 "\"n\"", 7716 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7717 EXPECT_EQ("\"\\\\\\\\\"\n" 7718 "\"\\n\"", 7719 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7720 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7721 EXPECT_EQ("\"\\uff01\"\n" 7722 "\"test\"", 7723 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7724 EXPECT_EQ("\"\\Uff01ff02\"", 7725 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7726 EXPECT_EQ("\"\\x000000000001\"\n" 7727 "\"next\"", 7728 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7729 EXPECT_EQ("\"\\x000000000001next\"", 7730 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7731 EXPECT_EQ("\"\\x000000000001\"", 7732 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7733 EXPECT_EQ("\"test\"\n" 7734 "\"\\000000\"\n" 7735 "\"000001\"", 7736 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7737 EXPECT_EQ("\"test\\000\"\n" 7738 "\"00000000\"\n" 7739 "\"1\"", 7740 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7741 } 7742 7743 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7744 verifyFormat("void f() {\n" 7745 " return g() {}\n" 7746 " void h() {}"); 7747 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7748 "g();\n" 7749 "}"); 7750 } 7751 7752 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7753 verifyFormat( 7754 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7755 } 7756 7757 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7758 verifyFormat("class X {\n" 7759 " void f() {\n" 7760 " }\n" 7761 "};", 7762 getLLVMStyleWithColumns(12)); 7763 } 7764 7765 TEST_F(FormatTest, ConfigurableIndentWidth) { 7766 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7767 EightIndent.IndentWidth = 8; 7768 EightIndent.ContinuationIndentWidth = 8; 7769 verifyFormat("void f() {\n" 7770 " someFunction();\n" 7771 " if (true) {\n" 7772 " f();\n" 7773 " }\n" 7774 "}", 7775 EightIndent); 7776 verifyFormat("class X {\n" 7777 " void f() {\n" 7778 " }\n" 7779 "};", 7780 EightIndent); 7781 verifyFormat("int x[] = {\n" 7782 " call(),\n" 7783 " call()};", 7784 EightIndent); 7785 } 7786 7787 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7788 verifyFormat("double\n" 7789 "f();", 7790 getLLVMStyleWithColumns(8)); 7791 } 7792 7793 TEST_F(FormatTest, ConfigurableUseOfTab) { 7794 FormatStyle Tab = getLLVMStyleWithColumns(42); 7795 Tab.IndentWidth = 8; 7796 Tab.UseTab = FormatStyle::UT_Always; 7797 Tab.AlignEscapedNewlinesLeft = true; 7798 7799 EXPECT_EQ("if (aaaaaaaa && // q\n" 7800 " bb)\t\t// w\n" 7801 "\t;", 7802 format("if (aaaaaaaa &&// q\n" 7803 "bb)// w\n" 7804 ";", 7805 Tab)); 7806 EXPECT_EQ("if (aaa && bbb) // w\n" 7807 "\t;", 7808 format("if(aaa&&bbb)// w\n" 7809 ";", 7810 Tab)); 7811 7812 verifyFormat("class X {\n" 7813 "\tvoid f() {\n" 7814 "\t\tsomeFunction(parameter1,\n" 7815 "\t\t\t parameter2);\n" 7816 "\t}\n" 7817 "};", 7818 Tab); 7819 verifyFormat("#define A \\\n" 7820 "\tvoid f() { \\\n" 7821 "\t\tsomeFunction( \\\n" 7822 "\t\t parameter1, \\\n" 7823 "\t\t parameter2); \\\n" 7824 "\t}", 7825 Tab); 7826 7827 Tab.TabWidth = 4; 7828 Tab.IndentWidth = 8; 7829 verifyFormat("class TabWidth4Indent8 {\n" 7830 "\t\tvoid f() {\n" 7831 "\t\t\t\tsomeFunction(parameter1,\n" 7832 "\t\t\t\t\t\t\t parameter2);\n" 7833 "\t\t}\n" 7834 "};", 7835 Tab); 7836 7837 Tab.TabWidth = 4; 7838 Tab.IndentWidth = 4; 7839 verifyFormat("class TabWidth4Indent4 {\n" 7840 "\tvoid f() {\n" 7841 "\t\tsomeFunction(parameter1,\n" 7842 "\t\t\t\t\t parameter2);\n" 7843 "\t}\n" 7844 "};", 7845 Tab); 7846 7847 Tab.TabWidth = 8; 7848 Tab.IndentWidth = 4; 7849 verifyFormat("class TabWidth8Indent4 {\n" 7850 " void f() {\n" 7851 "\tsomeFunction(parameter1,\n" 7852 "\t\t parameter2);\n" 7853 " }\n" 7854 "};", 7855 Tab); 7856 7857 Tab.TabWidth = 8; 7858 Tab.IndentWidth = 8; 7859 EXPECT_EQ("/*\n" 7860 "\t a\t\tcomment\n" 7861 "\t in multiple lines\n" 7862 " */", 7863 format(" /*\t \t \n" 7864 " \t \t a\t\tcomment\t \t\n" 7865 " \t \t in multiple lines\t\n" 7866 " \t */", 7867 Tab)); 7868 7869 Tab.UseTab = FormatStyle::UT_ForIndentation; 7870 verifyFormat("{\n" 7871 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7872 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7873 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7874 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7875 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7876 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7877 "};", 7878 Tab); 7879 verifyFormat("enum AA {\n" 7880 "\ta1, // Force multiple lines\n" 7881 "\ta2,\n" 7882 "\ta3\n" 7883 "};", 7884 Tab); 7885 EXPECT_EQ("if (aaaaaaaa && // q\n" 7886 " bb) // w\n" 7887 "\t;", 7888 format("if (aaaaaaaa &&// q\n" 7889 "bb)// w\n" 7890 ";", 7891 Tab)); 7892 verifyFormat("class X {\n" 7893 "\tvoid f() {\n" 7894 "\t\tsomeFunction(parameter1,\n" 7895 "\t\t parameter2);\n" 7896 "\t}\n" 7897 "};", 7898 Tab); 7899 verifyFormat("{\n" 7900 "\tQ(\n" 7901 "\t {\n" 7902 "\t\t int a;\n" 7903 "\t\t someFunction(aaaaaaaa,\n" 7904 "\t\t bbbbbbb);\n" 7905 "\t },\n" 7906 "\t p);\n" 7907 "}", 7908 Tab); 7909 EXPECT_EQ("{\n" 7910 "\t/* aaaa\n" 7911 "\t bbbb */\n" 7912 "}", 7913 format("{\n" 7914 "/* aaaa\n" 7915 " bbbb */\n" 7916 "}", 7917 Tab)); 7918 EXPECT_EQ("{\n" 7919 "\t/*\n" 7920 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7921 "\t bbbbbbbbbbbbb\n" 7922 "\t*/\n" 7923 "}", 7924 format("{\n" 7925 "/*\n" 7926 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7927 "*/\n" 7928 "}", 7929 Tab)); 7930 EXPECT_EQ("{\n" 7931 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7932 "\t// bbbbbbbbbbbbb\n" 7933 "}", 7934 format("{\n" 7935 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7936 "}", 7937 Tab)); 7938 EXPECT_EQ("{\n" 7939 "\t/*\n" 7940 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7941 "\t bbbbbbbbbbbbb\n" 7942 "\t*/\n" 7943 "}", 7944 format("{\n" 7945 "\t/*\n" 7946 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7947 "\t*/\n" 7948 "}", 7949 Tab)); 7950 EXPECT_EQ("{\n" 7951 "\t/*\n" 7952 "\n" 7953 "\t*/\n" 7954 "}", 7955 format("{\n" 7956 "\t/*\n" 7957 "\n" 7958 "\t*/\n" 7959 "}", 7960 Tab)); 7961 EXPECT_EQ("{\n" 7962 "\t/*\n" 7963 " asdf\n" 7964 "\t*/\n" 7965 "}", 7966 format("{\n" 7967 "\t/*\n" 7968 " asdf\n" 7969 "\t*/\n" 7970 "}", 7971 Tab)); 7972 7973 Tab.UseTab = FormatStyle::UT_Never; 7974 EXPECT_EQ("/*\n" 7975 " a\t\tcomment\n" 7976 " in multiple lines\n" 7977 " */", 7978 format(" /*\t \t \n" 7979 " \t \t a\t\tcomment\t \t\n" 7980 " \t \t in multiple lines\t\n" 7981 " \t */", 7982 Tab)); 7983 EXPECT_EQ("/* some\n" 7984 " comment */", 7985 format(" \t \t /* some\n" 7986 " \t \t comment */", 7987 Tab)); 7988 EXPECT_EQ("int a; /* some\n" 7989 " comment */", 7990 format(" \t \t int a; /* some\n" 7991 " \t \t comment */", 7992 Tab)); 7993 7994 EXPECT_EQ("int a; /* some\n" 7995 "comment */", 7996 format(" \t \t int\ta; /* some\n" 7997 " \t \t comment */", 7998 Tab)); 7999 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8000 " comment */", 8001 format(" \t \t f(\"\t\t\"); /* some\n" 8002 " \t \t comment */", 8003 Tab)); 8004 EXPECT_EQ("{\n" 8005 " /*\n" 8006 " * Comment\n" 8007 " */\n" 8008 " int i;\n" 8009 "}", 8010 format("{\n" 8011 "\t/*\n" 8012 "\t * Comment\n" 8013 "\t */\n" 8014 "\t int i;\n" 8015 "}")); 8016 8017 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8018 Tab.TabWidth = 8; 8019 Tab.IndentWidth = 8; 8020 EXPECT_EQ("if (aaaaaaaa && // q\n" 8021 " bb) // w\n" 8022 "\t;", 8023 format("if (aaaaaaaa &&// q\n" 8024 "bb)// w\n" 8025 ";", 8026 Tab)); 8027 EXPECT_EQ("if (aaa && bbb) // w\n" 8028 "\t;", 8029 format("if(aaa&&bbb)// w\n" 8030 ";", 8031 Tab)); 8032 verifyFormat("class X {\n" 8033 "\tvoid f() {\n" 8034 "\t\tsomeFunction(parameter1,\n" 8035 "\t\t\t parameter2);\n" 8036 "\t}\n" 8037 "};", 8038 Tab); 8039 verifyFormat("#define A \\\n" 8040 "\tvoid f() { \\\n" 8041 "\t\tsomeFunction( \\\n" 8042 "\t\t parameter1, \\\n" 8043 "\t\t parameter2); \\\n" 8044 "\t}", 8045 Tab); 8046 Tab.TabWidth = 4; 8047 Tab.IndentWidth = 8; 8048 verifyFormat("class TabWidth4Indent8 {\n" 8049 "\t\tvoid f() {\n" 8050 "\t\t\t\tsomeFunction(parameter1,\n" 8051 "\t\t\t\t\t\t\t parameter2);\n" 8052 "\t\t}\n" 8053 "};", 8054 Tab); 8055 Tab.TabWidth = 4; 8056 Tab.IndentWidth = 4; 8057 verifyFormat("class TabWidth4Indent4 {\n" 8058 "\tvoid f() {\n" 8059 "\t\tsomeFunction(parameter1,\n" 8060 "\t\t\t\t\t parameter2);\n" 8061 "\t}\n" 8062 "};", 8063 Tab); 8064 Tab.TabWidth = 8; 8065 Tab.IndentWidth = 4; 8066 verifyFormat("class TabWidth8Indent4 {\n" 8067 " void f() {\n" 8068 "\tsomeFunction(parameter1,\n" 8069 "\t\t parameter2);\n" 8070 " }\n" 8071 "};", 8072 Tab); 8073 Tab.TabWidth = 8; 8074 Tab.IndentWidth = 8; 8075 EXPECT_EQ("/*\n" 8076 "\t a\t\tcomment\n" 8077 "\t in multiple lines\n" 8078 " */", 8079 format(" /*\t \t \n" 8080 " \t \t a\t\tcomment\t \t\n" 8081 " \t \t in multiple lines\t\n" 8082 " \t */", 8083 Tab)); 8084 verifyFormat("{\n" 8085 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8086 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8087 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8088 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8089 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8090 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8091 "};", 8092 Tab); 8093 verifyFormat("enum AA {\n" 8094 "\ta1, // Force multiple lines\n" 8095 "\ta2,\n" 8096 "\ta3\n" 8097 "};", 8098 Tab); 8099 EXPECT_EQ("if (aaaaaaaa && // q\n" 8100 " bb) // w\n" 8101 "\t;", 8102 format("if (aaaaaaaa &&// q\n" 8103 "bb)// w\n" 8104 ";", 8105 Tab)); 8106 verifyFormat("class X {\n" 8107 "\tvoid f() {\n" 8108 "\t\tsomeFunction(parameter1,\n" 8109 "\t\t\t parameter2);\n" 8110 "\t}\n" 8111 "};", 8112 Tab); 8113 verifyFormat("{\n" 8114 "\tQ(\n" 8115 "\t {\n" 8116 "\t\t int a;\n" 8117 "\t\t someFunction(aaaaaaaa,\n" 8118 "\t\t\t\t bbbbbbb);\n" 8119 "\t },\n" 8120 "\t p);\n" 8121 "}", 8122 Tab); 8123 EXPECT_EQ("{\n" 8124 "\t/* aaaa\n" 8125 "\t bbbb */\n" 8126 "}", 8127 format("{\n" 8128 "/* aaaa\n" 8129 " bbbb */\n" 8130 "}", 8131 Tab)); 8132 EXPECT_EQ("{\n" 8133 "\t/*\n" 8134 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8135 "\t bbbbbbbbbbbbb\n" 8136 "\t*/\n" 8137 "}", 8138 format("{\n" 8139 "/*\n" 8140 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8141 "*/\n" 8142 "}", 8143 Tab)); 8144 EXPECT_EQ("{\n" 8145 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8146 "\t// bbbbbbbbbbbbb\n" 8147 "}", 8148 format("{\n" 8149 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8150 "}", 8151 Tab)); 8152 EXPECT_EQ("{\n" 8153 "\t/*\n" 8154 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8155 "\t bbbbbbbbbbbbb\n" 8156 "\t*/\n" 8157 "}", 8158 format("{\n" 8159 "\t/*\n" 8160 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8161 "\t*/\n" 8162 "}", 8163 Tab)); 8164 EXPECT_EQ("{\n" 8165 "\t/*\n" 8166 "\n" 8167 "\t*/\n" 8168 "}", 8169 format("{\n" 8170 "\t/*\n" 8171 "\n" 8172 "\t*/\n" 8173 "}", 8174 Tab)); 8175 EXPECT_EQ("{\n" 8176 "\t/*\n" 8177 " asdf\n" 8178 "\t*/\n" 8179 "}", 8180 format("{\n" 8181 "\t/*\n" 8182 " asdf\n" 8183 "\t*/\n" 8184 "}", 8185 Tab)); 8186 EXPECT_EQ("/*\n" 8187 "\t a\t\tcomment\n" 8188 "\t in multiple lines\n" 8189 " */", 8190 format(" /*\t \t \n" 8191 " \t \t a\t\tcomment\t \t\n" 8192 " \t \t in multiple lines\t\n" 8193 " \t */", 8194 Tab)); 8195 EXPECT_EQ("/* some\n" 8196 " comment */", 8197 format(" \t \t /* some\n" 8198 " \t \t comment */", 8199 Tab)); 8200 EXPECT_EQ("int a; /* some\n" 8201 " comment */", 8202 format(" \t \t int a; /* some\n" 8203 " \t \t comment */", 8204 Tab)); 8205 EXPECT_EQ("int a; /* some\n" 8206 "comment */", 8207 format(" \t \t int\ta; /* some\n" 8208 " \t \t comment */", 8209 Tab)); 8210 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8211 " comment */", 8212 format(" \t \t f(\"\t\t\"); /* some\n" 8213 " \t \t comment */", 8214 Tab)); 8215 EXPECT_EQ("{\n" 8216 " /*\n" 8217 " * Comment\n" 8218 " */\n" 8219 " int i;\n" 8220 "}", 8221 format("{\n" 8222 "\t/*\n" 8223 "\t * Comment\n" 8224 "\t */\n" 8225 "\t int i;\n" 8226 "}")); 8227 Tab.AlignConsecutiveAssignments = true; 8228 Tab.AlignConsecutiveDeclarations = true; 8229 Tab.TabWidth = 4; 8230 Tab.IndentWidth = 4; 8231 verifyFormat("class Assign {\n" 8232 "\tvoid f() {\n" 8233 "\t\tint x = 123;\n" 8234 "\t\tint random = 4;\n" 8235 "\t\tstd::string alphabet =\n" 8236 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8237 "\t}\n" 8238 "};", 8239 Tab); 8240 } 8241 8242 TEST_F(FormatTest, CalculatesOriginalColumn) { 8243 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8244 "q\"; /* some\n" 8245 " comment */", 8246 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8247 "q\"; /* some\n" 8248 " comment */", 8249 getLLVMStyle())); 8250 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8251 "/* some\n" 8252 " comment */", 8253 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8254 " /* some\n" 8255 " comment */", 8256 getLLVMStyle())); 8257 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8258 "qqq\n" 8259 "/* some\n" 8260 " comment */", 8261 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8262 "qqq\n" 8263 " /* some\n" 8264 " comment */", 8265 getLLVMStyle())); 8266 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8267 "wwww; /* some\n" 8268 " comment */", 8269 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8270 "wwww; /* some\n" 8271 " comment */", 8272 getLLVMStyle())); 8273 } 8274 8275 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8276 FormatStyle NoSpace = getLLVMStyle(); 8277 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8278 8279 verifyFormat("while(true)\n" 8280 " continue;", 8281 NoSpace); 8282 verifyFormat("for(;;)\n" 8283 " continue;", 8284 NoSpace); 8285 verifyFormat("if(true)\n" 8286 " f();\n" 8287 "else if(true)\n" 8288 " f();", 8289 NoSpace); 8290 verifyFormat("do {\n" 8291 " do_something();\n" 8292 "} while(something());", 8293 NoSpace); 8294 verifyFormat("switch(x) {\n" 8295 "default:\n" 8296 " break;\n" 8297 "}", 8298 NoSpace); 8299 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8300 verifyFormat("size_t x = sizeof(x);", NoSpace); 8301 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8302 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8303 verifyFormat("alignas(128) char a[128];", NoSpace); 8304 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8305 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8306 verifyFormat("int f() throw(Deprecated);", NoSpace); 8307 verifyFormat("typedef void (*cb)(int);", NoSpace); 8308 verifyFormat("T A::operator()();", NoSpace); 8309 verifyFormat("X A::operator++(T);", NoSpace); 8310 8311 FormatStyle Space = getLLVMStyle(); 8312 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8313 8314 verifyFormat("int f ();", Space); 8315 verifyFormat("void f (int a, T b) {\n" 8316 " while (true)\n" 8317 " continue;\n" 8318 "}", 8319 Space); 8320 verifyFormat("if (true)\n" 8321 " f ();\n" 8322 "else if (true)\n" 8323 " f ();", 8324 Space); 8325 verifyFormat("do {\n" 8326 " do_something ();\n" 8327 "} while (something ());", 8328 Space); 8329 verifyFormat("switch (x) {\n" 8330 "default:\n" 8331 " break;\n" 8332 "}", 8333 Space); 8334 verifyFormat("A::A () : a (1) {}", Space); 8335 verifyFormat("void f () __attribute__ ((asdf));", Space); 8336 verifyFormat("*(&a + 1);\n" 8337 "&((&a)[1]);\n" 8338 "a[(b + c) * d];\n" 8339 "(((a + 1) * 2) + 3) * 4;", 8340 Space); 8341 verifyFormat("#define A(x) x", Space); 8342 verifyFormat("#define A (x) x", Space); 8343 verifyFormat("#if defined(x)\n" 8344 "#endif", 8345 Space); 8346 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8347 verifyFormat("size_t x = sizeof (x);", Space); 8348 verifyFormat("auto f (int x) -> decltype (x);", Space); 8349 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8350 verifyFormat("alignas (128) char a[128];", Space); 8351 verifyFormat("size_t x = alignof (MyType);", Space); 8352 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8353 verifyFormat("int f () throw (Deprecated);", Space); 8354 verifyFormat("typedef void (*cb) (int);", Space); 8355 verifyFormat("T A::operator() ();", Space); 8356 verifyFormat("X A::operator++ (T);", Space); 8357 } 8358 8359 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8360 FormatStyle Spaces = getLLVMStyle(); 8361 8362 Spaces.SpacesInParentheses = true; 8363 verifyFormat("call( x, y, z );", Spaces); 8364 verifyFormat("call();", Spaces); 8365 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8366 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8367 Spaces); 8368 verifyFormat("while ( (bool)1 )\n" 8369 " continue;", 8370 Spaces); 8371 verifyFormat("for ( ;; )\n" 8372 " continue;", 8373 Spaces); 8374 verifyFormat("if ( true )\n" 8375 " f();\n" 8376 "else if ( true )\n" 8377 " f();", 8378 Spaces); 8379 verifyFormat("do {\n" 8380 " do_something( (int)i );\n" 8381 "} while ( something() );", 8382 Spaces); 8383 verifyFormat("switch ( x ) {\n" 8384 "default:\n" 8385 " break;\n" 8386 "}", 8387 Spaces); 8388 8389 Spaces.SpacesInParentheses = false; 8390 Spaces.SpacesInCStyleCastParentheses = true; 8391 verifyFormat("Type *A = ( Type * )P;", Spaces); 8392 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8393 verifyFormat("x = ( int32 )y;", Spaces); 8394 verifyFormat("int a = ( int )(2.0f);", Spaces); 8395 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8396 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8397 verifyFormat("#define x (( int )-1)", Spaces); 8398 8399 // Run the first set of tests again with: 8400 Spaces.SpacesInParentheses = false; 8401 Spaces.SpaceInEmptyParentheses = true; 8402 Spaces.SpacesInCStyleCastParentheses = true; 8403 verifyFormat("call(x, y, z);", Spaces); 8404 verifyFormat("call( );", Spaces); 8405 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8406 verifyFormat("while (( bool )1)\n" 8407 " continue;", 8408 Spaces); 8409 verifyFormat("for (;;)\n" 8410 " continue;", 8411 Spaces); 8412 verifyFormat("if (true)\n" 8413 " f( );\n" 8414 "else if (true)\n" 8415 " f( );", 8416 Spaces); 8417 verifyFormat("do {\n" 8418 " do_something(( int )i);\n" 8419 "} while (something( ));", 8420 Spaces); 8421 verifyFormat("switch (x) {\n" 8422 "default:\n" 8423 " break;\n" 8424 "}", 8425 Spaces); 8426 8427 // Run the first set of tests again with: 8428 Spaces.SpaceAfterCStyleCast = true; 8429 verifyFormat("call(x, y, z);", Spaces); 8430 verifyFormat("call( );", Spaces); 8431 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8432 verifyFormat("while (( bool ) 1)\n" 8433 " continue;", 8434 Spaces); 8435 verifyFormat("for (;;)\n" 8436 " continue;", 8437 Spaces); 8438 verifyFormat("if (true)\n" 8439 " f( );\n" 8440 "else if (true)\n" 8441 " f( );", 8442 Spaces); 8443 verifyFormat("do {\n" 8444 " do_something(( int ) i);\n" 8445 "} while (something( ));", 8446 Spaces); 8447 verifyFormat("switch (x) {\n" 8448 "default:\n" 8449 " break;\n" 8450 "}", 8451 Spaces); 8452 8453 // Run subset of tests again with: 8454 Spaces.SpacesInCStyleCastParentheses = false; 8455 Spaces.SpaceAfterCStyleCast = true; 8456 verifyFormat("while ((bool) 1)\n" 8457 " continue;", 8458 Spaces); 8459 verifyFormat("do {\n" 8460 " do_something((int) i);\n" 8461 "} while (something( ));", 8462 Spaces); 8463 } 8464 8465 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8466 verifyFormat("int a[5];"); 8467 verifyFormat("a[3] += 42;"); 8468 8469 FormatStyle Spaces = getLLVMStyle(); 8470 Spaces.SpacesInSquareBrackets = true; 8471 // Lambdas unchanged. 8472 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8473 verifyFormat("return [i, args...] {};", Spaces); 8474 8475 // Not lambdas. 8476 verifyFormat("int a[ 5 ];", Spaces); 8477 verifyFormat("a[ 3 ] += 42;", Spaces); 8478 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8479 verifyFormat("double &operator[](int i) { return 0; }\n" 8480 "int i;", 8481 Spaces); 8482 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8483 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8484 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8485 } 8486 8487 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8488 verifyFormat("int a = 5;"); 8489 verifyFormat("a += 42;"); 8490 verifyFormat("a or_eq 8;"); 8491 8492 FormatStyle Spaces = getLLVMStyle(); 8493 Spaces.SpaceBeforeAssignmentOperators = false; 8494 verifyFormat("int a= 5;", Spaces); 8495 verifyFormat("a+= 42;", Spaces); 8496 verifyFormat("a or_eq 8;", Spaces); 8497 } 8498 8499 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8500 FormatStyle Alignment = getLLVMStyle(); 8501 Alignment.AlignConsecutiveAssignments = false; 8502 verifyFormat("int a = 5;\n" 8503 "int oneTwoThree = 123;", 8504 Alignment); 8505 verifyFormat("int a = 5;\n" 8506 "int oneTwoThree = 123;", 8507 Alignment); 8508 8509 Alignment.AlignConsecutiveAssignments = true; 8510 verifyFormat("int a = 5;\n" 8511 "int oneTwoThree = 123;", 8512 Alignment); 8513 verifyFormat("int a = method();\n" 8514 "int oneTwoThree = 133;", 8515 Alignment); 8516 verifyFormat("a &= 5;\n" 8517 "bcd *= 5;\n" 8518 "ghtyf += 5;\n" 8519 "dvfvdb -= 5;\n" 8520 "a /= 5;\n" 8521 "vdsvsv %= 5;\n" 8522 "sfdbddfbdfbb ^= 5;\n" 8523 "dvsdsv |= 5;\n" 8524 "int dsvvdvsdvvv = 123;", 8525 Alignment); 8526 verifyFormat("int i = 1, j = 10;\n" 8527 "something = 2000;", 8528 Alignment); 8529 verifyFormat("something = 2000;\n" 8530 "int i = 1, j = 10;\n", 8531 Alignment); 8532 verifyFormat("something = 2000;\n" 8533 "another = 911;\n" 8534 "int i = 1, j = 10;\n" 8535 "oneMore = 1;\n" 8536 "i = 2;", 8537 Alignment); 8538 verifyFormat("int a = 5;\n" 8539 "int one = 1;\n" 8540 "method();\n" 8541 "int oneTwoThree = 123;\n" 8542 "int oneTwo = 12;", 8543 Alignment); 8544 verifyFormat("int oneTwoThree = 123;\n" 8545 "int oneTwo = 12;\n" 8546 "method();\n", 8547 Alignment); 8548 verifyFormat("int oneTwoThree = 123; // comment\n" 8549 "int oneTwo = 12; // comment", 8550 Alignment); 8551 EXPECT_EQ("int a = 5;\n" 8552 "\n" 8553 "int oneTwoThree = 123;", 8554 format("int a = 5;\n" 8555 "\n" 8556 "int oneTwoThree= 123;", 8557 Alignment)); 8558 EXPECT_EQ("int a = 5;\n" 8559 "int one = 1;\n" 8560 "\n" 8561 "int oneTwoThree = 123;", 8562 format("int a = 5;\n" 8563 "int one = 1;\n" 8564 "\n" 8565 "int oneTwoThree = 123;", 8566 Alignment)); 8567 EXPECT_EQ("int a = 5;\n" 8568 "int one = 1;\n" 8569 "\n" 8570 "int oneTwoThree = 123;\n" 8571 "int oneTwo = 12;", 8572 format("int a = 5;\n" 8573 "int one = 1;\n" 8574 "\n" 8575 "int oneTwoThree = 123;\n" 8576 "int oneTwo = 12;", 8577 Alignment)); 8578 Alignment.AlignEscapedNewlinesLeft = true; 8579 verifyFormat("#define A \\\n" 8580 " int aaaa = 12; \\\n" 8581 " int b = 23; \\\n" 8582 " int ccc = 234; \\\n" 8583 " int dddddddddd = 2345;", 8584 Alignment); 8585 Alignment.AlignEscapedNewlinesLeft = false; 8586 verifyFormat("#define A " 8587 " \\\n" 8588 " int aaaa = 12; " 8589 " \\\n" 8590 " int b = 23; " 8591 " \\\n" 8592 " int ccc = 234; " 8593 " \\\n" 8594 " int dddddddddd = 2345;", 8595 Alignment); 8596 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8597 "k = 4, int l = 5,\n" 8598 " int m = 6) {\n" 8599 " int j = 10;\n" 8600 " otherThing = 1;\n" 8601 "}", 8602 Alignment); 8603 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8604 " int i = 1;\n" 8605 " int j = 2;\n" 8606 " int big = 10000;\n" 8607 "}", 8608 Alignment); 8609 verifyFormat("class C {\n" 8610 "public:\n" 8611 " int i = 1;\n" 8612 " virtual void f() = 0;\n" 8613 "};", 8614 Alignment); 8615 verifyFormat("int i = 1;\n" 8616 "if (SomeType t = getSomething()) {\n" 8617 "}\n" 8618 "int j = 2;\n" 8619 "int big = 10000;", 8620 Alignment); 8621 verifyFormat("int j = 7;\n" 8622 "for (int k = 0; k < N; ++k) {\n" 8623 "}\n" 8624 "int j = 2;\n" 8625 "int big = 10000;\n" 8626 "}", 8627 Alignment); 8628 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8629 verifyFormat("int i = 1;\n" 8630 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8631 " = someLooooooooooooooooongFunction();\n" 8632 "int j = 2;", 8633 Alignment); 8634 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8635 verifyFormat("int i = 1;\n" 8636 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8637 " someLooooooooooooooooongFunction();\n" 8638 "int j = 2;", 8639 Alignment); 8640 8641 verifyFormat("auto lambda = []() {\n" 8642 " auto i = 0;\n" 8643 " return 0;\n" 8644 "};\n" 8645 "int i = 0;\n" 8646 "auto v = type{\n" 8647 " i = 1, //\n" 8648 " (i = 2), //\n" 8649 " i = 3 //\n" 8650 "};", 8651 Alignment); 8652 8653 // FIXME: Should align all three assignments 8654 verifyFormat( 8655 "int i = 1;\n" 8656 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8657 " loooooooooooooooooooooongParameterB);\n" 8658 "int j = 2;", 8659 Alignment); 8660 8661 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8662 " typename B = very_long_type_name_1,\n" 8663 " typename T_2 = very_long_type_name_2>\n" 8664 "auto foo() {}\n", 8665 Alignment); 8666 verifyFormat("int a, b = 1;\n" 8667 "int c = 2;\n" 8668 "int dd = 3;\n", 8669 Alignment); 8670 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8671 "float b[1][] = {{3.f}};\n", 8672 Alignment); 8673 } 8674 8675 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8676 FormatStyle Alignment = getLLVMStyle(); 8677 Alignment.AlignConsecutiveDeclarations = false; 8678 verifyFormat("float const a = 5;\n" 8679 "int oneTwoThree = 123;", 8680 Alignment); 8681 verifyFormat("int a = 5;\n" 8682 "float const oneTwoThree = 123;", 8683 Alignment); 8684 8685 Alignment.AlignConsecutiveDeclarations = true; 8686 verifyFormat("float const a = 5;\n" 8687 "int oneTwoThree = 123;", 8688 Alignment); 8689 verifyFormat("int a = method();\n" 8690 "float const oneTwoThree = 133;", 8691 Alignment); 8692 verifyFormat("int i = 1, j = 10;\n" 8693 "something = 2000;", 8694 Alignment); 8695 verifyFormat("something = 2000;\n" 8696 "int i = 1, j = 10;\n", 8697 Alignment); 8698 verifyFormat("float something = 2000;\n" 8699 "double another = 911;\n" 8700 "int i = 1, j = 10;\n" 8701 "const int *oneMore = 1;\n" 8702 "unsigned i = 2;", 8703 Alignment); 8704 verifyFormat("float a = 5;\n" 8705 "int one = 1;\n" 8706 "method();\n" 8707 "const double oneTwoThree = 123;\n" 8708 "const unsigned int oneTwo = 12;", 8709 Alignment); 8710 verifyFormat("int oneTwoThree{0}; // comment\n" 8711 "unsigned oneTwo; // comment", 8712 Alignment); 8713 EXPECT_EQ("float const a = 5;\n" 8714 "\n" 8715 "int oneTwoThree = 123;", 8716 format("float const a = 5;\n" 8717 "\n" 8718 "int oneTwoThree= 123;", 8719 Alignment)); 8720 EXPECT_EQ("float a = 5;\n" 8721 "int one = 1;\n" 8722 "\n" 8723 "unsigned oneTwoThree = 123;", 8724 format("float a = 5;\n" 8725 "int one = 1;\n" 8726 "\n" 8727 "unsigned oneTwoThree = 123;", 8728 Alignment)); 8729 EXPECT_EQ("float a = 5;\n" 8730 "int one = 1;\n" 8731 "\n" 8732 "unsigned oneTwoThree = 123;\n" 8733 "int oneTwo = 12;", 8734 format("float a = 5;\n" 8735 "int one = 1;\n" 8736 "\n" 8737 "unsigned oneTwoThree = 123;\n" 8738 "int oneTwo = 12;", 8739 Alignment)); 8740 Alignment.AlignConsecutiveAssignments = true; 8741 verifyFormat("float something = 2000;\n" 8742 "double another = 911;\n" 8743 "int i = 1, j = 10;\n" 8744 "const int *oneMore = 1;\n" 8745 "unsigned i = 2;", 8746 Alignment); 8747 verifyFormat("int oneTwoThree = {0}; // comment\n" 8748 "unsigned oneTwo = 0; // comment", 8749 Alignment); 8750 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8751 " int const i = 1;\n" 8752 " int * j = 2;\n" 8753 " int big = 10000;\n" 8754 "\n" 8755 " unsigned oneTwoThree = 123;\n" 8756 " int oneTwo = 12;\n" 8757 " method();\n" 8758 " float k = 2;\n" 8759 " int ll = 10000;\n" 8760 "}", 8761 format("void SomeFunction(int parameter= 0) {\n" 8762 " int const i= 1;\n" 8763 " int *j=2;\n" 8764 " int big = 10000;\n" 8765 "\n" 8766 "unsigned oneTwoThree =123;\n" 8767 "int oneTwo = 12;\n" 8768 " method();\n" 8769 "float k= 2;\n" 8770 "int ll=10000;\n" 8771 "}", 8772 Alignment)); 8773 Alignment.AlignConsecutiveAssignments = false; 8774 Alignment.AlignEscapedNewlinesLeft = true; 8775 verifyFormat("#define A \\\n" 8776 " int aaaa = 12; \\\n" 8777 " float b = 23; \\\n" 8778 " const int ccc = 234; \\\n" 8779 " unsigned dddddddddd = 2345;", 8780 Alignment); 8781 Alignment.AlignEscapedNewlinesLeft = false; 8782 Alignment.ColumnLimit = 30; 8783 verifyFormat("#define A \\\n" 8784 " int aaaa = 12; \\\n" 8785 " float b = 23; \\\n" 8786 " const int ccc = 234; \\\n" 8787 " int dddddddddd = 2345;", 8788 Alignment); 8789 Alignment.ColumnLimit = 80; 8790 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8791 "k = 4, int l = 5,\n" 8792 " int m = 6) {\n" 8793 " const int j = 10;\n" 8794 " otherThing = 1;\n" 8795 "}", 8796 Alignment); 8797 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8798 " int const i = 1;\n" 8799 " int * j = 2;\n" 8800 " int big = 10000;\n" 8801 "}", 8802 Alignment); 8803 verifyFormat("class C {\n" 8804 "public:\n" 8805 " int i = 1;\n" 8806 " virtual void f() = 0;\n" 8807 "};", 8808 Alignment); 8809 verifyFormat("float i = 1;\n" 8810 "if (SomeType t = getSomething()) {\n" 8811 "}\n" 8812 "const unsigned j = 2;\n" 8813 "int big = 10000;", 8814 Alignment); 8815 verifyFormat("float j = 7;\n" 8816 "for (int k = 0; k < N; ++k) {\n" 8817 "}\n" 8818 "unsigned j = 2;\n" 8819 "int big = 10000;\n" 8820 "}", 8821 Alignment); 8822 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8823 verifyFormat("float i = 1;\n" 8824 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8825 " = someLooooooooooooooooongFunction();\n" 8826 "int j = 2;", 8827 Alignment); 8828 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8829 verifyFormat("int i = 1;\n" 8830 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8831 " someLooooooooooooooooongFunction();\n" 8832 "int j = 2;", 8833 Alignment); 8834 8835 Alignment.AlignConsecutiveAssignments = true; 8836 verifyFormat("auto lambda = []() {\n" 8837 " auto ii = 0;\n" 8838 " float j = 0;\n" 8839 " return 0;\n" 8840 "};\n" 8841 "int i = 0;\n" 8842 "float i2 = 0;\n" 8843 "auto v = type{\n" 8844 " i = 1, //\n" 8845 " (i = 2), //\n" 8846 " i = 3 //\n" 8847 "};", 8848 Alignment); 8849 Alignment.AlignConsecutiveAssignments = false; 8850 8851 // FIXME: Should align all three declarations 8852 verifyFormat( 8853 "int i = 1;\n" 8854 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8855 " loooooooooooooooooooooongParameterB);\n" 8856 "int j = 2;", 8857 Alignment); 8858 8859 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8860 // We expect declarations and assignments to align, as long as it doesn't 8861 // exceed the column limit, starting a new alignemnt sequence whenever it 8862 // happens. 8863 Alignment.AlignConsecutiveAssignments = true; 8864 Alignment.ColumnLimit = 30; 8865 verifyFormat("float ii = 1;\n" 8866 "unsigned j = 2;\n" 8867 "int someVerylongVariable = 1;\n" 8868 "AnotherLongType ll = 123456;\n" 8869 "VeryVeryLongType k = 2;\n" 8870 "int myvar = 1;", 8871 Alignment); 8872 Alignment.ColumnLimit = 80; 8873 Alignment.AlignConsecutiveAssignments = false; 8874 8875 verifyFormat( 8876 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8877 " typename LongType, typename B>\n" 8878 "auto foo() {}\n", 8879 Alignment); 8880 verifyFormat("float a, b = 1;\n" 8881 "int c = 2;\n" 8882 "int dd = 3;\n", 8883 Alignment); 8884 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8885 "float b[1][] = {{3.f}};\n", 8886 Alignment); 8887 Alignment.AlignConsecutiveAssignments = true; 8888 verifyFormat("float a, b = 1;\n" 8889 "int c = 2;\n" 8890 "int dd = 3;\n", 8891 Alignment); 8892 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8893 "float b[1][] = {{3.f}};\n", 8894 Alignment); 8895 Alignment.AlignConsecutiveAssignments = false; 8896 8897 Alignment.ColumnLimit = 30; 8898 Alignment.BinPackParameters = false; 8899 verifyFormat("void foo(float a,\n" 8900 " float b,\n" 8901 " int c,\n" 8902 " uint32_t *d) {\n" 8903 " int * e = 0;\n" 8904 " float f = 0;\n" 8905 " double g = 0;\n" 8906 "}\n" 8907 "void bar(ino_t a,\n" 8908 " int b,\n" 8909 " uint32_t *c,\n" 8910 " bool d) {}\n", 8911 Alignment); 8912 Alignment.BinPackParameters = true; 8913 Alignment.ColumnLimit = 80; 8914 } 8915 8916 TEST_F(FormatTest, LinuxBraceBreaking) { 8917 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8918 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8919 verifyFormat("namespace a\n" 8920 "{\n" 8921 "class A\n" 8922 "{\n" 8923 " void f()\n" 8924 " {\n" 8925 " if (true) {\n" 8926 " a();\n" 8927 " b();\n" 8928 " } else {\n" 8929 " a();\n" 8930 " }\n" 8931 " }\n" 8932 " void g() { return; }\n" 8933 "};\n" 8934 "struct B {\n" 8935 " int x;\n" 8936 "};\n" 8937 "}\n", 8938 LinuxBraceStyle); 8939 verifyFormat("enum X {\n" 8940 " Y = 0,\n" 8941 "}\n", 8942 LinuxBraceStyle); 8943 verifyFormat("struct S {\n" 8944 " int Type;\n" 8945 " union {\n" 8946 " int x;\n" 8947 " double y;\n" 8948 " } Value;\n" 8949 " class C\n" 8950 " {\n" 8951 " MyFavoriteType Value;\n" 8952 " } Class;\n" 8953 "}\n", 8954 LinuxBraceStyle); 8955 } 8956 8957 TEST_F(FormatTest, MozillaBraceBreaking) { 8958 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8959 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8960 verifyFormat("namespace a {\n" 8961 "class A\n" 8962 "{\n" 8963 " void f()\n" 8964 " {\n" 8965 " if (true) {\n" 8966 " a();\n" 8967 " b();\n" 8968 " }\n" 8969 " }\n" 8970 " void g() { return; }\n" 8971 "};\n" 8972 "enum E\n" 8973 "{\n" 8974 " A,\n" 8975 " // foo\n" 8976 " B,\n" 8977 " C\n" 8978 "};\n" 8979 "struct B\n" 8980 "{\n" 8981 " int x;\n" 8982 "};\n" 8983 "}\n", 8984 MozillaBraceStyle); 8985 verifyFormat("struct S\n" 8986 "{\n" 8987 " int Type;\n" 8988 " union\n" 8989 " {\n" 8990 " int x;\n" 8991 " double y;\n" 8992 " } Value;\n" 8993 " class C\n" 8994 " {\n" 8995 " MyFavoriteType Value;\n" 8996 " } Class;\n" 8997 "}\n", 8998 MozillaBraceStyle); 8999 } 9000 9001 TEST_F(FormatTest, StroustrupBraceBreaking) { 9002 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9003 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9004 verifyFormat("namespace a {\n" 9005 "class A {\n" 9006 " void f()\n" 9007 " {\n" 9008 " if (true) {\n" 9009 " a();\n" 9010 " b();\n" 9011 " }\n" 9012 " }\n" 9013 " void g() { return; }\n" 9014 "};\n" 9015 "struct B {\n" 9016 " int x;\n" 9017 "};\n" 9018 "}\n", 9019 StroustrupBraceStyle); 9020 9021 verifyFormat("void foo()\n" 9022 "{\n" 9023 " if (a) {\n" 9024 " a();\n" 9025 " }\n" 9026 " else {\n" 9027 " b();\n" 9028 " }\n" 9029 "}\n", 9030 StroustrupBraceStyle); 9031 9032 verifyFormat("#ifdef _DEBUG\n" 9033 "int foo(int i = 0)\n" 9034 "#else\n" 9035 "int foo(int i = 5)\n" 9036 "#endif\n" 9037 "{\n" 9038 " return i;\n" 9039 "}", 9040 StroustrupBraceStyle); 9041 9042 verifyFormat("void foo() {}\n" 9043 "void bar()\n" 9044 "#ifdef _DEBUG\n" 9045 "{\n" 9046 " foo();\n" 9047 "}\n" 9048 "#else\n" 9049 "{\n" 9050 "}\n" 9051 "#endif", 9052 StroustrupBraceStyle); 9053 9054 verifyFormat("void foobar() { int i = 5; }\n" 9055 "#ifdef _DEBUG\n" 9056 "void bar() {}\n" 9057 "#else\n" 9058 "void bar() { foobar(); }\n" 9059 "#endif", 9060 StroustrupBraceStyle); 9061 } 9062 9063 TEST_F(FormatTest, AllmanBraceBreaking) { 9064 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9065 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9066 verifyFormat("namespace a\n" 9067 "{\n" 9068 "class A\n" 9069 "{\n" 9070 " void f()\n" 9071 " {\n" 9072 " if (true)\n" 9073 " {\n" 9074 " a();\n" 9075 " b();\n" 9076 " }\n" 9077 " }\n" 9078 " void g() { return; }\n" 9079 "};\n" 9080 "struct B\n" 9081 "{\n" 9082 " int x;\n" 9083 "};\n" 9084 "}", 9085 AllmanBraceStyle); 9086 9087 verifyFormat("void f()\n" 9088 "{\n" 9089 " if (true)\n" 9090 " {\n" 9091 " a();\n" 9092 " }\n" 9093 " else if (false)\n" 9094 " {\n" 9095 " b();\n" 9096 " }\n" 9097 " else\n" 9098 " {\n" 9099 " c();\n" 9100 " }\n" 9101 "}\n", 9102 AllmanBraceStyle); 9103 9104 verifyFormat("void f()\n" 9105 "{\n" 9106 " for (int i = 0; i < 10; ++i)\n" 9107 " {\n" 9108 " a();\n" 9109 " }\n" 9110 " while (false)\n" 9111 " {\n" 9112 " b();\n" 9113 " }\n" 9114 " do\n" 9115 " {\n" 9116 " c();\n" 9117 " } while (false)\n" 9118 "}\n", 9119 AllmanBraceStyle); 9120 9121 verifyFormat("void f(int a)\n" 9122 "{\n" 9123 " switch (a)\n" 9124 " {\n" 9125 " case 0:\n" 9126 " break;\n" 9127 " case 1:\n" 9128 " {\n" 9129 " break;\n" 9130 " }\n" 9131 " case 2:\n" 9132 " {\n" 9133 " }\n" 9134 " break;\n" 9135 " default:\n" 9136 " break;\n" 9137 " }\n" 9138 "}\n", 9139 AllmanBraceStyle); 9140 9141 verifyFormat("enum X\n" 9142 "{\n" 9143 " Y = 0,\n" 9144 "}\n", 9145 AllmanBraceStyle); 9146 verifyFormat("enum X\n" 9147 "{\n" 9148 " Y = 0\n" 9149 "}\n", 9150 AllmanBraceStyle); 9151 9152 verifyFormat("@interface BSApplicationController ()\n" 9153 "{\n" 9154 "@private\n" 9155 " id _extraIvar;\n" 9156 "}\n" 9157 "@end\n", 9158 AllmanBraceStyle); 9159 9160 verifyFormat("#ifdef _DEBUG\n" 9161 "int foo(int i = 0)\n" 9162 "#else\n" 9163 "int foo(int i = 5)\n" 9164 "#endif\n" 9165 "{\n" 9166 " return i;\n" 9167 "}", 9168 AllmanBraceStyle); 9169 9170 verifyFormat("void foo() {}\n" 9171 "void bar()\n" 9172 "#ifdef _DEBUG\n" 9173 "{\n" 9174 " foo();\n" 9175 "}\n" 9176 "#else\n" 9177 "{\n" 9178 "}\n" 9179 "#endif", 9180 AllmanBraceStyle); 9181 9182 verifyFormat("void foobar() { int i = 5; }\n" 9183 "#ifdef _DEBUG\n" 9184 "void bar() {}\n" 9185 "#else\n" 9186 "void bar() { foobar(); }\n" 9187 "#endif", 9188 AllmanBraceStyle); 9189 9190 // This shouldn't affect ObjC blocks.. 9191 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9192 " // ...\n" 9193 " int i;\n" 9194 "}];", 9195 AllmanBraceStyle); 9196 verifyFormat("void (^block)(void) = ^{\n" 9197 " // ...\n" 9198 " int i;\n" 9199 "};", 9200 AllmanBraceStyle); 9201 // .. or dict literals. 9202 verifyFormat("void f()\n" 9203 "{\n" 9204 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9205 "}", 9206 AllmanBraceStyle); 9207 verifyFormat("int f()\n" 9208 "{ // comment\n" 9209 " return 42;\n" 9210 "}", 9211 AllmanBraceStyle); 9212 9213 AllmanBraceStyle.ColumnLimit = 19; 9214 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9215 AllmanBraceStyle.ColumnLimit = 18; 9216 verifyFormat("void f()\n" 9217 "{\n" 9218 " int i;\n" 9219 "}", 9220 AllmanBraceStyle); 9221 AllmanBraceStyle.ColumnLimit = 80; 9222 9223 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9224 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9225 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9226 verifyFormat("void f(bool b)\n" 9227 "{\n" 9228 " if (b)\n" 9229 " {\n" 9230 " return;\n" 9231 " }\n" 9232 "}\n", 9233 BreakBeforeBraceShortIfs); 9234 verifyFormat("void f(bool b)\n" 9235 "{\n" 9236 " if (b) return;\n" 9237 "}\n", 9238 BreakBeforeBraceShortIfs); 9239 verifyFormat("void f(bool b)\n" 9240 "{\n" 9241 " while (b)\n" 9242 " {\n" 9243 " return;\n" 9244 " }\n" 9245 "}\n", 9246 BreakBeforeBraceShortIfs); 9247 } 9248 9249 TEST_F(FormatTest, GNUBraceBreaking) { 9250 FormatStyle GNUBraceStyle = getLLVMStyle(); 9251 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9252 verifyFormat("namespace a\n" 9253 "{\n" 9254 "class A\n" 9255 "{\n" 9256 " void f()\n" 9257 " {\n" 9258 " int a;\n" 9259 " {\n" 9260 " int b;\n" 9261 " }\n" 9262 " if (true)\n" 9263 " {\n" 9264 " a();\n" 9265 " b();\n" 9266 " }\n" 9267 " }\n" 9268 " void g() { return; }\n" 9269 "}\n" 9270 "}", 9271 GNUBraceStyle); 9272 9273 verifyFormat("void f()\n" 9274 "{\n" 9275 " if (true)\n" 9276 " {\n" 9277 " a();\n" 9278 " }\n" 9279 " else if (false)\n" 9280 " {\n" 9281 " b();\n" 9282 " }\n" 9283 " else\n" 9284 " {\n" 9285 " c();\n" 9286 " }\n" 9287 "}\n", 9288 GNUBraceStyle); 9289 9290 verifyFormat("void f()\n" 9291 "{\n" 9292 " for (int i = 0; i < 10; ++i)\n" 9293 " {\n" 9294 " a();\n" 9295 " }\n" 9296 " while (false)\n" 9297 " {\n" 9298 " b();\n" 9299 " }\n" 9300 " do\n" 9301 " {\n" 9302 " c();\n" 9303 " }\n" 9304 " while (false);\n" 9305 "}\n", 9306 GNUBraceStyle); 9307 9308 verifyFormat("void f(int a)\n" 9309 "{\n" 9310 " switch (a)\n" 9311 " {\n" 9312 " case 0:\n" 9313 " break;\n" 9314 " case 1:\n" 9315 " {\n" 9316 " break;\n" 9317 " }\n" 9318 " case 2:\n" 9319 " {\n" 9320 " }\n" 9321 " break;\n" 9322 " default:\n" 9323 " break;\n" 9324 " }\n" 9325 "}\n", 9326 GNUBraceStyle); 9327 9328 verifyFormat("enum X\n" 9329 "{\n" 9330 " Y = 0,\n" 9331 "}\n", 9332 GNUBraceStyle); 9333 9334 verifyFormat("@interface BSApplicationController ()\n" 9335 "{\n" 9336 "@private\n" 9337 " id _extraIvar;\n" 9338 "}\n" 9339 "@end\n", 9340 GNUBraceStyle); 9341 9342 verifyFormat("#ifdef _DEBUG\n" 9343 "int foo(int i = 0)\n" 9344 "#else\n" 9345 "int foo(int i = 5)\n" 9346 "#endif\n" 9347 "{\n" 9348 " return i;\n" 9349 "}", 9350 GNUBraceStyle); 9351 9352 verifyFormat("void foo() {}\n" 9353 "void bar()\n" 9354 "#ifdef _DEBUG\n" 9355 "{\n" 9356 " foo();\n" 9357 "}\n" 9358 "#else\n" 9359 "{\n" 9360 "}\n" 9361 "#endif", 9362 GNUBraceStyle); 9363 9364 verifyFormat("void foobar() { int i = 5; }\n" 9365 "#ifdef _DEBUG\n" 9366 "void bar() {}\n" 9367 "#else\n" 9368 "void bar() { foobar(); }\n" 9369 "#endif", 9370 GNUBraceStyle); 9371 } 9372 9373 TEST_F(FormatTest, WebKitBraceBreaking) { 9374 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9375 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9376 verifyFormat("namespace a {\n" 9377 "class A {\n" 9378 " void f()\n" 9379 " {\n" 9380 " if (true) {\n" 9381 " a();\n" 9382 " b();\n" 9383 " }\n" 9384 " }\n" 9385 " void g() { return; }\n" 9386 "};\n" 9387 "enum E {\n" 9388 " A,\n" 9389 " // foo\n" 9390 " B,\n" 9391 " C\n" 9392 "};\n" 9393 "struct B {\n" 9394 " int x;\n" 9395 "};\n" 9396 "}\n", 9397 WebKitBraceStyle); 9398 verifyFormat("struct S {\n" 9399 " int Type;\n" 9400 " union {\n" 9401 " int x;\n" 9402 " double y;\n" 9403 " } Value;\n" 9404 " class C {\n" 9405 " MyFavoriteType Value;\n" 9406 " } Class;\n" 9407 "};\n", 9408 WebKitBraceStyle); 9409 } 9410 9411 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9412 verifyFormat("void f() {\n" 9413 " try {\n" 9414 " } catch (const Exception &e) {\n" 9415 " }\n" 9416 "}\n", 9417 getLLVMStyle()); 9418 } 9419 9420 TEST_F(FormatTest, UnderstandsPragmas) { 9421 verifyFormat("#pragma omp reduction(| : var)"); 9422 verifyFormat("#pragma omp reduction(+ : var)"); 9423 9424 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9425 "(including parentheses).", 9426 format("#pragma mark Any non-hyphenated or hyphenated string " 9427 "(including parentheses).")); 9428 } 9429 9430 TEST_F(FormatTest, UnderstandPragmaOption) { 9431 verifyFormat("#pragma option -C -A"); 9432 9433 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9434 } 9435 9436 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9437 for (size_t i = 1; i < Styles.size(); ++i) \ 9438 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9439 << " differs from Style #0" 9440 9441 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9442 SmallVector<FormatStyle, 3> Styles; 9443 Styles.resize(3); 9444 9445 Styles[0] = getLLVMStyle(); 9446 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9447 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9448 EXPECT_ALL_STYLES_EQUAL(Styles); 9449 9450 Styles[0] = getGoogleStyle(); 9451 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9452 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9453 EXPECT_ALL_STYLES_EQUAL(Styles); 9454 9455 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9456 EXPECT_TRUE( 9457 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9458 EXPECT_TRUE( 9459 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9460 EXPECT_ALL_STYLES_EQUAL(Styles); 9461 9462 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9463 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9464 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9465 EXPECT_ALL_STYLES_EQUAL(Styles); 9466 9467 Styles[0] = getMozillaStyle(); 9468 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9469 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9470 EXPECT_ALL_STYLES_EQUAL(Styles); 9471 9472 Styles[0] = getWebKitStyle(); 9473 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9474 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9475 EXPECT_ALL_STYLES_EQUAL(Styles); 9476 9477 Styles[0] = getGNUStyle(); 9478 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9479 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9480 EXPECT_ALL_STYLES_EQUAL(Styles); 9481 9482 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9483 } 9484 9485 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9486 SmallVector<FormatStyle, 8> Styles; 9487 Styles.resize(2); 9488 9489 Styles[0] = getGoogleStyle(); 9490 Styles[1] = getLLVMStyle(); 9491 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9492 EXPECT_ALL_STYLES_EQUAL(Styles); 9493 9494 Styles.resize(5); 9495 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9496 Styles[1] = getLLVMStyle(); 9497 Styles[1].Language = FormatStyle::LK_JavaScript; 9498 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9499 9500 Styles[2] = getLLVMStyle(); 9501 Styles[2].Language = FormatStyle::LK_JavaScript; 9502 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9503 "BasedOnStyle: Google", 9504 &Styles[2]) 9505 .value()); 9506 9507 Styles[3] = getLLVMStyle(); 9508 Styles[3].Language = FormatStyle::LK_JavaScript; 9509 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9510 "Language: JavaScript", 9511 &Styles[3]) 9512 .value()); 9513 9514 Styles[4] = getLLVMStyle(); 9515 Styles[4].Language = FormatStyle::LK_JavaScript; 9516 EXPECT_EQ(0, parseConfiguration("---\n" 9517 "BasedOnStyle: LLVM\n" 9518 "IndentWidth: 123\n" 9519 "---\n" 9520 "BasedOnStyle: Google\n" 9521 "Language: JavaScript", 9522 &Styles[4]) 9523 .value()); 9524 EXPECT_ALL_STYLES_EQUAL(Styles); 9525 } 9526 9527 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9528 Style.FIELD = false; \ 9529 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9530 EXPECT_TRUE(Style.FIELD); \ 9531 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9532 EXPECT_FALSE(Style.FIELD); 9533 9534 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9535 9536 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9537 Style.STRUCT.FIELD = false; \ 9538 EXPECT_EQ(0, \ 9539 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9540 .value()); \ 9541 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9542 EXPECT_EQ(0, \ 9543 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9544 .value()); \ 9545 EXPECT_FALSE(Style.STRUCT.FIELD); 9546 9547 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9548 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9549 9550 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9551 EXPECT_NE(VALUE, Style.FIELD); \ 9552 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9553 EXPECT_EQ(VALUE, Style.FIELD) 9554 9555 TEST_F(FormatTest, ParsesConfigurationBools) { 9556 FormatStyle Style = {}; 9557 Style.Language = FormatStyle::LK_Cpp; 9558 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9559 CHECK_PARSE_BOOL(AlignOperands); 9560 CHECK_PARSE_BOOL(AlignTrailingComments); 9561 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9562 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9563 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9564 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9565 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9566 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9567 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9568 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9569 CHECK_PARSE_BOOL(BinPackArguments); 9570 CHECK_PARSE_BOOL(BinPackParameters); 9571 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9572 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9573 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9574 CHECK_PARSE_BOOL(BreakStringLiterals); 9575 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9576 CHECK_PARSE_BOOL(DerivePointerAlignment); 9577 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9578 CHECK_PARSE_BOOL(DisableFormat); 9579 CHECK_PARSE_BOOL(IndentCaseLabels); 9580 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9581 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9582 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9583 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9584 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9585 CHECK_PARSE_BOOL(ReflowComments); 9586 CHECK_PARSE_BOOL(SortIncludes); 9587 CHECK_PARSE_BOOL(SpacesInParentheses); 9588 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9589 CHECK_PARSE_BOOL(SpacesInAngles); 9590 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9591 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9592 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9593 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9594 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9595 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9596 9597 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9598 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9599 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9600 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9601 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9602 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9603 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9604 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9605 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9606 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9607 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9608 } 9609 9610 #undef CHECK_PARSE_BOOL 9611 9612 TEST_F(FormatTest, ParsesConfiguration) { 9613 FormatStyle Style = {}; 9614 Style.Language = FormatStyle::LK_Cpp; 9615 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9616 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9617 ConstructorInitializerIndentWidth, 1234u); 9618 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9619 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9620 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9621 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9622 PenaltyBreakBeforeFirstCallParameter, 1234u); 9623 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9624 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9625 PenaltyReturnTypeOnItsOwnLine, 1234u); 9626 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9627 SpacesBeforeTrailingComments, 1234u); 9628 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9629 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9630 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9631 9632 Style.PointerAlignment = FormatStyle::PAS_Middle; 9633 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9634 FormatStyle::PAS_Left); 9635 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9636 FormatStyle::PAS_Right); 9637 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9638 FormatStyle::PAS_Middle); 9639 // For backward compatibility: 9640 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9641 FormatStyle::PAS_Left); 9642 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9643 FormatStyle::PAS_Right); 9644 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9645 FormatStyle::PAS_Middle); 9646 9647 Style.Standard = FormatStyle::LS_Auto; 9648 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9649 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9650 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9651 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9652 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9653 9654 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9655 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9656 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9657 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9658 FormatStyle::BOS_None); 9659 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9660 FormatStyle::BOS_All); 9661 // For backward compatibility: 9662 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9663 FormatStyle::BOS_None); 9664 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9665 FormatStyle::BOS_All); 9666 9667 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9668 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9669 FormatStyle::BAS_Align); 9670 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9671 FormatStyle::BAS_DontAlign); 9672 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9673 FormatStyle::BAS_AlwaysBreak); 9674 // For backward compatibility: 9675 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9676 FormatStyle::BAS_DontAlign); 9677 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9678 FormatStyle::BAS_Align); 9679 9680 Style.UseTab = FormatStyle::UT_ForIndentation; 9681 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9682 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9683 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9684 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9685 FormatStyle::UT_ForContinuationAndIndentation); 9686 // For backward compatibility: 9687 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9688 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9689 9690 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9691 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9692 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9693 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9694 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9695 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9696 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9697 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9698 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9699 // For backward compatibility: 9700 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9701 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9702 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9703 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9704 9705 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9706 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9707 FormatStyle::SBPO_Never); 9708 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9709 FormatStyle::SBPO_Always); 9710 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9711 FormatStyle::SBPO_ControlStatements); 9712 // For backward compatibility: 9713 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9714 FormatStyle::SBPO_Never); 9715 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9716 FormatStyle::SBPO_ControlStatements); 9717 9718 Style.ColumnLimit = 123; 9719 FormatStyle BaseStyle = getLLVMStyle(); 9720 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9721 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9722 9723 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9724 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9725 FormatStyle::BS_Attach); 9726 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9727 FormatStyle::BS_Linux); 9728 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9729 FormatStyle::BS_Mozilla); 9730 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9731 FormatStyle::BS_Stroustrup); 9732 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9733 FormatStyle::BS_Allman); 9734 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9735 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9736 FormatStyle::BS_WebKit); 9737 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9738 FormatStyle::BS_Custom); 9739 9740 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9741 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9742 FormatStyle::RTBS_None); 9743 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9744 FormatStyle::RTBS_All); 9745 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9746 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9747 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9748 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9749 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9750 AlwaysBreakAfterReturnType, 9751 FormatStyle::RTBS_TopLevelDefinitions); 9752 9753 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9754 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9755 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9756 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9757 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9758 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9759 AlwaysBreakAfterDefinitionReturnType, 9760 FormatStyle::DRTBS_TopLevel); 9761 9762 Style.NamespaceIndentation = FormatStyle::NI_All; 9763 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9764 FormatStyle::NI_None); 9765 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9766 FormatStyle::NI_Inner); 9767 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9768 FormatStyle::NI_All); 9769 9770 // FIXME: This is required because parsing a configuration simply overwrites 9771 // the first N elements of the list instead of resetting it. 9772 Style.ForEachMacros.clear(); 9773 std::vector<std::string> BoostForeach; 9774 BoostForeach.push_back("BOOST_FOREACH"); 9775 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9776 std::vector<std::string> BoostAndQForeach; 9777 BoostAndQForeach.push_back("BOOST_FOREACH"); 9778 BoostAndQForeach.push_back("Q_FOREACH"); 9779 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9780 BoostAndQForeach); 9781 9782 Style.IncludeCategories.clear(); 9783 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9784 {".*", 1}}; 9785 CHECK_PARSE("IncludeCategories:\n" 9786 " - Regex: abc/.*\n" 9787 " Priority: 2\n" 9788 " - Regex: .*\n" 9789 " Priority: 1", 9790 IncludeCategories, ExpectedCategories); 9791 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9792 } 9793 9794 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9795 FormatStyle Style = {}; 9796 Style.Language = FormatStyle::LK_Cpp; 9797 CHECK_PARSE("Language: Cpp\n" 9798 "IndentWidth: 12", 9799 IndentWidth, 12u); 9800 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9801 "IndentWidth: 34", 9802 &Style), 9803 ParseError::Unsuitable); 9804 EXPECT_EQ(12u, Style.IndentWidth); 9805 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9806 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9807 9808 Style.Language = FormatStyle::LK_JavaScript; 9809 CHECK_PARSE("Language: JavaScript\n" 9810 "IndentWidth: 12", 9811 IndentWidth, 12u); 9812 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9813 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9814 "IndentWidth: 34", 9815 &Style), 9816 ParseError::Unsuitable); 9817 EXPECT_EQ(23u, Style.IndentWidth); 9818 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9819 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9820 9821 CHECK_PARSE("BasedOnStyle: LLVM\n" 9822 "IndentWidth: 67", 9823 IndentWidth, 67u); 9824 9825 CHECK_PARSE("---\n" 9826 "Language: JavaScript\n" 9827 "IndentWidth: 12\n" 9828 "---\n" 9829 "Language: Cpp\n" 9830 "IndentWidth: 34\n" 9831 "...\n", 9832 IndentWidth, 12u); 9833 9834 Style.Language = FormatStyle::LK_Cpp; 9835 CHECK_PARSE("---\n" 9836 "Language: JavaScript\n" 9837 "IndentWidth: 12\n" 9838 "---\n" 9839 "Language: Cpp\n" 9840 "IndentWidth: 34\n" 9841 "...\n", 9842 IndentWidth, 34u); 9843 CHECK_PARSE("---\n" 9844 "IndentWidth: 78\n" 9845 "---\n" 9846 "Language: JavaScript\n" 9847 "IndentWidth: 56\n" 9848 "...\n", 9849 IndentWidth, 78u); 9850 9851 Style.ColumnLimit = 123; 9852 Style.IndentWidth = 234; 9853 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9854 Style.TabWidth = 345; 9855 EXPECT_FALSE(parseConfiguration("---\n" 9856 "IndentWidth: 456\n" 9857 "BreakBeforeBraces: Allman\n" 9858 "---\n" 9859 "Language: JavaScript\n" 9860 "IndentWidth: 111\n" 9861 "TabWidth: 111\n" 9862 "---\n" 9863 "Language: Cpp\n" 9864 "BreakBeforeBraces: Stroustrup\n" 9865 "TabWidth: 789\n" 9866 "...\n", 9867 &Style)); 9868 EXPECT_EQ(123u, Style.ColumnLimit); 9869 EXPECT_EQ(456u, Style.IndentWidth); 9870 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9871 EXPECT_EQ(789u, Style.TabWidth); 9872 9873 EXPECT_EQ(parseConfiguration("---\n" 9874 "Language: JavaScript\n" 9875 "IndentWidth: 56\n" 9876 "---\n" 9877 "IndentWidth: 78\n" 9878 "...\n", 9879 &Style), 9880 ParseError::Error); 9881 EXPECT_EQ(parseConfiguration("---\n" 9882 "Language: JavaScript\n" 9883 "IndentWidth: 56\n" 9884 "---\n" 9885 "Language: JavaScript\n" 9886 "IndentWidth: 78\n" 9887 "...\n", 9888 &Style), 9889 ParseError::Error); 9890 9891 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9892 } 9893 9894 #undef CHECK_PARSE 9895 9896 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9897 FormatStyle Style = {}; 9898 Style.Language = FormatStyle::LK_JavaScript; 9899 Style.BreakBeforeTernaryOperators = true; 9900 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9901 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9902 9903 Style.BreakBeforeTernaryOperators = true; 9904 EXPECT_EQ(0, parseConfiguration("---\n" 9905 "BasedOnStyle: Google\n" 9906 "---\n" 9907 "Language: JavaScript\n" 9908 "IndentWidth: 76\n" 9909 "...\n", 9910 &Style) 9911 .value()); 9912 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9913 EXPECT_EQ(76u, Style.IndentWidth); 9914 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9915 } 9916 9917 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9918 FormatStyle Style = getLLVMStyle(); 9919 std::string YAML = configurationAsText(Style); 9920 FormatStyle ParsedStyle = {}; 9921 ParsedStyle.Language = FormatStyle::LK_Cpp; 9922 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9923 EXPECT_EQ(Style, ParsedStyle); 9924 } 9925 9926 TEST_F(FormatTest, WorksFor8bitEncodings) { 9927 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9928 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9929 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9930 "\"\xef\xee\xf0\xf3...\"", 9931 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9932 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9933 "\xef\xee\xf0\xf3...\"", 9934 getLLVMStyleWithColumns(12))); 9935 } 9936 9937 TEST_F(FormatTest, HandlesUTF8BOM) { 9938 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9939 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9940 format("\xef\xbb\xbf#include <iostream>")); 9941 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9942 format("\xef\xbb\xbf\n#include <iostream>")); 9943 } 9944 9945 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9946 #if !defined(_MSC_VER) 9947 9948 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9949 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9950 getLLVMStyleWithColumns(35)); 9951 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9952 getLLVMStyleWithColumns(31)); 9953 verifyFormat("// Однажды в студёную зимнюю пору...", 9954 getLLVMStyleWithColumns(36)); 9955 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9956 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9957 getLLVMStyleWithColumns(39)); 9958 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9959 getLLVMStyleWithColumns(35)); 9960 } 9961 9962 TEST_F(FormatTest, SplitsUTF8Strings) { 9963 // Non-printable characters' width is currently considered to be the length in 9964 // bytes in UTF8. The characters can be displayed in very different manner 9965 // (zero-width, single width with a substitution glyph, expanded to their code 9966 // (e.g. "<8d>"), so there's no single correct way to handle them. 9967 EXPECT_EQ("\"aaaaÄ\"\n" 9968 "\"\xc2\x8d\";", 9969 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9970 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9971 "\"\xc2\x8d\";", 9972 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9973 EXPECT_EQ("\"Однажды, в \"\n" 9974 "\"студёную \"\n" 9975 "\"зимнюю \"\n" 9976 "\"пору,\"", 9977 format("\"Однажды, в студёную зимнюю пору,\"", 9978 getLLVMStyleWithColumns(13))); 9979 EXPECT_EQ( 9980 "\"一 二 三 \"\n" 9981 "\"四 五六 \"\n" 9982 "\"七 八 九 \"\n" 9983 "\"十\"", 9984 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9985 EXPECT_EQ("\"一\t二 \"\n" 9986 "\"\t三 \"\n" 9987 "\"四 五\t六 \"\n" 9988 "\"\t七 \"\n" 9989 "\"八九十\tqq\"", 9990 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9991 getLLVMStyleWithColumns(11))); 9992 9993 // UTF8 character in an escape sequence. 9994 EXPECT_EQ("\"aaaaaa\"\n" 9995 "\"\\\xC2\x8D\"", 9996 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 9997 } 9998 9999 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10000 EXPECT_EQ("const char *sssss =\n" 10001 " \"一二三四五六七八\\\n" 10002 " 九 十\";", 10003 format("const char *sssss = \"一二三四五六七八\\\n" 10004 " 九 十\";", 10005 getLLVMStyleWithColumns(30))); 10006 } 10007 10008 TEST_F(FormatTest, SplitsUTF8LineComments) { 10009 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10010 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10011 EXPECT_EQ("// Я из лесу\n" 10012 "// вышел; был\n" 10013 "// сильный\n" 10014 "// мороз.", 10015 format("// Я из лесу вышел; был сильный мороз.", 10016 getLLVMStyleWithColumns(13))); 10017 EXPECT_EQ("// 一二三\n" 10018 "// 四五六七\n" 10019 "// 八 九\n" 10020 "// 十", 10021 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10022 } 10023 10024 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10025 EXPECT_EQ("/* Гляжу,\n" 10026 " * поднимается\n" 10027 " * медленно в\n" 10028 " * гору\n" 10029 " * Лошадка,\n" 10030 " * везущая\n" 10031 " * хворосту\n" 10032 " * воз. */", 10033 format("/* Гляжу, поднимается медленно в гору\n" 10034 " * Лошадка, везущая хворосту воз. */", 10035 getLLVMStyleWithColumns(13))); 10036 EXPECT_EQ( 10037 "/* 一二三\n" 10038 " * 四五六七\n" 10039 " * 八 九\n" 10040 " * 十 */", 10041 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10042 EXPECT_EQ("/* \n" 10043 " * \n" 10044 " * - */", 10045 format("/* - */", getLLVMStyleWithColumns(12))); 10046 } 10047 10048 #endif // _MSC_VER 10049 10050 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10051 FormatStyle Style = getLLVMStyle(); 10052 10053 Style.ConstructorInitializerIndentWidth = 4; 10054 verifyFormat( 10055 "SomeClass::Constructor()\n" 10056 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10057 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10058 Style); 10059 10060 Style.ConstructorInitializerIndentWidth = 2; 10061 verifyFormat( 10062 "SomeClass::Constructor()\n" 10063 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10064 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10065 Style); 10066 10067 Style.ConstructorInitializerIndentWidth = 0; 10068 verifyFormat( 10069 "SomeClass::Constructor()\n" 10070 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10071 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10072 Style); 10073 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10074 verifyFormat( 10075 "SomeLongTemplateVariableName<\n" 10076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10077 Style); 10078 verifyFormat( 10079 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10081 Style); 10082 } 10083 10084 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10085 FormatStyle Style = getLLVMStyle(); 10086 Style.BreakConstructorInitializersBeforeComma = true; 10087 Style.ConstructorInitializerIndentWidth = 4; 10088 verifyFormat("SomeClass::Constructor()\n" 10089 " : a(a)\n" 10090 " , b(b)\n" 10091 " , c(c) {}", 10092 Style); 10093 verifyFormat("SomeClass::Constructor()\n" 10094 " : a(a) {}", 10095 Style); 10096 10097 Style.ColumnLimit = 0; 10098 verifyFormat("SomeClass::Constructor()\n" 10099 " : a(a) {}", 10100 Style); 10101 verifyFormat("SomeClass::Constructor() noexcept\n" 10102 " : a(a) {}", 10103 Style); 10104 verifyFormat("SomeClass::Constructor()\n" 10105 " : a(a)\n" 10106 " , b(b)\n" 10107 " , c(c) {}", 10108 Style); 10109 verifyFormat("SomeClass::Constructor()\n" 10110 " : a(a) {\n" 10111 " foo();\n" 10112 " bar();\n" 10113 "}", 10114 Style); 10115 10116 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10117 verifyFormat("SomeClass::Constructor()\n" 10118 " : a(a)\n" 10119 " , b(b)\n" 10120 " , c(c) {\n}", 10121 Style); 10122 verifyFormat("SomeClass::Constructor()\n" 10123 " : a(a) {\n}", 10124 Style); 10125 10126 Style.ColumnLimit = 80; 10127 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10128 Style.ConstructorInitializerIndentWidth = 2; 10129 verifyFormat("SomeClass::Constructor()\n" 10130 " : a(a)\n" 10131 " , b(b)\n" 10132 " , c(c) {}", 10133 Style); 10134 10135 Style.ConstructorInitializerIndentWidth = 0; 10136 verifyFormat("SomeClass::Constructor()\n" 10137 ": a(a)\n" 10138 ", b(b)\n" 10139 ", c(c) {}", 10140 Style); 10141 10142 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10143 Style.ConstructorInitializerIndentWidth = 4; 10144 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10145 verifyFormat( 10146 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10147 Style); 10148 verifyFormat( 10149 "SomeClass::Constructor()\n" 10150 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10151 Style); 10152 Style.ConstructorInitializerIndentWidth = 4; 10153 Style.ColumnLimit = 60; 10154 verifyFormat("SomeClass::Constructor()\n" 10155 " : aaaaaaaa(aaaaaaaa)\n" 10156 " , aaaaaaaa(aaaaaaaa)\n" 10157 " , aaaaaaaa(aaaaaaaa) {}", 10158 Style); 10159 } 10160 10161 TEST_F(FormatTest, Destructors) { 10162 verifyFormat("void F(int &i) { i.~int(); }"); 10163 verifyFormat("void F(int &i) { i->~int(); }"); 10164 } 10165 10166 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10167 FormatStyle Style = getWebKitStyle(); 10168 10169 // Don't indent in outer namespaces. 10170 verifyFormat("namespace outer {\n" 10171 "int i;\n" 10172 "namespace inner {\n" 10173 " int i;\n" 10174 "} // namespace inner\n" 10175 "} // namespace outer\n" 10176 "namespace other_outer {\n" 10177 "int i;\n" 10178 "}", 10179 Style); 10180 10181 // Don't indent case labels. 10182 verifyFormat("switch (variable) {\n" 10183 "case 1:\n" 10184 "case 2:\n" 10185 " doSomething();\n" 10186 " break;\n" 10187 "default:\n" 10188 " ++variable;\n" 10189 "}", 10190 Style); 10191 10192 // Wrap before binary operators. 10193 EXPECT_EQ("void f()\n" 10194 "{\n" 10195 " if (aaaaaaaaaaaaaaaa\n" 10196 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10197 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10198 " return;\n" 10199 "}", 10200 format("void f() {\n" 10201 "if (aaaaaaaaaaaaaaaa\n" 10202 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10203 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10204 "return;\n" 10205 "}", 10206 Style)); 10207 10208 // Allow functions on a single line. 10209 verifyFormat("void f() { return; }", Style); 10210 10211 // Constructor initializers are formatted one per line with the "," on the 10212 // new line. 10213 verifyFormat("Constructor()\n" 10214 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10215 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10216 " aaaaaaaaaaaaaa)\n" 10217 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10218 "{\n" 10219 "}", 10220 Style); 10221 verifyFormat("SomeClass::Constructor()\n" 10222 " : a(a)\n" 10223 "{\n" 10224 "}", 10225 Style); 10226 EXPECT_EQ("SomeClass::Constructor()\n" 10227 " : a(a)\n" 10228 "{\n" 10229 "}", 10230 format("SomeClass::Constructor():a(a){}", Style)); 10231 verifyFormat("SomeClass::Constructor()\n" 10232 " : a(a)\n" 10233 " , b(b)\n" 10234 " , c(c)\n" 10235 "{\n" 10236 "}", 10237 Style); 10238 verifyFormat("SomeClass::Constructor()\n" 10239 " : a(a)\n" 10240 "{\n" 10241 " foo();\n" 10242 " bar();\n" 10243 "}", 10244 Style); 10245 10246 // Access specifiers should be aligned left. 10247 verifyFormat("class C {\n" 10248 "public:\n" 10249 " int i;\n" 10250 "};", 10251 Style); 10252 10253 // Do not align comments. 10254 verifyFormat("int a; // Do not\n" 10255 "double b; // align comments.", 10256 Style); 10257 10258 // Do not align operands. 10259 EXPECT_EQ("ASSERT(aaaa\n" 10260 " || bbbb);", 10261 format("ASSERT ( aaaa\n||bbbb);", Style)); 10262 10263 // Accept input's line breaks. 10264 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10265 " || bbbbbbbbbbbbbbb) {\n" 10266 " i++;\n" 10267 "}", 10268 format("if (aaaaaaaaaaaaaaa\n" 10269 "|| bbbbbbbbbbbbbbb) { i++; }", 10270 Style)); 10271 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10272 " i++;\n" 10273 "}", 10274 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10275 10276 // Don't automatically break all macro definitions (llvm.org/PR17842). 10277 verifyFormat("#define aNumber 10", Style); 10278 // However, generally keep the line breaks that the user authored. 10279 EXPECT_EQ("#define aNumber \\\n" 10280 " 10", 10281 format("#define aNumber \\\n" 10282 " 10", 10283 Style)); 10284 10285 // Keep empty and one-element array literals on a single line. 10286 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10287 " copyItems:YES];", 10288 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10289 "copyItems:YES];", 10290 Style)); 10291 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10292 " copyItems:YES];", 10293 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10294 " copyItems:YES];", 10295 Style)); 10296 // FIXME: This does not seem right, there should be more indentation before 10297 // the array literal's entries. Nested blocks have the same problem. 10298 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10299 " @\"a\",\n" 10300 " @\"a\"\n" 10301 "]\n" 10302 " copyItems:YES];", 10303 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10304 " @\"a\",\n" 10305 " @\"a\"\n" 10306 " ]\n" 10307 " copyItems:YES];", 10308 Style)); 10309 EXPECT_EQ( 10310 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10311 " copyItems:YES];", 10312 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10313 " copyItems:YES];", 10314 Style)); 10315 10316 verifyFormat("[self.a b:c c:d];", Style); 10317 EXPECT_EQ("[self.a b:c\n" 10318 " c:d];", 10319 format("[self.a b:c\n" 10320 "c:d];", 10321 Style)); 10322 } 10323 10324 TEST_F(FormatTest, FormatsLambdas) { 10325 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10326 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10327 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10328 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10329 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10330 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10331 verifyFormat("int x = f(*+[] {});"); 10332 verifyFormat("void f() {\n" 10333 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10334 "}\n"); 10335 verifyFormat("void f() {\n" 10336 " other(x.begin(), //\n" 10337 " x.end(), //\n" 10338 " [&](int, int) { return 1; });\n" 10339 "}\n"); 10340 verifyFormat("SomeFunction([]() { // A cool function...\n" 10341 " return 43;\n" 10342 "});"); 10343 EXPECT_EQ("SomeFunction([]() {\n" 10344 "#define A a\n" 10345 " return 43;\n" 10346 "});", 10347 format("SomeFunction([](){\n" 10348 "#define A a\n" 10349 "return 43;\n" 10350 "});")); 10351 verifyFormat("void f() {\n" 10352 " SomeFunction([](decltype(x), A *a) {});\n" 10353 "}"); 10354 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10355 " [](const aaaaaaaaaa &a) { return a; });"); 10356 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10357 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10358 "});"); 10359 verifyFormat("Constructor()\n" 10360 " : Field([] { // comment\n" 10361 " int i;\n" 10362 " }) {}"); 10363 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10364 " return some_parameter.size();\n" 10365 "};"); 10366 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10367 " [](const string &s) { return s; };"); 10368 verifyFormat("int i = aaaaaa ? 1 //\n" 10369 " : [] {\n" 10370 " return 2; //\n" 10371 " }();"); 10372 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10373 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10374 " return x == 2; // force break\n" 10375 " });"); 10376 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10377 " int iiiiiiiiiiii) {\n" 10378 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10379 "});", 10380 getLLVMStyleWithColumns(60)); 10381 verifyFormat("SomeFunction({[&] {\n" 10382 " // comment\n" 10383 " },\n" 10384 " [&] {\n" 10385 " // comment\n" 10386 " }});"); 10387 verifyFormat("SomeFunction({[&] {\n" 10388 " // comment\n" 10389 "}});"); 10390 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10391 " [&]() { return true; },\n" 10392 " aaaaa aaaaaaaaa);"); 10393 10394 // Lambdas with return types. 10395 verifyFormat("int c = []() -> int { return 2; }();\n"); 10396 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10397 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10398 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10399 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10400 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10401 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10402 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10403 verifyFormat("[a, a]() -> a<1> {};"); 10404 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10405 " int j) -> int {\n" 10406 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10407 "};"); 10408 verifyFormat( 10409 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10410 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10411 " return aaaaaaaaaaaaaaaaa;\n" 10412 " });", 10413 getLLVMStyleWithColumns(70)); 10414 verifyFormat("[]() //\n" 10415 " -> int {\n" 10416 " return 1; //\n" 10417 "};"); 10418 10419 // Multiple lambdas in the same parentheses change indentation rules. 10420 verifyFormat("SomeFunction(\n" 10421 " []() {\n" 10422 " int i = 42;\n" 10423 " return i;\n" 10424 " },\n" 10425 " []() {\n" 10426 " int j = 43;\n" 10427 " return j;\n" 10428 " });"); 10429 10430 // More complex introducers. 10431 verifyFormat("return [i, args...] {};"); 10432 10433 // Not lambdas. 10434 verifyFormat("constexpr char hello[]{\"hello\"};"); 10435 verifyFormat("double &operator[](int i) { return 0; }\n" 10436 "int i;"); 10437 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10438 verifyFormat("int i = a[a][a]->f();"); 10439 verifyFormat("int i = (*b)[a]->f();"); 10440 10441 // Other corner cases. 10442 verifyFormat("void f() {\n" 10443 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10444 " );\n" 10445 "}"); 10446 10447 // Lambdas created through weird macros. 10448 verifyFormat("void f() {\n" 10449 " MACRO((const AA &a) { return 1; });\n" 10450 " MACRO((AA &a) { return 1; });\n" 10451 "}"); 10452 10453 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10454 " doo_dah();\n" 10455 " doo_dah();\n" 10456 " })) {\n" 10457 "}"); 10458 verifyFormat("auto lambda = []() {\n" 10459 " int a = 2\n" 10460 "#if A\n" 10461 " + 2\n" 10462 "#endif\n" 10463 " ;\n" 10464 "};"); 10465 } 10466 10467 TEST_F(FormatTest, FormatsBlocks) { 10468 FormatStyle ShortBlocks = getLLVMStyle(); 10469 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10470 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10471 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10472 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10473 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10474 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10475 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10476 10477 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10478 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10479 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10480 10481 verifyFormat("[operation setCompletionBlock:^{\n" 10482 " [self onOperationDone];\n" 10483 "}];"); 10484 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10485 " [self onOperationDone];\n" 10486 "}]};"); 10487 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10488 " f();\n" 10489 "}];"); 10490 verifyFormat("int a = [operation block:^int(int *i) {\n" 10491 " return 1;\n" 10492 "}];"); 10493 verifyFormat("[myObject doSomethingWith:arg1\n" 10494 " aaa:^int(int *a) {\n" 10495 " return 1;\n" 10496 " }\n" 10497 " bbb:f(a * bbbbbbbb)];"); 10498 10499 verifyFormat("[operation setCompletionBlock:^{\n" 10500 " [self.delegate newDataAvailable];\n" 10501 "}];", 10502 getLLVMStyleWithColumns(60)); 10503 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10504 " NSString *path = [self sessionFilePath];\n" 10505 " if (path) {\n" 10506 " // ...\n" 10507 " }\n" 10508 "});"); 10509 verifyFormat("[[SessionService sharedService]\n" 10510 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10511 " if (window) {\n" 10512 " [self windowDidLoad:window];\n" 10513 " } else {\n" 10514 " [self errorLoadingWindow];\n" 10515 " }\n" 10516 " }];"); 10517 verifyFormat("void (^largeBlock)(void) = ^{\n" 10518 " // ...\n" 10519 "};\n", 10520 getLLVMStyleWithColumns(40)); 10521 verifyFormat("[[SessionService sharedService]\n" 10522 " loadWindowWithCompletionBlock: //\n" 10523 " ^(SessionWindow *window) {\n" 10524 " if (window) {\n" 10525 " [self windowDidLoad:window];\n" 10526 " } else {\n" 10527 " [self errorLoadingWindow];\n" 10528 " }\n" 10529 " }];", 10530 getLLVMStyleWithColumns(60)); 10531 verifyFormat("[myObject doSomethingWith:arg1\n" 10532 " firstBlock:^(Foo *a) {\n" 10533 " // ...\n" 10534 " int i;\n" 10535 " }\n" 10536 " secondBlock:^(Bar *b) {\n" 10537 " // ...\n" 10538 " int i;\n" 10539 " }\n" 10540 " thirdBlock:^Foo(Bar *b) {\n" 10541 " // ...\n" 10542 " int i;\n" 10543 " }];"); 10544 verifyFormat("[myObject doSomethingWith:arg1\n" 10545 " firstBlock:-1\n" 10546 " secondBlock:^(Bar *b) {\n" 10547 " // ...\n" 10548 " int i;\n" 10549 " }];"); 10550 10551 verifyFormat("f(^{\n" 10552 " @autoreleasepool {\n" 10553 " if (a) {\n" 10554 " g();\n" 10555 " }\n" 10556 " }\n" 10557 "});"); 10558 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10559 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10560 "};"); 10561 10562 FormatStyle FourIndent = getLLVMStyle(); 10563 FourIndent.ObjCBlockIndentWidth = 4; 10564 verifyFormat("[operation setCompletionBlock:^{\n" 10565 " [self onOperationDone];\n" 10566 "}];", 10567 FourIndent); 10568 } 10569 10570 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10571 FormatStyle ZeroColumn = getLLVMStyle(); 10572 ZeroColumn.ColumnLimit = 0; 10573 10574 verifyFormat("[[SessionService sharedService] " 10575 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10576 " if (window) {\n" 10577 " [self windowDidLoad:window];\n" 10578 " } else {\n" 10579 " [self errorLoadingWindow];\n" 10580 " }\n" 10581 "}];", 10582 ZeroColumn); 10583 EXPECT_EQ("[[SessionService sharedService]\n" 10584 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10585 " if (window) {\n" 10586 " [self windowDidLoad:window];\n" 10587 " } else {\n" 10588 " [self errorLoadingWindow];\n" 10589 " }\n" 10590 " }];", 10591 format("[[SessionService sharedService]\n" 10592 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10593 " if (window) {\n" 10594 " [self windowDidLoad:window];\n" 10595 " } else {\n" 10596 " [self errorLoadingWindow];\n" 10597 " }\n" 10598 "}];", 10599 ZeroColumn)); 10600 verifyFormat("[myObject doSomethingWith:arg1\n" 10601 " firstBlock:^(Foo *a) {\n" 10602 " // ...\n" 10603 " int i;\n" 10604 " }\n" 10605 " secondBlock:^(Bar *b) {\n" 10606 " // ...\n" 10607 " int i;\n" 10608 " }\n" 10609 " thirdBlock:^Foo(Bar *b) {\n" 10610 " // ...\n" 10611 " int i;\n" 10612 " }];", 10613 ZeroColumn); 10614 verifyFormat("f(^{\n" 10615 " @autoreleasepool {\n" 10616 " if (a) {\n" 10617 " g();\n" 10618 " }\n" 10619 " }\n" 10620 "});", 10621 ZeroColumn); 10622 verifyFormat("void (^largeBlock)(void) = ^{\n" 10623 " // ...\n" 10624 "};", 10625 ZeroColumn); 10626 10627 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10628 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10629 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10630 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10631 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10632 " int i;\n" 10633 "};", 10634 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10635 } 10636 10637 TEST_F(FormatTest, SupportsCRLF) { 10638 EXPECT_EQ("int a;\r\n" 10639 "int b;\r\n" 10640 "int c;\r\n", 10641 format("int a;\r\n" 10642 " int b;\r\n" 10643 " int c;\r\n", 10644 getLLVMStyle())); 10645 EXPECT_EQ("int a;\r\n" 10646 "int b;\r\n" 10647 "int c;\r\n", 10648 format("int a;\r\n" 10649 " int b;\n" 10650 " int c;\r\n", 10651 getLLVMStyle())); 10652 EXPECT_EQ("int a;\n" 10653 "int b;\n" 10654 "int c;\n", 10655 format("int a;\r\n" 10656 " int b;\n" 10657 " int c;\n", 10658 getLLVMStyle())); 10659 EXPECT_EQ("\"aaaaaaa \"\r\n" 10660 "\"bbbbbbb\";\r\n", 10661 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10662 EXPECT_EQ("#define A \\\r\n" 10663 " b; \\\r\n" 10664 " c; \\\r\n" 10665 " d;\r\n", 10666 format("#define A \\\r\n" 10667 " b; \\\r\n" 10668 " c; d; \r\n", 10669 getGoogleStyle())); 10670 10671 EXPECT_EQ("/*\r\n" 10672 "multi line block comments\r\n" 10673 "should not introduce\r\n" 10674 "an extra carriage return\r\n" 10675 "*/\r\n", 10676 format("/*\r\n" 10677 "multi line block comments\r\n" 10678 "should not introduce\r\n" 10679 "an extra carriage return\r\n" 10680 "*/\r\n")); 10681 } 10682 10683 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10684 verifyFormat("MY_CLASS(C) {\n" 10685 " int i;\n" 10686 " int j;\n" 10687 "};"); 10688 } 10689 10690 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10691 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10692 TwoIndent.ContinuationIndentWidth = 2; 10693 10694 EXPECT_EQ("int i =\n" 10695 " longFunction(\n" 10696 " arg);", 10697 format("int i = longFunction(arg);", TwoIndent)); 10698 10699 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10700 SixIndent.ContinuationIndentWidth = 6; 10701 10702 EXPECT_EQ("int i =\n" 10703 " longFunction(\n" 10704 " arg);", 10705 format("int i = longFunction(arg);", SixIndent)); 10706 } 10707 10708 TEST_F(FormatTest, SpacesInAngles) { 10709 FormatStyle Spaces = getLLVMStyle(); 10710 Spaces.SpacesInAngles = true; 10711 10712 verifyFormat("static_cast< int >(arg);", Spaces); 10713 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10714 verifyFormat("f< int, float >();", Spaces); 10715 verifyFormat("template <> g() {}", Spaces); 10716 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10717 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10718 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10719 Spaces); 10720 10721 Spaces.Standard = FormatStyle::LS_Cpp03; 10722 Spaces.SpacesInAngles = true; 10723 verifyFormat("A< A< int > >();", Spaces); 10724 10725 Spaces.SpacesInAngles = false; 10726 verifyFormat("A<A<int> >();", Spaces); 10727 10728 Spaces.Standard = FormatStyle::LS_Cpp11; 10729 Spaces.SpacesInAngles = true; 10730 verifyFormat("A< A< int > >();", Spaces); 10731 10732 Spaces.SpacesInAngles = false; 10733 verifyFormat("A<A<int>>();", Spaces); 10734 } 10735 10736 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10737 FormatStyle Style = getLLVMStyle(); 10738 Style.SpaceAfterTemplateKeyword = false; 10739 verifyFormat("template<int> void foo();", Style); 10740 } 10741 10742 TEST_F(FormatTest, TripleAngleBrackets) { 10743 verifyFormat("f<<<1, 1>>>();"); 10744 verifyFormat("f<<<1, 1, 1, s>>>();"); 10745 verifyFormat("f<<<a, b, c, d>>>();"); 10746 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10747 verifyFormat("f<param><<<1, 1>>>();"); 10748 verifyFormat("f<1><<<1, 1>>>();"); 10749 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10750 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10751 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10752 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10753 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10754 } 10755 10756 TEST_F(FormatTest, MergeLessLessAtEnd) { 10757 verifyFormat("<<"); 10758 EXPECT_EQ("< < <", format("\\\n<<<")); 10759 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10760 "aaallvm::outs() <<"); 10761 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10762 "aaaallvm::outs()\n <<"); 10763 } 10764 10765 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10766 std::string code = "#if A\n" 10767 "#if B\n" 10768 "a.\n" 10769 "#endif\n" 10770 " a = 1;\n" 10771 "#else\n" 10772 "#endif\n" 10773 "#if C\n" 10774 "#else\n" 10775 "#endif\n"; 10776 EXPECT_EQ(code, format(code)); 10777 } 10778 10779 TEST_F(FormatTest, HandleConflictMarkers) { 10780 // Git/SVN conflict markers. 10781 EXPECT_EQ("int a;\n" 10782 "void f() {\n" 10783 " callme(some(parameter1,\n" 10784 "<<<<<<< text by the vcs\n" 10785 " parameter2),\n" 10786 "||||||| text by the vcs\n" 10787 " parameter2),\n" 10788 " parameter3,\n" 10789 "======= text by the vcs\n" 10790 " parameter2, parameter3),\n" 10791 ">>>>>>> text by the vcs\n" 10792 " otherparameter);\n", 10793 format("int a;\n" 10794 "void f() {\n" 10795 " callme(some(parameter1,\n" 10796 "<<<<<<< text by the vcs\n" 10797 " parameter2),\n" 10798 "||||||| text by the vcs\n" 10799 " parameter2),\n" 10800 " parameter3,\n" 10801 "======= text by the vcs\n" 10802 " parameter2,\n" 10803 " parameter3),\n" 10804 ">>>>>>> text by the vcs\n" 10805 " otherparameter);\n")); 10806 10807 // Perforce markers. 10808 EXPECT_EQ("void f() {\n" 10809 " function(\n" 10810 ">>>> text by the vcs\n" 10811 " parameter,\n" 10812 "==== text by the vcs\n" 10813 " parameter,\n" 10814 "==== text by the vcs\n" 10815 " parameter,\n" 10816 "<<<< text by the vcs\n" 10817 " parameter);\n", 10818 format("void f() {\n" 10819 " function(\n" 10820 ">>>> text by the vcs\n" 10821 " parameter,\n" 10822 "==== text by the vcs\n" 10823 " parameter,\n" 10824 "==== text by the vcs\n" 10825 " parameter,\n" 10826 "<<<< text by the vcs\n" 10827 " parameter);\n")); 10828 10829 EXPECT_EQ("<<<<<<<\n" 10830 "|||||||\n" 10831 "=======\n" 10832 ">>>>>>>", 10833 format("<<<<<<<\n" 10834 "|||||||\n" 10835 "=======\n" 10836 ">>>>>>>")); 10837 10838 EXPECT_EQ("<<<<<<<\n" 10839 "|||||||\n" 10840 "int i;\n" 10841 "=======\n" 10842 ">>>>>>>", 10843 format("<<<<<<<\n" 10844 "|||||||\n" 10845 "int i;\n" 10846 "=======\n" 10847 ">>>>>>>")); 10848 10849 // FIXME: Handle parsing of macros around conflict markers correctly: 10850 EXPECT_EQ("#define Macro \\\n" 10851 "<<<<<<<\n" 10852 "Something \\\n" 10853 "|||||||\n" 10854 "Else \\\n" 10855 "=======\n" 10856 "Other \\\n" 10857 ">>>>>>>\n" 10858 " End int i;\n", 10859 format("#define Macro \\\n" 10860 "<<<<<<<\n" 10861 " Something \\\n" 10862 "|||||||\n" 10863 " Else \\\n" 10864 "=======\n" 10865 " Other \\\n" 10866 ">>>>>>>\n" 10867 " End\n" 10868 "int i;\n")); 10869 } 10870 10871 TEST_F(FormatTest, DisableRegions) { 10872 EXPECT_EQ("int i;\n" 10873 "// clang-format off\n" 10874 " int j;\n" 10875 "// clang-format on\n" 10876 "int k;", 10877 format(" int i;\n" 10878 " // clang-format off\n" 10879 " int j;\n" 10880 " // clang-format on\n" 10881 " int k;")); 10882 EXPECT_EQ("int i;\n" 10883 "/* clang-format off */\n" 10884 " int j;\n" 10885 "/* clang-format on */\n" 10886 "int k;", 10887 format(" int i;\n" 10888 " /* clang-format off */\n" 10889 " int j;\n" 10890 " /* clang-format on */\n" 10891 " int k;")); 10892 } 10893 10894 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10895 format("? ) ="); 10896 verifyNoCrash("#define a\\\n /**/}"); 10897 } 10898 10899 TEST_F(FormatTest, FormatsTableGenCode) { 10900 FormatStyle Style = getLLVMStyle(); 10901 Style.Language = FormatStyle::LK_TableGen; 10902 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10903 } 10904 10905 TEST_F(FormatTest, ArrayOfTemplates) { 10906 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10907 format("auto a = new unique_ptr<int > [ 10];")); 10908 10909 FormatStyle Spaces = getLLVMStyle(); 10910 Spaces.SpacesInSquareBrackets = true; 10911 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10912 format("auto a = new unique_ptr<int > [10];", Spaces)); 10913 } 10914 10915 TEST_F(FormatTest, ArrayAsTemplateType) { 10916 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10917 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10918 10919 FormatStyle Spaces = getLLVMStyle(); 10920 Spaces.SpacesInSquareBrackets = true; 10921 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10922 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10923 } 10924 10925 // Since this test case uses UNIX-style file path. We disable it for MS 10926 // compiler. 10927 #if !defined(_MSC_VER) && !defined(__MINGW32__) 10928 10929 TEST(FormatStyle, GetStyleOfFile) { 10930 vfs::InMemoryFileSystem FS; 10931 // Test 1: format file in the same directory. 10932 ASSERT_TRUE( 10933 FS.addFile("/a/.clang-format", 0, 10934 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10935 ASSERT_TRUE( 10936 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10937 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10938 ASSERT_EQ(Style1, getLLVMStyle()); 10939 10940 // Test 2: fallback to default. 10941 ASSERT_TRUE( 10942 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10943 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10944 ASSERT_EQ(Style2, getMozillaStyle()); 10945 10946 // Test 3: format file in parent directory. 10947 ASSERT_TRUE( 10948 FS.addFile("/c/.clang-format", 0, 10949 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10950 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10951 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10952 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10953 ASSERT_EQ(Style3, getGoogleStyle()); 10954 } 10955 10956 #endif // _MSC_VER 10957 10958 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 10959 // Column limit is 20. 10960 std::string Code = "Type *a =\n" 10961 " new Type();\n" 10962 "g(iiiii, 0, jjjjj,\n" 10963 " 0, kkkkk, 0, mm);\n" 10964 "int bad = format ;"; 10965 std::string Expected = "auto a = new Type();\n" 10966 "g(iiiii, nullptr,\n" 10967 " jjjjj, nullptr,\n" 10968 " kkkkk, nullptr,\n" 10969 " mm);\n" 10970 "int bad = format ;"; 10971 FileID ID = Context.createInMemoryFile("format.cpp", Code); 10972 tooling::Replacements Replaces = toReplacements( 10973 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 10974 "auto "), 10975 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 10976 "nullptr"), 10977 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 10978 "nullptr"), 10979 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 10980 "nullptr")}); 10981 10982 format::FormatStyle Style = format::getLLVMStyle(); 10983 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 10984 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10985 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10986 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10987 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10988 EXPECT_TRUE(static_cast<bool>(Result)); 10989 EXPECT_EQ(Expected, *Result); 10990 } 10991 10992 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 10993 std::string Code = "#include \"a.h\"\n" 10994 "#include \"c.h\"\n" 10995 "\n" 10996 "int main() {\n" 10997 " return 0;\n" 10998 "}"; 10999 std::string Expected = "#include \"a.h\"\n" 11000 "#include \"b.h\"\n" 11001 "#include \"c.h\"\n" 11002 "\n" 11003 "int main() {\n" 11004 " return 0;\n" 11005 "}"; 11006 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11007 tooling::Replacements Replaces = toReplacements( 11008 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11009 "#include \"b.h\"\n")}); 11010 11011 format::FormatStyle Style = format::getLLVMStyle(); 11012 Style.SortIncludes = true; 11013 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11014 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11015 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11016 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11017 EXPECT_TRUE(static_cast<bool>(Result)); 11018 EXPECT_EQ(Expected, *Result); 11019 } 11020 11021 TEST_F(FormatTest, AllignTrailingComments) { 11022 EXPECT_EQ("#define MACRO(V) \\\n" 11023 " V(Rt2) /* one more char */ \\\n" 11024 " V(Rs) /* than here */ \\\n" 11025 "/* comment 3 */\n", 11026 format("#define MACRO(V)\\\n" 11027 "V(Rt2) /* one more char */ \\\n" 11028 "V(Rs) /* than here */ \\\n" 11029 "/* comment 3 */ \\\n", 11030 getLLVMStyleWithColumns(40))); 11031 } 11032 } // end namespace 11033 } // end namespace format 11034 } // end namespace clang 11035