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