1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/RewriterTestContext.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 namespace clang { 23 namespace format { 24 namespace { 25 26 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 27 28 class FormatTest : public ::testing::Test { 29 protected: 30 enum IncompleteCheck { 31 IC_ExpectComplete, 32 IC_ExpectIncomplete, 33 IC_DoNotCheck 34 }; 35 36 std::string format(llvm::StringRef Code, 37 const FormatStyle &Style = getLLVMStyle(), 38 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 39 DEBUG(llvm::errs() << "---\n"); 40 DEBUG(llvm::errs() << Code << "\n\n"); 41 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 42 bool IncompleteFormat = false; 43 tooling::Replacements Replaces = 44 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 45 if (CheckIncomplete != IC_DoNotCheck) { 46 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 47 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 48 } 49 ReplacementCount = Replaces.size(); 50 std::string Result = applyAllReplacements(Code, Replaces); 51 EXPECT_NE("", Result); 52 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 53 return Result; 54 } 55 56 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 57 FormatStyle Style = getLLVMStyle(); 58 Style.ColumnLimit = ColumnLimit; 59 return Style; 60 } 61 62 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 63 FormatStyle Style = getGoogleStyle(); 64 Style.ColumnLimit = ColumnLimit; 65 return Style; 66 } 67 68 void verifyFormat(llvm::StringRef Code, 69 const FormatStyle &Style = getLLVMStyle()) { 70 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 71 } 72 73 void verifyIncompleteFormat(llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), 76 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 77 } 78 79 void verifyGoogleFormat(llvm::StringRef Code) { 80 verifyFormat(Code, getGoogleStyle()); 81 } 82 83 void verifyIndependentOfContext(llvm::StringRef text) { 84 verifyFormat(text); 85 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 86 } 87 88 /// \brief Verify that clang-format does not crash on the given input. 89 void verifyNoCrash(llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 format(Code, Style, IC_DoNotCheck); 92 } 93 94 int ReplacementCount; 95 }; 96 97 TEST_F(FormatTest, MessUp) { 98 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 99 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 100 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 101 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 102 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 103 } 104 105 //===----------------------------------------------------------------------===// 106 // Basic function tests. 107 //===----------------------------------------------------------------------===// 108 109 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 110 EXPECT_EQ(";", format(";")); 111 } 112 113 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 114 EXPECT_EQ("int i;", format(" int i;")); 115 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 116 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 117 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 118 } 119 120 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 121 EXPECT_EQ("int i;", format("int\ni;")); 122 } 123 124 TEST_F(FormatTest, FormatsNestedBlockStatements) { 125 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 126 } 127 128 TEST_F(FormatTest, FormatsNestedCall) { 129 verifyFormat("Method(f1, f2(f3));"); 130 verifyFormat("Method(f1(f2, f3()));"); 131 verifyFormat("Method(f1(f2, (f3())));"); 132 } 133 134 TEST_F(FormatTest, NestedNameSpecifiers) { 135 verifyFormat("vector<::Type> v;"); 136 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 137 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 138 verifyFormat("bool a = 2 < ::SomeFunction();"); 139 } 140 141 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 142 EXPECT_EQ("if (a) {\n" 143 " f();\n" 144 "}", 145 format("if(a){f();}")); 146 EXPECT_EQ(4, ReplacementCount); 147 EXPECT_EQ("if (a) {\n" 148 " f();\n" 149 "}", 150 format("if (a) {\n" 151 " f();\n" 152 "}")); 153 EXPECT_EQ(0, ReplacementCount); 154 EXPECT_EQ("/*\r\n" 155 "\r\n" 156 "*/\r\n", 157 format("/*\r\n" 158 "\r\n" 159 "*/\r\n")); 160 EXPECT_EQ(0, ReplacementCount); 161 } 162 163 TEST_F(FormatTest, RemovesEmptyLines) { 164 EXPECT_EQ("class C {\n" 165 " int i;\n" 166 "};", 167 format("class C {\n" 168 " int i;\n" 169 "\n" 170 "};")); 171 172 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 173 EXPECT_EQ("namespace N {\n" 174 "\n" 175 "int i;\n" 176 "}", 177 format("namespace N {\n" 178 "\n" 179 "int i;\n" 180 "}", 181 getGoogleStyle())); 182 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 183 "\n" 184 "int i;\n" 185 "}", 186 format("extern /**/ \"C\" /**/ {\n" 187 "\n" 188 "int i;\n" 189 "}", 190 getGoogleStyle())); 191 192 // ...but do keep inlining and removing empty lines for non-block extern "C" 193 // functions. 194 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 195 EXPECT_EQ("extern \"C\" int f() {\n" 196 " int i = 42;\n" 197 " return i;\n" 198 "}", 199 format("extern \"C\" int f() {\n" 200 "\n" 201 " int i = 42;\n" 202 " return i;\n" 203 "}", 204 getGoogleStyle())); 205 206 // Remove empty lines at the beginning and end of blocks. 207 EXPECT_EQ("void f() {\n" 208 "\n" 209 " if (a) {\n" 210 "\n" 211 " f();\n" 212 " }\n" 213 "}", 214 format("void f() {\n" 215 "\n" 216 " if (a) {\n" 217 "\n" 218 " f();\n" 219 "\n" 220 " }\n" 221 "\n" 222 "}", 223 getLLVMStyle())); 224 EXPECT_EQ("void f() {\n" 225 " if (a) {\n" 226 " f();\n" 227 " }\n" 228 "}", 229 format("void f() {\n" 230 "\n" 231 " if (a) {\n" 232 "\n" 233 " f();\n" 234 "\n" 235 " }\n" 236 "\n" 237 "}", 238 getGoogleStyle())); 239 240 // Don't remove empty lines in more complex control statements. 241 EXPECT_EQ("void f() {\n" 242 " if (a) {\n" 243 " f();\n" 244 "\n" 245 " } else if (b) {\n" 246 " f();\n" 247 " }\n" 248 "}", 249 format("void f() {\n" 250 " if (a) {\n" 251 " f();\n" 252 "\n" 253 " } else if (b) {\n" 254 " f();\n" 255 "\n" 256 " }\n" 257 "\n" 258 "}")); 259 260 // FIXME: This is slightly inconsistent. 261 EXPECT_EQ("namespace {\n" 262 "int i;\n" 263 "}", 264 format("namespace {\n" 265 "int i;\n" 266 "\n" 267 "}")); 268 EXPECT_EQ("namespace {\n" 269 "int i;\n" 270 "\n" 271 "} // namespace", 272 format("namespace {\n" 273 "int i;\n" 274 "\n" 275 "} // namespace")); 276 } 277 278 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 279 verifyFormat("x = (a) and (b);"); 280 verifyFormat("x = (a) or (b);"); 281 verifyFormat("x = (a) bitand (b);"); 282 verifyFormat("x = (a) bitor (b);"); 283 verifyFormat("x = (a) not_eq (b);"); 284 verifyFormat("x = (a) and_eq (b);"); 285 verifyFormat("x = (a) or_eq (b);"); 286 verifyFormat("x = (a) xor (b);"); 287 } 288 289 //===----------------------------------------------------------------------===// 290 // Tests for control statements. 291 //===----------------------------------------------------------------------===// 292 293 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 294 verifyFormat("if (true)\n f();\ng();"); 295 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 296 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 297 298 FormatStyle AllowsMergedIf = getLLVMStyle(); 299 AllowsMergedIf.AlignEscapedNewlinesLeft = true; 300 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 301 verifyFormat("if (a)\n" 302 " // comment\n" 303 " f();", 304 AllowsMergedIf); 305 verifyFormat("{\n" 306 " if (a)\n" 307 " label:\n" 308 " f();\n" 309 "}", 310 AllowsMergedIf); 311 verifyFormat("#define A \\\n" 312 " if (a) \\\n" 313 " label: \\\n" 314 " f()", 315 AllowsMergedIf); 316 verifyFormat("if (a)\n" 317 " ;", 318 AllowsMergedIf); 319 verifyFormat("if (a)\n" 320 " if (b) return;", 321 AllowsMergedIf); 322 323 verifyFormat("if (a) // Can't merge this\n" 324 " f();\n", 325 AllowsMergedIf); 326 verifyFormat("if (a) /* still don't merge */\n" 327 " f();", 328 AllowsMergedIf); 329 verifyFormat("if (a) { // Never merge this\n" 330 " f();\n" 331 "}", 332 AllowsMergedIf); 333 verifyFormat("if (a) { /* Never merge this */\n" 334 " f();\n" 335 "}", 336 AllowsMergedIf); 337 338 AllowsMergedIf.ColumnLimit = 14; 339 verifyFormat("if (a) return;", AllowsMergedIf); 340 verifyFormat("if (aaaaaaaaa)\n" 341 " return;", 342 AllowsMergedIf); 343 344 AllowsMergedIf.ColumnLimit = 13; 345 verifyFormat("if (a)\n return;", AllowsMergedIf); 346 } 347 348 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 349 FormatStyle AllowsMergedLoops = getLLVMStyle(); 350 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 351 verifyFormat("while (true) continue;", AllowsMergedLoops); 352 verifyFormat("for (;;) continue;", AllowsMergedLoops); 353 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 354 verifyFormat("while (true)\n" 355 " ;", 356 AllowsMergedLoops); 357 verifyFormat("for (;;)\n" 358 " ;", 359 AllowsMergedLoops); 360 verifyFormat("for (;;)\n" 361 " for (;;) continue;", 362 AllowsMergedLoops); 363 verifyFormat("for (;;) // Can't merge this\n" 364 " continue;", 365 AllowsMergedLoops); 366 verifyFormat("for (;;) /* still don't merge */\n" 367 " continue;", 368 AllowsMergedLoops); 369 } 370 371 TEST_F(FormatTest, FormatShortBracedStatements) { 372 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 373 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 374 375 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 376 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 377 378 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 379 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 380 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 381 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 382 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 383 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 384 verifyFormat("if (true) { //\n" 385 " f();\n" 386 "}", 387 AllowSimpleBracedStatements); 388 verifyFormat("if (true) {\n" 389 " f();\n" 390 " f();\n" 391 "}", 392 AllowSimpleBracedStatements); 393 verifyFormat("if (true) {\n" 394 " f();\n" 395 "} else {\n" 396 " f();\n" 397 "}", 398 AllowSimpleBracedStatements); 399 400 verifyFormat("template <int> struct A2 {\n" 401 " struct B {};\n" 402 "};", 403 AllowSimpleBracedStatements); 404 405 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 406 verifyFormat("if (true) {\n" 407 " f();\n" 408 "}", 409 AllowSimpleBracedStatements); 410 verifyFormat("if (true) {\n" 411 " f();\n" 412 "} else {\n" 413 " f();\n" 414 "}", 415 AllowSimpleBracedStatements); 416 417 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 418 verifyFormat("while (true) {\n" 419 " f();\n" 420 "}", 421 AllowSimpleBracedStatements); 422 verifyFormat("for (;;) {\n" 423 " f();\n" 424 "}", 425 AllowSimpleBracedStatements); 426 } 427 428 TEST_F(FormatTest, ParseIfElse) { 429 verifyFormat("if (true)\n" 430 " if (true)\n" 431 " if (true)\n" 432 " f();\n" 433 " else\n" 434 " g();\n" 435 " else\n" 436 " h();\n" 437 "else\n" 438 " i();"); 439 verifyFormat("if (true)\n" 440 " if (true)\n" 441 " if (true) {\n" 442 " if (true)\n" 443 " f();\n" 444 " } else {\n" 445 " g();\n" 446 " }\n" 447 " else\n" 448 " h();\n" 449 "else {\n" 450 " i();\n" 451 "}"); 452 verifyFormat("void f() {\n" 453 " if (a) {\n" 454 " } else {\n" 455 " }\n" 456 "}"); 457 } 458 459 TEST_F(FormatTest, ElseIf) { 460 verifyFormat("if (a) {\n} else if (b) {\n}"); 461 verifyFormat("if (a)\n" 462 " f();\n" 463 "else if (b)\n" 464 " g();\n" 465 "else\n" 466 " h();"); 467 verifyFormat("if (a) {\n" 468 " f();\n" 469 "}\n" 470 "// or else ..\n" 471 "else {\n" 472 " g()\n" 473 "}"); 474 475 verifyFormat("if (a) {\n" 476 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 478 "}"); 479 verifyFormat("if (a) {\n" 480 "} else if (\n" 481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 482 "}", 483 getLLVMStyleWithColumns(62)); 484 } 485 486 TEST_F(FormatTest, FormatsForLoop) { 487 verifyFormat( 488 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 489 " ++VeryVeryLongLoopVariable)\n" 490 " ;"); 491 verifyFormat("for (;;)\n" 492 " f();"); 493 verifyFormat("for (;;) {\n}"); 494 verifyFormat("for (;;) {\n" 495 " f();\n" 496 "}"); 497 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 498 499 verifyFormat( 500 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 501 " E = UnwrappedLines.end();\n" 502 " I != E; ++I) {\n}"); 503 504 verifyFormat( 505 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 506 " ++IIIII) {\n}"); 507 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 508 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 509 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 510 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 511 " I = FD->getDeclsInPrototypeScope().begin(),\n" 512 " E = FD->getDeclsInPrototypeScope().end();\n" 513 " I != E; ++I) {\n}"); 514 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 515 " I = Container.begin(),\n" 516 " E = Container.end();\n" 517 " I != E; ++I) {\n}", 518 getLLVMStyleWithColumns(76)); 519 520 verifyFormat( 521 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 525 " ++aaaaaaaaaaa) {\n}"); 526 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 527 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 528 " ++i) {\n}"); 529 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 530 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 531 "}"); 532 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 533 " aaaaaaaaaa);\n" 534 " iter; ++iter) {\n" 535 "}"); 536 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 538 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 539 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 540 541 FormatStyle NoBinPacking = getLLVMStyle(); 542 NoBinPacking.BinPackParameters = false; 543 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 544 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 545 " aaaaaaaaaaaaaaaa,\n" 546 " aaaaaaaaaaaaaaaa,\n" 547 " aaaaaaaaaaaaaaaa);\n" 548 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 549 "}", 550 NoBinPacking); 551 verifyFormat( 552 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 553 " E = UnwrappedLines.end();\n" 554 " I != E;\n" 555 " ++I) {\n}", 556 NoBinPacking); 557 } 558 559 TEST_F(FormatTest, RangeBasedForLoops) { 560 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 562 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 563 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 564 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 566 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 567 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 568 } 569 570 TEST_F(FormatTest, ForEachLoops) { 571 verifyFormat("void f() {\n" 572 " foreach (Item *item, itemlist) {}\n" 573 " Q_FOREACH (Item *item, itemlist) {}\n" 574 " BOOST_FOREACH (Item *item, itemlist) {}\n" 575 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 576 "}"); 577 578 // As function-like macros. 579 verifyFormat("#define foreach(x, y)\n" 580 "#define Q_FOREACH(x, y)\n" 581 "#define BOOST_FOREACH(x, y)\n" 582 "#define UNKNOWN_FOREACH(x, y)\n"); 583 584 // Not as function-like macros. 585 verifyFormat("#define foreach (x, y)\n" 586 "#define Q_FOREACH (x, y)\n" 587 "#define BOOST_FOREACH (x, y)\n" 588 "#define UNKNOWN_FOREACH (x, y)\n"); 589 } 590 591 TEST_F(FormatTest, FormatsWhileLoop) { 592 verifyFormat("while (true) {\n}"); 593 verifyFormat("while (true)\n" 594 " f();"); 595 verifyFormat("while () {\n}"); 596 verifyFormat("while () {\n" 597 " f();\n" 598 "}"); 599 } 600 601 TEST_F(FormatTest, FormatsDoWhile) { 602 verifyFormat("do {\n" 603 " do_something();\n" 604 "} while (something());"); 605 verifyFormat("do\n" 606 " do_something();\n" 607 "while (something());"); 608 } 609 610 TEST_F(FormatTest, FormatsSwitchStatement) { 611 verifyFormat("switch (x) {\n" 612 "case 1:\n" 613 " f();\n" 614 " break;\n" 615 "case kFoo:\n" 616 "case ns::kBar:\n" 617 "case kBaz:\n" 618 " break;\n" 619 "default:\n" 620 " g();\n" 621 " break;\n" 622 "}"); 623 verifyFormat("switch (x) {\n" 624 "case 1: {\n" 625 " f();\n" 626 " break;\n" 627 "}\n" 628 "case 2: {\n" 629 " break;\n" 630 "}\n" 631 "}"); 632 verifyFormat("switch (x) {\n" 633 "case 1: {\n" 634 " f();\n" 635 " {\n" 636 " g();\n" 637 " h();\n" 638 " }\n" 639 " break;\n" 640 "}\n" 641 "}"); 642 verifyFormat("switch (x) {\n" 643 "case 1: {\n" 644 " f();\n" 645 " if (foo) {\n" 646 " g();\n" 647 " h();\n" 648 " }\n" 649 " break;\n" 650 "}\n" 651 "}"); 652 verifyFormat("switch (x) {\n" 653 "case 1: {\n" 654 " f();\n" 655 " g();\n" 656 "} break;\n" 657 "}"); 658 verifyFormat("switch (test)\n" 659 " ;"); 660 verifyFormat("switch (x) {\n" 661 "default: {\n" 662 " // Do nothing.\n" 663 "}\n" 664 "}"); 665 verifyFormat("switch (x) {\n" 666 "// comment\n" 667 "// if 1, do f()\n" 668 "case 1:\n" 669 " f();\n" 670 "}"); 671 verifyFormat("switch (x) {\n" 672 "case 1:\n" 673 " // Do amazing stuff\n" 674 " {\n" 675 " f();\n" 676 " g();\n" 677 " }\n" 678 " break;\n" 679 "}"); 680 verifyFormat("#define A \\\n" 681 " switch (x) { \\\n" 682 " case a: \\\n" 683 " foo = b; \\\n" 684 " }", 685 getLLVMStyleWithColumns(20)); 686 verifyFormat("#define OPERATION_CASE(name) \\\n" 687 " case OP_name: \\\n" 688 " return operations::Operation##name\n", 689 getLLVMStyleWithColumns(40)); 690 verifyFormat("switch (x) {\n" 691 "case 1:;\n" 692 "default:;\n" 693 " int i;\n" 694 "}"); 695 696 verifyGoogleFormat("switch (x) {\n" 697 " case 1:\n" 698 " f();\n" 699 " break;\n" 700 " case kFoo:\n" 701 " case ns::kBar:\n" 702 " case kBaz:\n" 703 " break;\n" 704 " default:\n" 705 " g();\n" 706 " break;\n" 707 "}"); 708 verifyGoogleFormat("switch (x) {\n" 709 " case 1: {\n" 710 " f();\n" 711 " break;\n" 712 " }\n" 713 "}"); 714 verifyGoogleFormat("switch (test)\n" 715 " ;"); 716 717 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 718 " case OP_name: \\\n" 719 " return operations::Operation##name\n"); 720 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 721 " // Get the correction operation class.\n" 722 " switch (OpCode) {\n" 723 " CASE(Add);\n" 724 " CASE(Subtract);\n" 725 " default:\n" 726 " return operations::Unknown;\n" 727 " }\n" 728 "#undef OPERATION_CASE\n" 729 "}"); 730 verifyFormat("DEBUG({\n" 731 " switch (x) {\n" 732 " case A:\n" 733 " f();\n" 734 " break;\n" 735 " // On B:\n" 736 " case B:\n" 737 " g();\n" 738 " break;\n" 739 " }\n" 740 "});"); 741 verifyFormat("switch (a) {\n" 742 "case (b):\n" 743 " return;\n" 744 "}"); 745 746 verifyFormat("switch (a) {\n" 747 "case some_namespace::\n" 748 " some_constant:\n" 749 " return;\n" 750 "}", 751 getLLVMStyleWithColumns(34)); 752 } 753 754 TEST_F(FormatTest, CaseRanges) { 755 verifyFormat("switch (x) {\n" 756 "case 'A' ... 'Z':\n" 757 "case 1 ... 5:\n" 758 "case a ... b:\n" 759 " break;\n" 760 "}"); 761 } 762 763 TEST_F(FormatTest, ShortCaseLabels) { 764 FormatStyle Style = getLLVMStyle(); 765 Style.AllowShortCaseLabelsOnASingleLine = true; 766 verifyFormat("switch (a) {\n" 767 "case 1: x = 1; break;\n" 768 "case 2: return;\n" 769 "case 3:\n" 770 "case 4:\n" 771 "case 5: return;\n" 772 "case 6: // comment\n" 773 " return;\n" 774 "case 7:\n" 775 " // comment\n" 776 " return;\n" 777 "case 8:\n" 778 " x = 8; // comment\n" 779 " break;\n" 780 "default: y = 1; break;\n" 781 "}", 782 Style); 783 verifyFormat("switch (a) {\n" 784 "#if FOO\n" 785 "case 0: return 0;\n" 786 "#endif\n" 787 "}", 788 Style); 789 verifyFormat("switch (a) {\n" 790 "case 1: {\n" 791 "}\n" 792 "case 2: {\n" 793 " return;\n" 794 "}\n" 795 "case 3: {\n" 796 " x = 1;\n" 797 " return;\n" 798 "}\n" 799 "case 4:\n" 800 " if (x)\n" 801 " return;\n" 802 "}", 803 Style); 804 Style.ColumnLimit = 21; 805 verifyFormat("switch (a) {\n" 806 "case 1: x = 1; break;\n" 807 "case 2: return;\n" 808 "case 3:\n" 809 "case 4:\n" 810 "case 5: return;\n" 811 "default:\n" 812 " y = 1;\n" 813 " break;\n" 814 "}", 815 Style); 816 } 817 818 TEST_F(FormatTest, FormatsLabels) { 819 verifyFormat("void f() {\n" 820 " some_code();\n" 821 "test_label:\n" 822 " some_other_code();\n" 823 " {\n" 824 " some_more_code();\n" 825 " another_label:\n" 826 " some_more_code();\n" 827 " }\n" 828 "}"); 829 verifyFormat("{\n" 830 " some_code();\n" 831 "test_label:\n" 832 " some_other_code();\n" 833 "}"); 834 verifyFormat("{\n" 835 " some_code();\n" 836 "test_label:;\n" 837 " int i = 0;\n" 838 "}"); 839 } 840 841 //===----------------------------------------------------------------------===// 842 // Tests for comments. 843 //===----------------------------------------------------------------------===// 844 845 TEST_F(FormatTest, UnderstandsSingleLineComments) { 846 verifyFormat("//* */"); 847 verifyFormat("// line 1\n" 848 "// line 2\n" 849 "void f() {}\n"); 850 851 verifyFormat("void f() {\n" 852 " // Doesn't do anything\n" 853 "}"); 854 verifyFormat("SomeObject\n" 855 " // Calling someFunction on SomeObject\n" 856 " .someFunction();"); 857 verifyFormat("auto result = SomeObject\n" 858 " // Calling someFunction on SomeObject\n" 859 " .someFunction();"); 860 verifyFormat("void f(int i, // some comment (probably for i)\n" 861 " int j, // some comment (probably for j)\n" 862 " int k); // some comment (probably for k)"); 863 verifyFormat("void f(int i,\n" 864 " // some comment (probably for j)\n" 865 " int j,\n" 866 " // some comment (probably for k)\n" 867 " int k);"); 868 869 verifyFormat("int i // This is a fancy variable\n" 870 " = 5; // with nicely aligned comment."); 871 872 verifyFormat("// Leading comment.\n" 873 "int a; // Trailing comment."); 874 verifyFormat("int a; // Trailing comment\n" 875 " // on 2\n" 876 " // or 3 lines.\n" 877 "int b;"); 878 verifyFormat("int a; // Trailing comment\n" 879 "\n" 880 "// Leading comment.\n" 881 "int b;"); 882 verifyFormat("int a; // Comment.\n" 883 " // More details.\n" 884 "int bbbb; // Another comment."); 885 verifyFormat( 886 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 887 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 888 "int cccccccccccccccccccccccccccccc; // comment\n" 889 "int ddd; // looooooooooooooooooooooooong comment\n" 890 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 891 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 892 "int ccccccccccccccccccc; // comment"); 893 894 verifyFormat("#include \"a\" // comment\n" 895 "#include \"a/b/c\" // comment"); 896 verifyFormat("#include <a> // comment\n" 897 "#include <a/b/c> // comment"); 898 EXPECT_EQ("#include \"a\" // comment\n" 899 "#include \"a/b/c\" // comment", 900 format("#include \\\n" 901 " \"a\" // comment\n" 902 "#include \"a/b/c\" // comment")); 903 904 verifyFormat("enum E {\n" 905 " // comment\n" 906 " VAL_A, // comment\n" 907 " VAL_B\n" 908 "};"); 909 910 verifyFormat( 911 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 912 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 913 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 914 " // Comment inside a statement.\n" 915 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 916 verifyFormat("SomeFunction(a,\n" 917 " // comment\n" 918 " b + x);"); 919 verifyFormat("SomeFunction(a, a,\n" 920 " // comment\n" 921 " b + x);"); 922 verifyFormat( 923 "bool aaaaaaaaaaaaa = // comment\n" 924 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 926 927 verifyFormat("int aaaa; // aaaaa\n" 928 "int aa; // aaaaaaa", 929 getLLVMStyleWithColumns(20)); 930 931 EXPECT_EQ("void f() { // This does something ..\n" 932 "}\n" 933 "int a; // This is unrelated", 934 format("void f() { // This does something ..\n" 935 " }\n" 936 "int a; // This is unrelated")); 937 EXPECT_EQ("class C {\n" 938 " void f() { // This does something ..\n" 939 " } // awesome..\n" 940 "\n" 941 " int a; // This is unrelated\n" 942 "};", 943 format("class C{void f() { // This does something ..\n" 944 " } // awesome..\n" 945 " \n" 946 "int a; // This is unrelated\n" 947 "};")); 948 949 EXPECT_EQ("int i; // single line trailing comment", 950 format("int i;\\\n// single line trailing comment")); 951 952 verifyGoogleFormat("int a; // Trailing comment."); 953 954 verifyFormat("someFunction(anotherFunction( // Force break.\n" 955 " parameter));"); 956 957 verifyGoogleFormat("#endif // HEADER_GUARD"); 958 959 verifyFormat("const char *test[] = {\n" 960 " // A\n" 961 " \"aaaa\",\n" 962 " // B\n" 963 " \"aaaaa\"};"); 964 verifyGoogleFormat( 965 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 966 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 967 EXPECT_EQ("D(a, {\n" 968 " // test\n" 969 " int a;\n" 970 "});", 971 format("D(a, {\n" 972 "// test\n" 973 "int a;\n" 974 "});")); 975 976 EXPECT_EQ("lineWith(); // comment\n" 977 "// at start\n" 978 "otherLine();", 979 format("lineWith(); // comment\n" 980 "// at start\n" 981 "otherLine();")); 982 EXPECT_EQ("lineWith(); // comment\n" 983 "/*\n" 984 " * at start */\n" 985 "otherLine();", 986 format("lineWith(); // comment\n" 987 "/*\n" 988 " * at start */\n" 989 "otherLine();")); 990 EXPECT_EQ("lineWith(); // comment\n" 991 " // at start\n" 992 "otherLine();", 993 format("lineWith(); // comment\n" 994 " // at start\n" 995 "otherLine();")); 996 997 EXPECT_EQ("lineWith(); // comment\n" 998 "// at start\n" 999 "otherLine(); // comment", 1000 format("lineWith(); // comment\n" 1001 "// at start\n" 1002 "otherLine(); // comment")); 1003 EXPECT_EQ("lineWith();\n" 1004 "// at start\n" 1005 "otherLine(); // comment", 1006 format("lineWith();\n" 1007 " // at start\n" 1008 "otherLine(); // comment")); 1009 EXPECT_EQ("// first\n" 1010 "// at start\n" 1011 "otherLine(); // comment", 1012 format("// first\n" 1013 " // at start\n" 1014 "otherLine(); // comment")); 1015 EXPECT_EQ("f();\n" 1016 "// first\n" 1017 "// at start\n" 1018 "otherLine(); // comment", 1019 format("f();\n" 1020 "// first\n" 1021 " // at start\n" 1022 "otherLine(); // comment")); 1023 verifyFormat("f(); // comment\n" 1024 "// first\n" 1025 "// at start\n" 1026 "otherLine();"); 1027 EXPECT_EQ("f(); // comment\n" 1028 "// first\n" 1029 "// at start\n" 1030 "otherLine();", 1031 format("f(); // comment\n" 1032 "// first\n" 1033 " // at start\n" 1034 "otherLine();")); 1035 EXPECT_EQ("f(); // comment\n" 1036 " // first\n" 1037 "// at start\n" 1038 "otherLine();", 1039 format("f(); // comment\n" 1040 " // first\n" 1041 "// at start\n" 1042 "otherLine();")); 1043 EXPECT_EQ("void f() {\n" 1044 " lineWith(); // comment\n" 1045 " // at start\n" 1046 "}", 1047 format("void f() {\n" 1048 " lineWith(); // comment\n" 1049 " // at start\n" 1050 "}")); 1051 EXPECT_EQ("int xy; // a\n" 1052 "int z; // b", 1053 format("int xy; // a\n" 1054 "int z; //b")); 1055 EXPECT_EQ("int xy; // a\n" 1056 "int z; // bb", 1057 format("int xy; // a\n" 1058 "int z; //bb", 1059 getLLVMStyleWithColumns(12))); 1060 1061 verifyFormat("#define A \\\n" 1062 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1063 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1064 getLLVMStyleWithColumns(60)); 1065 verifyFormat( 1066 "#define A \\\n" 1067 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1068 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1069 getLLVMStyleWithColumns(61)); 1070 1071 verifyFormat("if ( // This is some comment\n" 1072 " x + 3) {\n" 1073 "}"); 1074 EXPECT_EQ("if ( // This is some comment\n" 1075 " // spanning two lines\n" 1076 " x + 3) {\n" 1077 "}", 1078 format("if( // This is some comment\n" 1079 " // spanning two lines\n" 1080 " x + 3) {\n" 1081 "}")); 1082 1083 verifyNoCrash("/\\\n/"); 1084 verifyNoCrash("/\\\n* */"); 1085 // The 0-character somehow makes the lexer return a proper comment. 1086 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1087 } 1088 1089 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1090 EXPECT_EQ("SomeFunction(a,\n" 1091 " b, // comment\n" 1092 " c);", 1093 format("SomeFunction(a,\n" 1094 " b, // comment\n" 1095 " c);")); 1096 EXPECT_EQ("SomeFunction(a, b,\n" 1097 " // comment\n" 1098 " c);", 1099 format("SomeFunction(a,\n" 1100 " b,\n" 1101 " // comment\n" 1102 " c);")); 1103 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1104 " c);", 1105 format("SomeFunction(a, b, // comment (unclear relation)\n" 1106 " c);")); 1107 EXPECT_EQ("SomeFunction(a, // comment\n" 1108 " b,\n" 1109 " c); // comment", 1110 format("SomeFunction(a, // comment\n" 1111 " b,\n" 1112 " c); // comment")); 1113 } 1114 1115 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1116 EXPECT_EQ("// comment", format("// comment ")); 1117 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1118 format("int aaaaaaa, bbbbbbb; // comment ", 1119 getLLVMStyleWithColumns(33))); 1120 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1121 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1122 } 1123 1124 TEST_F(FormatTest, UnderstandsBlockComments) { 1125 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1126 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 1127 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1128 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1129 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1130 "/* Trailing comment for aa... */\n" 1131 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1132 EXPECT_EQ( 1133 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1134 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1135 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1136 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1137 EXPECT_EQ( 1138 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1139 " aaaaaaaaaaaaaaaaaa,\n" 1140 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1141 "}", 1142 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1143 " aaaaaaaaaaaaaaaaaa ,\n" 1144 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1145 "}")); 1146 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 1147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1148 1149 FormatStyle NoBinPacking = getLLVMStyle(); 1150 NoBinPacking.BinPackParameters = false; 1151 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1152 " /* parameter 2 */ aaaaaa,\n" 1153 " /* parameter 3 */ aaaaaa,\n" 1154 " /* parameter 4 */ aaaaaa);", 1155 NoBinPacking); 1156 1157 // Aligning block comments in macros. 1158 verifyGoogleFormat("#define A \\\n" 1159 " int i; /*a*/ \\\n" 1160 " int jjj; /*b*/"); 1161 } 1162 1163 TEST_F(FormatTest, AlignsBlockComments) { 1164 EXPECT_EQ("/*\n" 1165 " * Really multi-line\n" 1166 " * comment.\n" 1167 " */\n" 1168 "void f() {}", 1169 format(" /*\n" 1170 " * Really multi-line\n" 1171 " * comment.\n" 1172 " */\n" 1173 " void f() {}")); 1174 EXPECT_EQ("class C {\n" 1175 " /*\n" 1176 " * Another multi-line\n" 1177 " * comment.\n" 1178 " */\n" 1179 " void f() {}\n" 1180 "};", 1181 format("class C {\n" 1182 "/*\n" 1183 " * Another multi-line\n" 1184 " * comment.\n" 1185 " */\n" 1186 "void f() {}\n" 1187 "};")); 1188 EXPECT_EQ("/*\n" 1189 " 1. This is a comment with non-trivial formatting.\n" 1190 " 1.1. We have to indent/outdent all lines equally\n" 1191 " 1.1.1. to keep the formatting.\n" 1192 " */", 1193 format(" /*\n" 1194 " 1. This is a comment with non-trivial formatting.\n" 1195 " 1.1. We have to indent/outdent all lines equally\n" 1196 " 1.1.1. to keep the formatting.\n" 1197 " */")); 1198 EXPECT_EQ("/*\n" 1199 "Don't try to outdent if there's not enough indentation.\n" 1200 "*/", 1201 format(" /*\n" 1202 " Don't try to outdent if there's not enough indentation.\n" 1203 " */")); 1204 1205 EXPECT_EQ("int i; /* Comment with empty...\n" 1206 " *\n" 1207 " * line. */", 1208 format("int i; /* Comment with empty...\n" 1209 " *\n" 1210 " * line. */")); 1211 EXPECT_EQ("int foobar = 0; /* comment */\n" 1212 "int bar = 0; /* multiline\n" 1213 " comment 1 */\n" 1214 "int baz = 0; /* multiline\n" 1215 " comment 2 */\n" 1216 "int bzz = 0; /* multiline\n" 1217 " comment 3 */", 1218 format("int foobar = 0; /* comment */\n" 1219 "int bar = 0; /* multiline\n" 1220 " comment 1 */\n" 1221 "int baz = 0; /* multiline\n" 1222 " comment 2 */\n" 1223 "int bzz = 0; /* multiline\n" 1224 " comment 3 */")); 1225 EXPECT_EQ("int foobar = 0; /* comment */\n" 1226 "int bar = 0; /* multiline\n" 1227 " comment */\n" 1228 "int baz = 0; /* multiline\n" 1229 "comment */", 1230 format("int foobar = 0; /* comment */\n" 1231 "int bar = 0; /* multiline\n" 1232 "comment */\n" 1233 "int baz = 0; /* multiline\n" 1234 "comment */")); 1235 } 1236 1237 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1238 FormatStyle Style = getLLVMStyleWithColumns(20); 1239 Style.ReflowComments = false; 1240 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1241 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1242 } 1243 1244 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1245 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1247 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1249 EXPECT_EQ( 1250 "void ffffffffffff(\n" 1251 " int aaaaaaaa, int bbbbbbbb,\n" 1252 " int cccccccccccc) { /*\n" 1253 " aaaaaaaaaa\n" 1254 " aaaaaaaaaaaaa\n" 1255 " bbbbbbbbbbbbbb\n" 1256 " bbbbbbbbbb\n" 1257 " */\n" 1258 "}", 1259 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1260 "{ /*\n" 1261 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1262 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1263 " */\n" 1264 "}", 1265 getLLVMStyleWithColumns(40))); 1266 } 1267 1268 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1269 EXPECT_EQ("void ffffffffff(\n" 1270 " int aaaaa /* test */);", 1271 format("void ffffffffff(int aaaaa /* test */);", 1272 getLLVMStyleWithColumns(35))); 1273 } 1274 1275 TEST_F(FormatTest, SplitsLongCxxComments) { 1276 EXPECT_EQ("// A comment that\n" 1277 "// doesn't fit on\n" 1278 "// one line", 1279 format("// A comment that doesn't fit on one line", 1280 getLLVMStyleWithColumns(20))); 1281 EXPECT_EQ("/// A comment that\n" 1282 "/// doesn't fit on\n" 1283 "/// one line", 1284 format("/// A comment that doesn't fit on one line", 1285 getLLVMStyleWithColumns(20))); 1286 EXPECT_EQ("//! A comment that\n" 1287 "//! doesn't fit on\n" 1288 "//! one line", 1289 format("//! A comment that doesn't fit on one line", 1290 getLLVMStyleWithColumns(20))); 1291 EXPECT_EQ("// a b c d\n" 1292 "// e f g\n" 1293 "// h i j k", 1294 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1295 EXPECT_EQ( 1296 "// a b c d\n" 1297 "// e f g\n" 1298 "// h i j k", 1299 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1300 EXPECT_EQ("if (true) // A comment that\n" 1301 " // doesn't fit on\n" 1302 " // one line", 1303 format("if (true) // A comment that doesn't fit on one line ", 1304 getLLVMStyleWithColumns(30))); 1305 EXPECT_EQ("// Don't_touch_leading_whitespace", 1306 format("// Don't_touch_leading_whitespace", 1307 getLLVMStyleWithColumns(20))); 1308 EXPECT_EQ("// Add leading\n" 1309 "// whitespace", 1310 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1311 EXPECT_EQ("/// Add leading\n" 1312 "/// whitespace", 1313 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1314 EXPECT_EQ("//! Add leading\n" 1315 "//! whitespace", 1316 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1317 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1318 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1319 "// limit", 1320 format("//Even if it makes the line exceed the column limit", 1321 getLLVMStyleWithColumns(51))); 1322 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1323 1324 EXPECT_EQ("// aa bb cc dd", 1325 format("// aa bb cc dd ", 1326 getLLVMStyleWithColumns(15))); 1327 1328 EXPECT_EQ("// A comment before\n" 1329 "// a macro\n" 1330 "// definition\n" 1331 "#define a b", 1332 format("// A comment before a macro definition\n" 1333 "#define a b", 1334 getLLVMStyleWithColumns(20))); 1335 EXPECT_EQ("void ffffff(\n" 1336 " int aaaaaaaaa, // wwww\n" 1337 " int bbbbbbbbbb, // xxxxxxx\n" 1338 " // yyyyyyyyyy\n" 1339 " int c, int d, int e) {}", 1340 format("void ffffff(\n" 1341 " int aaaaaaaaa, // wwww\n" 1342 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1343 " int c, int d, int e) {}", 1344 getLLVMStyleWithColumns(40))); 1345 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1346 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1347 getLLVMStyleWithColumns(20))); 1348 EXPECT_EQ( 1349 "#define XXX // a b c d\n" 1350 " // e f g h", 1351 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1352 EXPECT_EQ( 1353 "#define XXX // q w e r\n" 1354 " // t y u i", 1355 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1356 } 1357 1358 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1359 EXPECT_EQ("// A comment\n" 1360 "// that doesn't\n" 1361 "// fit on one\n" 1362 "// line", 1363 format("// A comment that doesn't fit on one line", 1364 getLLVMStyleWithColumns(20))); 1365 EXPECT_EQ("/// A comment\n" 1366 "/// that doesn't\n" 1367 "/// fit on one\n" 1368 "/// line", 1369 format("/// A comment that doesn't fit on one line", 1370 getLLVMStyleWithColumns(20))); 1371 } 1372 1373 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1374 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1375 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1376 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1377 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1378 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1379 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1380 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1381 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1382 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1383 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1384 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1385 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1386 getLLVMStyleWithColumns(50))); 1387 // FIXME: One day we might want to implement adjustment of leading whitespace 1388 // of the consecutive lines in this kind of comment: 1389 EXPECT_EQ("double\n" 1390 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1391 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1392 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1393 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1394 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1395 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1396 getLLVMStyleWithColumns(49))); 1397 } 1398 1399 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1400 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1401 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1402 EXPECT_EQ( 1403 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1404 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1405 EXPECT_EQ( 1406 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1407 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1408 } 1409 1410 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1411 EXPECT_EQ("if (xxx ==\n" 1412 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1413 " zzz)\n" 1414 " q();", 1415 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1416 " zzz) q();", 1417 getLLVMStyleWithColumns(40))); 1418 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1419 " yyy && // aaaaaa bbbbbbbb cccc\n" 1420 " zzz)\n" 1421 " q();", 1422 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1423 " zzz) q();", 1424 getLLVMStyleWithColumns(40))); 1425 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1426 " yyy || // aaaaaa bbbbbbbb cccc\n" 1427 " zzz)\n" 1428 " q();", 1429 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1430 " zzz) q();", 1431 getLLVMStyleWithColumns(40))); 1432 EXPECT_EQ("fffffffff(\n" 1433 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1434 " zzz);", 1435 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1436 " zzz);", 1437 getLLVMStyleWithColumns(40))); 1438 } 1439 1440 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1441 EXPECT_EQ("#define A(x) /* \\\n" 1442 " a comment \\\n" 1443 " inside */ \\\n" 1444 " f();", 1445 format("#define A(x) /* \\\n" 1446 " a comment \\\n" 1447 " inside */ \\\n" 1448 " f();", 1449 getLLVMStyleWithColumns(17))); 1450 EXPECT_EQ("#define A( \\\n" 1451 " x) /* \\\n" 1452 " a comment \\\n" 1453 " inside */ \\\n" 1454 " f();", 1455 format("#define A( \\\n" 1456 " x) /* \\\n" 1457 " a comment \\\n" 1458 " inside */ \\\n" 1459 " f();", 1460 getLLVMStyleWithColumns(17))); 1461 } 1462 1463 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1464 EXPECT_EQ("namespace {}\n// Test\n#define A", 1465 format("namespace {}\n // Test\n#define A")); 1466 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1467 format("namespace {}\n /* Test */\n#define A")); 1468 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1469 format("namespace {}\n /* Test */ #define A")); 1470 } 1471 1472 TEST_F(FormatTest, SplitsLongLinesInComments) { 1473 EXPECT_EQ("/* This is a long\n" 1474 " * comment that\n" 1475 " * doesn't\n" 1476 " * fit on one line.\n" 1477 " */", 1478 format("/* " 1479 "This is a long " 1480 "comment that " 1481 "doesn't " 1482 "fit on one line. */", 1483 getLLVMStyleWithColumns(20))); 1484 EXPECT_EQ( 1485 "/* a b c d\n" 1486 " * e f g\n" 1487 " * h i j k\n" 1488 " */", 1489 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1490 EXPECT_EQ( 1491 "/* a b c d\n" 1492 " * e f g\n" 1493 " * h i j k\n" 1494 " */", 1495 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1496 EXPECT_EQ("/*\n" 1497 "This is a long\n" 1498 "comment that doesn't\n" 1499 "fit on one line.\n" 1500 "*/", 1501 format("/*\n" 1502 "This is a long " 1503 "comment that doesn't " 1504 "fit on one line. \n" 1505 "*/", 1506 getLLVMStyleWithColumns(20))); 1507 EXPECT_EQ("/*\n" 1508 " * This is a long\n" 1509 " * comment that\n" 1510 " * doesn't fit on\n" 1511 " * one line.\n" 1512 " */", 1513 format("/* \n" 1514 " * This is a long " 1515 " comment that " 1516 " doesn't fit on " 1517 " one line. \n" 1518 " */", 1519 getLLVMStyleWithColumns(20))); 1520 EXPECT_EQ("/*\n" 1521 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1522 " * so_it_should_be_broken\n" 1523 " * wherever_a_space_occurs\n" 1524 " */", 1525 format("/*\n" 1526 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1527 " so_it_should_be_broken " 1528 " wherever_a_space_occurs \n" 1529 " */", 1530 getLLVMStyleWithColumns(20))); 1531 EXPECT_EQ("/*\n" 1532 " * This_comment_can_not_be_broken_into_lines\n" 1533 " */", 1534 format("/*\n" 1535 " * This_comment_can_not_be_broken_into_lines\n" 1536 " */", 1537 getLLVMStyleWithColumns(20))); 1538 EXPECT_EQ("{\n" 1539 " /*\n" 1540 " This is another\n" 1541 " long comment that\n" 1542 " doesn't fit on one\n" 1543 " line 1234567890\n" 1544 " */\n" 1545 "}", 1546 format("{\n" 1547 "/*\n" 1548 "This is another " 1549 " long comment that " 1550 " doesn't fit on one" 1551 " line 1234567890\n" 1552 "*/\n" 1553 "}", 1554 getLLVMStyleWithColumns(20))); 1555 EXPECT_EQ("{\n" 1556 " /*\n" 1557 " * This i s\n" 1558 " * another comment\n" 1559 " * t hat doesn' t\n" 1560 " * fit on one l i\n" 1561 " * n e\n" 1562 " */\n" 1563 "}", 1564 format("{\n" 1565 "/*\n" 1566 " * This i s" 1567 " another comment" 1568 " t hat doesn' t" 1569 " fit on one l i" 1570 " n e\n" 1571 " */\n" 1572 "}", 1573 getLLVMStyleWithColumns(20))); 1574 EXPECT_EQ("/*\n" 1575 " * This is a long\n" 1576 " * comment that\n" 1577 " * doesn't fit on\n" 1578 " * one line\n" 1579 " */", 1580 format(" /*\n" 1581 " * This is a long comment that doesn't fit on one line\n" 1582 " */", 1583 getLLVMStyleWithColumns(20))); 1584 EXPECT_EQ("{\n" 1585 " if (something) /* This is a\n" 1586 " long\n" 1587 " comment */\n" 1588 " ;\n" 1589 "}", 1590 format("{\n" 1591 " if (something) /* This is a long comment */\n" 1592 " ;\n" 1593 "}", 1594 getLLVMStyleWithColumns(30))); 1595 1596 EXPECT_EQ("/* A comment before\n" 1597 " * a macro\n" 1598 " * definition */\n" 1599 "#define a b", 1600 format("/* A comment before a macro definition */\n" 1601 "#define a b", 1602 getLLVMStyleWithColumns(20))); 1603 1604 EXPECT_EQ("/* some comment\n" 1605 " * a comment\n" 1606 "* that we break\n" 1607 " * another comment\n" 1608 "* we have to break\n" 1609 "* a left comment\n" 1610 " */", 1611 format(" /* some comment\n" 1612 " * a comment that we break\n" 1613 " * another comment we have to break\n" 1614 "* a left comment\n" 1615 " */", 1616 getLLVMStyleWithColumns(20))); 1617 1618 EXPECT_EQ("/**\n" 1619 " * multiline block\n" 1620 " * comment\n" 1621 " *\n" 1622 " */", 1623 format("/**\n" 1624 " * multiline block comment\n" 1625 " *\n" 1626 " */", 1627 getLLVMStyleWithColumns(20))); 1628 1629 EXPECT_EQ("/*\n" 1630 "\n" 1631 "\n" 1632 " */\n", 1633 format(" /* \n" 1634 " \n" 1635 " \n" 1636 " */\n")); 1637 1638 EXPECT_EQ("/* a a */", 1639 format("/* a a */", getLLVMStyleWithColumns(15))); 1640 EXPECT_EQ("/* a a bc */", 1641 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1642 EXPECT_EQ("/* aaa aaa\n" 1643 " * aaaaa */", 1644 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1645 EXPECT_EQ("/* aaa aaa\n" 1646 " * aaaaa */", 1647 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1648 } 1649 1650 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1651 EXPECT_EQ("#define X \\\n" 1652 " /* \\\n" 1653 " Test \\\n" 1654 " Macro comment \\\n" 1655 " with a long \\\n" 1656 " line \\\n" 1657 " */ \\\n" 1658 " A + B", 1659 format("#define X \\\n" 1660 " /*\n" 1661 " Test\n" 1662 " Macro comment with a long line\n" 1663 " */ \\\n" 1664 " A + B", 1665 getLLVMStyleWithColumns(20))); 1666 EXPECT_EQ("#define X \\\n" 1667 " /* Macro comment \\\n" 1668 " with a long \\\n" 1669 " line */ \\\n" 1670 " A + B", 1671 format("#define X \\\n" 1672 " /* Macro comment with a long\n" 1673 " line */ \\\n" 1674 " A + B", 1675 getLLVMStyleWithColumns(20))); 1676 EXPECT_EQ("#define X \\\n" 1677 " /* Macro comment \\\n" 1678 " * with a long \\\n" 1679 " * line */ \\\n" 1680 " A + B", 1681 format("#define X \\\n" 1682 " /* Macro comment with a long line */ \\\n" 1683 " A + B", 1684 getLLVMStyleWithColumns(20))); 1685 } 1686 1687 TEST_F(FormatTest, CommentsInStaticInitializers) { 1688 EXPECT_EQ( 1689 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1690 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1691 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1692 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1693 " aaaaaaaaaaaaaaaaaaaa};", 1694 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1695 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1696 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1697 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1698 " aaaaaaaaaaaaaaaaaaaa };")); 1699 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1700 " bbbbbbbbbbb, ccccccccccc};"); 1701 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1702 " // comment for bb....\n" 1703 " bbbbbbbbbbb, ccccccccccc};"); 1704 verifyGoogleFormat( 1705 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1706 " bbbbbbbbbbb, ccccccccccc};"); 1707 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1708 " // comment for bb....\n" 1709 " bbbbbbbbbbb, ccccccccccc};"); 1710 1711 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1712 " {d, e, f}, // Group #2\n" 1713 " {g, h, i}}; // Group #3"); 1714 verifyFormat("S s = {{// Group #1\n" 1715 " a, b, c},\n" 1716 " {// Group #2\n" 1717 " d, e, f},\n" 1718 " {// Group #3\n" 1719 " g, h, i}};"); 1720 1721 EXPECT_EQ("S s = {\n" 1722 " // Some comment\n" 1723 " a,\n" 1724 "\n" 1725 " // Comment after empty line\n" 1726 " b}", 1727 format("S s = {\n" 1728 " // Some comment\n" 1729 " a,\n" 1730 " \n" 1731 " // Comment after empty line\n" 1732 " b\n" 1733 "}")); 1734 EXPECT_EQ("S s = {\n" 1735 " /* Some comment */\n" 1736 " a,\n" 1737 "\n" 1738 " /* Comment after empty line */\n" 1739 " b}", 1740 format("S s = {\n" 1741 " /* Some comment */\n" 1742 " a,\n" 1743 " \n" 1744 " /* Comment after empty line */\n" 1745 " b\n" 1746 "}")); 1747 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1748 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1749 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1750 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1751 } 1752 1753 TEST_F(FormatTest, IgnoresIf0Contents) { 1754 EXPECT_EQ("#if 0\n" 1755 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1756 "#endif\n" 1757 "void f() {}", 1758 format("#if 0\n" 1759 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1760 "#endif\n" 1761 "void f( ) { }")); 1762 EXPECT_EQ("#if false\n" 1763 "void f( ) { }\n" 1764 "#endif\n" 1765 "void g() {}\n", 1766 format("#if false\n" 1767 "void f( ) { }\n" 1768 "#endif\n" 1769 "void g( ) { }\n")); 1770 EXPECT_EQ("enum E {\n" 1771 " One,\n" 1772 " Two,\n" 1773 "#if 0\n" 1774 "Three,\n" 1775 " Four,\n" 1776 "#endif\n" 1777 " Five\n" 1778 "};", 1779 format("enum E {\n" 1780 " One,Two,\n" 1781 "#if 0\n" 1782 "Three,\n" 1783 " Four,\n" 1784 "#endif\n" 1785 " Five};")); 1786 EXPECT_EQ("enum F {\n" 1787 " One,\n" 1788 "#if 1\n" 1789 " Two,\n" 1790 "#if 0\n" 1791 "Three,\n" 1792 " Four,\n" 1793 "#endif\n" 1794 " Five\n" 1795 "#endif\n" 1796 "};", 1797 format("enum F {\n" 1798 "One,\n" 1799 "#if 1\n" 1800 "Two,\n" 1801 "#if 0\n" 1802 "Three,\n" 1803 " Four,\n" 1804 "#endif\n" 1805 "Five\n" 1806 "#endif\n" 1807 "};")); 1808 EXPECT_EQ("enum G {\n" 1809 " One,\n" 1810 "#if 0\n" 1811 "Two,\n" 1812 "#else\n" 1813 " Three,\n" 1814 "#endif\n" 1815 " Four\n" 1816 "};", 1817 format("enum G {\n" 1818 "One,\n" 1819 "#if 0\n" 1820 "Two,\n" 1821 "#else\n" 1822 "Three,\n" 1823 "#endif\n" 1824 "Four\n" 1825 "};")); 1826 EXPECT_EQ("enum H {\n" 1827 " One,\n" 1828 "#if 0\n" 1829 "#ifdef Q\n" 1830 "Two,\n" 1831 "#else\n" 1832 "Three,\n" 1833 "#endif\n" 1834 "#endif\n" 1835 " Four\n" 1836 "};", 1837 format("enum H {\n" 1838 "One,\n" 1839 "#if 0\n" 1840 "#ifdef Q\n" 1841 "Two,\n" 1842 "#else\n" 1843 "Three,\n" 1844 "#endif\n" 1845 "#endif\n" 1846 "Four\n" 1847 "};")); 1848 EXPECT_EQ("enum I {\n" 1849 " One,\n" 1850 "#if /* test */ 0 || 1\n" 1851 "Two,\n" 1852 "Three,\n" 1853 "#endif\n" 1854 " Four\n" 1855 "};", 1856 format("enum I {\n" 1857 "One,\n" 1858 "#if /* test */ 0 || 1\n" 1859 "Two,\n" 1860 "Three,\n" 1861 "#endif\n" 1862 "Four\n" 1863 "};")); 1864 EXPECT_EQ("enum J {\n" 1865 " One,\n" 1866 "#if 0\n" 1867 "#if 0\n" 1868 "Two,\n" 1869 "#else\n" 1870 "Three,\n" 1871 "#endif\n" 1872 "Four,\n" 1873 "#endif\n" 1874 " Five\n" 1875 "};", 1876 format("enum J {\n" 1877 "One,\n" 1878 "#if 0\n" 1879 "#if 0\n" 1880 "Two,\n" 1881 "#else\n" 1882 "Three,\n" 1883 "#endif\n" 1884 "Four,\n" 1885 "#endif\n" 1886 "Five\n" 1887 "};")); 1888 } 1889 1890 //===----------------------------------------------------------------------===// 1891 // Tests for classes, namespaces, etc. 1892 //===----------------------------------------------------------------------===// 1893 1894 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1895 verifyFormat("class A {};"); 1896 } 1897 1898 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1899 verifyFormat("class A {\n" 1900 "public:\n" 1901 "public: // comment\n" 1902 "protected:\n" 1903 "private:\n" 1904 " void f() {}\n" 1905 "};"); 1906 verifyGoogleFormat("class A {\n" 1907 " public:\n" 1908 " protected:\n" 1909 " private:\n" 1910 " void f() {}\n" 1911 "};"); 1912 verifyFormat("class A {\n" 1913 "public slots:\n" 1914 " void f1() {}\n" 1915 "public Q_SLOTS:\n" 1916 " void f2() {}\n" 1917 "protected slots:\n" 1918 " void f3() {}\n" 1919 "protected Q_SLOTS:\n" 1920 " void f4() {}\n" 1921 "private slots:\n" 1922 " void f5() {}\n" 1923 "private Q_SLOTS:\n" 1924 " void f6() {}\n" 1925 "signals:\n" 1926 " void g1();\n" 1927 "Q_SIGNALS:\n" 1928 " void g2();\n" 1929 "};"); 1930 1931 // Don't interpret 'signals' the wrong way. 1932 verifyFormat("signals.set();"); 1933 verifyFormat("for (Signals signals : f()) {\n}"); 1934 verifyFormat("{\n" 1935 " signals.set(); // This needs indentation.\n" 1936 "}"); 1937 } 1938 1939 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1940 EXPECT_EQ("class A {\n" 1941 "public:\n" 1942 " void f();\n" 1943 "\n" 1944 "private:\n" 1945 " void g() {}\n" 1946 " // test\n" 1947 "protected:\n" 1948 " int h;\n" 1949 "};", 1950 format("class A {\n" 1951 "public:\n" 1952 "void f();\n" 1953 "private:\n" 1954 "void g() {}\n" 1955 "// test\n" 1956 "protected:\n" 1957 "int h;\n" 1958 "};")); 1959 EXPECT_EQ("class A {\n" 1960 "protected:\n" 1961 "public:\n" 1962 " void f();\n" 1963 "};", 1964 format("class A {\n" 1965 "protected:\n" 1966 "\n" 1967 "public:\n" 1968 "\n" 1969 " void f();\n" 1970 "};")); 1971 1972 // Even ensure proper spacing inside macros. 1973 EXPECT_EQ("#define B \\\n" 1974 " class A { \\\n" 1975 " protected: \\\n" 1976 " public: \\\n" 1977 " void f(); \\\n" 1978 " };", 1979 format("#define B \\\n" 1980 " class A { \\\n" 1981 " protected: \\\n" 1982 " \\\n" 1983 " public: \\\n" 1984 " \\\n" 1985 " void f(); \\\n" 1986 " };", 1987 getGoogleStyle())); 1988 // But don't remove empty lines after macros ending in access specifiers. 1989 EXPECT_EQ("#define A private:\n" 1990 "\n" 1991 "int i;", 1992 format("#define A private:\n" 1993 "\n" 1994 "int i;")); 1995 } 1996 1997 TEST_F(FormatTest, FormatsClasses) { 1998 verifyFormat("class A : public B {};"); 1999 verifyFormat("class A : public ::B {};"); 2000 2001 verifyFormat( 2002 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2003 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2004 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2005 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2006 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2007 verifyFormat( 2008 "class A : public B, public C, public D, public E, public F {};"); 2009 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2010 " public C,\n" 2011 " public D,\n" 2012 " public E,\n" 2013 " public F,\n" 2014 " public G {};"); 2015 2016 verifyFormat("class\n" 2017 " ReallyReallyLongClassName {\n" 2018 " int i;\n" 2019 "};", 2020 getLLVMStyleWithColumns(32)); 2021 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2022 " aaaaaaaaaaaaaaaa> {};"); 2023 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2024 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2025 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2026 verifyFormat("template <class R, class C>\n" 2027 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2028 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2029 verifyFormat("class ::A::B {};"); 2030 } 2031 2032 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2033 verifyFormat("class A {\n} a, b;"); 2034 verifyFormat("struct A {\n} a, b;"); 2035 verifyFormat("union A {\n} a;"); 2036 } 2037 2038 TEST_F(FormatTest, FormatsEnum) { 2039 verifyFormat("enum {\n" 2040 " Zero,\n" 2041 " One = 1,\n" 2042 " Two = One + 1,\n" 2043 " Three = (One + Two),\n" 2044 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2045 " Five = (One, Two, Three, Four, 5)\n" 2046 "};"); 2047 verifyGoogleFormat("enum {\n" 2048 " Zero,\n" 2049 " One = 1,\n" 2050 " Two = One + 1,\n" 2051 " Three = (One + Two),\n" 2052 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2053 " Five = (One, Two, Three, Four, 5)\n" 2054 "};"); 2055 verifyFormat("enum Enum {};"); 2056 verifyFormat("enum {};"); 2057 verifyFormat("enum X E {} d;"); 2058 verifyFormat("enum __attribute__((...)) E {} d;"); 2059 verifyFormat("enum __declspec__((...)) E {} d;"); 2060 verifyFormat("enum {\n" 2061 " Bar = Foo<int, int>::value\n" 2062 "};", 2063 getLLVMStyleWithColumns(30)); 2064 2065 verifyFormat("enum ShortEnum { A, B, C };"); 2066 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2067 2068 EXPECT_EQ("enum KeepEmptyLines {\n" 2069 " ONE,\n" 2070 "\n" 2071 " TWO,\n" 2072 "\n" 2073 " THREE\n" 2074 "}", 2075 format("enum KeepEmptyLines {\n" 2076 " ONE,\n" 2077 "\n" 2078 " TWO,\n" 2079 "\n" 2080 "\n" 2081 " THREE\n" 2082 "}")); 2083 verifyFormat("enum E { // comment\n" 2084 " ONE,\n" 2085 " TWO\n" 2086 "};\n" 2087 "int i;"); 2088 // Not enums. 2089 verifyFormat("enum X f() {\n" 2090 " a();\n" 2091 " return 42;\n" 2092 "}"); 2093 verifyFormat("enum X Type::f() {\n" 2094 " a();\n" 2095 " return 42;\n" 2096 "}"); 2097 verifyFormat("enum ::X f() {\n" 2098 " a();\n" 2099 " return 42;\n" 2100 "}"); 2101 verifyFormat("enum ns::X f() {\n" 2102 " a();\n" 2103 " return 42;\n" 2104 "}"); 2105 } 2106 2107 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2108 verifyFormat("enum Type {\n" 2109 " One = 0; // These semicolons should be commas.\n" 2110 " Two = 1;\n" 2111 "};"); 2112 verifyFormat("namespace n {\n" 2113 "enum Type {\n" 2114 " One,\n" 2115 " Two, // missing };\n" 2116 " int i;\n" 2117 "}\n" 2118 "void g() {}"); 2119 } 2120 2121 TEST_F(FormatTest, FormatsEnumStruct) { 2122 verifyFormat("enum struct {\n" 2123 " Zero,\n" 2124 " One = 1,\n" 2125 " Two = One + 1,\n" 2126 " Three = (One + Two),\n" 2127 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2128 " Five = (One, Two, Three, Four, 5)\n" 2129 "};"); 2130 verifyFormat("enum struct Enum {};"); 2131 verifyFormat("enum struct {};"); 2132 verifyFormat("enum struct X E {} d;"); 2133 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2134 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2135 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2136 } 2137 2138 TEST_F(FormatTest, FormatsEnumClass) { 2139 verifyFormat("enum class {\n" 2140 " Zero,\n" 2141 " One = 1,\n" 2142 " Two = One + 1,\n" 2143 " Three = (One + Two),\n" 2144 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2145 " Five = (One, Two, Three, Four, 5)\n" 2146 "};"); 2147 verifyFormat("enum class Enum {};"); 2148 verifyFormat("enum class {};"); 2149 verifyFormat("enum class X E {} d;"); 2150 verifyFormat("enum class __attribute__((...)) E {} d;"); 2151 verifyFormat("enum class __declspec__((...)) E {} d;"); 2152 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2153 } 2154 2155 TEST_F(FormatTest, FormatsEnumTypes) { 2156 verifyFormat("enum X : int {\n" 2157 " A, // Force multiple lines.\n" 2158 " B\n" 2159 "};"); 2160 verifyFormat("enum X : int { A, B };"); 2161 verifyFormat("enum X : std::uint32_t { A, B };"); 2162 } 2163 2164 TEST_F(FormatTest, FormatsNSEnums) { 2165 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2166 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2167 " // Information about someDecentlyLongValue.\n" 2168 " someDecentlyLongValue,\n" 2169 " // Information about anotherDecentlyLongValue.\n" 2170 " anotherDecentlyLongValue,\n" 2171 " // Information about aThirdDecentlyLongValue.\n" 2172 " aThirdDecentlyLongValue\n" 2173 "};"); 2174 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2175 " a = 1,\n" 2176 " b = 2,\n" 2177 " c = 3,\n" 2178 "};"); 2179 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2180 " a = 1,\n" 2181 " b = 2,\n" 2182 " c = 3,\n" 2183 "};"); 2184 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2185 " a = 1,\n" 2186 " b = 2,\n" 2187 " c = 3,\n" 2188 "};"); 2189 } 2190 2191 TEST_F(FormatTest, FormatsBitfields) { 2192 verifyFormat("struct Bitfields {\n" 2193 " unsigned sClass : 8;\n" 2194 " unsigned ValueKind : 2;\n" 2195 "};"); 2196 verifyFormat("struct A {\n" 2197 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2198 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2199 "};"); 2200 verifyFormat("struct MyStruct {\n" 2201 " uchar data;\n" 2202 " uchar : 8;\n" 2203 " uchar : 8;\n" 2204 " uchar other;\n" 2205 "};"); 2206 } 2207 2208 TEST_F(FormatTest, FormatsNamespaces) { 2209 verifyFormat("namespace some_namespace {\n" 2210 "class A {};\n" 2211 "void f() { f(); }\n" 2212 "}"); 2213 verifyFormat("namespace {\n" 2214 "class A {};\n" 2215 "void f() { f(); }\n" 2216 "}"); 2217 verifyFormat("inline namespace X {\n" 2218 "class A {};\n" 2219 "void f() { f(); }\n" 2220 "}"); 2221 verifyFormat("using namespace some_namespace;\n" 2222 "class A {};\n" 2223 "void f() { f(); }"); 2224 2225 // This code is more common than we thought; if we 2226 // layout this correctly the semicolon will go into 2227 // its own line, which is undesirable. 2228 verifyFormat("namespace {};"); 2229 verifyFormat("namespace {\n" 2230 "class A {};\n" 2231 "};"); 2232 2233 verifyFormat("namespace {\n" 2234 "int SomeVariable = 0; // comment\n" 2235 "} // namespace"); 2236 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2237 "#define HEADER_GUARD\n" 2238 "namespace my_namespace {\n" 2239 "int i;\n" 2240 "} // my_namespace\n" 2241 "#endif // HEADER_GUARD", 2242 format("#ifndef HEADER_GUARD\n" 2243 " #define HEADER_GUARD\n" 2244 " namespace my_namespace {\n" 2245 "int i;\n" 2246 "} // my_namespace\n" 2247 "#endif // HEADER_GUARD")); 2248 2249 EXPECT_EQ("namespace A::B {\n" 2250 "class C {};\n" 2251 "}", 2252 format("namespace A::B {\n" 2253 "class C {};\n" 2254 "}")); 2255 2256 FormatStyle Style = getLLVMStyle(); 2257 Style.NamespaceIndentation = FormatStyle::NI_All; 2258 EXPECT_EQ("namespace out {\n" 2259 " int i;\n" 2260 " namespace in {\n" 2261 " int i;\n" 2262 " } // namespace\n" 2263 "} // namespace", 2264 format("namespace out {\n" 2265 "int i;\n" 2266 "namespace in {\n" 2267 "int i;\n" 2268 "} // namespace\n" 2269 "} // namespace", 2270 Style)); 2271 2272 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2273 EXPECT_EQ("namespace out {\n" 2274 "int i;\n" 2275 "namespace in {\n" 2276 " int i;\n" 2277 "} // namespace\n" 2278 "} // namespace", 2279 format("namespace out {\n" 2280 "int i;\n" 2281 "namespace in {\n" 2282 "int i;\n" 2283 "} // namespace\n" 2284 "} // namespace", 2285 Style)); 2286 } 2287 2288 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2289 2290 TEST_F(FormatTest, FormatsInlineASM) { 2291 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2292 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2293 verifyFormat( 2294 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2295 " \"cpuid\\n\\t\"\n" 2296 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2297 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2298 " : \"a\"(value));"); 2299 EXPECT_EQ( 2300 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2301 " __asm {\n" 2302 " mov edx,[that] // vtable in edx\n" 2303 " mov eax,methodIndex\n" 2304 " call [edx][eax*4] // stdcall\n" 2305 " }\n" 2306 "}", 2307 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2308 " __asm {\n" 2309 " mov edx,[that] // vtable in edx\n" 2310 " mov eax,methodIndex\n" 2311 " call [edx][eax*4] // stdcall\n" 2312 " }\n" 2313 "}")); 2314 EXPECT_EQ("_asm {\n" 2315 " xor eax, eax;\n" 2316 " cpuid;\n" 2317 "}", 2318 format("_asm {\n" 2319 " xor eax, eax;\n" 2320 " cpuid;\n" 2321 "}")); 2322 verifyFormat("void function() {\n" 2323 " // comment\n" 2324 " asm(\"\");\n" 2325 "}"); 2326 EXPECT_EQ("__asm {\n" 2327 "}\n" 2328 "int i;", 2329 format("__asm {\n" 2330 "}\n" 2331 "int i;")); 2332 } 2333 2334 TEST_F(FormatTest, FormatTryCatch) { 2335 verifyFormat("try {\n" 2336 " throw a * b;\n" 2337 "} catch (int a) {\n" 2338 " // Do nothing.\n" 2339 "} catch (...) {\n" 2340 " exit(42);\n" 2341 "}"); 2342 2343 // Function-level try statements. 2344 verifyFormat("int f() try { return 4; } catch (...) {\n" 2345 " return 5;\n" 2346 "}"); 2347 verifyFormat("class A {\n" 2348 " int a;\n" 2349 " A() try : a(0) {\n" 2350 " } catch (...) {\n" 2351 " throw;\n" 2352 " }\n" 2353 "};\n"); 2354 2355 // Incomplete try-catch blocks. 2356 verifyIncompleteFormat("try {} catch ("); 2357 } 2358 2359 TEST_F(FormatTest, FormatSEHTryCatch) { 2360 verifyFormat("__try {\n" 2361 " int a = b * c;\n" 2362 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2363 " // Do nothing.\n" 2364 "}"); 2365 2366 verifyFormat("__try {\n" 2367 " int a = b * c;\n" 2368 "} __finally {\n" 2369 " // Do nothing.\n" 2370 "}"); 2371 2372 verifyFormat("DEBUG({\n" 2373 " __try {\n" 2374 " } __finally {\n" 2375 " }\n" 2376 "});\n"); 2377 } 2378 2379 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2380 verifyFormat("try {\n" 2381 " f();\n" 2382 "} catch {\n" 2383 " g();\n" 2384 "}"); 2385 verifyFormat("try {\n" 2386 " f();\n" 2387 "} catch (A a) MACRO(x) {\n" 2388 " g();\n" 2389 "} catch (B b) MACRO(x) {\n" 2390 " g();\n" 2391 "}"); 2392 } 2393 2394 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2395 FormatStyle Style = getLLVMStyle(); 2396 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2397 FormatStyle::BS_WebKit}) { 2398 Style.BreakBeforeBraces = BraceStyle; 2399 verifyFormat("try {\n" 2400 " // something\n" 2401 "} catch (...) {\n" 2402 " // something\n" 2403 "}", 2404 Style); 2405 } 2406 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2407 verifyFormat("try {\n" 2408 " // something\n" 2409 "}\n" 2410 "catch (...) {\n" 2411 " // something\n" 2412 "}", 2413 Style); 2414 verifyFormat("__try {\n" 2415 " // something\n" 2416 "}\n" 2417 "__finally {\n" 2418 " // something\n" 2419 "}", 2420 Style); 2421 verifyFormat("@try {\n" 2422 " // something\n" 2423 "}\n" 2424 "@finally {\n" 2425 " // something\n" 2426 "}", 2427 Style); 2428 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2429 verifyFormat("try\n" 2430 "{\n" 2431 " // something\n" 2432 "}\n" 2433 "catch (...)\n" 2434 "{\n" 2435 " // something\n" 2436 "}", 2437 Style); 2438 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2439 verifyFormat("try\n" 2440 " {\n" 2441 " // something\n" 2442 " }\n" 2443 "catch (...)\n" 2444 " {\n" 2445 " // something\n" 2446 " }", 2447 Style); 2448 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2449 Style.BraceWrapping.BeforeCatch = true; 2450 verifyFormat("try {\n" 2451 " // something\n" 2452 "}\n" 2453 "catch (...) {\n" 2454 " // something\n" 2455 "}", 2456 Style); 2457 } 2458 2459 TEST_F(FormatTest, FormatObjCTryCatch) { 2460 verifyFormat("@try {\n" 2461 " f();\n" 2462 "} @catch (NSException e) {\n" 2463 " @throw;\n" 2464 "} @finally {\n" 2465 " exit(42);\n" 2466 "}"); 2467 verifyFormat("DEBUG({\n" 2468 " @try {\n" 2469 " } @finally {\n" 2470 " }\n" 2471 "});\n"); 2472 } 2473 2474 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2475 FormatStyle Style = getLLVMStyle(); 2476 verifyFormat("@autoreleasepool {\n" 2477 " f();\n" 2478 "}\n" 2479 "@autoreleasepool {\n" 2480 " f();\n" 2481 "}\n", 2482 Style); 2483 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2484 verifyFormat("@autoreleasepool\n" 2485 "{\n" 2486 " f();\n" 2487 "}\n" 2488 "@autoreleasepool\n" 2489 "{\n" 2490 " f();\n" 2491 "}\n", 2492 Style); 2493 } 2494 2495 TEST_F(FormatTest, StaticInitializers) { 2496 verifyFormat("static SomeClass SC = {1, 'a'};"); 2497 2498 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2499 " 100000000, " 2500 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2501 2502 // Here, everything other than the "}" would fit on a line. 2503 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2504 " 10000000000000000000000000};"); 2505 EXPECT_EQ("S s = {a,\n" 2506 "\n" 2507 " b};", 2508 format("S s = {\n" 2509 " a,\n" 2510 "\n" 2511 " b\n" 2512 "};")); 2513 2514 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2515 // line. However, the formatting looks a bit off and this probably doesn't 2516 // happen often in practice. 2517 verifyFormat("static int Variable[1] = {\n" 2518 " {1000000000000000000000000000000000000}};", 2519 getLLVMStyleWithColumns(40)); 2520 } 2521 2522 TEST_F(FormatTest, DesignatedInitializers) { 2523 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2524 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2525 " .bbbbbbbbbb = 2,\n" 2526 " .cccccccccc = 3,\n" 2527 " .dddddddddd = 4,\n" 2528 " .eeeeeeeeee = 5};"); 2529 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2530 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2531 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2532 " .ccccccccccccccccccccccccccc = 3,\n" 2533 " .ddddddddddddddddddddddddddd = 4,\n" 2534 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2535 2536 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2537 } 2538 2539 TEST_F(FormatTest, NestedStaticInitializers) { 2540 verifyFormat("static A x = {{{}}};\n"); 2541 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2542 " {init1, init2, init3, init4}}};", 2543 getLLVMStyleWithColumns(50)); 2544 2545 verifyFormat("somes Status::global_reps[3] = {\n" 2546 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2547 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2548 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2549 getLLVMStyleWithColumns(60)); 2550 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2551 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2552 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2553 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2554 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2555 " {rect.fRight - rect.fLeft, rect.fBottom - " 2556 "rect.fTop}};"); 2557 2558 verifyFormat( 2559 "SomeArrayOfSomeType a = {\n" 2560 " {{1, 2, 3},\n" 2561 " {1, 2, 3},\n" 2562 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2563 " 333333333333333333333333333333},\n" 2564 " {1, 2, 3},\n" 2565 " {1, 2, 3}}};"); 2566 verifyFormat( 2567 "SomeArrayOfSomeType a = {\n" 2568 " {{1, 2, 3}},\n" 2569 " {{1, 2, 3}},\n" 2570 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2571 " 333333333333333333333333333333}},\n" 2572 " {{1, 2, 3}},\n" 2573 " {{1, 2, 3}}};"); 2574 2575 verifyFormat("struct {\n" 2576 " unsigned bit;\n" 2577 " const char *const name;\n" 2578 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2579 " {kOsWin, \"Windows\"},\n" 2580 " {kOsLinux, \"Linux\"},\n" 2581 " {kOsCrOS, \"Chrome OS\"}};"); 2582 verifyFormat("struct {\n" 2583 " unsigned bit;\n" 2584 " const char *const name;\n" 2585 "} kBitsToOs[] = {\n" 2586 " {kOsMac, \"Mac\"},\n" 2587 " {kOsWin, \"Windows\"},\n" 2588 " {kOsLinux, \"Linux\"},\n" 2589 " {kOsCrOS, \"Chrome OS\"},\n" 2590 "};"); 2591 } 2592 2593 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2594 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2595 " \\\n" 2596 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2597 } 2598 2599 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2600 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2601 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2602 2603 // Do break defaulted and deleted functions. 2604 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2605 " default;", 2606 getLLVMStyleWithColumns(40)); 2607 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2608 " delete;", 2609 getLLVMStyleWithColumns(40)); 2610 } 2611 2612 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2613 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2614 getLLVMStyleWithColumns(40)); 2615 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2616 getLLVMStyleWithColumns(40)); 2617 EXPECT_EQ("#define Q \\\n" 2618 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2619 " \"aaaaaaaa.cpp\"", 2620 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2621 getLLVMStyleWithColumns(40))); 2622 } 2623 2624 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2625 EXPECT_EQ("# 123 \"A string literal\"", 2626 format(" # 123 \"A string literal\"")); 2627 } 2628 2629 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2630 EXPECT_EQ("#;", format("#;")); 2631 verifyFormat("#\n;\n;\n;"); 2632 } 2633 2634 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2635 EXPECT_EQ("#line 42 \"test\"\n", 2636 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2637 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2638 getLLVMStyleWithColumns(12))); 2639 } 2640 2641 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2642 EXPECT_EQ("#line 42 \"test\"", 2643 format("# \\\n line \\\n 42 \\\n \"test\"")); 2644 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2645 } 2646 2647 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2648 verifyFormat("#define A \\x20"); 2649 verifyFormat("#define A \\ x20"); 2650 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2651 verifyFormat("#define A ''"); 2652 verifyFormat("#define A ''qqq"); 2653 verifyFormat("#define A `qqq"); 2654 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2655 EXPECT_EQ("const char *c = STRINGIFY(\n" 2656 "\\na : b);", 2657 format("const char * c = STRINGIFY(\n" 2658 "\\na : b);")); 2659 2660 verifyFormat("a\r\\"); 2661 verifyFormat("a\v\\"); 2662 verifyFormat("a\f\\"); 2663 } 2664 2665 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2666 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2667 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2668 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2669 // FIXME: We never break before the macro name. 2670 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2671 2672 verifyFormat("#define A A\n#define A A"); 2673 verifyFormat("#define A(X) A\n#define A A"); 2674 2675 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2676 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2677 } 2678 2679 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2680 EXPECT_EQ("// somecomment\n" 2681 "#include \"a.h\"\n" 2682 "#define A( \\\n" 2683 " A, B)\n" 2684 "#include \"b.h\"\n" 2685 "// somecomment\n", 2686 format(" // somecomment\n" 2687 " #include \"a.h\"\n" 2688 "#define A(A,\\\n" 2689 " B)\n" 2690 " #include \"b.h\"\n" 2691 " // somecomment\n", 2692 getLLVMStyleWithColumns(13))); 2693 } 2694 2695 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2696 2697 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2698 EXPECT_EQ("#define A \\\n" 2699 " c; \\\n" 2700 " e;\n" 2701 "f;", 2702 format("#define A c; e;\n" 2703 "f;", 2704 getLLVMStyleWithColumns(14))); 2705 } 2706 2707 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2708 2709 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2710 EXPECT_EQ("int x,\n" 2711 "#define A\n" 2712 " y;", 2713 format("int x,\n#define A\ny;")); 2714 } 2715 2716 TEST_F(FormatTest, HashInMacroDefinition) { 2717 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2718 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2719 verifyFormat("#define A \\\n" 2720 " { \\\n" 2721 " f(#c); \\\n" 2722 " }", 2723 getLLVMStyleWithColumns(11)); 2724 2725 verifyFormat("#define A(X) \\\n" 2726 " void function##X()", 2727 getLLVMStyleWithColumns(22)); 2728 2729 verifyFormat("#define A(a, b, c) \\\n" 2730 " void a##b##c()", 2731 getLLVMStyleWithColumns(22)); 2732 2733 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2734 } 2735 2736 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2737 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2738 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2739 } 2740 2741 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2742 EXPECT_EQ("#define A b;", format("#define A \\\n" 2743 " \\\n" 2744 " b;", 2745 getLLVMStyleWithColumns(25))); 2746 EXPECT_EQ("#define A \\\n" 2747 " \\\n" 2748 " a; \\\n" 2749 " b;", 2750 format("#define A \\\n" 2751 " \\\n" 2752 " a; \\\n" 2753 " b;", 2754 getLLVMStyleWithColumns(11))); 2755 EXPECT_EQ("#define A \\\n" 2756 " a; \\\n" 2757 " \\\n" 2758 " b;", 2759 format("#define A \\\n" 2760 " a; \\\n" 2761 " \\\n" 2762 " b;", 2763 getLLVMStyleWithColumns(11))); 2764 } 2765 2766 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2767 verifyIncompleteFormat("#define A :"); 2768 verifyFormat("#define SOMECASES \\\n" 2769 " case 1: \\\n" 2770 " case 2\n", 2771 getLLVMStyleWithColumns(20)); 2772 verifyFormat("#define MACRO(a) \\\n" 2773 " if (a) \\\n" 2774 " f(); \\\n" 2775 " else \\\n" 2776 " g()", 2777 getLLVMStyleWithColumns(18)); 2778 verifyFormat("#define A template <typename T>"); 2779 verifyIncompleteFormat("#define STR(x) #x\n" 2780 "f(STR(this_is_a_string_literal{));"); 2781 verifyFormat("#pragma omp threadprivate( \\\n" 2782 " y)), // expected-warning", 2783 getLLVMStyleWithColumns(28)); 2784 verifyFormat("#d, = };"); 2785 verifyFormat("#if \"a"); 2786 verifyIncompleteFormat("({\n" 2787 "#define b \\\n" 2788 " } \\\n" 2789 " a\n" 2790 "a", 2791 getLLVMStyleWithColumns(15)); 2792 verifyFormat("#define A \\\n" 2793 " { \\\n" 2794 " {\n" 2795 "#define B \\\n" 2796 " } \\\n" 2797 " }", 2798 getLLVMStyleWithColumns(15)); 2799 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2800 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2801 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2802 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2803 } 2804 2805 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2806 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2807 EXPECT_EQ("class A : public QObject {\n" 2808 " Q_OBJECT\n" 2809 "\n" 2810 " A() {}\n" 2811 "};", 2812 format("class A : public QObject {\n" 2813 " Q_OBJECT\n" 2814 "\n" 2815 " A() {\n}\n" 2816 "} ;")); 2817 EXPECT_EQ("MACRO\n" 2818 "/*static*/ int i;", 2819 format("MACRO\n" 2820 " /*static*/ int i;")); 2821 EXPECT_EQ("SOME_MACRO\n" 2822 "namespace {\n" 2823 "void f();\n" 2824 "}", 2825 format("SOME_MACRO\n" 2826 " namespace {\n" 2827 "void f( );\n" 2828 "}")); 2829 // Only if the identifier contains at least 5 characters. 2830 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2831 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2832 // Only if everything is upper case. 2833 EXPECT_EQ("class A : public QObject {\n" 2834 " Q_Object A() {}\n" 2835 "};", 2836 format("class A : public QObject {\n" 2837 " Q_Object\n" 2838 " A() {\n}\n" 2839 "} ;")); 2840 2841 // Only if the next line can actually start an unwrapped line. 2842 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2843 format("SOME_WEIRD_LOG_MACRO\n" 2844 "<< SomeThing;")); 2845 2846 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2847 "(n, buffers))\n", 2848 getChromiumStyle(FormatStyle::LK_Cpp)); 2849 } 2850 2851 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2852 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2853 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2854 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2855 "class X {};\n" 2856 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2857 "int *createScopDetectionPass() { return 0; }", 2858 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2859 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2860 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2861 " class X {};\n" 2862 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2863 " int *createScopDetectionPass() { return 0; }")); 2864 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2865 // braces, so that inner block is indented one level more. 2866 EXPECT_EQ("int q() {\n" 2867 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2868 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2869 " IPC_END_MESSAGE_MAP()\n" 2870 "}", 2871 format("int q() {\n" 2872 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2873 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2874 " IPC_END_MESSAGE_MAP()\n" 2875 "}")); 2876 2877 // Same inside macros. 2878 EXPECT_EQ("#define LIST(L) \\\n" 2879 " L(A) \\\n" 2880 " L(B) \\\n" 2881 " L(C)", 2882 format("#define LIST(L) \\\n" 2883 " L(A) \\\n" 2884 " L(B) \\\n" 2885 " L(C)", 2886 getGoogleStyle())); 2887 2888 // These must not be recognized as macros. 2889 EXPECT_EQ("int q() {\n" 2890 " f(x);\n" 2891 " f(x) {}\n" 2892 " f(x)->g();\n" 2893 " f(x)->*g();\n" 2894 " f(x).g();\n" 2895 " f(x) = x;\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)[y].z();\n" 2907 " LOG(INFO) << x;\n" 2908 " ifstream(x) >> x;\n" 2909 "}\n", 2910 format("int q() {\n" 2911 " f(x)\n;\n" 2912 " f(x)\n {}\n" 2913 " f(x)\n->g();\n" 2914 " f(x)\n->*g();\n" 2915 " f(x)\n.g();\n" 2916 " f(x)\n = x;\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[y].z();\n" 2928 " LOG(INFO)\n << x;\n" 2929 " ifstream(x)\n >> x;\n" 2930 "}\n")); 2931 EXPECT_EQ("int q() {\n" 2932 " F(x)\n" 2933 " if (1) {\n" 2934 " }\n" 2935 " F(x)\n" 2936 " while (1) {\n" 2937 " }\n" 2938 " F(x)\n" 2939 " G(x);\n" 2940 " F(x)\n" 2941 " try {\n" 2942 " Q();\n" 2943 " } catch (...) {\n" 2944 " }\n" 2945 "}\n", 2946 format("int q() {\n" 2947 "F(x)\n" 2948 "if (1) {}\n" 2949 "F(x)\n" 2950 "while (1) {}\n" 2951 "F(x)\n" 2952 "G(x);\n" 2953 "F(x)\n" 2954 "try { Q(); } catch (...) {}\n" 2955 "}\n")); 2956 EXPECT_EQ("class A {\n" 2957 " A() : t(0) {}\n" 2958 " A(int i) noexcept() : {}\n" 2959 " A(X x)\n" // FIXME: function-level try blocks are broken. 2960 " try : t(0) {\n" 2961 " } catch (...) {\n" 2962 " }\n" 2963 "};", 2964 format("class A {\n" 2965 " A()\n : t(0) {}\n" 2966 " A(int i)\n noexcept() : {}\n" 2967 " A(X x)\n" 2968 " try : t(0) {} catch (...) {}\n" 2969 "};")); 2970 EXPECT_EQ("class SomeClass {\n" 2971 "public:\n" 2972 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2973 "};", 2974 format("class SomeClass {\n" 2975 "public:\n" 2976 " SomeClass()\n" 2977 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2978 "};")); 2979 EXPECT_EQ("class SomeClass {\n" 2980 "public:\n" 2981 " SomeClass()\n" 2982 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2983 "};", 2984 format("class SomeClass {\n" 2985 "public:\n" 2986 " SomeClass()\n" 2987 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2988 "};", 2989 getLLVMStyleWithColumns(40))); 2990 2991 verifyFormat("MACRO(>)"); 2992 } 2993 2994 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2995 verifyFormat("#define A \\\n" 2996 " f({ \\\n" 2997 " g(); \\\n" 2998 " });", 2999 getLLVMStyleWithColumns(11)); 3000 } 3001 3002 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 3003 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 3004 } 3005 3006 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3007 verifyFormat("{\n { a #c; }\n}"); 3008 } 3009 3010 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3011 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3012 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3013 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3014 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3015 } 3016 3017 TEST_F(FormatTest, EscapedNewlines) { 3018 EXPECT_EQ( 3019 "#define A \\\n int i; \\\n int j;", 3020 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3021 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3022 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3023 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3024 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3025 } 3026 3027 TEST_F(FormatTest, DontCrashOnBlockComments) { 3028 EXPECT_EQ( 3029 "int xxxxxxxxx; /* " 3030 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3031 "zzzzzz\n" 3032 "0*/", 3033 format("int xxxxxxxxx; /* " 3034 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3035 "0*/")); 3036 } 3037 3038 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3039 verifyFormat("#define A \\\n" 3040 " int v( \\\n" 3041 " a); \\\n" 3042 " int i;", 3043 getLLVMStyleWithColumns(11)); 3044 } 3045 3046 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3047 EXPECT_EQ( 3048 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3049 " \\\n" 3050 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3051 "\n" 3052 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3053 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3054 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3055 "\\\n" 3056 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3057 " \n" 3058 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3059 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3060 } 3061 3062 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3063 EXPECT_EQ("int\n" 3064 "#define A\n" 3065 " a;", 3066 format("int\n#define A\na;")); 3067 verifyFormat("functionCallTo(\n" 3068 " someOtherFunction(\n" 3069 " withSomeParameters, whichInSequence,\n" 3070 " areLongerThanALine(andAnotherCall,\n" 3071 "#define A B\n" 3072 " withMoreParamters,\n" 3073 " whichStronglyInfluenceTheLayout),\n" 3074 " andMoreParameters),\n" 3075 " trailing);", 3076 getLLVMStyleWithColumns(69)); 3077 verifyFormat("Foo::Foo()\n" 3078 "#ifdef BAR\n" 3079 " : baz(0)\n" 3080 "#endif\n" 3081 "{\n" 3082 "}"); 3083 verifyFormat("void f() {\n" 3084 " if (true)\n" 3085 "#ifdef A\n" 3086 " f(42);\n" 3087 " x();\n" 3088 "#else\n" 3089 " g();\n" 3090 " x();\n" 3091 "#endif\n" 3092 "}"); 3093 verifyFormat("void f(param1, param2,\n" 3094 " param3,\n" 3095 "#ifdef A\n" 3096 " param4(param5,\n" 3097 "#ifdef A1\n" 3098 " param6,\n" 3099 "#ifdef A2\n" 3100 " param7),\n" 3101 "#else\n" 3102 " param8),\n" 3103 " param9,\n" 3104 "#endif\n" 3105 " param10,\n" 3106 "#endif\n" 3107 " param11)\n" 3108 "#else\n" 3109 " param12)\n" 3110 "#endif\n" 3111 "{\n" 3112 " x();\n" 3113 "}", 3114 getLLVMStyleWithColumns(28)); 3115 verifyFormat("#if 1\n" 3116 "int i;"); 3117 verifyFormat("#if 1\n" 3118 "#endif\n" 3119 "#if 1\n" 3120 "#else\n" 3121 "#endif\n"); 3122 verifyFormat("DEBUG({\n" 3123 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3125 "});\n" 3126 "#if a\n" 3127 "#else\n" 3128 "#endif"); 3129 3130 verifyIncompleteFormat("void f(\n" 3131 "#if A\n" 3132 " );\n" 3133 "#else\n" 3134 "#endif"); 3135 } 3136 3137 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3138 verifyFormat("#endif\n" 3139 "#if B"); 3140 } 3141 3142 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3143 FormatStyle SingleLine = getLLVMStyle(); 3144 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3145 verifyFormat("#if 0\n" 3146 "#elif 1\n" 3147 "#endif\n" 3148 "void foo() {\n" 3149 " if (test) foo2();\n" 3150 "}", 3151 SingleLine); 3152 } 3153 3154 TEST_F(FormatTest, LayoutBlockInsideParens) { 3155 verifyFormat("functionCall({ int i; });"); 3156 verifyFormat("functionCall({\n" 3157 " int i;\n" 3158 " int j;\n" 3159 "});"); 3160 verifyFormat("functionCall(\n" 3161 " {\n" 3162 " int i;\n" 3163 " int j;\n" 3164 " },\n" 3165 " aaaa, bbbb, cccc);"); 3166 verifyFormat("functionA(functionB({\n" 3167 " int i;\n" 3168 " int j;\n" 3169 " }),\n" 3170 " aaaa, bbbb, cccc);"); 3171 verifyFormat("functionCall(\n" 3172 " {\n" 3173 " int i;\n" 3174 " int j;\n" 3175 " },\n" 3176 " aaaa, bbbb, // comment\n" 3177 " cccc);"); 3178 verifyFormat("functionA(functionB({\n" 3179 " int i;\n" 3180 " int j;\n" 3181 " }),\n" 3182 " aaaa, bbbb, // comment\n" 3183 " cccc);"); 3184 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3185 verifyFormat("functionCall(aaaa, bbbb, {\n" 3186 " int i;\n" 3187 " int j;\n" 3188 "});"); 3189 verifyFormat( 3190 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3191 " {\n" 3192 " int i; // break\n" 3193 " },\n" 3194 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3195 " ccccccccccccccccc));"); 3196 verifyFormat("DEBUG({\n" 3197 " if (a)\n" 3198 " f();\n" 3199 "});"); 3200 } 3201 3202 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3203 EXPECT_EQ("SOME_MACRO { int i; }\n" 3204 "int i;", 3205 format(" SOME_MACRO {int i;} int i;")); 3206 } 3207 3208 TEST_F(FormatTest, LayoutNestedBlocks) { 3209 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3210 " struct s {\n" 3211 " int i;\n" 3212 " };\n" 3213 " s kBitsToOs[] = {{10}};\n" 3214 " for (int i = 0; i < 10; ++i)\n" 3215 " return;\n" 3216 "}"); 3217 verifyFormat("call(parameter, {\n" 3218 " something();\n" 3219 " // Comment using all columns.\n" 3220 " somethingelse();\n" 3221 "});", 3222 getLLVMStyleWithColumns(40)); 3223 verifyFormat("DEBUG( //\n" 3224 " { f(); }, a);"); 3225 verifyFormat("DEBUG( //\n" 3226 " {\n" 3227 " f(); //\n" 3228 " },\n" 3229 " a);"); 3230 3231 EXPECT_EQ("call(parameter, {\n" 3232 " something();\n" 3233 " // Comment too\n" 3234 " // looooooooooong.\n" 3235 " somethingElse();\n" 3236 "});", 3237 format("call(parameter, {\n" 3238 " something();\n" 3239 " // Comment too looooooooooong.\n" 3240 " somethingElse();\n" 3241 "});", 3242 getLLVMStyleWithColumns(29))); 3243 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3244 EXPECT_EQ("DEBUG({ // comment\n" 3245 " int i;\n" 3246 "});", 3247 format("DEBUG({ // comment\n" 3248 "int i;\n" 3249 "});")); 3250 EXPECT_EQ("DEBUG({\n" 3251 " int i;\n" 3252 "\n" 3253 " // comment\n" 3254 " int j;\n" 3255 "});", 3256 format("DEBUG({\n" 3257 " int i;\n" 3258 "\n" 3259 " // comment\n" 3260 " int j;\n" 3261 "});")); 3262 3263 verifyFormat("DEBUG({\n" 3264 " if (a)\n" 3265 " return;\n" 3266 "});"); 3267 verifyGoogleFormat("DEBUG({\n" 3268 " if (a) return;\n" 3269 "});"); 3270 FormatStyle Style = getGoogleStyle(); 3271 Style.ColumnLimit = 45; 3272 verifyFormat("Debug(aaaaa,\n" 3273 " {\n" 3274 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3275 " },\n" 3276 " a);", 3277 Style); 3278 3279 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3280 3281 verifyNoCrash("^{v^{a}}"); 3282 } 3283 3284 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3285 EXPECT_EQ("#define MACRO() \\\n" 3286 " Debug(aaa, /* force line break */ \\\n" 3287 " { \\\n" 3288 " int i; \\\n" 3289 " int j; \\\n" 3290 " })", 3291 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3292 " { int i; int j; })", 3293 getGoogleStyle())); 3294 3295 EXPECT_EQ("#define A \\\n" 3296 " [] { \\\n" 3297 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3298 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3299 " }", 3300 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3301 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3302 getGoogleStyle())); 3303 } 3304 3305 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3306 EXPECT_EQ("{}", format("{}")); 3307 verifyFormat("enum E {};"); 3308 verifyFormat("enum E {}"); 3309 } 3310 3311 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3312 FormatStyle Style = getLLVMStyle(); 3313 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3314 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3315 verifyFormat("FOO_BEGIN\n" 3316 " FOO_ENTRY\n" 3317 "FOO_END", Style); 3318 verifyFormat("FOO_BEGIN\n" 3319 " NESTED_FOO_BEGIN\n" 3320 " NESTED_FOO_ENTRY\n" 3321 " NESTED_FOO_END\n" 3322 "FOO_END", Style); 3323 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3324 " int x;\n" 3325 " x = 1;\n" 3326 "FOO_END(Baz)", Style); 3327 } 3328 3329 //===----------------------------------------------------------------------===// 3330 // Line break tests. 3331 //===----------------------------------------------------------------------===// 3332 3333 TEST_F(FormatTest, PreventConfusingIndents) { 3334 verifyFormat( 3335 "void f() {\n" 3336 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3337 " parameter, parameter, parameter)),\n" 3338 " SecondLongCall(parameter));\n" 3339 "}"); 3340 verifyFormat( 3341 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3342 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3344 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3345 verifyFormat( 3346 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3347 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3348 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3349 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3350 verifyFormat( 3351 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3352 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3354 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3355 verifyFormat("int a = bbbb && ccc && fffff(\n" 3356 "#define A Just forcing a new line\n" 3357 " ddd);"); 3358 } 3359 3360 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3361 verifyFormat( 3362 "bool aaaaaaa =\n" 3363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3364 " bbbbbbbb();"); 3365 verifyFormat( 3366 "bool aaaaaaa =\n" 3367 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3368 " bbbbbbbb();"); 3369 3370 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3372 " ccccccccc == ddddddddddd;"); 3373 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3374 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3375 " ccccccccc == ddddddddddd;"); 3376 verifyFormat( 3377 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3379 " ccccccccc == ddddddddddd;"); 3380 3381 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3382 " aaaaaa) &&\n" 3383 " bbbbbb && cccccc;"); 3384 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3385 " aaaaaa) >>\n" 3386 " bbbbbb;"); 3387 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3388 " SourceMgr.getSpellingColumnNumber(\n" 3389 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3390 " 1);"); 3391 3392 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3393 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3394 " cccccc) {\n}"); 3395 verifyFormat("b = a &&\n" 3396 " // Comment\n" 3397 " b.c && d;"); 3398 3399 // If the LHS of a comparison is not a binary expression itself, the 3400 // additional linebreak confuses many people. 3401 verifyFormat( 3402 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3404 "}"); 3405 verifyFormat( 3406 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3408 "}"); 3409 verifyFormat( 3410 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3412 "}"); 3413 // Even explicit parentheses stress the precedence enough to make the 3414 // additional break unnecessary. 3415 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3417 "}"); 3418 // This cases is borderline, but with the indentation it is still readable. 3419 verifyFormat( 3420 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3421 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3423 "}", 3424 getLLVMStyleWithColumns(75)); 3425 3426 // If the LHS is a binary expression, we should still use the additional break 3427 // as otherwise the formatting hides the operator precedence. 3428 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3430 " 5) {\n" 3431 "}"); 3432 3433 FormatStyle OnePerLine = getLLVMStyle(); 3434 OnePerLine.BinPackParameters = false; 3435 verifyFormat( 3436 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3439 OnePerLine); 3440 } 3441 3442 TEST_F(FormatTest, ExpressionIndentation) { 3443 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3447 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3448 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3451 " ccccccccccccccccccccccccccccccccccccccccc;"); 3452 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3455 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3456 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3459 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3460 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3463 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3464 verifyFormat("if () {\n" 3465 "} else if (aaaaa &&\n" 3466 " bbbbb > // break\n" 3467 " ccccc) {\n" 3468 "}"); 3469 3470 // Presence of a trailing comment used to change indentation of b. 3471 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3472 " b;\n" 3473 "return aaaaaaaaaaaaaaaaaaa +\n" 3474 " b; //", 3475 getLLVMStyleWithColumns(30)); 3476 } 3477 3478 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3479 // Not sure what the best system is here. Like this, the LHS can be found 3480 // immediately above an operator (everything with the same or a higher 3481 // indent). The RHS is aligned right of the operator and so compasses 3482 // everything until something with the same indent as the operator is found. 3483 // FIXME: Is this a good system? 3484 FormatStyle Style = getLLVMStyle(); 3485 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3486 verifyFormat( 3487 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3488 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3489 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3490 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3491 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3492 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3493 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3494 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3495 " > ccccccccccccccccccccccccccccccccccccccccc;", 3496 Style); 3497 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3498 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3499 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3500 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3501 Style); 3502 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3503 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3504 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3505 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3506 Style); 3507 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3508 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3509 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3510 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3511 Style); 3512 verifyFormat("if () {\n" 3513 "} else if (aaaaa\n" 3514 " && bbbbb // break\n" 3515 " > ccccc) {\n" 3516 "}", 3517 Style); 3518 verifyFormat("return (a)\n" 3519 " // comment\n" 3520 " + b;", 3521 Style); 3522 verifyFormat( 3523 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3524 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3525 " + cc;", 3526 Style); 3527 3528 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3529 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3530 Style); 3531 3532 // Forced by comments. 3533 verifyFormat( 3534 "unsigned ContentSize =\n" 3535 " sizeof(int16_t) // DWARF ARange version number\n" 3536 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3537 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3538 " + sizeof(int8_t); // Segment Size (in bytes)"); 3539 3540 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3541 " == boost::fusion::at_c<1>(iiii).second;", 3542 Style); 3543 3544 Style.ColumnLimit = 60; 3545 verifyFormat("zzzzzzzzzz\n" 3546 " = bbbbbbbbbbbbbbbbb\n" 3547 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3548 Style); 3549 } 3550 3551 TEST_F(FormatTest, NoOperandAlignment) { 3552 FormatStyle Style = getLLVMStyle(); 3553 Style.AlignOperands = false; 3554 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3555 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3556 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3557 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3558 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3559 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3560 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3561 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3562 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3563 " > ccccccccccccccccccccccccccccccccccccccccc;", 3564 Style); 3565 3566 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3567 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3568 " + cc;", 3569 Style); 3570 verifyFormat("int a = aa\n" 3571 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3572 " * cccccccccccccccccccccccccccccccccccc;", 3573 Style); 3574 3575 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3576 verifyFormat("return (a > b\n" 3577 " // comment1\n" 3578 " // comment2\n" 3579 " || c);", 3580 Style); 3581 } 3582 3583 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3584 FormatStyle Style = getLLVMStyle(); 3585 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3586 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3588 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3589 Style); 3590 } 3591 3592 TEST_F(FormatTest, ConstructorInitializers) { 3593 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3594 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3595 getLLVMStyleWithColumns(45)); 3596 verifyFormat("Constructor()\n" 3597 " : Inttializer(FitsOnTheLine) {}", 3598 getLLVMStyleWithColumns(44)); 3599 verifyFormat("Constructor()\n" 3600 " : Inttializer(FitsOnTheLine) {}", 3601 getLLVMStyleWithColumns(43)); 3602 3603 verifyFormat("template <typename T>\n" 3604 "Constructor() : Initializer(FitsOnTheLine) {}", 3605 getLLVMStyleWithColumns(45)); 3606 3607 verifyFormat( 3608 "SomeClass::Constructor()\n" 3609 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3610 3611 verifyFormat( 3612 "SomeClass::Constructor()\n" 3613 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3614 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3615 verifyFormat( 3616 "SomeClass::Constructor()\n" 3617 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3618 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3619 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3620 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3621 " : aaaaaaaaaa(aaaaaa) {}"); 3622 3623 verifyFormat("Constructor()\n" 3624 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3625 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3626 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3627 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3628 3629 verifyFormat("Constructor()\n" 3630 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3632 3633 verifyFormat("Constructor(int Parameter = 0)\n" 3634 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3635 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3636 verifyFormat("Constructor()\n" 3637 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3638 "}", 3639 getLLVMStyleWithColumns(60)); 3640 verifyFormat("Constructor()\n" 3641 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3642 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3643 3644 // Here a line could be saved by splitting the second initializer onto two 3645 // lines, but that is not desirable. 3646 verifyFormat("Constructor()\n" 3647 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3648 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3649 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3650 3651 FormatStyle OnePerLine = getLLVMStyle(); 3652 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3653 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3654 verifyFormat("SomeClass::Constructor()\n" 3655 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3656 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3657 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3658 OnePerLine); 3659 verifyFormat("SomeClass::Constructor()\n" 3660 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3661 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3662 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3663 OnePerLine); 3664 verifyFormat("MyClass::MyClass(int var)\n" 3665 " : some_var_(var), // 4 space indent\n" 3666 " some_other_var_(var + 1) { // lined up\n" 3667 "}", 3668 OnePerLine); 3669 verifyFormat("Constructor()\n" 3670 " : aaaaa(aaaaaa),\n" 3671 " aaaaa(aaaaaa),\n" 3672 " aaaaa(aaaaaa),\n" 3673 " aaaaa(aaaaaa),\n" 3674 " aaaaa(aaaaaa) {}", 3675 OnePerLine); 3676 verifyFormat("Constructor()\n" 3677 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3678 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3679 OnePerLine); 3680 OnePerLine.BinPackParameters = false; 3681 verifyFormat( 3682 "Constructor()\n" 3683 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3684 " aaaaaaaaaaa().aaa(),\n" 3685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3686 OnePerLine); 3687 OnePerLine.ColumnLimit = 60; 3688 verifyFormat("Constructor()\n" 3689 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3690 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3691 OnePerLine); 3692 3693 EXPECT_EQ("Constructor()\n" 3694 " : // Comment forcing unwanted break.\n" 3695 " aaaa(aaaa) {}", 3696 format("Constructor() :\n" 3697 " // Comment forcing unwanted break.\n" 3698 " aaaa(aaaa) {}")); 3699 } 3700 3701 TEST_F(FormatTest, MemoizationTests) { 3702 // This breaks if the memoization lookup does not take \c Indent and 3703 // \c LastSpace into account. 3704 verifyFormat( 3705 "extern CFRunLoopTimerRef\n" 3706 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3707 " CFTimeInterval interval, CFOptionFlags flags,\n" 3708 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3709 " CFRunLoopTimerContext *context) {}"); 3710 3711 // Deep nesting somewhat works around our memoization. 3712 verifyFormat( 3713 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3714 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3715 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3716 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3717 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3718 getLLVMStyleWithColumns(65)); 3719 verifyFormat( 3720 "aaaaa(\n" 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))))))))))));", 3745 getLLVMStyleWithColumns(65)); 3746 verifyFormat( 3747 "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" 3748 " 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)", 3765 getLLVMStyleWithColumns(65)); 3766 3767 // This test takes VERY long when memoization is broken. 3768 FormatStyle OnePerLine = getLLVMStyle(); 3769 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3770 OnePerLine.BinPackParameters = false; 3771 std::string input = "Constructor()\n" 3772 " : aaaa(a,\n"; 3773 for (unsigned i = 0, e = 80; i != e; ++i) { 3774 input += " a,\n"; 3775 } 3776 input += " a) {}"; 3777 verifyFormat(input, OnePerLine); 3778 } 3779 3780 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3781 verifyFormat( 3782 "void f() {\n" 3783 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3784 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3785 " f();\n" 3786 "}"); 3787 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3788 " Intervals[i - 1].getRange().getLast()) {\n}"); 3789 } 3790 3791 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3792 // Principially, we break function declarations in a certain order: 3793 // 1) break amongst arguments. 3794 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3795 " Cccccccccccccc cccccccccccccc);"); 3796 verifyFormat("template <class TemplateIt>\n" 3797 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3798 " TemplateIt *stop) {}"); 3799 3800 // 2) break after return type. 3801 verifyFormat( 3802 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3803 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3804 getGoogleStyle()); 3805 3806 // 3) break after (. 3807 verifyFormat( 3808 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3809 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3810 getGoogleStyle()); 3811 3812 // 4) break before after nested name specifiers. 3813 verifyFormat( 3814 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3815 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3816 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3817 getGoogleStyle()); 3818 3819 // However, there are exceptions, if a sufficient amount of lines can be 3820 // saved. 3821 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3822 // more adjusting. 3823 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3824 " Cccccccccccccc cccccccccc,\n" 3825 " Cccccccccccccc cccccccccc,\n" 3826 " Cccccccccccccc cccccccccc,\n" 3827 " Cccccccccccccc cccccccccc);"); 3828 verifyFormat( 3829 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3830 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3831 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3832 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3833 getGoogleStyle()); 3834 verifyFormat( 3835 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3836 " Cccccccccccccc cccccccccc,\n" 3837 " Cccccccccccccc cccccccccc,\n" 3838 " Cccccccccccccc cccccccccc,\n" 3839 " Cccccccccccccc cccccccccc,\n" 3840 " Cccccccccccccc cccccccccc,\n" 3841 " Cccccccccccccc cccccccccc);"); 3842 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3843 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3844 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3845 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3846 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3847 3848 // Break after multi-line parameters. 3849 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3852 " bbbb bbbb);"); 3853 verifyFormat("void SomeLoooooooooooongFunction(\n" 3854 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3855 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3856 " int bbbbbbbbbbbbb);"); 3857 3858 // Treat overloaded operators like other functions. 3859 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3860 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3861 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3862 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3863 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3864 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3865 verifyGoogleFormat( 3866 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3867 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3868 verifyGoogleFormat( 3869 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3870 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3871 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3872 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3873 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3874 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3875 verifyGoogleFormat( 3876 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3877 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3878 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3879 verifyGoogleFormat( 3880 "template <typename T>\n" 3881 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3882 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3883 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3884 3885 FormatStyle Style = getLLVMStyle(); 3886 Style.PointerAlignment = FormatStyle::PAS_Left; 3887 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3888 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3889 Style); 3890 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3892 Style); 3893 } 3894 3895 TEST_F(FormatTest, TrailingReturnType) { 3896 verifyFormat("auto foo() -> int;\n"); 3897 verifyFormat("struct S {\n" 3898 " auto bar() const -> int;\n" 3899 "};"); 3900 verifyFormat("template <size_t Order, typename T>\n" 3901 "auto load_img(const std::string &filename)\n" 3902 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3903 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3904 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3905 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3906 verifyFormat("template <typename T>\n" 3907 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3908 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3909 3910 // Not trailing return types. 3911 verifyFormat("void f() { auto a = b->c(); }"); 3912 } 3913 3914 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3915 // Avoid breaking before trailing 'const' or other trailing annotations, if 3916 // they are not function-like. 3917 FormatStyle Style = getGoogleStyle(); 3918 Style.ColumnLimit = 47; 3919 verifyFormat("void someLongFunction(\n" 3920 " int someLoooooooooooooongParameter) const {\n}", 3921 getLLVMStyleWithColumns(47)); 3922 verifyFormat("LoooooongReturnType\n" 3923 "someLoooooooongFunction() const {}", 3924 getLLVMStyleWithColumns(47)); 3925 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3926 " const {}", 3927 Style); 3928 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3929 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3930 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3931 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3932 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3933 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3934 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3935 " aaaaaaaaaaa aaaaa) const override;"); 3936 verifyGoogleFormat( 3937 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3938 " const override;"); 3939 3940 // Even if the first parameter has to be wrapped. 3941 verifyFormat("void someLongFunction(\n" 3942 " int someLongParameter) const {}", 3943 getLLVMStyleWithColumns(46)); 3944 verifyFormat("void someLongFunction(\n" 3945 " int someLongParameter) const {}", 3946 Style); 3947 verifyFormat("void someLongFunction(\n" 3948 " int someLongParameter) override {}", 3949 Style); 3950 verifyFormat("void someLongFunction(\n" 3951 " int someLongParameter) OVERRIDE {}", 3952 Style); 3953 verifyFormat("void someLongFunction(\n" 3954 " int someLongParameter) final {}", 3955 Style); 3956 verifyFormat("void someLongFunction(\n" 3957 " int someLongParameter) FINAL {}", 3958 Style); 3959 verifyFormat("void someLongFunction(\n" 3960 " int parameter) const override {}", 3961 Style); 3962 3963 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3964 verifyFormat("void someLongFunction(\n" 3965 " int someLongParameter) const\n" 3966 "{\n" 3967 "}", 3968 Style); 3969 3970 // Unless these are unknown annotations. 3971 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3972 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3973 " LONG_AND_UGLY_ANNOTATION;"); 3974 3975 // Breaking before function-like trailing annotations is fine to keep them 3976 // close to their arguments. 3977 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3978 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3979 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3980 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3981 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3982 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3983 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3984 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3985 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3986 3987 verifyFormat( 3988 "void aaaaaaaaaaaaaaaaaa()\n" 3989 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3990 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3991 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3992 " __attribute__((unused));"); 3993 verifyGoogleFormat( 3994 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3995 " GUARDED_BY(aaaaaaaaaaaa);"); 3996 verifyGoogleFormat( 3997 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3998 " GUARDED_BY(aaaaaaaaaaaa);"); 3999 verifyGoogleFormat( 4000 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4001 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4002 verifyGoogleFormat( 4003 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4004 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4005 } 4006 4007 TEST_F(FormatTest, FunctionAnnotations) { 4008 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4009 "int OldFunction(const string ¶meter) {}"); 4010 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4011 "string OldFunction(const string ¶meter) {}"); 4012 verifyFormat("template <typename T>\n" 4013 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4014 "string OldFunction(const string ¶meter) {}"); 4015 4016 // Not function annotations. 4017 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4018 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4019 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4020 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4021 verifyFormat("MACRO(abc).function() // wrap\n" 4022 " << abc;"); 4023 verifyFormat("MACRO(abc)->function() // wrap\n" 4024 " << abc;"); 4025 verifyFormat("MACRO(abc)::function() // wrap\n" 4026 " << abc;"); 4027 } 4028 4029 TEST_F(FormatTest, BreaksDesireably) { 4030 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4031 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4032 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4033 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4035 "}"); 4036 4037 verifyFormat( 4038 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4040 4041 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4044 4045 verifyFormat( 4046 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4048 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4050 4051 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4052 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4053 4054 verifyFormat( 4055 "void f() {\n" 4056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4058 "}"); 4059 verifyFormat( 4060 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4062 verifyFormat( 4063 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4065 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4068 4069 // Indent consistently independent of call expression and unary operator. 4070 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4071 " dddddddddddddddddddddddddddddd));"); 4072 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4073 " dddddddddddddddddddddddddddddd));"); 4074 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4075 " dddddddddddddddddddddddddddddd));"); 4076 4077 // This test case breaks on an incorrect memoization, i.e. an optimization not 4078 // taking into account the StopAt value. 4079 verifyFormat( 4080 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4081 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4082 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4083 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4084 4085 verifyFormat("{\n {\n {\n" 4086 " Annotation.SpaceRequiredBefore =\n" 4087 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4088 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4089 " }\n }\n}"); 4090 4091 // Break on an outer level if there was a break on an inner level. 4092 EXPECT_EQ("f(g(h(a, // comment\n" 4093 " b, c),\n" 4094 " d, e),\n" 4095 " x, y);", 4096 format("f(g(h(a, // comment\n" 4097 " b, c), d, e), x, y);")); 4098 4099 // Prefer breaking similar line breaks. 4100 verifyFormat( 4101 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4102 " NSTrackingMouseEnteredAndExited |\n" 4103 " NSTrackingActiveAlways;"); 4104 } 4105 4106 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4107 FormatStyle NoBinPacking = getGoogleStyle(); 4108 NoBinPacking.BinPackParameters = false; 4109 NoBinPacking.BinPackArguments = true; 4110 verifyFormat("void f() {\n" 4111 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4113 "}", 4114 NoBinPacking); 4115 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4116 " int aaaaaaaaaaaaaaaaaaaa,\n" 4117 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4118 NoBinPacking); 4119 4120 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4121 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4122 " vector<int> bbbbbbbbbbbbbbb);", 4123 NoBinPacking); 4124 // FIXME: This behavior difference is probably not wanted. However, currently 4125 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4126 // template arguments from BreakBeforeParameter being set because of the 4127 // one-per-line formatting. 4128 verifyFormat( 4129 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4130 " aaaaaaaaaa> aaaaaaaaaa);", 4131 NoBinPacking); 4132 verifyFormat( 4133 "void fffffffffff(\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4135 " aaaaaaaaaa);"); 4136 } 4137 4138 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4139 FormatStyle NoBinPacking = getGoogleStyle(); 4140 NoBinPacking.BinPackParameters = false; 4141 NoBinPacking.BinPackArguments = false; 4142 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4143 " aaaaaaaaaaaaaaaaaaaa,\n" 4144 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4145 NoBinPacking); 4146 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4147 " aaaaaaaaaaaaa,\n" 4148 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4149 NoBinPacking); 4150 verifyFormat( 4151 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4152 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4154 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4156 NoBinPacking); 4157 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4158 " .aaaaaaaaaaaaaaaaaa();", 4159 NoBinPacking); 4160 verifyFormat("void f() {\n" 4161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4162 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4163 "}", 4164 NoBinPacking); 4165 4166 verifyFormat( 4167 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4168 " aaaaaaaaaaaa,\n" 4169 " aaaaaaaaaaaa);", 4170 NoBinPacking); 4171 verifyFormat( 4172 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4173 " ddddddddddddddddddddddddddddd),\n" 4174 " test);", 4175 NoBinPacking); 4176 4177 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4178 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4179 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4180 " aaaaaaaaaaaaaaaaaa;", 4181 NoBinPacking); 4182 verifyFormat("a(\"a\"\n" 4183 " \"a\",\n" 4184 " a);"); 4185 4186 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4187 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4188 " aaaaaaaaa,\n" 4189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4190 NoBinPacking); 4191 verifyFormat( 4192 "void f() {\n" 4193 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4194 " .aaaaaaa();\n" 4195 "}", 4196 NoBinPacking); 4197 verifyFormat( 4198 "template <class SomeType, class SomeOtherType>\n" 4199 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4200 NoBinPacking); 4201 } 4202 4203 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4204 FormatStyle Style = getLLVMStyleWithColumns(15); 4205 Style.ExperimentalAutoDetectBinPacking = true; 4206 EXPECT_EQ("aaa(aaaa,\n" 4207 " aaaa,\n" 4208 " aaaa);\n" 4209 "aaa(aaaa,\n" 4210 " aaaa,\n" 4211 " aaaa);", 4212 format("aaa(aaaa,\n" // one-per-line 4213 " aaaa,\n" 4214 " aaaa );\n" 4215 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4216 Style)); 4217 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4218 " aaaa);\n" 4219 "aaa(aaaa, aaaa,\n" 4220 " aaaa);", 4221 format("aaa(aaaa, aaaa,\n" // bin-packed 4222 " aaaa );\n" 4223 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4224 Style)); 4225 } 4226 4227 TEST_F(FormatTest, FormatsBuilderPattern) { 4228 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4229 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4230 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4231 " .StartsWith(\".init\", ORDER_INIT)\n" 4232 " .StartsWith(\".fini\", ORDER_FINI)\n" 4233 " .StartsWith(\".hash\", ORDER_HASH)\n" 4234 " .Default(ORDER_TEXT);\n"); 4235 4236 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4237 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4238 verifyFormat( 4239 "aaaaaaa->aaaaaaa\n" 4240 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4242 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4243 verifyFormat( 4244 "aaaaaaa->aaaaaaa\n" 4245 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4246 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4247 verifyFormat( 4248 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4249 " aaaaaaaaaaaaaa);"); 4250 verifyFormat( 4251 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4252 " aaaaaa->aaaaaaaaaaaa()\n" 4253 " ->aaaaaaaaaaaaaaaa(\n" 4254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4255 " ->aaaaaaaaaaaaaaaaa();"); 4256 verifyGoogleFormat( 4257 "void f() {\n" 4258 " someo->Add((new util::filetools::Handler(dir))\n" 4259 " ->OnEvent1(NewPermanentCallback(\n" 4260 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4261 " ->OnEvent2(NewPermanentCallback(\n" 4262 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4263 " ->OnEvent3(NewPermanentCallback(\n" 4264 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4265 " ->OnEvent5(NewPermanentCallback(\n" 4266 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4267 " ->OnEvent6(NewPermanentCallback(\n" 4268 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4269 "}"); 4270 4271 verifyFormat( 4272 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4273 verifyFormat("aaaaaaaaaaaaaaa()\n" 4274 " .aaaaaaaaaaaaaaa()\n" 4275 " .aaaaaaaaaaaaaaa()\n" 4276 " .aaaaaaaaaaaaaaa()\n" 4277 " .aaaaaaaaaaaaaaa();"); 4278 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4279 " .aaaaaaaaaaaaaaa()\n" 4280 " .aaaaaaaaaaaaaaa()\n" 4281 " .aaaaaaaaaaaaaaa();"); 4282 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4283 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4284 " .aaaaaaaaaaaaaaa();"); 4285 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4286 " ->aaaaaaaaaaaaaae(0)\n" 4287 " ->aaaaaaaaaaaaaaa();"); 4288 4289 // Don't linewrap after very short segments. 4290 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4291 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4292 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4293 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4294 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4295 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4296 verifyFormat("aaa()\n" 4297 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4298 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4299 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4300 4301 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4302 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4303 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4304 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4307 4308 // Prefer not to break after empty parentheses. 4309 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4310 " First->LastNewlineOffset);"); 4311 4312 // Prefer not to create "hanging" indents. 4313 verifyFormat( 4314 "return !soooooooooooooome_map\n" 4315 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4316 " .second;"); 4317 verifyFormat( 4318 "return aaaaaaaaaaaaaaaa\n" 4319 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4320 " .aaaa(aaaaaaaaaaaaaa);"); 4321 // No hanging indent here. 4322 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4324 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4326 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4327 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4328 getLLVMStyleWithColumns(60)); 4329 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4330 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4331 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4332 getLLVMStyleWithColumns(59)); 4333 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4335 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4336 } 4337 4338 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4339 verifyFormat( 4340 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4341 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4342 verifyFormat( 4343 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4344 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4345 4346 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4347 " ccccccccccccccccccccccccc) {\n}"); 4348 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4349 " ccccccccccccccccccccccccc) {\n}"); 4350 4351 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4352 " ccccccccccccccccccccccccc) {\n}"); 4353 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4354 " ccccccccccccccccccccccccc) {\n}"); 4355 4356 verifyFormat( 4357 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4358 " ccccccccccccccccccccccccc) {\n}"); 4359 verifyFormat( 4360 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4361 " ccccccccccccccccccccccccc) {\n}"); 4362 4363 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4364 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4365 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4366 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4367 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4368 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4369 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4370 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4371 4372 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4373 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4374 " aaaaaaaaaaaaaaa != aa) {\n}"); 4375 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4376 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4377 " aaaaaaaaaaaaaaa != aa) {\n}"); 4378 } 4379 4380 TEST_F(FormatTest, BreaksAfterAssignments) { 4381 verifyFormat( 4382 "unsigned Cost =\n" 4383 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4384 " SI->getPointerAddressSpaceee());\n"); 4385 verifyFormat( 4386 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4387 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4388 4389 verifyFormat( 4390 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4391 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4392 verifyFormat("unsigned OriginalStartColumn =\n" 4393 " SourceMgr.getSpellingColumnNumber(\n" 4394 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4395 " 1;"); 4396 } 4397 4398 TEST_F(FormatTest, AlignsAfterAssignments) { 4399 verifyFormat( 4400 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4401 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4402 verifyFormat( 4403 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4404 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4405 verifyFormat( 4406 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4407 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4408 verifyFormat( 4409 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4410 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4411 verifyFormat( 4412 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4413 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4414 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4415 } 4416 4417 TEST_F(FormatTest, AlignsAfterReturn) { 4418 verifyFormat( 4419 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4420 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4421 verifyFormat( 4422 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4423 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4424 verifyFormat( 4425 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4426 " aaaaaaaaaaaaaaaaaaaaaa();"); 4427 verifyFormat( 4428 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4429 " aaaaaaaaaaaaaaaaaaaaaa());"); 4430 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4432 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4435 verifyFormat("return\n" 4436 " // true if code is one of a or b.\n" 4437 " code == a || code == b;"); 4438 } 4439 4440 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4441 verifyFormat( 4442 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4443 " aaaaaaaaa aaaaaaa) {}"); 4444 verifyFormat( 4445 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4446 " aaaaaaaaaaa aaaaaaaaa);"); 4447 verifyFormat( 4448 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4449 " aaaaaaaaaaaaaaaaaaaaa));"); 4450 FormatStyle Style = getLLVMStyle(); 4451 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4452 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4453 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4454 Style); 4455 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4456 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4457 Style); 4458 verifyFormat("SomeLongVariableName->someFunction(\n" 4459 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4460 Style); 4461 verifyFormat( 4462 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4463 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4464 Style); 4465 verifyFormat( 4466 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4467 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4468 Style); 4469 verifyFormat( 4470 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4471 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4472 Style); 4473 4474 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4475 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4476 " b));", 4477 Style); 4478 4479 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4480 Style.BinPackArguments = false; 4481 Style.BinPackParameters = false; 4482 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4483 " aaaaaaaaaaa aaaaaaaa,\n" 4484 " aaaaaaaaa aaaaaaa,\n" 4485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4486 Style); 4487 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4488 " aaaaaaaaaaa aaaaaaaaa,\n" 4489 " aaaaaaaaaaa aaaaaaaaa,\n" 4490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4491 Style); 4492 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4493 " aaaaaaaaaaaaaaa,\n" 4494 " aaaaaaaaaaaaaaaaaaaaa,\n" 4495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4496 Style); 4497 verifyFormat( 4498 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4499 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4500 Style); 4501 verifyFormat( 4502 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4503 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4504 Style); 4505 verifyFormat( 4506 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4507 " aaaaaaaaaaaaaaaaaaaaa(\n" 4508 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4509 " aaaaaaaaaaaaaaaa);", 4510 Style); 4511 verifyFormat( 4512 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4513 " aaaaaaaaaaaaaaaaaaaaa(\n" 4514 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4515 " aaaaaaaaaaaaaaaa);", 4516 Style); 4517 } 4518 4519 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4520 FormatStyle Style = getLLVMStyleWithColumns(40); 4521 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4522 " bbbbbbbbbbbbbbbbbbbbbb);", 4523 Style); 4524 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4525 Style.AlignOperands = false; 4526 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4527 " bbbbbbbbbbbbbbbbbbbbbb);", 4528 Style); 4529 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4530 Style.AlignOperands = true; 4531 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4532 " bbbbbbbbbbbbbbbbbbbbbb);", 4533 Style); 4534 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4535 Style.AlignOperands = false; 4536 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4537 " bbbbbbbbbbbbbbbbbbbbbb);", 4538 Style); 4539 } 4540 4541 TEST_F(FormatTest, BreaksConditionalExpressions) { 4542 verifyFormat( 4543 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4544 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4545 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4546 verifyFormat( 4547 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4548 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4549 verifyFormat( 4550 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4551 " : aaaaaaaaaaaaa);"); 4552 verifyFormat( 4553 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4554 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4556 " aaaaaaaaaaaaa);"); 4557 verifyFormat( 4558 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4559 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4560 " aaaaaaaaaaaaa);"); 4561 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4562 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4564 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4566 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4568 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4570 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4572 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4573 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4575 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4577 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4578 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4579 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4580 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4581 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4583 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4584 " : aaaaaaaaaaaaaaaa;"); 4585 verifyFormat( 4586 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4587 " ? aaaaaaaaaaaaaaa\n" 4588 " : aaaaaaaaaaaaaaa;"); 4589 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4590 " aaaaaaaaa\n" 4591 " ? b\n" 4592 " : c);"); 4593 verifyFormat("return aaaa == bbbb\n" 4594 " // comment\n" 4595 " ? aaaa\n" 4596 " : bbbb;"); 4597 verifyFormat("unsigned Indent =\n" 4598 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4599 " ? IndentForLevel[TheLine.Level]\n" 4600 " : TheLine * 2,\n" 4601 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4602 getLLVMStyleWithColumns(70)); 4603 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4604 " ? aaaaaaaaaaaaaaa\n" 4605 " : bbbbbbbbbbbbbbb //\n" 4606 " ? ccccccccccccccc\n" 4607 " : ddddddddddddddd;"); 4608 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4609 " ? aaaaaaaaaaaaaaa\n" 4610 " : (bbbbbbbbbbbbbbb //\n" 4611 " ? ccccccccccccccc\n" 4612 " : ddddddddddddddd);"); 4613 verifyFormat( 4614 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4615 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4616 " aaaaaaaaaaaaaaaaaaaaa +\n" 4617 " aaaaaaaaaaaaaaaaaaaaa\n" 4618 " : aaaaaaaaaa;"); 4619 verifyFormat( 4620 "aaaaaa = aaaaaaaaaaaa\n" 4621 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4622 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4623 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4624 4625 FormatStyle NoBinPacking = getLLVMStyle(); 4626 NoBinPacking.BinPackArguments = false; 4627 verifyFormat( 4628 "void f() {\n" 4629 " g(aaa,\n" 4630 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4632 " ? aaaaaaaaaaaaaaa\n" 4633 " : aaaaaaaaaaaaaaa);\n" 4634 "}", 4635 NoBinPacking); 4636 verifyFormat( 4637 "void f() {\n" 4638 " g(aaa,\n" 4639 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4641 " ?: aaaaaaaaaaaaaaa);\n" 4642 "}", 4643 NoBinPacking); 4644 4645 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4646 " // comment.\n" 4647 " ccccccccccccccccccccccccccccccccccccccc\n" 4648 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4649 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4650 4651 // Assignments in conditional expressions. Apparently not uncommon :-(. 4652 verifyFormat("return a != b\n" 4653 " // comment\n" 4654 " ? a = b\n" 4655 " : a = b;"); 4656 verifyFormat("return a != b\n" 4657 " // comment\n" 4658 " ? a = a != b\n" 4659 " // comment\n" 4660 " ? a = b\n" 4661 " : a\n" 4662 " : a;\n"); 4663 verifyFormat("return a != b\n" 4664 " // comment\n" 4665 " ? a\n" 4666 " : a = a != b\n" 4667 " // comment\n" 4668 " ? a = b\n" 4669 " : a;"); 4670 } 4671 4672 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4673 FormatStyle Style = getLLVMStyle(); 4674 Style.BreakBeforeTernaryOperators = false; 4675 Style.ColumnLimit = 70; 4676 verifyFormat( 4677 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4680 Style); 4681 verifyFormat( 4682 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4684 Style); 4685 verifyFormat( 4686 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4687 " aaaaaaaaaaaaa);", 4688 Style); 4689 verifyFormat( 4690 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4691 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4693 " aaaaaaaaaaaaa);", 4694 Style); 4695 verifyFormat( 4696 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4697 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4698 " aaaaaaaaaaaaa);", 4699 Style); 4700 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4705 Style); 4706 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4712 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4713 Style); 4714 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4716 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4718 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4719 Style); 4720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4721 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4723 Style); 4724 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4728 Style); 4729 verifyFormat( 4730 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4731 " aaaaaaaaaaaaaaa :\n" 4732 " aaaaaaaaaaaaaaa;", 4733 Style); 4734 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4735 " aaaaaaaaa ?\n" 4736 " b :\n" 4737 " c);", 4738 Style); 4739 verifyFormat( 4740 "unsigned Indent =\n" 4741 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4742 " IndentForLevel[TheLine.Level] :\n" 4743 " TheLine * 2,\n" 4744 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4745 Style); 4746 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4747 " aaaaaaaaaaaaaaa :\n" 4748 " bbbbbbbbbbbbbbb ? //\n" 4749 " ccccccccccccccc :\n" 4750 " ddddddddddddddd;", 4751 Style); 4752 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4753 " aaaaaaaaaaaaaaa :\n" 4754 " (bbbbbbbbbbbbbbb ? //\n" 4755 " ccccccccccccccc :\n" 4756 " ddddddddddddddd);", 4757 Style); 4758 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4759 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4760 " ccccccccccccccccccccccccccc;", 4761 Style); 4762 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4763 " aaaaa :\n" 4764 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4765 Style); 4766 } 4767 4768 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4769 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4770 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4771 verifyFormat("bool a = true, b = false;"); 4772 4773 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4775 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4776 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4777 verifyFormat( 4778 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4779 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4780 " d = e && f;"); 4781 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4782 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4783 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4784 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4785 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4786 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4787 4788 FormatStyle Style = getGoogleStyle(); 4789 Style.PointerAlignment = FormatStyle::PAS_Left; 4790 Style.DerivePointerAlignment = false; 4791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4792 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4793 " *b = bbbbbbbbbbbbbbbbbbb;", 4794 Style); 4795 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4796 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4797 Style); 4798 } 4799 4800 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4801 verifyFormat("arr[foo ? bar : baz];"); 4802 verifyFormat("f()[foo ? bar : baz];"); 4803 verifyFormat("(a + b)[foo ? bar : baz];"); 4804 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4805 } 4806 4807 TEST_F(FormatTest, AlignsStringLiterals) { 4808 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4809 " \"short literal\");"); 4810 verifyFormat( 4811 "looooooooooooooooooooooooongFunction(\n" 4812 " \"short literal\"\n" 4813 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4814 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4815 " \" string literals\",\n" 4816 " and, other, parameters);"); 4817 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4818 " \"5678\";", 4819 format("fun + \"1243\" /* comment */\n" 4820 " \"5678\";", 4821 getLLVMStyleWithColumns(28))); 4822 EXPECT_EQ( 4823 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4824 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4825 " \"aaaaaaaaaaaaaaaa\";", 4826 format("aaaaaa =" 4827 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4828 "aaaaaaaaaaaaaaaaaaaaa\" " 4829 "\"aaaaaaaaaaaaaaaa\";")); 4830 verifyFormat("a = a + \"a\"\n" 4831 " \"a\"\n" 4832 " \"a\";"); 4833 verifyFormat("f(\"a\", \"b\"\n" 4834 " \"c\");"); 4835 4836 verifyFormat( 4837 "#define LL_FORMAT \"ll\"\n" 4838 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4839 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4840 4841 verifyFormat("#define A(X) \\\n" 4842 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4843 " \"ccccc\"", 4844 getLLVMStyleWithColumns(23)); 4845 verifyFormat("#define A \"def\"\n" 4846 "f(\"abc\" A \"ghi\"\n" 4847 " \"jkl\");"); 4848 4849 verifyFormat("f(L\"a\"\n" 4850 " L\"b\");"); 4851 verifyFormat("#define A(X) \\\n" 4852 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4853 " L\"ccccc\"", 4854 getLLVMStyleWithColumns(25)); 4855 4856 verifyFormat("f(@\"a\"\n" 4857 " @\"b\");"); 4858 verifyFormat("NSString s = @\"a\"\n" 4859 " @\"b\"\n" 4860 " @\"c\";"); 4861 verifyFormat("NSString s = @\"a\"\n" 4862 " \"b\"\n" 4863 " \"c\";"); 4864 } 4865 4866 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4867 FormatStyle Style = getLLVMStyle(); 4868 // No declarations or definitions should be moved to own line. 4869 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4870 verifyFormat("class A {\n" 4871 " int f() { return 1; }\n" 4872 " int g();\n" 4873 "};\n" 4874 "int f() { return 1; }\n" 4875 "int g();\n", 4876 Style); 4877 4878 // All declarations and definitions should have the return type moved to its 4879 // own 4880 // line. 4881 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4882 verifyFormat("class E {\n" 4883 " int\n" 4884 " f() {\n" 4885 " return 1;\n" 4886 " }\n" 4887 " int\n" 4888 " g();\n" 4889 "};\n" 4890 "int\n" 4891 "f() {\n" 4892 " return 1;\n" 4893 "}\n" 4894 "int\n" 4895 "g();\n", 4896 Style); 4897 4898 // Top-level definitions, and no kinds of declarations should have the 4899 // return type moved to its own line. 4900 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4901 verifyFormat("class B {\n" 4902 " int f() { return 1; }\n" 4903 " int g();\n" 4904 "};\n" 4905 "int\n" 4906 "f() {\n" 4907 " return 1;\n" 4908 "}\n" 4909 "int g();\n", 4910 Style); 4911 4912 // Top-level definitions and declarations should have the return type moved 4913 // to its own line. 4914 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4915 verifyFormat("class C {\n" 4916 " int f() { return 1; }\n" 4917 " int g();\n" 4918 "};\n" 4919 "int\n" 4920 "f() {\n" 4921 " return 1;\n" 4922 "}\n" 4923 "int\n" 4924 "g();\n", 4925 Style); 4926 4927 // All definitions should have the return type moved to its own line, but no 4928 // kinds of declarations. 4929 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4930 verifyFormat("class D {\n" 4931 " int\n" 4932 " f() {\n" 4933 " return 1;\n" 4934 " }\n" 4935 " int g();\n" 4936 "};\n" 4937 "int\n" 4938 "f() {\n" 4939 " return 1;\n" 4940 "}\n" 4941 "int g();\n", 4942 Style); 4943 verifyFormat("const char *\n" 4944 "f(void) {\n" // Break here. 4945 " return \"\";\n" 4946 "}\n" 4947 "const char *bar(void);\n", // No break here. 4948 Style); 4949 verifyFormat("template <class T>\n" 4950 "T *\n" 4951 "f(T &c) {\n" // Break here. 4952 " return NULL;\n" 4953 "}\n" 4954 "template <class T> T *f(T &c);\n", // No break here. 4955 Style); 4956 verifyFormat("class C {\n" 4957 " int\n" 4958 " operator+() {\n" 4959 " return 1;\n" 4960 " }\n" 4961 " int\n" 4962 " operator()() {\n" 4963 " return 1;\n" 4964 " }\n" 4965 "};\n", 4966 Style); 4967 verifyFormat("void\n" 4968 "A::operator()() {}\n" 4969 "void\n" 4970 "A::operator>>() {}\n" 4971 "void\n" 4972 "A::operator+() {}\n", 4973 Style); 4974 verifyFormat("void *operator new(std::size_t s);", // No break here. 4975 Style); 4976 verifyFormat("void *\n" 4977 "operator new(std::size_t s) {}", 4978 Style); 4979 verifyFormat("void *\n" 4980 "operator delete[](void *ptr) {}", 4981 Style); 4982 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4983 verifyFormat("const char *\n" 4984 "f(void)\n" // Break here. 4985 "{\n" 4986 " return \"\";\n" 4987 "}\n" 4988 "const char *bar(void);\n", // No break here. 4989 Style); 4990 verifyFormat("template <class T>\n" 4991 "T *\n" // Problem here: no line break 4992 "f(T &c)\n" // Break here. 4993 "{\n" 4994 " return NULL;\n" 4995 "}\n" 4996 "template <class T> T *f(T &c);\n", // No break here. 4997 Style); 4998 } 4999 5000 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5001 FormatStyle NoBreak = getLLVMStyle(); 5002 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5003 FormatStyle Break = getLLVMStyle(); 5004 Break.AlwaysBreakBeforeMultilineStrings = true; 5005 verifyFormat("aaaa = \"bbbb\"\n" 5006 " \"cccc\";", 5007 NoBreak); 5008 verifyFormat("aaaa =\n" 5009 " \"bbbb\"\n" 5010 " \"cccc\";", 5011 Break); 5012 verifyFormat("aaaa(\"bbbb\"\n" 5013 " \"cccc\");", 5014 NoBreak); 5015 verifyFormat("aaaa(\n" 5016 " \"bbbb\"\n" 5017 " \"cccc\");", 5018 Break); 5019 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5020 " \"cccc\");", 5021 NoBreak); 5022 verifyFormat("aaaa(qqq,\n" 5023 " \"bbbb\"\n" 5024 " \"cccc\");", 5025 Break); 5026 verifyFormat("aaaa(qqq,\n" 5027 " L\"bbbb\"\n" 5028 " L\"cccc\");", 5029 Break); 5030 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5031 " \"bbbb\"));", 5032 Break); 5033 verifyFormat("string s = someFunction(\n" 5034 " \"abc\"\n" 5035 " \"abc\");", 5036 Break); 5037 5038 // As we break before unary operators, breaking right after them is bad. 5039 verifyFormat("string foo = abc ? \"x\"\n" 5040 " \"blah blah blah blah blah blah\"\n" 5041 " : \"y\";", 5042 Break); 5043 5044 // Don't break if there is no column gain. 5045 verifyFormat("f(\"aaaa\"\n" 5046 " \"bbbb\");", 5047 Break); 5048 5049 // Treat literals with escaped newlines like multi-line string literals. 5050 EXPECT_EQ("x = \"a\\\n" 5051 "b\\\n" 5052 "c\";", 5053 format("x = \"a\\\n" 5054 "b\\\n" 5055 "c\";", 5056 NoBreak)); 5057 EXPECT_EQ("xxxx =\n" 5058 " \"a\\\n" 5059 "b\\\n" 5060 "c\";", 5061 format("xxxx = \"a\\\n" 5062 "b\\\n" 5063 "c\";", 5064 Break)); 5065 5066 // Exempt ObjC strings for now. 5067 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5068 " @\"bbbb\";", 5069 format("NSString *const kString = @\"aaaa\"\n" 5070 "@\"bbbb\";", 5071 Break)); 5072 5073 Break.ColumnLimit = 0; 5074 verifyFormat("const char *hello = \"hello llvm\";", Break); 5075 } 5076 5077 TEST_F(FormatTest, AlignsPipes) { 5078 verifyFormat( 5079 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5080 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5081 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5082 verifyFormat( 5083 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5084 " << aaaaaaaaaaaaaaaaaaaa;"); 5085 verifyFormat( 5086 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5087 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5088 verifyFormat( 5089 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5090 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5091 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5092 verifyFormat( 5093 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5094 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5095 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5096 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5099 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5100 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5101 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5102 verifyFormat( 5103 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5105 5106 verifyFormat("return out << \"somepacket = {\\n\"\n" 5107 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5108 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5109 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5110 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5111 " << \"}\";"); 5112 5113 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5114 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5115 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5116 verifyFormat( 5117 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5118 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5119 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5120 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5121 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5122 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5123 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5124 verifyFormat( 5125 "void f() {\n" 5126 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5127 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5128 "}"); 5129 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5130 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5131 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5133 " aaaaaaaaaaaaaaaaaaaaa)\n" 5134 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5135 verifyFormat("LOG_IF(aaa == //\n" 5136 " bbb)\n" 5137 " << a << b;"); 5138 5139 // Breaking before the first "<<" is generally not desirable. 5140 verifyFormat( 5141 "llvm::errs()\n" 5142 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5143 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5144 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5145 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5146 getLLVMStyleWithColumns(70)); 5147 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5148 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5149 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5150 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5151 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5152 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5153 getLLVMStyleWithColumns(70)); 5154 5155 // But sometimes, breaking before the first "<<" is desirable. 5156 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5157 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5158 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5159 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5160 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5161 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5162 " << BEF << IsTemplate << Description << E->getType();"); 5163 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5164 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5166 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5167 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5169 " << aaa;"); 5170 5171 verifyFormat( 5172 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5173 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5174 5175 // Incomplete string literal. 5176 EXPECT_EQ("llvm::errs() << \"\n" 5177 " << a;", 5178 format("llvm::errs() << \"\n<<a;")); 5179 5180 verifyFormat("void f() {\n" 5181 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5182 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5183 "}"); 5184 5185 // Handle 'endl'. 5186 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5187 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5188 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5189 5190 // Handle '\n'. 5191 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5192 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5193 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5194 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5195 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5196 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5197 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5198 } 5199 5200 TEST_F(FormatTest, UnderstandsEquals) { 5201 verifyFormat( 5202 "aaaaaaaaaaaaaaaaa =\n" 5203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5204 verifyFormat( 5205 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5207 verifyFormat( 5208 "if (a) {\n" 5209 " f();\n" 5210 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5212 "}"); 5213 5214 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5215 " 100000000 + 10000000) {\n}"); 5216 } 5217 5218 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5219 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5220 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5221 5222 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5223 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5224 5225 verifyFormat( 5226 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5227 " Parameter2);"); 5228 5229 verifyFormat( 5230 "ShortObject->shortFunction(\n" 5231 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5232 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5233 5234 verifyFormat("loooooooooooooongFunction(\n" 5235 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5236 5237 verifyFormat( 5238 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5239 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5240 5241 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5242 " .WillRepeatedly(Return(SomeValue));"); 5243 verifyFormat("void f() {\n" 5244 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5245 " .Times(2)\n" 5246 " .WillRepeatedly(Return(SomeValue));\n" 5247 "}"); 5248 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5249 " ccccccccccccccccccccccc);"); 5250 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5252 " .aaaaa(aaaaa),\n" 5253 " aaaaaaaaaaaaaaaaaaaaa);"); 5254 verifyFormat("void f() {\n" 5255 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5256 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5257 "}"); 5258 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5259 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5260 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5261 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5262 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5263 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5264 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5265 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5266 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5267 "}"); 5268 5269 // Here, it is not necessary to wrap at "." or "->". 5270 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5271 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5272 verifyFormat( 5273 "aaaaaaaaaaa->aaaaaaaaa(\n" 5274 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5275 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5276 5277 verifyFormat( 5278 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5280 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5281 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5282 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5283 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5284 5285 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5287 " .a();"); 5288 5289 FormatStyle NoBinPacking = getLLVMStyle(); 5290 NoBinPacking.BinPackParameters = false; 5291 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5292 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5293 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5294 " aaaaaaaaaaaaaaaaaaa,\n" 5295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5296 NoBinPacking); 5297 5298 // If there is a subsequent call, change to hanging indentation. 5299 verifyFormat( 5300 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5301 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5303 verifyFormat( 5304 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5305 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5306 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5309 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5311 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5312 } 5313 5314 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5315 verifyFormat("template <typename T>\n" 5316 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5317 verifyFormat("template <typename T>\n" 5318 "// T should be one of {A, B}.\n" 5319 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5320 verifyFormat( 5321 "template <typename T>\n" 5322 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5323 verifyFormat("template <typename T>\n" 5324 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5325 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5326 verifyFormat( 5327 "template <typename T>\n" 5328 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5329 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5330 verifyFormat( 5331 "template <typename T>\n" 5332 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5333 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5335 verifyFormat("template <typename T>\n" 5336 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5337 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5338 verifyFormat( 5339 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5340 " typename T4 = char>\n" 5341 "void f();"); 5342 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5343 " template <typename> class cccccccccccccccccccccc,\n" 5344 " typename ddddddddddddd>\n" 5345 "class C {};"); 5346 verifyFormat( 5347 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5349 5350 verifyFormat("void f() {\n" 5351 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5352 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5353 "}"); 5354 5355 verifyFormat("template <typename T> class C {};"); 5356 verifyFormat("template <typename T> void f();"); 5357 verifyFormat("template <typename T> void f() {}"); 5358 verifyFormat( 5359 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5362 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5365 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5366 getLLVMStyleWithColumns(72)); 5367 EXPECT_EQ("static_cast<A< //\n" 5368 " B> *>(\n" 5369 "\n" 5370 " );", 5371 format("static_cast<A<//\n" 5372 " B>*>(\n" 5373 "\n" 5374 " );")); 5375 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5376 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5377 5378 FormatStyle AlwaysBreak = getLLVMStyle(); 5379 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5380 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5381 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5382 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5383 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5384 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5385 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5386 verifyFormat("template <template <typename> class Fooooooo,\n" 5387 " template <typename> class Baaaaaaar>\n" 5388 "struct C {};", 5389 AlwaysBreak); 5390 verifyFormat("template <typename T> // T can be A, B or C.\n" 5391 "struct C {};", 5392 AlwaysBreak); 5393 verifyFormat("template <enum E> class A {\n" 5394 "public:\n" 5395 " E *f();\n" 5396 "};"); 5397 } 5398 5399 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5400 verifyFormat( 5401 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5403 verifyFormat( 5404 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5407 5408 // FIXME: Should we have the extra indent after the second break? 5409 verifyFormat( 5410 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5413 5414 verifyFormat( 5415 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5416 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5417 5418 // Breaking at nested name specifiers is generally not desirable. 5419 verifyFormat( 5420 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5422 5423 verifyFormat( 5424 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5426 " aaaaaaaaaaaaaaaaaaaaa);", 5427 getLLVMStyleWithColumns(74)); 5428 5429 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5431 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5432 } 5433 5434 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5435 verifyFormat("A<int> a;"); 5436 verifyFormat("A<A<A<int>>> a;"); 5437 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5438 verifyFormat("bool x = a < 1 || 2 > a;"); 5439 verifyFormat("bool x = 5 < f<int>();"); 5440 verifyFormat("bool x = f<int>() > 5;"); 5441 verifyFormat("bool x = 5 < a<int>::x;"); 5442 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5443 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5444 5445 verifyGoogleFormat("A<A<int>> a;"); 5446 verifyGoogleFormat("A<A<A<int>>> a;"); 5447 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5448 verifyGoogleFormat("A<A<int> > a;"); 5449 verifyGoogleFormat("A<A<A<int> > > a;"); 5450 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5451 verifyGoogleFormat("A<::A<int>> a;"); 5452 verifyGoogleFormat("A<::A> a;"); 5453 verifyGoogleFormat("A< ::A> a;"); 5454 verifyGoogleFormat("A< ::A<int> > a;"); 5455 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5456 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5457 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5458 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5459 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5460 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5461 5462 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5463 5464 verifyFormat("test >> a >> b;"); 5465 verifyFormat("test << a >> b;"); 5466 5467 verifyFormat("f<int>();"); 5468 verifyFormat("template <typename T> void f() {}"); 5469 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5470 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5471 "sizeof(char)>::type>;"); 5472 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5473 verifyFormat("f(a.operator()<A>());"); 5474 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5475 " .template operator()<A>());", 5476 getLLVMStyleWithColumns(35)); 5477 5478 // Not template parameters. 5479 verifyFormat("return a < b && c > d;"); 5480 verifyFormat("void f() {\n" 5481 " while (a < b && c > d) {\n" 5482 " }\n" 5483 "}"); 5484 verifyFormat("template <typename... Types>\n" 5485 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5486 5487 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5489 getLLVMStyleWithColumns(60)); 5490 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5491 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5492 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5493 } 5494 5495 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5496 verifyFormat("COMPARE(a, ==, b);"); 5497 } 5498 5499 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5500 verifyFormat("int A::*x;"); 5501 verifyFormat("int (S::*func)(void *);"); 5502 verifyFormat("void f() { int (S::*func)(void *); }"); 5503 verifyFormat("typedef bool *(Class::*Member)() const;"); 5504 verifyFormat("void f() {\n" 5505 " (a->*f)();\n" 5506 " a->*x;\n" 5507 " (a.*f)();\n" 5508 " ((*a).*f)();\n" 5509 " a.*x;\n" 5510 "}"); 5511 verifyFormat("void f() {\n" 5512 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5513 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5514 "}"); 5515 verifyFormat( 5516 "(aaaaaaaaaa->*bbbbbbb)(\n" 5517 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5518 FormatStyle Style = getLLVMStyle(); 5519 Style.PointerAlignment = FormatStyle::PAS_Left; 5520 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5521 } 5522 5523 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5524 verifyFormat("int a = -2;"); 5525 verifyFormat("f(-1, -2, -3);"); 5526 verifyFormat("a[-1] = 5;"); 5527 verifyFormat("int a = 5 + -2;"); 5528 verifyFormat("if (i == -1) {\n}"); 5529 verifyFormat("if (i != -1) {\n}"); 5530 verifyFormat("if (i > -1) {\n}"); 5531 verifyFormat("if (i < -1) {\n}"); 5532 verifyFormat("++(a->f());"); 5533 verifyFormat("--(a->f());"); 5534 verifyFormat("(a->f())++;"); 5535 verifyFormat("a[42]++;"); 5536 verifyFormat("if (!(a->f())) {\n}"); 5537 5538 verifyFormat("a-- > b;"); 5539 verifyFormat("b ? -a : c;"); 5540 verifyFormat("n * sizeof char16;"); 5541 verifyFormat("n * alignof char16;", getGoogleStyle()); 5542 verifyFormat("sizeof(char);"); 5543 verifyFormat("alignof(char);", getGoogleStyle()); 5544 5545 verifyFormat("return -1;"); 5546 verifyFormat("switch (a) {\n" 5547 "case -1:\n" 5548 " break;\n" 5549 "}"); 5550 verifyFormat("#define X -1"); 5551 verifyFormat("#define X -kConstant"); 5552 5553 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5554 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5555 5556 verifyFormat("int a = /* confusing comment */ -1;"); 5557 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5558 verifyFormat("int a = i /* confusing comment */++;"); 5559 } 5560 5561 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5562 verifyFormat("if (!aaaaaaaaaa( // break\n" 5563 " aaaaa)) {\n" 5564 "}"); 5565 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5566 " aaaaa));"); 5567 verifyFormat("*aaa = aaaaaaa( // break\n" 5568 " bbbbbb);"); 5569 } 5570 5571 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5572 verifyFormat("bool operator<();"); 5573 verifyFormat("bool operator>();"); 5574 verifyFormat("bool operator=();"); 5575 verifyFormat("bool operator==();"); 5576 verifyFormat("bool operator!=();"); 5577 verifyFormat("int operator+();"); 5578 verifyFormat("int operator++();"); 5579 verifyFormat("bool operator,();"); 5580 verifyFormat("bool operator();"); 5581 verifyFormat("bool operator()();"); 5582 verifyFormat("bool operator[]();"); 5583 verifyFormat("operator bool();"); 5584 verifyFormat("operator int();"); 5585 verifyFormat("operator void *();"); 5586 verifyFormat("operator SomeType<int>();"); 5587 verifyFormat("operator SomeType<int, int>();"); 5588 verifyFormat("operator SomeType<SomeType<int>>();"); 5589 verifyFormat("void *operator new(std::size_t size);"); 5590 verifyFormat("void *operator new[](std::size_t size);"); 5591 verifyFormat("void operator delete(void *ptr);"); 5592 verifyFormat("void operator delete[](void *ptr);"); 5593 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5594 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5595 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5596 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5597 5598 verifyFormat( 5599 "ostream &operator<<(ostream &OutputStream,\n" 5600 " SomeReallyLongType WithSomeReallyLongValue);"); 5601 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5602 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5603 " return left.group < right.group;\n" 5604 "}"); 5605 verifyFormat("SomeType &operator=(const SomeType &S);"); 5606 verifyFormat("f.template operator()<int>();"); 5607 5608 verifyGoogleFormat("operator void*();"); 5609 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5610 verifyGoogleFormat("operator ::A();"); 5611 5612 verifyFormat("using A::operator+;"); 5613 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5614 "int i;"); 5615 } 5616 5617 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5618 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5619 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5620 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5621 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5622 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5623 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5624 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5625 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5626 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5627 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5628 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5629 5630 FormatStyle AlignLeft = getLLVMStyle(); 5631 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5632 verifyFormat("void A::b() && {}", AlignLeft); 5633 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5634 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5635 AlignLeft); 5636 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5637 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5638 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5639 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5640 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5641 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5642 5643 FormatStyle Spaces = getLLVMStyle(); 5644 Spaces.SpacesInCStyleCastParentheses = true; 5645 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5646 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5647 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5648 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5649 5650 Spaces.SpacesInCStyleCastParentheses = false; 5651 Spaces.SpacesInParentheses = true; 5652 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5653 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5654 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5655 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5656 } 5657 5658 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5659 verifyFormat("void f() {\n" 5660 " A *a = new A;\n" 5661 " A *a = new (placement) A;\n" 5662 " delete a;\n" 5663 " delete (A *)a;\n" 5664 "}"); 5665 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5666 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5667 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5668 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5669 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5670 verifyFormat("delete[] h->p;"); 5671 } 5672 5673 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5674 verifyFormat("int *f(int *a) {}"); 5675 verifyFormat("int main(int argc, char **argv) {}"); 5676 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5677 verifyIndependentOfContext("f(a, *a);"); 5678 verifyFormat("void g() { f(*a); }"); 5679 verifyIndependentOfContext("int a = b * 10;"); 5680 verifyIndependentOfContext("int a = 10 * b;"); 5681 verifyIndependentOfContext("int a = b * c;"); 5682 verifyIndependentOfContext("int a += b * c;"); 5683 verifyIndependentOfContext("int a -= b * c;"); 5684 verifyIndependentOfContext("int a *= b * c;"); 5685 verifyIndependentOfContext("int a /= b * c;"); 5686 verifyIndependentOfContext("int a = *b;"); 5687 verifyIndependentOfContext("int a = *b * c;"); 5688 verifyIndependentOfContext("int a = b * *c;"); 5689 verifyIndependentOfContext("int a = b * (10);"); 5690 verifyIndependentOfContext("S << b * (10);"); 5691 verifyIndependentOfContext("return 10 * b;"); 5692 verifyIndependentOfContext("return *b * *c;"); 5693 verifyIndependentOfContext("return a & ~b;"); 5694 verifyIndependentOfContext("f(b ? *c : *d);"); 5695 verifyIndependentOfContext("int a = b ? *c : *d;"); 5696 verifyIndependentOfContext("*b = a;"); 5697 verifyIndependentOfContext("a * ~b;"); 5698 verifyIndependentOfContext("a * !b;"); 5699 verifyIndependentOfContext("a * +b;"); 5700 verifyIndependentOfContext("a * -b;"); 5701 verifyIndependentOfContext("a * ++b;"); 5702 verifyIndependentOfContext("a * --b;"); 5703 verifyIndependentOfContext("a[4] * b;"); 5704 verifyIndependentOfContext("a[a * a] = 1;"); 5705 verifyIndependentOfContext("f() * b;"); 5706 verifyIndependentOfContext("a * [self dostuff];"); 5707 verifyIndependentOfContext("int x = a * (a + b);"); 5708 verifyIndependentOfContext("(a *)(a + b);"); 5709 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5710 verifyIndependentOfContext("int *pa = (int *)&a;"); 5711 verifyIndependentOfContext("return sizeof(int **);"); 5712 verifyIndependentOfContext("return sizeof(int ******);"); 5713 verifyIndependentOfContext("return (int **&)a;"); 5714 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5715 verifyFormat("void f(Type (*parameter)[10]) {}"); 5716 verifyFormat("void f(Type (¶meter)[10]) {}"); 5717 verifyGoogleFormat("return sizeof(int**);"); 5718 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5719 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5720 verifyFormat("auto a = [](int **&, int ***) {};"); 5721 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5722 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5723 verifyFormat("[](const decltype(*a) &value) {}"); 5724 verifyFormat("decltype(a * b) F();"); 5725 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5726 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5727 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5728 verifyIndependentOfContext("int i{a * b};"); 5729 verifyIndependentOfContext("aaa && aaa->f();"); 5730 verifyIndependentOfContext("int x = ~*p;"); 5731 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5732 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5733 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5734 verifyFormat("void f() { f(a, c * d); }"); 5735 verifyFormat("void f() { f(new a(), c * d); }"); 5736 5737 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5738 5739 verifyIndependentOfContext("A<int *> a;"); 5740 verifyIndependentOfContext("A<int **> a;"); 5741 verifyIndependentOfContext("A<int *, int *> a;"); 5742 verifyIndependentOfContext("A<int *[]> a;"); 5743 verifyIndependentOfContext( 5744 "const char *const p = reinterpret_cast<const char *const>(q);"); 5745 verifyIndependentOfContext("A<int **, int **> a;"); 5746 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5747 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5748 verifyFormat("for (; a && b;) {\n}"); 5749 verifyFormat("bool foo = true && [] { return false; }();"); 5750 5751 verifyFormat( 5752 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5754 5755 verifyGoogleFormat("**outparam = 1;"); 5756 verifyGoogleFormat("*outparam = a * b;"); 5757 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5758 verifyGoogleFormat("A<int*> a;"); 5759 verifyGoogleFormat("A<int**> a;"); 5760 verifyGoogleFormat("A<int*, int*> a;"); 5761 verifyGoogleFormat("A<int**, int**> a;"); 5762 verifyGoogleFormat("f(b ? *c : *d);"); 5763 verifyGoogleFormat("int a = b ? *c : *d;"); 5764 verifyGoogleFormat("Type* t = **x;"); 5765 verifyGoogleFormat("Type* t = *++*x;"); 5766 verifyGoogleFormat("*++*x;"); 5767 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5768 verifyGoogleFormat("Type* t = x++ * y;"); 5769 verifyGoogleFormat( 5770 "const char* const p = reinterpret_cast<const char* const>(q);"); 5771 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5772 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5773 verifyGoogleFormat("template <typename T>\n" 5774 "void f(int i = 0, SomeType** temps = NULL);"); 5775 5776 FormatStyle Left = getLLVMStyle(); 5777 Left.PointerAlignment = FormatStyle::PAS_Left; 5778 verifyFormat("x = *a(x) = *a(y);", Left); 5779 verifyFormat("for (;; * = b) {\n}", Left); 5780 verifyFormat("return *this += 1;", Left); 5781 5782 verifyIndependentOfContext("a = *(x + y);"); 5783 verifyIndependentOfContext("a = &(x + y);"); 5784 verifyIndependentOfContext("*(x + y).call();"); 5785 verifyIndependentOfContext("&(x + y)->call();"); 5786 verifyFormat("void f() { &(*I).first; }"); 5787 5788 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5789 verifyFormat( 5790 "int *MyValues = {\n" 5791 " *A, // Operator detection might be confused by the '{'\n" 5792 " *BB // Operator detection might be confused by previous comment\n" 5793 "};"); 5794 5795 verifyIndependentOfContext("if (int *a = &b)"); 5796 verifyIndependentOfContext("if (int &a = *b)"); 5797 verifyIndependentOfContext("if (a & b[i])"); 5798 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5799 verifyIndependentOfContext("if (*b[i])"); 5800 verifyIndependentOfContext("if (int *a = (&b))"); 5801 verifyIndependentOfContext("while (int *a = &b)"); 5802 verifyIndependentOfContext("size = sizeof *a;"); 5803 verifyIndependentOfContext("if (a && (b = c))"); 5804 verifyFormat("void f() {\n" 5805 " for (const int &v : Values) {\n" 5806 " }\n" 5807 "}"); 5808 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5809 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5810 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5811 5812 verifyFormat("#define A (!a * b)"); 5813 verifyFormat("#define MACRO \\\n" 5814 " int *i = a * b; \\\n" 5815 " void f(a *b);", 5816 getLLVMStyleWithColumns(19)); 5817 5818 verifyIndependentOfContext("A = new SomeType *[Length];"); 5819 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5820 verifyIndependentOfContext("T **t = new T *;"); 5821 verifyIndependentOfContext("T **t = new T *();"); 5822 verifyGoogleFormat("A = new SomeType*[Length]();"); 5823 verifyGoogleFormat("A = new SomeType*[Length];"); 5824 verifyGoogleFormat("T** t = new T*;"); 5825 verifyGoogleFormat("T** t = new T*();"); 5826 5827 FormatStyle PointerLeft = getLLVMStyle(); 5828 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5829 verifyFormat("delete *x;", PointerLeft); 5830 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5831 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5832 verifyFormat("template <bool a, bool b> " 5833 "typename t::if<x && y>::type f() {}"); 5834 verifyFormat("template <int *y> f() {}"); 5835 verifyFormat("vector<int *> v;"); 5836 verifyFormat("vector<int *const> v;"); 5837 verifyFormat("vector<int *const **const *> v;"); 5838 verifyFormat("vector<int *volatile> v;"); 5839 verifyFormat("vector<a * b> v;"); 5840 verifyFormat("foo<b && false>();"); 5841 verifyFormat("foo<b & 1>();"); 5842 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5843 verifyFormat( 5844 "template <class T, class = typename std::enable_if<\n" 5845 " std::is_integral<T>::value &&\n" 5846 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5847 "void F();", 5848 getLLVMStyleWithColumns(76)); 5849 verifyFormat( 5850 "template <class T,\n" 5851 " class = typename ::std::enable_if<\n" 5852 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5853 "void F();", 5854 getGoogleStyleWithColumns(68)); 5855 5856 verifyIndependentOfContext("MACRO(int *i);"); 5857 verifyIndependentOfContext("MACRO(auto *a);"); 5858 verifyIndependentOfContext("MACRO(const A *a);"); 5859 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5860 // FIXME: Is there a way to make this work? 5861 // verifyIndependentOfContext("MACRO(A *a);"); 5862 5863 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5864 verifyFormat("return options != nullptr && operator==(*options);"); 5865 5866 EXPECT_EQ("#define OP(x) \\\n" 5867 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5868 " return s << a.DebugString(); \\\n" 5869 " }", 5870 format("#define OP(x) \\\n" 5871 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5872 " return s << a.DebugString(); \\\n" 5873 " }", 5874 getLLVMStyleWithColumns(50))); 5875 5876 // FIXME: We cannot handle this case yet; we might be able to figure out that 5877 // foo<x> d > v; doesn't make sense. 5878 verifyFormat("foo<a<b && c> d> v;"); 5879 5880 FormatStyle PointerMiddle = getLLVMStyle(); 5881 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5882 verifyFormat("delete *x;", PointerMiddle); 5883 verifyFormat("int * x;", PointerMiddle); 5884 verifyFormat("template <int * y> f() {}", PointerMiddle); 5885 verifyFormat("int * f(int * a) {}", PointerMiddle); 5886 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5887 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5888 verifyFormat("A<int *> a;", PointerMiddle); 5889 verifyFormat("A<int **> a;", PointerMiddle); 5890 verifyFormat("A<int *, int *> a;", PointerMiddle); 5891 verifyFormat("A<int * []> a;", PointerMiddle); 5892 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5893 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5894 verifyFormat("T ** t = new T *;", PointerMiddle); 5895 5896 // Member function reference qualifiers aren't binary operators. 5897 verifyFormat("string // break\n" 5898 "operator()() & {}"); 5899 verifyFormat("string // break\n" 5900 "operator()() && {}"); 5901 verifyGoogleFormat("template <typename T>\n" 5902 "auto x() & -> int {}"); 5903 } 5904 5905 TEST_F(FormatTest, UnderstandsAttributes) { 5906 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5907 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5908 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5909 FormatStyle AfterType = getLLVMStyle(); 5910 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5911 verifyFormat("__attribute__((nodebug)) void\n" 5912 "foo() {}\n", 5913 AfterType); 5914 } 5915 5916 TEST_F(FormatTest, UnderstandsEllipsis) { 5917 verifyFormat("int printf(const char *fmt, ...);"); 5918 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5919 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5920 5921 FormatStyle PointersLeft = getLLVMStyle(); 5922 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5923 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5924 } 5925 5926 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5927 EXPECT_EQ("int *a;\n" 5928 "int *a;\n" 5929 "int *a;", 5930 format("int *a;\n" 5931 "int* a;\n" 5932 "int *a;", 5933 getGoogleStyle())); 5934 EXPECT_EQ("int* a;\n" 5935 "int* a;\n" 5936 "int* a;", 5937 format("int* a;\n" 5938 "int* a;\n" 5939 "int *a;", 5940 getGoogleStyle())); 5941 EXPECT_EQ("int *a;\n" 5942 "int *a;\n" 5943 "int *a;", 5944 format("int *a;\n" 5945 "int * a;\n" 5946 "int * a;", 5947 getGoogleStyle())); 5948 EXPECT_EQ("auto x = [] {\n" 5949 " int *a;\n" 5950 " int *a;\n" 5951 " int *a;\n" 5952 "};", 5953 format("auto x=[]{int *a;\n" 5954 "int * a;\n" 5955 "int * a;};", 5956 getGoogleStyle())); 5957 } 5958 5959 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5960 verifyFormat("int f(int &&a) {}"); 5961 verifyFormat("int f(int a, char &&b) {}"); 5962 verifyFormat("void f() { int &&a = b; }"); 5963 verifyGoogleFormat("int f(int a, char&& b) {}"); 5964 verifyGoogleFormat("void f() { int&& a = b; }"); 5965 5966 verifyIndependentOfContext("A<int &&> a;"); 5967 verifyIndependentOfContext("A<int &&, int &&> a;"); 5968 verifyGoogleFormat("A<int&&> a;"); 5969 verifyGoogleFormat("A<int&&, int&&> a;"); 5970 5971 // Not rvalue references: 5972 verifyFormat("template <bool B, bool C> class A {\n" 5973 " static_assert(B && C, \"Something is wrong\");\n" 5974 "};"); 5975 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5976 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5977 verifyFormat("#define A(a, b) (a && b)"); 5978 } 5979 5980 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5981 verifyFormat("void f() {\n" 5982 " x[aaaaaaaaa -\n" 5983 " b] = 23;\n" 5984 "}", 5985 getLLVMStyleWithColumns(15)); 5986 } 5987 5988 TEST_F(FormatTest, FormatsCasts) { 5989 verifyFormat("Type *A = static_cast<Type *>(P);"); 5990 verifyFormat("Type *A = (Type *)P;"); 5991 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5992 verifyFormat("int a = (int)(2.0f);"); 5993 verifyFormat("int a = (int)2.0f;"); 5994 verifyFormat("x[(int32)y];"); 5995 verifyFormat("x = (int32)y;"); 5996 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5997 verifyFormat("int a = (int)*b;"); 5998 verifyFormat("int a = (int)2.0f;"); 5999 verifyFormat("int a = (int)~0;"); 6000 verifyFormat("int a = (int)++a;"); 6001 verifyFormat("int a = (int)sizeof(int);"); 6002 verifyFormat("int a = (int)+2;"); 6003 verifyFormat("my_int a = (my_int)2.0f;"); 6004 verifyFormat("my_int a = (my_int)sizeof(int);"); 6005 verifyFormat("return (my_int)aaa;"); 6006 verifyFormat("#define x ((int)-1)"); 6007 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6008 verifyFormat("#define p(q) ((int *)&q)"); 6009 verifyFormat("fn(a)(b) + 1;"); 6010 6011 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6012 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6013 verifyFormat("my_int a = (my_int)~0;"); 6014 verifyFormat("my_int a = (my_int)++a;"); 6015 verifyFormat("my_int a = (my_int)-2;"); 6016 verifyFormat("my_int a = (my_int)1;"); 6017 verifyFormat("my_int a = (my_int *)1;"); 6018 verifyFormat("my_int a = (const my_int)-1;"); 6019 verifyFormat("my_int a = (const my_int *)-1;"); 6020 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6021 verifyFormat("my_int a = (ns::my_int)-2;"); 6022 verifyFormat("case (my_int)ONE:"); 6023 verifyFormat("auto x = (X)this;"); 6024 6025 // FIXME: single value wrapped with paren will be treated as cast. 6026 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6027 6028 verifyFormat("{ (void)F; }"); 6029 6030 // Don't break after a cast's 6031 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6032 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6033 " bbbbbbbbbbbbbbbbbbbbbb);"); 6034 6035 // These are not casts. 6036 verifyFormat("void f(int *) {}"); 6037 verifyFormat("f(foo)->b;"); 6038 verifyFormat("f(foo).b;"); 6039 verifyFormat("f(foo)(b);"); 6040 verifyFormat("f(foo)[b];"); 6041 verifyFormat("[](foo) { return 4; }(bar);"); 6042 verifyFormat("(*funptr)(foo)[4];"); 6043 verifyFormat("funptrs[4](foo)[4];"); 6044 verifyFormat("void f(int *);"); 6045 verifyFormat("void f(int *) = 0;"); 6046 verifyFormat("void f(SmallVector<int>) {}"); 6047 verifyFormat("void f(SmallVector<int>);"); 6048 verifyFormat("void f(SmallVector<int>) = 0;"); 6049 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6050 verifyFormat("int a = sizeof(int) * b;"); 6051 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6052 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6053 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6054 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6055 6056 // These are not casts, but at some point were confused with casts. 6057 verifyFormat("virtual void foo(int *) override;"); 6058 verifyFormat("virtual void foo(char &) const;"); 6059 verifyFormat("virtual void foo(int *a, char *) const;"); 6060 verifyFormat("int a = sizeof(int *) + b;"); 6061 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6062 verifyFormat("bool b = f(g<int>) && c;"); 6063 verifyFormat("typedef void (*f)(int i) func;"); 6064 6065 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6066 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6067 // FIXME: The indentation here is not ideal. 6068 verifyFormat( 6069 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6070 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6071 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6072 } 6073 6074 TEST_F(FormatTest, FormatsFunctionTypes) { 6075 verifyFormat("A<bool()> a;"); 6076 verifyFormat("A<SomeType()> a;"); 6077 verifyFormat("A<void (*)(int, std::string)> a;"); 6078 verifyFormat("A<void *(int)>;"); 6079 verifyFormat("void *(*a)(int *, SomeType *);"); 6080 verifyFormat("int (*func)(void *);"); 6081 verifyFormat("void f() { int (*func)(void *); }"); 6082 verifyFormat("template <class CallbackClass>\n" 6083 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6084 6085 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6086 verifyGoogleFormat("void* (*a)(int);"); 6087 verifyGoogleFormat( 6088 "template <class CallbackClass>\n" 6089 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6090 6091 // Other constructs can look somewhat like function types: 6092 verifyFormat("A<sizeof(*x)> a;"); 6093 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6094 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6095 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6096 verifyFormat("int x = f(&h)();"); 6097 } 6098 6099 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6100 verifyFormat("A (*foo_)[6];"); 6101 verifyFormat("vector<int> (*foo_)[6];"); 6102 } 6103 6104 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6105 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6106 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6107 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6108 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6109 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6110 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6111 6112 // Different ways of ()-initializiation. 6113 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6114 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6115 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6116 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6117 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6118 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6119 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6120 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6121 } 6122 6123 TEST_F(FormatTest, BreaksLongDeclarations) { 6124 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6125 " AnotherNameForTheLongType;"); 6126 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6128 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6129 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6130 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6131 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6132 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6133 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6134 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6135 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6136 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6137 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6138 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6139 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6140 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6141 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6142 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6143 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6144 FormatStyle Indented = getLLVMStyle(); 6145 Indented.IndentWrappedFunctionNames = true; 6146 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6147 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6148 Indented); 6149 verifyFormat( 6150 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6151 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6152 Indented); 6153 verifyFormat( 6154 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6155 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6156 Indented); 6157 verifyFormat( 6158 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6159 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6160 Indented); 6161 6162 // FIXME: Without the comment, this breaks after "(". 6163 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6164 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6165 getGoogleStyle()); 6166 6167 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6168 " int LoooooooooooooooooooongParam2) {}"); 6169 verifyFormat( 6170 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6171 " SourceLocation L, IdentifierIn *II,\n" 6172 " Type *T) {}"); 6173 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6174 "ReallyReaaallyLongFunctionName(\n" 6175 " const std::string &SomeParameter,\n" 6176 " const SomeType<string, SomeOtherTemplateParameter>\n" 6177 " &ReallyReallyLongParameterName,\n" 6178 " const SomeType<string, SomeOtherTemplateParameter>\n" 6179 " &AnotherLongParameterName) {}"); 6180 verifyFormat("template <typename A>\n" 6181 "SomeLoooooooooooooooooooooongType<\n" 6182 " typename some_namespace::SomeOtherType<A>::Type>\n" 6183 "Function() {}"); 6184 6185 verifyGoogleFormat( 6186 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6187 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6188 verifyGoogleFormat( 6189 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6190 " SourceLocation L) {}"); 6191 verifyGoogleFormat( 6192 "some_namespace::LongReturnType\n" 6193 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6194 " int first_long_parameter, int second_parameter) {}"); 6195 6196 verifyGoogleFormat("template <typename T>\n" 6197 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6198 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6199 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6200 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6201 6202 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6203 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6204 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6205 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6206 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6207 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6208 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6209 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6212 } 6213 6214 TEST_F(FormatTest, FormatsArrays) { 6215 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6216 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6217 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6218 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6219 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6220 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6221 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6222 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6223 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6224 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6225 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6226 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6227 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6228 verifyFormat( 6229 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6230 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6231 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6232 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6233 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6234 6235 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6237 verifyFormat( 6238 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6239 " .aaaaaaa[0]\n" 6240 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6241 verifyFormat("a[::b::c];"); 6242 6243 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6244 6245 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6246 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6247 } 6248 6249 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6250 verifyFormat("(a)->b();"); 6251 verifyFormat("--a;"); 6252 } 6253 6254 TEST_F(FormatTest, HandlesIncludeDirectives) { 6255 verifyFormat("#include <string>\n" 6256 "#include <a/b/c.h>\n" 6257 "#include \"a/b/string\"\n" 6258 "#include \"string.h\"\n" 6259 "#include \"string.h\"\n" 6260 "#include <a-a>\n" 6261 "#include < path with space >\n" 6262 "#include_next <test.h>" 6263 "#include \"abc.h\" // this is included for ABC\n" 6264 "#include \"some long include\" // with a comment\n" 6265 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6266 getLLVMStyleWithColumns(35)); 6267 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6268 EXPECT_EQ("#include <a>", format("#include<a>")); 6269 6270 verifyFormat("#import <string>"); 6271 verifyFormat("#import <a/b/c.h>"); 6272 verifyFormat("#import \"a/b/string\""); 6273 verifyFormat("#import \"string.h\""); 6274 verifyFormat("#import \"string.h\""); 6275 verifyFormat("#if __has_include(<strstream>)\n" 6276 "#include <strstream>\n" 6277 "#endif"); 6278 6279 verifyFormat("#define MY_IMPORT <a/b>"); 6280 6281 // Protocol buffer definition or missing "#". 6282 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6283 getLLVMStyleWithColumns(30)); 6284 6285 FormatStyle Style = getLLVMStyle(); 6286 Style.AlwaysBreakBeforeMultilineStrings = true; 6287 Style.ColumnLimit = 0; 6288 verifyFormat("#import \"abc.h\"", Style); 6289 6290 // But 'import' might also be a regular C++ namespace. 6291 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6293 } 6294 6295 //===----------------------------------------------------------------------===// 6296 // Error recovery tests. 6297 //===----------------------------------------------------------------------===// 6298 6299 TEST_F(FormatTest, IncompleteParameterLists) { 6300 FormatStyle NoBinPacking = getLLVMStyle(); 6301 NoBinPacking.BinPackParameters = false; 6302 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6303 " double *min_x,\n" 6304 " double *max_x,\n" 6305 " double *min_y,\n" 6306 " double *max_y,\n" 6307 " double *min_z,\n" 6308 " double *max_z, ) {}", 6309 NoBinPacking); 6310 } 6311 6312 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6313 verifyFormat("void f() { return; }\n42"); 6314 verifyFormat("void f() {\n" 6315 " if (0)\n" 6316 " return;\n" 6317 "}\n" 6318 "42"); 6319 verifyFormat("void f() { return }\n42"); 6320 verifyFormat("void f() {\n" 6321 " if (0)\n" 6322 " return\n" 6323 "}\n" 6324 "42"); 6325 } 6326 6327 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6328 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6329 EXPECT_EQ("void f() {\n" 6330 " if (a)\n" 6331 " return\n" 6332 "}", 6333 format("void f ( ) { if ( a ) return }")); 6334 EXPECT_EQ("namespace N {\n" 6335 "void f()\n" 6336 "}", 6337 format("namespace N { void f() }")); 6338 EXPECT_EQ("namespace N {\n" 6339 "void f() {}\n" 6340 "void g()\n" 6341 "}", 6342 format("namespace N { void f( ) { } void g( ) }")); 6343 } 6344 6345 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6346 verifyFormat("int aaaaaaaa =\n" 6347 " // Overlylongcomment\n" 6348 " b;", 6349 getLLVMStyleWithColumns(20)); 6350 verifyFormat("function(\n" 6351 " ShortArgument,\n" 6352 " LoooooooooooongArgument);\n", 6353 getLLVMStyleWithColumns(20)); 6354 } 6355 6356 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6357 verifyFormat("public:"); 6358 verifyFormat("class A {\n" 6359 "public\n" 6360 " void f() {}\n" 6361 "};"); 6362 verifyFormat("public\n" 6363 "int qwerty;"); 6364 verifyFormat("public\n" 6365 "B {}"); 6366 verifyFormat("public\n" 6367 "{}"); 6368 verifyFormat("public\n" 6369 "B { int x; }"); 6370 } 6371 6372 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6373 verifyFormat("{"); 6374 verifyFormat("#})"); 6375 verifyNoCrash("(/**/[:!] ?[)."); 6376 } 6377 6378 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6379 verifyFormat("do {\n}"); 6380 verifyFormat("do {\n}\n" 6381 "f();"); 6382 verifyFormat("do {\n}\n" 6383 "wheeee(fun);"); 6384 verifyFormat("do {\n" 6385 " f();\n" 6386 "}"); 6387 } 6388 6389 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6390 verifyFormat("if {\n foo;\n foo();\n}"); 6391 verifyFormat("switch {\n foo;\n foo();\n}"); 6392 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6393 verifyFormat("while {\n foo;\n foo();\n}"); 6394 verifyFormat("do {\n foo;\n foo();\n} while;"); 6395 } 6396 6397 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6398 verifyIncompleteFormat("namespace {\n" 6399 "class Foo { Foo (\n" 6400 "};\n" 6401 "} // comment"); 6402 } 6403 6404 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6405 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6406 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6407 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6408 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6409 6410 EXPECT_EQ("{\n" 6411 " {\n" 6412 " breakme(\n" 6413 " qwe);\n" 6414 " }\n", 6415 format("{\n" 6416 " {\n" 6417 " breakme(qwe);\n" 6418 "}\n", 6419 getLLVMStyleWithColumns(10))); 6420 } 6421 6422 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6423 verifyFormat("int x = {\n" 6424 " avariable,\n" 6425 " b(alongervariable)};", 6426 getLLVMStyleWithColumns(25)); 6427 } 6428 6429 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6430 verifyFormat("return (a)(b){1, 2, 3};"); 6431 } 6432 6433 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6434 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6435 verifyFormat("vector<int> x{\n" 6436 " 1, 2, 3, 4,\n" 6437 "};"); 6438 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6439 verifyFormat("f({1, 2});"); 6440 verifyFormat("auto v = Foo{-1};"); 6441 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6442 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6443 verifyFormat("new vector<int>{1, 2, 3};"); 6444 verifyFormat("new int[3]{1, 2, 3};"); 6445 verifyFormat("new int{1};"); 6446 verifyFormat("return {arg1, arg2};"); 6447 verifyFormat("return {arg1, SomeType{parameter}};"); 6448 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6449 verifyFormat("new T{arg1, arg2};"); 6450 verifyFormat("f(MyMap[{composite, key}]);"); 6451 verifyFormat("class Class {\n" 6452 " T member = {arg1, arg2};\n" 6453 "};"); 6454 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6455 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6456 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6457 6458 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6459 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6460 verifyFormat("auto i = decltype(x){};"); 6461 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6462 verifyFormat("Node n{1, Node{1000}, //\n" 6463 " 2};"); 6464 verifyFormat("Aaaa aaaaaaa{\n" 6465 " {\n" 6466 " aaaa,\n" 6467 " },\n" 6468 "};"); 6469 verifyFormat("class C : public D {\n" 6470 " SomeClass SC{2};\n" 6471 "};"); 6472 verifyFormat("class C : public A {\n" 6473 " class D : public B {\n" 6474 " void f() { int i{2}; }\n" 6475 " };\n" 6476 "};"); 6477 verifyFormat("#define A {a, a},"); 6478 6479 // In combination with BinPackArguments = false. 6480 FormatStyle NoBinPacking = getLLVMStyle(); 6481 NoBinPacking.BinPackArguments = false; 6482 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6483 " bbbbb,\n" 6484 " ccccc,\n" 6485 " ddddd,\n" 6486 " eeeee,\n" 6487 " ffffff,\n" 6488 " ggggg,\n" 6489 " hhhhhh,\n" 6490 " iiiiii,\n" 6491 " jjjjjj,\n" 6492 " kkkkkk};", 6493 NoBinPacking); 6494 verifyFormat("const Aaaaaa aaaaa = {\n" 6495 " aaaaa,\n" 6496 " bbbbb,\n" 6497 " ccccc,\n" 6498 " ddddd,\n" 6499 " eeeee,\n" 6500 " ffffff,\n" 6501 " ggggg,\n" 6502 " hhhhhh,\n" 6503 " iiiiii,\n" 6504 " jjjjjj,\n" 6505 " kkkkkk,\n" 6506 "};", 6507 NoBinPacking); 6508 verifyFormat( 6509 "const Aaaaaa aaaaa = {\n" 6510 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6511 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6512 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6513 "};", 6514 NoBinPacking); 6515 6516 // FIXME: The alignment of these trailing comments might be bad. Then again, 6517 // this might be utterly useless in real code. 6518 verifyFormat("Constructor::Constructor()\n" 6519 " : some_value{ //\n" 6520 " aaaaaaa, //\n" 6521 " bbbbbbb} {}"); 6522 6523 // In braced lists, the first comment is always assumed to belong to the 6524 // first element. Thus, it can be moved to the next or previous line as 6525 // appropriate. 6526 EXPECT_EQ("function({// First element:\n" 6527 " 1,\n" 6528 " // Second element:\n" 6529 " 2});", 6530 format("function({\n" 6531 " // First element:\n" 6532 " 1,\n" 6533 " // Second element:\n" 6534 " 2});")); 6535 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6536 " // First element:\n" 6537 " 1,\n" 6538 " // Second element:\n" 6539 " 2};", 6540 format("std::vector<int> MyNumbers{// First element:\n" 6541 " 1,\n" 6542 " // Second element:\n" 6543 " 2};", 6544 getLLVMStyleWithColumns(30))); 6545 // A trailing comma should still lead to an enforced line break. 6546 EXPECT_EQ("vector<int> SomeVector = {\n" 6547 " // aaa\n" 6548 " 1, 2,\n" 6549 "};", 6550 format("vector<int> SomeVector = { // aaa\n" 6551 " 1, 2, };")); 6552 6553 FormatStyle ExtraSpaces = getLLVMStyle(); 6554 ExtraSpaces.Cpp11BracedListStyle = false; 6555 ExtraSpaces.ColumnLimit = 75; 6556 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6557 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6558 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6559 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6560 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6561 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6562 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6563 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6564 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6565 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6566 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6567 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6568 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6569 verifyFormat("class Class {\n" 6570 " T member = { arg1, arg2 };\n" 6571 "};", 6572 ExtraSpaces); 6573 verifyFormat( 6574 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6575 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6576 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6577 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6578 ExtraSpaces); 6579 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6580 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6581 ExtraSpaces); 6582 verifyFormat( 6583 "someFunction(OtherParam,\n" 6584 " BracedList{ // comment 1 (Forcing interesting break)\n" 6585 " param1, param2,\n" 6586 " // comment 2\n" 6587 " param3, param4 });", 6588 ExtraSpaces); 6589 verifyFormat( 6590 "std::this_thread::sleep_for(\n" 6591 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6592 ExtraSpaces); 6593 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6594 " aaaaaaa,\n" 6595 " aaaaaaaaaa,\n" 6596 " aaaaa,\n" 6597 " aaaaaaaaaaaaaaa,\n" 6598 " aaa,\n" 6599 " aaaaaaaaaa,\n" 6600 " a,\n" 6601 " aaaaaaaaaaaaaaaaaaaaa,\n" 6602 " aaaaaaaaaaaa,\n" 6603 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6604 " aaaaaaa,\n" 6605 " a};"); 6606 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6607 } 6608 6609 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6610 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6611 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6612 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6613 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6614 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6615 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6616 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6617 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6618 " 1, 22, 333, 4444, 55555, //\n" 6619 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6620 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6621 verifyFormat( 6622 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6623 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6624 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6625 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6626 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6627 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6628 " 7777777};"); 6629 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6630 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6631 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6632 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6633 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6634 " // Separating comment.\n" 6635 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6636 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6637 " // Leading comment\n" 6638 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6639 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6640 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6641 " 1, 1, 1, 1};", 6642 getLLVMStyleWithColumns(39)); 6643 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6644 " 1, 1, 1, 1};", 6645 getLLVMStyleWithColumns(38)); 6646 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6647 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6648 getLLVMStyleWithColumns(43)); 6649 verifyFormat( 6650 "static unsigned SomeValues[10][3] = {\n" 6651 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6652 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6653 verifyFormat("static auto fields = new vector<string>{\n" 6654 " \"aaaaaaaaaaaaa\",\n" 6655 " \"aaaaaaaaaaaaa\",\n" 6656 " \"aaaaaaaaaaaa\",\n" 6657 " \"aaaaaaaaaaaaaa\",\n" 6658 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6659 " \"aaaaaaaaaaaa\",\n" 6660 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6661 "};"); 6662 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6663 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6664 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6665 " 3, cccccccccccccccccccccc};", 6666 getLLVMStyleWithColumns(60)); 6667 6668 // Trailing commas. 6669 verifyFormat("vector<int> x = {\n" 6670 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6671 "};", 6672 getLLVMStyleWithColumns(39)); 6673 verifyFormat("vector<int> x = {\n" 6674 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6675 "};", 6676 getLLVMStyleWithColumns(39)); 6677 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6678 " 1, 1, 1, 1,\n" 6679 " /**/ /**/};", 6680 getLLVMStyleWithColumns(39)); 6681 6682 // Trailing comment in the first line. 6683 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6684 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6685 " 111111111, 222222222, 3333333333, 444444444, //\n" 6686 " 11111111, 22222222, 333333333, 44444444};"); 6687 // Trailing comment in the last line. 6688 verifyFormat("int aaaaa[] = {\n" 6689 " 1, 2, 3, // comment\n" 6690 " 4, 5, 6 // comment\n" 6691 "};"); 6692 6693 // With nested lists, we should either format one item per line or all nested 6694 // lists one on line. 6695 // FIXME: For some nested lists, we can do better. 6696 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6697 " {aaaaaaaaaaaaaaaaaaa},\n" 6698 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6699 " {aaaaaaaaaaaaaaaaa}};", 6700 getLLVMStyleWithColumns(60)); 6701 verifyFormat( 6702 "SomeStruct my_struct_array = {\n" 6703 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6704 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6705 " {aaa, aaa},\n" 6706 " {aaa, aaa},\n" 6707 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6708 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6709 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6710 6711 // No column layout should be used here. 6712 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6713 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6714 6715 verifyNoCrash("a<,"); 6716 6717 // No braced initializer here. 6718 verifyFormat("void f() {\n" 6719 " struct Dummy {};\n" 6720 " f(v);\n" 6721 "}"); 6722 6723 // Long lists should be formatted in columns even if they are nested. 6724 verifyFormat( 6725 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6726 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6727 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6728 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6729 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6730 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6731 } 6732 6733 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6734 FormatStyle DoNotMerge = getLLVMStyle(); 6735 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6736 6737 verifyFormat("void f() { return 42; }"); 6738 verifyFormat("void f() {\n" 6739 " return 42;\n" 6740 "}", 6741 DoNotMerge); 6742 verifyFormat("void f() {\n" 6743 " // Comment\n" 6744 "}"); 6745 verifyFormat("{\n" 6746 "#error {\n" 6747 " int a;\n" 6748 "}"); 6749 verifyFormat("{\n" 6750 " int a;\n" 6751 "#error {\n" 6752 "}"); 6753 verifyFormat("void f() {} // comment"); 6754 verifyFormat("void f() { int a; } // comment"); 6755 verifyFormat("void f() {\n" 6756 "} // comment", 6757 DoNotMerge); 6758 verifyFormat("void f() {\n" 6759 " int a;\n" 6760 "} // comment", 6761 DoNotMerge); 6762 verifyFormat("void f() {\n" 6763 "} // comment", 6764 getLLVMStyleWithColumns(15)); 6765 6766 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6767 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6768 6769 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6770 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6771 verifyFormat("class C {\n" 6772 " C()\n" 6773 " : iiiiiiii(nullptr),\n" 6774 " kkkkkkk(nullptr),\n" 6775 " mmmmmmm(nullptr),\n" 6776 " nnnnnnn(nullptr) {}\n" 6777 "};", 6778 getGoogleStyle()); 6779 6780 FormatStyle NoColumnLimit = getLLVMStyle(); 6781 NoColumnLimit.ColumnLimit = 0; 6782 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6783 EXPECT_EQ("class C {\n" 6784 " A() : b(0) {}\n" 6785 "};", 6786 format("class C{A():b(0){}};", NoColumnLimit)); 6787 EXPECT_EQ("A()\n" 6788 " : b(0) {\n" 6789 "}", 6790 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6791 6792 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6793 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6794 FormatStyle::SFS_None; 6795 EXPECT_EQ("A()\n" 6796 " : b(0) {\n" 6797 "}", 6798 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6799 EXPECT_EQ("A()\n" 6800 " : b(0) {\n" 6801 "}", 6802 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6803 6804 verifyFormat("#define A \\\n" 6805 " void f() { \\\n" 6806 " int i; \\\n" 6807 " }", 6808 getLLVMStyleWithColumns(20)); 6809 verifyFormat("#define A \\\n" 6810 " void f() { int i; }", 6811 getLLVMStyleWithColumns(21)); 6812 verifyFormat("#define A \\\n" 6813 " void f() { \\\n" 6814 " int i; \\\n" 6815 " } \\\n" 6816 " int j;", 6817 getLLVMStyleWithColumns(22)); 6818 verifyFormat("#define A \\\n" 6819 " void f() { int i; } \\\n" 6820 " int j;", 6821 getLLVMStyleWithColumns(23)); 6822 } 6823 6824 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6825 FormatStyle MergeInlineOnly = getLLVMStyle(); 6826 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6827 verifyFormat("class C {\n" 6828 " int f() { return 42; }\n" 6829 "};", 6830 MergeInlineOnly); 6831 verifyFormat("int f() {\n" 6832 " return 42;\n" 6833 "}", 6834 MergeInlineOnly); 6835 } 6836 6837 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6838 // Elaborate type variable declarations. 6839 verifyFormat("struct foo a = {bar};\nint n;"); 6840 verifyFormat("class foo a = {bar};\nint n;"); 6841 verifyFormat("union foo a = {bar};\nint n;"); 6842 6843 // Elaborate types inside function definitions. 6844 verifyFormat("struct foo f() {}\nint n;"); 6845 verifyFormat("class foo f() {}\nint n;"); 6846 verifyFormat("union foo f() {}\nint n;"); 6847 6848 // Templates. 6849 verifyFormat("template <class X> void f() {}\nint n;"); 6850 verifyFormat("template <struct X> void f() {}\nint n;"); 6851 verifyFormat("template <union X> void f() {}\nint n;"); 6852 6853 // Actual definitions... 6854 verifyFormat("struct {\n} n;"); 6855 verifyFormat( 6856 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6857 verifyFormat("union Z {\n int n;\n} x;"); 6858 verifyFormat("class MACRO Z {\n} n;"); 6859 verifyFormat("class MACRO(X) Z {\n} n;"); 6860 verifyFormat("class __attribute__(X) Z {\n} n;"); 6861 verifyFormat("class __declspec(X) Z {\n} n;"); 6862 verifyFormat("class A##B##C {\n} n;"); 6863 verifyFormat("class alignas(16) Z {\n} n;"); 6864 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6865 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6866 6867 // Redefinition from nested context: 6868 verifyFormat("class A::B::C {\n} n;"); 6869 6870 // Template definitions. 6871 verifyFormat( 6872 "template <typename F>\n" 6873 "Matcher(const Matcher<F> &Other,\n" 6874 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6875 " !is_same<F, T>::value>::type * = 0)\n" 6876 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6877 6878 // FIXME: This is still incorrectly handled at the formatter side. 6879 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6880 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6881 6882 // FIXME: 6883 // This now gets parsed incorrectly as class definition. 6884 // verifyFormat("class A<int> f() {\n}\nint n;"); 6885 6886 // Elaborate types where incorrectly parsing the structural element would 6887 // break the indent. 6888 verifyFormat("if (true)\n" 6889 " class X x;\n" 6890 "else\n" 6891 " f();\n"); 6892 6893 // This is simply incomplete. Formatting is not important, but must not crash. 6894 verifyFormat("class A:"); 6895 } 6896 6897 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6898 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6899 format("#error Leave all white!!!!! space* alone!\n")); 6900 EXPECT_EQ( 6901 "#warning Leave all white!!!!! space* alone!\n", 6902 format("#warning Leave all white!!!!! space* alone!\n")); 6903 EXPECT_EQ("#error 1", format(" # error 1")); 6904 EXPECT_EQ("#warning 1", format(" # warning 1")); 6905 } 6906 6907 TEST_F(FormatTest, FormatHashIfExpressions) { 6908 verifyFormat("#if AAAA && BBBB"); 6909 verifyFormat("#if (AAAA && BBBB)"); 6910 verifyFormat("#elif (AAAA && BBBB)"); 6911 // FIXME: Come up with a better indentation for #elif. 6912 verifyFormat( 6913 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6914 " defined(BBBBBBBB)\n" 6915 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6916 " defined(BBBBBBBB)\n" 6917 "#endif", 6918 getLLVMStyleWithColumns(65)); 6919 } 6920 6921 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6922 FormatStyle AllowsMergedIf = getGoogleStyle(); 6923 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6924 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6925 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6926 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6927 EXPECT_EQ("if (true) return 42;", 6928 format("if (true)\nreturn 42;", AllowsMergedIf)); 6929 FormatStyle ShortMergedIf = AllowsMergedIf; 6930 ShortMergedIf.ColumnLimit = 25; 6931 verifyFormat("#define A \\\n" 6932 " if (true) return 42;", 6933 ShortMergedIf); 6934 verifyFormat("#define A \\\n" 6935 " f(); \\\n" 6936 " if (true)\n" 6937 "#define B", 6938 ShortMergedIf); 6939 verifyFormat("#define A \\\n" 6940 " f(); \\\n" 6941 " if (true)\n" 6942 "g();", 6943 ShortMergedIf); 6944 verifyFormat("{\n" 6945 "#ifdef A\n" 6946 " // Comment\n" 6947 " if (true) continue;\n" 6948 "#endif\n" 6949 " // Comment\n" 6950 " if (true) continue;\n" 6951 "}", 6952 ShortMergedIf); 6953 ShortMergedIf.ColumnLimit = 29; 6954 verifyFormat("#define A \\\n" 6955 " if (aaaaaaaaaa) return 1; \\\n" 6956 " return 2;", 6957 ShortMergedIf); 6958 ShortMergedIf.ColumnLimit = 28; 6959 verifyFormat("#define A \\\n" 6960 " if (aaaaaaaaaa) \\\n" 6961 " return 1; \\\n" 6962 " return 2;", 6963 ShortMergedIf); 6964 } 6965 6966 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6967 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6968 " f();\n" 6969 "}"); 6970 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6971 " f();\n" 6972 "} /* another comment */ else /* comment #3 */ {\n" 6973 " g();\n" 6974 "}"); 6975 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6976 " f();\n" 6977 "}"); 6978 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6979 " f();\n" 6980 "}"); 6981 verifyFormat("do /* a comment in a strange place */ {\n" 6982 " f();\n" 6983 "} /* another comment */ while (0);"); 6984 } 6985 6986 TEST_F(FormatTest, BlockComments) { 6987 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6988 format("/* *//* */ /* */\n/* *//* */ /* */")); 6989 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6990 EXPECT_EQ("#define A /*123*/ \\\n" 6991 " b\n" 6992 "/* */\n" 6993 "someCall(\n" 6994 " parameter);", 6995 format("#define A /*123*/ b\n" 6996 "/* */\n" 6997 "someCall(parameter);", 6998 getLLVMStyleWithColumns(15))); 6999 7000 EXPECT_EQ("#define A\n" 7001 "/* */ someCall(\n" 7002 " parameter);", 7003 format("#define A\n" 7004 "/* */someCall(parameter);", 7005 getLLVMStyleWithColumns(15))); 7006 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 7007 EXPECT_EQ("/*\n" 7008 "*\n" 7009 " * aaaaaa\n" 7010 " * aaaaaa\n" 7011 "*/", 7012 format("/*\n" 7013 "*\n" 7014 " * aaaaaa aaaaaa\n" 7015 "*/", 7016 getLLVMStyleWithColumns(10))); 7017 EXPECT_EQ("/*\n" 7018 "**\n" 7019 "* aaaaaa\n" 7020 "*aaaaaa\n" 7021 "*/", 7022 format("/*\n" 7023 "**\n" 7024 "* aaaaaa aaaaaa\n" 7025 "*/", 7026 getLLVMStyleWithColumns(10))); 7027 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7028 " /* line 1\n" 7029 " bbbbbbbbbbbb */\n" 7030 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7031 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7032 " /* line 1\n" 7033 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7034 getLLVMStyleWithColumns(50))); 7035 7036 FormatStyle NoBinPacking = getLLVMStyle(); 7037 NoBinPacking.BinPackParameters = false; 7038 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 7039 " 2, /* comment 2 */\n" 7040 " 3, /* comment 3 */\n" 7041 " aaaa,\n" 7042 " bbbb);", 7043 format("someFunction (1, /* comment 1 */\n" 7044 " 2, /* comment 2 */ \n" 7045 " 3, /* comment 3 */\n" 7046 "aaaa, bbbb );", 7047 NoBinPacking)); 7048 verifyFormat( 7049 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7051 EXPECT_EQ( 7052 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7053 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7055 format( 7056 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7057 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7059 EXPECT_EQ( 7060 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7061 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7062 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7063 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7064 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7065 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7066 7067 verifyFormat("void f(int * /* unused */) {}"); 7068 7069 EXPECT_EQ("/*\n" 7070 " **\n" 7071 " */", 7072 format("/*\n" 7073 " **\n" 7074 " */")); 7075 EXPECT_EQ("/*\n" 7076 " *q\n" 7077 " */", 7078 format("/*\n" 7079 " *q\n" 7080 " */")); 7081 EXPECT_EQ("/*\n" 7082 " * q\n" 7083 " */", 7084 format("/*\n" 7085 " * q\n" 7086 " */")); 7087 EXPECT_EQ("/*\n" 7088 " **/", 7089 format("/*\n" 7090 " **/")); 7091 EXPECT_EQ("/*\n" 7092 " ***/", 7093 format("/*\n" 7094 " ***/")); 7095 } 7096 7097 TEST_F(FormatTest, BlockCommentsInMacros) { 7098 EXPECT_EQ("#define A \\\n" 7099 " { \\\n" 7100 " /* one line */ \\\n" 7101 " someCall();", 7102 format("#define A { \\\n" 7103 " /* one line */ \\\n" 7104 " someCall();", 7105 getLLVMStyleWithColumns(20))); 7106 EXPECT_EQ("#define A \\\n" 7107 " { \\\n" 7108 " /* previous */ \\\n" 7109 " /* one line */ \\\n" 7110 " someCall();", 7111 format("#define A { \\\n" 7112 " /* previous */ \\\n" 7113 " /* one line */ \\\n" 7114 " someCall();", 7115 getLLVMStyleWithColumns(20))); 7116 } 7117 7118 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7119 EXPECT_EQ("a = {\n" 7120 " 1111 /* */\n" 7121 "};", 7122 format("a = {1111 /* */\n" 7123 "};", 7124 getLLVMStyleWithColumns(15))); 7125 EXPECT_EQ("a = {\n" 7126 " 1111 /* */\n" 7127 "};", 7128 format("a = {1111 /* */\n" 7129 "};", 7130 getLLVMStyleWithColumns(15))); 7131 7132 // FIXME: The formatting is still wrong here. 7133 EXPECT_EQ("a = {\n" 7134 " 1111 /* a\n" 7135 " */\n" 7136 "};", 7137 format("a = {1111 /* a */\n" 7138 "};", 7139 getLLVMStyleWithColumns(15))); 7140 } 7141 7142 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7143 verifyFormat("{\n" 7144 " // a\n" 7145 " // b"); 7146 } 7147 7148 TEST_F(FormatTest, FormatStarDependingOnContext) { 7149 verifyFormat("void f(int *a);"); 7150 verifyFormat("void f() { f(fint * b); }"); 7151 verifyFormat("class A {\n void f(int *a);\n};"); 7152 verifyFormat("class A {\n int *a;\n};"); 7153 verifyFormat("namespace a {\n" 7154 "namespace b {\n" 7155 "class A {\n" 7156 " void f() {}\n" 7157 " int *a;\n" 7158 "};\n" 7159 "}\n" 7160 "}"); 7161 } 7162 7163 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7164 verifyFormat("while"); 7165 verifyFormat("operator"); 7166 } 7167 7168 //===----------------------------------------------------------------------===// 7169 // Objective-C tests. 7170 //===----------------------------------------------------------------------===// 7171 7172 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7173 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7174 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7175 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7176 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7177 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7178 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7179 format("-(NSInteger)Method3:(id)anObject;")); 7180 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7181 format("-(NSInteger)Method4:(id)anObject;")); 7182 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7183 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7184 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7185 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7186 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7187 "forAllCells:(BOOL)flag;", 7188 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7189 "forAllCells:(BOOL)flag;")); 7190 7191 // Very long objectiveC method declaration. 7192 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7193 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7194 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7195 " inRange:(NSRange)range\n" 7196 " outRange:(NSRange)out_range\n" 7197 " outRange1:(NSRange)out_range1\n" 7198 " outRange2:(NSRange)out_range2\n" 7199 " outRange3:(NSRange)out_range3\n" 7200 " outRange4:(NSRange)out_range4\n" 7201 " outRange5:(NSRange)out_range5\n" 7202 " outRange6:(NSRange)out_range6\n" 7203 " outRange7:(NSRange)out_range7\n" 7204 " outRange8:(NSRange)out_range8\n" 7205 " outRange9:(NSRange)out_range9;"); 7206 7207 // When the function name has to be wrapped. 7208 FormatStyle Style = getLLVMStyle(); 7209 Style.IndentWrappedFunctionNames = false; 7210 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7211 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7212 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7213 "}", 7214 Style); 7215 Style.IndentWrappedFunctionNames = true; 7216 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7217 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7218 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7219 "}", 7220 Style); 7221 7222 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7223 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7224 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7225 // protocol lists (but not for template classes): 7226 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7227 7228 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7229 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7230 7231 // If there's no return type (very rare in practice!), LLVM and Google style 7232 // agree. 7233 verifyFormat("- foo;"); 7234 verifyFormat("- foo:(int)f;"); 7235 verifyGoogleFormat("- foo:(int)foo;"); 7236 } 7237 7238 TEST_F(FormatTest, FormatObjCInterface) { 7239 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7240 "@public\n" 7241 " int field1;\n" 7242 "@protected\n" 7243 " int field2;\n" 7244 "@private\n" 7245 " int field3;\n" 7246 "@package\n" 7247 " int field4;\n" 7248 "}\n" 7249 "+ (id)init;\n" 7250 "@end"); 7251 7252 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7253 " @public\n" 7254 " int field1;\n" 7255 " @protected\n" 7256 " int field2;\n" 7257 " @private\n" 7258 " int field3;\n" 7259 " @package\n" 7260 " int field4;\n" 7261 "}\n" 7262 "+ (id)init;\n" 7263 "@end"); 7264 7265 verifyFormat("@interface /* wait for it */ Foo\n" 7266 "+ (id)init;\n" 7267 "// Look, a comment!\n" 7268 "- (int)answerWith:(int)i;\n" 7269 "@end"); 7270 7271 verifyFormat("@interface Foo\n" 7272 "@end\n" 7273 "@interface Bar\n" 7274 "@end"); 7275 7276 verifyFormat("@interface Foo : Bar\n" 7277 "+ (id)init;\n" 7278 "@end"); 7279 7280 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7281 "+ (id)init;\n" 7282 "@end"); 7283 7284 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7285 "+ (id)init;\n" 7286 "@end"); 7287 7288 verifyFormat("@interface Foo (HackStuff)\n" 7289 "+ (id)init;\n" 7290 "@end"); 7291 7292 verifyFormat("@interface Foo ()\n" 7293 "+ (id)init;\n" 7294 "@end"); 7295 7296 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7297 "+ (id)init;\n" 7298 "@end"); 7299 7300 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7301 "+ (id)init;\n" 7302 "@end"); 7303 7304 verifyFormat("@interface Foo {\n" 7305 " int _i;\n" 7306 "}\n" 7307 "+ (id)init;\n" 7308 "@end"); 7309 7310 verifyFormat("@interface Foo : Bar {\n" 7311 " int _i;\n" 7312 "}\n" 7313 "+ (id)init;\n" 7314 "@end"); 7315 7316 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7317 " int _i;\n" 7318 "}\n" 7319 "+ (id)init;\n" 7320 "@end"); 7321 7322 verifyFormat("@interface Foo (HackStuff) {\n" 7323 " int _i;\n" 7324 "}\n" 7325 "+ (id)init;\n" 7326 "@end"); 7327 7328 verifyFormat("@interface Foo () {\n" 7329 " int _i;\n" 7330 "}\n" 7331 "+ (id)init;\n" 7332 "@end"); 7333 7334 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7335 " int _i;\n" 7336 "}\n" 7337 "+ (id)init;\n" 7338 "@end"); 7339 7340 FormatStyle OnePerLine = getGoogleStyle(); 7341 OnePerLine.BinPackParameters = false; 7342 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7347 "}", 7348 OnePerLine); 7349 } 7350 7351 TEST_F(FormatTest, FormatObjCImplementation) { 7352 verifyFormat("@implementation Foo : NSObject {\n" 7353 "@public\n" 7354 " int field1;\n" 7355 "@protected\n" 7356 " int field2;\n" 7357 "@private\n" 7358 " int field3;\n" 7359 "@package\n" 7360 " int field4;\n" 7361 "}\n" 7362 "+ (id)init {\n}\n" 7363 "@end"); 7364 7365 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7366 " @public\n" 7367 " int field1;\n" 7368 " @protected\n" 7369 " int field2;\n" 7370 " @private\n" 7371 " int field3;\n" 7372 " @package\n" 7373 " int field4;\n" 7374 "}\n" 7375 "+ (id)init {\n}\n" 7376 "@end"); 7377 7378 verifyFormat("@implementation Foo\n" 7379 "+ (id)init {\n" 7380 " if (true)\n" 7381 " return nil;\n" 7382 "}\n" 7383 "// Look, a comment!\n" 7384 "- (int)answerWith:(int)i {\n" 7385 " return i;\n" 7386 "}\n" 7387 "+ (int)answerWith:(int)i {\n" 7388 " return i;\n" 7389 "}\n" 7390 "@end"); 7391 7392 verifyFormat("@implementation Foo\n" 7393 "@end\n" 7394 "@implementation Bar\n" 7395 "@end"); 7396 7397 EXPECT_EQ("@implementation Foo : Bar\n" 7398 "+ (id)init {\n}\n" 7399 "- (void)foo {\n}\n" 7400 "@end", 7401 format("@implementation Foo : Bar\n" 7402 "+(id)init{}\n" 7403 "-(void)foo{}\n" 7404 "@end")); 7405 7406 verifyFormat("@implementation Foo {\n" 7407 " int _i;\n" 7408 "}\n" 7409 "+ (id)init {\n}\n" 7410 "@end"); 7411 7412 verifyFormat("@implementation Foo : Bar {\n" 7413 " int _i;\n" 7414 "}\n" 7415 "+ (id)init {\n}\n" 7416 "@end"); 7417 7418 verifyFormat("@implementation Foo (HackStuff)\n" 7419 "+ (id)init {\n}\n" 7420 "@end"); 7421 verifyFormat("@implementation ObjcClass\n" 7422 "- (void)method;\n" 7423 "{}\n" 7424 "@end"); 7425 } 7426 7427 TEST_F(FormatTest, FormatObjCProtocol) { 7428 verifyFormat("@protocol Foo\n" 7429 "@property(weak) id delegate;\n" 7430 "- (NSUInteger)numberOfThings;\n" 7431 "@end"); 7432 7433 verifyFormat("@protocol MyProtocol <NSObject>\n" 7434 "- (NSUInteger)numberOfThings;\n" 7435 "@end"); 7436 7437 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7438 "- (NSUInteger)numberOfThings;\n" 7439 "@end"); 7440 7441 verifyFormat("@protocol Foo;\n" 7442 "@protocol Bar;\n"); 7443 7444 verifyFormat("@protocol Foo\n" 7445 "@end\n" 7446 "@protocol Bar\n" 7447 "@end"); 7448 7449 verifyFormat("@protocol myProtocol\n" 7450 "- (void)mandatoryWithInt:(int)i;\n" 7451 "@optional\n" 7452 "- (void)optional;\n" 7453 "@required\n" 7454 "- (void)required;\n" 7455 "@optional\n" 7456 "@property(assign) int madProp;\n" 7457 "@end\n"); 7458 7459 verifyFormat("@property(nonatomic, assign, readonly)\n" 7460 " int *looooooooooooooooooooooooooooongNumber;\n" 7461 "@property(nonatomic, assign, readonly)\n" 7462 " NSString *looooooooooooooooooooooooooooongName;"); 7463 7464 verifyFormat("@implementation PR18406\n" 7465 "}\n" 7466 "@end"); 7467 } 7468 7469 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7470 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7471 " rect:(NSRect)theRect\n" 7472 " interval:(float)theInterval {\n" 7473 "}"); 7474 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7475 " longKeyword:(NSRect)theRect\n" 7476 " longerKeyword:(float)theInterval\n" 7477 " error:(NSError **)theError {\n" 7478 "}"); 7479 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7480 " longKeyword:(NSRect)theRect\n" 7481 " evenLongerKeyword:(float)theInterval\n" 7482 " error:(NSError **)theError {\n" 7483 "}"); 7484 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7485 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7486 " NS_DESIGNATED_INITIALIZER;", 7487 getLLVMStyleWithColumns(60)); 7488 7489 // Continuation indent width should win over aligning colons if the function 7490 // name is long. 7491 FormatStyle continuationStyle = getGoogleStyle(); 7492 continuationStyle.ColumnLimit = 40; 7493 continuationStyle.IndentWrappedFunctionNames = true; 7494 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7495 " dontAlignNamef:(NSRect)theRect {\n" 7496 "}", 7497 continuationStyle); 7498 7499 // Make sure we don't break aligning for short parameter names. 7500 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7501 " aShortf:(NSRect)theRect {\n" 7502 "}", 7503 continuationStyle); 7504 } 7505 7506 TEST_F(FormatTest, FormatObjCMethodExpr) { 7507 verifyFormat("[foo bar:baz];"); 7508 verifyFormat("return [foo bar:baz];"); 7509 verifyFormat("return (a)[foo bar:baz];"); 7510 verifyFormat("f([foo bar:baz]);"); 7511 verifyFormat("f(2, [foo bar:baz]);"); 7512 verifyFormat("f(2, a ? b : c);"); 7513 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7514 7515 // Unary operators. 7516 verifyFormat("int a = +[foo bar:baz];"); 7517 verifyFormat("int a = -[foo bar:baz];"); 7518 verifyFormat("int a = ![foo bar:baz];"); 7519 verifyFormat("int a = ~[foo bar:baz];"); 7520 verifyFormat("int a = ++[foo bar:baz];"); 7521 verifyFormat("int a = --[foo bar:baz];"); 7522 verifyFormat("int a = sizeof [foo bar:baz];"); 7523 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7524 verifyFormat("int a = &[foo bar:baz];"); 7525 verifyFormat("int a = *[foo bar:baz];"); 7526 // FIXME: Make casts work, without breaking f()[4]. 7527 // verifyFormat("int a = (int)[foo bar:baz];"); 7528 // verifyFormat("return (int)[foo bar:baz];"); 7529 // verifyFormat("(void)[foo bar:baz];"); 7530 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7531 7532 // Binary operators. 7533 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7534 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7535 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7536 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7537 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7538 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7539 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7540 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7541 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7542 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7543 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7544 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7545 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7546 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7547 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7548 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7549 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7550 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7551 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7552 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7553 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7554 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7555 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7556 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7557 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7558 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7559 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7560 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7561 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7562 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7563 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7564 // Whew! 7565 7566 verifyFormat("return in[42];"); 7567 verifyFormat("for (auto v : in[1]) {\n}"); 7568 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7569 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7570 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7571 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7572 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7573 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7574 "}"); 7575 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7576 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7577 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7578 7579 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7580 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7581 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7582 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7583 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7584 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7585 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7586 7587 verifyFormat("arr[[self indexForFoo:a]];"); 7588 verifyFormat("throw [self errorFor:a];"); 7589 verifyFormat("@throw [self errorFor:a];"); 7590 7591 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7592 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7593 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7594 7595 // This tests that the formatter doesn't break after "backing" but before ":", 7596 // which would be at 80 columns. 7597 verifyFormat( 7598 "void f() {\n" 7599 " if ((self = [super initWithContentRect:contentRect\n" 7600 " styleMask:styleMask ?: otherMask\n" 7601 " backing:NSBackingStoreBuffered\n" 7602 " defer:YES]))"); 7603 7604 verifyFormat( 7605 "[foo checkThatBreakingAfterColonWorksOk:\n" 7606 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7607 7608 verifyFormat("[myObj short:arg1 // Force line break\n" 7609 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7610 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7611 " error:arg4];"); 7612 verifyFormat( 7613 "void f() {\n" 7614 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7615 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7616 " pos.width(), pos.height())\n" 7617 " styleMask:NSBorderlessWindowMask\n" 7618 " backing:NSBackingStoreBuffered\n" 7619 " defer:NO]);\n" 7620 "}"); 7621 verifyFormat( 7622 "void f() {\n" 7623 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7624 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7625 " pos.width(), pos.height())\n" 7626 " syeMask:NSBorderlessWindowMask\n" 7627 " bking:NSBackingStoreBuffered\n" 7628 " der:NO]);\n" 7629 "}", 7630 getLLVMStyleWithColumns(70)); 7631 verifyFormat( 7632 "void f() {\n" 7633 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7634 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7635 " pos.width(), pos.height())\n" 7636 " styleMask:NSBorderlessWindowMask\n" 7637 " backing:NSBackingStoreBuffered\n" 7638 " defer:NO]);\n" 7639 "}", 7640 getChromiumStyle(FormatStyle::LK_Cpp)); 7641 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7642 " with:contentsNativeView];"); 7643 7644 verifyFormat( 7645 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7646 " owner:nillllll];"); 7647 7648 verifyFormat( 7649 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7650 " forType:kBookmarkButtonDragType];"); 7651 7652 verifyFormat("[defaultCenter addObserver:self\n" 7653 " selector:@selector(willEnterFullscreen)\n" 7654 " name:kWillEnterFullscreenNotification\n" 7655 " object:nil];"); 7656 verifyFormat("[image_rep drawInRect:drawRect\n" 7657 " fromRect:NSZeroRect\n" 7658 " operation:NSCompositeCopy\n" 7659 " fraction:1.0\n" 7660 " respectFlipped:NO\n" 7661 " hints:nil];"); 7662 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7664 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7666 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7667 " aaaaaaaaaaaaaaaaaaaaaa];"); 7668 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7669 " .aaaaaaaa];", // FIXME: Indentation seems off. 7670 getLLVMStyleWithColumns(60)); 7671 7672 verifyFormat( 7673 "scoped_nsobject<NSTextField> message(\n" 7674 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7675 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7676 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7677 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7678 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7679 " aaaa:bbb];"); 7680 verifyFormat("[self param:function( //\n" 7681 " parameter)]"); 7682 verifyFormat( 7683 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7684 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7685 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7686 7687 // FIXME: This violates the column limit. 7688 verifyFormat( 7689 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7690 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7691 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7692 getLLVMStyleWithColumns(60)); 7693 7694 // Variadic parameters. 7695 verifyFormat( 7696 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7697 verifyFormat( 7698 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7699 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7700 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7701 verifyFormat("[self // break\n" 7702 " a:a\n" 7703 " aaa:aaa];"); 7704 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7705 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7706 } 7707 7708 TEST_F(FormatTest, ObjCAt) { 7709 verifyFormat("@autoreleasepool"); 7710 verifyFormat("@catch"); 7711 verifyFormat("@class"); 7712 verifyFormat("@compatibility_alias"); 7713 verifyFormat("@defs"); 7714 verifyFormat("@dynamic"); 7715 verifyFormat("@encode"); 7716 verifyFormat("@end"); 7717 verifyFormat("@finally"); 7718 verifyFormat("@implementation"); 7719 verifyFormat("@import"); 7720 verifyFormat("@interface"); 7721 verifyFormat("@optional"); 7722 verifyFormat("@package"); 7723 verifyFormat("@private"); 7724 verifyFormat("@property"); 7725 verifyFormat("@protected"); 7726 verifyFormat("@protocol"); 7727 verifyFormat("@public"); 7728 verifyFormat("@required"); 7729 verifyFormat("@selector"); 7730 verifyFormat("@synchronized"); 7731 verifyFormat("@synthesize"); 7732 verifyFormat("@throw"); 7733 verifyFormat("@try"); 7734 7735 EXPECT_EQ("@interface", format("@ interface")); 7736 7737 // The precise formatting of this doesn't matter, nobody writes code like 7738 // this. 7739 verifyFormat("@ /*foo*/ interface"); 7740 } 7741 7742 TEST_F(FormatTest, ObjCSnippets) { 7743 verifyFormat("@autoreleasepool {\n" 7744 " foo();\n" 7745 "}"); 7746 verifyFormat("@class Foo, Bar;"); 7747 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7748 verifyFormat("@dynamic textColor;"); 7749 verifyFormat("char *buf1 = @encode(int *);"); 7750 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7751 verifyFormat("char *buf1 = @encode(int **);"); 7752 verifyFormat("Protocol *proto = @protocol(p1);"); 7753 verifyFormat("SEL s = @selector(foo:);"); 7754 verifyFormat("@synchronized(self) {\n" 7755 " f();\n" 7756 "}"); 7757 7758 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7759 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7760 7761 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7762 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7763 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7764 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7765 getMozillaStyle()); 7766 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7767 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7768 getWebKitStyle()); 7769 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7770 7771 verifyFormat("@import foo.bar;\n" 7772 "@import baz;"); 7773 } 7774 7775 TEST_F(FormatTest, ObjCForIn) { 7776 verifyFormat("- (void)test {\n" 7777 " for (NSString *n in arrayOfStrings) {\n" 7778 " foo(n);\n" 7779 " }\n" 7780 "}"); 7781 verifyFormat("- (void)test {\n" 7782 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7783 " foo(n);\n" 7784 " }\n" 7785 "}"); 7786 } 7787 7788 TEST_F(FormatTest, ObjCLiterals) { 7789 verifyFormat("@\"String\""); 7790 verifyFormat("@1"); 7791 verifyFormat("@+4.8"); 7792 verifyFormat("@-4"); 7793 verifyFormat("@1LL"); 7794 verifyFormat("@.5"); 7795 verifyFormat("@'c'"); 7796 verifyFormat("@true"); 7797 7798 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7799 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7800 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7801 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7802 7803 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7804 } 7805 7806 TEST_F(FormatTest, ObjCDictLiterals) { 7807 verifyFormat("@{"); 7808 verifyFormat("@{}"); 7809 verifyFormat("@{@\"one\" : @1}"); 7810 verifyFormat("return @{@\"one\" : @1;"); 7811 verifyFormat("@{@\"one\" : @1}"); 7812 7813 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7814 verifyFormat("@{\n" 7815 " @\"one\" : @{@2 : @1},\n" 7816 "}"); 7817 7818 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7819 verifyIncompleteFormat("[self setDict:@{}"); 7820 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7821 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7822 verifyFormat( 7823 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7824 verifyFormat( 7825 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7826 7827 verifyFormat("NSDictionary *d = @{\n" 7828 " @\"nam\" : NSUserNam(),\n" 7829 " @\"dte\" : [NSDate date],\n" 7830 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7831 "};"); 7832 verifyFormat( 7833 "@{\n" 7834 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7835 "regularFont,\n" 7836 "};"); 7837 verifyGoogleFormat( 7838 "@{\n" 7839 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7840 "regularFont,\n" 7841 "};"); 7842 verifyFormat( 7843 "@{\n" 7844 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7845 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7846 "};"); 7847 7848 // We should try to be robust in case someone forgets the "@". 7849 verifyFormat("NSDictionary *d = {\n" 7850 " @\"nam\" : NSUserNam(),\n" 7851 " @\"dte\" : [NSDate date],\n" 7852 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7853 "};"); 7854 verifyFormat("NSMutableDictionary *dictionary =\n" 7855 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7856 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7857 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7858 " cccccccccccccccc : ccccccccccccccc\n" 7859 " }];"); 7860 7861 // Ensure that casts before the key are kept on the same line as the key. 7862 verifyFormat( 7863 "NSDictionary *d = @{\n" 7864 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7865 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7866 "};"); 7867 } 7868 7869 TEST_F(FormatTest, ObjCArrayLiterals) { 7870 verifyIncompleteFormat("@["); 7871 verifyFormat("@[]"); 7872 verifyFormat( 7873 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7874 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7875 verifyFormat("NSArray *array = @[ [foo description] ];"); 7876 7877 verifyFormat( 7878 "NSArray *some_variable = @[\n" 7879 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7880 " @\"aaaaaaaaaaaaaaaaa\",\n" 7881 " @\"aaaaaaaaaaaaaaaaa\",\n" 7882 " @\"aaaaaaaaaaaaaaaaa\",\n" 7883 "];"); 7884 verifyFormat( 7885 "NSArray *some_variable = @[\n" 7886 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7887 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7888 "];"); 7889 verifyFormat("NSArray *some_variable = @[\n" 7890 " @\"aaaaaaaaaaaaaaaaa\",\n" 7891 " @\"aaaaaaaaaaaaaaaaa\",\n" 7892 " @\"aaaaaaaaaaaaaaaaa\",\n" 7893 " @\"aaaaaaaaaaaaaaaaa\",\n" 7894 "];"); 7895 verifyFormat("NSArray *array = @[\n" 7896 " @\"a\",\n" 7897 " @\"a\",\n" // Trailing comma -> one per line. 7898 "];"); 7899 7900 // We should try to be robust in case someone forgets the "@". 7901 verifyFormat("NSArray *some_variable = [\n" 7902 " @\"aaaaaaaaaaaaaaaaa\",\n" 7903 " @\"aaaaaaaaaaaaaaaaa\",\n" 7904 " @\"aaaaaaaaaaaaaaaaa\",\n" 7905 " @\"aaaaaaaaaaaaaaaaa\",\n" 7906 "];"); 7907 verifyFormat( 7908 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7909 " index:(NSUInteger)index\n" 7910 " nonDigitAttributes:\n" 7911 " (NSDictionary *)noDigitAttributes;"); 7912 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7913 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7914 "]];"); 7915 } 7916 7917 TEST_F(FormatTest, BreaksStringLiterals) { 7918 EXPECT_EQ("\"some text \"\n" 7919 "\"other\";", 7920 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7921 EXPECT_EQ("\"some text \"\n" 7922 "\"other\";", 7923 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7924 EXPECT_EQ( 7925 "#define A \\\n" 7926 " \"some \" \\\n" 7927 " \"text \" \\\n" 7928 " \"other\";", 7929 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7930 EXPECT_EQ( 7931 "#define A \\\n" 7932 " \"so \" \\\n" 7933 " \"text \" \\\n" 7934 " \"other\";", 7935 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7936 7937 EXPECT_EQ("\"some text\"", 7938 format("\"some text\"", getLLVMStyleWithColumns(1))); 7939 EXPECT_EQ("\"some text\"", 7940 format("\"some text\"", getLLVMStyleWithColumns(11))); 7941 EXPECT_EQ("\"some \"\n" 7942 "\"text\"", 7943 format("\"some text\"", getLLVMStyleWithColumns(10))); 7944 EXPECT_EQ("\"some \"\n" 7945 "\"text\"", 7946 format("\"some text\"", getLLVMStyleWithColumns(7))); 7947 EXPECT_EQ("\"some\"\n" 7948 "\" tex\"\n" 7949 "\"t\"", 7950 format("\"some text\"", getLLVMStyleWithColumns(6))); 7951 EXPECT_EQ("\"some\"\n" 7952 "\" tex\"\n" 7953 "\" and\"", 7954 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7955 EXPECT_EQ("\"some\"\n" 7956 "\"/tex\"\n" 7957 "\"/and\"", 7958 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7959 7960 EXPECT_EQ("variable =\n" 7961 " \"long string \"\n" 7962 " \"literal\";", 7963 format("variable = \"long string literal\";", 7964 getLLVMStyleWithColumns(20))); 7965 7966 EXPECT_EQ("variable = f(\n" 7967 " \"long string \"\n" 7968 " \"literal\",\n" 7969 " short,\n" 7970 " loooooooooooooooooooong);", 7971 format("variable = f(\"long string literal\", short, " 7972 "loooooooooooooooooooong);", 7973 getLLVMStyleWithColumns(20))); 7974 7975 EXPECT_EQ( 7976 "f(g(\"long string \"\n" 7977 " \"literal\"),\n" 7978 " b);", 7979 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7980 EXPECT_EQ("f(g(\"long string \"\n" 7981 " \"literal\",\n" 7982 " a),\n" 7983 " b);", 7984 format("f(g(\"long string literal\", a), b);", 7985 getLLVMStyleWithColumns(20))); 7986 EXPECT_EQ( 7987 "f(\"one two\".split(\n" 7988 " variable));", 7989 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7990 EXPECT_EQ("f(\"one two three four five six \"\n" 7991 " \"seven\".split(\n" 7992 " really_looooong_variable));", 7993 format("f(\"one two three four five six seven\"." 7994 "split(really_looooong_variable));", 7995 getLLVMStyleWithColumns(33))); 7996 7997 EXPECT_EQ("f(\"some \"\n" 7998 " \"text\",\n" 7999 " other);", 8000 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8001 8002 // Only break as a last resort. 8003 verifyFormat( 8004 "aaaaaaaaaaaaaaaaaaaa(\n" 8005 " aaaaaaaaaaaaaaaaaaaa,\n" 8006 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8007 8008 EXPECT_EQ("\"splitmea\"\n" 8009 "\"trandomp\"\n" 8010 "\"oint\"", 8011 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8012 8013 EXPECT_EQ("\"split/\"\n" 8014 "\"pathat/\"\n" 8015 "\"slashes\"", 8016 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8017 8018 EXPECT_EQ("\"split/\"\n" 8019 "\"pathat/\"\n" 8020 "\"slashes\"", 8021 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8022 EXPECT_EQ("\"split at \"\n" 8023 "\"spaces/at/\"\n" 8024 "\"slashes.at.any$\"\n" 8025 "\"non-alphanumeric%\"\n" 8026 "\"1111111111characte\"\n" 8027 "\"rs\"", 8028 format("\"split at " 8029 "spaces/at/" 8030 "slashes.at." 8031 "any$non-" 8032 "alphanumeric%" 8033 "1111111111characte" 8034 "rs\"", 8035 getLLVMStyleWithColumns(20))); 8036 8037 // Verify that splitting the strings understands 8038 // Style::AlwaysBreakBeforeMultilineStrings. 8039 EXPECT_EQ( 8040 "aaaaaaaaaaaa(\n" 8041 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8042 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8043 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8044 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8045 "aaaaaaaaaaaaaaaaaaaaaa\");", 8046 getGoogleStyle())); 8047 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8048 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8049 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8050 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8051 "aaaaaaaaaaaaaaaaaaaaaa\";", 8052 getGoogleStyle())); 8053 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8054 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8055 format("llvm::outs() << " 8056 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8057 "aaaaaaaaaaaaaaaaaaa\";")); 8058 EXPECT_EQ("ffff(\n" 8059 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8060 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8061 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8062 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8063 getGoogleStyle())); 8064 8065 FormatStyle Style = getLLVMStyleWithColumns(12); 8066 Style.BreakStringLiterals = false; 8067 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8068 8069 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8070 AlignLeft.AlignEscapedNewlinesLeft = true; 8071 EXPECT_EQ("#define A \\\n" 8072 " \"some \" \\\n" 8073 " \"text \" \\\n" 8074 " \"other\";", 8075 format("#define A \"some text other\";", AlignLeft)); 8076 } 8077 8078 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8079 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8080 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8081 EXPECT_EQ("int i = a(b());", 8082 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8083 } 8084 8085 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8086 EXPECT_EQ( 8087 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8088 "(\n" 8089 " \"x\t\");", 8090 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8091 "aaaaaaa(" 8092 "\"x\t\");")); 8093 } 8094 8095 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8096 EXPECT_EQ( 8097 "u8\"utf8 string \"\n" 8098 "u8\"literal\";", 8099 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8100 EXPECT_EQ( 8101 "u\"utf16 string \"\n" 8102 "u\"literal\";", 8103 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8104 EXPECT_EQ( 8105 "U\"utf32 string \"\n" 8106 "U\"literal\";", 8107 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8108 EXPECT_EQ("L\"wide string \"\n" 8109 "L\"literal\";", 8110 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8111 EXPECT_EQ("@\"NSString \"\n" 8112 "@\"literal\";", 8113 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8114 8115 // This input makes clang-format try to split the incomplete unicode escape 8116 // sequence, which used to lead to a crasher. 8117 verifyNoCrash( 8118 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8119 getLLVMStyleWithColumns(60)); 8120 } 8121 8122 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8123 FormatStyle Style = getGoogleStyleWithColumns(15); 8124 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8125 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8126 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8127 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8128 EXPECT_EQ("u8R\"x(raw literal)x\";", 8129 format("u8R\"x(raw literal)x\";", Style)); 8130 } 8131 8132 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8133 FormatStyle Style = getLLVMStyleWithColumns(20); 8134 EXPECT_EQ( 8135 "_T(\"aaaaaaaaaaaaaa\")\n" 8136 "_T(\"aaaaaaaaaaaaaa\")\n" 8137 "_T(\"aaaaaaaaaaaa\")", 8138 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8139 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8140 " _T(\"aaaaaa\"),\n" 8141 " z);", 8142 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8143 8144 // FIXME: Handle embedded spaces in one iteration. 8145 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8146 // "_T(\"aaaaaaaaaaaaa\")\n" 8147 // "_T(\"aaaaaaaaaaaaa\")\n" 8148 // "_T(\"a\")", 8149 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8150 // getLLVMStyleWithColumns(20))); 8151 EXPECT_EQ( 8152 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8153 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8154 EXPECT_EQ("f(\n" 8155 "#if !TEST\n" 8156 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8157 "#endif\n" 8158 " );", 8159 format("f(\n" 8160 "#if !TEST\n" 8161 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8162 "#endif\n" 8163 ");")); 8164 EXPECT_EQ("f(\n" 8165 "\n" 8166 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8167 format("f(\n" 8168 "\n" 8169 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8170 } 8171 8172 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8173 EXPECT_EQ( 8174 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8177 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8180 } 8181 8182 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8183 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8184 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8185 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8186 "multiline raw string literal xxxxxxxxxxxxxx\n" 8187 ")x\",\n" 8188 " a),\n" 8189 " b);", 8190 format("fffffffffff(g(R\"x(\n" 8191 "multiline raw string literal xxxxxxxxxxxxxx\n" 8192 ")x\", a), b);", 8193 getGoogleStyleWithColumns(20))); 8194 EXPECT_EQ("fffffffffff(\n" 8195 " g(R\"x(qqq\n" 8196 "multiline raw string literal xxxxxxxxxxxxxx\n" 8197 ")x\",\n" 8198 " a),\n" 8199 " b);", 8200 format("fffffffffff(g(R\"x(qqq\n" 8201 "multiline raw string literal xxxxxxxxxxxxxx\n" 8202 ")x\", a), b);", 8203 getGoogleStyleWithColumns(20))); 8204 8205 EXPECT_EQ("fffffffffff(R\"x(\n" 8206 "multiline raw string literal xxxxxxxxxxxxxx\n" 8207 ")x\");", 8208 format("fffffffffff(R\"x(\n" 8209 "multiline raw string literal xxxxxxxxxxxxxx\n" 8210 ")x\");", 8211 getGoogleStyleWithColumns(20))); 8212 EXPECT_EQ("fffffffffff(R\"x(\n" 8213 "multiline raw string literal xxxxxxxxxxxxxx\n" 8214 ")x\" + bbbbbb);", 8215 format("fffffffffff(R\"x(\n" 8216 "multiline raw string literal xxxxxxxxxxxxxx\n" 8217 ")x\" + bbbbbb);", 8218 getGoogleStyleWithColumns(20))); 8219 EXPECT_EQ("fffffffffff(\n" 8220 " R\"x(\n" 8221 "multiline raw string literal xxxxxxxxxxxxxx\n" 8222 ")x\" +\n" 8223 " bbbbbb);", 8224 format("fffffffffff(\n" 8225 " R\"x(\n" 8226 "multiline raw string literal xxxxxxxxxxxxxx\n" 8227 ")x\" + bbbbbb);", 8228 getGoogleStyleWithColumns(20))); 8229 } 8230 8231 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8232 verifyFormat("string a = \"unterminated;"); 8233 EXPECT_EQ("function(\"unterminated,\n" 8234 " OtherParameter);", 8235 format("function( \"unterminated,\n" 8236 " OtherParameter);")); 8237 } 8238 8239 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8240 FormatStyle Style = getLLVMStyle(); 8241 Style.Standard = FormatStyle::LS_Cpp03; 8242 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8243 format("#define x(_a) printf(\"foo\"_a);", Style)); 8244 } 8245 8246 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8247 8248 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8249 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8250 " \"ddeeefff\");", 8251 format("someFunction(\"aaabbbcccdddeeefff\");", 8252 getLLVMStyleWithColumns(25))); 8253 EXPECT_EQ("someFunction1234567890(\n" 8254 " \"aaabbbcccdddeeefff\");", 8255 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8256 getLLVMStyleWithColumns(26))); 8257 EXPECT_EQ("someFunction1234567890(\n" 8258 " \"aaabbbcccdddeeeff\"\n" 8259 " \"f\");", 8260 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8261 getLLVMStyleWithColumns(25))); 8262 EXPECT_EQ("someFunction1234567890(\n" 8263 " \"aaabbbcccdddeeeff\"\n" 8264 " \"f\");", 8265 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8266 getLLVMStyleWithColumns(24))); 8267 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8268 " \"ddde \"\n" 8269 " \"efff\");", 8270 format("someFunction(\"aaabbbcc ddde efff\");", 8271 getLLVMStyleWithColumns(25))); 8272 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8273 " \"ddeeefff\");", 8274 format("someFunction(\"aaabbbccc ddeeefff\");", 8275 getLLVMStyleWithColumns(25))); 8276 EXPECT_EQ("someFunction1234567890(\n" 8277 " \"aaabb \"\n" 8278 " \"cccdddeeefff\");", 8279 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8280 getLLVMStyleWithColumns(25))); 8281 EXPECT_EQ("#define A \\\n" 8282 " string s = \\\n" 8283 " \"123456789\" \\\n" 8284 " \"0\"; \\\n" 8285 " int i;", 8286 format("#define A string s = \"1234567890\"; int i;", 8287 getLLVMStyleWithColumns(20))); 8288 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8289 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8290 " \"dddeeeff\"\n" 8291 " \"f\");", 8292 format("someFunction(\"aaabbbcc dddeeefff\");", 8293 getLLVMStyleWithColumns(25))); 8294 } 8295 8296 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8297 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8298 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8299 EXPECT_EQ("\"test\"\n" 8300 "\"\\n\"", 8301 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8302 EXPECT_EQ("\"tes\\\\\"\n" 8303 "\"n\"", 8304 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8305 EXPECT_EQ("\"\\\\\\\\\"\n" 8306 "\"\\n\"", 8307 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8308 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8309 EXPECT_EQ("\"\\uff01\"\n" 8310 "\"test\"", 8311 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8312 EXPECT_EQ("\"\\Uff01ff02\"", 8313 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8314 EXPECT_EQ("\"\\x000000000001\"\n" 8315 "\"next\"", 8316 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8317 EXPECT_EQ("\"\\x000000000001next\"", 8318 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8319 EXPECT_EQ("\"\\x000000000001\"", 8320 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8321 EXPECT_EQ("\"test\"\n" 8322 "\"\\000000\"\n" 8323 "\"000001\"", 8324 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8325 EXPECT_EQ("\"test\\000\"\n" 8326 "\"00000000\"\n" 8327 "\"1\"", 8328 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8329 } 8330 8331 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8332 verifyFormat("void f() {\n" 8333 " return g() {}\n" 8334 " void h() {}"); 8335 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8336 "g();\n" 8337 "}"); 8338 } 8339 8340 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8341 verifyFormat( 8342 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8343 } 8344 8345 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8346 verifyFormat("class X {\n" 8347 " void f() {\n" 8348 " }\n" 8349 "};", 8350 getLLVMStyleWithColumns(12)); 8351 } 8352 8353 TEST_F(FormatTest, ConfigurableIndentWidth) { 8354 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8355 EightIndent.IndentWidth = 8; 8356 EightIndent.ContinuationIndentWidth = 8; 8357 verifyFormat("void f() {\n" 8358 " someFunction();\n" 8359 " if (true) {\n" 8360 " f();\n" 8361 " }\n" 8362 "}", 8363 EightIndent); 8364 verifyFormat("class X {\n" 8365 " void f() {\n" 8366 " }\n" 8367 "};", 8368 EightIndent); 8369 verifyFormat("int x[] = {\n" 8370 " call(),\n" 8371 " call()};", 8372 EightIndent); 8373 } 8374 8375 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8376 verifyFormat("double\n" 8377 "f();", 8378 getLLVMStyleWithColumns(8)); 8379 } 8380 8381 TEST_F(FormatTest, ConfigurableUseOfTab) { 8382 FormatStyle Tab = getLLVMStyleWithColumns(42); 8383 Tab.IndentWidth = 8; 8384 Tab.UseTab = FormatStyle::UT_Always; 8385 Tab.AlignEscapedNewlinesLeft = true; 8386 8387 EXPECT_EQ("if (aaaaaaaa && // q\n" 8388 " bb)\t\t// w\n" 8389 "\t;", 8390 format("if (aaaaaaaa &&// q\n" 8391 "bb)// w\n" 8392 ";", 8393 Tab)); 8394 EXPECT_EQ("if (aaa && bbb) // w\n" 8395 "\t;", 8396 format("if(aaa&&bbb)// w\n" 8397 ";", 8398 Tab)); 8399 8400 verifyFormat("class X {\n" 8401 "\tvoid f() {\n" 8402 "\t\tsomeFunction(parameter1,\n" 8403 "\t\t\t parameter2);\n" 8404 "\t}\n" 8405 "};", 8406 Tab); 8407 verifyFormat("#define A \\\n" 8408 "\tvoid f() { \\\n" 8409 "\t\tsomeFunction( \\\n" 8410 "\t\t parameter1, \\\n" 8411 "\t\t parameter2); \\\n" 8412 "\t}", 8413 Tab); 8414 8415 Tab.TabWidth = 4; 8416 Tab.IndentWidth = 8; 8417 verifyFormat("class TabWidth4Indent8 {\n" 8418 "\t\tvoid f() {\n" 8419 "\t\t\t\tsomeFunction(parameter1,\n" 8420 "\t\t\t\t\t\t\t parameter2);\n" 8421 "\t\t}\n" 8422 "};", 8423 Tab); 8424 8425 Tab.TabWidth = 4; 8426 Tab.IndentWidth = 4; 8427 verifyFormat("class TabWidth4Indent4 {\n" 8428 "\tvoid f() {\n" 8429 "\t\tsomeFunction(parameter1,\n" 8430 "\t\t\t\t\t parameter2);\n" 8431 "\t}\n" 8432 "};", 8433 Tab); 8434 8435 Tab.TabWidth = 8; 8436 Tab.IndentWidth = 4; 8437 verifyFormat("class TabWidth8Indent4 {\n" 8438 " void f() {\n" 8439 "\tsomeFunction(parameter1,\n" 8440 "\t\t parameter2);\n" 8441 " }\n" 8442 "};", 8443 Tab); 8444 8445 Tab.TabWidth = 8; 8446 Tab.IndentWidth = 8; 8447 EXPECT_EQ("/*\n" 8448 "\t a\t\tcomment\n" 8449 "\t in multiple lines\n" 8450 " */", 8451 format(" /*\t \t \n" 8452 " \t \t a\t\tcomment\t \t\n" 8453 " \t \t in multiple lines\t\n" 8454 " \t */", 8455 Tab)); 8456 8457 Tab.UseTab = FormatStyle::UT_ForIndentation; 8458 verifyFormat("{\n" 8459 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8460 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8461 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8462 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8463 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8464 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8465 "};", 8466 Tab); 8467 verifyFormat("enum AA {\n" 8468 "\ta1, // Force multiple lines\n" 8469 "\ta2,\n" 8470 "\ta3\n" 8471 "};", 8472 Tab); 8473 EXPECT_EQ("if (aaaaaaaa && // q\n" 8474 " bb) // w\n" 8475 "\t;", 8476 format("if (aaaaaaaa &&// q\n" 8477 "bb)// w\n" 8478 ";", 8479 Tab)); 8480 verifyFormat("class X {\n" 8481 "\tvoid f() {\n" 8482 "\t\tsomeFunction(parameter1,\n" 8483 "\t\t parameter2);\n" 8484 "\t}\n" 8485 "};", 8486 Tab); 8487 verifyFormat("{\n" 8488 "\tQ(\n" 8489 "\t {\n" 8490 "\t\t int a;\n" 8491 "\t\t someFunction(aaaaaaaa,\n" 8492 "\t\t bbbbbbb);\n" 8493 "\t },\n" 8494 "\t p);\n" 8495 "}", 8496 Tab); 8497 EXPECT_EQ("{\n" 8498 "\t/* aaaa\n" 8499 "\t bbbb */\n" 8500 "}", 8501 format("{\n" 8502 "/* aaaa\n" 8503 " bbbb */\n" 8504 "}", 8505 Tab)); 8506 EXPECT_EQ("{\n" 8507 "\t/*\n" 8508 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8509 "\t bbbbbbbbbbbbb\n" 8510 "\t*/\n" 8511 "}", 8512 format("{\n" 8513 "/*\n" 8514 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8515 "*/\n" 8516 "}", 8517 Tab)); 8518 EXPECT_EQ("{\n" 8519 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8520 "\t// bbbbbbbbbbbbb\n" 8521 "}", 8522 format("{\n" 8523 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8524 "}", 8525 Tab)); 8526 EXPECT_EQ("{\n" 8527 "\t/*\n" 8528 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8529 "\t bbbbbbbbbbbbb\n" 8530 "\t*/\n" 8531 "}", 8532 format("{\n" 8533 "\t/*\n" 8534 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8535 "\t*/\n" 8536 "}", 8537 Tab)); 8538 EXPECT_EQ("{\n" 8539 "\t/*\n" 8540 "\n" 8541 "\t*/\n" 8542 "}", 8543 format("{\n" 8544 "\t/*\n" 8545 "\n" 8546 "\t*/\n" 8547 "}", 8548 Tab)); 8549 EXPECT_EQ("{\n" 8550 "\t/*\n" 8551 " asdf\n" 8552 "\t*/\n" 8553 "}", 8554 format("{\n" 8555 "\t/*\n" 8556 " asdf\n" 8557 "\t*/\n" 8558 "}", 8559 Tab)); 8560 8561 Tab.UseTab = FormatStyle::UT_Never; 8562 EXPECT_EQ("/*\n" 8563 " a\t\tcomment\n" 8564 " in multiple lines\n" 8565 " */", 8566 format(" /*\t \t \n" 8567 " \t \t a\t\tcomment\t \t\n" 8568 " \t \t in multiple lines\t\n" 8569 " \t */", 8570 Tab)); 8571 EXPECT_EQ("/* some\n" 8572 " comment */", 8573 format(" \t \t /* some\n" 8574 " \t \t comment */", 8575 Tab)); 8576 EXPECT_EQ("int a; /* some\n" 8577 " comment */", 8578 format(" \t \t int a; /* some\n" 8579 " \t \t comment */", 8580 Tab)); 8581 8582 EXPECT_EQ("int a; /* some\n" 8583 "comment */", 8584 format(" \t \t int\ta; /* some\n" 8585 " \t \t comment */", 8586 Tab)); 8587 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8588 " comment */", 8589 format(" \t \t f(\"\t\t\"); /* some\n" 8590 " \t \t comment */", 8591 Tab)); 8592 EXPECT_EQ("{\n" 8593 " /*\n" 8594 " * Comment\n" 8595 " */\n" 8596 " int i;\n" 8597 "}", 8598 format("{\n" 8599 "\t/*\n" 8600 "\t * Comment\n" 8601 "\t */\n" 8602 "\t int i;\n" 8603 "}")); 8604 8605 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8606 Tab.TabWidth = 8; 8607 Tab.IndentWidth = 8; 8608 EXPECT_EQ("if (aaaaaaaa && // q\n" 8609 " bb) // w\n" 8610 "\t;", 8611 format("if (aaaaaaaa &&// q\n" 8612 "bb)// w\n" 8613 ";", 8614 Tab)); 8615 EXPECT_EQ("if (aaa && bbb) // w\n" 8616 "\t;", 8617 format("if(aaa&&bbb)// w\n" 8618 ";", 8619 Tab)); 8620 verifyFormat("class X {\n" 8621 "\tvoid f() {\n" 8622 "\t\tsomeFunction(parameter1,\n" 8623 "\t\t\t parameter2);\n" 8624 "\t}\n" 8625 "};", 8626 Tab); 8627 verifyFormat("#define A \\\n" 8628 "\tvoid f() { \\\n" 8629 "\t\tsomeFunction( \\\n" 8630 "\t\t parameter1, \\\n" 8631 "\t\t parameter2); \\\n" 8632 "\t}", 8633 Tab); 8634 Tab.TabWidth = 4; 8635 Tab.IndentWidth = 8; 8636 verifyFormat("class TabWidth4Indent8 {\n" 8637 "\t\tvoid f() {\n" 8638 "\t\t\t\tsomeFunction(parameter1,\n" 8639 "\t\t\t\t\t\t\t parameter2);\n" 8640 "\t\t}\n" 8641 "};", 8642 Tab); 8643 Tab.TabWidth = 4; 8644 Tab.IndentWidth = 4; 8645 verifyFormat("class TabWidth4Indent4 {\n" 8646 "\tvoid f() {\n" 8647 "\t\tsomeFunction(parameter1,\n" 8648 "\t\t\t\t\t parameter2);\n" 8649 "\t}\n" 8650 "};", 8651 Tab); 8652 Tab.TabWidth = 8; 8653 Tab.IndentWidth = 4; 8654 verifyFormat("class TabWidth8Indent4 {\n" 8655 " void f() {\n" 8656 "\tsomeFunction(parameter1,\n" 8657 "\t\t parameter2);\n" 8658 " }\n" 8659 "};", 8660 Tab); 8661 Tab.TabWidth = 8; 8662 Tab.IndentWidth = 8; 8663 EXPECT_EQ("/*\n" 8664 "\t a\t\tcomment\n" 8665 "\t in multiple lines\n" 8666 " */", 8667 format(" /*\t \t \n" 8668 " \t \t a\t\tcomment\t \t\n" 8669 " \t \t in multiple lines\t\n" 8670 " \t */", 8671 Tab)); 8672 verifyFormat("{\n" 8673 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8674 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8675 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8676 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8677 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8678 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8679 "};", 8680 Tab); 8681 verifyFormat("enum AA {\n" 8682 "\ta1, // Force multiple lines\n" 8683 "\ta2,\n" 8684 "\ta3\n" 8685 "};", 8686 Tab); 8687 EXPECT_EQ("if (aaaaaaaa && // q\n" 8688 " bb) // w\n" 8689 "\t;", 8690 format("if (aaaaaaaa &&// q\n" 8691 "bb)// w\n" 8692 ";", 8693 Tab)); 8694 verifyFormat("class X {\n" 8695 "\tvoid f() {\n" 8696 "\t\tsomeFunction(parameter1,\n" 8697 "\t\t\t parameter2);\n" 8698 "\t}\n" 8699 "};", 8700 Tab); 8701 verifyFormat("{\n" 8702 "\tQ(\n" 8703 "\t {\n" 8704 "\t\t int a;\n" 8705 "\t\t someFunction(aaaaaaaa,\n" 8706 "\t\t\t\t bbbbbbb);\n" 8707 "\t },\n" 8708 "\t p);\n" 8709 "}", 8710 Tab); 8711 EXPECT_EQ("{\n" 8712 "\t/* aaaa\n" 8713 "\t bbbb */\n" 8714 "}", 8715 format("{\n" 8716 "/* aaaa\n" 8717 " bbbb */\n" 8718 "}", 8719 Tab)); 8720 EXPECT_EQ("{\n" 8721 "\t/*\n" 8722 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8723 "\t bbbbbbbbbbbbb\n" 8724 "\t*/\n" 8725 "}", 8726 format("{\n" 8727 "/*\n" 8728 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8729 "*/\n" 8730 "}", 8731 Tab)); 8732 EXPECT_EQ("{\n" 8733 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8734 "\t// bbbbbbbbbbbbb\n" 8735 "}", 8736 format("{\n" 8737 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8738 "}", 8739 Tab)); 8740 EXPECT_EQ("{\n" 8741 "\t/*\n" 8742 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8743 "\t bbbbbbbbbbbbb\n" 8744 "\t*/\n" 8745 "}", 8746 format("{\n" 8747 "\t/*\n" 8748 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8749 "\t*/\n" 8750 "}", 8751 Tab)); 8752 EXPECT_EQ("{\n" 8753 "\t/*\n" 8754 "\n" 8755 "\t*/\n" 8756 "}", 8757 format("{\n" 8758 "\t/*\n" 8759 "\n" 8760 "\t*/\n" 8761 "}", 8762 Tab)); 8763 EXPECT_EQ("{\n" 8764 "\t/*\n" 8765 " asdf\n" 8766 "\t*/\n" 8767 "}", 8768 format("{\n" 8769 "\t/*\n" 8770 " asdf\n" 8771 "\t*/\n" 8772 "}", 8773 Tab)); 8774 EXPECT_EQ("/*\n" 8775 "\t a\t\tcomment\n" 8776 "\t in multiple lines\n" 8777 " */", 8778 format(" /*\t \t \n" 8779 " \t \t a\t\tcomment\t \t\n" 8780 " \t \t in multiple lines\t\n" 8781 " \t */", 8782 Tab)); 8783 EXPECT_EQ("/* some\n" 8784 " comment */", 8785 format(" \t \t /* some\n" 8786 " \t \t comment */", 8787 Tab)); 8788 EXPECT_EQ("int a; /* some\n" 8789 " comment */", 8790 format(" \t \t int a; /* some\n" 8791 " \t \t comment */", 8792 Tab)); 8793 EXPECT_EQ("int a; /* some\n" 8794 "comment */", 8795 format(" \t \t int\ta; /* some\n" 8796 " \t \t comment */", 8797 Tab)); 8798 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8799 " comment */", 8800 format(" \t \t f(\"\t\t\"); /* some\n" 8801 " \t \t comment */", 8802 Tab)); 8803 EXPECT_EQ("{\n" 8804 " /*\n" 8805 " * Comment\n" 8806 " */\n" 8807 " int i;\n" 8808 "}", 8809 format("{\n" 8810 "\t/*\n" 8811 "\t * Comment\n" 8812 "\t */\n" 8813 "\t int i;\n" 8814 "}")); 8815 Tab.AlignConsecutiveAssignments = true; 8816 Tab.AlignConsecutiveDeclarations = true; 8817 Tab.TabWidth = 4; 8818 Tab.IndentWidth = 4; 8819 verifyFormat("class Assign {\n" 8820 "\tvoid f() {\n" 8821 "\t\tint x = 123;\n" 8822 "\t\tint random = 4;\n" 8823 "\t\tstd::string alphabet =\n" 8824 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8825 "\t}\n" 8826 "};", 8827 Tab); 8828 } 8829 8830 TEST_F(FormatTest, CalculatesOriginalColumn) { 8831 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8832 "q\"; /* some\n" 8833 " comment */", 8834 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8835 "q\"; /* some\n" 8836 " comment */", 8837 getLLVMStyle())); 8838 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8839 "/* some\n" 8840 " comment */", 8841 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8842 " /* some\n" 8843 " comment */", 8844 getLLVMStyle())); 8845 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8846 "qqq\n" 8847 "/* some\n" 8848 " comment */", 8849 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8850 "qqq\n" 8851 " /* some\n" 8852 " comment */", 8853 getLLVMStyle())); 8854 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8855 "wwww; /* some\n" 8856 " comment */", 8857 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8858 "wwww; /* some\n" 8859 " comment */", 8860 getLLVMStyle())); 8861 } 8862 8863 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8864 FormatStyle NoSpace = getLLVMStyle(); 8865 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8866 8867 verifyFormat("while(true)\n" 8868 " continue;", 8869 NoSpace); 8870 verifyFormat("for(;;)\n" 8871 " continue;", 8872 NoSpace); 8873 verifyFormat("if(true)\n" 8874 " f();\n" 8875 "else if(true)\n" 8876 " f();", 8877 NoSpace); 8878 verifyFormat("do {\n" 8879 " do_something();\n" 8880 "} while(something());", 8881 NoSpace); 8882 verifyFormat("switch(x) {\n" 8883 "default:\n" 8884 " break;\n" 8885 "}", 8886 NoSpace); 8887 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8888 verifyFormat("size_t x = sizeof(x);", NoSpace); 8889 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8890 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8891 verifyFormat("alignas(128) char a[128];", NoSpace); 8892 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8893 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8894 verifyFormat("int f() throw(Deprecated);", NoSpace); 8895 verifyFormat("typedef void (*cb)(int);", NoSpace); 8896 verifyFormat("T A::operator()();", NoSpace); 8897 verifyFormat("X A::operator++(T);", NoSpace); 8898 8899 FormatStyle Space = getLLVMStyle(); 8900 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8901 8902 verifyFormat("int f ();", Space); 8903 verifyFormat("void f (int a, T b) {\n" 8904 " while (true)\n" 8905 " continue;\n" 8906 "}", 8907 Space); 8908 verifyFormat("if (true)\n" 8909 " f ();\n" 8910 "else if (true)\n" 8911 " f ();", 8912 Space); 8913 verifyFormat("do {\n" 8914 " do_something ();\n" 8915 "} while (something ());", 8916 Space); 8917 verifyFormat("switch (x) {\n" 8918 "default:\n" 8919 " break;\n" 8920 "}", 8921 Space); 8922 verifyFormat("A::A () : a (1) {}", Space); 8923 verifyFormat("void f () __attribute__ ((asdf));", Space); 8924 verifyFormat("*(&a + 1);\n" 8925 "&((&a)[1]);\n" 8926 "a[(b + c) * d];\n" 8927 "(((a + 1) * 2) + 3) * 4;", 8928 Space); 8929 verifyFormat("#define A(x) x", Space); 8930 verifyFormat("#define A (x) x", Space); 8931 verifyFormat("#if defined(x)\n" 8932 "#endif", 8933 Space); 8934 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8935 verifyFormat("size_t x = sizeof (x);", Space); 8936 verifyFormat("auto f (int x) -> decltype (x);", Space); 8937 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8938 verifyFormat("alignas (128) char a[128];", Space); 8939 verifyFormat("size_t x = alignof (MyType);", Space); 8940 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8941 verifyFormat("int f () throw (Deprecated);", Space); 8942 verifyFormat("typedef void (*cb) (int);", Space); 8943 verifyFormat("T A::operator() ();", Space); 8944 verifyFormat("X A::operator++ (T);", Space); 8945 } 8946 8947 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8948 FormatStyle Spaces = getLLVMStyle(); 8949 8950 Spaces.SpacesInParentheses = true; 8951 verifyFormat("call( x, y, z );", Spaces); 8952 verifyFormat("call();", Spaces); 8953 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8954 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8955 Spaces); 8956 verifyFormat("while ( (bool)1 )\n" 8957 " continue;", 8958 Spaces); 8959 verifyFormat("for ( ;; )\n" 8960 " continue;", 8961 Spaces); 8962 verifyFormat("if ( true )\n" 8963 " f();\n" 8964 "else if ( true )\n" 8965 " f();", 8966 Spaces); 8967 verifyFormat("do {\n" 8968 " do_something( (int)i );\n" 8969 "} while ( something() );", 8970 Spaces); 8971 verifyFormat("switch ( x ) {\n" 8972 "default:\n" 8973 " break;\n" 8974 "}", 8975 Spaces); 8976 8977 Spaces.SpacesInParentheses = false; 8978 Spaces.SpacesInCStyleCastParentheses = true; 8979 verifyFormat("Type *A = ( Type * )P;", Spaces); 8980 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8981 verifyFormat("x = ( int32 )y;", Spaces); 8982 verifyFormat("int a = ( int )(2.0f);", Spaces); 8983 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8984 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8985 verifyFormat("#define x (( int )-1)", Spaces); 8986 8987 // Run the first set of tests again with: 8988 Spaces.SpacesInParentheses = false; 8989 Spaces.SpaceInEmptyParentheses = true; 8990 Spaces.SpacesInCStyleCastParentheses = true; 8991 verifyFormat("call(x, y, z);", Spaces); 8992 verifyFormat("call( );", Spaces); 8993 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8994 verifyFormat("while (( bool )1)\n" 8995 " continue;", 8996 Spaces); 8997 verifyFormat("for (;;)\n" 8998 " continue;", 8999 Spaces); 9000 verifyFormat("if (true)\n" 9001 " f( );\n" 9002 "else if (true)\n" 9003 " f( );", 9004 Spaces); 9005 verifyFormat("do {\n" 9006 " do_something(( int )i);\n" 9007 "} while (something( ));", 9008 Spaces); 9009 verifyFormat("switch (x) {\n" 9010 "default:\n" 9011 " break;\n" 9012 "}", 9013 Spaces); 9014 9015 // Run the first set of tests again with: 9016 Spaces.SpaceAfterCStyleCast = true; 9017 verifyFormat("call(x, y, z);", Spaces); 9018 verifyFormat("call( );", Spaces); 9019 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9020 verifyFormat("while (( bool ) 1)\n" 9021 " continue;", 9022 Spaces); 9023 verifyFormat("for (;;)\n" 9024 " continue;", 9025 Spaces); 9026 verifyFormat("if (true)\n" 9027 " f( );\n" 9028 "else if (true)\n" 9029 " f( );", 9030 Spaces); 9031 verifyFormat("do {\n" 9032 " do_something(( int ) i);\n" 9033 "} while (something( ));", 9034 Spaces); 9035 verifyFormat("switch (x) {\n" 9036 "default:\n" 9037 " break;\n" 9038 "}", 9039 Spaces); 9040 9041 // Run subset of tests again with: 9042 Spaces.SpacesInCStyleCastParentheses = false; 9043 Spaces.SpaceAfterCStyleCast = true; 9044 verifyFormat("while ((bool) 1)\n" 9045 " continue;", 9046 Spaces); 9047 verifyFormat("do {\n" 9048 " do_something((int) i);\n" 9049 "} while (something( ));", 9050 Spaces); 9051 } 9052 9053 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9054 verifyFormat("int a[5];"); 9055 verifyFormat("a[3] += 42;"); 9056 9057 FormatStyle Spaces = getLLVMStyle(); 9058 Spaces.SpacesInSquareBrackets = true; 9059 // Lambdas unchanged. 9060 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9061 verifyFormat("return [i, args...] {};", Spaces); 9062 9063 // Not lambdas. 9064 verifyFormat("int a[ 5 ];", Spaces); 9065 verifyFormat("a[ 3 ] += 42;", Spaces); 9066 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9067 verifyFormat("double &operator[](int i) { return 0; }\n" 9068 "int i;", 9069 Spaces); 9070 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9071 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9072 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9073 } 9074 9075 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9076 verifyFormat("int a = 5;"); 9077 verifyFormat("a += 42;"); 9078 verifyFormat("a or_eq 8;"); 9079 9080 FormatStyle Spaces = getLLVMStyle(); 9081 Spaces.SpaceBeforeAssignmentOperators = false; 9082 verifyFormat("int a= 5;", Spaces); 9083 verifyFormat("a+= 42;", Spaces); 9084 verifyFormat("a or_eq 8;", Spaces); 9085 } 9086 9087 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9088 FormatStyle Alignment = getLLVMStyle(); 9089 Alignment.AlignConsecutiveAssignments = false; 9090 verifyFormat("int a = 5;\n" 9091 "int oneTwoThree = 123;", 9092 Alignment); 9093 verifyFormat("int a = 5;\n" 9094 "int oneTwoThree = 123;", 9095 Alignment); 9096 9097 Alignment.AlignConsecutiveAssignments = true; 9098 verifyFormat("int a = 5;\n" 9099 "int oneTwoThree = 123;", 9100 Alignment); 9101 verifyFormat("int a = method();\n" 9102 "int oneTwoThree = 133;", 9103 Alignment); 9104 verifyFormat("a &= 5;\n" 9105 "bcd *= 5;\n" 9106 "ghtyf += 5;\n" 9107 "dvfvdb -= 5;\n" 9108 "a /= 5;\n" 9109 "vdsvsv %= 5;\n" 9110 "sfdbddfbdfbb ^= 5;\n" 9111 "dvsdsv |= 5;\n" 9112 "int dsvvdvsdvvv = 123;", 9113 Alignment); 9114 verifyFormat("int i = 1, j = 10;\n" 9115 "something = 2000;", 9116 Alignment); 9117 verifyFormat("something = 2000;\n" 9118 "int i = 1, j = 10;\n", 9119 Alignment); 9120 verifyFormat("something = 2000;\n" 9121 "another = 911;\n" 9122 "int i = 1, j = 10;\n" 9123 "oneMore = 1;\n" 9124 "i = 2;", 9125 Alignment); 9126 verifyFormat("int a = 5;\n" 9127 "int one = 1;\n" 9128 "method();\n" 9129 "int oneTwoThree = 123;\n" 9130 "int oneTwo = 12;", 9131 Alignment); 9132 verifyFormat("int oneTwoThree = 123;\n" 9133 "int oneTwo = 12;\n" 9134 "method();\n", 9135 Alignment); 9136 verifyFormat("int oneTwoThree = 123; // comment\n" 9137 "int oneTwo = 12; // comment", 9138 Alignment); 9139 EXPECT_EQ("int a = 5;\n" 9140 "\n" 9141 "int oneTwoThree = 123;", 9142 format("int a = 5;\n" 9143 "\n" 9144 "int oneTwoThree= 123;", 9145 Alignment)); 9146 EXPECT_EQ("int a = 5;\n" 9147 "int one = 1;\n" 9148 "\n" 9149 "int oneTwoThree = 123;", 9150 format("int a = 5;\n" 9151 "int one = 1;\n" 9152 "\n" 9153 "int oneTwoThree = 123;", 9154 Alignment)); 9155 EXPECT_EQ("int a = 5;\n" 9156 "int one = 1;\n" 9157 "\n" 9158 "int oneTwoThree = 123;\n" 9159 "int oneTwo = 12;", 9160 format("int a = 5;\n" 9161 "int one = 1;\n" 9162 "\n" 9163 "int oneTwoThree = 123;\n" 9164 "int oneTwo = 12;", 9165 Alignment)); 9166 Alignment.AlignEscapedNewlinesLeft = true; 9167 verifyFormat("#define A \\\n" 9168 " int aaaa = 12; \\\n" 9169 " int b = 23; \\\n" 9170 " int ccc = 234; \\\n" 9171 " int dddddddddd = 2345;", 9172 Alignment); 9173 Alignment.AlignEscapedNewlinesLeft = false; 9174 verifyFormat("#define A " 9175 " \\\n" 9176 " int aaaa = 12; " 9177 " \\\n" 9178 " int b = 23; " 9179 " \\\n" 9180 " int ccc = 234; " 9181 " \\\n" 9182 " int dddddddddd = 2345;", 9183 Alignment); 9184 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9185 "k = 4, int l = 5,\n" 9186 " int m = 6) {\n" 9187 " int j = 10;\n" 9188 " otherThing = 1;\n" 9189 "}", 9190 Alignment); 9191 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9192 " int i = 1;\n" 9193 " int j = 2;\n" 9194 " int big = 10000;\n" 9195 "}", 9196 Alignment); 9197 verifyFormat("class C {\n" 9198 "public:\n" 9199 " int i = 1;\n" 9200 " virtual void f() = 0;\n" 9201 "};", 9202 Alignment); 9203 verifyFormat("int i = 1;\n" 9204 "if (SomeType t = getSomething()) {\n" 9205 "}\n" 9206 "int j = 2;\n" 9207 "int big = 10000;", 9208 Alignment); 9209 verifyFormat("int j = 7;\n" 9210 "for (int k = 0; k < N; ++k) {\n" 9211 "}\n" 9212 "int j = 2;\n" 9213 "int big = 10000;\n" 9214 "}", 9215 Alignment); 9216 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9217 verifyFormat("int i = 1;\n" 9218 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9219 " = someLooooooooooooooooongFunction();\n" 9220 "int j = 2;", 9221 Alignment); 9222 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9223 verifyFormat("int i = 1;\n" 9224 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9225 " someLooooooooooooooooongFunction();\n" 9226 "int j = 2;", 9227 Alignment); 9228 9229 verifyFormat("auto lambda = []() {\n" 9230 " auto i = 0;\n" 9231 " return 0;\n" 9232 "};\n" 9233 "int i = 0;\n" 9234 "auto v = type{\n" 9235 " i = 1, //\n" 9236 " (i = 2), //\n" 9237 " i = 3 //\n" 9238 "};", 9239 Alignment); 9240 9241 // FIXME: Should align all three assignments 9242 verifyFormat( 9243 "int i = 1;\n" 9244 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9245 " loooooooooooooooooooooongParameterB);\n" 9246 "int j = 2;", 9247 Alignment); 9248 9249 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9250 " typename B = very_long_type_name_1,\n" 9251 " typename T_2 = very_long_type_name_2>\n" 9252 "auto foo() {}\n", 9253 Alignment); 9254 verifyFormat("int a, b = 1;\n" 9255 "int c = 2;\n" 9256 "int dd = 3;\n", 9257 Alignment); 9258 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9259 "float b[1][] = {{3.f}};\n", 9260 Alignment); 9261 } 9262 9263 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9264 FormatStyle Alignment = getLLVMStyle(); 9265 Alignment.AlignConsecutiveDeclarations = false; 9266 verifyFormat("float const a = 5;\n" 9267 "int oneTwoThree = 123;", 9268 Alignment); 9269 verifyFormat("int a = 5;\n" 9270 "float const oneTwoThree = 123;", 9271 Alignment); 9272 9273 Alignment.AlignConsecutiveDeclarations = true; 9274 verifyFormat("float const a = 5;\n" 9275 "int oneTwoThree = 123;", 9276 Alignment); 9277 verifyFormat("int a = method();\n" 9278 "float const oneTwoThree = 133;", 9279 Alignment); 9280 verifyFormat("int i = 1, j = 10;\n" 9281 "something = 2000;", 9282 Alignment); 9283 verifyFormat("something = 2000;\n" 9284 "int i = 1, j = 10;\n", 9285 Alignment); 9286 verifyFormat("float something = 2000;\n" 9287 "double another = 911;\n" 9288 "int i = 1, j = 10;\n" 9289 "const int *oneMore = 1;\n" 9290 "unsigned i = 2;", 9291 Alignment); 9292 verifyFormat("float a = 5;\n" 9293 "int one = 1;\n" 9294 "method();\n" 9295 "const double oneTwoThree = 123;\n" 9296 "const unsigned int oneTwo = 12;", 9297 Alignment); 9298 verifyFormat("int oneTwoThree{0}; // comment\n" 9299 "unsigned oneTwo; // comment", 9300 Alignment); 9301 EXPECT_EQ("float const a = 5;\n" 9302 "\n" 9303 "int oneTwoThree = 123;", 9304 format("float const a = 5;\n" 9305 "\n" 9306 "int oneTwoThree= 123;", 9307 Alignment)); 9308 EXPECT_EQ("float a = 5;\n" 9309 "int one = 1;\n" 9310 "\n" 9311 "unsigned oneTwoThree = 123;", 9312 format("float a = 5;\n" 9313 "int one = 1;\n" 9314 "\n" 9315 "unsigned oneTwoThree = 123;", 9316 Alignment)); 9317 EXPECT_EQ("float a = 5;\n" 9318 "int one = 1;\n" 9319 "\n" 9320 "unsigned oneTwoThree = 123;\n" 9321 "int oneTwo = 12;", 9322 format("float a = 5;\n" 9323 "int one = 1;\n" 9324 "\n" 9325 "unsigned oneTwoThree = 123;\n" 9326 "int oneTwo = 12;", 9327 Alignment)); 9328 Alignment.AlignConsecutiveAssignments = true; 9329 verifyFormat("float something = 2000;\n" 9330 "double another = 911;\n" 9331 "int i = 1, j = 10;\n" 9332 "const int *oneMore = 1;\n" 9333 "unsigned i = 2;", 9334 Alignment); 9335 verifyFormat("int oneTwoThree = {0}; // comment\n" 9336 "unsigned oneTwo = 0; // comment", 9337 Alignment); 9338 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9339 " int const i = 1;\n" 9340 " int * j = 2;\n" 9341 " int big = 10000;\n" 9342 "\n" 9343 " unsigned oneTwoThree = 123;\n" 9344 " int oneTwo = 12;\n" 9345 " method();\n" 9346 " float k = 2;\n" 9347 " int ll = 10000;\n" 9348 "}", 9349 format("void SomeFunction(int parameter= 0) {\n" 9350 " int const i= 1;\n" 9351 " int *j=2;\n" 9352 " int big = 10000;\n" 9353 "\n" 9354 "unsigned oneTwoThree =123;\n" 9355 "int oneTwo = 12;\n" 9356 " method();\n" 9357 "float k= 2;\n" 9358 "int ll=10000;\n" 9359 "}", 9360 Alignment)); 9361 Alignment.AlignConsecutiveAssignments = false; 9362 Alignment.AlignEscapedNewlinesLeft = true; 9363 verifyFormat("#define A \\\n" 9364 " int aaaa = 12; \\\n" 9365 " float b = 23; \\\n" 9366 " const int ccc = 234; \\\n" 9367 " unsigned dddddddddd = 2345;", 9368 Alignment); 9369 Alignment.AlignEscapedNewlinesLeft = false; 9370 Alignment.ColumnLimit = 30; 9371 verifyFormat("#define A \\\n" 9372 " int aaaa = 12; \\\n" 9373 " float b = 23; \\\n" 9374 " const int ccc = 234; \\\n" 9375 " int dddddddddd = 2345;", 9376 Alignment); 9377 Alignment.ColumnLimit = 80; 9378 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9379 "k = 4, int l = 5,\n" 9380 " int m = 6) {\n" 9381 " const int j = 10;\n" 9382 " otherThing = 1;\n" 9383 "}", 9384 Alignment); 9385 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9386 " int const i = 1;\n" 9387 " int * j = 2;\n" 9388 " int big = 10000;\n" 9389 "}", 9390 Alignment); 9391 verifyFormat("class C {\n" 9392 "public:\n" 9393 " int i = 1;\n" 9394 " virtual void f() = 0;\n" 9395 "};", 9396 Alignment); 9397 verifyFormat("float i = 1;\n" 9398 "if (SomeType t = getSomething()) {\n" 9399 "}\n" 9400 "const unsigned j = 2;\n" 9401 "int big = 10000;", 9402 Alignment); 9403 verifyFormat("float j = 7;\n" 9404 "for (int k = 0; k < N; ++k) {\n" 9405 "}\n" 9406 "unsigned j = 2;\n" 9407 "int big = 10000;\n" 9408 "}", 9409 Alignment); 9410 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9411 verifyFormat("float i = 1;\n" 9412 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9413 " = someLooooooooooooooooongFunction();\n" 9414 "int j = 2;", 9415 Alignment); 9416 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9417 verifyFormat("int i = 1;\n" 9418 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9419 " someLooooooooooooooooongFunction();\n" 9420 "int j = 2;", 9421 Alignment); 9422 9423 Alignment.AlignConsecutiveAssignments = true; 9424 verifyFormat("auto lambda = []() {\n" 9425 " auto ii = 0;\n" 9426 " float j = 0;\n" 9427 " return 0;\n" 9428 "};\n" 9429 "int i = 0;\n" 9430 "float i2 = 0;\n" 9431 "auto v = type{\n" 9432 " i = 1, //\n" 9433 " (i = 2), //\n" 9434 " i = 3 //\n" 9435 "};", 9436 Alignment); 9437 Alignment.AlignConsecutiveAssignments = false; 9438 9439 // FIXME: Should align all three declarations 9440 verifyFormat( 9441 "int i = 1;\n" 9442 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9443 " loooooooooooooooooooooongParameterB);\n" 9444 "int j = 2;", 9445 Alignment); 9446 9447 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9448 // We expect declarations and assignments to align, as long as it doesn't 9449 // exceed the column limit, starting a new alignemnt sequence whenever it 9450 // happens. 9451 Alignment.AlignConsecutiveAssignments = true; 9452 Alignment.ColumnLimit = 30; 9453 verifyFormat("float ii = 1;\n" 9454 "unsigned j = 2;\n" 9455 "int someVerylongVariable = 1;\n" 9456 "AnotherLongType ll = 123456;\n" 9457 "VeryVeryLongType k = 2;\n" 9458 "int myvar = 1;", 9459 Alignment); 9460 Alignment.ColumnLimit = 80; 9461 Alignment.AlignConsecutiveAssignments = false; 9462 9463 verifyFormat( 9464 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9465 " typename LongType, typename B>\n" 9466 "auto foo() {}\n", 9467 Alignment); 9468 verifyFormat("float a, b = 1;\n" 9469 "int c = 2;\n" 9470 "int dd = 3;\n", 9471 Alignment); 9472 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9473 "float b[1][] = {{3.f}};\n", 9474 Alignment); 9475 Alignment.AlignConsecutiveAssignments = true; 9476 verifyFormat("float a, b = 1;\n" 9477 "int c = 2;\n" 9478 "int dd = 3;\n", 9479 Alignment); 9480 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9481 "float b[1][] = {{3.f}};\n", 9482 Alignment); 9483 Alignment.AlignConsecutiveAssignments = false; 9484 9485 Alignment.ColumnLimit = 30; 9486 Alignment.BinPackParameters = false; 9487 verifyFormat("void foo(float a,\n" 9488 " float b,\n" 9489 " int c,\n" 9490 " uint32_t *d) {\n" 9491 " int * e = 0;\n" 9492 " float f = 0;\n" 9493 " double g = 0;\n" 9494 "}\n" 9495 "void bar(ino_t a,\n" 9496 " int b,\n" 9497 " uint32_t *c,\n" 9498 " bool d) {}\n", 9499 Alignment); 9500 Alignment.BinPackParameters = true; 9501 Alignment.ColumnLimit = 80; 9502 } 9503 9504 TEST_F(FormatTest, LinuxBraceBreaking) { 9505 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9506 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9507 verifyFormat("namespace a\n" 9508 "{\n" 9509 "class A\n" 9510 "{\n" 9511 " void f()\n" 9512 " {\n" 9513 " if (true) {\n" 9514 " a();\n" 9515 " b();\n" 9516 " } else {\n" 9517 " a();\n" 9518 " }\n" 9519 " }\n" 9520 " void g() { return; }\n" 9521 "};\n" 9522 "struct B {\n" 9523 " int x;\n" 9524 "};\n" 9525 "}\n", 9526 LinuxBraceStyle); 9527 verifyFormat("enum X {\n" 9528 " Y = 0,\n" 9529 "}\n", 9530 LinuxBraceStyle); 9531 verifyFormat("struct S {\n" 9532 " int Type;\n" 9533 " union {\n" 9534 " int x;\n" 9535 " double y;\n" 9536 " } Value;\n" 9537 " class C\n" 9538 " {\n" 9539 " MyFavoriteType Value;\n" 9540 " } Class;\n" 9541 "}\n", 9542 LinuxBraceStyle); 9543 } 9544 9545 TEST_F(FormatTest, MozillaBraceBreaking) { 9546 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9547 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9548 verifyFormat("namespace a {\n" 9549 "class A\n" 9550 "{\n" 9551 " void f()\n" 9552 " {\n" 9553 " if (true) {\n" 9554 " a();\n" 9555 " b();\n" 9556 " }\n" 9557 " }\n" 9558 " void g() { return; }\n" 9559 "};\n" 9560 "enum E\n" 9561 "{\n" 9562 " A,\n" 9563 " // foo\n" 9564 " B,\n" 9565 " C\n" 9566 "};\n" 9567 "struct B\n" 9568 "{\n" 9569 " int x;\n" 9570 "};\n" 9571 "}\n", 9572 MozillaBraceStyle); 9573 verifyFormat("struct S\n" 9574 "{\n" 9575 " int Type;\n" 9576 " union\n" 9577 " {\n" 9578 " int x;\n" 9579 " double y;\n" 9580 " } Value;\n" 9581 " class C\n" 9582 " {\n" 9583 " MyFavoriteType Value;\n" 9584 " } Class;\n" 9585 "}\n", 9586 MozillaBraceStyle); 9587 } 9588 9589 TEST_F(FormatTest, StroustrupBraceBreaking) { 9590 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9591 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9592 verifyFormat("namespace a {\n" 9593 "class A {\n" 9594 " void f()\n" 9595 " {\n" 9596 " if (true) {\n" 9597 " a();\n" 9598 " b();\n" 9599 " }\n" 9600 " }\n" 9601 " void g() { return; }\n" 9602 "};\n" 9603 "struct B {\n" 9604 " int x;\n" 9605 "};\n" 9606 "}\n", 9607 StroustrupBraceStyle); 9608 9609 verifyFormat("void foo()\n" 9610 "{\n" 9611 " if (a) {\n" 9612 " a();\n" 9613 " }\n" 9614 " else {\n" 9615 " b();\n" 9616 " }\n" 9617 "}\n", 9618 StroustrupBraceStyle); 9619 9620 verifyFormat("#ifdef _DEBUG\n" 9621 "int foo(int i = 0)\n" 9622 "#else\n" 9623 "int foo(int i = 5)\n" 9624 "#endif\n" 9625 "{\n" 9626 " return i;\n" 9627 "}", 9628 StroustrupBraceStyle); 9629 9630 verifyFormat("void foo() {}\n" 9631 "void bar()\n" 9632 "#ifdef _DEBUG\n" 9633 "{\n" 9634 " foo();\n" 9635 "}\n" 9636 "#else\n" 9637 "{\n" 9638 "}\n" 9639 "#endif", 9640 StroustrupBraceStyle); 9641 9642 verifyFormat("void foobar() { int i = 5; }\n" 9643 "#ifdef _DEBUG\n" 9644 "void bar() {}\n" 9645 "#else\n" 9646 "void bar() { foobar(); }\n" 9647 "#endif", 9648 StroustrupBraceStyle); 9649 } 9650 9651 TEST_F(FormatTest, AllmanBraceBreaking) { 9652 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9653 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9654 verifyFormat("namespace a\n" 9655 "{\n" 9656 "class A\n" 9657 "{\n" 9658 " void f()\n" 9659 " {\n" 9660 " if (true)\n" 9661 " {\n" 9662 " a();\n" 9663 " b();\n" 9664 " }\n" 9665 " }\n" 9666 " void g() { return; }\n" 9667 "};\n" 9668 "struct B\n" 9669 "{\n" 9670 " int x;\n" 9671 "};\n" 9672 "}", 9673 AllmanBraceStyle); 9674 9675 verifyFormat("void f()\n" 9676 "{\n" 9677 " if (true)\n" 9678 " {\n" 9679 " a();\n" 9680 " }\n" 9681 " else if (false)\n" 9682 " {\n" 9683 " b();\n" 9684 " }\n" 9685 " else\n" 9686 " {\n" 9687 " c();\n" 9688 " }\n" 9689 "}\n", 9690 AllmanBraceStyle); 9691 9692 verifyFormat("void f()\n" 9693 "{\n" 9694 " for (int i = 0; i < 10; ++i)\n" 9695 " {\n" 9696 " a();\n" 9697 " }\n" 9698 " while (false)\n" 9699 " {\n" 9700 " b();\n" 9701 " }\n" 9702 " do\n" 9703 " {\n" 9704 " c();\n" 9705 " } while (false)\n" 9706 "}\n", 9707 AllmanBraceStyle); 9708 9709 verifyFormat("void f(int a)\n" 9710 "{\n" 9711 " switch (a)\n" 9712 " {\n" 9713 " case 0:\n" 9714 " break;\n" 9715 " case 1:\n" 9716 " {\n" 9717 " break;\n" 9718 " }\n" 9719 " case 2:\n" 9720 " {\n" 9721 " }\n" 9722 " break;\n" 9723 " default:\n" 9724 " break;\n" 9725 " }\n" 9726 "}\n", 9727 AllmanBraceStyle); 9728 9729 verifyFormat("enum X\n" 9730 "{\n" 9731 " Y = 0,\n" 9732 "}\n", 9733 AllmanBraceStyle); 9734 verifyFormat("enum X\n" 9735 "{\n" 9736 " Y = 0\n" 9737 "}\n", 9738 AllmanBraceStyle); 9739 9740 verifyFormat("@interface BSApplicationController ()\n" 9741 "{\n" 9742 "@private\n" 9743 " id _extraIvar;\n" 9744 "}\n" 9745 "@end\n", 9746 AllmanBraceStyle); 9747 9748 verifyFormat("#ifdef _DEBUG\n" 9749 "int foo(int i = 0)\n" 9750 "#else\n" 9751 "int foo(int i = 5)\n" 9752 "#endif\n" 9753 "{\n" 9754 " return i;\n" 9755 "}", 9756 AllmanBraceStyle); 9757 9758 verifyFormat("void foo() {}\n" 9759 "void bar()\n" 9760 "#ifdef _DEBUG\n" 9761 "{\n" 9762 " foo();\n" 9763 "}\n" 9764 "#else\n" 9765 "{\n" 9766 "}\n" 9767 "#endif", 9768 AllmanBraceStyle); 9769 9770 verifyFormat("void foobar() { int i = 5; }\n" 9771 "#ifdef _DEBUG\n" 9772 "void bar() {}\n" 9773 "#else\n" 9774 "void bar() { foobar(); }\n" 9775 "#endif", 9776 AllmanBraceStyle); 9777 9778 // This shouldn't affect ObjC blocks.. 9779 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9780 " // ...\n" 9781 " int i;\n" 9782 "}];", 9783 AllmanBraceStyle); 9784 verifyFormat("void (^block)(void) = ^{\n" 9785 " // ...\n" 9786 " int i;\n" 9787 "};", 9788 AllmanBraceStyle); 9789 // .. or dict literals. 9790 verifyFormat("void f()\n" 9791 "{\n" 9792 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9793 "}", 9794 AllmanBraceStyle); 9795 verifyFormat("int f()\n" 9796 "{ // comment\n" 9797 " return 42;\n" 9798 "}", 9799 AllmanBraceStyle); 9800 9801 AllmanBraceStyle.ColumnLimit = 19; 9802 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9803 AllmanBraceStyle.ColumnLimit = 18; 9804 verifyFormat("void f()\n" 9805 "{\n" 9806 " int i;\n" 9807 "}", 9808 AllmanBraceStyle); 9809 AllmanBraceStyle.ColumnLimit = 80; 9810 9811 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9812 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9813 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9814 verifyFormat("void f(bool b)\n" 9815 "{\n" 9816 " if (b)\n" 9817 " {\n" 9818 " return;\n" 9819 " }\n" 9820 "}\n", 9821 BreakBeforeBraceShortIfs); 9822 verifyFormat("void f(bool b)\n" 9823 "{\n" 9824 " if (b) return;\n" 9825 "}\n", 9826 BreakBeforeBraceShortIfs); 9827 verifyFormat("void f(bool b)\n" 9828 "{\n" 9829 " while (b)\n" 9830 " {\n" 9831 " return;\n" 9832 " }\n" 9833 "}\n", 9834 BreakBeforeBraceShortIfs); 9835 } 9836 9837 TEST_F(FormatTest, GNUBraceBreaking) { 9838 FormatStyle GNUBraceStyle = getLLVMStyle(); 9839 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9840 verifyFormat("namespace a\n" 9841 "{\n" 9842 "class A\n" 9843 "{\n" 9844 " void f()\n" 9845 " {\n" 9846 " int a;\n" 9847 " {\n" 9848 " int b;\n" 9849 " }\n" 9850 " if (true)\n" 9851 " {\n" 9852 " a();\n" 9853 " b();\n" 9854 " }\n" 9855 " }\n" 9856 " void g() { return; }\n" 9857 "}\n" 9858 "}", 9859 GNUBraceStyle); 9860 9861 verifyFormat("void f()\n" 9862 "{\n" 9863 " if (true)\n" 9864 " {\n" 9865 " a();\n" 9866 " }\n" 9867 " else if (false)\n" 9868 " {\n" 9869 " b();\n" 9870 " }\n" 9871 " else\n" 9872 " {\n" 9873 " c();\n" 9874 " }\n" 9875 "}\n", 9876 GNUBraceStyle); 9877 9878 verifyFormat("void f()\n" 9879 "{\n" 9880 " for (int i = 0; i < 10; ++i)\n" 9881 " {\n" 9882 " a();\n" 9883 " }\n" 9884 " while (false)\n" 9885 " {\n" 9886 " b();\n" 9887 " }\n" 9888 " do\n" 9889 " {\n" 9890 " c();\n" 9891 " }\n" 9892 " while (false);\n" 9893 "}\n", 9894 GNUBraceStyle); 9895 9896 verifyFormat("void f(int a)\n" 9897 "{\n" 9898 " switch (a)\n" 9899 " {\n" 9900 " case 0:\n" 9901 " break;\n" 9902 " case 1:\n" 9903 " {\n" 9904 " break;\n" 9905 " }\n" 9906 " case 2:\n" 9907 " {\n" 9908 " }\n" 9909 " break;\n" 9910 " default:\n" 9911 " break;\n" 9912 " }\n" 9913 "}\n", 9914 GNUBraceStyle); 9915 9916 verifyFormat("enum X\n" 9917 "{\n" 9918 " Y = 0,\n" 9919 "}\n", 9920 GNUBraceStyle); 9921 9922 verifyFormat("@interface BSApplicationController ()\n" 9923 "{\n" 9924 "@private\n" 9925 " id _extraIvar;\n" 9926 "}\n" 9927 "@end\n", 9928 GNUBraceStyle); 9929 9930 verifyFormat("#ifdef _DEBUG\n" 9931 "int foo(int i = 0)\n" 9932 "#else\n" 9933 "int foo(int i = 5)\n" 9934 "#endif\n" 9935 "{\n" 9936 " return i;\n" 9937 "}", 9938 GNUBraceStyle); 9939 9940 verifyFormat("void foo() {}\n" 9941 "void bar()\n" 9942 "#ifdef _DEBUG\n" 9943 "{\n" 9944 " foo();\n" 9945 "}\n" 9946 "#else\n" 9947 "{\n" 9948 "}\n" 9949 "#endif", 9950 GNUBraceStyle); 9951 9952 verifyFormat("void foobar() { int i = 5; }\n" 9953 "#ifdef _DEBUG\n" 9954 "void bar() {}\n" 9955 "#else\n" 9956 "void bar() { foobar(); }\n" 9957 "#endif", 9958 GNUBraceStyle); 9959 } 9960 9961 TEST_F(FormatTest, WebKitBraceBreaking) { 9962 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9963 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9964 verifyFormat("namespace a {\n" 9965 "class A {\n" 9966 " void f()\n" 9967 " {\n" 9968 " if (true) {\n" 9969 " a();\n" 9970 " b();\n" 9971 " }\n" 9972 " }\n" 9973 " void g() { return; }\n" 9974 "};\n" 9975 "enum E {\n" 9976 " A,\n" 9977 " // foo\n" 9978 " B,\n" 9979 " C\n" 9980 "};\n" 9981 "struct B {\n" 9982 " int x;\n" 9983 "};\n" 9984 "}\n", 9985 WebKitBraceStyle); 9986 verifyFormat("struct S {\n" 9987 " int Type;\n" 9988 " union {\n" 9989 " int x;\n" 9990 " double y;\n" 9991 " } Value;\n" 9992 " class C {\n" 9993 " MyFavoriteType Value;\n" 9994 " } Class;\n" 9995 "};\n", 9996 WebKitBraceStyle); 9997 } 9998 9999 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10000 verifyFormat("void f() {\n" 10001 " try {\n" 10002 " } catch (const Exception &e) {\n" 10003 " }\n" 10004 "}\n", 10005 getLLVMStyle()); 10006 } 10007 10008 TEST_F(FormatTest, UnderstandsPragmas) { 10009 verifyFormat("#pragma omp reduction(| : var)"); 10010 verifyFormat("#pragma omp reduction(+ : var)"); 10011 10012 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10013 "(including parentheses).", 10014 format("#pragma mark Any non-hyphenated or hyphenated string " 10015 "(including parentheses).")); 10016 } 10017 10018 TEST_F(FormatTest, UnderstandPragmaOption) { 10019 verifyFormat("#pragma option -C -A"); 10020 10021 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10022 } 10023 10024 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10025 for (size_t i = 1; i < Styles.size(); ++i) \ 10026 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10027 << " differs from Style #0" 10028 10029 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10030 SmallVector<FormatStyle, 3> Styles; 10031 Styles.resize(3); 10032 10033 Styles[0] = getLLVMStyle(); 10034 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10035 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10036 EXPECT_ALL_STYLES_EQUAL(Styles); 10037 10038 Styles[0] = getGoogleStyle(); 10039 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10040 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10041 EXPECT_ALL_STYLES_EQUAL(Styles); 10042 10043 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10044 EXPECT_TRUE( 10045 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10046 EXPECT_TRUE( 10047 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10048 EXPECT_ALL_STYLES_EQUAL(Styles); 10049 10050 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10051 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10052 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10053 EXPECT_ALL_STYLES_EQUAL(Styles); 10054 10055 Styles[0] = getMozillaStyle(); 10056 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10057 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10058 EXPECT_ALL_STYLES_EQUAL(Styles); 10059 10060 Styles[0] = getWebKitStyle(); 10061 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10062 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10063 EXPECT_ALL_STYLES_EQUAL(Styles); 10064 10065 Styles[0] = getGNUStyle(); 10066 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10067 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10068 EXPECT_ALL_STYLES_EQUAL(Styles); 10069 10070 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10071 } 10072 10073 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10074 SmallVector<FormatStyle, 8> Styles; 10075 Styles.resize(2); 10076 10077 Styles[0] = getGoogleStyle(); 10078 Styles[1] = getLLVMStyle(); 10079 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10080 EXPECT_ALL_STYLES_EQUAL(Styles); 10081 10082 Styles.resize(5); 10083 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10084 Styles[1] = getLLVMStyle(); 10085 Styles[1].Language = FormatStyle::LK_JavaScript; 10086 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10087 10088 Styles[2] = getLLVMStyle(); 10089 Styles[2].Language = FormatStyle::LK_JavaScript; 10090 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10091 "BasedOnStyle: Google", 10092 &Styles[2]) 10093 .value()); 10094 10095 Styles[3] = getLLVMStyle(); 10096 Styles[3].Language = FormatStyle::LK_JavaScript; 10097 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10098 "Language: JavaScript", 10099 &Styles[3]) 10100 .value()); 10101 10102 Styles[4] = getLLVMStyle(); 10103 Styles[4].Language = FormatStyle::LK_JavaScript; 10104 EXPECT_EQ(0, parseConfiguration("---\n" 10105 "BasedOnStyle: LLVM\n" 10106 "IndentWidth: 123\n" 10107 "---\n" 10108 "BasedOnStyle: Google\n" 10109 "Language: JavaScript", 10110 &Styles[4]) 10111 .value()); 10112 EXPECT_ALL_STYLES_EQUAL(Styles); 10113 } 10114 10115 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10116 Style.FIELD = false; \ 10117 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10118 EXPECT_TRUE(Style.FIELD); \ 10119 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10120 EXPECT_FALSE(Style.FIELD); 10121 10122 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10123 10124 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10125 Style.STRUCT.FIELD = false; \ 10126 EXPECT_EQ(0, \ 10127 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10128 .value()); \ 10129 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10130 EXPECT_EQ(0, \ 10131 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10132 .value()); \ 10133 EXPECT_FALSE(Style.STRUCT.FIELD); 10134 10135 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10136 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10137 10138 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10139 EXPECT_NE(VALUE, Style.FIELD); \ 10140 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10141 EXPECT_EQ(VALUE, Style.FIELD) 10142 10143 TEST_F(FormatTest, ParsesConfigurationBools) { 10144 FormatStyle Style = {}; 10145 Style.Language = FormatStyle::LK_Cpp; 10146 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 10147 CHECK_PARSE_BOOL(AlignOperands); 10148 CHECK_PARSE_BOOL(AlignTrailingComments); 10149 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10150 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10151 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10152 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10153 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10154 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10155 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10156 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10157 CHECK_PARSE_BOOL(BinPackArguments); 10158 CHECK_PARSE_BOOL(BinPackParameters); 10159 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10160 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10161 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 10162 CHECK_PARSE_BOOL(BreakStringLiterals); 10163 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10164 CHECK_PARSE_BOOL(DerivePointerAlignment); 10165 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10166 CHECK_PARSE_BOOL(DisableFormat); 10167 CHECK_PARSE_BOOL(IndentCaseLabels); 10168 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10169 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10170 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10171 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10172 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10173 CHECK_PARSE_BOOL(ReflowComments); 10174 CHECK_PARSE_BOOL(SortIncludes); 10175 CHECK_PARSE_BOOL(SpacesInParentheses); 10176 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10177 CHECK_PARSE_BOOL(SpacesInAngles); 10178 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10179 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10180 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10181 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10182 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10183 10184 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10185 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10186 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10187 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10188 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10189 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10190 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10191 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10192 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10193 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10194 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10195 } 10196 10197 #undef CHECK_PARSE_BOOL 10198 10199 TEST_F(FormatTest, ParsesConfiguration) { 10200 FormatStyle Style = {}; 10201 Style.Language = FormatStyle::LK_Cpp; 10202 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10203 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10204 ConstructorInitializerIndentWidth, 1234u); 10205 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10206 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10207 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10208 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10209 PenaltyBreakBeforeFirstCallParameter, 1234u); 10210 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10211 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10212 PenaltyReturnTypeOnItsOwnLine, 1234u); 10213 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10214 SpacesBeforeTrailingComments, 1234u); 10215 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10216 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10217 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10218 10219 Style.PointerAlignment = FormatStyle::PAS_Middle; 10220 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10221 FormatStyle::PAS_Left); 10222 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10223 FormatStyle::PAS_Right); 10224 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10225 FormatStyle::PAS_Middle); 10226 // For backward compatibility: 10227 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10228 FormatStyle::PAS_Left); 10229 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10230 FormatStyle::PAS_Right); 10231 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10232 FormatStyle::PAS_Middle); 10233 10234 Style.Standard = FormatStyle::LS_Auto; 10235 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10236 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10237 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10238 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10239 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10240 10241 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10242 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10243 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10244 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10245 FormatStyle::BOS_None); 10246 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10247 FormatStyle::BOS_All); 10248 // For backward compatibility: 10249 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10250 FormatStyle::BOS_None); 10251 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10252 FormatStyle::BOS_All); 10253 10254 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10255 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10256 FormatStyle::BAS_Align); 10257 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10258 FormatStyle::BAS_DontAlign); 10259 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10260 FormatStyle::BAS_AlwaysBreak); 10261 // For backward compatibility: 10262 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10263 FormatStyle::BAS_DontAlign); 10264 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10265 FormatStyle::BAS_Align); 10266 10267 Style.UseTab = FormatStyle::UT_ForIndentation; 10268 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10269 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10270 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10271 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10272 FormatStyle::UT_ForContinuationAndIndentation); 10273 // For backward compatibility: 10274 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10275 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10276 10277 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10278 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10279 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10280 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10281 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10282 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10283 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10284 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10285 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10286 // For backward compatibility: 10287 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10288 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10289 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10290 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10291 10292 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10293 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10294 FormatStyle::SBPO_Never); 10295 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10296 FormatStyle::SBPO_Always); 10297 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10298 FormatStyle::SBPO_ControlStatements); 10299 // For backward compatibility: 10300 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10301 FormatStyle::SBPO_Never); 10302 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10303 FormatStyle::SBPO_ControlStatements); 10304 10305 Style.ColumnLimit = 123; 10306 FormatStyle BaseStyle = getLLVMStyle(); 10307 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10308 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10309 10310 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10311 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10312 FormatStyle::BS_Attach); 10313 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10314 FormatStyle::BS_Linux); 10315 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10316 FormatStyle::BS_Mozilla); 10317 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10318 FormatStyle::BS_Stroustrup); 10319 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10320 FormatStyle::BS_Allman); 10321 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10322 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10323 FormatStyle::BS_WebKit); 10324 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10325 FormatStyle::BS_Custom); 10326 10327 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10328 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10329 FormatStyle::RTBS_None); 10330 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10331 FormatStyle::RTBS_All); 10332 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10333 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10334 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10335 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10336 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10337 AlwaysBreakAfterReturnType, 10338 FormatStyle::RTBS_TopLevelDefinitions); 10339 10340 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10341 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10342 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10343 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10344 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10345 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10346 AlwaysBreakAfterDefinitionReturnType, 10347 FormatStyle::DRTBS_TopLevel); 10348 10349 Style.NamespaceIndentation = FormatStyle::NI_All; 10350 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10351 FormatStyle::NI_None); 10352 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10353 FormatStyle::NI_Inner); 10354 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10355 FormatStyle::NI_All); 10356 10357 // FIXME: This is required because parsing a configuration simply overwrites 10358 // the first N elements of the list instead of resetting it. 10359 Style.ForEachMacros.clear(); 10360 std::vector<std::string> BoostForeach; 10361 BoostForeach.push_back("BOOST_FOREACH"); 10362 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10363 std::vector<std::string> BoostAndQForeach; 10364 BoostAndQForeach.push_back("BOOST_FOREACH"); 10365 BoostAndQForeach.push_back("Q_FOREACH"); 10366 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10367 BoostAndQForeach); 10368 10369 Style.IncludeCategories.clear(); 10370 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10371 {".*", 1}}; 10372 CHECK_PARSE("IncludeCategories:\n" 10373 " - Regex: abc/.*\n" 10374 " Priority: 2\n" 10375 " - Regex: .*\n" 10376 " Priority: 1", 10377 IncludeCategories, ExpectedCategories); 10378 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10379 } 10380 10381 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10382 FormatStyle Style = {}; 10383 Style.Language = FormatStyle::LK_Cpp; 10384 CHECK_PARSE("Language: Cpp\n" 10385 "IndentWidth: 12", 10386 IndentWidth, 12u); 10387 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10388 "IndentWidth: 34", 10389 &Style), 10390 ParseError::Unsuitable); 10391 EXPECT_EQ(12u, Style.IndentWidth); 10392 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10393 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10394 10395 Style.Language = FormatStyle::LK_JavaScript; 10396 CHECK_PARSE("Language: JavaScript\n" 10397 "IndentWidth: 12", 10398 IndentWidth, 12u); 10399 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10400 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10401 "IndentWidth: 34", 10402 &Style), 10403 ParseError::Unsuitable); 10404 EXPECT_EQ(23u, Style.IndentWidth); 10405 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10406 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10407 10408 CHECK_PARSE("BasedOnStyle: LLVM\n" 10409 "IndentWidth: 67", 10410 IndentWidth, 67u); 10411 10412 CHECK_PARSE("---\n" 10413 "Language: JavaScript\n" 10414 "IndentWidth: 12\n" 10415 "---\n" 10416 "Language: Cpp\n" 10417 "IndentWidth: 34\n" 10418 "...\n", 10419 IndentWidth, 12u); 10420 10421 Style.Language = FormatStyle::LK_Cpp; 10422 CHECK_PARSE("---\n" 10423 "Language: JavaScript\n" 10424 "IndentWidth: 12\n" 10425 "---\n" 10426 "Language: Cpp\n" 10427 "IndentWidth: 34\n" 10428 "...\n", 10429 IndentWidth, 34u); 10430 CHECK_PARSE("---\n" 10431 "IndentWidth: 78\n" 10432 "---\n" 10433 "Language: JavaScript\n" 10434 "IndentWidth: 56\n" 10435 "...\n", 10436 IndentWidth, 78u); 10437 10438 Style.ColumnLimit = 123; 10439 Style.IndentWidth = 234; 10440 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10441 Style.TabWidth = 345; 10442 EXPECT_FALSE(parseConfiguration("---\n" 10443 "IndentWidth: 456\n" 10444 "BreakBeforeBraces: Allman\n" 10445 "---\n" 10446 "Language: JavaScript\n" 10447 "IndentWidth: 111\n" 10448 "TabWidth: 111\n" 10449 "---\n" 10450 "Language: Cpp\n" 10451 "BreakBeforeBraces: Stroustrup\n" 10452 "TabWidth: 789\n" 10453 "...\n", 10454 &Style)); 10455 EXPECT_EQ(123u, Style.ColumnLimit); 10456 EXPECT_EQ(456u, Style.IndentWidth); 10457 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10458 EXPECT_EQ(789u, Style.TabWidth); 10459 10460 EXPECT_EQ(parseConfiguration("---\n" 10461 "Language: JavaScript\n" 10462 "IndentWidth: 56\n" 10463 "---\n" 10464 "IndentWidth: 78\n" 10465 "...\n", 10466 &Style), 10467 ParseError::Error); 10468 EXPECT_EQ(parseConfiguration("---\n" 10469 "Language: JavaScript\n" 10470 "IndentWidth: 56\n" 10471 "---\n" 10472 "Language: JavaScript\n" 10473 "IndentWidth: 78\n" 10474 "...\n", 10475 &Style), 10476 ParseError::Error); 10477 10478 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10479 } 10480 10481 #undef CHECK_PARSE 10482 10483 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10484 FormatStyle Style = {}; 10485 Style.Language = FormatStyle::LK_JavaScript; 10486 Style.BreakBeforeTernaryOperators = true; 10487 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10488 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10489 10490 Style.BreakBeforeTernaryOperators = true; 10491 EXPECT_EQ(0, parseConfiguration("---\n" 10492 "BasedOnStyle: Google\n" 10493 "---\n" 10494 "Language: JavaScript\n" 10495 "IndentWidth: 76\n" 10496 "...\n", 10497 &Style) 10498 .value()); 10499 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10500 EXPECT_EQ(76u, Style.IndentWidth); 10501 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10502 } 10503 10504 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10505 FormatStyle Style = getLLVMStyle(); 10506 std::string YAML = configurationAsText(Style); 10507 FormatStyle ParsedStyle = {}; 10508 ParsedStyle.Language = FormatStyle::LK_Cpp; 10509 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10510 EXPECT_EQ(Style, ParsedStyle); 10511 } 10512 10513 TEST_F(FormatTest, WorksFor8bitEncodings) { 10514 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10515 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10516 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10517 "\"\xef\xee\xf0\xf3...\"", 10518 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10519 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10520 "\xef\xee\xf0\xf3...\"", 10521 getLLVMStyleWithColumns(12))); 10522 } 10523 10524 TEST_F(FormatTest, HandlesUTF8BOM) { 10525 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10526 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10527 format("\xef\xbb\xbf#include <iostream>")); 10528 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10529 format("\xef\xbb\xbf\n#include <iostream>")); 10530 } 10531 10532 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10533 #if !defined(_MSC_VER) 10534 10535 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10536 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10537 getLLVMStyleWithColumns(35)); 10538 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10539 getLLVMStyleWithColumns(31)); 10540 verifyFormat("// Однажды в студёную зимнюю пору...", 10541 getLLVMStyleWithColumns(36)); 10542 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10543 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10544 getLLVMStyleWithColumns(39)); 10545 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10546 getLLVMStyleWithColumns(35)); 10547 } 10548 10549 TEST_F(FormatTest, SplitsUTF8Strings) { 10550 // Non-printable characters' width is currently considered to be the length in 10551 // bytes in UTF8. The characters can be displayed in very different manner 10552 // (zero-width, single width with a substitution glyph, expanded to their code 10553 // (e.g. "<8d>"), so there's no single correct way to handle them. 10554 EXPECT_EQ("\"aaaaÄ\"\n" 10555 "\"\xc2\x8d\";", 10556 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10557 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10558 "\"\xc2\x8d\";", 10559 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10560 EXPECT_EQ("\"Однажды, в \"\n" 10561 "\"студёную \"\n" 10562 "\"зимнюю \"\n" 10563 "\"пору,\"", 10564 format("\"Однажды, в студёную зимнюю пору,\"", 10565 getLLVMStyleWithColumns(13))); 10566 EXPECT_EQ( 10567 "\"一 二 三 \"\n" 10568 "\"四 五六 \"\n" 10569 "\"七 八 九 \"\n" 10570 "\"十\"", 10571 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10572 EXPECT_EQ("\"一\t二 \"\n" 10573 "\"\t三 \"\n" 10574 "\"四 五\t六 \"\n" 10575 "\"\t七 \"\n" 10576 "\"八九十\tqq\"", 10577 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10578 getLLVMStyleWithColumns(11))); 10579 10580 // UTF8 character in an escape sequence. 10581 EXPECT_EQ("\"aaaaaa\"\n" 10582 "\"\\\xC2\x8D\"", 10583 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10584 } 10585 10586 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10587 EXPECT_EQ("const char *sssss =\n" 10588 " \"一二三四五六七八\\\n" 10589 " 九 十\";", 10590 format("const char *sssss = \"一二三四五六七八\\\n" 10591 " 九 十\";", 10592 getLLVMStyleWithColumns(30))); 10593 } 10594 10595 TEST_F(FormatTest, SplitsUTF8LineComments) { 10596 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10597 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10598 EXPECT_EQ("// Я из лесу\n" 10599 "// вышел; был\n" 10600 "// сильный\n" 10601 "// мороз.", 10602 format("// Я из лесу вышел; был сильный мороз.", 10603 getLLVMStyleWithColumns(13))); 10604 EXPECT_EQ("// 一二三\n" 10605 "// 四五六七\n" 10606 "// 八 九\n" 10607 "// 十", 10608 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10609 } 10610 10611 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10612 EXPECT_EQ("/* Гляжу,\n" 10613 " * поднимается\n" 10614 " * медленно в\n" 10615 " * гору\n" 10616 " * Лошадка,\n" 10617 " * везущая\n" 10618 " * хворосту\n" 10619 " * воз. */", 10620 format("/* Гляжу, поднимается медленно в гору\n" 10621 " * Лошадка, везущая хворосту воз. */", 10622 getLLVMStyleWithColumns(13))); 10623 EXPECT_EQ( 10624 "/* 一二三\n" 10625 " * 四五六七\n" 10626 " * 八 九\n" 10627 " * 十 */", 10628 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10629 EXPECT_EQ("/* \n" 10630 " * \n" 10631 " * - */", 10632 format("/* - */", getLLVMStyleWithColumns(12))); 10633 } 10634 10635 #endif // _MSC_VER 10636 10637 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10638 FormatStyle Style = getLLVMStyle(); 10639 10640 Style.ConstructorInitializerIndentWidth = 4; 10641 verifyFormat( 10642 "SomeClass::Constructor()\n" 10643 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10644 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10645 Style); 10646 10647 Style.ConstructorInitializerIndentWidth = 2; 10648 verifyFormat( 10649 "SomeClass::Constructor()\n" 10650 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10651 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10652 Style); 10653 10654 Style.ConstructorInitializerIndentWidth = 0; 10655 verifyFormat( 10656 "SomeClass::Constructor()\n" 10657 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10658 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10659 Style); 10660 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10661 verifyFormat( 10662 "SomeLongTemplateVariableName<\n" 10663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10664 Style); 10665 verifyFormat( 10666 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10668 Style); 10669 } 10670 10671 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10672 FormatStyle Style = getLLVMStyle(); 10673 Style.BreakConstructorInitializersBeforeComma = true; 10674 Style.ConstructorInitializerIndentWidth = 4; 10675 verifyFormat("SomeClass::Constructor()\n" 10676 " : a(a)\n" 10677 " , b(b)\n" 10678 " , c(c) {}", 10679 Style); 10680 verifyFormat("SomeClass::Constructor()\n" 10681 " : a(a) {}", 10682 Style); 10683 10684 Style.ColumnLimit = 0; 10685 verifyFormat("SomeClass::Constructor()\n" 10686 " : a(a) {}", 10687 Style); 10688 verifyFormat("SomeClass::Constructor() noexcept\n" 10689 " : a(a) {}", 10690 Style); 10691 verifyFormat("SomeClass::Constructor()\n" 10692 " : a(a)\n" 10693 " , b(b)\n" 10694 " , c(c) {}", 10695 Style); 10696 verifyFormat("SomeClass::Constructor()\n" 10697 " : a(a) {\n" 10698 " foo();\n" 10699 " bar();\n" 10700 "}", 10701 Style); 10702 10703 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10704 verifyFormat("SomeClass::Constructor()\n" 10705 " : a(a)\n" 10706 " , b(b)\n" 10707 " , c(c) {\n}", 10708 Style); 10709 verifyFormat("SomeClass::Constructor()\n" 10710 " : a(a) {\n}", 10711 Style); 10712 10713 Style.ColumnLimit = 80; 10714 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10715 Style.ConstructorInitializerIndentWidth = 2; 10716 verifyFormat("SomeClass::Constructor()\n" 10717 " : a(a)\n" 10718 " , b(b)\n" 10719 " , c(c) {}", 10720 Style); 10721 10722 Style.ConstructorInitializerIndentWidth = 0; 10723 verifyFormat("SomeClass::Constructor()\n" 10724 ": a(a)\n" 10725 ", b(b)\n" 10726 ", c(c) {}", 10727 Style); 10728 10729 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10730 Style.ConstructorInitializerIndentWidth = 4; 10731 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10732 verifyFormat( 10733 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10734 Style); 10735 verifyFormat( 10736 "SomeClass::Constructor()\n" 10737 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10738 Style); 10739 Style.ConstructorInitializerIndentWidth = 4; 10740 Style.ColumnLimit = 60; 10741 verifyFormat("SomeClass::Constructor()\n" 10742 " : aaaaaaaa(aaaaaaaa)\n" 10743 " , aaaaaaaa(aaaaaaaa)\n" 10744 " , aaaaaaaa(aaaaaaaa) {}", 10745 Style); 10746 } 10747 10748 TEST_F(FormatTest, Destructors) { 10749 verifyFormat("void F(int &i) { i.~int(); }"); 10750 verifyFormat("void F(int &i) { i->~int(); }"); 10751 } 10752 10753 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10754 FormatStyle Style = getWebKitStyle(); 10755 10756 // Don't indent in outer namespaces. 10757 verifyFormat("namespace outer {\n" 10758 "int i;\n" 10759 "namespace inner {\n" 10760 " int i;\n" 10761 "} // namespace inner\n" 10762 "} // namespace outer\n" 10763 "namespace other_outer {\n" 10764 "int i;\n" 10765 "}", 10766 Style); 10767 10768 // Don't indent case labels. 10769 verifyFormat("switch (variable) {\n" 10770 "case 1:\n" 10771 "case 2:\n" 10772 " doSomething();\n" 10773 " break;\n" 10774 "default:\n" 10775 " ++variable;\n" 10776 "}", 10777 Style); 10778 10779 // Wrap before binary operators. 10780 EXPECT_EQ("void f()\n" 10781 "{\n" 10782 " if (aaaaaaaaaaaaaaaa\n" 10783 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10784 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10785 " return;\n" 10786 "}", 10787 format("void f() {\n" 10788 "if (aaaaaaaaaaaaaaaa\n" 10789 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10790 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10791 "return;\n" 10792 "}", 10793 Style)); 10794 10795 // Allow functions on a single line. 10796 verifyFormat("void f() { return; }", Style); 10797 10798 // Constructor initializers are formatted one per line with the "," on the 10799 // new line. 10800 verifyFormat("Constructor()\n" 10801 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10802 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10803 " aaaaaaaaaaaaaa)\n" 10804 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10805 "{\n" 10806 "}", 10807 Style); 10808 verifyFormat("SomeClass::Constructor()\n" 10809 " : a(a)\n" 10810 "{\n" 10811 "}", 10812 Style); 10813 EXPECT_EQ("SomeClass::Constructor()\n" 10814 " : a(a)\n" 10815 "{\n" 10816 "}", 10817 format("SomeClass::Constructor():a(a){}", Style)); 10818 verifyFormat("SomeClass::Constructor()\n" 10819 " : a(a)\n" 10820 " , b(b)\n" 10821 " , c(c)\n" 10822 "{\n" 10823 "}", 10824 Style); 10825 verifyFormat("SomeClass::Constructor()\n" 10826 " : a(a)\n" 10827 "{\n" 10828 " foo();\n" 10829 " bar();\n" 10830 "}", 10831 Style); 10832 10833 // Access specifiers should be aligned left. 10834 verifyFormat("class C {\n" 10835 "public:\n" 10836 " int i;\n" 10837 "};", 10838 Style); 10839 10840 // Do not align comments. 10841 verifyFormat("int a; // Do not\n" 10842 "double b; // align comments.", 10843 Style); 10844 10845 // Do not align operands. 10846 EXPECT_EQ("ASSERT(aaaa\n" 10847 " || bbbb);", 10848 format("ASSERT ( aaaa\n||bbbb);", Style)); 10849 10850 // Accept input's line breaks. 10851 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10852 " || bbbbbbbbbbbbbbb) {\n" 10853 " i++;\n" 10854 "}", 10855 format("if (aaaaaaaaaaaaaaa\n" 10856 "|| bbbbbbbbbbbbbbb) { i++; }", 10857 Style)); 10858 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10859 " i++;\n" 10860 "}", 10861 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10862 10863 // Don't automatically break all macro definitions (llvm.org/PR17842). 10864 verifyFormat("#define aNumber 10", Style); 10865 // However, generally keep the line breaks that the user authored. 10866 EXPECT_EQ("#define aNumber \\\n" 10867 " 10", 10868 format("#define aNumber \\\n" 10869 " 10", 10870 Style)); 10871 10872 // Keep empty and one-element array literals on a single line. 10873 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10874 " copyItems:YES];", 10875 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10876 "copyItems:YES];", 10877 Style)); 10878 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10879 " copyItems:YES];", 10880 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10881 " copyItems:YES];", 10882 Style)); 10883 // FIXME: This does not seem right, there should be more indentation before 10884 // the array literal's entries. Nested blocks have the same problem. 10885 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10886 " @\"a\",\n" 10887 " @\"a\"\n" 10888 "]\n" 10889 " copyItems:YES];", 10890 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10891 " @\"a\",\n" 10892 " @\"a\"\n" 10893 " ]\n" 10894 " copyItems:YES];", 10895 Style)); 10896 EXPECT_EQ( 10897 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10898 " copyItems:YES];", 10899 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10900 " copyItems:YES];", 10901 Style)); 10902 10903 verifyFormat("[self.a b:c c:d];", Style); 10904 EXPECT_EQ("[self.a b:c\n" 10905 " c:d];", 10906 format("[self.a b:c\n" 10907 "c:d];", 10908 Style)); 10909 } 10910 10911 TEST_F(FormatTest, FormatsLambdas) { 10912 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10913 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10914 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10915 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10916 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10917 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10918 verifyFormat("void f() {\n" 10919 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10920 "}\n"); 10921 verifyFormat("void f() {\n" 10922 " other(x.begin(), //\n" 10923 " x.end(), //\n" 10924 " [&](int, int) { return 1; });\n" 10925 "}\n"); 10926 verifyFormat("SomeFunction([]() { // A cool function...\n" 10927 " return 43;\n" 10928 "});"); 10929 EXPECT_EQ("SomeFunction([]() {\n" 10930 "#define A a\n" 10931 " return 43;\n" 10932 "});", 10933 format("SomeFunction([](){\n" 10934 "#define A a\n" 10935 "return 43;\n" 10936 "});")); 10937 verifyFormat("void f() {\n" 10938 " SomeFunction([](decltype(x), A *a) {});\n" 10939 "}"); 10940 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10941 " [](const aaaaaaaaaa &a) { return a; });"); 10942 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10943 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10944 "});"); 10945 verifyFormat("Constructor()\n" 10946 " : Field([] { // comment\n" 10947 " int i;\n" 10948 " }) {}"); 10949 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10950 " return some_parameter.size();\n" 10951 "};"); 10952 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10953 " [](const string &s) { return s; };"); 10954 verifyFormat("int i = aaaaaa ? 1 //\n" 10955 " : [] {\n" 10956 " return 2; //\n" 10957 " }();"); 10958 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10959 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10960 " return x == 2; // force break\n" 10961 " });"); 10962 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10963 " int iiiiiiiiiiii) {\n" 10964 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10965 "});", 10966 getLLVMStyleWithColumns(60)); 10967 verifyFormat("SomeFunction({[&] {\n" 10968 " // comment\n" 10969 " },\n" 10970 " [&] {\n" 10971 " // comment\n" 10972 " }});"); 10973 verifyFormat("SomeFunction({[&] {\n" 10974 " // comment\n" 10975 "}});"); 10976 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10977 " [&]() { return true; },\n" 10978 " aaaaa aaaaaaaaa);"); 10979 10980 // Lambdas with return types. 10981 verifyFormat("int c = []() -> int { return 2; }();\n"); 10982 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10983 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10984 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10985 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10986 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10987 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10988 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10989 verifyFormat("[a, a]() -> a<1> {};"); 10990 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10991 " int j) -> int {\n" 10992 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10993 "};"); 10994 verifyFormat( 10995 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10996 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10997 " return aaaaaaaaaaaaaaaaa;\n" 10998 " });", 10999 getLLVMStyleWithColumns(70)); 11000 11001 // Multiple lambdas in the same parentheses change indentation rules. 11002 verifyFormat("SomeFunction(\n" 11003 " []() {\n" 11004 " int i = 42;\n" 11005 " return i;\n" 11006 " },\n" 11007 " []() {\n" 11008 " int j = 43;\n" 11009 " return j;\n" 11010 " });"); 11011 11012 // More complex introducers. 11013 verifyFormat("return [i, args...] {};"); 11014 11015 // Not lambdas. 11016 verifyFormat("constexpr char hello[]{\"hello\"};"); 11017 verifyFormat("double &operator[](int i) { return 0; }\n" 11018 "int i;"); 11019 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11020 verifyFormat("int i = a[a][a]->f();"); 11021 verifyFormat("int i = (*b)[a]->f();"); 11022 11023 // Other corner cases. 11024 verifyFormat("void f() {\n" 11025 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11026 " );\n" 11027 "}"); 11028 11029 // Lambdas created through weird macros. 11030 verifyFormat("void f() {\n" 11031 " MACRO((const AA &a) { return 1; });\n" 11032 " MACRO((AA &a) { return 1; });\n" 11033 "}"); 11034 11035 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11036 " doo_dah();\n" 11037 " doo_dah();\n" 11038 " })) {\n" 11039 "}"); 11040 verifyFormat("auto lambda = []() {\n" 11041 " int a = 2\n" 11042 "#if A\n" 11043 " + 2\n" 11044 "#endif\n" 11045 " ;\n" 11046 "};"); 11047 } 11048 11049 TEST_F(FormatTest, FormatsBlocks) { 11050 FormatStyle ShortBlocks = getLLVMStyle(); 11051 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11052 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11053 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11054 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11055 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11056 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11057 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11058 11059 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11060 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11061 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11062 11063 verifyFormat("[operation setCompletionBlock:^{\n" 11064 " [self onOperationDone];\n" 11065 "}];"); 11066 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11067 " [self onOperationDone];\n" 11068 "}]};"); 11069 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11070 " f();\n" 11071 "}];"); 11072 verifyFormat("int a = [operation block:^int(int *i) {\n" 11073 " return 1;\n" 11074 "}];"); 11075 verifyFormat("[myObject doSomethingWith:arg1\n" 11076 " aaa:^int(int *a) {\n" 11077 " return 1;\n" 11078 " }\n" 11079 " bbb:f(a * bbbbbbbb)];"); 11080 11081 verifyFormat("[operation setCompletionBlock:^{\n" 11082 " [self.delegate newDataAvailable];\n" 11083 "}];", 11084 getLLVMStyleWithColumns(60)); 11085 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11086 " NSString *path = [self sessionFilePath];\n" 11087 " if (path) {\n" 11088 " // ...\n" 11089 " }\n" 11090 "});"); 11091 verifyFormat("[[SessionService sharedService]\n" 11092 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11093 " if (window) {\n" 11094 " [self windowDidLoad:window];\n" 11095 " } else {\n" 11096 " [self errorLoadingWindow];\n" 11097 " }\n" 11098 " }];"); 11099 verifyFormat("void (^largeBlock)(void) = ^{\n" 11100 " // ...\n" 11101 "};\n", 11102 getLLVMStyleWithColumns(40)); 11103 verifyFormat("[[SessionService sharedService]\n" 11104 " loadWindowWithCompletionBlock: //\n" 11105 " ^(SessionWindow *window) {\n" 11106 " if (window) {\n" 11107 " [self windowDidLoad:window];\n" 11108 " } else {\n" 11109 " [self errorLoadingWindow];\n" 11110 " }\n" 11111 " }];", 11112 getLLVMStyleWithColumns(60)); 11113 verifyFormat("[myObject doSomethingWith:arg1\n" 11114 " firstBlock:^(Foo *a) {\n" 11115 " // ...\n" 11116 " int i;\n" 11117 " }\n" 11118 " secondBlock:^(Bar *b) {\n" 11119 " // ...\n" 11120 " int i;\n" 11121 " }\n" 11122 " thirdBlock:^Foo(Bar *b) {\n" 11123 " // ...\n" 11124 " int i;\n" 11125 " }];"); 11126 verifyFormat("[myObject doSomethingWith:arg1\n" 11127 " firstBlock:-1\n" 11128 " secondBlock:^(Bar *b) {\n" 11129 " // ...\n" 11130 " int i;\n" 11131 " }];"); 11132 11133 verifyFormat("f(^{\n" 11134 " @autoreleasepool {\n" 11135 " if (a) {\n" 11136 " g();\n" 11137 " }\n" 11138 " }\n" 11139 "});"); 11140 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11141 11142 FormatStyle FourIndent = getLLVMStyle(); 11143 FourIndent.ObjCBlockIndentWidth = 4; 11144 verifyFormat("[operation setCompletionBlock:^{\n" 11145 " [self onOperationDone];\n" 11146 "}];", 11147 FourIndent); 11148 } 11149 11150 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11151 FormatStyle ZeroColumn = getLLVMStyle(); 11152 ZeroColumn.ColumnLimit = 0; 11153 11154 verifyFormat("[[SessionService sharedService] " 11155 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11156 " if (window) {\n" 11157 " [self windowDidLoad:window];\n" 11158 " } else {\n" 11159 " [self errorLoadingWindow];\n" 11160 " }\n" 11161 "}];", 11162 ZeroColumn); 11163 EXPECT_EQ("[[SessionService sharedService]\n" 11164 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11165 " if (window) {\n" 11166 " [self windowDidLoad:window];\n" 11167 " } else {\n" 11168 " [self errorLoadingWindow];\n" 11169 " }\n" 11170 " }];", 11171 format("[[SessionService sharedService]\n" 11172 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11173 " if (window) {\n" 11174 " [self windowDidLoad:window];\n" 11175 " } else {\n" 11176 " [self errorLoadingWindow];\n" 11177 " }\n" 11178 "}];", 11179 ZeroColumn)); 11180 verifyFormat("[myObject doSomethingWith:arg1\n" 11181 " firstBlock:^(Foo *a) {\n" 11182 " // ...\n" 11183 " int i;\n" 11184 " }\n" 11185 " secondBlock:^(Bar *b) {\n" 11186 " // ...\n" 11187 " int i;\n" 11188 " }\n" 11189 " thirdBlock:^Foo(Bar *b) {\n" 11190 " // ...\n" 11191 " int i;\n" 11192 " }];", 11193 ZeroColumn); 11194 verifyFormat("f(^{\n" 11195 " @autoreleasepool {\n" 11196 " if (a) {\n" 11197 " g();\n" 11198 " }\n" 11199 " }\n" 11200 "});", 11201 ZeroColumn); 11202 verifyFormat("void (^largeBlock)(void) = ^{\n" 11203 " // ...\n" 11204 "};", 11205 ZeroColumn); 11206 11207 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11208 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11209 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11210 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11211 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11212 " int i;\n" 11213 "};", 11214 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11215 } 11216 11217 TEST_F(FormatTest, SupportsCRLF) { 11218 EXPECT_EQ("int a;\r\n" 11219 "int b;\r\n" 11220 "int c;\r\n", 11221 format("int a;\r\n" 11222 " int b;\r\n" 11223 " int c;\r\n", 11224 getLLVMStyle())); 11225 EXPECT_EQ("int a;\r\n" 11226 "int b;\r\n" 11227 "int c;\r\n", 11228 format("int a;\r\n" 11229 " int b;\n" 11230 " int c;\r\n", 11231 getLLVMStyle())); 11232 EXPECT_EQ("int a;\n" 11233 "int b;\n" 11234 "int c;\n", 11235 format("int a;\r\n" 11236 " int b;\n" 11237 " int c;\n", 11238 getLLVMStyle())); 11239 EXPECT_EQ("\"aaaaaaa \"\r\n" 11240 "\"bbbbbbb\";\r\n", 11241 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11242 EXPECT_EQ("#define A \\\r\n" 11243 " b; \\\r\n" 11244 " c; \\\r\n" 11245 " d;\r\n", 11246 format("#define A \\\r\n" 11247 " b; \\\r\n" 11248 " c; d; \r\n", 11249 getGoogleStyle())); 11250 11251 EXPECT_EQ("/*\r\n" 11252 "multi line block comments\r\n" 11253 "should not introduce\r\n" 11254 "an extra carriage return\r\n" 11255 "*/\r\n", 11256 format("/*\r\n" 11257 "multi line block comments\r\n" 11258 "should not introduce\r\n" 11259 "an extra carriage return\r\n" 11260 "*/\r\n")); 11261 } 11262 11263 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11264 verifyFormat("MY_CLASS(C) {\n" 11265 " int i;\n" 11266 " int j;\n" 11267 "};"); 11268 } 11269 11270 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11271 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11272 TwoIndent.ContinuationIndentWidth = 2; 11273 11274 EXPECT_EQ("int i =\n" 11275 " longFunction(\n" 11276 " arg);", 11277 format("int i = longFunction(arg);", TwoIndent)); 11278 11279 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11280 SixIndent.ContinuationIndentWidth = 6; 11281 11282 EXPECT_EQ("int i =\n" 11283 " longFunction(\n" 11284 " arg);", 11285 format("int i = longFunction(arg);", SixIndent)); 11286 } 11287 11288 TEST_F(FormatTest, SpacesInAngles) { 11289 FormatStyle Spaces = getLLVMStyle(); 11290 Spaces.SpacesInAngles = true; 11291 11292 verifyFormat("static_cast< int >(arg);", Spaces); 11293 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11294 verifyFormat("f< int, float >();", Spaces); 11295 verifyFormat("template <> g() {}", Spaces); 11296 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11297 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11298 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11299 Spaces); 11300 11301 Spaces.Standard = FormatStyle::LS_Cpp03; 11302 Spaces.SpacesInAngles = true; 11303 verifyFormat("A< A< int > >();", Spaces); 11304 11305 Spaces.SpacesInAngles = false; 11306 verifyFormat("A<A<int> >();", Spaces); 11307 11308 Spaces.Standard = FormatStyle::LS_Cpp11; 11309 Spaces.SpacesInAngles = true; 11310 verifyFormat("A< A< int > >();", Spaces); 11311 11312 Spaces.SpacesInAngles = false; 11313 verifyFormat("A<A<int>>();", Spaces); 11314 } 11315 11316 TEST_F(FormatTest, TripleAngleBrackets) { 11317 verifyFormat("f<<<1, 1>>>();"); 11318 verifyFormat("f<<<1, 1, 1, s>>>();"); 11319 verifyFormat("f<<<a, b, c, d>>>();"); 11320 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11321 verifyFormat("f<param><<<1, 1>>>();"); 11322 verifyFormat("f<1><<<1, 1>>>();"); 11323 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11324 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11325 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11326 } 11327 11328 TEST_F(FormatTest, MergeLessLessAtEnd) { 11329 verifyFormat("<<"); 11330 EXPECT_EQ("< < <", format("\\\n<<<")); 11331 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11332 "aaallvm::outs() <<"); 11333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11334 "aaaallvm::outs()\n <<"); 11335 } 11336 11337 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11338 std::string code = "#if A\n" 11339 "#if B\n" 11340 "a.\n" 11341 "#endif\n" 11342 " a = 1;\n" 11343 "#else\n" 11344 "#endif\n" 11345 "#if C\n" 11346 "#else\n" 11347 "#endif\n"; 11348 EXPECT_EQ(code, format(code)); 11349 } 11350 11351 TEST_F(FormatTest, HandleConflictMarkers) { 11352 // Git/SVN conflict markers. 11353 EXPECT_EQ("int a;\n" 11354 "void f() {\n" 11355 " callme(some(parameter1,\n" 11356 "<<<<<<< text by the vcs\n" 11357 " parameter2),\n" 11358 "||||||| text by the vcs\n" 11359 " parameter2),\n" 11360 " parameter3,\n" 11361 "======= text by the vcs\n" 11362 " parameter2, parameter3),\n" 11363 ">>>>>>> text by the vcs\n" 11364 " otherparameter);\n", 11365 format("int a;\n" 11366 "void f() {\n" 11367 " callme(some(parameter1,\n" 11368 "<<<<<<< text by the vcs\n" 11369 " parameter2),\n" 11370 "||||||| text by the vcs\n" 11371 " parameter2),\n" 11372 " parameter3,\n" 11373 "======= text by the vcs\n" 11374 " parameter2,\n" 11375 " parameter3),\n" 11376 ">>>>>>> text by the vcs\n" 11377 " otherparameter);\n")); 11378 11379 // Perforce markers. 11380 EXPECT_EQ("void f() {\n" 11381 " function(\n" 11382 ">>>> text by the vcs\n" 11383 " parameter,\n" 11384 "==== text by the vcs\n" 11385 " parameter,\n" 11386 "==== text by the vcs\n" 11387 " parameter,\n" 11388 "<<<< text by the vcs\n" 11389 " parameter);\n", 11390 format("void f() {\n" 11391 " function(\n" 11392 ">>>> text by the vcs\n" 11393 " parameter,\n" 11394 "==== text by the vcs\n" 11395 " parameter,\n" 11396 "==== text by the vcs\n" 11397 " parameter,\n" 11398 "<<<< text by the vcs\n" 11399 " parameter);\n")); 11400 11401 EXPECT_EQ("<<<<<<<\n" 11402 "|||||||\n" 11403 "=======\n" 11404 ">>>>>>>", 11405 format("<<<<<<<\n" 11406 "|||||||\n" 11407 "=======\n" 11408 ">>>>>>>")); 11409 11410 EXPECT_EQ("<<<<<<<\n" 11411 "|||||||\n" 11412 "int i;\n" 11413 "=======\n" 11414 ">>>>>>>", 11415 format("<<<<<<<\n" 11416 "|||||||\n" 11417 "int i;\n" 11418 "=======\n" 11419 ">>>>>>>")); 11420 11421 // FIXME: Handle parsing of macros around conflict markers correctly: 11422 EXPECT_EQ("#define Macro \\\n" 11423 "<<<<<<<\n" 11424 "Something \\\n" 11425 "|||||||\n" 11426 "Else \\\n" 11427 "=======\n" 11428 "Other \\\n" 11429 ">>>>>>>\n" 11430 " End int i;\n", 11431 format("#define Macro \\\n" 11432 "<<<<<<<\n" 11433 " Something \\\n" 11434 "|||||||\n" 11435 " Else \\\n" 11436 "=======\n" 11437 " Other \\\n" 11438 ">>>>>>>\n" 11439 " End\n" 11440 "int i;\n")); 11441 } 11442 11443 TEST_F(FormatTest, DisableRegions) { 11444 EXPECT_EQ("int i;\n" 11445 "// clang-format off\n" 11446 " int j;\n" 11447 "// clang-format on\n" 11448 "int k;", 11449 format(" int i;\n" 11450 " // clang-format off\n" 11451 " int j;\n" 11452 " // clang-format on\n" 11453 " int k;")); 11454 EXPECT_EQ("int i;\n" 11455 "/* clang-format off */\n" 11456 " int j;\n" 11457 "/* clang-format on */\n" 11458 "int k;", 11459 format(" int i;\n" 11460 " /* clang-format off */\n" 11461 " int j;\n" 11462 " /* clang-format on */\n" 11463 " int k;")); 11464 } 11465 11466 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11467 format("? ) ="); 11468 verifyNoCrash("#define a\\\n /**/}"); 11469 } 11470 11471 TEST_F(FormatTest, FormatsTableGenCode) { 11472 FormatStyle Style = getLLVMStyle(); 11473 Style.Language = FormatStyle::LK_TableGen; 11474 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11475 } 11476 11477 // Since this test case uses UNIX-style file path. We disable it for MS 11478 // compiler. 11479 #if !defined(_MSC_VER) && !defined(__MINGW32__) 11480 11481 TEST(FormatStyle, GetStyleOfFile) { 11482 vfs::InMemoryFileSystem FS; 11483 // Test 1: format file in the same directory. 11484 ASSERT_TRUE( 11485 FS.addFile("/a/.clang-format", 0, 11486 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11487 ASSERT_TRUE( 11488 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11489 auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS); 11490 ASSERT_EQ(Style1, getLLVMStyle()); 11491 11492 // Test 2: fallback to default. 11493 ASSERT_TRUE( 11494 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11495 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS); 11496 ASSERT_EQ(Style2, getMozillaStyle()); 11497 11498 // Test 3: format file in parent directory. 11499 ASSERT_TRUE( 11500 FS.addFile("/c/.clang-format", 0, 11501 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11502 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11503 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11504 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS); 11505 ASSERT_EQ(Style3, getGoogleStyle()); 11506 } 11507 11508 #endif // _MSC_VER 11509 11510 class ReplacementTest : public ::testing::Test { 11511 protected: 11512 tooling::Replacement createReplacement(SourceLocation Start, unsigned Length, 11513 llvm::StringRef ReplacementText) { 11514 return tooling::Replacement(Context.Sources, Start, Length, 11515 ReplacementText); 11516 } 11517 11518 RewriterTestContext Context; 11519 }; 11520 11521 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11522 // Column limit is 20. 11523 std::string Code = "Type *a =\n" 11524 " new Type();\n" 11525 "g(iiiii, 0, jjjjj,\n" 11526 " 0, kkkkk, 0, mm);\n" 11527 "int bad = format ;"; 11528 std::string Expected = "auto a = new Type();\n" 11529 "g(iiiii, nullptr,\n" 11530 " jjjjj, nullptr,\n" 11531 " kkkkk, nullptr,\n" 11532 " mm);\n" 11533 "int bad = format ;"; 11534 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11535 tooling::Replacements Replaces; 11536 Replaces.insert(tooling::Replacement( 11537 Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto ")); 11538 Replaces.insert(tooling::Replacement( 11539 Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr")); 11540 Replaces.insert(tooling::Replacement( 11541 Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr")); 11542 Replaces.insert(tooling::Replacement( 11543 Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr")); 11544 11545 format::FormatStyle Style = format::getLLVMStyle(); 11546 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11547 EXPECT_EQ(Expected, applyAllReplacements( 11548 Code, formatReplacements(Code, Replaces, Style))); 11549 } 11550 11551 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11552 std::string Code = "#include \"a.h\"\n" 11553 "#include \"c.h\"\n" 11554 "\n" 11555 "int main() {\n" 11556 " return 0;\n" 11557 "}"; 11558 std::string Expected = "#include \"a.h\"\n" 11559 "#include \"b.h\"\n" 11560 "#include \"c.h\"\n" 11561 "\n" 11562 "int main() {\n" 11563 " return 0;\n" 11564 "}"; 11565 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11566 tooling::Replacements Replaces; 11567 Replaces.insert(tooling::Replacement( 11568 Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n")); 11569 11570 format::FormatStyle Style = format::getLLVMStyle(); 11571 Style.SortIncludes = true; 11572 auto FinalReplaces = formatReplacements(Code, Replaces, Style); 11573 EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces)); 11574 } 11575 11576 } // end namespace 11577 } // end namespace format 11578 } // end namespace clang 11579