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.AllowShortIfStatementsOnASingleLine = true; 300 verifyFormat("if (a)\n" 301 " // comment\n" 302 " f();", 303 AllowsMergedIf); 304 verifyFormat("if (a)\n" 305 " ;", 306 AllowsMergedIf); 307 verifyFormat("if (a)\n" 308 " if (b) return;", 309 AllowsMergedIf); 310 311 verifyFormat("if (a) // Can't merge this\n" 312 " f();\n", 313 AllowsMergedIf); 314 verifyFormat("if (a) /* still don't merge */\n" 315 " f();", 316 AllowsMergedIf); 317 verifyFormat("if (a) { // Never merge this\n" 318 " f();\n" 319 "}", 320 AllowsMergedIf); 321 verifyFormat("if (a) { /* Never merge this */\n" 322 " f();\n" 323 "}", 324 AllowsMergedIf); 325 326 AllowsMergedIf.ColumnLimit = 14; 327 verifyFormat("if (a) return;", AllowsMergedIf); 328 verifyFormat("if (aaaaaaaaa)\n" 329 " return;", 330 AllowsMergedIf); 331 332 AllowsMergedIf.ColumnLimit = 13; 333 verifyFormat("if (a)\n return;", AllowsMergedIf); 334 } 335 336 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 337 FormatStyle AllowsMergedLoops = getLLVMStyle(); 338 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 339 verifyFormat("while (true) continue;", AllowsMergedLoops); 340 verifyFormat("for (;;) continue;", AllowsMergedLoops); 341 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 342 verifyFormat("while (true)\n" 343 " ;", 344 AllowsMergedLoops); 345 verifyFormat("for (;;)\n" 346 " ;", 347 AllowsMergedLoops); 348 verifyFormat("for (;;)\n" 349 " for (;;) continue;", 350 AllowsMergedLoops); 351 verifyFormat("for (;;) // Can't merge this\n" 352 " continue;", 353 AllowsMergedLoops); 354 verifyFormat("for (;;) /* still don't merge */\n" 355 " continue;", 356 AllowsMergedLoops); 357 } 358 359 TEST_F(FormatTest, FormatShortBracedStatements) { 360 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 361 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 362 363 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 364 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 365 366 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 367 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 368 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 369 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 370 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 371 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 372 verifyFormat("if (true) { //\n" 373 " f();\n" 374 "}", 375 AllowSimpleBracedStatements); 376 verifyFormat("if (true) {\n" 377 " f();\n" 378 " f();\n" 379 "}", 380 AllowSimpleBracedStatements); 381 verifyFormat("if (true) {\n" 382 " f();\n" 383 "} else {\n" 384 " f();\n" 385 "}", 386 AllowSimpleBracedStatements); 387 388 verifyFormat("template <int> struct A2 {\n" 389 " struct B {};\n" 390 "};", 391 AllowSimpleBracedStatements); 392 393 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 394 verifyFormat("if (true) {\n" 395 " f();\n" 396 "}", 397 AllowSimpleBracedStatements); 398 verifyFormat("if (true) {\n" 399 " f();\n" 400 "} else {\n" 401 " f();\n" 402 "}", 403 AllowSimpleBracedStatements); 404 405 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 406 verifyFormat("while (true) {\n" 407 " f();\n" 408 "}", 409 AllowSimpleBracedStatements); 410 verifyFormat("for (;;) {\n" 411 " f();\n" 412 "}", 413 AllowSimpleBracedStatements); 414 } 415 416 TEST_F(FormatTest, ParseIfElse) { 417 verifyFormat("if (true)\n" 418 " if (true)\n" 419 " if (true)\n" 420 " f();\n" 421 " else\n" 422 " g();\n" 423 " else\n" 424 " h();\n" 425 "else\n" 426 " i();"); 427 verifyFormat("if (true)\n" 428 " if (true)\n" 429 " if (true) {\n" 430 " if (true)\n" 431 " f();\n" 432 " } else {\n" 433 " g();\n" 434 " }\n" 435 " else\n" 436 " h();\n" 437 "else {\n" 438 " i();\n" 439 "}"); 440 verifyFormat("void f() {\n" 441 " if (a) {\n" 442 " } else {\n" 443 " }\n" 444 "}"); 445 } 446 447 TEST_F(FormatTest, ElseIf) { 448 verifyFormat("if (a) {\n} else if (b) {\n}"); 449 verifyFormat("if (a)\n" 450 " f();\n" 451 "else if (b)\n" 452 " g();\n" 453 "else\n" 454 " h();"); 455 verifyFormat("if (a) {\n" 456 " f();\n" 457 "}\n" 458 "// or else ..\n" 459 "else {\n" 460 " g()\n" 461 "}"); 462 463 verifyFormat("if (a) {\n" 464 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 466 "}"); 467 verifyFormat("if (a) {\n" 468 "} else if (\n" 469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 470 "}", 471 getLLVMStyleWithColumns(62)); 472 } 473 474 TEST_F(FormatTest, FormatsForLoop) { 475 verifyFormat( 476 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 477 " ++VeryVeryLongLoopVariable)\n" 478 " ;"); 479 verifyFormat("for (;;)\n" 480 " f();"); 481 verifyFormat("for (;;) {\n}"); 482 verifyFormat("for (;;) {\n" 483 " f();\n" 484 "}"); 485 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 486 487 verifyFormat( 488 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 489 " E = UnwrappedLines.end();\n" 490 " I != E; ++I) {\n}"); 491 492 verifyFormat( 493 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 494 " ++IIIII) {\n}"); 495 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 496 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 497 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 498 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 499 " I = FD->getDeclsInPrototypeScope().begin(),\n" 500 " E = FD->getDeclsInPrototypeScope().end();\n" 501 " I != E; ++I) {\n}"); 502 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 503 " I = Container.begin(),\n" 504 " E = Container.end();\n" 505 " I != E; ++I) {\n}", 506 getLLVMStyleWithColumns(76)); 507 508 verifyFormat( 509 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 511 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 513 " ++aaaaaaaaaaa) {\n}"); 514 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 515 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 516 " ++i) {\n}"); 517 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 518 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 519 "}"); 520 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 521 " aaaaaaaaaa);\n" 522 " iter; ++iter) {\n" 523 "}"); 524 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 526 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 527 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 528 529 FormatStyle NoBinPacking = getLLVMStyle(); 530 NoBinPacking.BinPackParameters = false; 531 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 532 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 533 " aaaaaaaaaaaaaaaa,\n" 534 " aaaaaaaaaaaaaaaa,\n" 535 " aaaaaaaaaaaaaaaa);\n" 536 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 537 "}", 538 NoBinPacking); 539 verifyFormat( 540 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 541 " E = UnwrappedLines.end();\n" 542 " I != E;\n" 543 " ++I) {\n}", 544 NoBinPacking); 545 } 546 547 TEST_F(FormatTest, RangeBasedForLoops) { 548 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 550 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 551 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 552 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 554 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 555 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 556 } 557 558 TEST_F(FormatTest, ForEachLoops) { 559 verifyFormat("void f() {\n" 560 " foreach (Item *item, itemlist) {}\n" 561 " Q_FOREACH (Item *item, itemlist) {}\n" 562 " BOOST_FOREACH (Item *item, itemlist) {}\n" 563 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 564 "}"); 565 566 // As function-like macros. 567 verifyFormat("#define foreach(x, y)\n" 568 "#define Q_FOREACH(x, y)\n" 569 "#define BOOST_FOREACH(x, y)\n" 570 "#define UNKNOWN_FOREACH(x, y)\n"); 571 572 // Not as function-like macros. 573 verifyFormat("#define foreach (x, y)\n" 574 "#define Q_FOREACH (x, y)\n" 575 "#define BOOST_FOREACH (x, y)\n" 576 "#define UNKNOWN_FOREACH (x, y)\n"); 577 } 578 579 TEST_F(FormatTest, FormatsWhileLoop) { 580 verifyFormat("while (true) {\n}"); 581 verifyFormat("while (true)\n" 582 " f();"); 583 verifyFormat("while () {\n}"); 584 verifyFormat("while () {\n" 585 " f();\n" 586 "}"); 587 } 588 589 TEST_F(FormatTest, FormatsDoWhile) { 590 verifyFormat("do {\n" 591 " do_something();\n" 592 "} while (something());"); 593 verifyFormat("do\n" 594 " do_something();\n" 595 "while (something());"); 596 } 597 598 TEST_F(FormatTest, FormatsSwitchStatement) { 599 verifyFormat("switch (x) {\n" 600 "case 1:\n" 601 " f();\n" 602 " break;\n" 603 "case kFoo:\n" 604 "case ns::kBar:\n" 605 "case kBaz:\n" 606 " break;\n" 607 "default:\n" 608 " g();\n" 609 " break;\n" 610 "}"); 611 verifyFormat("switch (x) {\n" 612 "case 1: {\n" 613 " f();\n" 614 " break;\n" 615 "}\n" 616 "case 2: {\n" 617 " break;\n" 618 "}\n" 619 "}"); 620 verifyFormat("switch (x) {\n" 621 "case 1: {\n" 622 " f();\n" 623 " {\n" 624 " g();\n" 625 " h();\n" 626 " }\n" 627 " break;\n" 628 "}\n" 629 "}"); 630 verifyFormat("switch (x) {\n" 631 "case 1: {\n" 632 " f();\n" 633 " if (foo) {\n" 634 " g();\n" 635 " h();\n" 636 " }\n" 637 " break;\n" 638 "}\n" 639 "}"); 640 verifyFormat("switch (x) {\n" 641 "case 1: {\n" 642 " f();\n" 643 " g();\n" 644 "} break;\n" 645 "}"); 646 verifyFormat("switch (test)\n" 647 " ;"); 648 verifyFormat("switch (x) {\n" 649 "default: {\n" 650 " // Do nothing.\n" 651 "}\n" 652 "}"); 653 verifyFormat("switch (x) {\n" 654 "// comment\n" 655 "// if 1, do f()\n" 656 "case 1:\n" 657 " f();\n" 658 "}"); 659 verifyFormat("switch (x) {\n" 660 "case 1:\n" 661 " // Do amazing stuff\n" 662 " {\n" 663 " f();\n" 664 " g();\n" 665 " }\n" 666 " break;\n" 667 "}"); 668 verifyFormat("#define A \\\n" 669 " switch (x) { \\\n" 670 " case a: \\\n" 671 " foo = b; \\\n" 672 " }", 673 getLLVMStyleWithColumns(20)); 674 verifyFormat("#define OPERATION_CASE(name) \\\n" 675 " case OP_name: \\\n" 676 " return operations::Operation##name\n", 677 getLLVMStyleWithColumns(40)); 678 verifyFormat("switch (x) {\n" 679 "case 1:;\n" 680 "default:;\n" 681 " int i;\n" 682 "}"); 683 684 verifyGoogleFormat("switch (x) {\n" 685 " case 1:\n" 686 " f();\n" 687 " break;\n" 688 " case kFoo:\n" 689 " case ns::kBar:\n" 690 " case kBaz:\n" 691 " break;\n" 692 " default:\n" 693 " g();\n" 694 " break;\n" 695 "}"); 696 verifyGoogleFormat("switch (x) {\n" 697 " case 1: {\n" 698 " f();\n" 699 " break;\n" 700 " }\n" 701 "}"); 702 verifyGoogleFormat("switch (test)\n" 703 " ;"); 704 705 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 706 " case OP_name: \\\n" 707 " return operations::Operation##name\n"); 708 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 709 " // Get the correction operation class.\n" 710 " switch (OpCode) {\n" 711 " CASE(Add);\n" 712 " CASE(Subtract);\n" 713 " default:\n" 714 " return operations::Unknown;\n" 715 " }\n" 716 "#undef OPERATION_CASE\n" 717 "}"); 718 verifyFormat("DEBUG({\n" 719 " switch (x) {\n" 720 " case A:\n" 721 " f();\n" 722 " break;\n" 723 " // On B:\n" 724 " case B:\n" 725 " g();\n" 726 " break;\n" 727 " }\n" 728 "});"); 729 verifyFormat("switch (a) {\n" 730 "case (b):\n" 731 " return;\n" 732 "}"); 733 734 verifyFormat("switch (a) {\n" 735 "case some_namespace::\n" 736 " some_constant:\n" 737 " return;\n" 738 "}", 739 getLLVMStyleWithColumns(34)); 740 } 741 742 TEST_F(FormatTest, CaseRanges) { 743 verifyFormat("switch (x) {\n" 744 "case 'A' ... 'Z':\n" 745 "case 1 ... 5:\n" 746 " break;\n" 747 "}"); 748 } 749 750 TEST_F(FormatTest, ShortCaseLabels) { 751 FormatStyle Style = getLLVMStyle(); 752 Style.AllowShortCaseLabelsOnASingleLine = true; 753 verifyFormat("switch (a) {\n" 754 "case 1: x = 1; break;\n" 755 "case 2: return;\n" 756 "case 3:\n" 757 "case 4:\n" 758 "case 5: return;\n" 759 "case 6: // comment\n" 760 " return;\n" 761 "case 7:\n" 762 " // comment\n" 763 " return;\n" 764 "case 8:\n" 765 " x = 8; // comment\n" 766 " break;\n" 767 "default: y = 1; break;\n" 768 "}", 769 Style); 770 verifyFormat("switch (a) {\n" 771 "#if FOO\n" 772 "case 0: return 0;\n" 773 "#endif\n" 774 "}", 775 Style); 776 verifyFormat("switch (a) {\n" 777 "case 1: {\n" 778 "}\n" 779 "case 2: {\n" 780 " return;\n" 781 "}\n" 782 "case 3: {\n" 783 " x = 1;\n" 784 " return;\n" 785 "}\n" 786 "case 4:\n" 787 " if (x)\n" 788 " return;\n" 789 "}", 790 Style); 791 Style.ColumnLimit = 21; 792 verifyFormat("switch (a) {\n" 793 "case 1: x = 1; break;\n" 794 "case 2: return;\n" 795 "case 3:\n" 796 "case 4:\n" 797 "case 5: return;\n" 798 "default:\n" 799 " y = 1;\n" 800 " break;\n" 801 "}", 802 Style); 803 } 804 805 TEST_F(FormatTest, FormatsLabels) { 806 verifyFormat("void f() {\n" 807 " some_code();\n" 808 "test_label:\n" 809 " some_other_code();\n" 810 " {\n" 811 " some_more_code();\n" 812 " another_label:\n" 813 " some_more_code();\n" 814 " }\n" 815 "}"); 816 verifyFormat("{\n" 817 " some_code();\n" 818 "test_label:\n" 819 " some_other_code();\n" 820 "}"); 821 verifyFormat("{\n" 822 " some_code();\n" 823 "test_label:;\n" 824 " int i = 0;\n" 825 "}"); 826 } 827 828 //===----------------------------------------------------------------------===// 829 // Tests for comments. 830 //===----------------------------------------------------------------------===// 831 832 TEST_F(FormatTest, UnderstandsSingleLineComments) { 833 verifyFormat("//* */"); 834 verifyFormat("// line 1\n" 835 "// line 2\n" 836 "void f() {}\n"); 837 838 verifyFormat("void f() {\n" 839 " // Doesn't do anything\n" 840 "}"); 841 verifyFormat("SomeObject\n" 842 " // Calling someFunction on SomeObject\n" 843 " .someFunction();"); 844 verifyFormat("auto result = SomeObject\n" 845 " // Calling someFunction on SomeObject\n" 846 " .someFunction();"); 847 verifyFormat("void f(int i, // some comment (probably for i)\n" 848 " int j, // some comment (probably for j)\n" 849 " int k); // some comment (probably for k)"); 850 verifyFormat("void f(int i,\n" 851 " // some comment (probably for j)\n" 852 " int j,\n" 853 " // some comment (probably for k)\n" 854 " int k);"); 855 856 verifyFormat("int i // This is a fancy variable\n" 857 " = 5; // with nicely aligned comment."); 858 859 verifyFormat("// Leading comment.\n" 860 "int a; // Trailing comment."); 861 verifyFormat("int a; // Trailing comment\n" 862 " // on 2\n" 863 " // or 3 lines.\n" 864 "int b;"); 865 verifyFormat("int a; // Trailing comment\n" 866 "\n" 867 "// Leading comment.\n" 868 "int b;"); 869 verifyFormat("int a; // Comment.\n" 870 " // More details.\n" 871 "int bbbb; // Another comment."); 872 verifyFormat( 873 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 874 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 875 "int cccccccccccccccccccccccccccccc; // comment\n" 876 "int ddd; // looooooooooooooooooooooooong comment\n" 877 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 878 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 879 "int ccccccccccccccccccc; // comment"); 880 881 verifyFormat("#include \"a\" // comment\n" 882 "#include \"a/b/c\" // comment"); 883 verifyFormat("#include <a> // comment\n" 884 "#include <a/b/c> // comment"); 885 EXPECT_EQ("#include \"a\" // comment\n" 886 "#include \"a/b/c\" // comment", 887 format("#include \\\n" 888 " \"a\" // comment\n" 889 "#include \"a/b/c\" // comment")); 890 891 verifyFormat("enum E {\n" 892 " // comment\n" 893 " VAL_A, // comment\n" 894 " VAL_B\n" 895 "};"); 896 897 verifyFormat( 898 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 899 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 900 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 901 " // Comment inside a statement.\n" 902 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 903 verifyFormat("SomeFunction(a,\n" 904 " // comment\n" 905 " b + x);"); 906 verifyFormat("SomeFunction(a, a,\n" 907 " // comment\n" 908 " b + x);"); 909 verifyFormat( 910 "bool aaaaaaaaaaaaa = // comment\n" 911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 913 914 verifyFormat("int aaaa; // aaaaa\n" 915 "int aa; // aaaaaaa", 916 getLLVMStyleWithColumns(20)); 917 918 EXPECT_EQ("void f() { // This does something ..\n" 919 "}\n" 920 "int a; // This is unrelated", 921 format("void f() { // This does something ..\n" 922 " }\n" 923 "int a; // This is unrelated")); 924 EXPECT_EQ("class C {\n" 925 " void f() { // This does something ..\n" 926 " } // awesome..\n" 927 "\n" 928 " int a; // This is unrelated\n" 929 "};", 930 format("class C{void f() { // This does something ..\n" 931 " } // awesome..\n" 932 " \n" 933 "int a; // This is unrelated\n" 934 "};")); 935 936 EXPECT_EQ("int i; // single line trailing comment", 937 format("int i;\\\n// single line trailing comment")); 938 939 verifyGoogleFormat("int a; // Trailing comment."); 940 941 verifyFormat("someFunction(anotherFunction( // Force break.\n" 942 " parameter));"); 943 944 verifyGoogleFormat("#endif // HEADER_GUARD"); 945 946 verifyFormat("const char *test[] = {\n" 947 " // A\n" 948 " \"aaaa\",\n" 949 " // B\n" 950 " \"aaaaa\"};"); 951 verifyGoogleFormat( 952 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 953 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 954 EXPECT_EQ("D(a, {\n" 955 " // test\n" 956 " int a;\n" 957 "});", 958 format("D(a, {\n" 959 "// test\n" 960 "int a;\n" 961 "});")); 962 963 EXPECT_EQ("lineWith(); // comment\n" 964 "// at start\n" 965 "otherLine();", 966 format("lineWith(); // comment\n" 967 "// at start\n" 968 "otherLine();")); 969 EXPECT_EQ("lineWith(); // comment\n" 970 "/*\n" 971 " * at start */\n" 972 "otherLine();", 973 format("lineWith(); // comment\n" 974 "/*\n" 975 " * at start */\n" 976 "otherLine();")); 977 EXPECT_EQ("lineWith(); // comment\n" 978 " // at start\n" 979 "otherLine();", 980 format("lineWith(); // comment\n" 981 " // at start\n" 982 "otherLine();")); 983 984 EXPECT_EQ("lineWith(); // comment\n" 985 "// at start\n" 986 "otherLine(); // comment", 987 format("lineWith(); // comment\n" 988 "// at start\n" 989 "otherLine(); // comment")); 990 EXPECT_EQ("lineWith();\n" 991 "// at start\n" 992 "otherLine(); // comment", 993 format("lineWith();\n" 994 " // at start\n" 995 "otherLine(); // comment")); 996 EXPECT_EQ("// first\n" 997 "// at start\n" 998 "otherLine(); // comment", 999 format("// first\n" 1000 " // at start\n" 1001 "otherLine(); // comment")); 1002 EXPECT_EQ("f();\n" 1003 "// first\n" 1004 "// at start\n" 1005 "otherLine(); // comment", 1006 format("f();\n" 1007 "// first\n" 1008 " // at start\n" 1009 "otherLine(); // comment")); 1010 verifyFormat("f(); // comment\n" 1011 "// first\n" 1012 "// at start\n" 1013 "otherLine();"); 1014 EXPECT_EQ("f(); // comment\n" 1015 "// first\n" 1016 "// at start\n" 1017 "otherLine();", 1018 format("f(); // comment\n" 1019 "// first\n" 1020 " // at start\n" 1021 "otherLine();")); 1022 EXPECT_EQ("f(); // comment\n" 1023 " // first\n" 1024 "// at start\n" 1025 "otherLine();", 1026 format("f(); // comment\n" 1027 " // first\n" 1028 "// at start\n" 1029 "otherLine();")); 1030 EXPECT_EQ("void f() {\n" 1031 " lineWith(); // comment\n" 1032 " // at start\n" 1033 "}", 1034 format("void f() {\n" 1035 " lineWith(); // comment\n" 1036 " // at start\n" 1037 "}")); 1038 EXPECT_EQ("int xy; // a\n" 1039 "int z; // b", 1040 format("int xy; // a\n" 1041 "int z; //b")); 1042 EXPECT_EQ("int xy; // a\n" 1043 "int z; // bb", 1044 format("int xy; // a\n" 1045 "int z; //bb", 1046 getLLVMStyleWithColumns(12))); 1047 1048 verifyFormat("#define A \\\n" 1049 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1050 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1051 getLLVMStyleWithColumns(60)); 1052 verifyFormat( 1053 "#define A \\\n" 1054 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1055 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1056 getLLVMStyleWithColumns(61)); 1057 1058 verifyFormat("if ( // This is some comment\n" 1059 " x + 3) {\n" 1060 "}"); 1061 EXPECT_EQ("if ( // This is some comment\n" 1062 " // spanning two lines\n" 1063 " x + 3) {\n" 1064 "}", 1065 format("if( // This is some comment\n" 1066 " // spanning two lines\n" 1067 " x + 3) {\n" 1068 "}")); 1069 1070 verifyNoCrash("/\\\n/"); 1071 verifyNoCrash("/\\\n* */"); 1072 // The 0-character somehow makes the lexer return a proper comment. 1073 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1074 } 1075 1076 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1077 EXPECT_EQ("SomeFunction(a,\n" 1078 " b, // comment\n" 1079 " c);", 1080 format("SomeFunction(a,\n" 1081 " b, // comment\n" 1082 " c);")); 1083 EXPECT_EQ("SomeFunction(a, b,\n" 1084 " // comment\n" 1085 " c);", 1086 format("SomeFunction(a,\n" 1087 " b,\n" 1088 " // comment\n" 1089 " c);")); 1090 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1091 " c);", 1092 format("SomeFunction(a, b, // comment (unclear relation)\n" 1093 " c);")); 1094 EXPECT_EQ("SomeFunction(a, // comment\n" 1095 " b,\n" 1096 " c); // comment", 1097 format("SomeFunction(a, // comment\n" 1098 " b,\n" 1099 " c); // comment")); 1100 } 1101 1102 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1103 EXPECT_EQ("// comment", format("// comment ")); 1104 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1105 format("int aaaaaaa, bbbbbbb; // comment ", 1106 getLLVMStyleWithColumns(33))); 1107 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1108 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1109 } 1110 1111 TEST_F(FormatTest, UnderstandsBlockComments) { 1112 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1113 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1114 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1115 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1116 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1117 "/* Trailing comment for aa... */\n" 1118 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1119 EXPECT_EQ( 1120 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1121 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1122 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1123 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1124 EXPECT_EQ( 1125 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1126 " aaaaaaaaaaaaaaaaaa,\n" 1127 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1128 "}", 1129 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1130 " aaaaaaaaaaaaaaaaaa ,\n" 1131 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1132 "}")); 1133 1134 FormatStyle NoBinPacking = getLLVMStyle(); 1135 NoBinPacking.BinPackParameters = false; 1136 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1137 " /* parameter 2 */ aaaaaa,\n" 1138 " /* parameter 3 */ aaaaaa,\n" 1139 " /* parameter 4 */ aaaaaa);", 1140 NoBinPacking); 1141 1142 // Aligning block comments in macros. 1143 verifyGoogleFormat("#define A \\\n" 1144 " int i; /*a*/ \\\n" 1145 " int jjj; /*b*/"); 1146 } 1147 1148 TEST_F(FormatTest, AlignsBlockComments) { 1149 EXPECT_EQ("/*\n" 1150 " * Really multi-line\n" 1151 " * comment.\n" 1152 " */\n" 1153 "void f() {}", 1154 format(" /*\n" 1155 " * Really multi-line\n" 1156 " * comment.\n" 1157 " */\n" 1158 " void f() {}")); 1159 EXPECT_EQ("class C {\n" 1160 " /*\n" 1161 " * Another multi-line\n" 1162 " * comment.\n" 1163 " */\n" 1164 " void f() {}\n" 1165 "};", 1166 format("class C {\n" 1167 "/*\n" 1168 " * Another multi-line\n" 1169 " * comment.\n" 1170 " */\n" 1171 "void f() {}\n" 1172 "};")); 1173 EXPECT_EQ("/*\n" 1174 " 1. This is a comment with non-trivial formatting.\n" 1175 " 1.1. We have to indent/outdent all lines equally\n" 1176 " 1.1.1. to keep the formatting.\n" 1177 " */", 1178 format(" /*\n" 1179 " 1. This is a comment with non-trivial formatting.\n" 1180 " 1.1. We have to indent/outdent all lines equally\n" 1181 " 1.1.1. to keep the formatting.\n" 1182 " */")); 1183 EXPECT_EQ("/*\n" 1184 "Don't try to outdent if there's not enough indentation.\n" 1185 "*/", 1186 format(" /*\n" 1187 " Don't try to outdent if there's not enough indentation.\n" 1188 " */")); 1189 1190 EXPECT_EQ("int i; /* Comment with empty...\n" 1191 " *\n" 1192 " * line. */", 1193 format("int i; /* Comment with empty...\n" 1194 " *\n" 1195 " * line. */")); 1196 EXPECT_EQ("int foobar = 0; /* comment */\n" 1197 "int bar = 0; /* multiline\n" 1198 " comment 1 */\n" 1199 "int baz = 0; /* multiline\n" 1200 " comment 2 */\n" 1201 "int bzz = 0; /* multiline\n" 1202 " comment 3 */", 1203 format("int foobar = 0; /* comment */\n" 1204 "int bar = 0; /* multiline\n" 1205 " comment 1 */\n" 1206 "int baz = 0; /* multiline\n" 1207 " comment 2 */\n" 1208 "int bzz = 0; /* multiline\n" 1209 " comment 3 */")); 1210 EXPECT_EQ("int foobar = 0; /* comment */\n" 1211 "int bar = 0; /* multiline\n" 1212 " comment */\n" 1213 "int baz = 0; /* multiline\n" 1214 "comment */", 1215 format("int foobar = 0; /* comment */\n" 1216 "int bar = 0; /* multiline\n" 1217 "comment */\n" 1218 "int baz = 0; /* multiline\n" 1219 "comment */")); 1220 } 1221 1222 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1223 FormatStyle Style = getLLVMStyleWithColumns(20); 1224 Style.ReflowComments = false; 1225 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1226 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1227 } 1228 1229 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1230 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1232 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1234 EXPECT_EQ( 1235 "void ffffffffffff(\n" 1236 " int aaaaaaaa, int bbbbbbbb,\n" 1237 " int cccccccccccc) { /*\n" 1238 " aaaaaaaaaa\n" 1239 " aaaaaaaaaaaaa\n" 1240 " bbbbbbbbbbbbbb\n" 1241 " bbbbbbbbbb\n" 1242 " */\n" 1243 "}", 1244 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1245 "{ /*\n" 1246 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1247 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1248 " */\n" 1249 "}", 1250 getLLVMStyleWithColumns(40))); 1251 } 1252 1253 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1254 EXPECT_EQ("void ffffffffff(\n" 1255 " int aaaaa /* test */);", 1256 format("void ffffffffff(int aaaaa /* test */);", 1257 getLLVMStyleWithColumns(35))); 1258 } 1259 1260 TEST_F(FormatTest, SplitsLongCxxComments) { 1261 EXPECT_EQ("// A comment that\n" 1262 "// doesn't fit on\n" 1263 "// one line", 1264 format("// A comment that doesn't fit on one line", 1265 getLLVMStyleWithColumns(20))); 1266 EXPECT_EQ("/// A comment that\n" 1267 "/// doesn't fit on\n" 1268 "/// one line", 1269 format("/// A comment that doesn't fit on one line", 1270 getLLVMStyleWithColumns(20))); 1271 EXPECT_EQ("//! A comment that\n" 1272 "//! doesn't fit on\n" 1273 "//! one line", 1274 format("//! A comment that doesn't fit on one line", 1275 getLLVMStyleWithColumns(20))); 1276 EXPECT_EQ("// a b c d\n" 1277 "// e f g\n" 1278 "// h i j k", 1279 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1280 EXPECT_EQ( 1281 "// a b c d\n" 1282 "// e f g\n" 1283 "// h i j k", 1284 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1285 EXPECT_EQ("if (true) // A comment that\n" 1286 " // doesn't fit on\n" 1287 " // one line", 1288 format("if (true) // A comment that doesn't fit on one line ", 1289 getLLVMStyleWithColumns(30))); 1290 EXPECT_EQ("// Don't_touch_leading_whitespace", 1291 format("// Don't_touch_leading_whitespace", 1292 getLLVMStyleWithColumns(20))); 1293 EXPECT_EQ("// Add leading\n" 1294 "// whitespace", 1295 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1296 EXPECT_EQ("/// Add leading\n" 1297 "/// whitespace", 1298 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1299 EXPECT_EQ("//! Add leading\n" 1300 "//! whitespace", 1301 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1302 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1303 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1304 "// limit", 1305 format("//Even if it makes the line exceed the column limit", 1306 getLLVMStyleWithColumns(51))); 1307 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1308 1309 EXPECT_EQ("// aa bb cc dd", 1310 format("// aa bb cc dd ", 1311 getLLVMStyleWithColumns(15))); 1312 1313 EXPECT_EQ("// A comment before\n" 1314 "// a macro\n" 1315 "// definition\n" 1316 "#define a b", 1317 format("// A comment before a macro definition\n" 1318 "#define a b", 1319 getLLVMStyleWithColumns(20))); 1320 EXPECT_EQ("void ffffff(\n" 1321 " int aaaaaaaaa, // wwww\n" 1322 " int bbbbbbbbbb, // xxxxxxx\n" 1323 " // yyyyyyyyyy\n" 1324 " int c, int d, int e) {}", 1325 format("void ffffff(\n" 1326 " int aaaaaaaaa, // wwww\n" 1327 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1328 " int c, int d, int e) {}", 1329 getLLVMStyleWithColumns(40))); 1330 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1331 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1332 getLLVMStyleWithColumns(20))); 1333 EXPECT_EQ( 1334 "#define XXX // a b c d\n" 1335 " // e f g h", 1336 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1337 EXPECT_EQ( 1338 "#define XXX // q w e r\n" 1339 " // t y u i", 1340 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1341 } 1342 1343 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1344 EXPECT_EQ("// A comment\n" 1345 "// that doesn't\n" 1346 "// fit on one\n" 1347 "// line", 1348 format("// A comment that doesn't fit on one line", 1349 getLLVMStyleWithColumns(20))); 1350 EXPECT_EQ("/// A comment\n" 1351 "/// that doesn't\n" 1352 "/// fit on one\n" 1353 "/// line", 1354 format("/// A comment that doesn't fit on one line", 1355 getLLVMStyleWithColumns(20))); 1356 } 1357 1358 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1359 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1360 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1361 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1362 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1363 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1364 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1365 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1366 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1367 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1368 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1369 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1370 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1371 getLLVMStyleWithColumns(50))); 1372 // FIXME: One day we might want to implement adjustment of leading whitespace 1373 // of the consecutive lines in this kind of comment: 1374 EXPECT_EQ("double\n" 1375 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1376 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1377 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1378 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1379 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1380 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1381 getLLVMStyleWithColumns(49))); 1382 } 1383 1384 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1385 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1386 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1387 EXPECT_EQ( 1388 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1389 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1390 EXPECT_EQ( 1391 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1392 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1393 } 1394 1395 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1396 EXPECT_EQ("if (xxx ==\n" 1397 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1398 " zzz)\n" 1399 " q();", 1400 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1401 " zzz) q();", 1402 getLLVMStyleWithColumns(40))); 1403 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1404 " yyy && // aaaaaa bbbbbbbb cccc\n" 1405 " zzz)\n" 1406 " q();", 1407 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1408 " zzz) q();", 1409 getLLVMStyleWithColumns(40))); 1410 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1411 " yyy || // aaaaaa bbbbbbbb cccc\n" 1412 " zzz)\n" 1413 " q();", 1414 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1415 " zzz) q();", 1416 getLLVMStyleWithColumns(40))); 1417 EXPECT_EQ("fffffffff(\n" 1418 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1419 " zzz);", 1420 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1421 " zzz);", 1422 getLLVMStyleWithColumns(40))); 1423 } 1424 1425 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1426 EXPECT_EQ("#define A(x) /* \\\n" 1427 " a comment \\\n" 1428 " inside */ \\\n" 1429 " f();", 1430 format("#define A(x) /* \\\n" 1431 " a comment \\\n" 1432 " inside */ \\\n" 1433 " f();", 1434 getLLVMStyleWithColumns(17))); 1435 EXPECT_EQ("#define A( \\\n" 1436 " x) /* \\\n" 1437 " a comment \\\n" 1438 " inside */ \\\n" 1439 " f();", 1440 format("#define A( \\\n" 1441 " x) /* \\\n" 1442 " a comment \\\n" 1443 " inside */ \\\n" 1444 " f();", 1445 getLLVMStyleWithColumns(17))); 1446 } 1447 1448 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1449 EXPECT_EQ("namespace {}\n// Test\n#define A", 1450 format("namespace {}\n // Test\n#define A")); 1451 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1452 format("namespace {}\n /* Test */\n#define A")); 1453 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1454 format("namespace {}\n /* Test */ #define A")); 1455 } 1456 1457 TEST_F(FormatTest, SplitsLongLinesInComments) { 1458 EXPECT_EQ("/* This is a long\n" 1459 " * comment that\n" 1460 " * doesn't\n" 1461 " * fit on one line.\n" 1462 " */", 1463 format("/* " 1464 "This is a long " 1465 "comment that " 1466 "doesn't " 1467 "fit on one line. */", 1468 getLLVMStyleWithColumns(20))); 1469 EXPECT_EQ( 1470 "/* a b c d\n" 1471 " * e f g\n" 1472 " * h i j k\n" 1473 " */", 1474 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1475 EXPECT_EQ( 1476 "/* a b c d\n" 1477 " * e f g\n" 1478 " * h i j k\n" 1479 " */", 1480 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1481 EXPECT_EQ("/*\n" 1482 "This is a long\n" 1483 "comment that doesn't\n" 1484 "fit on one line.\n" 1485 "*/", 1486 format("/*\n" 1487 "This is a long " 1488 "comment that doesn't " 1489 "fit on one line. \n" 1490 "*/", 1491 getLLVMStyleWithColumns(20))); 1492 EXPECT_EQ("/*\n" 1493 " * This is a long\n" 1494 " * comment that\n" 1495 " * doesn't fit on\n" 1496 " * one line.\n" 1497 " */", 1498 format("/* \n" 1499 " * This is a long " 1500 " comment that " 1501 " doesn't fit on " 1502 " one line. \n" 1503 " */", 1504 getLLVMStyleWithColumns(20))); 1505 EXPECT_EQ("/*\n" 1506 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1507 " * so_it_should_be_broken\n" 1508 " * wherever_a_space_occurs\n" 1509 " */", 1510 format("/*\n" 1511 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1512 " so_it_should_be_broken " 1513 " wherever_a_space_occurs \n" 1514 " */", 1515 getLLVMStyleWithColumns(20))); 1516 EXPECT_EQ("/*\n" 1517 " * This_comment_can_not_be_broken_into_lines\n" 1518 " */", 1519 format("/*\n" 1520 " * This_comment_can_not_be_broken_into_lines\n" 1521 " */", 1522 getLLVMStyleWithColumns(20))); 1523 EXPECT_EQ("{\n" 1524 " /*\n" 1525 " This is another\n" 1526 " long comment that\n" 1527 " doesn't fit on one\n" 1528 " line 1234567890\n" 1529 " */\n" 1530 "}", 1531 format("{\n" 1532 "/*\n" 1533 "This is another " 1534 " long comment that " 1535 " doesn't fit on one" 1536 " line 1234567890\n" 1537 "*/\n" 1538 "}", 1539 getLLVMStyleWithColumns(20))); 1540 EXPECT_EQ("{\n" 1541 " /*\n" 1542 " * This i s\n" 1543 " * another comment\n" 1544 " * t hat doesn' t\n" 1545 " * fit on one l i\n" 1546 " * n e\n" 1547 " */\n" 1548 "}", 1549 format("{\n" 1550 "/*\n" 1551 " * This i s" 1552 " another comment" 1553 " t hat doesn' t" 1554 " fit on one l i" 1555 " n e\n" 1556 " */\n" 1557 "}", 1558 getLLVMStyleWithColumns(20))); 1559 EXPECT_EQ("/*\n" 1560 " * This is a long\n" 1561 " * comment that\n" 1562 " * doesn't fit on\n" 1563 " * one line\n" 1564 " */", 1565 format(" /*\n" 1566 " * This is a long comment that doesn't fit on one line\n" 1567 " */", 1568 getLLVMStyleWithColumns(20))); 1569 EXPECT_EQ("{\n" 1570 " if (something) /* This is a\n" 1571 " long\n" 1572 " comment */\n" 1573 " ;\n" 1574 "}", 1575 format("{\n" 1576 " if (something) /* This is a long comment */\n" 1577 " ;\n" 1578 "}", 1579 getLLVMStyleWithColumns(30))); 1580 1581 EXPECT_EQ("/* A comment before\n" 1582 " * a macro\n" 1583 " * definition */\n" 1584 "#define a b", 1585 format("/* A comment before a macro definition */\n" 1586 "#define a b", 1587 getLLVMStyleWithColumns(20))); 1588 1589 EXPECT_EQ("/* some comment\n" 1590 " * a comment\n" 1591 "* that we break\n" 1592 " * another comment\n" 1593 "* we have to break\n" 1594 "* a left comment\n" 1595 " */", 1596 format(" /* some comment\n" 1597 " * a comment that we break\n" 1598 " * another comment we have to break\n" 1599 "* a left comment\n" 1600 " */", 1601 getLLVMStyleWithColumns(20))); 1602 1603 EXPECT_EQ("/**\n" 1604 " * multiline block\n" 1605 " * comment\n" 1606 " *\n" 1607 " */", 1608 format("/**\n" 1609 " * multiline block comment\n" 1610 " *\n" 1611 " */", 1612 getLLVMStyleWithColumns(20))); 1613 1614 EXPECT_EQ("/*\n" 1615 "\n" 1616 "\n" 1617 " */\n", 1618 format(" /* \n" 1619 " \n" 1620 " \n" 1621 " */\n")); 1622 1623 EXPECT_EQ("/* a a */", 1624 format("/* a a */", getLLVMStyleWithColumns(15))); 1625 EXPECT_EQ("/* a a bc */", 1626 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1627 EXPECT_EQ("/* aaa aaa\n" 1628 " * aaaaa */", 1629 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1630 EXPECT_EQ("/* aaa aaa\n" 1631 " * aaaaa */", 1632 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1633 } 1634 1635 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1636 EXPECT_EQ("#define X \\\n" 1637 " /* \\\n" 1638 " Test \\\n" 1639 " Macro comment \\\n" 1640 " with a long \\\n" 1641 " line \\\n" 1642 " */ \\\n" 1643 " A + B", 1644 format("#define X \\\n" 1645 " /*\n" 1646 " Test\n" 1647 " Macro comment with a long line\n" 1648 " */ \\\n" 1649 " A + B", 1650 getLLVMStyleWithColumns(20))); 1651 EXPECT_EQ("#define X \\\n" 1652 " /* Macro comment \\\n" 1653 " with a long \\\n" 1654 " line */ \\\n" 1655 " A + B", 1656 format("#define X \\\n" 1657 " /* Macro comment with a long\n" 1658 " line */ \\\n" 1659 " A + B", 1660 getLLVMStyleWithColumns(20))); 1661 EXPECT_EQ("#define X \\\n" 1662 " /* Macro comment \\\n" 1663 " * with a long \\\n" 1664 " * line */ \\\n" 1665 " A + B", 1666 format("#define X \\\n" 1667 " /* Macro comment with a long line */ \\\n" 1668 " A + B", 1669 getLLVMStyleWithColumns(20))); 1670 } 1671 1672 TEST_F(FormatTest, CommentsInStaticInitializers) { 1673 EXPECT_EQ( 1674 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1675 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1676 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1677 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1678 " aaaaaaaaaaaaaaaaaaaa};", 1679 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1680 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1681 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1682 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1683 " aaaaaaaaaaaaaaaaaaaa };")); 1684 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1685 " bbbbbbbbbbb, ccccccccccc};"); 1686 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1687 " // comment for bb....\n" 1688 " bbbbbbbbbbb, ccccccccccc};"); 1689 verifyGoogleFormat( 1690 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1691 " bbbbbbbbbbb, ccccccccccc};"); 1692 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1693 " // comment for bb....\n" 1694 " bbbbbbbbbbb, ccccccccccc};"); 1695 1696 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1697 " {d, e, f}, // Group #2\n" 1698 " {g, h, i}}; // Group #3"); 1699 verifyFormat("S s = {{// Group #1\n" 1700 " a, b, c},\n" 1701 " {// Group #2\n" 1702 " d, e, f},\n" 1703 " {// Group #3\n" 1704 " g, h, i}};"); 1705 1706 EXPECT_EQ("S s = {\n" 1707 " // Some comment\n" 1708 " a,\n" 1709 "\n" 1710 " // Comment after empty line\n" 1711 " b}", 1712 format("S s = {\n" 1713 " // Some comment\n" 1714 " a,\n" 1715 " \n" 1716 " // Comment after empty line\n" 1717 " b\n" 1718 "}")); 1719 EXPECT_EQ("S s = {\n" 1720 " /* Some comment */\n" 1721 " a,\n" 1722 "\n" 1723 " /* Comment after empty line */\n" 1724 " b}", 1725 format("S s = {\n" 1726 " /* Some comment */\n" 1727 " a,\n" 1728 " \n" 1729 " /* Comment after empty line */\n" 1730 " b\n" 1731 "}")); 1732 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1733 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1734 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1735 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1736 } 1737 1738 TEST_F(FormatTest, IgnoresIf0Contents) { 1739 EXPECT_EQ("#if 0\n" 1740 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1741 "#endif\n" 1742 "void f() {}", 1743 format("#if 0\n" 1744 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1745 "#endif\n" 1746 "void f( ) { }")); 1747 EXPECT_EQ("#if false\n" 1748 "void f( ) { }\n" 1749 "#endif\n" 1750 "void g() {}\n", 1751 format("#if false\n" 1752 "void f( ) { }\n" 1753 "#endif\n" 1754 "void g( ) { }\n")); 1755 EXPECT_EQ("enum E {\n" 1756 " One,\n" 1757 " Two,\n" 1758 "#if 0\n" 1759 "Three,\n" 1760 " Four,\n" 1761 "#endif\n" 1762 " Five\n" 1763 "};", 1764 format("enum E {\n" 1765 " One,Two,\n" 1766 "#if 0\n" 1767 "Three,\n" 1768 " Four,\n" 1769 "#endif\n" 1770 " Five};")); 1771 EXPECT_EQ("enum F {\n" 1772 " One,\n" 1773 "#if 1\n" 1774 " Two,\n" 1775 "#if 0\n" 1776 "Three,\n" 1777 " Four,\n" 1778 "#endif\n" 1779 " Five\n" 1780 "#endif\n" 1781 "};", 1782 format("enum F {\n" 1783 "One,\n" 1784 "#if 1\n" 1785 "Two,\n" 1786 "#if 0\n" 1787 "Three,\n" 1788 " Four,\n" 1789 "#endif\n" 1790 "Five\n" 1791 "#endif\n" 1792 "};")); 1793 EXPECT_EQ("enum G {\n" 1794 " One,\n" 1795 "#if 0\n" 1796 "Two,\n" 1797 "#else\n" 1798 " Three,\n" 1799 "#endif\n" 1800 " Four\n" 1801 "};", 1802 format("enum G {\n" 1803 "One,\n" 1804 "#if 0\n" 1805 "Two,\n" 1806 "#else\n" 1807 "Three,\n" 1808 "#endif\n" 1809 "Four\n" 1810 "};")); 1811 EXPECT_EQ("enum H {\n" 1812 " One,\n" 1813 "#if 0\n" 1814 "#ifdef Q\n" 1815 "Two,\n" 1816 "#else\n" 1817 "Three,\n" 1818 "#endif\n" 1819 "#endif\n" 1820 " Four\n" 1821 "};", 1822 format("enum H {\n" 1823 "One,\n" 1824 "#if 0\n" 1825 "#ifdef Q\n" 1826 "Two,\n" 1827 "#else\n" 1828 "Three,\n" 1829 "#endif\n" 1830 "#endif\n" 1831 "Four\n" 1832 "};")); 1833 EXPECT_EQ("enum I {\n" 1834 " One,\n" 1835 "#if /* test */ 0 || 1\n" 1836 "Two,\n" 1837 "Three,\n" 1838 "#endif\n" 1839 " Four\n" 1840 "};", 1841 format("enum I {\n" 1842 "One,\n" 1843 "#if /* test */ 0 || 1\n" 1844 "Two,\n" 1845 "Three,\n" 1846 "#endif\n" 1847 "Four\n" 1848 "};")); 1849 EXPECT_EQ("enum J {\n" 1850 " One,\n" 1851 "#if 0\n" 1852 "#if 0\n" 1853 "Two,\n" 1854 "#else\n" 1855 "Three,\n" 1856 "#endif\n" 1857 "Four,\n" 1858 "#endif\n" 1859 " Five\n" 1860 "};", 1861 format("enum J {\n" 1862 "One,\n" 1863 "#if 0\n" 1864 "#if 0\n" 1865 "Two,\n" 1866 "#else\n" 1867 "Three,\n" 1868 "#endif\n" 1869 "Four,\n" 1870 "#endif\n" 1871 "Five\n" 1872 "};")); 1873 } 1874 1875 //===----------------------------------------------------------------------===// 1876 // Tests for classes, namespaces, etc. 1877 //===----------------------------------------------------------------------===// 1878 1879 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1880 verifyFormat("class A {};"); 1881 } 1882 1883 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1884 verifyFormat("class A {\n" 1885 "public:\n" 1886 "public: // comment\n" 1887 "protected:\n" 1888 "private:\n" 1889 " void f() {}\n" 1890 "};"); 1891 verifyGoogleFormat("class A {\n" 1892 " public:\n" 1893 " protected:\n" 1894 " private:\n" 1895 " void f() {}\n" 1896 "};"); 1897 verifyFormat("class A {\n" 1898 "public slots:\n" 1899 " void f1() {}\n" 1900 "public Q_SLOTS:\n" 1901 " void f2() {}\n" 1902 "protected slots:\n" 1903 " void f3() {}\n" 1904 "protected Q_SLOTS:\n" 1905 " void f4() {}\n" 1906 "private slots:\n" 1907 " void f5() {}\n" 1908 "private Q_SLOTS:\n" 1909 " void f6() {}\n" 1910 "signals:\n" 1911 " void g1();\n" 1912 "Q_SIGNALS:\n" 1913 " void g2();\n" 1914 "};"); 1915 1916 // Don't interpret 'signals' the wrong way. 1917 verifyFormat("signals.set();"); 1918 verifyFormat("for (Signals signals : f()) {\n}"); 1919 verifyFormat("{\n" 1920 " signals.set(); // This needs indentation.\n" 1921 "}"); 1922 } 1923 1924 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1925 EXPECT_EQ("class A {\n" 1926 "public:\n" 1927 " void f();\n" 1928 "\n" 1929 "private:\n" 1930 " void g() {}\n" 1931 " // test\n" 1932 "protected:\n" 1933 " int h;\n" 1934 "};", 1935 format("class A {\n" 1936 "public:\n" 1937 "void f();\n" 1938 "private:\n" 1939 "void g() {}\n" 1940 "// test\n" 1941 "protected:\n" 1942 "int h;\n" 1943 "};")); 1944 EXPECT_EQ("class A {\n" 1945 "protected:\n" 1946 "public:\n" 1947 " void f();\n" 1948 "};", 1949 format("class A {\n" 1950 "protected:\n" 1951 "\n" 1952 "public:\n" 1953 "\n" 1954 " void f();\n" 1955 "};")); 1956 1957 // Even ensure proper spacing inside macros. 1958 EXPECT_EQ("#define B \\\n" 1959 " class A { \\\n" 1960 " protected: \\\n" 1961 " public: \\\n" 1962 " void f(); \\\n" 1963 " };", 1964 format("#define B \\\n" 1965 " class A { \\\n" 1966 " protected: \\\n" 1967 " \\\n" 1968 " public: \\\n" 1969 " \\\n" 1970 " void f(); \\\n" 1971 " };", 1972 getGoogleStyle())); 1973 // But don't remove empty lines after macros ending in access specifiers. 1974 EXPECT_EQ("#define A private:\n" 1975 "\n" 1976 "int i;", 1977 format("#define A private:\n" 1978 "\n" 1979 "int i;")); 1980 } 1981 1982 TEST_F(FormatTest, FormatsClasses) { 1983 verifyFormat("class A : public B {};"); 1984 verifyFormat("class A : public ::B {};"); 1985 1986 verifyFormat( 1987 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1988 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1989 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1990 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1991 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1992 verifyFormat( 1993 "class A : public B, public C, public D, public E, public F {};"); 1994 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1995 " public C,\n" 1996 " public D,\n" 1997 " public E,\n" 1998 " public F,\n" 1999 " public G {};"); 2000 2001 verifyFormat("class\n" 2002 " ReallyReallyLongClassName {\n" 2003 " int i;\n" 2004 "};", 2005 getLLVMStyleWithColumns(32)); 2006 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2007 " aaaaaaaaaaaaaaaa> {};"); 2008 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2009 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2010 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2011 verifyFormat("template <class R, class C>\n" 2012 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2013 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2014 verifyFormat("class ::A::B {};"); 2015 } 2016 2017 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2018 verifyFormat("class A {\n} a, b;"); 2019 verifyFormat("struct A {\n} a, b;"); 2020 verifyFormat("union A {\n} a;"); 2021 } 2022 2023 TEST_F(FormatTest, FormatsEnum) { 2024 verifyFormat("enum {\n" 2025 " Zero,\n" 2026 " One = 1,\n" 2027 " Two = One + 1,\n" 2028 " Three = (One + Two),\n" 2029 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2030 " Five = (One, Two, Three, Four, 5)\n" 2031 "};"); 2032 verifyGoogleFormat("enum {\n" 2033 " Zero,\n" 2034 " One = 1,\n" 2035 " Two = One + 1,\n" 2036 " Three = (One + Two),\n" 2037 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2038 " Five = (One, Two, Three, Four, 5)\n" 2039 "};"); 2040 verifyFormat("enum Enum {};"); 2041 verifyFormat("enum {};"); 2042 verifyFormat("enum X E {} d;"); 2043 verifyFormat("enum __attribute__((...)) E {} d;"); 2044 verifyFormat("enum __declspec__((...)) E {} d;"); 2045 verifyFormat("enum {\n" 2046 " Bar = Foo<int, int>::value\n" 2047 "};", 2048 getLLVMStyleWithColumns(30)); 2049 2050 verifyFormat("enum ShortEnum { A, B, C };"); 2051 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2052 2053 EXPECT_EQ("enum KeepEmptyLines {\n" 2054 " ONE,\n" 2055 "\n" 2056 " TWO,\n" 2057 "\n" 2058 " THREE\n" 2059 "}", 2060 format("enum KeepEmptyLines {\n" 2061 " ONE,\n" 2062 "\n" 2063 " TWO,\n" 2064 "\n" 2065 "\n" 2066 " THREE\n" 2067 "}")); 2068 verifyFormat("enum E { // comment\n" 2069 " ONE,\n" 2070 " TWO\n" 2071 "};\n" 2072 "int i;"); 2073 // Not enums. 2074 verifyFormat("enum X f() {\n" 2075 " a();\n" 2076 " return 42;\n" 2077 "}"); 2078 verifyFormat("enum X Type::f() {\n" 2079 " a();\n" 2080 " return 42;\n" 2081 "}"); 2082 verifyFormat("enum ::X f() {\n" 2083 " a();\n" 2084 " return 42;\n" 2085 "}"); 2086 verifyFormat("enum ns::X f() {\n" 2087 " a();\n" 2088 " return 42;\n" 2089 "}"); 2090 } 2091 2092 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2093 verifyFormat("enum Type {\n" 2094 " One = 0; // These semicolons should be commas.\n" 2095 " Two = 1;\n" 2096 "};"); 2097 verifyFormat("namespace n {\n" 2098 "enum Type {\n" 2099 " One,\n" 2100 " Two, // missing };\n" 2101 " int i;\n" 2102 "}\n" 2103 "void g() {}"); 2104 } 2105 2106 TEST_F(FormatTest, FormatsEnumStruct) { 2107 verifyFormat("enum struct {\n" 2108 " Zero,\n" 2109 " One = 1,\n" 2110 " Two = One + 1,\n" 2111 " Three = (One + Two),\n" 2112 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2113 " Five = (One, Two, Three, Four, 5)\n" 2114 "};"); 2115 verifyFormat("enum struct Enum {};"); 2116 verifyFormat("enum struct {};"); 2117 verifyFormat("enum struct X E {} d;"); 2118 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2119 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2120 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2121 } 2122 2123 TEST_F(FormatTest, FormatsEnumClass) { 2124 verifyFormat("enum class {\n" 2125 " Zero,\n" 2126 " One = 1,\n" 2127 " Two = One + 1,\n" 2128 " Three = (One + Two),\n" 2129 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2130 " Five = (One, Two, Three, Four, 5)\n" 2131 "};"); 2132 verifyFormat("enum class Enum {};"); 2133 verifyFormat("enum class {};"); 2134 verifyFormat("enum class X E {} d;"); 2135 verifyFormat("enum class __attribute__((...)) E {} d;"); 2136 verifyFormat("enum class __declspec__((...)) E {} d;"); 2137 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2138 } 2139 2140 TEST_F(FormatTest, FormatsEnumTypes) { 2141 verifyFormat("enum X : int {\n" 2142 " A, // Force multiple lines.\n" 2143 " B\n" 2144 "};"); 2145 verifyFormat("enum X : int { A, B };"); 2146 verifyFormat("enum X : std::uint32_t { A, B };"); 2147 } 2148 2149 TEST_F(FormatTest, FormatsNSEnums) { 2150 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2151 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2152 " // Information about someDecentlyLongValue.\n" 2153 " someDecentlyLongValue,\n" 2154 " // Information about anotherDecentlyLongValue.\n" 2155 " anotherDecentlyLongValue,\n" 2156 " // Information about aThirdDecentlyLongValue.\n" 2157 " aThirdDecentlyLongValue\n" 2158 "};"); 2159 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2160 " a = 1,\n" 2161 " b = 2,\n" 2162 " c = 3,\n" 2163 "};"); 2164 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2165 " a = 1,\n" 2166 " b = 2,\n" 2167 " c = 3,\n" 2168 "};"); 2169 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2170 " a = 1,\n" 2171 " b = 2,\n" 2172 " c = 3,\n" 2173 "};"); 2174 } 2175 2176 TEST_F(FormatTest, FormatsBitfields) { 2177 verifyFormat("struct Bitfields {\n" 2178 " unsigned sClass : 8;\n" 2179 " unsigned ValueKind : 2;\n" 2180 "};"); 2181 verifyFormat("struct A {\n" 2182 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2183 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2184 "};"); 2185 verifyFormat("struct MyStruct {\n" 2186 " uchar data;\n" 2187 " uchar : 8;\n" 2188 " uchar : 8;\n" 2189 " uchar other;\n" 2190 "};"); 2191 } 2192 2193 TEST_F(FormatTest, FormatsNamespaces) { 2194 verifyFormat("namespace some_namespace {\n" 2195 "class A {};\n" 2196 "void f() { f(); }\n" 2197 "}"); 2198 verifyFormat("namespace {\n" 2199 "class A {};\n" 2200 "void f() { f(); }\n" 2201 "}"); 2202 verifyFormat("inline namespace X {\n" 2203 "class A {};\n" 2204 "void f() { f(); }\n" 2205 "}"); 2206 verifyFormat("using namespace some_namespace;\n" 2207 "class A {};\n" 2208 "void f() { f(); }"); 2209 2210 // This code is more common than we thought; if we 2211 // layout this correctly the semicolon will go into 2212 // its own line, which is undesirable. 2213 verifyFormat("namespace {};"); 2214 verifyFormat("namespace {\n" 2215 "class A {};\n" 2216 "};"); 2217 2218 verifyFormat("namespace {\n" 2219 "int SomeVariable = 0; // comment\n" 2220 "} // namespace"); 2221 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2222 "#define HEADER_GUARD\n" 2223 "namespace my_namespace {\n" 2224 "int i;\n" 2225 "} // my_namespace\n" 2226 "#endif // HEADER_GUARD", 2227 format("#ifndef HEADER_GUARD\n" 2228 " #define HEADER_GUARD\n" 2229 " namespace my_namespace {\n" 2230 "int i;\n" 2231 "} // my_namespace\n" 2232 "#endif // HEADER_GUARD")); 2233 2234 EXPECT_EQ("namespace A::B {\n" 2235 "class C {};\n" 2236 "}", 2237 format("namespace A::B {\n" 2238 "class C {};\n" 2239 "}")); 2240 2241 FormatStyle Style = getLLVMStyle(); 2242 Style.NamespaceIndentation = FormatStyle::NI_All; 2243 EXPECT_EQ("namespace out {\n" 2244 " int i;\n" 2245 " namespace in {\n" 2246 " int i;\n" 2247 " } // namespace\n" 2248 "} // namespace", 2249 format("namespace out {\n" 2250 "int i;\n" 2251 "namespace in {\n" 2252 "int i;\n" 2253 "} // namespace\n" 2254 "} // namespace", 2255 Style)); 2256 2257 Style.NamespaceIndentation = FormatStyle::NI_Inner; 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 2273 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2274 2275 TEST_F(FormatTest, FormatsInlineASM) { 2276 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2277 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2278 verifyFormat( 2279 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2280 " \"cpuid\\n\\t\"\n" 2281 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2282 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2283 " : \"a\"(value));"); 2284 EXPECT_EQ( 2285 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2286 " __asm {\n" 2287 " mov edx,[that] // vtable in edx\n" 2288 " mov eax,methodIndex\n" 2289 " call [edx][eax*4] // stdcall\n" 2290 " }\n" 2291 "}", 2292 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2293 " __asm {\n" 2294 " mov edx,[that] // vtable in edx\n" 2295 " mov eax,methodIndex\n" 2296 " call [edx][eax*4] // stdcall\n" 2297 " }\n" 2298 "}")); 2299 EXPECT_EQ("_asm {\n" 2300 " xor eax, eax;\n" 2301 " cpuid;\n" 2302 "}", 2303 format("_asm {\n" 2304 " xor eax, eax;\n" 2305 " cpuid;\n" 2306 "}")); 2307 verifyFormat("void function() {\n" 2308 " // comment\n" 2309 " asm(\"\");\n" 2310 "}"); 2311 EXPECT_EQ("__asm {\n" 2312 "}\n" 2313 "int i;", 2314 format("__asm {\n" 2315 "}\n" 2316 "int i;")); 2317 } 2318 2319 TEST_F(FormatTest, FormatTryCatch) { 2320 verifyFormat("try {\n" 2321 " throw a * b;\n" 2322 "} catch (int a) {\n" 2323 " // Do nothing.\n" 2324 "} catch (...) {\n" 2325 " exit(42);\n" 2326 "}"); 2327 2328 // Function-level try statements. 2329 verifyFormat("int f() try { return 4; } catch (...) {\n" 2330 " return 5;\n" 2331 "}"); 2332 verifyFormat("class A {\n" 2333 " int a;\n" 2334 " A() try : a(0) {\n" 2335 " } catch (...) {\n" 2336 " throw;\n" 2337 " }\n" 2338 "};\n"); 2339 2340 // Incomplete try-catch blocks. 2341 verifyIncompleteFormat("try {} catch ("); 2342 } 2343 2344 TEST_F(FormatTest, FormatSEHTryCatch) { 2345 verifyFormat("__try {\n" 2346 " int a = b * c;\n" 2347 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2348 " // Do nothing.\n" 2349 "}"); 2350 2351 verifyFormat("__try {\n" 2352 " int a = b * c;\n" 2353 "} __finally {\n" 2354 " // Do nothing.\n" 2355 "}"); 2356 2357 verifyFormat("DEBUG({\n" 2358 " __try {\n" 2359 " } __finally {\n" 2360 " }\n" 2361 "});\n"); 2362 } 2363 2364 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2365 verifyFormat("try {\n" 2366 " f();\n" 2367 "} catch {\n" 2368 " g();\n" 2369 "}"); 2370 verifyFormat("try {\n" 2371 " f();\n" 2372 "} catch (A a) MACRO(x) {\n" 2373 " g();\n" 2374 "} catch (B b) MACRO(x) {\n" 2375 " g();\n" 2376 "}"); 2377 } 2378 2379 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2380 FormatStyle Style = getLLVMStyle(); 2381 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2382 FormatStyle::BS_WebKit}) { 2383 Style.BreakBeforeBraces = BraceStyle; 2384 verifyFormat("try {\n" 2385 " // something\n" 2386 "} catch (...) {\n" 2387 " // something\n" 2388 "}", 2389 Style); 2390 } 2391 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2392 verifyFormat("try {\n" 2393 " // something\n" 2394 "}\n" 2395 "catch (...) {\n" 2396 " // something\n" 2397 "}", 2398 Style); 2399 verifyFormat("__try {\n" 2400 " // something\n" 2401 "}\n" 2402 "__finally {\n" 2403 " // something\n" 2404 "}", 2405 Style); 2406 verifyFormat("@try {\n" 2407 " // something\n" 2408 "}\n" 2409 "@finally {\n" 2410 " // something\n" 2411 "}", 2412 Style); 2413 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2414 verifyFormat("try\n" 2415 "{\n" 2416 " // something\n" 2417 "}\n" 2418 "catch (...)\n" 2419 "{\n" 2420 " // something\n" 2421 "}", 2422 Style); 2423 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2424 verifyFormat("try\n" 2425 " {\n" 2426 " // something\n" 2427 " }\n" 2428 "catch (...)\n" 2429 " {\n" 2430 " // something\n" 2431 " }", 2432 Style); 2433 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2434 Style.BraceWrapping.BeforeCatch = true; 2435 verifyFormat("try {\n" 2436 " // something\n" 2437 "}\n" 2438 "catch (...) {\n" 2439 " // something\n" 2440 "}", 2441 Style); 2442 } 2443 2444 TEST_F(FormatTest, FormatObjCTryCatch) { 2445 verifyFormat("@try {\n" 2446 " f();\n" 2447 "} @catch (NSException e) {\n" 2448 " @throw;\n" 2449 "} @finally {\n" 2450 " exit(42);\n" 2451 "}"); 2452 verifyFormat("DEBUG({\n" 2453 " @try {\n" 2454 " } @finally {\n" 2455 " }\n" 2456 "});\n"); 2457 } 2458 2459 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2460 FormatStyle Style = getLLVMStyle(); 2461 verifyFormat("@autoreleasepool {\n" 2462 " f();\n" 2463 "}\n" 2464 "@autoreleasepool {\n" 2465 " f();\n" 2466 "}\n", 2467 Style); 2468 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2469 verifyFormat("@autoreleasepool\n" 2470 "{\n" 2471 " f();\n" 2472 "}\n" 2473 "@autoreleasepool\n" 2474 "{\n" 2475 " f();\n" 2476 "}\n", 2477 Style); 2478 } 2479 2480 TEST_F(FormatTest, StaticInitializers) { 2481 verifyFormat("static SomeClass SC = {1, 'a'};"); 2482 2483 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2484 " 100000000, " 2485 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2486 2487 // Here, everything other than the "}" would fit on a line. 2488 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2489 " 10000000000000000000000000};"); 2490 EXPECT_EQ("S s = {a,\n" 2491 "\n" 2492 " b};", 2493 format("S s = {\n" 2494 " a,\n" 2495 "\n" 2496 " b\n" 2497 "};")); 2498 2499 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2500 // line. However, the formatting looks a bit off and this probably doesn't 2501 // happen often in practice. 2502 verifyFormat("static int Variable[1] = {\n" 2503 " {1000000000000000000000000000000000000}};", 2504 getLLVMStyleWithColumns(40)); 2505 } 2506 2507 TEST_F(FormatTest, DesignatedInitializers) { 2508 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2509 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2510 " .bbbbbbbbbb = 2,\n" 2511 " .cccccccccc = 3,\n" 2512 " .dddddddddd = 4,\n" 2513 " .eeeeeeeeee = 5};"); 2514 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2515 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2516 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2517 " .ccccccccccccccccccccccccccc = 3,\n" 2518 " .ddddddddddddddddddddddddddd = 4,\n" 2519 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2520 2521 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2522 } 2523 2524 TEST_F(FormatTest, NestedStaticInitializers) { 2525 verifyFormat("static A x = {{{}}};\n"); 2526 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2527 " {init1, init2, init3, init4}}};", 2528 getLLVMStyleWithColumns(50)); 2529 2530 verifyFormat("somes Status::global_reps[3] = {\n" 2531 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2532 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2533 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2534 getLLVMStyleWithColumns(60)); 2535 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2536 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2537 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2538 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2539 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2540 " {rect.fRight - rect.fLeft, rect.fBottom - " 2541 "rect.fTop}};"); 2542 2543 verifyFormat( 2544 "SomeArrayOfSomeType a = {\n" 2545 " {{1, 2, 3},\n" 2546 " {1, 2, 3},\n" 2547 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2548 " 333333333333333333333333333333},\n" 2549 " {1, 2, 3},\n" 2550 " {1, 2, 3}}};"); 2551 verifyFormat( 2552 "SomeArrayOfSomeType a = {\n" 2553 " {{1, 2, 3}},\n" 2554 " {{1, 2, 3}},\n" 2555 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2556 " 333333333333333333333333333333}},\n" 2557 " {{1, 2, 3}},\n" 2558 " {{1, 2, 3}}};"); 2559 2560 verifyFormat("struct {\n" 2561 " unsigned bit;\n" 2562 " const char *const name;\n" 2563 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2564 " {kOsWin, \"Windows\"},\n" 2565 " {kOsLinux, \"Linux\"},\n" 2566 " {kOsCrOS, \"Chrome OS\"}};"); 2567 verifyFormat("struct {\n" 2568 " unsigned bit;\n" 2569 " const char *const name;\n" 2570 "} kBitsToOs[] = {\n" 2571 " {kOsMac, \"Mac\"},\n" 2572 " {kOsWin, \"Windows\"},\n" 2573 " {kOsLinux, \"Linux\"},\n" 2574 " {kOsCrOS, \"Chrome OS\"},\n" 2575 "};"); 2576 } 2577 2578 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2579 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2580 " \\\n" 2581 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2582 } 2583 2584 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2585 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2586 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2587 2588 // Do break defaulted and deleted functions. 2589 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2590 " default;", 2591 getLLVMStyleWithColumns(40)); 2592 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2593 " delete;", 2594 getLLVMStyleWithColumns(40)); 2595 } 2596 2597 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2598 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2599 getLLVMStyleWithColumns(40)); 2600 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2601 getLLVMStyleWithColumns(40)); 2602 EXPECT_EQ("#define Q \\\n" 2603 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2604 " \"aaaaaaaa.cpp\"", 2605 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2606 getLLVMStyleWithColumns(40))); 2607 } 2608 2609 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2610 EXPECT_EQ("# 123 \"A string literal\"", 2611 format(" # 123 \"A string literal\"")); 2612 } 2613 2614 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2615 EXPECT_EQ("#;", format("#;")); 2616 verifyFormat("#\n;\n;\n;"); 2617 } 2618 2619 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2620 EXPECT_EQ("#line 42 \"test\"\n", 2621 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2622 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2623 getLLVMStyleWithColumns(12))); 2624 } 2625 2626 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2627 EXPECT_EQ("#line 42 \"test\"", 2628 format("# \\\n line \\\n 42 \\\n \"test\"")); 2629 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2630 } 2631 2632 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2633 verifyFormat("#define A \\x20"); 2634 verifyFormat("#define A \\ x20"); 2635 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2636 verifyFormat("#define A ''"); 2637 verifyFormat("#define A ''qqq"); 2638 verifyFormat("#define A `qqq"); 2639 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2640 EXPECT_EQ("const char *c = STRINGIFY(\n" 2641 "\\na : b);", 2642 format("const char * c = STRINGIFY(\n" 2643 "\\na : b);")); 2644 2645 verifyFormat("a\r\\"); 2646 verifyFormat("a\v\\"); 2647 verifyFormat("a\f\\"); 2648 } 2649 2650 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2651 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2652 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2653 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2654 // FIXME: We never break before the macro name. 2655 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2656 2657 verifyFormat("#define A A\n#define A A"); 2658 verifyFormat("#define A(X) A\n#define A A"); 2659 2660 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2661 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2662 } 2663 2664 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2665 EXPECT_EQ("// somecomment\n" 2666 "#include \"a.h\"\n" 2667 "#define A( \\\n" 2668 " A, B)\n" 2669 "#include \"b.h\"\n" 2670 "// somecomment\n", 2671 format(" // somecomment\n" 2672 " #include \"a.h\"\n" 2673 "#define A(A,\\\n" 2674 " B)\n" 2675 " #include \"b.h\"\n" 2676 " // somecomment\n", 2677 getLLVMStyleWithColumns(13))); 2678 } 2679 2680 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2681 2682 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2683 EXPECT_EQ("#define A \\\n" 2684 " c; \\\n" 2685 " e;\n" 2686 "f;", 2687 format("#define A c; e;\n" 2688 "f;", 2689 getLLVMStyleWithColumns(14))); 2690 } 2691 2692 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2693 2694 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2695 EXPECT_EQ("int x,\n" 2696 "#define A\n" 2697 " y;", 2698 format("int x,\n#define A\ny;")); 2699 } 2700 2701 TEST_F(FormatTest, HashInMacroDefinition) { 2702 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2703 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2704 verifyFormat("#define A \\\n" 2705 " { \\\n" 2706 " f(#c); \\\n" 2707 " }", 2708 getLLVMStyleWithColumns(11)); 2709 2710 verifyFormat("#define A(X) \\\n" 2711 " void function##X()", 2712 getLLVMStyleWithColumns(22)); 2713 2714 verifyFormat("#define A(a, b, c) \\\n" 2715 " void a##b##c()", 2716 getLLVMStyleWithColumns(22)); 2717 2718 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2719 } 2720 2721 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2722 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2723 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2724 } 2725 2726 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2727 EXPECT_EQ("#define A b;", format("#define A \\\n" 2728 " \\\n" 2729 " b;", 2730 getLLVMStyleWithColumns(25))); 2731 EXPECT_EQ("#define A \\\n" 2732 " \\\n" 2733 " a; \\\n" 2734 " b;", 2735 format("#define A \\\n" 2736 " \\\n" 2737 " a; \\\n" 2738 " b;", 2739 getLLVMStyleWithColumns(11))); 2740 EXPECT_EQ("#define A \\\n" 2741 " a; \\\n" 2742 " \\\n" 2743 " b;", 2744 format("#define A \\\n" 2745 " a; \\\n" 2746 " \\\n" 2747 " b;", 2748 getLLVMStyleWithColumns(11))); 2749 } 2750 2751 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2752 verifyIncompleteFormat("#define A :"); 2753 verifyFormat("#define SOMECASES \\\n" 2754 " case 1: \\\n" 2755 " case 2\n", 2756 getLLVMStyleWithColumns(20)); 2757 verifyFormat("#define A template <typename T>"); 2758 verifyIncompleteFormat("#define STR(x) #x\n" 2759 "f(STR(this_is_a_string_literal{));"); 2760 verifyFormat("#pragma omp threadprivate( \\\n" 2761 " y)), // expected-warning", 2762 getLLVMStyleWithColumns(28)); 2763 verifyFormat("#d, = };"); 2764 verifyFormat("#if \"a"); 2765 verifyIncompleteFormat("({\n" 2766 "#define b \\\n" 2767 " } \\\n" 2768 " a\n" 2769 "a", 2770 getLLVMStyleWithColumns(15)); 2771 verifyFormat("#define A \\\n" 2772 " { \\\n" 2773 " {\n" 2774 "#define B \\\n" 2775 " } \\\n" 2776 " }", 2777 getLLVMStyleWithColumns(15)); 2778 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2779 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2780 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2781 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2782 } 2783 2784 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2785 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2786 EXPECT_EQ("class A : public QObject {\n" 2787 " Q_OBJECT\n" 2788 "\n" 2789 " A() {}\n" 2790 "};", 2791 format("class A : public QObject {\n" 2792 " Q_OBJECT\n" 2793 "\n" 2794 " A() {\n}\n" 2795 "} ;")); 2796 EXPECT_EQ("MACRO\n" 2797 "/*static*/ int i;", 2798 format("MACRO\n" 2799 " /*static*/ int i;")); 2800 EXPECT_EQ("SOME_MACRO\n" 2801 "namespace {\n" 2802 "void f();\n" 2803 "}", 2804 format("SOME_MACRO\n" 2805 " namespace {\n" 2806 "void f( );\n" 2807 "}")); 2808 // Only if the identifier contains at least 5 characters. 2809 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2810 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2811 // Only if everything is upper case. 2812 EXPECT_EQ("class A : public QObject {\n" 2813 " Q_Object A() {}\n" 2814 "};", 2815 format("class A : public QObject {\n" 2816 " Q_Object\n" 2817 " A() {\n}\n" 2818 "} ;")); 2819 2820 // Only if the next line can actually start an unwrapped line. 2821 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2822 format("SOME_WEIRD_LOG_MACRO\n" 2823 "<< SomeThing;")); 2824 2825 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2826 "(n, buffers))\n", 2827 getChromiumStyle(FormatStyle::LK_Cpp)); 2828 } 2829 2830 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2831 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2832 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2833 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2834 "class X {};\n" 2835 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2836 "int *createScopDetectionPass() { return 0; }", 2837 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2838 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2839 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2840 " class X {};\n" 2841 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2842 " int *createScopDetectionPass() { return 0; }")); 2843 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2844 // braces, so that inner block is indented one level more. 2845 EXPECT_EQ("int q() {\n" 2846 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2847 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2848 " IPC_END_MESSAGE_MAP()\n" 2849 "}", 2850 format("int q() {\n" 2851 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2852 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2853 " IPC_END_MESSAGE_MAP()\n" 2854 "}")); 2855 2856 // Same inside macros. 2857 EXPECT_EQ("#define LIST(L) \\\n" 2858 " L(A) \\\n" 2859 " L(B) \\\n" 2860 " L(C)", 2861 format("#define LIST(L) \\\n" 2862 " L(A) \\\n" 2863 " L(B) \\\n" 2864 " L(C)", 2865 getGoogleStyle())); 2866 2867 // These must not be recognized as macros. 2868 EXPECT_EQ("int q() {\n" 2869 " f(x);\n" 2870 " f(x) {}\n" 2871 " f(x)->g();\n" 2872 " f(x)->*g();\n" 2873 " f(x).g();\n" 2874 " f(x) = x;\n" 2875 " f(x) += x;\n" 2876 " f(x) -= x;\n" 2877 " f(x) *= x;\n" 2878 " f(x) /= x;\n" 2879 " f(x) %= x;\n" 2880 " f(x) &= x;\n" 2881 " f(x) |= x;\n" 2882 " f(x) ^= x;\n" 2883 " f(x) >>= x;\n" 2884 " f(x) <<= x;\n" 2885 " f(x)[y].z();\n" 2886 " LOG(INFO) << x;\n" 2887 " ifstream(x) >> x;\n" 2888 "}\n", 2889 format("int q() {\n" 2890 " f(x)\n;\n" 2891 " f(x)\n {}\n" 2892 " f(x)\n->g();\n" 2893 " f(x)\n->*g();\n" 2894 " f(x)\n.g();\n" 2895 " f(x)\n = x;\n" 2896 " f(x)\n += x;\n" 2897 " f(x)\n -= x;\n" 2898 " f(x)\n *= x;\n" 2899 " f(x)\n /= x;\n" 2900 " f(x)\n %= x;\n" 2901 " f(x)\n &= x;\n" 2902 " f(x)\n |= x;\n" 2903 " f(x)\n ^= x;\n" 2904 " f(x)\n >>= x;\n" 2905 " f(x)\n <<= x;\n" 2906 " f(x)\n[y].z();\n" 2907 " LOG(INFO)\n << x;\n" 2908 " ifstream(x)\n >> x;\n" 2909 "}\n")); 2910 EXPECT_EQ("int q() {\n" 2911 " F(x)\n" 2912 " if (1) {\n" 2913 " }\n" 2914 " F(x)\n" 2915 " while (1) {\n" 2916 " }\n" 2917 " F(x)\n" 2918 " G(x);\n" 2919 " F(x)\n" 2920 " try {\n" 2921 " Q();\n" 2922 " } catch (...) {\n" 2923 " }\n" 2924 "}\n", 2925 format("int q() {\n" 2926 "F(x)\n" 2927 "if (1) {}\n" 2928 "F(x)\n" 2929 "while (1) {}\n" 2930 "F(x)\n" 2931 "G(x);\n" 2932 "F(x)\n" 2933 "try { Q(); } catch (...) {}\n" 2934 "}\n")); 2935 EXPECT_EQ("class A {\n" 2936 " A() : t(0) {}\n" 2937 " A(int i) noexcept() : {}\n" 2938 " A(X x)\n" // FIXME: function-level try blocks are broken. 2939 " try : t(0) {\n" 2940 " } catch (...) {\n" 2941 " }\n" 2942 "};", 2943 format("class A {\n" 2944 " A()\n : t(0) {}\n" 2945 " A(int i)\n noexcept() : {}\n" 2946 " A(X x)\n" 2947 " try : t(0) {} catch (...) {}\n" 2948 "};")); 2949 EXPECT_EQ("class SomeClass {\n" 2950 "public:\n" 2951 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2952 "};", 2953 format("class SomeClass {\n" 2954 "public:\n" 2955 " SomeClass()\n" 2956 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2957 "};")); 2958 EXPECT_EQ("class SomeClass {\n" 2959 "public:\n" 2960 " SomeClass()\n" 2961 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2962 "};", 2963 format("class SomeClass {\n" 2964 "public:\n" 2965 " SomeClass()\n" 2966 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2967 "};", 2968 getLLVMStyleWithColumns(40))); 2969 2970 verifyFormat("MACRO(>)"); 2971 } 2972 2973 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2974 verifyFormat("#define A \\\n" 2975 " f({ \\\n" 2976 " g(); \\\n" 2977 " });", 2978 getLLVMStyleWithColumns(11)); 2979 } 2980 2981 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2982 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2983 } 2984 2985 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2986 verifyFormat("{\n { a #c; }\n}"); 2987 } 2988 2989 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2990 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2991 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2992 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2993 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2994 } 2995 2996 TEST_F(FormatTest, EscapedNewlines) { 2997 EXPECT_EQ( 2998 "#define A \\\n int i; \\\n int j;", 2999 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3000 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3001 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3002 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3003 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3004 } 3005 3006 TEST_F(FormatTest, DontCrashOnBlockComments) { 3007 EXPECT_EQ( 3008 "int xxxxxxxxx; /* " 3009 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3010 "zzzzzz\n" 3011 "0*/", 3012 format("int xxxxxxxxx; /* " 3013 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3014 "0*/")); 3015 } 3016 3017 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3018 verifyFormat("#define A \\\n" 3019 " int v( \\\n" 3020 " a); \\\n" 3021 " int i;", 3022 getLLVMStyleWithColumns(11)); 3023 } 3024 3025 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3026 EXPECT_EQ( 3027 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3028 " \\\n" 3029 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3030 "\n" 3031 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3032 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3033 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3034 "\\\n" 3035 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3036 " \n" 3037 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3038 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3039 } 3040 3041 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3042 EXPECT_EQ("int\n" 3043 "#define A\n" 3044 " a;", 3045 format("int\n#define A\na;")); 3046 verifyFormat("functionCallTo(\n" 3047 " someOtherFunction(\n" 3048 " withSomeParameters, whichInSequence,\n" 3049 " areLongerThanALine(andAnotherCall,\n" 3050 "#define A B\n" 3051 " withMoreParamters,\n" 3052 " whichStronglyInfluenceTheLayout),\n" 3053 " andMoreParameters),\n" 3054 " trailing);", 3055 getLLVMStyleWithColumns(69)); 3056 verifyFormat("Foo::Foo()\n" 3057 "#ifdef BAR\n" 3058 " : baz(0)\n" 3059 "#endif\n" 3060 "{\n" 3061 "}"); 3062 verifyFormat("void f() {\n" 3063 " if (true)\n" 3064 "#ifdef A\n" 3065 " f(42);\n" 3066 " x();\n" 3067 "#else\n" 3068 " g();\n" 3069 " x();\n" 3070 "#endif\n" 3071 "}"); 3072 verifyFormat("void f(param1, param2,\n" 3073 " param3,\n" 3074 "#ifdef A\n" 3075 " param4(param5,\n" 3076 "#ifdef A1\n" 3077 " param6,\n" 3078 "#ifdef A2\n" 3079 " param7),\n" 3080 "#else\n" 3081 " param8),\n" 3082 " param9,\n" 3083 "#endif\n" 3084 " param10,\n" 3085 "#endif\n" 3086 " param11)\n" 3087 "#else\n" 3088 " param12)\n" 3089 "#endif\n" 3090 "{\n" 3091 " x();\n" 3092 "}", 3093 getLLVMStyleWithColumns(28)); 3094 verifyFormat("#if 1\n" 3095 "int i;"); 3096 verifyFormat("#if 1\n" 3097 "#endif\n" 3098 "#if 1\n" 3099 "#else\n" 3100 "#endif\n"); 3101 verifyFormat("DEBUG({\n" 3102 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3104 "});\n" 3105 "#if a\n" 3106 "#else\n" 3107 "#endif"); 3108 3109 verifyIncompleteFormat("void f(\n" 3110 "#if A\n" 3111 " );\n" 3112 "#else\n" 3113 "#endif"); 3114 } 3115 3116 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3117 verifyFormat("#endif\n" 3118 "#if B"); 3119 } 3120 3121 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3122 FormatStyle SingleLine = getLLVMStyle(); 3123 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3124 verifyFormat("#if 0\n" 3125 "#elif 1\n" 3126 "#endif\n" 3127 "void foo() {\n" 3128 " if (test) foo2();\n" 3129 "}", 3130 SingleLine); 3131 } 3132 3133 TEST_F(FormatTest, LayoutBlockInsideParens) { 3134 verifyFormat("functionCall({ int i; });"); 3135 verifyFormat("functionCall({\n" 3136 " int i;\n" 3137 " int j;\n" 3138 "});"); 3139 verifyFormat("functionCall(\n" 3140 " {\n" 3141 " int i;\n" 3142 " int j;\n" 3143 " },\n" 3144 " aaaa, bbbb, cccc);"); 3145 verifyFormat("functionA(functionB({\n" 3146 " int i;\n" 3147 " int j;\n" 3148 " }),\n" 3149 " aaaa, bbbb, cccc);"); 3150 verifyFormat("functionCall(\n" 3151 " {\n" 3152 " int i;\n" 3153 " int j;\n" 3154 " },\n" 3155 " aaaa, bbbb, // comment\n" 3156 " cccc);"); 3157 verifyFormat("functionA(functionB({\n" 3158 " int i;\n" 3159 " int j;\n" 3160 " }),\n" 3161 " aaaa, bbbb, // comment\n" 3162 " cccc);"); 3163 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3164 verifyFormat("functionCall(aaaa, bbbb, {\n" 3165 " int i;\n" 3166 " int j;\n" 3167 "});"); 3168 verifyFormat( 3169 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3170 " {\n" 3171 " int i; // break\n" 3172 " },\n" 3173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3174 " ccccccccccccccccc));"); 3175 verifyFormat("DEBUG({\n" 3176 " if (a)\n" 3177 " f();\n" 3178 "});"); 3179 } 3180 3181 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3182 EXPECT_EQ("SOME_MACRO { int i; }\n" 3183 "int i;", 3184 format(" SOME_MACRO {int i;} int i;")); 3185 } 3186 3187 TEST_F(FormatTest, LayoutNestedBlocks) { 3188 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3189 " struct s {\n" 3190 " int i;\n" 3191 " };\n" 3192 " s kBitsToOs[] = {{10}};\n" 3193 " for (int i = 0; i < 10; ++i)\n" 3194 " return;\n" 3195 "}"); 3196 verifyFormat("call(parameter, {\n" 3197 " something();\n" 3198 " // Comment using all columns.\n" 3199 " somethingelse();\n" 3200 "});", 3201 getLLVMStyleWithColumns(40)); 3202 verifyFormat("DEBUG( //\n" 3203 " { f(); }, a);"); 3204 verifyFormat("DEBUG( //\n" 3205 " {\n" 3206 " f(); //\n" 3207 " },\n" 3208 " a);"); 3209 3210 EXPECT_EQ("call(parameter, {\n" 3211 " something();\n" 3212 " // Comment too\n" 3213 " // looooooooooong.\n" 3214 " somethingElse();\n" 3215 "});", 3216 format("call(parameter, {\n" 3217 " something();\n" 3218 " // Comment too looooooooooong.\n" 3219 " somethingElse();\n" 3220 "});", 3221 getLLVMStyleWithColumns(29))); 3222 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3223 EXPECT_EQ("DEBUG({ // comment\n" 3224 " int i;\n" 3225 "});", 3226 format("DEBUG({ // comment\n" 3227 "int i;\n" 3228 "});")); 3229 EXPECT_EQ("DEBUG({\n" 3230 " int i;\n" 3231 "\n" 3232 " // comment\n" 3233 " int j;\n" 3234 "});", 3235 format("DEBUG({\n" 3236 " int i;\n" 3237 "\n" 3238 " // comment\n" 3239 " int j;\n" 3240 "});")); 3241 3242 verifyFormat("DEBUG({\n" 3243 " if (a)\n" 3244 " return;\n" 3245 "});"); 3246 verifyGoogleFormat("DEBUG({\n" 3247 " if (a) return;\n" 3248 "});"); 3249 FormatStyle Style = getGoogleStyle(); 3250 Style.ColumnLimit = 45; 3251 verifyFormat("Debug(aaaaa,\n" 3252 " {\n" 3253 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3254 " },\n" 3255 " a);", 3256 Style); 3257 3258 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3259 3260 verifyNoCrash("^{v^{a}}"); 3261 } 3262 3263 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3264 EXPECT_EQ("#define MACRO() \\\n" 3265 " Debug(aaa, /* force line break */ \\\n" 3266 " { \\\n" 3267 " int i; \\\n" 3268 " int j; \\\n" 3269 " })", 3270 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3271 " { int i; int j; })", 3272 getGoogleStyle())); 3273 3274 EXPECT_EQ("#define A \\\n" 3275 " [] { \\\n" 3276 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3277 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3278 " }", 3279 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3280 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3281 getGoogleStyle())); 3282 } 3283 3284 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3285 EXPECT_EQ("{}", format("{}")); 3286 verifyFormat("enum E {};"); 3287 verifyFormat("enum E {}"); 3288 } 3289 3290 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3291 FormatStyle Style = getLLVMStyle(); 3292 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3293 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3294 verifyFormat("FOO_BEGIN\n" 3295 " FOO_ENTRY\n" 3296 "FOO_END", Style); 3297 verifyFormat("FOO_BEGIN\n" 3298 " NESTED_FOO_BEGIN\n" 3299 " NESTED_FOO_ENTRY\n" 3300 " NESTED_FOO_END\n" 3301 "FOO_END", Style); 3302 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3303 " int x;\n" 3304 " x = 1;\n" 3305 "FOO_END(Baz)", Style); 3306 } 3307 3308 //===----------------------------------------------------------------------===// 3309 // Line break tests. 3310 //===----------------------------------------------------------------------===// 3311 3312 TEST_F(FormatTest, PreventConfusingIndents) { 3313 verifyFormat( 3314 "void f() {\n" 3315 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3316 " parameter, parameter, parameter)),\n" 3317 " SecondLongCall(parameter));\n" 3318 "}"); 3319 verifyFormat( 3320 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3321 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3323 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3324 verifyFormat( 3325 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3326 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3327 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3328 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3329 verifyFormat( 3330 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3331 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3333 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3334 verifyFormat("int a = bbbb && ccc && fffff(\n" 3335 "#define A Just forcing a new line\n" 3336 " ddd);"); 3337 } 3338 3339 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3340 verifyFormat( 3341 "bool aaaaaaa =\n" 3342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3343 " bbbbbbbb();"); 3344 verifyFormat( 3345 "bool aaaaaaa =\n" 3346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3347 " bbbbbbbb();"); 3348 3349 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3351 " ccccccccc == ddddddddddd;"); 3352 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3354 " ccccccccc == ddddddddddd;"); 3355 verifyFormat( 3356 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3358 " ccccccccc == ddddddddddd;"); 3359 3360 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3361 " aaaaaa) &&\n" 3362 " bbbbbb && cccccc;"); 3363 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3364 " aaaaaa) >>\n" 3365 " bbbbbb;"); 3366 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3367 " SourceMgr.getSpellingColumnNumber(\n" 3368 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3369 " 1);"); 3370 3371 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3372 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3373 " cccccc) {\n}"); 3374 verifyFormat("b = a &&\n" 3375 " // Comment\n" 3376 " b.c && d;"); 3377 3378 // If the LHS of a comparison is not a binary expression itself, the 3379 // additional linebreak confuses many people. 3380 verifyFormat( 3381 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3383 "}"); 3384 verifyFormat( 3385 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3387 "}"); 3388 verifyFormat( 3389 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3391 "}"); 3392 // Even explicit parentheses stress the precedence enough to make the 3393 // additional break unnecessary. 3394 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3396 "}"); 3397 // This cases is borderline, but with the indentation it is still readable. 3398 verifyFormat( 3399 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3400 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3402 "}", 3403 getLLVMStyleWithColumns(75)); 3404 3405 // If the LHS is a binary expression, we should still use the additional break 3406 // as otherwise the formatting hides the operator precedence. 3407 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3409 " 5) {\n" 3410 "}"); 3411 3412 FormatStyle OnePerLine = getLLVMStyle(); 3413 OnePerLine.BinPackParameters = false; 3414 verifyFormat( 3415 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3418 OnePerLine); 3419 } 3420 3421 TEST_F(FormatTest, ExpressionIndentation) { 3422 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3426 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3427 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3430 " ccccccccccccccccccccccccccccccccccccccccc;"); 3431 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3434 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3435 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3438 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3439 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3442 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3443 verifyFormat("if () {\n" 3444 "} else if (aaaaa &&\n" 3445 " bbbbb > // break\n" 3446 " ccccc) {\n" 3447 "}"); 3448 3449 // Presence of a trailing comment used to change indentation of b. 3450 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3451 " b;\n" 3452 "return aaaaaaaaaaaaaaaaaaa +\n" 3453 " b; //", 3454 getLLVMStyleWithColumns(30)); 3455 } 3456 3457 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3458 // Not sure what the best system is here. Like this, the LHS can be found 3459 // immediately above an operator (everything with the same or a higher 3460 // indent). The RHS is aligned right of the operator and so compasses 3461 // everything until something with the same indent as the operator is found. 3462 // FIXME: Is this a good system? 3463 FormatStyle Style = getLLVMStyle(); 3464 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3465 verifyFormat( 3466 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3467 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3468 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3469 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3470 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3471 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3472 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3473 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3474 " > ccccccccccccccccccccccccccccccccccccccccc;", 3475 Style); 3476 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3477 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3478 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3479 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3480 Style); 3481 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3482 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3483 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3484 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3485 Style); 3486 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3487 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3488 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3489 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3490 Style); 3491 verifyFormat("if () {\n" 3492 "} else if (aaaaa\n" 3493 " && bbbbb // break\n" 3494 " > ccccc) {\n" 3495 "}", 3496 Style); 3497 verifyFormat("return (a)\n" 3498 " // comment\n" 3499 " + b;", 3500 Style); 3501 verifyFormat( 3502 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3503 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3504 " + cc;", 3505 Style); 3506 3507 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3508 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3509 Style); 3510 3511 // Forced by comments. 3512 verifyFormat( 3513 "unsigned ContentSize =\n" 3514 " sizeof(int16_t) // DWARF ARange version number\n" 3515 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3516 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3517 " + sizeof(int8_t); // Segment Size (in bytes)"); 3518 3519 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3520 " == boost::fusion::at_c<1>(iiii).second;", 3521 Style); 3522 3523 Style.ColumnLimit = 60; 3524 verifyFormat("zzzzzzzzzz\n" 3525 " = bbbbbbbbbbbbbbbbb\n" 3526 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3527 Style); 3528 } 3529 3530 TEST_F(FormatTest, NoOperandAlignment) { 3531 FormatStyle Style = getLLVMStyle(); 3532 Style.AlignOperands = false; 3533 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3534 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3535 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3537 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3538 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3539 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3540 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3541 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3542 " > ccccccccccccccccccccccccccccccccccccccccc;", 3543 Style); 3544 3545 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3546 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3547 " + cc;", 3548 Style); 3549 verifyFormat("int a = aa\n" 3550 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3551 " * cccccccccccccccccccccccccccccccccccc;", 3552 Style); 3553 3554 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3555 verifyFormat("return (a > b\n" 3556 " // comment1\n" 3557 " // comment2\n" 3558 " || c);", 3559 Style); 3560 } 3561 3562 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3563 FormatStyle Style = getLLVMStyle(); 3564 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3565 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3567 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3568 Style); 3569 } 3570 3571 TEST_F(FormatTest, ConstructorInitializers) { 3572 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3573 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3574 getLLVMStyleWithColumns(45)); 3575 verifyFormat("Constructor()\n" 3576 " : Inttializer(FitsOnTheLine) {}", 3577 getLLVMStyleWithColumns(44)); 3578 verifyFormat("Constructor()\n" 3579 " : Inttializer(FitsOnTheLine) {}", 3580 getLLVMStyleWithColumns(43)); 3581 3582 verifyFormat("template <typename T>\n" 3583 "Constructor() : Initializer(FitsOnTheLine) {}", 3584 getLLVMStyleWithColumns(45)); 3585 3586 verifyFormat( 3587 "SomeClass::Constructor()\n" 3588 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3589 3590 verifyFormat( 3591 "SomeClass::Constructor()\n" 3592 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3593 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3594 verifyFormat( 3595 "SomeClass::Constructor()\n" 3596 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3597 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3598 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3599 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3600 " : aaaaaaaaaa(aaaaaa) {}"); 3601 3602 verifyFormat("Constructor()\n" 3603 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3604 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3605 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3606 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3607 3608 verifyFormat("Constructor()\n" 3609 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3611 3612 verifyFormat("Constructor(int Parameter = 0)\n" 3613 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3614 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3615 verifyFormat("Constructor()\n" 3616 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3617 "}", 3618 getLLVMStyleWithColumns(60)); 3619 verifyFormat("Constructor()\n" 3620 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3621 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3622 3623 // Here a line could be saved by splitting the second initializer onto two 3624 // lines, but that is not desirable. 3625 verifyFormat("Constructor()\n" 3626 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3627 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3628 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3629 3630 FormatStyle OnePerLine = getLLVMStyle(); 3631 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3632 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3633 verifyFormat("SomeClass::Constructor()\n" 3634 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3635 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3636 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3637 OnePerLine); 3638 verifyFormat("SomeClass::Constructor()\n" 3639 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3640 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3641 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3642 OnePerLine); 3643 verifyFormat("MyClass::MyClass(int var)\n" 3644 " : some_var_(var), // 4 space indent\n" 3645 " some_other_var_(var + 1) { // lined up\n" 3646 "}", 3647 OnePerLine); 3648 verifyFormat("Constructor()\n" 3649 " : aaaaa(aaaaaa),\n" 3650 " aaaaa(aaaaaa),\n" 3651 " aaaaa(aaaaaa),\n" 3652 " aaaaa(aaaaaa),\n" 3653 " aaaaa(aaaaaa) {}", 3654 OnePerLine); 3655 verifyFormat("Constructor()\n" 3656 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3657 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3658 OnePerLine); 3659 OnePerLine.BinPackParameters = false; 3660 verifyFormat( 3661 "Constructor()\n" 3662 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3663 " aaaaaaaaaaa().aaa(),\n" 3664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3665 OnePerLine); 3666 OnePerLine.ColumnLimit = 60; 3667 verifyFormat("Constructor()\n" 3668 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3669 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3670 OnePerLine); 3671 3672 EXPECT_EQ("Constructor()\n" 3673 " : // Comment forcing unwanted break.\n" 3674 " aaaa(aaaa) {}", 3675 format("Constructor() :\n" 3676 " // Comment forcing unwanted break.\n" 3677 " aaaa(aaaa) {}")); 3678 } 3679 3680 TEST_F(FormatTest, MemoizationTests) { 3681 // This breaks if the memoization lookup does not take \c Indent and 3682 // \c LastSpace into account. 3683 verifyFormat( 3684 "extern CFRunLoopTimerRef\n" 3685 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3686 " CFTimeInterval interval, CFOptionFlags flags,\n" 3687 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3688 " CFRunLoopTimerContext *context) {}"); 3689 3690 // Deep nesting somewhat works around our memoization. 3691 verifyFormat( 3692 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3693 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3694 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3695 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3696 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3697 getLLVMStyleWithColumns(65)); 3698 verifyFormat( 3699 "aaaaa(\n" 3700 " aaaaa,\n" 3701 " aaaaa(\n" 3702 " aaaaa,\n" 3703 " aaaaa(\n" 3704 " aaaaa,\n" 3705 " aaaaa(\n" 3706 " aaaaa,\n" 3707 " aaaaa(\n" 3708 " aaaaa,\n" 3709 " aaaaa(\n" 3710 " aaaaa,\n" 3711 " aaaaa(\n" 3712 " aaaaa,\n" 3713 " aaaaa(\n" 3714 " aaaaa,\n" 3715 " aaaaa(\n" 3716 " aaaaa,\n" 3717 " aaaaa(\n" 3718 " aaaaa,\n" 3719 " aaaaa(\n" 3720 " aaaaa,\n" 3721 " aaaaa(\n" 3722 " aaaaa,\n" 3723 " aaaaa))))))))))));", 3724 getLLVMStyleWithColumns(65)); 3725 verifyFormat( 3726 "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" 3727 " a),\n" 3728 " a),\n" 3729 " a),\n" 3730 " a),\n" 3731 " a),\n" 3732 " a),\n" 3733 " a),\n" 3734 " a),\n" 3735 " a),\n" 3736 " a),\n" 3737 " a),\n" 3738 " a),\n" 3739 " a),\n" 3740 " a),\n" 3741 " a),\n" 3742 " a),\n" 3743 " a)", 3744 getLLVMStyleWithColumns(65)); 3745 3746 // This test takes VERY long when memoization is broken. 3747 FormatStyle OnePerLine = getLLVMStyle(); 3748 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3749 OnePerLine.BinPackParameters = false; 3750 std::string input = "Constructor()\n" 3751 " : aaaa(a,\n"; 3752 for (unsigned i = 0, e = 80; i != e; ++i) { 3753 input += " a,\n"; 3754 } 3755 input += " a) {}"; 3756 verifyFormat(input, OnePerLine); 3757 } 3758 3759 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3760 verifyFormat( 3761 "void f() {\n" 3762 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3763 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3764 " f();\n" 3765 "}"); 3766 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3767 " Intervals[i - 1].getRange().getLast()) {\n}"); 3768 } 3769 3770 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3771 // Principially, we break function declarations in a certain order: 3772 // 1) break amongst arguments. 3773 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3774 " Cccccccccccccc cccccccccccccc);"); 3775 verifyFormat("template <class TemplateIt>\n" 3776 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3777 " TemplateIt *stop) {}"); 3778 3779 // 2) break after return type. 3780 verifyFormat( 3781 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3782 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3783 getGoogleStyle()); 3784 3785 // 3) break after (. 3786 verifyFormat( 3787 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3788 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3789 getGoogleStyle()); 3790 3791 // 4) break before after nested name specifiers. 3792 verifyFormat( 3793 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3794 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3795 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3796 getGoogleStyle()); 3797 3798 // However, there are exceptions, if a sufficient amount of lines can be 3799 // saved. 3800 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3801 // more adjusting. 3802 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3803 " Cccccccccccccc cccccccccc,\n" 3804 " Cccccccccccccc cccccccccc,\n" 3805 " Cccccccccccccc cccccccccc,\n" 3806 " Cccccccccccccc cccccccccc);"); 3807 verifyFormat( 3808 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3809 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3810 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3811 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3812 getGoogleStyle()); 3813 verifyFormat( 3814 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3815 " Cccccccccccccc cccccccccc,\n" 3816 " Cccccccccccccc cccccccccc,\n" 3817 " Cccccccccccccc cccccccccc,\n" 3818 " Cccccccccccccc cccccccccc,\n" 3819 " Cccccccccccccc cccccccccc,\n" 3820 " Cccccccccccccc cccccccccc);"); 3821 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3822 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3823 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3824 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3825 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3826 3827 // Break after multi-line parameters. 3828 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3831 " bbbb bbbb);"); 3832 verifyFormat("void SomeLoooooooooooongFunction(\n" 3833 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3834 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3835 " int bbbbbbbbbbbbb);"); 3836 3837 // Treat overloaded operators like other functions. 3838 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3839 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3840 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3841 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3842 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3843 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3844 verifyGoogleFormat( 3845 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3846 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3847 verifyGoogleFormat( 3848 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3849 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3850 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3851 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3852 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3853 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3854 verifyGoogleFormat( 3855 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3856 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3857 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3858 verifyGoogleFormat( 3859 "template <typename T>\n" 3860 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3861 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3862 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3863 3864 FormatStyle Style = getLLVMStyle(); 3865 Style.PointerAlignment = FormatStyle::PAS_Left; 3866 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3867 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3868 Style); 3869 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3871 Style); 3872 } 3873 3874 TEST_F(FormatTest, TrailingReturnType) { 3875 verifyFormat("auto foo() -> int;\n"); 3876 verifyFormat("struct S {\n" 3877 " auto bar() const -> int;\n" 3878 "};"); 3879 verifyFormat("template <size_t Order, typename T>\n" 3880 "auto load_img(const std::string &filename)\n" 3881 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3882 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3883 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3884 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3885 verifyFormat("template <typename T>\n" 3886 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3887 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3888 3889 // Not trailing return types. 3890 verifyFormat("void f() { auto a = b->c(); }"); 3891 } 3892 3893 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3894 // Avoid breaking before trailing 'const' or other trailing annotations, if 3895 // they are not function-like. 3896 FormatStyle Style = getGoogleStyle(); 3897 Style.ColumnLimit = 47; 3898 verifyFormat("void someLongFunction(\n" 3899 " int someLoooooooooooooongParameter) const {\n}", 3900 getLLVMStyleWithColumns(47)); 3901 verifyFormat("LoooooongReturnType\n" 3902 "someLoooooooongFunction() const {}", 3903 getLLVMStyleWithColumns(47)); 3904 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3905 " const {}", 3906 Style); 3907 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3908 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3909 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3910 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3911 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3912 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3913 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3914 " aaaaaaaaaaa aaaaa) const override;"); 3915 verifyGoogleFormat( 3916 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3917 " const override;"); 3918 3919 // Even if the first parameter has to be wrapped. 3920 verifyFormat("void someLongFunction(\n" 3921 " int someLongParameter) const {}", 3922 getLLVMStyleWithColumns(46)); 3923 verifyFormat("void someLongFunction(\n" 3924 " int someLongParameter) const {}", 3925 Style); 3926 verifyFormat("void someLongFunction(\n" 3927 " int someLongParameter) override {}", 3928 Style); 3929 verifyFormat("void someLongFunction(\n" 3930 " int someLongParameter) OVERRIDE {}", 3931 Style); 3932 verifyFormat("void someLongFunction(\n" 3933 " int someLongParameter) final {}", 3934 Style); 3935 verifyFormat("void someLongFunction(\n" 3936 " int someLongParameter) FINAL {}", 3937 Style); 3938 verifyFormat("void someLongFunction(\n" 3939 " int parameter) const override {}", 3940 Style); 3941 3942 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3943 verifyFormat("void someLongFunction(\n" 3944 " int someLongParameter) const\n" 3945 "{\n" 3946 "}", 3947 Style); 3948 3949 // Unless these are unknown annotations. 3950 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3951 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3952 " LONG_AND_UGLY_ANNOTATION;"); 3953 3954 // Breaking before function-like trailing annotations is fine to keep them 3955 // close to their arguments. 3956 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3957 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3958 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3959 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3960 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3961 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3962 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3963 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3964 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3965 3966 verifyFormat( 3967 "void aaaaaaaaaaaaaaaaaa()\n" 3968 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3969 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3970 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3971 " __attribute__((unused));"); 3972 verifyGoogleFormat( 3973 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3974 " GUARDED_BY(aaaaaaaaaaaa);"); 3975 verifyGoogleFormat( 3976 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3977 " GUARDED_BY(aaaaaaaaaaaa);"); 3978 verifyGoogleFormat( 3979 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3980 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3981 verifyGoogleFormat( 3982 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3983 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3984 } 3985 3986 TEST_F(FormatTest, FunctionAnnotations) { 3987 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3988 "int OldFunction(const string ¶meter) {}"); 3989 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3990 "string OldFunction(const string ¶meter) {}"); 3991 verifyFormat("template <typename T>\n" 3992 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3993 "string OldFunction(const string ¶meter) {}"); 3994 3995 // Not function annotations. 3996 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3997 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3998 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3999 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4000 } 4001 4002 TEST_F(FormatTest, BreaksDesireably) { 4003 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4004 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4005 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4006 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4008 "}"); 4009 4010 verifyFormat( 4011 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4013 4014 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4017 4018 verifyFormat( 4019 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4021 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4023 4024 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4025 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4026 4027 verifyFormat( 4028 "void f() {\n" 4029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4031 "}"); 4032 verifyFormat( 4033 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4035 verifyFormat( 4036 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4038 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4041 4042 // Indent consistently independent of call expression and unary operator. 4043 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4044 " dddddddddddddddddddddddddddddd));"); 4045 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4046 " dddddddddddddddddddddddddddddd));"); 4047 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4048 " dddddddddddddddddddddddddddddd));"); 4049 4050 // This test case breaks on an incorrect memoization, i.e. an optimization not 4051 // taking into account the StopAt value. 4052 verifyFormat( 4053 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4054 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4055 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4056 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4057 4058 verifyFormat("{\n {\n {\n" 4059 " Annotation.SpaceRequiredBefore =\n" 4060 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4061 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4062 " }\n }\n}"); 4063 4064 // Break on an outer level if there was a break on an inner level. 4065 EXPECT_EQ("f(g(h(a, // comment\n" 4066 " b, c),\n" 4067 " d, e),\n" 4068 " x, y);", 4069 format("f(g(h(a, // comment\n" 4070 " b, c), d, e), x, y);")); 4071 4072 // Prefer breaking similar line breaks. 4073 verifyFormat( 4074 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4075 " NSTrackingMouseEnteredAndExited |\n" 4076 " NSTrackingActiveAlways;"); 4077 } 4078 4079 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4080 FormatStyle NoBinPacking = getGoogleStyle(); 4081 NoBinPacking.BinPackParameters = false; 4082 NoBinPacking.BinPackArguments = true; 4083 verifyFormat("void f() {\n" 4084 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4086 "}", 4087 NoBinPacking); 4088 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4089 " int aaaaaaaaaaaaaaaaaaaa,\n" 4090 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4091 NoBinPacking); 4092 4093 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4094 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4095 " vector<int> bbbbbbbbbbbbbbb);", 4096 NoBinPacking); 4097 // FIXME: This behavior difference is probably not wanted. However, currently 4098 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4099 // template arguments from BreakBeforeParameter being set because of the 4100 // one-per-line formatting. 4101 verifyFormat( 4102 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4103 " aaaaaaaaaa> aaaaaaaaaa);", 4104 NoBinPacking); 4105 verifyFormat( 4106 "void fffffffffff(\n" 4107 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4108 " aaaaaaaaaa);"); 4109 } 4110 4111 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4112 FormatStyle NoBinPacking = getGoogleStyle(); 4113 NoBinPacking.BinPackParameters = false; 4114 NoBinPacking.BinPackArguments = false; 4115 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4116 " aaaaaaaaaaaaaaaaaaaa,\n" 4117 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4118 NoBinPacking); 4119 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4120 " aaaaaaaaaaaaa,\n" 4121 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4122 NoBinPacking); 4123 verifyFormat( 4124 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4125 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4127 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4129 NoBinPacking); 4130 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4131 " .aaaaaaaaaaaaaaaaaa();", 4132 NoBinPacking); 4133 verifyFormat("void f() {\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4135 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4136 "}", 4137 NoBinPacking); 4138 4139 verifyFormat( 4140 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4141 " aaaaaaaaaaaa,\n" 4142 " aaaaaaaaaaaa);", 4143 NoBinPacking); 4144 verifyFormat( 4145 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4146 " ddddddddddddddddddddddddddddd),\n" 4147 " test);", 4148 NoBinPacking); 4149 4150 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4151 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4152 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4153 " aaaaaaaaaaaaaaaaaa;", 4154 NoBinPacking); 4155 verifyFormat("a(\"a\"\n" 4156 " \"a\",\n" 4157 " a);"); 4158 4159 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4160 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4161 " aaaaaaaaa,\n" 4162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4163 NoBinPacking); 4164 verifyFormat( 4165 "void f() {\n" 4166 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4167 " .aaaaaaa();\n" 4168 "}", 4169 NoBinPacking); 4170 verifyFormat( 4171 "template <class SomeType, class SomeOtherType>\n" 4172 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4173 NoBinPacking); 4174 } 4175 4176 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4177 FormatStyle Style = getLLVMStyleWithColumns(15); 4178 Style.ExperimentalAutoDetectBinPacking = true; 4179 EXPECT_EQ("aaa(aaaa,\n" 4180 " aaaa,\n" 4181 " aaaa);\n" 4182 "aaa(aaaa,\n" 4183 " aaaa,\n" 4184 " aaaa);", 4185 format("aaa(aaaa,\n" // one-per-line 4186 " aaaa,\n" 4187 " aaaa );\n" 4188 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4189 Style)); 4190 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4191 " aaaa);\n" 4192 "aaa(aaaa, aaaa,\n" 4193 " aaaa);", 4194 format("aaa(aaaa, aaaa,\n" // bin-packed 4195 " aaaa );\n" 4196 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4197 Style)); 4198 } 4199 4200 TEST_F(FormatTest, FormatsBuilderPattern) { 4201 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4202 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4203 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4204 " .StartsWith(\".init\", ORDER_INIT)\n" 4205 " .StartsWith(\".fini\", ORDER_FINI)\n" 4206 " .StartsWith(\".hash\", ORDER_HASH)\n" 4207 " .Default(ORDER_TEXT);\n"); 4208 4209 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4210 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4211 verifyFormat( 4212 "aaaaaaa->aaaaaaa\n" 4213 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4215 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4216 verifyFormat( 4217 "aaaaaaa->aaaaaaa\n" 4218 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4219 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4220 verifyFormat( 4221 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4222 " aaaaaaaaaaaaaa);"); 4223 verifyFormat( 4224 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4225 " aaaaaa->aaaaaaaaaaaa()\n" 4226 " ->aaaaaaaaaaaaaaaa(\n" 4227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4228 " ->aaaaaaaaaaaaaaaaa();"); 4229 verifyGoogleFormat( 4230 "void f() {\n" 4231 " someo->Add((new util::filetools::Handler(dir))\n" 4232 " ->OnEvent1(NewPermanentCallback(\n" 4233 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4234 " ->OnEvent2(NewPermanentCallback(\n" 4235 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4236 " ->OnEvent3(NewPermanentCallback(\n" 4237 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4238 " ->OnEvent5(NewPermanentCallback(\n" 4239 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4240 " ->OnEvent6(NewPermanentCallback(\n" 4241 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4242 "}"); 4243 4244 verifyFormat( 4245 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4246 verifyFormat("aaaaaaaaaaaaaaa()\n" 4247 " .aaaaaaaaaaaaaaa()\n" 4248 " .aaaaaaaaaaaaaaa()\n" 4249 " .aaaaaaaaaaaaaaa()\n" 4250 " .aaaaaaaaaaaaaaa();"); 4251 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4252 " .aaaaaaaaaaaaaaa()\n" 4253 " .aaaaaaaaaaaaaaa()\n" 4254 " .aaaaaaaaaaaaaaa();"); 4255 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4256 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4257 " .aaaaaaaaaaaaaaa();"); 4258 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4259 " ->aaaaaaaaaaaaaae(0)\n" 4260 " ->aaaaaaaaaaaaaaa();"); 4261 4262 // Don't linewrap after very short segments. 4263 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4264 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4265 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4266 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4267 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4268 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4269 verifyFormat("aaa()\n" 4270 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4271 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4272 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4273 4274 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4275 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4276 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4277 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4278 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4280 4281 // Prefer not to break after empty parentheses. 4282 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4283 " First->LastNewlineOffset);"); 4284 4285 // Prefer not to create "hanging" indents. 4286 verifyFormat( 4287 "return !soooooooooooooome_map\n" 4288 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4289 " .second;"); 4290 verifyFormat( 4291 "return aaaaaaaaaaaaaaaa\n" 4292 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4293 " .aaaa(aaaaaaaaaaaaaa);"); 4294 // No hanging indent here. 4295 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4297 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4299 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4300 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4301 getLLVMStyleWithColumns(60)); 4302 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4303 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4304 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4305 getLLVMStyleWithColumns(59)); 4306 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4308 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4309 } 4310 4311 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4312 verifyFormat( 4313 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4314 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4315 verifyFormat( 4316 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4317 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4318 4319 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4320 " ccccccccccccccccccccccccc) {\n}"); 4321 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4322 " ccccccccccccccccccccccccc) {\n}"); 4323 4324 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4325 " ccccccccccccccccccccccccc) {\n}"); 4326 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4327 " ccccccccccccccccccccccccc) {\n}"); 4328 4329 verifyFormat( 4330 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4331 " ccccccccccccccccccccccccc) {\n}"); 4332 verifyFormat( 4333 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4334 " ccccccccccccccccccccccccc) {\n}"); 4335 4336 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4337 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4338 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4339 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4340 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4341 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4342 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4343 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4344 4345 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4346 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4347 " aaaaaaaaaaaaaaa != aa) {\n}"); 4348 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4349 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4350 " aaaaaaaaaaaaaaa != aa) {\n}"); 4351 } 4352 4353 TEST_F(FormatTest, BreaksAfterAssignments) { 4354 verifyFormat( 4355 "unsigned Cost =\n" 4356 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4357 " SI->getPointerAddressSpaceee());\n"); 4358 verifyFormat( 4359 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4360 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4361 4362 verifyFormat( 4363 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4364 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4365 verifyFormat("unsigned OriginalStartColumn =\n" 4366 " SourceMgr.getSpellingColumnNumber(\n" 4367 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4368 " 1;"); 4369 } 4370 4371 TEST_F(FormatTest, AlignsAfterAssignments) { 4372 verifyFormat( 4373 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4374 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4375 verifyFormat( 4376 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4377 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4378 verifyFormat( 4379 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4380 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4381 verifyFormat( 4382 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4383 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4384 verifyFormat( 4385 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4387 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4388 } 4389 4390 TEST_F(FormatTest, AlignsAfterReturn) { 4391 verifyFormat( 4392 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4393 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4394 verifyFormat( 4395 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4396 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4397 verifyFormat( 4398 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4399 " aaaaaaaaaaaaaaaaaaaaaa();"); 4400 verifyFormat( 4401 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4402 " aaaaaaaaaaaaaaaaaaaaaa());"); 4403 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4405 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4408 verifyFormat("return\n" 4409 " // true if code is one of a or b.\n" 4410 " code == a || code == b;"); 4411 } 4412 4413 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4414 verifyFormat( 4415 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4416 " aaaaaaaaa aaaaaaa) {}"); 4417 verifyFormat( 4418 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4419 " aaaaaaaaaaa aaaaaaaaa);"); 4420 verifyFormat( 4421 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4422 " aaaaaaaaaaaaaaaaaaaaa));"); 4423 FormatStyle Style = getLLVMStyle(); 4424 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4425 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4426 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4427 Style); 4428 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4429 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4430 Style); 4431 verifyFormat("SomeLongVariableName->someFunction(\n" 4432 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4433 Style); 4434 verifyFormat( 4435 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4436 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4437 Style); 4438 verifyFormat( 4439 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4440 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4441 Style); 4442 verifyFormat( 4443 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4444 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4445 Style); 4446 4447 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4448 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4449 " b));", 4450 Style); 4451 4452 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4453 Style.BinPackArguments = false; 4454 Style.BinPackParameters = false; 4455 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4456 " aaaaaaaaaaa aaaaaaaa,\n" 4457 " aaaaaaaaa aaaaaaa,\n" 4458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4459 Style); 4460 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4461 " aaaaaaaaaaa aaaaaaaaa,\n" 4462 " aaaaaaaaaaa aaaaaaaaa,\n" 4463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4464 Style); 4465 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4466 " aaaaaaaaaaaaaaa,\n" 4467 " aaaaaaaaaaaaaaaaaaaaa,\n" 4468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4469 Style); 4470 verifyFormat( 4471 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4472 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4473 Style); 4474 verifyFormat( 4475 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4476 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4477 Style); 4478 verifyFormat( 4479 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4480 " aaaaaaaaaaaaaaaaaaaaa(\n" 4481 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4482 " aaaaaaaaaaaaaaaa);", 4483 Style); 4484 verifyFormat( 4485 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4486 " aaaaaaaaaaaaaaaaaaaaa(\n" 4487 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4488 " aaaaaaaaaaaaaaaa);", 4489 Style); 4490 } 4491 4492 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4493 FormatStyle Style = getLLVMStyleWithColumns(40); 4494 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4495 " bbbbbbbbbbbbbbbbbbbbbb);", 4496 Style); 4497 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4498 Style.AlignOperands = false; 4499 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4500 " bbbbbbbbbbbbbbbbbbbbbb);", 4501 Style); 4502 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4503 Style.AlignOperands = true; 4504 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4505 " bbbbbbbbbbbbbbbbbbbbbb);", 4506 Style); 4507 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4508 Style.AlignOperands = false; 4509 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4510 " bbbbbbbbbbbbbbbbbbbbbb);", 4511 Style); 4512 } 4513 4514 TEST_F(FormatTest, BreaksConditionalExpressions) { 4515 verifyFormat( 4516 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4517 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4518 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4519 verifyFormat( 4520 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4521 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4522 verifyFormat( 4523 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4524 " : aaaaaaaaaaaaa);"); 4525 verifyFormat( 4526 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4527 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4528 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4529 " aaaaaaaaaaaaa);"); 4530 verifyFormat( 4531 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4532 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4533 " aaaaaaaaaaaaa);"); 4534 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4535 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4537 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4539 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4541 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4543 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4545 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4546 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4548 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4550 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4551 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4552 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4553 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4554 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4556 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4557 " : aaaaaaaaaaaaaaaa;"); 4558 verifyFormat( 4559 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4560 " ? aaaaaaaaaaaaaaa\n" 4561 " : aaaaaaaaaaaaaaa;"); 4562 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4563 " aaaaaaaaa\n" 4564 " ? b\n" 4565 " : c);"); 4566 verifyFormat("return aaaa == bbbb\n" 4567 " // comment\n" 4568 " ? aaaa\n" 4569 " : bbbb;"); 4570 verifyFormat("unsigned Indent =\n" 4571 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4572 " ? IndentForLevel[TheLine.Level]\n" 4573 " : TheLine * 2,\n" 4574 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4575 getLLVMStyleWithColumns(70)); 4576 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4577 " ? aaaaaaaaaaaaaaa\n" 4578 " : bbbbbbbbbbbbbbb //\n" 4579 " ? ccccccccccccccc\n" 4580 " : ddddddddddddddd;"); 4581 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4582 " ? aaaaaaaaaaaaaaa\n" 4583 " : (bbbbbbbbbbbbbbb //\n" 4584 " ? ccccccccccccccc\n" 4585 " : ddddddddddddddd);"); 4586 verifyFormat( 4587 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4588 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4589 " aaaaaaaaaaaaaaaaaaaaa +\n" 4590 " aaaaaaaaaaaaaaaaaaaaa\n" 4591 " : aaaaaaaaaa;"); 4592 verifyFormat( 4593 "aaaaaa = aaaaaaaaaaaa\n" 4594 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4595 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4596 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4597 4598 FormatStyle NoBinPacking = getLLVMStyle(); 4599 NoBinPacking.BinPackArguments = false; 4600 verifyFormat( 4601 "void f() {\n" 4602 " g(aaa,\n" 4603 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4605 " ? aaaaaaaaaaaaaaa\n" 4606 " : aaaaaaaaaaaaaaa);\n" 4607 "}", 4608 NoBinPacking); 4609 verifyFormat( 4610 "void f() {\n" 4611 " g(aaa,\n" 4612 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4614 " ?: aaaaaaaaaaaaaaa);\n" 4615 "}", 4616 NoBinPacking); 4617 4618 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4619 " // comment.\n" 4620 " ccccccccccccccccccccccccccccccccccccccc\n" 4621 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4622 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4623 4624 // Assignments in conditional expressions. Apparently not uncommon :-(. 4625 verifyFormat("return a != b\n" 4626 " // comment\n" 4627 " ? a = b\n" 4628 " : a = b;"); 4629 verifyFormat("return a != b\n" 4630 " // comment\n" 4631 " ? a = a != b\n" 4632 " // comment\n" 4633 " ? a = b\n" 4634 " : a\n" 4635 " : a;\n"); 4636 verifyFormat("return a != b\n" 4637 " // comment\n" 4638 " ? a\n" 4639 " : a = a != b\n" 4640 " // comment\n" 4641 " ? a = b\n" 4642 " : a;"); 4643 } 4644 4645 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4646 FormatStyle Style = getLLVMStyle(); 4647 Style.BreakBeforeTernaryOperators = false; 4648 Style.ColumnLimit = 70; 4649 verifyFormat( 4650 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4653 Style); 4654 verifyFormat( 4655 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4657 Style); 4658 verifyFormat( 4659 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4660 " aaaaaaaaaaaaa);", 4661 Style); 4662 verifyFormat( 4663 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4664 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4666 " aaaaaaaaaaaaa);", 4667 Style); 4668 verifyFormat( 4669 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4670 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4671 " aaaaaaaaaaaaa);", 4672 Style); 4673 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4678 Style); 4679 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4684 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4685 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4686 Style); 4687 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4691 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4692 Style); 4693 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4694 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4695 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4696 Style); 4697 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4701 Style); 4702 verifyFormat( 4703 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4704 " aaaaaaaaaaaaaaa :\n" 4705 " aaaaaaaaaaaaaaa;", 4706 Style); 4707 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4708 " aaaaaaaaa ?\n" 4709 " b :\n" 4710 " c);", 4711 Style); 4712 verifyFormat( 4713 "unsigned Indent =\n" 4714 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4715 " IndentForLevel[TheLine.Level] :\n" 4716 " TheLine * 2,\n" 4717 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4718 Style); 4719 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4720 " aaaaaaaaaaaaaaa :\n" 4721 " bbbbbbbbbbbbbbb ? //\n" 4722 " ccccccccccccccc :\n" 4723 " ddddddddddddddd;", 4724 Style); 4725 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4726 " aaaaaaaaaaaaaaa :\n" 4727 " (bbbbbbbbbbbbbbb ? //\n" 4728 " ccccccccccccccc :\n" 4729 " ddddddddddddddd);", 4730 Style); 4731 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4732 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4733 " ccccccccccccccccccccccccccc;", 4734 Style); 4735 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4736 " aaaaa :\n" 4737 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4738 Style); 4739 } 4740 4741 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4742 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4743 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4744 verifyFormat("bool a = true, b = false;"); 4745 4746 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4748 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4749 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4750 verifyFormat( 4751 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4752 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4753 " d = e && f;"); 4754 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4755 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4756 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4757 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4758 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4759 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4760 4761 FormatStyle Style = getGoogleStyle(); 4762 Style.PointerAlignment = FormatStyle::PAS_Left; 4763 Style.DerivePointerAlignment = false; 4764 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4765 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4766 " *b = bbbbbbbbbbbbbbbbbbb;", 4767 Style); 4768 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4769 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4770 Style); 4771 } 4772 4773 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4774 verifyFormat("arr[foo ? bar : baz];"); 4775 verifyFormat("f()[foo ? bar : baz];"); 4776 verifyFormat("(a + b)[foo ? bar : baz];"); 4777 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4778 } 4779 4780 TEST_F(FormatTest, AlignsStringLiterals) { 4781 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4782 " \"short literal\");"); 4783 verifyFormat( 4784 "looooooooooooooooooooooooongFunction(\n" 4785 " \"short literal\"\n" 4786 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4787 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4788 " \" string literals\",\n" 4789 " and, other, parameters);"); 4790 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4791 " \"5678\";", 4792 format("fun + \"1243\" /* comment */\n" 4793 " \"5678\";", 4794 getLLVMStyleWithColumns(28))); 4795 EXPECT_EQ( 4796 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4797 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4798 " \"aaaaaaaaaaaaaaaa\";", 4799 format("aaaaaa =" 4800 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4801 "aaaaaaaaaaaaaaaaaaaaa\" " 4802 "\"aaaaaaaaaaaaaaaa\";")); 4803 verifyFormat("a = a + \"a\"\n" 4804 " \"a\"\n" 4805 " \"a\";"); 4806 verifyFormat("f(\"a\", \"b\"\n" 4807 " \"c\");"); 4808 4809 verifyFormat( 4810 "#define LL_FORMAT \"ll\"\n" 4811 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4812 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4813 4814 verifyFormat("#define A(X) \\\n" 4815 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4816 " \"ccccc\"", 4817 getLLVMStyleWithColumns(23)); 4818 verifyFormat("#define A \"def\"\n" 4819 "f(\"abc\" A \"ghi\"\n" 4820 " \"jkl\");"); 4821 4822 verifyFormat("f(L\"a\"\n" 4823 " L\"b\");"); 4824 verifyFormat("#define A(X) \\\n" 4825 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4826 " L\"ccccc\"", 4827 getLLVMStyleWithColumns(25)); 4828 4829 verifyFormat("f(@\"a\"\n" 4830 " @\"b\");"); 4831 verifyFormat("NSString s = @\"a\"\n" 4832 " @\"b\"\n" 4833 " @\"c\";"); 4834 verifyFormat("NSString s = @\"a\"\n" 4835 " \"b\"\n" 4836 " \"c\";"); 4837 } 4838 4839 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4840 FormatStyle Style = getLLVMStyle(); 4841 // No declarations or definitions should be moved to own line. 4842 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4843 verifyFormat("class A {\n" 4844 " int f() { return 1; }\n" 4845 " int g();\n" 4846 "};\n" 4847 "int f() { return 1; }\n" 4848 "int g();\n", 4849 Style); 4850 4851 // All declarations and definitions should have the return type moved to its 4852 // own 4853 // line. 4854 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4855 verifyFormat("class E {\n" 4856 " int\n" 4857 " f() {\n" 4858 " return 1;\n" 4859 " }\n" 4860 " int\n" 4861 " g();\n" 4862 "};\n" 4863 "int\n" 4864 "f() {\n" 4865 " return 1;\n" 4866 "}\n" 4867 "int\n" 4868 "g();\n", 4869 Style); 4870 4871 // Top-level definitions, and no kinds of declarations should have the 4872 // return type moved to its own line. 4873 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4874 verifyFormat("class B {\n" 4875 " int f() { return 1; }\n" 4876 " int g();\n" 4877 "};\n" 4878 "int\n" 4879 "f() {\n" 4880 " return 1;\n" 4881 "}\n" 4882 "int g();\n", 4883 Style); 4884 4885 // Top-level definitions and declarations should have the return type moved 4886 // to its own line. 4887 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4888 verifyFormat("class C {\n" 4889 " int f() { return 1; }\n" 4890 " int g();\n" 4891 "};\n" 4892 "int\n" 4893 "f() {\n" 4894 " return 1;\n" 4895 "}\n" 4896 "int\n" 4897 "g();\n", 4898 Style); 4899 4900 // All definitions should have the return type moved to its own line, but no 4901 // kinds of declarations. 4902 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4903 verifyFormat("class D {\n" 4904 " int\n" 4905 " f() {\n" 4906 " return 1;\n" 4907 " }\n" 4908 " int g();\n" 4909 "};\n" 4910 "int\n" 4911 "f() {\n" 4912 " return 1;\n" 4913 "}\n" 4914 "int g();\n", 4915 Style); 4916 verifyFormat("const char *\n" 4917 "f(void) {\n" // Break here. 4918 " return \"\";\n" 4919 "}\n" 4920 "const char *bar(void);\n", // No break here. 4921 Style); 4922 verifyFormat("template <class T>\n" 4923 "T *\n" 4924 "f(T &c) {\n" // Break here. 4925 " return NULL;\n" 4926 "}\n" 4927 "template <class T> T *f(T &c);\n", // No break here. 4928 Style); 4929 verifyFormat("class C {\n" 4930 " int\n" 4931 " operator+() {\n" 4932 " return 1;\n" 4933 " }\n" 4934 " int\n" 4935 " operator()() {\n" 4936 " return 1;\n" 4937 " }\n" 4938 "};\n", 4939 Style); 4940 verifyFormat("void\n" 4941 "A::operator()() {}\n" 4942 "void\n" 4943 "A::operator>>() {}\n" 4944 "void\n" 4945 "A::operator+() {}\n", 4946 Style); 4947 verifyFormat("void *operator new(std::size_t s);", // No break here. 4948 Style); 4949 verifyFormat("void *\n" 4950 "operator new(std::size_t s) {}", 4951 Style); 4952 verifyFormat("void *\n" 4953 "operator delete[](void *ptr) {}", 4954 Style); 4955 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4956 verifyFormat("const char *\n" 4957 "f(void)\n" // Break here. 4958 "{\n" 4959 " return \"\";\n" 4960 "}\n" 4961 "const char *bar(void);\n", // No break here. 4962 Style); 4963 verifyFormat("template <class T>\n" 4964 "T *\n" // Problem here: no line break 4965 "f(T &c)\n" // Break here. 4966 "{\n" 4967 " return NULL;\n" 4968 "}\n" 4969 "template <class T> T *f(T &c);\n", // No break here. 4970 Style); 4971 } 4972 4973 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4974 FormatStyle NoBreak = getLLVMStyle(); 4975 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4976 FormatStyle Break = getLLVMStyle(); 4977 Break.AlwaysBreakBeforeMultilineStrings = true; 4978 verifyFormat("aaaa = \"bbbb\"\n" 4979 " \"cccc\";", 4980 NoBreak); 4981 verifyFormat("aaaa =\n" 4982 " \"bbbb\"\n" 4983 " \"cccc\";", 4984 Break); 4985 verifyFormat("aaaa(\"bbbb\"\n" 4986 " \"cccc\");", 4987 NoBreak); 4988 verifyFormat("aaaa(\n" 4989 " \"bbbb\"\n" 4990 " \"cccc\");", 4991 Break); 4992 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4993 " \"cccc\");", 4994 NoBreak); 4995 verifyFormat("aaaa(qqq,\n" 4996 " \"bbbb\"\n" 4997 " \"cccc\");", 4998 Break); 4999 verifyFormat("aaaa(qqq,\n" 5000 " L\"bbbb\"\n" 5001 " L\"cccc\");", 5002 Break); 5003 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5004 " \"bbbb\"));", 5005 Break); 5006 verifyFormat("string s = someFunction(\n" 5007 " \"abc\"\n" 5008 " \"abc\");", 5009 Break); 5010 5011 // As we break before unary operators, breaking right after them is bad. 5012 verifyFormat("string foo = abc ? \"x\"\n" 5013 " \"blah blah blah blah blah blah\"\n" 5014 " : \"y\";", 5015 Break); 5016 5017 // Don't break if there is no column gain. 5018 verifyFormat("f(\"aaaa\"\n" 5019 " \"bbbb\");", 5020 Break); 5021 5022 // Treat literals with escaped newlines like multi-line string literals. 5023 EXPECT_EQ("x = \"a\\\n" 5024 "b\\\n" 5025 "c\";", 5026 format("x = \"a\\\n" 5027 "b\\\n" 5028 "c\";", 5029 NoBreak)); 5030 EXPECT_EQ("xxxx =\n" 5031 " \"a\\\n" 5032 "b\\\n" 5033 "c\";", 5034 format("xxxx = \"a\\\n" 5035 "b\\\n" 5036 "c\";", 5037 Break)); 5038 5039 // Exempt ObjC strings for now. 5040 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5041 " @\"bbbb\";", 5042 format("NSString *const kString = @\"aaaa\"\n" 5043 "@\"bbbb\";", 5044 Break)); 5045 5046 Break.ColumnLimit = 0; 5047 verifyFormat("const char *hello = \"hello llvm\";", Break); 5048 } 5049 5050 TEST_F(FormatTest, AlignsPipes) { 5051 verifyFormat( 5052 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5053 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5054 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5055 verifyFormat( 5056 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5057 " << aaaaaaaaaaaaaaaaaaaa;"); 5058 verifyFormat( 5059 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5060 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5061 verifyFormat( 5062 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5063 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5064 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5065 verifyFormat( 5066 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5067 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5068 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5069 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5072 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5073 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5074 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5075 verifyFormat( 5076 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5078 5079 verifyFormat("return out << \"somepacket = {\\n\"\n" 5080 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5081 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5082 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5083 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5084 " << \"}\";"); 5085 5086 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5087 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5088 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5089 verifyFormat( 5090 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5091 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5092 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5093 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5094 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5095 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5096 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5097 verifyFormat( 5098 "void f() {\n" 5099 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5100 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5101 "}"); 5102 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5103 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5104 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5106 " aaaaaaaaaaaaaaaaaaaaa)\n" 5107 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5108 verifyFormat("LOG_IF(aaa == //\n" 5109 " bbb)\n" 5110 " << a << b;"); 5111 5112 // Breaking before the first "<<" is generally not desirable. 5113 verifyFormat( 5114 "llvm::errs()\n" 5115 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5116 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5117 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5118 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5119 getLLVMStyleWithColumns(70)); 5120 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5121 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5122 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5123 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5124 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5125 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5126 getLLVMStyleWithColumns(70)); 5127 5128 // But sometimes, breaking before the first "<<" is desirable. 5129 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5130 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5131 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5132 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5133 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5134 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5135 " << BEF << IsTemplate << Description << E->getType();"); 5136 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5137 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5139 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5140 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5142 " << aaa;"); 5143 5144 verifyFormat( 5145 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5146 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5147 5148 // Incomplete string literal. 5149 EXPECT_EQ("llvm::errs() << \"\n" 5150 " << a;", 5151 format("llvm::errs() << \"\n<<a;")); 5152 5153 verifyFormat("void f() {\n" 5154 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5155 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5156 "}"); 5157 5158 // Handle 'endl'. 5159 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5160 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5161 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5162 5163 // Handle '\n'. 5164 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5165 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5166 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5167 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5168 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5169 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5170 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5171 } 5172 5173 TEST_F(FormatTest, UnderstandsEquals) { 5174 verifyFormat( 5175 "aaaaaaaaaaaaaaaaa =\n" 5176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5177 verifyFormat( 5178 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5180 verifyFormat( 5181 "if (a) {\n" 5182 " f();\n" 5183 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5185 "}"); 5186 5187 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5188 " 100000000 + 10000000) {\n}"); 5189 } 5190 5191 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5192 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5193 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5194 5195 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5196 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5197 5198 verifyFormat( 5199 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5200 " Parameter2);"); 5201 5202 verifyFormat( 5203 "ShortObject->shortFunction(\n" 5204 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5205 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5206 5207 verifyFormat("loooooooooooooongFunction(\n" 5208 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5209 5210 verifyFormat( 5211 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5212 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5213 5214 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5215 " .WillRepeatedly(Return(SomeValue));"); 5216 verifyFormat("void f() {\n" 5217 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5218 " .Times(2)\n" 5219 " .WillRepeatedly(Return(SomeValue));\n" 5220 "}"); 5221 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5222 " ccccccccccccccccccccccc);"); 5223 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5225 " .aaaaa(aaaaa),\n" 5226 " aaaaaaaaaaaaaaaaaaaaa);"); 5227 verifyFormat("void f() {\n" 5228 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5229 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5230 "}"); 5231 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5233 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5234 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5235 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5236 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5237 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5238 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5239 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5240 "}"); 5241 5242 // Here, it is not necessary to wrap at "." or "->". 5243 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5244 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5245 verifyFormat( 5246 "aaaaaaaaaaa->aaaaaaaaa(\n" 5247 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5248 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5249 5250 verifyFormat( 5251 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5253 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5254 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5255 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5256 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5257 5258 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5259 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5260 " .a();"); 5261 5262 FormatStyle NoBinPacking = getLLVMStyle(); 5263 NoBinPacking.BinPackParameters = false; 5264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5265 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5266 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5267 " aaaaaaaaaaaaaaaaaaa,\n" 5268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5269 NoBinPacking); 5270 5271 // If there is a subsequent call, change to hanging indentation. 5272 verifyFormat( 5273 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5274 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5275 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5276 verifyFormat( 5277 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5278 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5279 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5281 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5282 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5284 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5285 } 5286 5287 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5288 verifyFormat("template <typename T>\n" 5289 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5290 verifyFormat("template <typename T>\n" 5291 "// T should be one of {A, B}.\n" 5292 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5293 verifyFormat( 5294 "template <typename T>\n" 5295 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5296 verifyFormat("template <typename T>\n" 5297 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5298 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5299 verifyFormat( 5300 "template <typename T>\n" 5301 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5302 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5303 verifyFormat( 5304 "template <typename T>\n" 5305 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5306 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5308 verifyFormat("template <typename T>\n" 5309 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5310 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5311 verifyFormat( 5312 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5313 " typename T4 = char>\n" 5314 "void f();"); 5315 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5316 " template <typename> class cccccccccccccccccccccc,\n" 5317 " typename ddddddddddddd>\n" 5318 "class C {};"); 5319 verifyFormat( 5320 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5322 5323 verifyFormat("void f() {\n" 5324 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5325 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5326 "}"); 5327 5328 verifyFormat("template <typename T> class C {};"); 5329 verifyFormat("template <typename T> void f();"); 5330 verifyFormat("template <typename T> void f() {}"); 5331 verifyFormat( 5332 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5335 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5338 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5339 getLLVMStyleWithColumns(72)); 5340 EXPECT_EQ("static_cast<A< //\n" 5341 " B> *>(\n" 5342 "\n" 5343 " );", 5344 format("static_cast<A<//\n" 5345 " B>*>(\n" 5346 "\n" 5347 " );")); 5348 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5349 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5350 5351 FormatStyle AlwaysBreak = getLLVMStyle(); 5352 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5353 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5354 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5355 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5356 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5357 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5358 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5359 verifyFormat("template <template <typename> class Fooooooo,\n" 5360 " template <typename> class Baaaaaaar>\n" 5361 "struct C {};", 5362 AlwaysBreak); 5363 verifyFormat("template <typename T> // T can be A, B or C.\n" 5364 "struct C {};", 5365 AlwaysBreak); 5366 } 5367 5368 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5369 verifyFormat( 5370 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5372 verifyFormat( 5373 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5374 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5375 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5376 5377 // FIXME: Should we have the extra indent after the second break? 5378 verifyFormat( 5379 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5382 5383 verifyFormat( 5384 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5385 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5386 5387 // Breaking at nested name specifiers is generally not desirable. 5388 verifyFormat( 5389 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5390 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5391 5392 verifyFormat( 5393 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5395 " aaaaaaaaaaaaaaaaaaaaa);", 5396 getLLVMStyleWithColumns(74)); 5397 5398 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5400 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5401 } 5402 5403 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5404 verifyFormat("A<int> a;"); 5405 verifyFormat("A<A<A<int>>> a;"); 5406 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5407 verifyFormat("bool x = a < 1 || 2 > a;"); 5408 verifyFormat("bool x = 5 < f<int>();"); 5409 verifyFormat("bool x = f<int>() > 5;"); 5410 verifyFormat("bool x = 5 < a<int>::x;"); 5411 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5412 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5413 5414 verifyGoogleFormat("A<A<int>> a;"); 5415 verifyGoogleFormat("A<A<A<int>>> a;"); 5416 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5417 verifyGoogleFormat("A<A<int> > a;"); 5418 verifyGoogleFormat("A<A<A<int> > > a;"); 5419 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5420 verifyGoogleFormat("A<::A<int>> a;"); 5421 verifyGoogleFormat("A<::A> a;"); 5422 verifyGoogleFormat("A< ::A> a;"); 5423 verifyGoogleFormat("A< ::A<int> > a;"); 5424 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5425 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5426 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5427 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5428 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5429 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5430 5431 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5432 5433 verifyFormat("test >> a >> b;"); 5434 verifyFormat("test << a >> b;"); 5435 5436 verifyFormat("f<int>();"); 5437 verifyFormat("template <typename T> void f() {}"); 5438 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5439 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5440 "sizeof(char)>::type>;"); 5441 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5442 verifyFormat("f(a.operator()<A>());"); 5443 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5444 " .template operator()<A>());", 5445 getLLVMStyleWithColumns(35)); 5446 5447 // Not template parameters. 5448 verifyFormat("return a < b && c > d;"); 5449 verifyFormat("void f() {\n" 5450 " while (a < b && c > d) {\n" 5451 " }\n" 5452 "}"); 5453 verifyFormat("template <typename... Types>\n" 5454 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5455 5456 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5458 getLLVMStyleWithColumns(60)); 5459 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5460 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5461 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5462 } 5463 5464 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5465 verifyFormat("COMPARE(a, ==, b);"); 5466 } 5467 5468 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5469 verifyFormat("int A::*x;"); 5470 verifyFormat("int (S::*func)(void *);"); 5471 verifyFormat("void f() { int (S::*func)(void *); }"); 5472 verifyFormat("typedef bool *(Class::*Member)() const;"); 5473 verifyFormat("void f() {\n" 5474 " (a->*f)();\n" 5475 " a->*x;\n" 5476 " (a.*f)();\n" 5477 " ((*a).*f)();\n" 5478 " a.*x;\n" 5479 "}"); 5480 verifyFormat("void f() {\n" 5481 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5482 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5483 "}"); 5484 verifyFormat( 5485 "(aaaaaaaaaa->*bbbbbbb)(\n" 5486 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5487 FormatStyle Style = getLLVMStyle(); 5488 Style.PointerAlignment = FormatStyle::PAS_Left; 5489 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5490 } 5491 5492 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5493 verifyFormat("int a = -2;"); 5494 verifyFormat("f(-1, -2, -3);"); 5495 verifyFormat("a[-1] = 5;"); 5496 verifyFormat("int a = 5 + -2;"); 5497 verifyFormat("if (i == -1) {\n}"); 5498 verifyFormat("if (i != -1) {\n}"); 5499 verifyFormat("if (i > -1) {\n}"); 5500 verifyFormat("if (i < -1) {\n}"); 5501 verifyFormat("++(a->f());"); 5502 verifyFormat("--(a->f());"); 5503 verifyFormat("(a->f())++;"); 5504 verifyFormat("a[42]++;"); 5505 verifyFormat("if (!(a->f())) {\n}"); 5506 5507 verifyFormat("a-- > b;"); 5508 verifyFormat("b ? -a : c;"); 5509 verifyFormat("n * sizeof char16;"); 5510 verifyFormat("n * alignof char16;", getGoogleStyle()); 5511 verifyFormat("sizeof(char);"); 5512 verifyFormat("alignof(char);", getGoogleStyle()); 5513 5514 verifyFormat("return -1;"); 5515 verifyFormat("switch (a) {\n" 5516 "case -1:\n" 5517 " break;\n" 5518 "}"); 5519 verifyFormat("#define X -1"); 5520 verifyFormat("#define X -kConstant"); 5521 5522 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5523 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5524 5525 verifyFormat("int a = /* confusing comment */ -1;"); 5526 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5527 verifyFormat("int a = i /* confusing comment */++;"); 5528 } 5529 5530 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5531 verifyFormat("if (!aaaaaaaaaa( // break\n" 5532 " aaaaa)) {\n" 5533 "}"); 5534 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5535 " aaaaa));"); 5536 verifyFormat("*aaa = aaaaaaa( // break\n" 5537 " bbbbbb);"); 5538 } 5539 5540 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5541 verifyFormat("bool operator<();"); 5542 verifyFormat("bool operator>();"); 5543 verifyFormat("bool operator=();"); 5544 verifyFormat("bool operator==();"); 5545 verifyFormat("bool operator!=();"); 5546 verifyFormat("int operator+();"); 5547 verifyFormat("int operator++();"); 5548 verifyFormat("bool operator,();"); 5549 verifyFormat("bool operator();"); 5550 verifyFormat("bool operator()();"); 5551 verifyFormat("bool operator[]();"); 5552 verifyFormat("operator bool();"); 5553 verifyFormat("operator int();"); 5554 verifyFormat("operator void *();"); 5555 verifyFormat("operator SomeType<int>();"); 5556 verifyFormat("operator SomeType<int, int>();"); 5557 verifyFormat("operator SomeType<SomeType<int>>();"); 5558 verifyFormat("void *operator new(std::size_t size);"); 5559 verifyFormat("void *operator new[](std::size_t size);"); 5560 verifyFormat("void operator delete(void *ptr);"); 5561 verifyFormat("void operator delete[](void *ptr);"); 5562 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5563 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5564 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5565 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5566 5567 verifyFormat( 5568 "ostream &operator<<(ostream &OutputStream,\n" 5569 " SomeReallyLongType WithSomeReallyLongValue);"); 5570 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5571 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5572 " return left.group < right.group;\n" 5573 "}"); 5574 verifyFormat("SomeType &operator=(const SomeType &S);"); 5575 verifyFormat("f.template operator()<int>();"); 5576 5577 verifyGoogleFormat("operator void*();"); 5578 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5579 verifyGoogleFormat("operator ::A();"); 5580 5581 verifyFormat("using A::operator+;"); 5582 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5583 "int i;"); 5584 } 5585 5586 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5587 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5588 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5589 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5590 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5591 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5592 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5593 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5594 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5595 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5596 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5597 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5598 5599 FormatStyle AlignLeft = getLLVMStyle(); 5600 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5601 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5602 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5603 AlignLeft); 5604 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5605 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5606 5607 FormatStyle Spaces = getLLVMStyle(); 5608 Spaces.SpacesInCStyleCastParentheses = true; 5609 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5610 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5611 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5612 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5613 5614 Spaces.SpacesInCStyleCastParentheses = false; 5615 Spaces.SpacesInParentheses = true; 5616 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5617 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5618 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5619 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5620 } 5621 5622 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5623 verifyFormat("void f() {\n" 5624 " A *a = new A;\n" 5625 " A *a = new (placement) A;\n" 5626 " delete a;\n" 5627 " delete (A *)a;\n" 5628 "}"); 5629 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5630 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5631 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5632 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5633 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5634 verifyFormat("delete[] h->p;"); 5635 } 5636 5637 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5638 verifyFormat("int *f(int *a) {}"); 5639 verifyFormat("int main(int argc, char **argv) {}"); 5640 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5641 verifyIndependentOfContext("f(a, *a);"); 5642 verifyFormat("void g() { f(*a); }"); 5643 verifyIndependentOfContext("int a = b * 10;"); 5644 verifyIndependentOfContext("int a = 10 * b;"); 5645 verifyIndependentOfContext("int a = b * c;"); 5646 verifyIndependentOfContext("int a += b * c;"); 5647 verifyIndependentOfContext("int a -= b * c;"); 5648 verifyIndependentOfContext("int a *= b * c;"); 5649 verifyIndependentOfContext("int a /= b * c;"); 5650 verifyIndependentOfContext("int a = *b;"); 5651 verifyIndependentOfContext("int a = *b * c;"); 5652 verifyIndependentOfContext("int a = b * *c;"); 5653 verifyIndependentOfContext("int a = b * (10);"); 5654 verifyIndependentOfContext("S << b * (10);"); 5655 verifyIndependentOfContext("return 10 * b;"); 5656 verifyIndependentOfContext("return *b * *c;"); 5657 verifyIndependentOfContext("return a & ~b;"); 5658 verifyIndependentOfContext("f(b ? *c : *d);"); 5659 verifyIndependentOfContext("int a = b ? *c : *d;"); 5660 verifyIndependentOfContext("*b = a;"); 5661 verifyIndependentOfContext("a * ~b;"); 5662 verifyIndependentOfContext("a * !b;"); 5663 verifyIndependentOfContext("a * +b;"); 5664 verifyIndependentOfContext("a * -b;"); 5665 verifyIndependentOfContext("a * ++b;"); 5666 verifyIndependentOfContext("a * --b;"); 5667 verifyIndependentOfContext("a[4] * b;"); 5668 verifyIndependentOfContext("a[a * a] = 1;"); 5669 verifyIndependentOfContext("f() * b;"); 5670 verifyIndependentOfContext("a * [self dostuff];"); 5671 verifyIndependentOfContext("int x = a * (a + b);"); 5672 verifyIndependentOfContext("(a *)(a + b);"); 5673 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5674 verifyIndependentOfContext("int *pa = (int *)&a;"); 5675 verifyIndependentOfContext("return sizeof(int **);"); 5676 verifyIndependentOfContext("return sizeof(int ******);"); 5677 verifyIndependentOfContext("return (int **&)a;"); 5678 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5679 verifyFormat("void f(Type (*parameter)[10]) {}"); 5680 verifyFormat("void f(Type (¶meter)[10]) {}"); 5681 verifyGoogleFormat("return sizeof(int**);"); 5682 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5683 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5684 verifyFormat("auto a = [](int **&, int ***) {};"); 5685 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5686 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5687 verifyFormat("[](const decltype(*a) &value) {}"); 5688 verifyFormat("decltype(a * b) F();"); 5689 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5690 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5691 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5692 verifyIndependentOfContext("int i{a * b};"); 5693 verifyIndependentOfContext("aaa && aaa->f();"); 5694 verifyIndependentOfContext("int x = ~*p;"); 5695 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5696 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5697 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5698 verifyFormat("void f() { f(a, c * d); }"); 5699 verifyFormat("void f() { f(new a(), c * d); }"); 5700 5701 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5702 5703 verifyIndependentOfContext("A<int *> a;"); 5704 verifyIndependentOfContext("A<int **> a;"); 5705 verifyIndependentOfContext("A<int *, int *> a;"); 5706 verifyIndependentOfContext("A<int *[]> a;"); 5707 verifyIndependentOfContext( 5708 "const char *const p = reinterpret_cast<const char *const>(q);"); 5709 verifyIndependentOfContext("A<int **, int **> a;"); 5710 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5711 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5712 verifyFormat("for (; a && b;) {\n}"); 5713 verifyFormat("bool foo = true && [] { return false; }();"); 5714 5715 verifyFormat( 5716 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5718 5719 verifyGoogleFormat("**outparam = 1;"); 5720 verifyGoogleFormat("*outparam = a * b;"); 5721 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5722 verifyGoogleFormat("A<int*> a;"); 5723 verifyGoogleFormat("A<int**> a;"); 5724 verifyGoogleFormat("A<int*, int*> a;"); 5725 verifyGoogleFormat("A<int**, int**> a;"); 5726 verifyGoogleFormat("f(b ? *c : *d);"); 5727 verifyGoogleFormat("int a = b ? *c : *d;"); 5728 verifyGoogleFormat("Type* t = **x;"); 5729 verifyGoogleFormat("Type* t = *++*x;"); 5730 verifyGoogleFormat("*++*x;"); 5731 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5732 verifyGoogleFormat("Type* t = x++ * y;"); 5733 verifyGoogleFormat( 5734 "const char* const p = reinterpret_cast<const char* const>(q);"); 5735 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5736 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5737 verifyGoogleFormat("template <typename T>\n" 5738 "void f(int i = 0, SomeType** temps = NULL);"); 5739 5740 FormatStyle Left = getLLVMStyle(); 5741 Left.PointerAlignment = FormatStyle::PAS_Left; 5742 verifyFormat("x = *a(x) = *a(y);", Left); 5743 verifyFormat("for (;; * = b) {\n}", Left); 5744 verifyFormat("return *this += 1;", Left); 5745 5746 verifyIndependentOfContext("a = *(x + y);"); 5747 verifyIndependentOfContext("a = &(x + y);"); 5748 verifyIndependentOfContext("*(x + y).call();"); 5749 verifyIndependentOfContext("&(x + y)->call();"); 5750 verifyFormat("void f() { &(*I).first; }"); 5751 5752 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5753 verifyFormat( 5754 "int *MyValues = {\n" 5755 " *A, // Operator detection might be confused by the '{'\n" 5756 " *BB // Operator detection might be confused by previous comment\n" 5757 "};"); 5758 5759 verifyIndependentOfContext("if (int *a = &b)"); 5760 verifyIndependentOfContext("if (int &a = *b)"); 5761 verifyIndependentOfContext("if (a & b[i])"); 5762 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5763 verifyIndependentOfContext("if (*b[i])"); 5764 verifyIndependentOfContext("if (int *a = (&b))"); 5765 verifyIndependentOfContext("while (int *a = &b)"); 5766 verifyIndependentOfContext("size = sizeof *a;"); 5767 verifyIndependentOfContext("if (a && (b = c))"); 5768 verifyFormat("void f() {\n" 5769 " for (const int &v : Values) {\n" 5770 " }\n" 5771 "}"); 5772 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5773 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5774 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5775 5776 verifyFormat("#define A (!a * b)"); 5777 verifyFormat("#define MACRO \\\n" 5778 " int *i = a * b; \\\n" 5779 " void f(a *b);", 5780 getLLVMStyleWithColumns(19)); 5781 5782 verifyIndependentOfContext("A = new SomeType *[Length];"); 5783 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5784 verifyIndependentOfContext("T **t = new T *;"); 5785 verifyIndependentOfContext("T **t = new T *();"); 5786 verifyGoogleFormat("A = new SomeType*[Length]();"); 5787 verifyGoogleFormat("A = new SomeType*[Length];"); 5788 verifyGoogleFormat("T** t = new T*;"); 5789 verifyGoogleFormat("T** t = new T*();"); 5790 5791 FormatStyle PointerLeft = getLLVMStyle(); 5792 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5793 verifyFormat("delete *x;", PointerLeft); 5794 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5795 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5796 verifyFormat("template <bool a, bool b> " 5797 "typename t::if<x && y>::type f() {}"); 5798 verifyFormat("template <int *y> f() {}"); 5799 verifyFormat("vector<int *> v;"); 5800 verifyFormat("vector<int *const> v;"); 5801 verifyFormat("vector<int *const **const *> v;"); 5802 verifyFormat("vector<int *volatile> v;"); 5803 verifyFormat("vector<a * b> v;"); 5804 verifyFormat("foo<b && false>();"); 5805 verifyFormat("foo<b & 1>();"); 5806 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5807 verifyFormat( 5808 "template <class T, class = typename std::enable_if<\n" 5809 " std::is_integral<T>::value &&\n" 5810 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5811 "void F();", 5812 getLLVMStyleWithColumns(76)); 5813 verifyFormat( 5814 "template <class T,\n" 5815 " class = typename ::std::enable_if<\n" 5816 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5817 "void F();", 5818 getGoogleStyleWithColumns(68)); 5819 5820 verifyIndependentOfContext("MACRO(int *i);"); 5821 verifyIndependentOfContext("MACRO(auto *a);"); 5822 verifyIndependentOfContext("MACRO(const A *a);"); 5823 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5824 // FIXME: Is there a way to make this work? 5825 // verifyIndependentOfContext("MACRO(A *a);"); 5826 5827 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5828 verifyFormat("return options != nullptr && operator==(*options);"); 5829 5830 EXPECT_EQ("#define OP(x) \\\n" 5831 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5832 " return s << a.DebugString(); \\\n" 5833 " }", 5834 format("#define OP(x) \\\n" 5835 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5836 " return s << a.DebugString(); \\\n" 5837 " }", 5838 getLLVMStyleWithColumns(50))); 5839 5840 // FIXME: We cannot handle this case yet; we might be able to figure out that 5841 // foo<x> d > v; doesn't make sense. 5842 verifyFormat("foo<a<b && c> d> v;"); 5843 5844 FormatStyle PointerMiddle = getLLVMStyle(); 5845 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5846 verifyFormat("delete *x;", PointerMiddle); 5847 verifyFormat("int * x;", PointerMiddle); 5848 verifyFormat("template <int * y> f() {}", PointerMiddle); 5849 verifyFormat("int * f(int * a) {}", PointerMiddle); 5850 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5851 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5852 verifyFormat("A<int *> a;", PointerMiddle); 5853 verifyFormat("A<int **> a;", PointerMiddle); 5854 verifyFormat("A<int *, int *> a;", PointerMiddle); 5855 verifyFormat("A<int * []> a;", PointerMiddle); 5856 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5857 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5858 verifyFormat("T ** t = new T *;", PointerMiddle); 5859 5860 // Member function reference qualifiers aren't binary operators. 5861 verifyFormat("string // break\n" 5862 "operator()() & {}"); 5863 verifyFormat("string // break\n" 5864 "operator()() && {}"); 5865 verifyGoogleFormat("template <typename T>\n" 5866 "auto x() & -> int {}"); 5867 } 5868 5869 TEST_F(FormatTest, UnderstandsAttributes) { 5870 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5871 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5872 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5873 FormatStyle AfterType = getLLVMStyle(); 5874 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5875 verifyFormat("__attribute__((nodebug)) void\n" 5876 "foo() {}\n", 5877 AfterType); 5878 } 5879 5880 TEST_F(FormatTest, UnderstandsEllipsis) { 5881 verifyFormat("int printf(const char *fmt, ...);"); 5882 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5883 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5884 5885 FormatStyle PointersLeft = getLLVMStyle(); 5886 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5887 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5888 } 5889 5890 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5891 EXPECT_EQ("int *a;\n" 5892 "int *a;\n" 5893 "int *a;", 5894 format("int *a;\n" 5895 "int* a;\n" 5896 "int *a;", 5897 getGoogleStyle())); 5898 EXPECT_EQ("int* a;\n" 5899 "int* a;\n" 5900 "int* a;", 5901 format("int* a;\n" 5902 "int* a;\n" 5903 "int *a;", 5904 getGoogleStyle())); 5905 EXPECT_EQ("int *a;\n" 5906 "int *a;\n" 5907 "int *a;", 5908 format("int *a;\n" 5909 "int * a;\n" 5910 "int * a;", 5911 getGoogleStyle())); 5912 EXPECT_EQ("auto x = [] {\n" 5913 " int *a;\n" 5914 " int *a;\n" 5915 " int *a;\n" 5916 "};", 5917 format("auto x=[]{int *a;\n" 5918 "int * a;\n" 5919 "int * a;};", 5920 getGoogleStyle())); 5921 } 5922 5923 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5924 verifyFormat("int f(int &&a) {}"); 5925 verifyFormat("int f(int a, char &&b) {}"); 5926 verifyFormat("void f() { int &&a = b; }"); 5927 verifyGoogleFormat("int f(int a, char&& b) {}"); 5928 verifyGoogleFormat("void f() { int&& a = b; }"); 5929 5930 verifyIndependentOfContext("A<int &&> a;"); 5931 verifyIndependentOfContext("A<int &&, int &&> a;"); 5932 verifyGoogleFormat("A<int&&> a;"); 5933 verifyGoogleFormat("A<int&&, int&&> a;"); 5934 5935 // Not rvalue references: 5936 verifyFormat("template <bool B, bool C> class A {\n" 5937 " static_assert(B && C, \"Something is wrong\");\n" 5938 "};"); 5939 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5940 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5941 verifyFormat("#define A(a, b) (a && b)"); 5942 } 5943 5944 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5945 verifyFormat("void f() {\n" 5946 " x[aaaaaaaaa -\n" 5947 " b] = 23;\n" 5948 "}", 5949 getLLVMStyleWithColumns(15)); 5950 } 5951 5952 TEST_F(FormatTest, FormatsCasts) { 5953 verifyFormat("Type *A = static_cast<Type *>(P);"); 5954 verifyFormat("Type *A = (Type *)P;"); 5955 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5956 verifyFormat("int a = (int)(2.0f);"); 5957 verifyFormat("int a = (int)2.0f;"); 5958 verifyFormat("x[(int32)y];"); 5959 verifyFormat("x = (int32)y;"); 5960 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5961 verifyFormat("int a = (int)*b;"); 5962 verifyFormat("int a = (int)2.0f;"); 5963 verifyFormat("int a = (int)~0;"); 5964 verifyFormat("int a = (int)++a;"); 5965 verifyFormat("int a = (int)sizeof(int);"); 5966 verifyFormat("int a = (int)+2;"); 5967 verifyFormat("my_int a = (my_int)2.0f;"); 5968 verifyFormat("my_int a = (my_int)sizeof(int);"); 5969 verifyFormat("return (my_int)aaa;"); 5970 verifyFormat("#define x ((int)-1)"); 5971 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5972 verifyFormat("#define p(q) ((int *)&q)"); 5973 verifyFormat("fn(a)(b) + 1;"); 5974 5975 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5976 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5977 verifyFormat("my_int a = (my_int)~0;"); 5978 verifyFormat("my_int a = (my_int)++a;"); 5979 verifyFormat("my_int a = (my_int)-2;"); 5980 verifyFormat("my_int a = (my_int)1;"); 5981 verifyFormat("my_int a = (my_int *)1;"); 5982 verifyFormat("my_int a = (const my_int)-1;"); 5983 verifyFormat("my_int a = (const my_int *)-1;"); 5984 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5985 verifyFormat("my_int a = (ns::my_int)-2;"); 5986 verifyFormat("case (my_int)ONE:"); 5987 5988 // FIXME: single value wrapped with paren will be treated as cast. 5989 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5990 5991 verifyFormat("{ (void)F; }"); 5992 5993 // Don't break after a cast's 5994 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5995 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5996 " bbbbbbbbbbbbbbbbbbbbbb);"); 5997 5998 // These are not casts. 5999 verifyFormat("void f(int *) {}"); 6000 verifyFormat("f(foo)->b;"); 6001 verifyFormat("f(foo).b;"); 6002 verifyFormat("f(foo)(b);"); 6003 verifyFormat("f(foo)[b];"); 6004 verifyFormat("[](foo) { return 4; }(bar);"); 6005 verifyFormat("(*funptr)(foo)[4];"); 6006 verifyFormat("funptrs[4](foo)[4];"); 6007 verifyFormat("void f(int *);"); 6008 verifyFormat("void f(int *) = 0;"); 6009 verifyFormat("void f(SmallVector<int>) {}"); 6010 verifyFormat("void f(SmallVector<int>);"); 6011 verifyFormat("void f(SmallVector<int>) = 0;"); 6012 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6013 verifyFormat("int a = sizeof(int) * b;"); 6014 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6015 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6016 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6017 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6018 6019 // These are not casts, but at some point were confused with casts. 6020 verifyFormat("virtual void foo(int *) override;"); 6021 verifyFormat("virtual void foo(char &) const;"); 6022 verifyFormat("virtual void foo(int *a, char *) const;"); 6023 verifyFormat("int a = sizeof(int *) + b;"); 6024 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6025 verifyFormat("bool b = f(g<int>) && c;"); 6026 verifyFormat("typedef void (*f)(int i) func;"); 6027 6028 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6029 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6030 // FIXME: The indentation here is not ideal. 6031 verifyFormat( 6032 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6033 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6034 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6035 } 6036 6037 TEST_F(FormatTest, FormatsFunctionTypes) { 6038 verifyFormat("A<bool()> a;"); 6039 verifyFormat("A<SomeType()> a;"); 6040 verifyFormat("A<void (*)(int, std::string)> a;"); 6041 verifyFormat("A<void *(int)>;"); 6042 verifyFormat("void *(*a)(int *, SomeType *);"); 6043 verifyFormat("int (*func)(void *);"); 6044 verifyFormat("void f() { int (*func)(void *); }"); 6045 verifyFormat("template <class CallbackClass>\n" 6046 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6047 6048 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6049 verifyGoogleFormat("void* (*a)(int);"); 6050 verifyGoogleFormat( 6051 "template <class CallbackClass>\n" 6052 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6053 6054 // Other constructs can look somewhat like function types: 6055 verifyFormat("A<sizeof(*x)> a;"); 6056 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6057 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6058 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6059 verifyFormat("int x = f(&h)();"); 6060 } 6061 6062 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6063 verifyFormat("A (*foo_)[6];"); 6064 verifyFormat("vector<int> (*foo_)[6];"); 6065 } 6066 6067 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6068 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6069 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6070 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6071 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6072 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6073 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6074 6075 // Different ways of ()-initializiation. 6076 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6077 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6078 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6079 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6080 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6081 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6082 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6083 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6084 } 6085 6086 TEST_F(FormatTest, BreaksLongDeclarations) { 6087 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6088 " AnotherNameForTheLongType;"); 6089 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6091 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6092 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6093 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6094 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6095 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6096 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6097 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6098 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6099 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6100 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6101 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6102 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6103 FormatStyle Indented = getLLVMStyle(); 6104 Indented.IndentWrappedFunctionNames = true; 6105 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6106 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6107 Indented); 6108 verifyFormat( 6109 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6110 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6111 Indented); 6112 verifyFormat( 6113 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6114 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6115 Indented); 6116 verifyFormat( 6117 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6118 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6119 Indented); 6120 6121 // FIXME: Without the comment, this breaks after "(". 6122 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6123 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6124 getGoogleStyle()); 6125 6126 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6127 " int LoooooooooooooooooooongParam2) {}"); 6128 verifyFormat( 6129 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6130 " SourceLocation L, IdentifierIn *II,\n" 6131 " Type *T) {}"); 6132 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6133 "ReallyReaaallyLongFunctionName(\n" 6134 " const std::string &SomeParameter,\n" 6135 " const SomeType<string, SomeOtherTemplateParameter>\n" 6136 " &ReallyReallyLongParameterName,\n" 6137 " const SomeType<string, SomeOtherTemplateParameter>\n" 6138 " &AnotherLongParameterName) {}"); 6139 verifyFormat("template <typename A>\n" 6140 "SomeLoooooooooooooooooooooongType<\n" 6141 " typename some_namespace::SomeOtherType<A>::Type>\n" 6142 "Function() {}"); 6143 6144 verifyGoogleFormat( 6145 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6146 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6147 verifyGoogleFormat( 6148 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6149 " SourceLocation L) {}"); 6150 verifyGoogleFormat( 6151 "some_namespace::LongReturnType\n" 6152 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6153 " int first_long_parameter, int second_parameter) {}"); 6154 6155 verifyGoogleFormat("template <typename T>\n" 6156 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6157 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6158 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6159 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6160 6161 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6162 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6163 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6164 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6165 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6166 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6167 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6168 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6169 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6171 } 6172 6173 TEST_F(FormatTest, FormatsArrays) { 6174 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6175 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6176 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6177 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6178 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6179 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6180 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6181 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6182 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6183 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6184 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6185 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6186 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6187 verifyFormat( 6188 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6189 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6190 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6191 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6192 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6193 6194 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6196 verifyFormat( 6197 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6198 " .aaaaaaa[0]\n" 6199 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6200 verifyFormat("a[::b::c];"); 6201 6202 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6203 6204 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6205 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6206 } 6207 6208 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6209 verifyFormat("(a)->b();"); 6210 verifyFormat("--a;"); 6211 } 6212 6213 TEST_F(FormatTest, HandlesIncludeDirectives) { 6214 verifyFormat("#include <string>\n" 6215 "#include <a/b/c.h>\n" 6216 "#include \"a/b/string\"\n" 6217 "#include \"string.h\"\n" 6218 "#include \"string.h\"\n" 6219 "#include <a-a>\n" 6220 "#include < path with space >\n" 6221 "#include_next <test.h>" 6222 "#include \"abc.h\" // this is included for ABC\n" 6223 "#include \"some long include\" // with a comment\n" 6224 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6225 getLLVMStyleWithColumns(35)); 6226 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6227 EXPECT_EQ("#include <a>", format("#include<a>")); 6228 6229 verifyFormat("#import <string>"); 6230 verifyFormat("#import <a/b/c.h>"); 6231 verifyFormat("#import \"a/b/string\""); 6232 verifyFormat("#import \"string.h\""); 6233 verifyFormat("#import \"string.h\""); 6234 verifyFormat("#if __has_include(<strstream>)\n" 6235 "#include <strstream>\n" 6236 "#endif"); 6237 6238 verifyFormat("#define MY_IMPORT <a/b>"); 6239 6240 // Protocol buffer definition or missing "#". 6241 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6242 getLLVMStyleWithColumns(30)); 6243 6244 FormatStyle Style = getLLVMStyle(); 6245 Style.AlwaysBreakBeforeMultilineStrings = true; 6246 Style.ColumnLimit = 0; 6247 verifyFormat("#import \"abc.h\"", Style); 6248 6249 // But 'import' might also be a regular C++ namespace. 6250 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6252 } 6253 6254 //===----------------------------------------------------------------------===// 6255 // Error recovery tests. 6256 //===----------------------------------------------------------------------===// 6257 6258 TEST_F(FormatTest, IncompleteParameterLists) { 6259 FormatStyle NoBinPacking = getLLVMStyle(); 6260 NoBinPacking.BinPackParameters = false; 6261 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6262 " double *min_x,\n" 6263 " double *max_x,\n" 6264 " double *min_y,\n" 6265 " double *max_y,\n" 6266 " double *min_z,\n" 6267 " double *max_z, ) {}", 6268 NoBinPacking); 6269 } 6270 6271 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6272 verifyFormat("void f() { return; }\n42"); 6273 verifyFormat("void f() {\n" 6274 " if (0)\n" 6275 " return;\n" 6276 "}\n" 6277 "42"); 6278 verifyFormat("void f() { return }\n42"); 6279 verifyFormat("void f() {\n" 6280 " if (0)\n" 6281 " return\n" 6282 "}\n" 6283 "42"); 6284 } 6285 6286 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6287 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6288 EXPECT_EQ("void f() {\n" 6289 " if (a)\n" 6290 " return\n" 6291 "}", 6292 format("void f ( ) { if ( a ) return }")); 6293 EXPECT_EQ("namespace N {\n" 6294 "void f()\n" 6295 "}", 6296 format("namespace N { void f() }")); 6297 EXPECT_EQ("namespace N {\n" 6298 "void f() {}\n" 6299 "void g()\n" 6300 "}", 6301 format("namespace N { void f( ) { } void g( ) }")); 6302 } 6303 6304 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6305 verifyFormat("int aaaaaaaa =\n" 6306 " // Overlylongcomment\n" 6307 " b;", 6308 getLLVMStyleWithColumns(20)); 6309 verifyFormat("function(\n" 6310 " ShortArgument,\n" 6311 " LoooooooooooongArgument);\n", 6312 getLLVMStyleWithColumns(20)); 6313 } 6314 6315 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6316 verifyFormat("public:"); 6317 verifyFormat("class A {\n" 6318 "public\n" 6319 " void f() {}\n" 6320 "};"); 6321 verifyFormat("public\n" 6322 "int qwerty;"); 6323 verifyFormat("public\n" 6324 "B {}"); 6325 verifyFormat("public\n" 6326 "{}"); 6327 verifyFormat("public\n" 6328 "B { int x; }"); 6329 } 6330 6331 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6332 verifyFormat("{"); 6333 verifyFormat("#})"); 6334 verifyNoCrash("(/**/[:!] ?[)."); 6335 } 6336 6337 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6338 verifyFormat("do {\n}"); 6339 verifyFormat("do {\n}\n" 6340 "f();"); 6341 verifyFormat("do {\n}\n" 6342 "wheeee(fun);"); 6343 verifyFormat("do {\n" 6344 " f();\n" 6345 "}"); 6346 } 6347 6348 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6349 verifyFormat("if {\n foo;\n foo();\n}"); 6350 verifyFormat("switch {\n foo;\n foo();\n}"); 6351 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6352 verifyFormat("while {\n foo;\n foo();\n}"); 6353 verifyFormat("do {\n foo;\n foo();\n} while;"); 6354 } 6355 6356 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6357 verifyIncompleteFormat("namespace {\n" 6358 "class Foo { Foo (\n" 6359 "};\n" 6360 "} // comment"); 6361 } 6362 6363 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6364 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6365 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6366 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6367 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6368 6369 EXPECT_EQ("{\n" 6370 " {\n" 6371 " breakme(\n" 6372 " qwe);\n" 6373 " }\n", 6374 format("{\n" 6375 " {\n" 6376 " breakme(qwe);\n" 6377 "}\n", 6378 getLLVMStyleWithColumns(10))); 6379 } 6380 6381 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6382 verifyFormat("int x = {\n" 6383 " avariable,\n" 6384 " b(alongervariable)};", 6385 getLLVMStyleWithColumns(25)); 6386 } 6387 6388 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6389 verifyFormat("return (a)(b){1, 2, 3};"); 6390 } 6391 6392 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6393 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6394 verifyFormat("vector<int> x{\n" 6395 " 1, 2, 3, 4,\n" 6396 "};"); 6397 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6398 verifyFormat("f({1, 2});"); 6399 verifyFormat("auto v = Foo{-1};"); 6400 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6401 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6402 verifyFormat("new vector<int>{1, 2, 3};"); 6403 verifyFormat("new int[3]{1, 2, 3};"); 6404 verifyFormat("new int{1};"); 6405 verifyFormat("return {arg1, arg2};"); 6406 verifyFormat("return {arg1, SomeType{parameter}};"); 6407 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6408 verifyFormat("new T{arg1, arg2};"); 6409 verifyFormat("f(MyMap[{composite, key}]);"); 6410 verifyFormat("class Class {\n" 6411 " T member = {arg1, arg2};\n" 6412 "};"); 6413 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6414 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6415 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6416 6417 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6418 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6419 verifyFormat("auto i = decltype(x){};"); 6420 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6421 verifyFormat("Node n{1, Node{1000}, //\n" 6422 " 2};"); 6423 verifyFormat("Aaaa aaaaaaa{\n" 6424 " {\n" 6425 " aaaa,\n" 6426 " },\n" 6427 "};"); 6428 verifyFormat("class C : public D {\n" 6429 " SomeClass SC{2};\n" 6430 "};"); 6431 verifyFormat("class C : public A {\n" 6432 " class D : public B {\n" 6433 " void f() { int i{2}; }\n" 6434 " };\n" 6435 "};"); 6436 verifyFormat("#define A {a, a},"); 6437 6438 // In combination with BinPackArguments = false. 6439 FormatStyle NoBinPacking = getLLVMStyle(); 6440 NoBinPacking.BinPackArguments = false; 6441 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6442 " bbbbb,\n" 6443 " ccccc,\n" 6444 " ddddd,\n" 6445 " eeeee,\n" 6446 " ffffff,\n" 6447 " ggggg,\n" 6448 " hhhhhh,\n" 6449 " iiiiii,\n" 6450 " jjjjjj,\n" 6451 " kkkkkk};", 6452 NoBinPacking); 6453 verifyFormat("const Aaaaaa aaaaa = {\n" 6454 " aaaaa,\n" 6455 " bbbbb,\n" 6456 " ccccc,\n" 6457 " ddddd,\n" 6458 " eeeee,\n" 6459 " ffffff,\n" 6460 " ggggg,\n" 6461 " hhhhhh,\n" 6462 " iiiiii,\n" 6463 " jjjjjj,\n" 6464 " kkkkkk,\n" 6465 "};", 6466 NoBinPacking); 6467 verifyFormat( 6468 "const Aaaaaa aaaaa = {\n" 6469 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6470 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6471 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6472 "};", 6473 NoBinPacking); 6474 6475 // FIXME: The alignment of these trailing comments might be bad. Then again, 6476 // this might be utterly useless in real code. 6477 verifyFormat("Constructor::Constructor()\n" 6478 " : some_value{ //\n" 6479 " aaaaaaa, //\n" 6480 " bbbbbbb} {}"); 6481 6482 // In braced lists, the first comment is always assumed to belong to the 6483 // first element. Thus, it can be moved to the next or previous line as 6484 // appropriate. 6485 EXPECT_EQ("function({// First element:\n" 6486 " 1,\n" 6487 " // Second element:\n" 6488 " 2});", 6489 format("function({\n" 6490 " // First element:\n" 6491 " 1,\n" 6492 " // Second element:\n" 6493 " 2});")); 6494 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6495 " // First element:\n" 6496 " 1,\n" 6497 " // Second element:\n" 6498 " 2};", 6499 format("std::vector<int> MyNumbers{// First element:\n" 6500 " 1,\n" 6501 " // Second element:\n" 6502 " 2};", 6503 getLLVMStyleWithColumns(30))); 6504 // A trailing comma should still lead to an enforced line break. 6505 EXPECT_EQ("vector<int> SomeVector = {\n" 6506 " // aaa\n" 6507 " 1, 2,\n" 6508 "};", 6509 format("vector<int> SomeVector = { // aaa\n" 6510 " 1, 2, };")); 6511 6512 FormatStyle ExtraSpaces = getLLVMStyle(); 6513 ExtraSpaces.Cpp11BracedListStyle = false; 6514 ExtraSpaces.ColumnLimit = 75; 6515 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6516 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6517 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6518 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6519 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6520 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6521 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6522 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6523 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6524 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6525 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6526 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6527 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6528 verifyFormat("class Class {\n" 6529 " T member = { arg1, arg2 };\n" 6530 "};", 6531 ExtraSpaces); 6532 verifyFormat( 6533 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6534 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6535 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6536 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6537 ExtraSpaces); 6538 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6539 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6540 ExtraSpaces); 6541 verifyFormat( 6542 "someFunction(OtherParam,\n" 6543 " BracedList{ // comment 1 (Forcing interesting break)\n" 6544 " param1, param2,\n" 6545 " // comment 2\n" 6546 " param3, param4 });", 6547 ExtraSpaces); 6548 verifyFormat( 6549 "std::this_thread::sleep_for(\n" 6550 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6551 ExtraSpaces); 6552 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6553 " aaaaaaa,\n" 6554 " aaaaaaaaaa,\n" 6555 " aaaaa,\n" 6556 " aaaaaaaaaaaaaaa,\n" 6557 " aaa,\n" 6558 " aaaaaaaaaa,\n" 6559 " a,\n" 6560 " aaaaaaaaaaaaaaaaaaaaa,\n" 6561 " aaaaaaaaaaaa,\n" 6562 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6563 " aaaaaaa,\n" 6564 " a};"); 6565 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6566 } 6567 6568 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6569 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6570 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6571 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6572 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6573 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6574 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6575 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6576 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6577 " 1, 22, 333, 4444, 55555, //\n" 6578 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6579 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6580 verifyFormat( 6581 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6582 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6583 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6584 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6585 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6586 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6587 " 7777777};"); 6588 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6589 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6590 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6591 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6592 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6593 " // Separating comment.\n" 6594 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6595 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6596 " // Leading comment\n" 6597 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6598 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6599 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6600 " 1, 1, 1, 1};", 6601 getLLVMStyleWithColumns(39)); 6602 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6603 " 1, 1, 1, 1};", 6604 getLLVMStyleWithColumns(38)); 6605 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6606 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6607 getLLVMStyleWithColumns(43)); 6608 verifyFormat( 6609 "static unsigned SomeValues[10][3] = {\n" 6610 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6611 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6612 verifyFormat("static auto fields = new vector<string>{\n" 6613 " \"aaaaaaaaaaaaa\",\n" 6614 " \"aaaaaaaaaaaaa\",\n" 6615 " \"aaaaaaaaaaaa\",\n" 6616 " \"aaaaaaaaaaaaaa\",\n" 6617 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6618 " \"aaaaaaaaaaaa\",\n" 6619 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6620 "};"); 6621 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6622 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6623 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6624 " 3, cccccccccccccccccccccc};", 6625 getLLVMStyleWithColumns(60)); 6626 6627 // Trailing commas. 6628 verifyFormat("vector<int> x = {\n" 6629 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6630 "};", 6631 getLLVMStyleWithColumns(39)); 6632 verifyFormat("vector<int> x = {\n" 6633 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6634 "};", 6635 getLLVMStyleWithColumns(39)); 6636 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6637 " 1, 1, 1, 1,\n" 6638 " /**/ /**/};", 6639 getLLVMStyleWithColumns(39)); 6640 6641 // Trailing comment in the first line. 6642 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6643 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6644 " 111111111, 222222222, 3333333333, 444444444, //\n" 6645 " 11111111, 22222222, 333333333, 44444444};"); 6646 // Trailing comment in the last line. 6647 verifyFormat("int aaaaa[] = {\n" 6648 " 1, 2, 3, // comment\n" 6649 " 4, 5, 6 // comment\n" 6650 "};"); 6651 6652 // With nested lists, we should either format one item per line or all nested 6653 // lists one on line. 6654 // FIXME: For some nested lists, we can do better. 6655 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6656 " {aaaaaaaaaaaaaaaaaaa},\n" 6657 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6658 " {aaaaaaaaaaaaaaaaa}};", 6659 getLLVMStyleWithColumns(60)); 6660 verifyFormat( 6661 "SomeStruct my_struct_array = {\n" 6662 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6663 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6664 " {aaa, aaa},\n" 6665 " {aaa, aaa},\n" 6666 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6667 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6668 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6669 6670 // No column layout should be used here. 6671 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6672 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6673 6674 verifyNoCrash("a<,"); 6675 6676 // No braced initializer here. 6677 verifyFormat("void f() {\n" 6678 " struct Dummy {};\n" 6679 " f(v);\n" 6680 "}"); 6681 6682 // Long lists should be formatted in columns even if they are nested. 6683 verifyFormat( 6684 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6685 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6686 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6687 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6688 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6689 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6690 } 6691 6692 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6693 FormatStyle DoNotMerge = getLLVMStyle(); 6694 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6695 6696 verifyFormat("void f() { return 42; }"); 6697 verifyFormat("void f() {\n" 6698 " return 42;\n" 6699 "}", 6700 DoNotMerge); 6701 verifyFormat("void f() {\n" 6702 " // Comment\n" 6703 "}"); 6704 verifyFormat("{\n" 6705 "#error {\n" 6706 " int a;\n" 6707 "}"); 6708 verifyFormat("{\n" 6709 " int a;\n" 6710 "#error {\n" 6711 "}"); 6712 verifyFormat("void f() {} // comment"); 6713 verifyFormat("void f() { int a; } // comment"); 6714 verifyFormat("void f() {\n" 6715 "} // comment", 6716 DoNotMerge); 6717 verifyFormat("void f() {\n" 6718 " int a;\n" 6719 "} // comment", 6720 DoNotMerge); 6721 verifyFormat("void f() {\n" 6722 "} // comment", 6723 getLLVMStyleWithColumns(15)); 6724 6725 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6726 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6727 6728 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6729 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6730 verifyFormat("class C {\n" 6731 " C()\n" 6732 " : iiiiiiii(nullptr),\n" 6733 " kkkkkkk(nullptr),\n" 6734 " mmmmmmm(nullptr),\n" 6735 " nnnnnnn(nullptr) {}\n" 6736 "};", 6737 getGoogleStyle()); 6738 6739 FormatStyle NoColumnLimit = getLLVMStyle(); 6740 NoColumnLimit.ColumnLimit = 0; 6741 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6742 EXPECT_EQ("class C {\n" 6743 " A() : b(0) {}\n" 6744 "};", 6745 format("class C{A():b(0){}};", NoColumnLimit)); 6746 EXPECT_EQ("A()\n" 6747 " : b(0) {\n" 6748 "}", 6749 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6750 6751 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6752 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6753 FormatStyle::SFS_None; 6754 EXPECT_EQ("A()\n" 6755 " : b(0) {\n" 6756 "}", 6757 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6758 EXPECT_EQ("A()\n" 6759 " : b(0) {\n" 6760 "}", 6761 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6762 6763 verifyFormat("#define A \\\n" 6764 " void f() { \\\n" 6765 " int i; \\\n" 6766 " }", 6767 getLLVMStyleWithColumns(20)); 6768 verifyFormat("#define A \\\n" 6769 " void f() { int i; }", 6770 getLLVMStyleWithColumns(21)); 6771 verifyFormat("#define A \\\n" 6772 " void f() { \\\n" 6773 " int i; \\\n" 6774 " } \\\n" 6775 " int j;", 6776 getLLVMStyleWithColumns(22)); 6777 verifyFormat("#define A \\\n" 6778 " void f() { int i; } \\\n" 6779 " int j;", 6780 getLLVMStyleWithColumns(23)); 6781 } 6782 6783 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6784 FormatStyle MergeInlineOnly = getLLVMStyle(); 6785 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6786 verifyFormat("class C {\n" 6787 " int f() { return 42; }\n" 6788 "};", 6789 MergeInlineOnly); 6790 verifyFormat("int f() {\n" 6791 " return 42;\n" 6792 "}", 6793 MergeInlineOnly); 6794 } 6795 6796 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6797 // Elaborate type variable declarations. 6798 verifyFormat("struct foo a = {bar};\nint n;"); 6799 verifyFormat("class foo a = {bar};\nint n;"); 6800 verifyFormat("union foo a = {bar};\nint n;"); 6801 6802 // Elaborate types inside function definitions. 6803 verifyFormat("struct foo f() {}\nint n;"); 6804 verifyFormat("class foo f() {}\nint n;"); 6805 verifyFormat("union foo f() {}\nint n;"); 6806 6807 // Templates. 6808 verifyFormat("template <class X> void f() {}\nint n;"); 6809 verifyFormat("template <struct X> void f() {}\nint n;"); 6810 verifyFormat("template <union X> void f() {}\nint n;"); 6811 6812 // Actual definitions... 6813 verifyFormat("struct {\n} n;"); 6814 verifyFormat( 6815 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6816 verifyFormat("union Z {\n int n;\n} x;"); 6817 verifyFormat("class MACRO Z {\n} n;"); 6818 verifyFormat("class MACRO(X) Z {\n} n;"); 6819 verifyFormat("class __attribute__(X) Z {\n} n;"); 6820 verifyFormat("class __declspec(X) Z {\n} n;"); 6821 verifyFormat("class A##B##C {\n} n;"); 6822 verifyFormat("class alignas(16) Z {\n} n;"); 6823 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6824 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6825 6826 // Redefinition from nested context: 6827 verifyFormat("class A::B::C {\n} n;"); 6828 6829 // Template definitions. 6830 verifyFormat( 6831 "template <typename F>\n" 6832 "Matcher(const Matcher<F> &Other,\n" 6833 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6834 " !is_same<F, T>::value>::type * = 0)\n" 6835 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6836 6837 // FIXME: This is still incorrectly handled at the formatter side. 6838 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6839 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6840 6841 // FIXME: 6842 // This now gets parsed incorrectly as class definition. 6843 // verifyFormat("class A<int> f() {\n}\nint n;"); 6844 6845 // Elaborate types where incorrectly parsing the structural element would 6846 // break the indent. 6847 verifyFormat("if (true)\n" 6848 " class X x;\n" 6849 "else\n" 6850 " f();\n"); 6851 6852 // This is simply incomplete. Formatting is not important, but must not crash. 6853 verifyFormat("class A:"); 6854 } 6855 6856 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6857 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6858 format("#error Leave all white!!!!! space* alone!\n")); 6859 EXPECT_EQ( 6860 "#warning Leave all white!!!!! space* alone!\n", 6861 format("#warning Leave all white!!!!! space* alone!\n")); 6862 EXPECT_EQ("#error 1", format(" # error 1")); 6863 EXPECT_EQ("#warning 1", format(" # warning 1")); 6864 } 6865 6866 TEST_F(FormatTest, FormatHashIfExpressions) { 6867 verifyFormat("#if AAAA && BBBB"); 6868 verifyFormat("#if (AAAA && BBBB)"); 6869 verifyFormat("#elif (AAAA && BBBB)"); 6870 // FIXME: Come up with a better indentation for #elif. 6871 verifyFormat( 6872 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6873 " defined(BBBBBBBB)\n" 6874 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6875 " defined(BBBBBBBB)\n" 6876 "#endif", 6877 getLLVMStyleWithColumns(65)); 6878 } 6879 6880 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6881 FormatStyle AllowsMergedIf = getGoogleStyle(); 6882 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6883 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6884 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6885 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6886 EXPECT_EQ("if (true) return 42;", 6887 format("if (true)\nreturn 42;", AllowsMergedIf)); 6888 FormatStyle ShortMergedIf = AllowsMergedIf; 6889 ShortMergedIf.ColumnLimit = 25; 6890 verifyFormat("#define A \\\n" 6891 " if (true) return 42;", 6892 ShortMergedIf); 6893 verifyFormat("#define A \\\n" 6894 " f(); \\\n" 6895 " if (true)\n" 6896 "#define B", 6897 ShortMergedIf); 6898 verifyFormat("#define A \\\n" 6899 " f(); \\\n" 6900 " if (true)\n" 6901 "g();", 6902 ShortMergedIf); 6903 verifyFormat("{\n" 6904 "#ifdef A\n" 6905 " // Comment\n" 6906 " if (true) continue;\n" 6907 "#endif\n" 6908 " // Comment\n" 6909 " if (true) continue;\n" 6910 "}", 6911 ShortMergedIf); 6912 ShortMergedIf.ColumnLimit = 29; 6913 verifyFormat("#define A \\\n" 6914 " if (aaaaaaaaaa) return 1; \\\n" 6915 " return 2;", 6916 ShortMergedIf); 6917 ShortMergedIf.ColumnLimit = 28; 6918 verifyFormat("#define A \\\n" 6919 " if (aaaaaaaaaa) \\\n" 6920 " return 1; \\\n" 6921 " return 2;", 6922 ShortMergedIf); 6923 } 6924 6925 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6926 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6927 " f();\n" 6928 "}"); 6929 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6930 " f();\n" 6931 "} /* another comment */ else /* comment #3 */ {\n" 6932 " g();\n" 6933 "}"); 6934 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6935 " f();\n" 6936 "}"); 6937 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6938 " f();\n" 6939 "}"); 6940 verifyFormat("do /* a comment in a strange place */ {\n" 6941 " f();\n" 6942 "} /* another comment */ while (0);"); 6943 } 6944 6945 TEST_F(FormatTest, BlockComments) { 6946 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6947 format("/* *//* */ /* */\n/* *//* */ /* */")); 6948 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6949 EXPECT_EQ("#define A /*123*/ \\\n" 6950 " b\n" 6951 "/* */\n" 6952 "someCall(\n" 6953 " parameter);", 6954 format("#define A /*123*/ b\n" 6955 "/* */\n" 6956 "someCall(parameter);", 6957 getLLVMStyleWithColumns(15))); 6958 6959 EXPECT_EQ("#define A\n" 6960 "/* */ someCall(\n" 6961 " parameter);", 6962 format("#define A\n" 6963 "/* */someCall(parameter);", 6964 getLLVMStyleWithColumns(15))); 6965 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6966 EXPECT_EQ("/*\n" 6967 "*\n" 6968 " * aaaaaa\n" 6969 " * aaaaaa\n" 6970 "*/", 6971 format("/*\n" 6972 "*\n" 6973 " * aaaaaa aaaaaa\n" 6974 "*/", 6975 getLLVMStyleWithColumns(10))); 6976 EXPECT_EQ("/*\n" 6977 "**\n" 6978 "* aaaaaa\n" 6979 "*aaaaaa\n" 6980 "*/", 6981 format("/*\n" 6982 "**\n" 6983 "* aaaaaa aaaaaa\n" 6984 "*/", 6985 getLLVMStyleWithColumns(10))); 6986 6987 FormatStyle NoBinPacking = getLLVMStyle(); 6988 NoBinPacking.BinPackParameters = false; 6989 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6990 " 2, /* comment 2 */\n" 6991 " 3, /* comment 3 */\n" 6992 " aaaa,\n" 6993 " bbbb);", 6994 format("someFunction (1, /* comment 1 */\n" 6995 " 2, /* comment 2 */ \n" 6996 " 3, /* comment 3 */\n" 6997 "aaaa, bbbb );", 6998 NoBinPacking)); 6999 verifyFormat( 7000 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7002 EXPECT_EQ( 7003 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7004 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 7006 format( 7007 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 7008 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 7010 EXPECT_EQ( 7011 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7012 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7013 "int cccccccccccccccccccccccccccccc; /* comment */\n", 7014 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 7015 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 7016 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 7017 7018 verifyFormat("void f(int * /* unused */) {}"); 7019 7020 EXPECT_EQ("/*\n" 7021 " **\n" 7022 " */", 7023 format("/*\n" 7024 " **\n" 7025 " */")); 7026 EXPECT_EQ("/*\n" 7027 " *q\n" 7028 " */", 7029 format("/*\n" 7030 " *q\n" 7031 " */")); 7032 EXPECT_EQ("/*\n" 7033 " * q\n" 7034 " */", 7035 format("/*\n" 7036 " * q\n" 7037 " */")); 7038 EXPECT_EQ("/*\n" 7039 " **/", 7040 format("/*\n" 7041 " **/")); 7042 EXPECT_EQ("/*\n" 7043 " ***/", 7044 format("/*\n" 7045 " ***/")); 7046 } 7047 7048 TEST_F(FormatTest, BlockCommentsInMacros) { 7049 EXPECT_EQ("#define A \\\n" 7050 " { \\\n" 7051 " /* one line */ \\\n" 7052 " someCall();", 7053 format("#define A { \\\n" 7054 " /* one line */ \\\n" 7055 " someCall();", 7056 getLLVMStyleWithColumns(20))); 7057 EXPECT_EQ("#define A \\\n" 7058 " { \\\n" 7059 " /* previous */ \\\n" 7060 " /* one line */ \\\n" 7061 " someCall();", 7062 format("#define A { \\\n" 7063 " /* previous */ \\\n" 7064 " /* one line */ \\\n" 7065 " someCall();", 7066 getLLVMStyleWithColumns(20))); 7067 } 7068 7069 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7070 EXPECT_EQ("a = {\n" 7071 " 1111 /* */\n" 7072 "};", 7073 format("a = {1111 /* */\n" 7074 "};", 7075 getLLVMStyleWithColumns(15))); 7076 EXPECT_EQ("a = {\n" 7077 " 1111 /* */\n" 7078 "};", 7079 format("a = {1111 /* */\n" 7080 "};", 7081 getLLVMStyleWithColumns(15))); 7082 7083 // FIXME: The formatting is still wrong here. 7084 EXPECT_EQ("a = {\n" 7085 " 1111 /* a\n" 7086 " */\n" 7087 "};", 7088 format("a = {1111 /* a */\n" 7089 "};", 7090 getLLVMStyleWithColumns(15))); 7091 } 7092 7093 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7094 // FIXME: This is not what we want... 7095 verifyFormat("{\n" 7096 "// a" 7097 "// b"); 7098 } 7099 7100 TEST_F(FormatTest, FormatStarDependingOnContext) { 7101 verifyFormat("void f(int *a);"); 7102 verifyFormat("void f() { f(fint * b); }"); 7103 verifyFormat("class A {\n void f(int *a);\n};"); 7104 verifyFormat("class A {\n int *a;\n};"); 7105 verifyFormat("namespace a {\n" 7106 "namespace b {\n" 7107 "class A {\n" 7108 " void f() {}\n" 7109 " int *a;\n" 7110 "};\n" 7111 "}\n" 7112 "}"); 7113 } 7114 7115 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7116 verifyFormat("while"); 7117 verifyFormat("operator"); 7118 } 7119 7120 //===----------------------------------------------------------------------===// 7121 // Objective-C tests. 7122 //===----------------------------------------------------------------------===// 7123 7124 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7125 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7126 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7127 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7128 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7129 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7130 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7131 format("-(NSInteger)Method3:(id)anObject;")); 7132 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7133 format("-(NSInteger)Method4:(id)anObject;")); 7134 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7135 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7136 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7137 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7138 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7139 "forAllCells:(BOOL)flag;", 7140 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7141 "forAllCells:(BOOL)flag;")); 7142 7143 // Very long objectiveC method declaration. 7144 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7145 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7146 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7147 " inRange:(NSRange)range\n" 7148 " outRange:(NSRange)out_range\n" 7149 " outRange1:(NSRange)out_range1\n" 7150 " outRange2:(NSRange)out_range2\n" 7151 " outRange3:(NSRange)out_range3\n" 7152 " outRange4:(NSRange)out_range4\n" 7153 " outRange5:(NSRange)out_range5\n" 7154 " outRange6:(NSRange)out_range6\n" 7155 " outRange7:(NSRange)out_range7\n" 7156 " outRange8:(NSRange)out_range8\n" 7157 " outRange9:(NSRange)out_range9;"); 7158 7159 // When the function name has to be wrapped. 7160 FormatStyle Style = getLLVMStyle(); 7161 Style.IndentWrappedFunctionNames = false; 7162 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7163 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7164 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7165 "}", 7166 Style); 7167 Style.IndentWrappedFunctionNames = true; 7168 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7169 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7170 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7171 "}", 7172 Style); 7173 7174 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7175 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7176 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7177 // protocol lists (but not for template classes): 7178 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7179 7180 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7181 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7182 7183 // If there's no return type (very rare in practice!), LLVM and Google style 7184 // agree. 7185 verifyFormat("- foo;"); 7186 verifyFormat("- foo:(int)f;"); 7187 verifyGoogleFormat("- foo:(int)foo;"); 7188 } 7189 7190 TEST_F(FormatTest, FormatObjCInterface) { 7191 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7192 "@public\n" 7193 " int field1;\n" 7194 "@protected\n" 7195 " int field2;\n" 7196 "@private\n" 7197 " int field3;\n" 7198 "@package\n" 7199 " int field4;\n" 7200 "}\n" 7201 "+ (id)init;\n" 7202 "@end"); 7203 7204 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7205 " @public\n" 7206 " int field1;\n" 7207 " @protected\n" 7208 " int field2;\n" 7209 " @private\n" 7210 " int field3;\n" 7211 " @package\n" 7212 " int field4;\n" 7213 "}\n" 7214 "+ (id)init;\n" 7215 "@end"); 7216 7217 verifyFormat("@interface /* wait for it */ Foo\n" 7218 "+ (id)init;\n" 7219 "// Look, a comment!\n" 7220 "- (int)answerWith:(int)i;\n" 7221 "@end"); 7222 7223 verifyFormat("@interface Foo\n" 7224 "@end\n" 7225 "@interface Bar\n" 7226 "@end"); 7227 7228 verifyFormat("@interface Foo : Bar\n" 7229 "+ (id)init;\n" 7230 "@end"); 7231 7232 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7233 "+ (id)init;\n" 7234 "@end"); 7235 7236 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7237 "+ (id)init;\n" 7238 "@end"); 7239 7240 verifyFormat("@interface Foo (HackStuff)\n" 7241 "+ (id)init;\n" 7242 "@end"); 7243 7244 verifyFormat("@interface Foo ()\n" 7245 "+ (id)init;\n" 7246 "@end"); 7247 7248 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7249 "+ (id)init;\n" 7250 "@end"); 7251 7252 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7253 "+ (id)init;\n" 7254 "@end"); 7255 7256 verifyFormat("@interface Foo {\n" 7257 " int _i;\n" 7258 "}\n" 7259 "+ (id)init;\n" 7260 "@end"); 7261 7262 verifyFormat("@interface Foo : Bar {\n" 7263 " int _i;\n" 7264 "}\n" 7265 "+ (id)init;\n" 7266 "@end"); 7267 7268 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7269 " int _i;\n" 7270 "}\n" 7271 "+ (id)init;\n" 7272 "@end"); 7273 7274 verifyFormat("@interface Foo (HackStuff) {\n" 7275 " int _i;\n" 7276 "}\n" 7277 "+ (id)init;\n" 7278 "@end"); 7279 7280 verifyFormat("@interface Foo () {\n" 7281 " int _i;\n" 7282 "}\n" 7283 "+ (id)init;\n" 7284 "@end"); 7285 7286 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7287 " int _i;\n" 7288 "}\n" 7289 "+ (id)init;\n" 7290 "@end"); 7291 7292 FormatStyle OnePerLine = getGoogleStyle(); 7293 OnePerLine.BinPackParameters = false; 7294 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7299 "}", 7300 OnePerLine); 7301 } 7302 7303 TEST_F(FormatTest, FormatObjCImplementation) { 7304 verifyFormat("@implementation Foo : NSObject {\n" 7305 "@public\n" 7306 " int field1;\n" 7307 "@protected\n" 7308 " int field2;\n" 7309 "@private\n" 7310 " int field3;\n" 7311 "@package\n" 7312 " int field4;\n" 7313 "}\n" 7314 "+ (id)init {\n}\n" 7315 "@end"); 7316 7317 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7318 " @public\n" 7319 " int field1;\n" 7320 " @protected\n" 7321 " int field2;\n" 7322 " @private\n" 7323 " int field3;\n" 7324 " @package\n" 7325 " int field4;\n" 7326 "}\n" 7327 "+ (id)init {\n}\n" 7328 "@end"); 7329 7330 verifyFormat("@implementation Foo\n" 7331 "+ (id)init {\n" 7332 " if (true)\n" 7333 " return nil;\n" 7334 "}\n" 7335 "// Look, a comment!\n" 7336 "- (int)answerWith:(int)i {\n" 7337 " return i;\n" 7338 "}\n" 7339 "+ (int)answerWith:(int)i {\n" 7340 " return i;\n" 7341 "}\n" 7342 "@end"); 7343 7344 verifyFormat("@implementation Foo\n" 7345 "@end\n" 7346 "@implementation Bar\n" 7347 "@end"); 7348 7349 EXPECT_EQ("@implementation Foo : Bar\n" 7350 "+ (id)init {\n}\n" 7351 "- (void)foo {\n}\n" 7352 "@end", 7353 format("@implementation Foo : Bar\n" 7354 "+(id)init{}\n" 7355 "-(void)foo{}\n" 7356 "@end")); 7357 7358 verifyFormat("@implementation Foo {\n" 7359 " int _i;\n" 7360 "}\n" 7361 "+ (id)init {\n}\n" 7362 "@end"); 7363 7364 verifyFormat("@implementation Foo : Bar {\n" 7365 " int _i;\n" 7366 "}\n" 7367 "+ (id)init {\n}\n" 7368 "@end"); 7369 7370 verifyFormat("@implementation Foo (HackStuff)\n" 7371 "+ (id)init {\n}\n" 7372 "@end"); 7373 verifyFormat("@implementation ObjcClass\n" 7374 "- (void)method;\n" 7375 "{}\n" 7376 "@end"); 7377 } 7378 7379 TEST_F(FormatTest, FormatObjCProtocol) { 7380 verifyFormat("@protocol Foo\n" 7381 "@property(weak) id delegate;\n" 7382 "- (NSUInteger)numberOfThings;\n" 7383 "@end"); 7384 7385 verifyFormat("@protocol MyProtocol <NSObject>\n" 7386 "- (NSUInteger)numberOfThings;\n" 7387 "@end"); 7388 7389 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7390 "- (NSUInteger)numberOfThings;\n" 7391 "@end"); 7392 7393 verifyFormat("@protocol Foo;\n" 7394 "@protocol Bar;\n"); 7395 7396 verifyFormat("@protocol Foo\n" 7397 "@end\n" 7398 "@protocol Bar\n" 7399 "@end"); 7400 7401 verifyFormat("@protocol myProtocol\n" 7402 "- (void)mandatoryWithInt:(int)i;\n" 7403 "@optional\n" 7404 "- (void)optional;\n" 7405 "@required\n" 7406 "- (void)required;\n" 7407 "@optional\n" 7408 "@property(assign) int madProp;\n" 7409 "@end\n"); 7410 7411 verifyFormat("@property(nonatomic, assign, readonly)\n" 7412 " int *looooooooooooooooooooooooooooongNumber;\n" 7413 "@property(nonatomic, assign, readonly)\n" 7414 " NSString *looooooooooooooooooooooooooooongName;"); 7415 7416 verifyFormat("@implementation PR18406\n" 7417 "}\n" 7418 "@end"); 7419 } 7420 7421 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7422 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7423 " rect:(NSRect)theRect\n" 7424 " interval:(float)theInterval {\n" 7425 "}"); 7426 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7427 " longKeyword:(NSRect)theRect\n" 7428 " longerKeyword:(float)theInterval\n" 7429 " error:(NSError **)theError {\n" 7430 "}"); 7431 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7432 " longKeyword:(NSRect)theRect\n" 7433 " evenLongerKeyword:(float)theInterval\n" 7434 " error:(NSError **)theError {\n" 7435 "}"); 7436 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7437 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7438 " NS_DESIGNATED_INITIALIZER;", 7439 getLLVMStyleWithColumns(60)); 7440 7441 // Continuation indent width should win over aligning colons if the function 7442 // name is long. 7443 FormatStyle continuationStyle = getGoogleStyle(); 7444 continuationStyle.ColumnLimit = 40; 7445 continuationStyle.IndentWrappedFunctionNames = true; 7446 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7447 " dontAlignNamef:(NSRect)theRect {\n" 7448 "}", 7449 continuationStyle); 7450 7451 // Make sure we don't break aligning for short parameter names. 7452 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7453 " aShortf:(NSRect)theRect {\n" 7454 "}", 7455 continuationStyle); 7456 } 7457 7458 TEST_F(FormatTest, FormatObjCMethodExpr) { 7459 verifyFormat("[foo bar:baz];"); 7460 verifyFormat("return [foo bar:baz];"); 7461 verifyFormat("return (a)[foo bar:baz];"); 7462 verifyFormat("f([foo bar:baz]);"); 7463 verifyFormat("f(2, [foo bar:baz]);"); 7464 verifyFormat("f(2, a ? b : c);"); 7465 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7466 7467 // Unary operators. 7468 verifyFormat("int a = +[foo bar:baz];"); 7469 verifyFormat("int a = -[foo bar:baz];"); 7470 verifyFormat("int a = ![foo bar:baz];"); 7471 verifyFormat("int a = ~[foo bar:baz];"); 7472 verifyFormat("int a = ++[foo bar:baz];"); 7473 verifyFormat("int a = --[foo bar:baz];"); 7474 verifyFormat("int a = sizeof [foo bar:baz];"); 7475 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7476 verifyFormat("int a = &[foo bar:baz];"); 7477 verifyFormat("int a = *[foo bar:baz];"); 7478 // FIXME: Make casts work, without breaking f()[4]. 7479 // verifyFormat("int a = (int)[foo bar:baz];"); 7480 // verifyFormat("return (int)[foo bar:baz];"); 7481 // verifyFormat("(void)[foo bar:baz];"); 7482 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7483 7484 // Binary operators. 7485 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7486 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7487 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7488 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7489 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7490 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7491 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7492 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7493 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7494 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7495 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7496 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7497 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7498 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7499 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7500 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7501 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7502 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7503 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7504 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7505 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7506 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7507 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7508 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7509 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7510 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7511 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7512 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7513 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7514 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7515 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7516 // Whew! 7517 7518 verifyFormat("return in[42];"); 7519 verifyFormat("for (auto v : in[1]) {\n}"); 7520 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7521 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7522 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7523 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7524 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7525 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7526 "}"); 7527 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7528 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7529 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7530 7531 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7532 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7533 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7534 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7535 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7536 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7537 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7538 7539 verifyFormat("arr[[self indexForFoo:a]];"); 7540 verifyFormat("throw [self errorFor:a];"); 7541 verifyFormat("@throw [self errorFor:a];"); 7542 7543 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7544 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7545 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7546 7547 // This tests that the formatter doesn't break after "backing" but before ":", 7548 // which would be at 80 columns. 7549 verifyFormat( 7550 "void f() {\n" 7551 " if ((self = [super initWithContentRect:contentRect\n" 7552 " styleMask:styleMask ?: otherMask\n" 7553 " backing:NSBackingStoreBuffered\n" 7554 " defer:YES]))"); 7555 7556 verifyFormat( 7557 "[foo checkThatBreakingAfterColonWorksOk:\n" 7558 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7559 7560 verifyFormat("[myObj short:arg1 // Force line break\n" 7561 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7562 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7563 " error:arg4];"); 7564 verifyFormat( 7565 "void f() {\n" 7566 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7567 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7568 " pos.width(), pos.height())\n" 7569 " styleMask:NSBorderlessWindowMask\n" 7570 " backing:NSBackingStoreBuffered\n" 7571 " defer:NO]);\n" 7572 "}"); 7573 verifyFormat( 7574 "void f() {\n" 7575 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7576 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7577 " pos.width(), pos.height())\n" 7578 " syeMask:NSBorderlessWindowMask\n" 7579 " bking:NSBackingStoreBuffered\n" 7580 " der:NO]);\n" 7581 "}", 7582 getLLVMStyleWithColumns(70)); 7583 verifyFormat( 7584 "void f() {\n" 7585 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7586 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7587 " pos.width(), pos.height())\n" 7588 " styleMask:NSBorderlessWindowMask\n" 7589 " backing:NSBackingStoreBuffered\n" 7590 " defer:NO]);\n" 7591 "}", 7592 getChromiumStyle(FormatStyle::LK_Cpp)); 7593 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7594 " with:contentsNativeView];"); 7595 7596 verifyFormat( 7597 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7598 " owner:nillllll];"); 7599 7600 verifyFormat( 7601 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7602 " forType:kBookmarkButtonDragType];"); 7603 7604 verifyFormat("[defaultCenter addObserver:self\n" 7605 " selector:@selector(willEnterFullscreen)\n" 7606 " name:kWillEnterFullscreenNotification\n" 7607 " object:nil];"); 7608 verifyFormat("[image_rep drawInRect:drawRect\n" 7609 " fromRect:NSZeroRect\n" 7610 " operation:NSCompositeCopy\n" 7611 " fraction:1.0\n" 7612 " respectFlipped:NO\n" 7613 " hints:nil];"); 7614 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7616 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7618 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7619 " aaaaaaaaaaaaaaaaaaaaaa];"); 7620 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7621 " .aaaaaaaa];", // FIXME: Indentation seems off. 7622 getLLVMStyleWithColumns(60)); 7623 7624 verifyFormat( 7625 "scoped_nsobject<NSTextField> message(\n" 7626 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7627 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7628 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7629 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7630 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7631 " aaaa:bbb];"); 7632 verifyFormat("[self param:function( //\n" 7633 " parameter)]"); 7634 verifyFormat( 7635 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7636 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7637 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7638 7639 // FIXME: This violates the column limit. 7640 verifyFormat( 7641 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7642 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7643 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7644 getLLVMStyleWithColumns(60)); 7645 7646 // Variadic parameters. 7647 verifyFormat( 7648 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7649 verifyFormat( 7650 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7651 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7652 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7653 verifyFormat("[self // break\n" 7654 " a:a\n" 7655 " aaa:aaa];"); 7656 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7657 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7658 } 7659 7660 TEST_F(FormatTest, ObjCAt) { 7661 verifyFormat("@autoreleasepool"); 7662 verifyFormat("@catch"); 7663 verifyFormat("@class"); 7664 verifyFormat("@compatibility_alias"); 7665 verifyFormat("@defs"); 7666 verifyFormat("@dynamic"); 7667 verifyFormat("@encode"); 7668 verifyFormat("@end"); 7669 verifyFormat("@finally"); 7670 verifyFormat("@implementation"); 7671 verifyFormat("@import"); 7672 verifyFormat("@interface"); 7673 verifyFormat("@optional"); 7674 verifyFormat("@package"); 7675 verifyFormat("@private"); 7676 verifyFormat("@property"); 7677 verifyFormat("@protected"); 7678 verifyFormat("@protocol"); 7679 verifyFormat("@public"); 7680 verifyFormat("@required"); 7681 verifyFormat("@selector"); 7682 verifyFormat("@synchronized"); 7683 verifyFormat("@synthesize"); 7684 verifyFormat("@throw"); 7685 verifyFormat("@try"); 7686 7687 EXPECT_EQ("@interface", format("@ interface")); 7688 7689 // The precise formatting of this doesn't matter, nobody writes code like 7690 // this. 7691 verifyFormat("@ /*foo*/ interface"); 7692 } 7693 7694 TEST_F(FormatTest, ObjCSnippets) { 7695 verifyFormat("@autoreleasepool {\n" 7696 " foo();\n" 7697 "}"); 7698 verifyFormat("@class Foo, Bar;"); 7699 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7700 verifyFormat("@dynamic textColor;"); 7701 verifyFormat("char *buf1 = @encode(int *);"); 7702 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7703 verifyFormat("char *buf1 = @encode(int **);"); 7704 verifyFormat("Protocol *proto = @protocol(p1);"); 7705 verifyFormat("SEL s = @selector(foo:);"); 7706 verifyFormat("@synchronized(self) {\n" 7707 " f();\n" 7708 "}"); 7709 7710 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7711 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7712 7713 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7714 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7715 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7716 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7717 getMozillaStyle()); 7718 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7719 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7720 getWebKitStyle()); 7721 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7722 7723 verifyFormat("@import foo.bar;\n" 7724 "@import baz;"); 7725 } 7726 7727 TEST_F(FormatTest, ObjCForIn) { 7728 verifyFormat("- (void)test {\n" 7729 " for (NSString *n in arrayOfStrings) {\n" 7730 " foo(n);\n" 7731 " }\n" 7732 "}"); 7733 verifyFormat("- (void)test {\n" 7734 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7735 " foo(n);\n" 7736 " }\n" 7737 "}"); 7738 } 7739 7740 TEST_F(FormatTest, ObjCLiterals) { 7741 verifyFormat("@\"String\""); 7742 verifyFormat("@1"); 7743 verifyFormat("@+4.8"); 7744 verifyFormat("@-4"); 7745 verifyFormat("@1LL"); 7746 verifyFormat("@.5"); 7747 verifyFormat("@'c'"); 7748 verifyFormat("@true"); 7749 7750 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7751 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7752 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7753 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7754 7755 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7756 } 7757 7758 TEST_F(FormatTest, ObjCDictLiterals) { 7759 verifyFormat("@{"); 7760 verifyFormat("@{}"); 7761 verifyFormat("@{@\"one\" : @1}"); 7762 verifyFormat("return @{@\"one\" : @1;"); 7763 verifyFormat("@{@\"one\" : @1}"); 7764 7765 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7766 verifyFormat("@{\n" 7767 " @\"one\" : @{@2 : @1},\n" 7768 "}"); 7769 7770 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7771 verifyIncompleteFormat("[self setDict:@{}"); 7772 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7773 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7774 verifyFormat( 7775 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7776 verifyFormat( 7777 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7778 7779 verifyFormat("NSDictionary *d = @{\n" 7780 " @\"nam\" : NSUserNam(),\n" 7781 " @\"dte\" : [NSDate date],\n" 7782 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7783 "};"); 7784 verifyFormat( 7785 "@{\n" 7786 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7787 "regularFont,\n" 7788 "};"); 7789 verifyGoogleFormat( 7790 "@{\n" 7791 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7792 "regularFont,\n" 7793 "};"); 7794 verifyFormat( 7795 "@{\n" 7796 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7797 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7798 "};"); 7799 7800 // We should try to be robust in case someone forgets the "@". 7801 verifyFormat("NSDictionary *d = {\n" 7802 " @\"nam\" : NSUserNam(),\n" 7803 " @\"dte\" : [NSDate date],\n" 7804 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7805 "};"); 7806 verifyFormat("NSMutableDictionary *dictionary =\n" 7807 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7808 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7809 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7810 " cccccccccccccccc : ccccccccccccccc\n" 7811 " }];"); 7812 7813 // Ensure that casts before the key are kept on the same line as the key. 7814 verifyFormat( 7815 "NSDictionary *d = @{\n" 7816 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7817 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7818 "};"); 7819 } 7820 7821 TEST_F(FormatTest, ObjCArrayLiterals) { 7822 verifyIncompleteFormat("@["); 7823 verifyFormat("@[]"); 7824 verifyFormat( 7825 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7826 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7827 verifyFormat("NSArray *array = @[ [foo description] ];"); 7828 7829 verifyFormat( 7830 "NSArray *some_variable = @[\n" 7831 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7832 " @\"aaaaaaaaaaaaaaaaa\",\n" 7833 " @\"aaaaaaaaaaaaaaaaa\",\n" 7834 " @\"aaaaaaaaaaaaaaaaa\",\n" 7835 "];"); 7836 verifyFormat( 7837 "NSArray *some_variable = @[\n" 7838 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7839 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7840 "];"); 7841 verifyFormat("NSArray *some_variable = @[\n" 7842 " @\"aaaaaaaaaaaaaaaaa\",\n" 7843 " @\"aaaaaaaaaaaaaaaaa\",\n" 7844 " @\"aaaaaaaaaaaaaaaaa\",\n" 7845 " @\"aaaaaaaaaaaaaaaaa\",\n" 7846 "];"); 7847 verifyFormat("NSArray *array = @[\n" 7848 " @\"a\",\n" 7849 " @\"a\",\n" // Trailing comma -> one per line. 7850 "];"); 7851 7852 // We should try to be robust in case someone forgets the "@". 7853 verifyFormat("NSArray *some_variable = [\n" 7854 " @\"aaaaaaaaaaaaaaaaa\",\n" 7855 " @\"aaaaaaaaaaaaaaaaa\",\n" 7856 " @\"aaaaaaaaaaaaaaaaa\",\n" 7857 " @\"aaaaaaaaaaaaaaaaa\",\n" 7858 "];"); 7859 verifyFormat( 7860 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7861 " index:(NSUInteger)index\n" 7862 " nonDigitAttributes:\n" 7863 " (NSDictionary *)noDigitAttributes;"); 7864 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7865 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7866 "]];"); 7867 } 7868 7869 TEST_F(FormatTest, BreaksStringLiterals) { 7870 EXPECT_EQ("\"some text \"\n" 7871 "\"other\";", 7872 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7873 EXPECT_EQ("\"some text \"\n" 7874 "\"other\";", 7875 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7876 EXPECT_EQ( 7877 "#define A \\\n" 7878 " \"some \" \\\n" 7879 " \"text \" \\\n" 7880 " \"other\";", 7881 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7882 EXPECT_EQ( 7883 "#define A \\\n" 7884 " \"so \" \\\n" 7885 " \"text \" \\\n" 7886 " \"other\";", 7887 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7888 7889 EXPECT_EQ("\"some text\"", 7890 format("\"some text\"", getLLVMStyleWithColumns(1))); 7891 EXPECT_EQ("\"some text\"", 7892 format("\"some text\"", getLLVMStyleWithColumns(11))); 7893 EXPECT_EQ("\"some \"\n" 7894 "\"text\"", 7895 format("\"some text\"", getLLVMStyleWithColumns(10))); 7896 EXPECT_EQ("\"some \"\n" 7897 "\"text\"", 7898 format("\"some text\"", getLLVMStyleWithColumns(7))); 7899 EXPECT_EQ("\"some\"\n" 7900 "\" tex\"\n" 7901 "\"t\"", 7902 format("\"some text\"", getLLVMStyleWithColumns(6))); 7903 EXPECT_EQ("\"some\"\n" 7904 "\" tex\"\n" 7905 "\" and\"", 7906 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7907 EXPECT_EQ("\"some\"\n" 7908 "\"/tex\"\n" 7909 "\"/and\"", 7910 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7911 7912 EXPECT_EQ("variable =\n" 7913 " \"long string \"\n" 7914 " \"literal\";", 7915 format("variable = \"long string literal\";", 7916 getLLVMStyleWithColumns(20))); 7917 7918 EXPECT_EQ("variable = f(\n" 7919 " \"long string \"\n" 7920 " \"literal\",\n" 7921 " short,\n" 7922 " loooooooooooooooooooong);", 7923 format("variable = f(\"long string literal\", short, " 7924 "loooooooooooooooooooong);", 7925 getLLVMStyleWithColumns(20))); 7926 7927 EXPECT_EQ( 7928 "f(g(\"long string \"\n" 7929 " \"literal\"),\n" 7930 " b);", 7931 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7932 EXPECT_EQ("f(g(\"long string \"\n" 7933 " \"literal\",\n" 7934 " a),\n" 7935 " b);", 7936 format("f(g(\"long string literal\", a), b);", 7937 getLLVMStyleWithColumns(20))); 7938 EXPECT_EQ( 7939 "f(\"one two\".split(\n" 7940 " variable));", 7941 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7942 EXPECT_EQ("f(\"one two three four five six \"\n" 7943 " \"seven\".split(\n" 7944 " really_looooong_variable));", 7945 format("f(\"one two three four five six seven\"." 7946 "split(really_looooong_variable));", 7947 getLLVMStyleWithColumns(33))); 7948 7949 EXPECT_EQ("f(\"some \"\n" 7950 " \"text\",\n" 7951 " other);", 7952 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7953 7954 // Only break as a last resort. 7955 verifyFormat( 7956 "aaaaaaaaaaaaaaaaaaaa(\n" 7957 " aaaaaaaaaaaaaaaaaaaa,\n" 7958 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7959 7960 EXPECT_EQ("\"splitmea\"\n" 7961 "\"trandomp\"\n" 7962 "\"oint\"", 7963 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7964 7965 EXPECT_EQ("\"split/\"\n" 7966 "\"pathat/\"\n" 7967 "\"slashes\"", 7968 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7969 7970 EXPECT_EQ("\"split/\"\n" 7971 "\"pathat/\"\n" 7972 "\"slashes\"", 7973 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7974 EXPECT_EQ("\"split at \"\n" 7975 "\"spaces/at/\"\n" 7976 "\"slashes.at.any$\"\n" 7977 "\"non-alphanumeric%\"\n" 7978 "\"1111111111characte\"\n" 7979 "\"rs\"", 7980 format("\"split at " 7981 "spaces/at/" 7982 "slashes.at." 7983 "any$non-" 7984 "alphanumeric%" 7985 "1111111111characte" 7986 "rs\"", 7987 getLLVMStyleWithColumns(20))); 7988 7989 // Verify that splitting the strings understands 7990 // Style::AlwaysBreakBeforeMultilineStrings. 7991 EXPECT_EQ( 7992 "aaaaaaaaaaaa(\n" 7993 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7994 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7995 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7996 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7997 "aaaaaaaaaaaaaaaaaaaaaa\");", 7998 getGoogleStyle())); 7999 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8000 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8001 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8002 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8003 "aaaaaaaaaaaaaaaaaaaaaa\";", 8004 getGoogleStyle())); 8005 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8006 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8007 format("llvm::outs() << " 8008 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8009 "aaaaaaaaaaaaaaaaaaa\";")); 8010 EXPECT_EQ("ffff(\n" 8011 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8012 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8013 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8014 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8015 getGoogleStyle())); 8016 8017 FormatStyle Style = getLLVMStyleWithColumns(12); 8018 Style.BreakStringLiterals = false; 8019 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8020 8021 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8022 AlignLeft.AlignEscapedNewlinesLeft = true; 8023 EXPECT_EQ("#define A \\\n" 8024 " \"some \" \\\n" 8025 " \"text \" \\\n" 8026 " \"other\";", 8027 format("#define A \"some text other\";", AlignLeft)); 8028 } 8029 8030 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8031 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8032 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8033 EXPECT_EQ("int i = a(b());", 8034 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8035 } 8036 8037 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8038 EXPECT_EQ( 8039 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8040 "(\n" 8041 " \"x\t\");", 8042 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8043 "aaaaaaa(" 8044 "\"x\t\");")); 8045 } 8046 8047 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8048 EXPECT_EQ( 8049 "u8\"utf8 string \"\n" 8050 "u8\"literal\";", 8051 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8052 EXPECT_EQ( 8053 "u\"utf16 string \"\n" 8054 "u\"literal\";", 8055 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8056 EXPECT_EQ( 8057 "U\"utf32 string \"\n" 8058 "U\"literal\";", 8059 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8060 EXPECT_EQ("L\"wide string \"\n" 8061 "L\"literal\";", 8062 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8063 EXPECT_EQ("@\"NSString \"\n" 8064 "@\"literal\";", 8065 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8066 8067 // This input makes clang-format try to split the incomplete unicode escape 8068 // sequence, which used to lead to a crasher. 8069 verifyNoCrash( 8070 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8071 getLLVMStyleWithColumns(60)); 8072 } 8073 8074 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8075 FormatStyle Style = getGoogleStyleWithColumns(15); 8076 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8077 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8078 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8079 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8080 EXPECT_EQ("u8R\"x(raw literal)x\";", 8081 format("u8R\"x(raw literal)x\";", Style)); 8082 } 8083 8084 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8085 FormatStyle Style = getLLVMStyleWithColumns(20); 8086 EXPECT_EQ( 8087 "_T(\"aaaaaaaaaaaaaa\")\n" 8088 "_T(\"aaaaaaaaaaaaaa\")\n" 8089 "_T(\"aaaaaaaaaaaa\")", 8090 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8091 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8092 " _T(\"aaaaaa\"),\n" 8093 " z);", 8094 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8095 8096 // FIXME: Handle embedded spaces in one iteration. 8097 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8098 // "_T(\"aaaaaaaaaaaaa\")\n" 8099 // "_T(\"aaaaaaaaaaaaa\")\n" 8100 // "_T(\"a\")", 8101 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8102 // getLLVMStyleWithColumns(20))); 8103 EXPECT_EQ( 8104 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8105 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8106 EXPECT_EQ("f(\n" 8107 "#if !TEST\n" 8108 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8109 "#endif\n" 8110 " );", 8111 format("f(\n" 8112 "#if !TEST\n" 8113 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8114 "#endif\n" 8115 ");")); 8116 EXPECT_EQ("f(\n" 8117 "\n" 8118 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8119 format("f(\n" 8120 "\n" 8121 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8122 } 8123 8124 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8125 EXPECT_EQ( 8126 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8129 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8132 } 8133 8134 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8135 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8136 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8137 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8138 "multiline raw string literal xxxxxxxxxxxxxx\n" 8139 ")x\",\n" 8140 " a),\n" 8141 " b);", 8142 format("fffffffffff(g(R\"x(\n" 8143 "multiline raw string literal xxxxxxxxxxxxxx\n" 8144 ")x\", a), b);", 8145 getGoogleStyleWithColumns(20))); 8146 EXPECT_EQ("fffffffffff(\n" 8147 " g(R\"x(qqq\n" 8148 "multiline raw string literal xxxxxxxxxxxxxx\n" 8149 ")x\",\n" 8150 " a),\n" 8151 " b);", 8152 format("fffffffffff(g(R\"x(qqq\n" 8153 "multiline raw string literal xxxxxxxxxxxxxx\n" 8154 ")x\", a), b);", 8155 getGoogleStyleWithColumns(20))); 8156 8157 EXPECT_EQ("fffffffffff(R\"x(\n" 8158 "multiline raw string literal xxxxxxxxxxxxxx\n" 8159 ")x\");", 8160 format("fffffffffff(R\"x(\n" 8161 "multiline raw string literal xxxxxxxxxxxxxx\n" 8162 ")x\");", 8163 getGoogleStyleWithColumns(20))); 8164 EXPECT_EQ("fffffffffff(R\"x(\n" 8165 "multiline raw string literal xxxxxxxxxxxxxx\n" 8166 ")x\" + bbbbbb);", 8167 format("fffffffffff(R\"x(\n" 8168 "multiline raw string literal xxxxxxxxxxxxxx\n" 8169 ")x\" + bbbbbb);", 8170 getGoogleStyleWithColumns(20))); 8171 EXPECT_EQ("fffffffffff(\n" 8172 " R\"x(\n" 8173 "multiline raw string literal xxxxxxxxxxxxxx\n" 8174 ")x\" +\n" 8175 " bbbbbb);", 8176 format("fffffffffff(\n" 8177 " R\"x(\n" 8178 "multiline raw string literal xxxxxxxxxxxxxx\n" 8179 ")x\" + bbbbbb);", 8180 getGoogleStyleWithColumns(20))); 8181 } 8182 8183 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8184 verifyFormat("string a = \"unterminated;"); 8185 EXPECT_EQ("function(\"unterminated,\n" 8186 " OtherParameter);", 8187 format("function( \"unterminated,\n" 8188 " OtherParameter);")); 8189 } 8190 8191 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8192 FormatStyle Style = getLLVMStyle(); 8193 Style.Standard = FormatStyle::LS_Cpp03; 8194 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8195 format("#define x(_a) printf(\"foo\"_a);", Style)); 8196 } 8197 8198 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8199 8200 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8201 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8202 " \"ddeeefff\");", 8203 format("someFunction(\"aaabbbcccdddeeefff\");", 8204 getLLVMStyleWithColumns(25))); 8205 EXPECT_EQ("someFunction1234567890(\n" 8206 " \"aaabbbcccdddeeefff\");", 8207 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8208 getLLVMStyleWithColumns(26))); 8209 EXPECT_EQ("someFunction1234567890(\n" 8210 " \"aaabbbcccdddeeeff\"\n" 8211 " \"f\");", 8212 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8213 getLLVMStyleWithColumns(25))); 8214 EXPECT_EQ("someFunction1234567890(\n" 8215 " \"aaabbbcccdddeeeff\"\n" 8216 " \"f\");", 8217 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8218 getLLVMStyleWithColumns(24))); 8219 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8220 " \"ddde \"\n" 8221 " \"efff\");", 8222 format("someFunction(\"aaabbbcc ddde efff\");", 8223 getLLVMStyleWithColumns(25))); 8224 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8225 " \"ddeeefff\");", 8226 format("someFunction(\"aaabbbccc ddeeefff\");", 8227 getLLVMStyleWithColumns(25))); 8228 EXPECT_EQ("someFunction1234567890(\n" 8229 " \"aaabb \"\n" 8230 " \"cccdddeeefff\");", 8231 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8232 getLLVMStyleWithColumns(25))); 8233 EXPECT_EQ("#define A \\\n" 8234 " string s = \\\n" 8235 " \"123456789\" \\\n" 8236 " \"0\"; \\\n" 8237 " int i;", 8238 format("#define A string s = \"1234567890\"; int i;", 8239 getLLVMStyleWithColumns(20))); 8240 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8241 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8242 " \"dddeeeff\"\n" 8243 " \"f\");", 8244 format("someFunction(\"aaabbbcc dddeeefff\");", 8245 getLLVMStyleWithColumns(25))); 8246 } 8247 8248 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8249 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8250 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8251 EXPECT_EQ("\"test\"\n" 8252 "\"\\n\"", 8253 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8254 EXPECT_EQ("\"tes\\\\\"\n" 8255 "\"n\"", 8256 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8257 EXPECT_EQ("\"\\\\\\\\\"\n" 8258 "\"\\n\"", 8259 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8260 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8261 EXPECT_EQ("\"\\uff01\"\n" 8262 "\"test\"", 8263 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8264 EXPECT_EQ("\"\\Uff01ff02\"", 8265 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8266 EXPECT_EQ("\"\\x000000000001\"\n" 8267 "\"next\"", 8268 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8269 EXPECT_EQ("\"\\x000000000001next\"", 8270 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8271 EXPECT_EQ("\"\\x000000000001\"", 8272 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8273 EXPECT_EQ("\"test\"\n" 8274 "\"\\000000\"\n" 8275 "\"000001\"", 8276 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8277 EXPECT_EQ("\"test\\000\"\n" 8278 "\"00000000\"\n" 8279 "\"1\"", 8280 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8281 } 8282 8283 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8284 verifyFormat("void f() {\n" 8285 " return g() {}\n" 8286 " void h() {}"); 8287 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8288 "g();\n" 8289 "}"); 8290 } 8291 8292 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8293 verifyFormat( 8294 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8295 } 8296 8297 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8298 verifyFormat("class X {\n" 8299 " void f() {\n" 8300 " }\n" 8301 "};", 8302 getLLVMStyleWithColumns(12)); 8303 } 8304 8305 TEST_F(FormatTest, ConfigurableIndentWidth) { 8306 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8307 EightIndent.IndentWidth = 8; 8308 EightIndent.ContinuationIndentWidth = 8; 8309 verifyFormat("void f() {\n" 8310 " someFunction();\n" 8311 " if (true) {\n" 8312 " f();\n" 8313 " }\n" 8314 "}", 8315 EightIndent); 8316 verifyFormat("class X {\n" 8317 " void f() {\n" 8318 " }\n" 8319 "};", 8320 EightIndent); 8321 verifyFormat("int x[] = {\n" 8322 " call(),\n" 8323 " call()};", 8324 EightIndent); 8325 } 8326 8327 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8328 verifyFormat("double\n" 8329 "f();", 8330 getLLVMStyleWithColumns(8)); 8331 } 8332 8333 TEST_F(FormatTest, ConfigurableUseOfTab) { 8334 FormatStyle Tab = getLLVMStyleWithColumns(42); 8335 Tab.IndentWidth = 8; 8336 Tab.UseTab = FormatStyle::UT_Always; 8337 Tab.AlignEscapedNewlinesLeft = true; 8338 8339 EXPECT_EQ("if (aaaaaaaa && // q\n" 8340 " bb)\t\t// w\n" 8341 "\t;", 8342 format("if (aaaaaaaa &&// q\n" 8343 "bb)// w\n" 8344 ";", 8345 Tab)); 8346 EXPECT_EQ("if (aaa && bbb) // w\n" 8347 "\t;", 8348 format("if(aaa&&bbb)// w\n" 8349 ";", 8350 Tab)); 8351 8352 verifyFormat("class X {\n" 8353 "\tvoid f() {\n" 8354 "\t\tsomeFunction(parameter1,\n" 8355 "\t\t\t parameter2);\n" 8356 "\t}\n" 8357 "};", 8358 Tab); 8359 verifyFormat("#define A \\\n" 8360 "\tvoid f() { \\\n" 8361 "\t\tsomeFunction( \\\n" 8362 "\t\t parameter1, \\\n" 8363 "\t\t parameter2); \\\n" 8364 "\t}", 8365 Tab); 8366 8367 Tab.TabWidth = 4; 8368 Tab.IndentWidth = 8; 8369 verifyFormat("class TabWidth4Indent8 {\n" 8370 "\t\tvoid f() {\n" 8371 "\t\t\t\tsomeFunction(parameter1,\n" 8372 "\t\t\t\t\t\t\t parameter2);\n" 8373 "\t\t}\n" 8374 "};", 8375 Tab); 8376 8377 Tab.TabWidth = 4; 8378 Tab.IndentWidth = 4; 8379 verifyFormat("class TabWidth4Indent4 {\n" 8380 "\tvoid f() {\n" 8381 "\t\tsomeFunction(parameter1,\n" 8382 "\t\t\t\t\t parameter2);\n" 8383 "\t}\n" 8384 "};", 8385 Tab); 8386 8387 Tab.TabWidth = 8; 8388 Tab.IndentWidth = 4; 8389 verifyFormat("class TabWidth8Indent4 {\n" 8390 " void f() {\n" 8391 "\tsomeFunction(parameter1,\n" 8392 "\t\t parameter2);\n" 8393 " }\n" 8394 "};", 8395 Tab); 8396 8397 Tab.TabWidth = 8; 8398 Tab.IndentWidth = 8; 8399 EXPECT_EQ("/*\n" 8400 "\t a\t\tcomment\n" 8401 "\t in multiple lines\n" 8402 " */", 8403 format(" /*\t \t \n" 8404 " \t \t a\t\tcomment\t \t\n" 8405 " \t \t in multiple lines\t\n" 8406 " \t */", 8407 Tab)); 8408 8409 Tab.UseTab = FormatStyle::UT_ForIndentation; 8410 verifyFormat("{\n" 8411 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8412 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8413 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8414 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8415 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8416 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8417 "};", 8418 Tab); 8419 verifyFormat("enum AA {\n" 8420 "\ta1, // Force multiple lines\n" 8421 "\ta2,\n" 8422 "\ta3\n" 8423 "};", 8424 Tab); 8425 EXPECT_EQ("if (aaaaaaaa && // q\n" 8426 " bb) // w\n" 8427 "\t;", 8428 format("if (aaaaaaaa &&// q\n" 8429 "bb)// w\n" 8430 ";", 8431 Tab)); 8432 verifyFormat("class X {\n" 8433 "\tvoid f() {\n" 8434 "\t\tsomeFunction(parameter1,\n" 8435 "\t\t parameter2);\n" 8436 "\t}\n" 8437 "};", 8438 Tab); 8439 verifyFormat("{\n" 8440 "\tQ(\n" 8441 "\t {\n" 8442 "\t\t int a;\n" 8443 "\t\t someFunction(aaaaaaaa,\n" 8444 "\t\t bbbbbbb);\n" 8445 "\t },\n" 8446 "\t p);\n" 8447 "}", 8448 Tab); 8449 EXPECT_EQ("{\n" 8450 "\t/* aaaa\n" 8451 "\t bbbb */\n" 8452 "}", 8453 format("{\n" 8454 "/* aaaa\n" 8455 " bbbb */\n" 8456 "}", 8457 Tab)); 8458 EXPECT_EQ("{\n" 8459 "\t/*\n" 8460 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8461 "\t bbbbbbbbbbbbb\n" 8462 "\t*/\n" 8463 "}", 8464 format("{\n" 8465 "/*\n" 8466 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8467 "*/\n" 8468 "}", 8469 Tab)); 8470 EXPECT_EQ("{\n" 8471 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8472 "\t// bbbbbbbbbbbbb\n" 8473 "}", 8474 format("{\n" 8475 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8476 "}", 8477 Tab)); 8478 EXPECT_EQ("{\n" 8479 "\t/*\n" 8480 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8481 "\t bbbbbbbbbbbbb\n" 8482 "\t*/\n" 8483 "}", 8484 format("{\n" 8485 "\t/*\n" 8486 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8487 "\t*/\n" 8488 "}", 8489 Tab)); 8490 EXPECT_EQ("{\n" 8491 "\t/*\n" 8492 "\n" 8493 "\t*/\n" 8494 "}", 8495 format("{\n" 8496 "\t/*\n" 8497 "\n" 8498 "\t*/\n" 8499 "}", 8500 Tab)); 8501 EXPECT_EQ("{\n" 8502 "\t/*\n" 8503 " asdf\n" 8504 "\t*/\n" 8505 "}", 8506 format("{\n" 8507 "\t/*\n" 8508 " asdf\n" 8509 "\t*/\n" 8510 "}", 8511 Tab)); 8512 8513 Tab.UseTab = FormatStyle::UT_Never; 8514 EXPECT_EQ("/*\n" 8515 " a\t\tcomment\n" 8516 " in multiple lines\n" 8517 " */", 8518 format(" /*\t \t \n" 8519 " \t \t a\t\tcomment\t \t\n" 8520 " \t \t in multiple lines\t\n" 8521 " \t */", 8522 Tab)); 8523 EXPECT_EQ("/* some\n" 8524 " comment */", 8525 format(" \t \t /* some\n" 8526 " \t \t comment */", 8527 Tab)); 8528 EXPECT_EQ("int a; /* some\n" 8529 " comment */", 8530 format(" \t \t int a; /* some\n" 8531 " \t \t comment */", 8532 Tab)); 8533 8534 EXPECT_EQ("int a; /* some\n" 8535 "comment */", 8536 format(" \t \t int\ta; /* some\n" 8537 " \t \t comment */", 8538 Tab)); 8539 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8540 " comment */", 8541 format(" \t \t f(\"\t\t\"); /* some\n" 8542 " \t \t comment */", 8543 Tab)); 8544 EXPECT_EQ("{\n" 8545 " /*\n" 8546 " * Comment\n" 8547 " */\n" 8548 " int i;\n" 8549 "}", 8550 format("{\n" 8551 "\t/*\n" 8552 "\t * Comment\n" 8553 "\t */\n" 8554 "\t int i;\n" 8555 "}")); 8556 } 8557 8558 TEST_F(FormatTest, CalculatesOriginalColumn) { 8559 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8560 "q\"; /* some\n" 8561 " comment */", 8562 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8563 "q\"; /* some\n" 8564 " comment */", 8565 getLLVMStyle())); 8566 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8567 "/* some\n" 8568 " comment */", 8569 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8570 " /* some\n" 8571 " comment */", 8572 getLLVMStyle())); 8573 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8574 "qqq\n" 8575 "/* some\n" 8576 " comment */", 8577 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8578 "qqq\n" 8579 " /* some\n" 8580 " comment */", 8581 getLLVMStyle())); 8582 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8583 "wwww; /* some\n" 8584 " comment */", 8585 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8586 "wwww; /* some\n" 8587 " comment */", 8588 getLLVMStyle())); 8589 } 8590 8591 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8592 FormatStyle NoSpace = getLLVMStyle(); 8593 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8594 8595 verifyFormat("while(true)\n" 8596 " continue;", 8597 NoSpace); 8598 verifyFormat("for(;;)\n" 8599 " continue;", 8600 NoSpace); 8601 verifyFormat("if(true)\n" 8602 " f();\n" 8603 "else if(true)\n" 8604 " f();", 8605 NoSpace); 8606 verifyFormat("do {\n" 8607 " do_something();\n" 8608 "} while(something());", 8609 NoSpace); 8610 verifyFormat("switch(x) {\n" 8611 "default:\n" 8612 " break;\n" 8613 "}", 8614 NoSpace); 8615 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8616 verifyFormat("size_t x = sizeof(x);", NoSpace); 8617 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8618 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8619 verifyFormat("alignas(128) char a[128];", NoSpace); 8620 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8621 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8622 verifyFormat("int f() throw(Deprecated);", NoSpace); 8623 verifyFormat("typedef void (*cb)(int);", NoSpace); 8624 verifyFormat("T A::operator()();", NoSpace); 8625 verifyFormat("X A::operator++(T);", NoSpace); 8626 8627 FormatStyle Space = getLLVMStyle(); 8628 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8629 8630 verifyFormat("int f ();", Space); 8631 verifyFormat("void f (int a, T b) {\n" 8632 " while (true)\n" 8633 " continue;\n" 8634 "}", 8635 Space); 8636 verifyFormat("if (true)\n" 8637 " f ();\n" 8638 "else if (true)\n" 8639 " f ();", 8640 Space); 8641 verifyFormat("do {\n" 8642 " do_something ();\n" 8643 "} while (something ());", 8644 Space); 8645 verifyFormat("switch (x) {\n" 8646 "default:\n" 8647 " break;\n" 8648 "}", 8649 Space); 8650 verifyFormat("A::A () : a (1) {}", Space); 8651 verifyFormat("void f () __attribute__ ((asdf));", Space); 8652 verifyFormat("*(&a + 1);\n" 8653 "&((&a)[1]);\n" 8654 "a[(b + c) * d];\n" 8655 "(((a + 1) * 2) + 3) * 4;", 8656 Space); 8657 verifyFormat("#define A(x) x", Space); 8658 verifyFormat("#define A (x) x", Space); 8659 verifyFormat("#if defined(x)\n" 8660 "#endif", 8661 Space); 8662 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8663 verifyFormat("size_t x = sizeof (x);", Space); 8664 verifyFormat("auto f (int x) -> decltype (x);", Space); 8665 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8666 verifyFormat("alignas (128) char a[128];", Space); 8667 verifyFormat("size_t x = alignof (MyType);", Space); 8668 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8669 verifyFormat("int f () throw (Deprecated);", Space); 8670 verifyFormat("typedef void (*cb) (int);", Space); 8671 verifyFormat("T A::operator() ();", Space); 8672 verifyFormat("X A::operator++ (T);", Space); 8673 } 8674 8675 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8676 FormatStyle Spaces = getLLVMStyle(); 8677 8678 Spaces.SpacesInParentheses = true; 8679 verifyFormat("call( x, y, z );", Spaces); 8680 verifyFormat("call();", Spaces); 8681 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8682 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8683 Spaces); 8684 verifyFormat("while ( (bool)1 )\n" 8685 " continue;", 8686 Spaces); 8687 verifyFormat("for ( ;; )\n" 8688 " continue;", 8689 Spaces); 8690 verifyFormat("if ( true )\n" 8691 " f();\n" 8692 "else if ( true )\n" 8693 " f();", 8694 Spaces); 8695 verifyFormat("do {\n" 8696 " do_something( (int)i );\n" 8697 "} while ( something() );", 8698 Spaces); 8699 verifyFormat("switch ( x ) {\n" 8700 "default:\n" 8701 " break;\n" 8702 "}", 8703 Spaces); 8704 8705 Spaces.SpacesInParentheses = false; 8706 Spaces.SpacesInCStyleCastParentheses = true; 8707 verifyFormat("Type *A = ( Type * )P;", Spaces); 8708 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8709 verifyFormat("x = ( int32 )y;", Spaces); 8710 verifyFormat("int a = ( int )(2.0f);", Spaces); 8711 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8712 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8713 verifyFormat("#define x (( int )-1)", Spaces); 8714 8715 // Run the first set of tests again with: 8716 Spaces.SpacesInParentheses = false; 8717 Spaces.SpaceInEmptyParentheses = true; 8718 Spaces.SpacesInCStyleCastParentheses = true; 8719 verifyFormat("call(x, y, z);", Spaces); 8720 verifyFormat("call( );", Spaces); 8721 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8722 verifyFormat("while (( bool )1)\n" 8723 " continue;", 8724 Spaces); 8725 verifyFormat("for (;;)\n" 8726 " continue;", 8727 Spaces); 8728 verifyFormat("if (true)\n" 8729 " f( );\n" 8730 "else if (true)\n" 8731 " f( );", 8732 Spaces); 8733 verifyFormat("do {\n" 8734 " do_something(( int )i);\n" 8735 "} while (something( ));", 8736 Spaces); 8737 verifyFormat("switch (x) {\n" 8738 "default:\n" 8739 " break;\n" 8740 "}", 8741 Spaces); 8742 8743 // Run the first set of tests again with: 8744 Spaces.SpaceAfterCStyleCast = true; 8745 verifyFormat("call(x, y, z);", Spaces); 8746 verifyFormat("call( );", Spaces); 8747 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8748 verifyFormat("while (( bool ) 1)\n" 8749 " continue;", 8750 Spaces); 8751 verifyFormat("for (;;)\n" 8752 " continue;", 8753 Spaces); 8754 verifyFormat("if (true)\n" 8755 " f( );\n" 8756 "else if (true)\n" 8757 " f( );", 8758 Spaces); 8759 verifyFormat("do {\n" 8760 " do_something(( int ) i);\n" 8761 "} while (something( ));", 8762 Spaces); 8763 verifyFormat("switch (x) {\n" 8764 "default:\n" 8765 " break;\n" 8766 "}", 8767 Spaces); 8768 8769 // Run subset of tests again with: 8770 Spaces.SpacesInCStyleCastParentheses = false; 8771 Spaces.SpaceAfterCStyleCast = true; 8772 verifyFormat("while ((bool) 1)\n" 8773 " continue;", 8774 Spaces); 8775 verifyFormat("do {\n" 8776 " do_something((int) i);\n" 8777 "} while (something( ));", 8778 Spaces); 8779 } 8780 8781 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8782 verifyFormat("int a[5];"); 8783 verifyFormat("a[3] += 42;"); 8784 8785 FormatStyle Spaces = getLLVMStyle(); 8786 Spaces.SpacesInSquareBrackets = true; 8787 // Lambdas unchanged. 8788 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8789 verifyFormat("return [i, args...] {};", Spaces); 8790 8791 // Not lambdas. 8792 verifyFormat("int a[ 5 ];", Spaces); 8793 verifyFormat("a[ 3 ] += 42;", Spaces); 8794 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8795 verifyFormat("double &operator[](int i) { return 0; }\n" 8796 "int i;", 8797 Spaces); 8798 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8799 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8800 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8801 } 8802 8803 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8804 verifyFormat("int a = 5;"); 8805 verifyFormat("a += 42;"); 8806 verifyFormat("a or_eq 8;"); 8807 8808 FormatStyle Spaces = getLLVMStyle(); 8809 Spaces.SpaceBeforeAssignmentOperators = false; 8810 verifyFormat("int a= 5;", Spaces); 8811 verifyFormat("a+= 42;", Spaces); 8812 verifyFormat("a or_eq 8;", Spaces); 8813 } 8814 8815 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8816 FormatStyle Alignment = getLLVMStyle(); 8817 Alignment.AlignConsecutiveAssignments = false; 8818 verifyFormat("int a = 5;\n" 8819 "int oneTwoThree = 123;", 8820 Alignment); 8821 verifyFormat("int a = 5;\n" 8822 "int oneTwoThree = 123;", 8823 Alignment); 8824 8825 Alignment.AlignConsecutiveAssignments = true; 8826 verifyFormat("int a = 5;\n" 8827 "int oneTwoThree = 123;", 8828 Alignment); 8829 verifyFormat("int a = method();\n" 8830 "int oneTwoThree = 133;", 8831 Alignment); 8832 verifyFormat("a &= 5;\n" 8833 "bcd *= 5;\n" 8834 "ghtyf += 5;\n" 8835 "dvfvdb -= 5;\n" 8836 "a /= 5;\n" 8837 "vdsvsv %= 5;\n" 8838 "sfdbddfbdfbb ^= 5;\n" 8839 "dvsdsv |= 5;\n" 8840 "int dsvvdvsdvvv = 123;", 8841 Alignment); 8842 verifyFormat("int i = 1, j = 10;\n" 8843 "something = 2000;", 8844 Alignment); 8845 verifyFormat("something = 2000;\n" 8846 "int i = 1, j = 10;\n", 8847 Alignment); 8848 verifyFormat("something = 2000;\n" 8849 "another = 911;\n" 8850 "int i = 1, j = 10;\n" 8851 "oneMore = 1;\n" 8852 "i = 2;", 8853 Alignment); 8854 verifyFormat("int a = 5;\n" 8855 "int one = 1;\n" 8856 "method();\n" 8857 "int oneTwoThree = 123;\n" 8858 "int oneTwo = 12;", 8859 Alignment); 8860 verifyFormat("int oneTwoThree = 123;\n" 8861 "int oneTwo = 12;\n" 8862 "method();\n", 8863 Alignment); 8864 verifyFormat("int oneTwoThree = 123; // comment\n" 8865 "int oneTwo = 12; // comment", 8866 Alignment); 8867 EXPECT_EQ("int a = 5;\n" 8868 "\n" 8869 "int oneTwoThree = 123;", 8870 format("int a = 5;\n" 8871 "\n" 8872 "int oneTwoThree= 123;", 8873 Alignment)); 8874 EXPECT_EQ("int a = 5;\n" 8875 "int one = 1;\n" 8876 "\n" 8877 "int oneTwoThree = 123;", 8878 format("int a = 5;\n" 8879 "int one = 1;\n" 8880 "\n" 8881 "int oneTwoThree = 123;", 8882 Alignment)); 8883 EXPECT_EQ("int a = 5;\n" 8884 "int one = 1;\n" 8885 "\n" 8886 "int oneTwoThree = 123;\n" 8887 "int oneTwo = 12;", 8888 format("int a = 5;\n" 8889 "int one = 1;\n" 8890 "\n" 8891 "int oneTwoThree = 123;\n" 8892 "int oneTwo = 12;", 8893 Alignment)); 8894 Alignment.AlignEscapedNewlinesLeft = true; 8895 verifyFormat("#define A \\\n" 8896 " int aaaa = 12; \\\n" 8897 " int b = 23; \\\n" 8898 " int ccc = 234; \\\n" 8899 " int dddddddddd = 2345;", 8900 Alignment); 8901 Alignment.AlignEscapedNewlinesLeft = false; 8902 verifyFormat("#define A " 8903 " \\\n" 8904 " int aaaa = 12; " 8905 " \\\n" 8906 " int b = 23; " 8907 " \\\n" 8908 " int ccc = 234; " 8909 " \\\n" 8910 " int dddddddddd = 2345;", 8911 Alignment); 8912 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8913 "k = 4, int l = 5,\n" 8914 " int m = 6) {\n" 8915 " int j = 10;\n" 8916 " otherThing = 1;\n" 8917 "}", 8918 Alignment); 8919 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8920 " int i = 1;\n" 8921 " int j = 2;\n" 8922 " int big = 10000;\n" 8923 "}", 8924 Alignment); 8925 verifyFormat("class C {\n" 8926 "public:\n" 8927 " int i = 1;\n" 8928 " virtual void f() = 0;\n" 8929 "};", 8930 Alignment); 8931 verifyFormat("int i = 1;\n" 8932 "if (SomeType t = getSomething()) {\n" 8933 "}\n" 8934 "int j = 2;\n" 8935 "int big = 10000;", 8936 Alignment); 8937 verifyFormat("int j = 7;\n" 8938 "for (int k = 0; k < N; ++k) {\n" 8939 "}\n" 8940 "int j = 2;\n" 8941 "int big = 10000;\n" 8942 "}", 8943 Alignment); 8944 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8945 verifyFormat("int i = 1;\n" 8946 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8947 " = someLooooooooooooooooongFunction();\n" 8948 "int j = 2;", 8949 Alignment); 8950 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8951 verifyFormat("int i = 1;\n" 8952 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8953 " someLooooooooooooooooongFunction();\n" 8954 "int j = 2;", 8955 Alignment); 8956 8957 verifyFormat("auto lambda = []() {\n" 8958 " auto i = 0;\n" 8959 " return 0;\n" 8960 "};\n" 8961 "int i = 0;\n" 8962 "auto v = type{\n" 8963 " i = 1, //\n" 8964 " (i = 2), //\n" 8965 " i = 3 //\n" 8966 "};", 8967 Alignment); 8968 8969 // FIXME: Should align all three assignments 8970 verifyFormat( 8971 "int i = 1;\n" 8972 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8973 " loooooooooooooooooooooongParameterB);\n" 8974 "int j = 2;", 8975 Alignment); 8976 8977 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8978 " typename B = very_long_type_name_1,\n" 8979 " typename T_2 = very_long_type_name_2>\n" 8980 "auto foo() {}\n", 8981 Alignment); 8982 verifyFormat("int a, b = 1;\n" 8983 "int c = 2;\n" 8984 "int dd = 3;\n", 8985 Alignment); 8986 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8987 "float b[1][] = {{3.f}};\n", 8988 Alignment); 8989 } 8990 8991 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8992 FormatStyle Alignment = getLLVMStyle(); 8993 Alignment.AlignConsecutiveDeclarations = false; 8994 verifyFormat("float const a = 5;\n" 8995 "int oneTwoThree = 123;", 8996 Alignment); 8997 verifyFormat("int a = 5;\n" 8998 "float const oneTwoThree = 123;", 8999 Alignment); 9000 9001 Alignment.AlignConsecutiveDeclarations = true; 9002 verifyFormat("float const a = 5;\n" 9003 "int oneTwoThree = 123;", 9004 Alignment); 9005 verifyFormat("int a = method();\n" 9006 "float const oneTwoThree = 133;", 9007 Alignment); 9008 verifyFormat("int i = 1, j = 10;\n" 9009 "something = 2000;", 9010 Alignment); 9011 verifyFormat("something = 2000;\n" 9012 "int i = 1, j = 10;\n", 9013 Alignment); 9014 verifyFormat("float something = 2000;\n" 9015 "double another = 911;\n" 9016 "int i = 1, j = 10;\n" 9017 "const int *oneMore = 1;\n" 9018 "unsigned i = 2;", 9019 Alignment); 9020 verifyFormat("float a = 5;\n" 9021 "int one = 1;\n" 9022 "method();\n" 9023 "const double oneTwoThree = 123;\n" 9024 "const unsigned int oneTwo = 12;", 9025 Alignment); 9026 verifyFormat("int oneTwoThree{0}; // comment\n" 9027 "unsigned oneTwo; // comment", 9028 Alignment); 9029 EXPECT_EQ("float const a = 5;\n" 9030 "\n" 9031 "int oneTwoThree = 123;", 9032 format("float const a = 5;\n" 9033 "\n" 9034 "int oneTwoThree= 123;", 9035 Alignment)); 9036 EXPECT_EQ("float a = 5;\n" 9037 "int one = 1;\n" 9038 "\n" 9039 "unsigned oneTwoThree = 123;", 9040 format("float a = 5;\n" 9041 "int one = 1;\n" 9042 "\n" 9043 "unsigned oneTwoThree = 123;", 9044 Alignment)); 9045 EXPECT_EQ("float a = 5;\n" 9046 "int one = 1;\n" 9047 "\n" 9048 "unsigned oneTwoThree = 123;\n" 9049 "int oneTwo = 12;", 9050 format("float a = 5;\n" 9051 "int one = 1;\n" 9052 "\n" 9053 "unsigned oneTwoThree = 123;\n" 9054 "int oneTwo = 12;", 9055 Alignment)); 9056 Alignment.AlignConsecutiveAssignments = true; 9057 verifyFormat("float something = 2000;\n" 9058 "double another = 911;\n" 9059 "int i = 1, j = 10;\n" 9060 "const int *oneMore = 1;\n" 9061 "unsigned i = 2;", 9062 Alignment); 9063 verifyFormat("int oneTwoThree = {0}; // comment\n" 9064 "unsigned oneTwo = 0; // comment", 9065 Alignment); 9066 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9067 " int const i = 1;\n" 9068 " int * j = 2;\n" 9069 " int big = 10000;\n" 9070 "\n" 9071 " unsigned oneTwoThree = 123;\n" 9072 " int oneTwo = 12;\n" 9073 " method();\n" 9074 " float k = 2;\n" 9075 " int ll = 10000;\n" 9076 "}", 9077 format("void SomeFunction(int parameter= 0) {\n" 9078 " int const i= 1;\n" 9079 " int *j=2;\n" 9080 " int big = 10000;\n" 9081 "\n" 9082 "unsigned oneTwoThree =123;\n" 9083 "int oneTwo = 12;\n" 9084 " method();\n" 9085 "float k= 2;\n" 9086 "int ll=10000;\n" 9087 "}", 9088 Alignment)); 9089 Alignment.AlignConsecutiveAssignments = false; 9090 Alignment.AlignEscapedNewlinesLeft = true; 9091 verifyFormat("#define A \\\n" 9092 " int aaaa = 12; \\\n" 9093 " float b = 23; \\\n" 9094 " const int ccc = 234; \\\n" 9095 " unsigned dddddddddd = 2345;", 9096 Alignment); 9097 Alignment.AlignEscapedNewlinesLeft = false; 9098 Alignment.ColumnLimit = 30; 9099 verifyFormat("#define A \\\n" 9100 " int aaaa = 12; \\\n" 9101 " float b = 23; \\\n" 9102 " const int ccc = 234; \\\n" 9103 " int dddddddddd = 2345;", 9104 Alignment); 9105 Alignment.ColumnLimit = 80; 9106 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9107 "k = 4, int l = 5,\n" 9108 " int m = 6) {\n" 9109 " const int j = 10;\n" 9110 " otherThing = 1;\n" 9111 "}", 9112 Alignment); 9113 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9114 " int const i = 1;\n" 9115 " int * j = 2;\n" 9116 " int big = 10000;\n" 9117 "}", 9118 Alignment); 9119 verifyFormat("class C {\n" 9120 "public:\n" 9121 " int i = 1;\n" 9122 " virtual void f() = 0;\n" 9123 "};", 9124 Alignment); 9125 verifyFormat("float i = 1;\n" 9126 "if (SomeType t = getSomething()) {\n" 9127 "}\n" 9128 "const unsigned j = 2;\n" 9129 "int big = 10000;", 9130 Alignment); 9131 verifyFormat("float j = 7;\n" 9132 "for (int k = 0; k < N; ++k) {\n" 9133 "}\n" 9134 "unsigned j = 2;\n" 9135 "int big = 10000;\n" 9136 "}", 9137 Alignment); 9138 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9139 verifyFormat("float i = 1;\n" 9140 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9141 " = someLooooooooooooooooongFunction();\n" 9142 "int j = 2;", 9143 Alignment); 9144 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9145 verifyFormat("int i = 1;\n" 9146 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9147 " someLooooooooooooooooongFunction();\n" 9148 "int j = 2;", 9149 Alignment); 9150 9151 Alignment.AlignConsecutiveAssignments = true; 9152 verifyFormat("auto lambda = []() {\n" 9153 " auto ii = 0;\n" 9154 " float j = 0;\n" 9155 " return 0;\n" 9156 "};\n" 9157 "int i = 0;\n" 9158 "float i2 = 0;\n" 9159 "auto v = type{\n" 9160 " i = 1, //\n" 9161 " (i = 2), //\n" 9162 " i = 3 //\n" 9163 "};", 9164 Alignment); 9165 Alignment.AlignConsecutiveAssignments = false; 9166 9167 // FIXME: Should align all three declarations 9168 verifyFormat( 9169 "int i = 1;\n" 9170 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9171 " loooooooooooooooooooooongParameterB);\n" 9172 "int j = 2;", 9173 Alignment); 9174 9175 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9176 // We expect declarations and assignments to align, as long as it doesn't 9177 // exceed the column limit, starting a new alignemnt sequence whenever it 9178 // happens. 9179 Alignment.AlignConsecutiveAssignments = true; 9180 Alignment.ColumnLimit = 30; 9181 verifyFormat("float ii = 1;\n" 9182 "unsigned j = 2;\n" 9183 "int someVerylongVariable = 1;\n" 9184 "AnotherLongType ll = 123456;\n" 9185 "VeryVeryLongType k = 2;\n" 9186 "int myvar = 1;", 9187 Alignment); 9188 Alignment.ColumnLimit = 80; 9189 Alignment.AlignConsecutiveAssignments = false; 9190 9191 verifyFormat( 9192 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9193 " typename LongType, typename B>\n" 9194 "auto foo() {}\n", 9195 Alignment); 9196 verifyFormat("float a, b = 1;\n" 9197 "int c = 2;\n" 9198 "int dd = 3;\n", 9199 Alignment); 9200 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9201 "float b[1][] = {{3.f}};\n", 9202 Alignment); 9203 Alignment.AlignConsecutiveAssignments = true; 9204 verifyFormat("float a, b = 1;\n" 9205 "int c = 2;\n" 9206 "int dd = 3;\n", 9207 Alignment); 9208 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9209 "float b[1][] = {{3.f}};\n", 9210 Alignment); 9211 Alignment.AlignConsecutiveAssignments = false; 9212 9213 Alignment.ColumnLimit = 30; 9214 Alignment.BinPackParameters = false; 9215 verifyFormat("void foo(float a,\n" 9216 " float b,\n" 9217 " int c,\n" 9218 " uint32_t *d) {\n" 9219 " int * e = 0;\n" 9220 " float f = 0;\n" 9221 " double g = 0;\n" 9222 "}\n" 9223 "void bar(ino_t a,\n" 9224 " int b,\n" 9225 " uint32_t *c,\n" 9226 " bool d) {}\n", 9227 Alignment); 9228 Alignment.BinPackParameters = true; 9229 Alignment.ColumnLimit = 80; 9230 } 9231 9232 TEST_F(FormatTest, LinuxBraceBreaking) { 9233 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9234 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9235 verifyFormat("namespace a\n" 9236 "{\n" 9237 "class A\n" 9238 "{\n" 9239 " void f()\n" 9240 " {\n" 9241 " if (true) {\n" 9242 " a();\n" 9243 " b();\n" 9244 " } else {\n" 9245 " a();\n" 9246 " }\n" 9247 " }\n" 9248 " void g() { return; }\n" 9249 "};\n" 9250 "struct B {\n" 9251 " int x;\n" 9252 "};\n" 9253 "}\n", 9254 LinuxBraceStyle); 9255 verifyFormat("enum X {\n" 9256 " Y = 0,\n" 9257 "}\n", 9258 LinuxBraceStyle); 9259 verifyFormat("struct S {\n" 9260 " int Type;\n" 9261 " union {\n" 9262 " int x;\n" 9263 " double y;\n" 9264 " } Value;\n" 9265 " class C\n" 9266 " {\n" 9267 " MyFavoriteType Value;\n" 9268 " } Class;\n" 9269 "}\n", 9270 LinuxBraceStyle); 9271 } 9272 9273 TEST_F(FormatTest, MozillaBraceBreaking) { 9274 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9275 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9276 verifyFormat("namespace a {\n" 9277 "class A\n" 9278 "{\n" 9279 " void f()\n" 9280 " {\n" 9281 " if (true) {\n" 9282 " a();\n" 9283 " b();\n" 9284 " }\n" 9285 " }\n" 9286 " void g() { return; }\n" 9287 "};\n" 9288 "enum E\n" 9289 "{\n" 9290 " A,\n" 9291 " // foo\n" 9292 " B,\n" 9293 " C\n" 9294 "};\n" 9295 "struct B\n" 9296 "{\n" 9297 " int x;\n" 9298 "};\n" 9299 "}\n", 9300 MozillaBraceStyle); 9301 verifyFormat("struct S\n" 9302 "{\n" 9303 " int Type;\n" 9304 " union\n" 9305 " {\n" 9306 " int x;\n" 9307 " double y;\n" 9308 " } Value;\n" 9309 " class C\n" 9310 " {\n" 9311 " MyFavoriteType Value;\n" 9312 " } Class;\n" 9313 "}\n", 9314 MozillaBraceStyle); 9315 } 9316 9317 TEST_F(FormatTest, StroustrupBraceBreaking) { 9318 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9319 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9320 verifyFormat("namespace a {\n" 9321 "class A {\n" 9322 " void f()\n" 9323 " {\n" 9324 " if (true) {\n" 9325 " a();\n" 9326 " b();\n" 9327 " }\n" 9328 " }\n" 9329 " void g() { return; }\n" 9330 "};\n" 9331 "struct B {\n" 9332 " int x;\n" 9333 "};\n" 9334 "}\n", 9335 StroustrupBraceStyle); 9336 9337 verifyFormat("void foo()\n" 9338 "{\n" 9339 " if (a) {\n" 9340 " a();\n" 9341 " }\n" 9342 " else {\n" 9343 " b();\n" 9344 " }\n" 9345 "}\n", 9346 StroustrupBraceStyle); 9347 9348 verifyFormat("#ifdef _DEBUG\n" 9349 "int foo(int i = 0)\n" 9350 "#else\n" 9351 "int foo(int i = 5)\n" 9352 "#endif\n" 9353 "{\n" 9354 " return i;\n" 9355 "}", 9356 StroustrupBraceStyle); 9357 9358 verifyFormat("void foo() {}\n" 9359 "void bar()\n" 9360 "#ifdef _DEBUG\n" 9361 "{\n" 9362 " foo();\n" 9363 "}\n" 9364 "#else\n" 9365 "{\n" 9366 "}\n" 9367 "#endif", 9368 StroustrupBraceStyle); 9369 9370 verifyFormat("void foobar() { int i = 5; }\n" 9371 "#ifdef _DEBUG\n" 9372 "void bar() {}\n" 9373 "#else\n" 9374 "void bar() { foobar(); }\n" 9375 "#endif", 9376 StroustrupBraceStyle); 9377 } 9378 9379 TEST_F(FormatTest, AllmanBraceBreaking) { 9380 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9381 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9382 verifyFormat("namespace a\n" 9383 "{\n" 9384 "class A\n" 9385 "{\n" 9386 " void f()\n" 9387 " {\n" 9388 " if (true)\n" 9389 " {\n" 9390 " a();\n" 9391 " b();\n" 9392 " }\n" 9393 " }\n" 9394 " void g() { return; }\n" 9395 "};\n" 9396 "struct B\n" 9397 "{\n" 9398 " int x;\n" 9399 "};\n" 9400 "}", 9401 AllmanBraceStyle); 9402 9403 verifyFormat("void f()\n" 9404 "{\n" 9405 " if (true)\n" 9406 " {\n" 9407 " a();\n" 9408 " }\n" 9409 " else if (false)\n" 9410 " {\n" 9411 " b();\n" 9412 " }\n" 9413 " else\n" 9414 " {\n" 9415 " c();\n" 9416 " }\n" 9417 "}\n", 9418 AllmanBraceStyle); 9419 9420 verifyFormat("void f()\n" 9421 "{\n" 9422 " for (int i = 0; i < 10; ++i)\n" 9423 " {\n" 9424 " a();\n" 9425 " }\n" 9426 " while (false)\n" 9427 " {\n" 9428 " b();\n" 9429 " }\n" 9430 " do\n" 9431 " {\n" 9432 " c();\n" 9433 " } while (false)\n" 9434 "}\n", 9435 AllmanBraceStyle); 9436 9437 verifyFormat("void f(int a)\n" 9438 "{\n" 9439 " switch (a)\n" 9440 " {\n" 9441 " case 0:\n" 9442 " break;\n" 9443 " case 1:\n" 9444 " {\n" 9445 " break;\n" 9446 " }\n" 9447 " case 2:\n" 9448 " {\n" 9449 " }\n" 9450 " break;\n" 9451 " default:\n" 9452 " break;\n" 9453 " }\n" 9454 "}\n", 9455 AllmanBraceStyle); 9456 9457 verifyFormat("enum X\n" 9458 "{\n" 9459 " Y = 0,\n" 9460 "}\n", 9461 AllmanBraceStyle); 9462 verifyFormat("enum X\n" 9463 "{\n" 9464 " Y = 0\n" 9465 "}\n", 9466 AllmanBraceStyle); 9467 9468 verifyFormat("@interface BSApplicationController ()\n" 9469 "{\n" 9470 "@private\n" 9471 " id _extraIvar;\n" 9472 "}\n" 9473 "@end\n", 9474 AllmanBraceStyle); 9475 9476 verifyFormat("#ifdef _DEBUG\n" 9477 "int foo(int i = 0)\n" 9478 "#else\n" 9479 "int foo(int i = 5)\n" 9480 "#endif\n" 9481 "{\n" 9482 " return i;\n" 9483 "}", 9484 AllmanBraceStyle); 9485 9486 verifyFormat("void foo() {}\n" 9487 "void bar()\n" 9488 "#ifdef _DEBUG\n" 9489 "{\n" 9490 " foo();\n" 9491 "}\n" 9492 "#else\n" 9493 "{\n" 9494 "}\n" 9495 "#endif", 9496 AllmanBraceStyle); 9497 9498 verifyFormat("void foobar() { int i = 5; }\n" 9499 "#ifdef _DEBUG\n" 9500 "void bar() {}\n" 9501 "#else\n" 9502 "void bar() { foobar(); }\n" 9503 "#endif", 9504 AllmanBraceStyle); 9505 9506 // This shouldn't affect ObjC blocks.. 9507 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9508 " // ...\n" 9509 " int i;\n" 9510 "}];", 9511 AllmanBraceStyle); 9512 verifyFormat("void (^block)(void) = ^{\n" 9513 " // ...\n" 9514 " int i;\n" 9515 "};", 9516 AllmanBraceStyle); 9517 // .. or dict literals. 9518 verifyFormat("void f()\n" 9519 "{\n" 9520 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9521 "}", 9522 AllmanBraceStyle); 9523 verifyFormat("int f()\n" 9524 "{ // comment\n" 9525 " return 42;\n" 9526 "}", 9527 AllmanBraceStyle); 9528 9529 AllmanBraceStyle.ColumnLimit = 19; 9530 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9531 AllmanBraceStyle.ColumnLimit = 18; 9532 verifyFormat("void f()\n" 9533 "{\n" 9534 " int i;\n" 9535 "}", 9536 AllmanBraceStyle); 9537 AllmanBraceStyle.ColumnLimit = 80; 9538 9539 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9540 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9541 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9542 verifyFormat("void f(bool b)\n" 9543 "{\n" 9544 " if (b)\n" 9545 " {\n" 9546 " return;\n" 9547 " }\n" 9548 "}\n", 9549 BreakBeforeBraceShortIfs); 9550 verifyFormat("void f(bool b)\n" 9551 "{\n" 9552 " if (b) return;\n" 9553 "}\n", 9554 BreakBeforeBraceShortIfs); 9555 verifyFormat("void f(bool b)\n" 9556 "{\n" 9557 " while (b)\n" 9558 " {\n" 9559 " return;\n" 9560 " }\n" 9561 "}\n", 9562 BreakBeforeBraceShortIfs); 9563 } 9564 9565 TEST_F(FormatTest, GNUBraceBreaking) { 9566 FormatStyle GNUBraceStyle = getLLVMStyle(); 9567 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9568 verifyFormat("namespace a\n" 9569 "{\n" 9570 "class A\n" 9571 "{\n" 9572 " void f()\n" 9573 " {\n" 9574 " int a;\n" 9575 " {\n" 9576 " int b;\n" 9577 " }\n" 9578 " if (true)\n" 9579 " {\n" 9580 " a();\n" 9581 " b();\n" 9582 " }\n" 9583 " }\n" 9584 " void g() { return; }\n" 9585 "}\n" 9586 "}", 9587 GNUBraceStyle); 9588 9589 verifyFormat("void f()\n" 9590 "{\n" 9591 " if (true)\n" 9592 " {\n" 9593 " a();\n" 9594 " }\n" 9595 " else if (false)\n" 9596 " {\n" 9597 " b();\n" 9598 " }\n" 9599 " else\n" 9600 " {\n" 9601 " c();\n" 9602 " }\n" 9603 "}\n", 9604 GNUBraceStyle); 9605 9606 verifyFormat("void f()\n" 9607 "{\n" 9608 " for (int i = 0; i < 10; ++i)\n" 9609 " {\n" 9610 " a();\n" 9611 " }\n" 9612 " while (false)\n" 9613 " {\n" 9614 " b();\n" 9615 " }\n" 9616 " do\n" 9617 " {\n" 9618 " c();\n" 9619 " }\n" 9620 " while (false);\n" 9621 "}\n", 9622 GNUBraceStyle); 9623 9624 verifyFormat("void f(int a)\n" 9625 "{\n" 9626 " switch (a)\n" 9627 " {\n" 9628 " case 0:\n" 9629 " break;\n" 9630 " case 1:\n" 9631 " {\n" 9632 " break;\n" 9633 " }\n" 9634 " case 2:\n" 9635 " {\n" 9636 " }\n" 9637 " break;\n" 9638 " default:\n" 9639 " break;\n" 9640 " }\n" 9641 "}\n", 9642 GNUBraceStyle); 9643 9644 verifyFormat("enum X\n" 9645 "{\n" 9646 " Y = 0,\n" 9647 "}\n", 9648 GNUBraceStyle); 9649 9650 verifyFormat("@interface BSApplicationController ()\n" 9651 "{\n" 9652 "@private\n" 9653 " id _extraIvar;\n" 9654 "}\n" 9655 "@end\n", 9656 GNUBraceStyle); 9657 9658 verifyFormat("#ifdef _DEBUG\n" 9659 "int foo(int i = 0)\n" 9660 "#else\n" 9661 "int foo(int i = 5)\n" 9662 "#endif\n" 9663 "{\n" 9664 " return i;\n" 9665 "}", 9666 GNUBraceStyle); 9667 9668 verifyFormat("void foo() {}\n" 9669 "void bar()\n" 9670 "#ifdef _DEBUG\n" 9671 "{\n" 9672 " foo();\n" 9673 "}\n" 9674 "#else\n" 9675 "{\n" 9676 "}\n" 9677 "#endif", 9678 GNUBraceStyle); 9679 9680 verifyFormat("void foobar() { int i = 5; }\n" 9681 "#ifdef _DEBUG\n" 9682 "void bar() {}\n" 9683 "#else\n" 9684 "void bar() { foobar(); }\n" 9685 "#endif", 9686 GNUBraceStyle); 9687 } 9688 9689 TEST_F(FormatTest, WebKitBraceBreaking) { 9690 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9691 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9692 verifyFormat("namespace a {\n" 9693 "class A {\n" 9694 " void f()\n" 9695 " {\n" 9696 " if (true) {\n" 9697 " a();\n" 9698 " b();\n" 9699 " }\n" 9700 " }\n" 9701 " void g() { return; }\n" 9702 "};\n" 9703 "enum E {\n" 9704 " A,\n" 9705 " // foo\n" 9706 " B,\n" 9707 " C\n" 9708 "};\n" 9709 "struct B {\n" 9710 " int x;\n" 9711 "};\n" 9712 "}\n", 9713 WebKitBraceStyle); 9714 verifyFormat("struct S {\n" 9715 " int Type;\n" 9716 " union {\n" 9717 " int x;\n" 9718 " double y;\n" 9719 " } Value;\n" 9720 " class C {\n" 9721 " MyFavoriteType Value;\n" 9722 " } Class;\n" 9723 "};\n", 9724 WebKitBraceStyle); 9725 } 9726 9727 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9728 verifyFormat("void f() {\n" 9729 " try {\n" 9730 " } catch (const Exception &e) {\n" 9731 " }\n" 9732 "}\n", 9733 getLLVMStyle()); 9734 } 9735 9736 TEST_F(FormatTest, UnderstandsPragmas) { 9737 verifyFormat("#pragma omp reduction(| : var)"); 9738 verifyFormat("#pragma omp reduction(+ : var)"); 9739 9740 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9741 "(including parentheses).", 9742 format("#pragma mark Any non-hyphenated or hyphenated string " 9743 "(including parentheses).")); 9744 } 9745 9746 TEST_F(FormatTest, UnderstandPragmaOption) { 9747 verifyFormat("#pragma option -C -A"); 9748 9749 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9750 } 9751 9752 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9753 for (size_t i = 1; i < Styles.size(); ++i) \ 9754 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9755 << " differs from Style #0" 9756 9757 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9758 SmallVector<FormatStyle, 3> Styles; 9759 Styles.resize(3); 9760 9761 Styles[0] = getLLVMStyle(); 9762 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9763 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9764 EXPECT_ALL_STYLES_EQUAL(Styles); 9765 9766 Styles[0] = getGoogleStyle(); 9767 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9768 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9769 EXPECT_ALL_STYLES_EQUAL(Styles); 9770 9771 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9772 EXPECT_TRUE( 9773 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9774 EXPECT_TRUE( 9775 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9776 EXPECT_ALL_STYLES_EQUAL(Styles); 9777 9778 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9779 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9780 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9781 EXPECT_ALL_STYLES_EQUAL(Styles); 9782 9783 Styles[0] = getMozillaStyle(); 9784 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9785 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9786 EXPECT_ALL_STYLES_EQUAL(Styles); 9787 9788 Styles[0] = getWebKitStyle(); 9789 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9790 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9791 EXPECT_ALL_STYLES_EQUAL(Styles); 9792 9793 Styles[0] = getGNUStyle(); 9794 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9795 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9796 EXPECT_ALL_STYLES_EQUAL(Styles); 9797 9798 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9799 } 9800 9801 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9802 SmallVector<FormatStyle, 8> Styles; 9803 Styles.resize(2); 9804 9805 Styles[0] = getGoogleStyle(); 9806 Styles[1] = getLLVMStyle(); 9807 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9808 EXPECT_ALL_STYLES_EQUAL(Styles); 9809 9810 Styles.resize(5); 9811 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9812 Styles[1] = getLLVMStyle(); 9813 Styles[1].Language = FormatStyle::LK_JavaScript; 9814 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9815 9816 Styles[2] = getLLVMStyle(); 9817 Styles[2].Language = FormatStyle::LK_JavaScript; 9818 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9819 "BasedOnStyle: Google", 9820 &Styles[2]) 9821 .value()); 9822 9823 Styles[3] = getLLVMStyle(); 9824 Styles[3].Language = FormatStyle::LK_JavaScript; 9825 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9826 "Language: JavaScript", 9827 &Styles[3]) 9828 .value()); 9829 9830 Styles[4] = getLLVMStyle(); 9831 Styles[4].Language = FormatStyle::LK_JavaScript; 9832 EXPECT_EQ(0, parseConfiguration("---\n" 9833 "BasedOnStyle: LLVM\n" 9834 "IndentWidth: 123\n" 9835 "---\n" 9836 "BasedOnStyle: Google\n" 9837 "Language: JavaScript", 9838 &Styles[4]) 9839 .value()); 9840 EXPECT_ALL_STYLES_EQUAL(Styles); 9841 } 9842 9843 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9844 Style.FIELD = false; \ 9845 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9846 EXPECT_TRUE(Style.FIELD); \ 9847 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9848 EXPECT_FALSE(Style.FIELD); 9849 9850 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9851 9852 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9853 Style.STRUCT.FIELD = false; \ 9854 EXPECT_EQ(0, \ 9855 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9856 .value()); \ 9857 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9858 EXPECT_EQ(0, \ 9859 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9860 .value()); \ 9861 EXPECT_FALSE(Style.STRUCT.FIELD); 9862 9863 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9864 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9865 9866 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9867 EXPECT_NE(VALUE, Style.FIELD); \ 9868 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9869 EXPECT_EQ(VALUE, Style.FIELD) 9870 9871 TEST_F(FormatTest, ParsesConfigurationBools) { 9872 FormatStyle Style = {}; 9873 Style.Language = FormatStyle::LK_Cpp; 9874 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9875 CHECK_PARSE_BOOL(AlignOperands); 9876 CHECK_PARSE_BOOL(AlignTrailingComments); 9877 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9878 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9879 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9880 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9881 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9882 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9883 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9884 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9885 CHECK_PARSE_BOOL(BinPackArguments); 9886 CHECK_PARSE_BOOL(BinPackParameters); 9887 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9888 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9889 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9890 CHECK_PARSE_BOOL(BreakStringLiterals); 9891 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9892 CHECK_PARSE_BOOL(DerivePointerAlignment); 9893 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9894 CHECK_PARSE_BOOL(DisableFormat); 9895 CHECK_PARSE_BOOL(IndentCaseLabels); 9896 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9897 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9898 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9899 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9900 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9901 CHECK_PARSE_BOOL(ReflowComments); 9902 CHECK_PARSE_BOOL(SortIncludes); 9903 CHECK_PARSE_BOOL(SpacesInParentheses); 9904 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9905 CHECK_PARSE_BOOL(SpacesInAngles); 9906 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9907 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9908 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9909 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9910 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9911 9912 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9913 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9914 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9915 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9916 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9917 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9918 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9919 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9920 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9921 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9922 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9923 } 9924 9925 #undef CHECK_PARSE_BOOL 9926 9927 TEST_F(FormatTest, ParsesConfiguration) { 9928 FormatStyle Style = {}; 9929 Style.Language = FormatStyle::LK_Cpp; 9930 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9931 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9932 ConstructorInitializerIndentWidth, 1234u); 9933 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9934 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9935 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9936 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9937 PenaltyBreakBeforeFirstCallParameter, 1234u); 9938 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9939 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9940 PenaltyReturnTypeOnItsOwnLine, 1234u); 9941 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9942 SpacesBeforeTrailingComments, 1234u); 9943 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9944 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9945 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9946 9947 Style.PointerAlignment = FormatStyle::PAS_Middle; 9948 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9949 FormatStyle::PAS_Left); 9950 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9951 FormatStyle::PAS_Right); 9952 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9953 FormatStyle::PAS_Middle); 9954 // For backward compatibility: 9955 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9956 FormatStyle::PAS_Left); 9957 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9958 FormatStyle::PAS_Right); 9959 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9960 FormatStyle::PAS_Middle); 9961 9962 Style.Standard = FormatStyle::LS_Auto; 9963 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9964 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9965 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9966 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9967 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9968 9969 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9970 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9971 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9972 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9973 FormatStyle::BOS_None); 9974 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9975 FormatStyle::BOS_All); 9976 // For backward compatibility: 9977 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9978 FormatStyle::BOS_None); 9979 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9980 FormatStyle::BOS_All); 9981 9982 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9983 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9984 FormatStyle::BAS_Align); 9985 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9986 FormatStyle::BAS_DontAlign); 9987 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9988 FormatStyle::BAS_AlwaysBreak); 9989 // For backward compatibility: 9990 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9991 FormatStyle::BAS_DontAlign); 9992 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9993 FormatStyle::BAS_Align); 9994 9995 Style.UseTab = FormatStyle::UT_ForIndentation; 9996 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9997 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9998 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9999 // For backward compatibility: 10000 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10001 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10002 10003 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10004 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10005 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10006 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10007 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10008 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10009 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10010 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10011 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10012 // For backward compatibility: 10013 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10014 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10015 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10016 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10017 10018 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10019 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10020 FormatStyle::SBPO_Never); 10021 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10022 FormatStyle::SBPO_Always); 10023 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10024 FormatStyle::SBPO_ControlStatements); 10025 // For backward compatibility: 10026 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10027 FormatStyle::SBPO_Never); 10028 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10029 FormatStyle::SBPO_ControlStatements); 10030 10031 Style.ColumnLimit = 123; 10032 FormatStyle BaseStyle = getLLVMStyle(); 10033 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10034 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10035 10036 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10037 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10038 FormatStyle::BS_Attach); 10039 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10040 FormatStyle::BS_Linux); 10041 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10042 FormatStyle::BS_Mozilla); 10043 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10044 FormatStyle::BS_Stroustrup); 10045 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10046 FormatStyle::BS_Allman); 10047 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10048 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10049 FormatStyle::BS_WebKit); 10050 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10051 FormatStyle::BS_Custom); 10052 10053 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10054 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10055 FormatStyle::RTBS_None); 10056 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10057 FormatStyle::RTBS_All); 10058 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10059 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10060 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10061 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10062 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10063 AlwaysBreakAfterReturnType, 10064 FormatStyle::RTBS_TopLevelDefinitions); 10065 10066 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10067 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10068 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10069 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10070 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10071 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10072 AlwaysBreakAfterDefinitionReturnType, 10073 FormatStyle::DRTBS_TopLevel); 10074 10075 Style.NamespaceIndentation = FormatStyle::NI_All; 10076 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10077 FormatStyle::NI_None); 10078 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10079 FormatStyle::NI_Inner); 10080 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10081 FormatStyle::NI_All); 10082 10083 // FIXME: This is required because parsing a configuration simply overwrites 10084 // the first N elements of the list instead of resetting it. 10085 Style.ForEachMacros.clear(); 10086 std::vector<std::string> BoostForeach; 10087 BoostForeach.push_back("BOOST_FOREACH"); 10088 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10089 std::vector<std::string> BoostAndQForeach; 10090 BoostAndQForeach.push_back("BOOST_FOREACH"); 10091 BoostAndQForeach.push_back("Q_FOREACH"); 10092 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10093 BoostAndQForeach); 10094 10095 Style.IncludeCategories.clear(); 10096 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10097 {".*", 1}}; 10098 CHECK_PARSE("IncludeCategories:\n" 10099 " - Regex: abc/.*\n" 10100 " Priority: 2\n" 10101 " - Regex: .*\n" 10102 " Priority: 1", 10103 IncludeCategories, ExpectedCategories); 10104 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10105 } 10106 10107 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10108 FormatStyle Style = {}; 10109 Style.Language = FormatStyle::LK_Cpp; 10110 CHECK_PARSE("Language: Cpp\n" 10111 "IndentWidth: 12", 10112 IndentWidth, 12u); 10113 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10114 "IndentWidth: 34", 10115 &Style), 10116 ParseError::Unsuitable); 10117 EXPECT_EQ(12u, Style.IndentWidth); 10118 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10119 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10120 10121 Style.Language = FormatStyle::LK_JavaScript; 10122 CHECK_PARSE("Language: JavaScript\n" 10123 "IndentWidth: 12", 10124 IndentWidth, 12u); 10125 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10126 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10127 "IndentWidth: 34", 10128 &Style), 10129 ParseError::Unsuitable); 10130 EXPECT_EQ(23u, Style.IndentWidth); 10131 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10132 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10133 10134 CHECK_PARSE("BasedOnStyle: LLVM\n" 10135 "IndentWidth: 67", 10136 IndentWidth, 67u); 10137 10138 CHECK_PARSE("---\n" 10139 "Language: JavaScript\n" 10140 "IndentWidth: 12\n" 10141 "---\n" 10142 "Language: Cpp\n" 10143 "IndentWidth: 34\n" 10144 "...\n", 10145 IndentWidth, 12u); 10146 10147 Style.Language = FormatStyle::LK_Cpp; 10148 CHECK_PARSE("---\n" 10149 "Language: JavaScript\n" 10150 "IndentWidth: 12\n" 10151 "---\n" 10152 "Language: Cpp\n" 10153 "IndentWidth: 34\n" 10154 "...\n", 10155 IndentWidth, 34u); 10156 CHECK_PARSE("---\n" 10157 "IndentWidth: 78\n" 10158 "---\n" 10159 "Language: JavaScript\n" 10160 "IndentWidth: 56\n" 10161 "...\n", 10162 IndentWidth, 78u); 10163 10164 Style.ColumnLimit = 123; 10165 Style.IndentWidth = 234; 10166 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10167 Style.TabWidth = 345; 10168 EXPECT_FALSE(parseConfiguration("---\n" 10169 "IndentWidth: 456\n" 10170 "BreakBeforeBraces: Allman\n" 10171 "---\n" 10172 "Language: JavaScript\n" 10173 "IndentWidth: 111\n" 10174 "TabWidth: 111\n" 10175 "---\n" 10176 "Language: Cpp\n" 10177 "BreakBeforeBraces: Stroustrup\n" 10178 "TabWidth: 789\n" 10179 "...\n", 10180 &Style)); 10181 EXPECT_EQ(123u, Style.ColumnLimit); 10182 EXPECT_EQ(456u, Style.IndentWidth); 10183 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10184 EXPECT_EQ(789u, Style.TabWidth); 10185 10186 EXPECT_EQ(parseConfiguration("---\n" 10187 "Language: JavaScript\n" 10188 "IndentWidth: 56\n" 10189 "---\n" 10190 "IndentWidth: 78\n" 10191 "...\n", 10192 &Style), 10193 ParseError::Error); 10194 EXPECT_EQ(parseConfiguration("---\n" 10195 "Language: JavaScript\n" 10196 "IndentWidth: 56\n" 10197 "---\n" 10198 "Language: JavaScript\n" 10199 "IndentWidth: 78\n" 10200 "...\n", 10201 &Style), 10202 ParseError::Error); 10203 10204 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10205 } 10206 10207 #undef CHECK_PARSE 10208 10209 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10210 FormatStyle Style = {}; 10211 Style.Language = FormatStyle::LK_JavaScript; 10212 Style.BreakBeforeTernaryOperators = true; 10213 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10214 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10215 10216 Style.BreakBeforeTernaryOperators = true; 10217 EXPECT_EQ(0, parseConfiguration("---\n" 10218 "BasedOnStyle: Google\n" 10219 "---\n" 10220 "Language: JavaScript\n" 10221 "IndentWidth: 76\n" 10222 "...\n", 10223 &Style) 10224 .value()); 10225 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10226 EXPECT_EQ(76u, Style.IndentWidth); 10227 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10228 } 10229 10230 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10231 FormatStyle Style = getLLVMStyle(); 10232 std::string YAML = configurationAsText(Style); 10233 FormatStyle ParsedStyle = {}; 10234 ParsedStyle.Language = FormatStyle::LK_Cpp; 10235 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10236 EXPECT_EQ(Style, ParsedStyle); 10237 } 10238 10239 TEST_F(FormatTest, WorksFor8bitEncodings) { 10240 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10241 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10242 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10243 "\"\xef\xee\xf0\xf3...\"", 10244 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10245 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10246 "\xef\xee\xf0\xf3...\"", 10247 getLLVMStyleWithColumns(12))); 10248 } 10249 10250 TEST_F(FormatTest, HandlesUTF8BOM) { 10251 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10252 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10253 format("\xef\xbb\xbf#include <iostream>")); 10254 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10255 format("\xef\xbb\xbf\n#include <iostream>")); 10256 } 10257 10258 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10259 #if !defined(_MSC_VER) 10260 10261 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10262 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10263 getLLVMStyleWithColumns(35)); 10264 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10265 getLLVMStyleWithColumns(31)); 10266 verifyFormat("// Однажды в студёную зимнюю пору...", 10267 getLLVMStyleWithColumns(36)); 10268 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10269 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10270 getLLVMStyleWithColumns(39)); 10271 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10272 getLLVMStyleWithColumns(35)); 10273 } 10274 10275 TEST_F(FormatTest, SplitsUTF8Strings) { 10276 // Non-printable characters' width is currently considered to be the length in 10277 // bytes in UTF8. The characters can be displayed in very different manner 10278 // (zero-width, single width with a substitution glyph, expanded to their code 10279 // (e.g. "<8d>"), so there's no single correct way to handle them. 10280 EXPECT_EQ("\"aaaaÄ\"\n" 10281 "\"\xc2\x8d\";", 10282 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10283 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10284 "\"\xc2\x8d\";", 10285 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10286 EXPECT_EQ("\"Однажды, в \"\n" 10287 "\"студёную \"\n" 10288 "\"зимнюю \"\n" 10289 "\"пору,\"", 10290 format("\"Однажды, в студёную зимнюю пору,\"", 10291 getLLVMStyleWithColumns(13))); 10292 EXPECT_EQ( 10293 "\"一 二 三 \"\n" 10294 "\"四 五六 \"\n" 10295 "\"七 八 九 \"\n" 10296 "\"十\"", 10297 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10298 EXPECT_EQ("\"一\t二 \"\n" 10299 "\"\t三 \"\n" 10300 "\"四 五\t六 \"\n" 10301 "\"\t七 \"\n" 10302 "\"八九十\tqq\"", 10303 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10304 getLLVMStyleWithColumns(11))); 10305 10306 // UTF8 character in an escape sequence. 10307 EXPECT_EQ("\"aaaaaa\"\n" 10308 "\"\\\xC2\x8D\"", 10309 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10310 } 10311 10312 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10313 EXPECT_EQ("const char *sssss =\n" 10314 " \"一二三四五六七八\\\n" 10315 " 九 十\";", 10316 format("const char *sssss = \"一二三四五六七八\\\n" 10317 " 九 十\";", 10318 getLLVMStyleWithColumns(30))); 10319 } 10320 10321 TEST_F(FormatTest, SplitsUTF8LineComments) { 10322 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10323 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10324 EXPECT_EQ("// Я из лесу\n" 10325 "// вышел; был\n" 10326 "// сильный\n" 10327 "// мороз.", 10328 format("// Я из лесу вышел; был сильный мороз.", 10329 getLLVMStyleWithColumns(13))); 10330 EXPECT_EQ("// 一二三\n" 10331 "// 四五六七\n" 10332 "// 八 九\n" 10333 "// 十", 10334 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10335 } 10336 10337 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10338 EXPECT_EQ("/* Гляжу,\n" 10339 " * поднимается\n" 10340 " * медленно в\n" 10341 " * гору\n" 10342 " * Лошадка,\n" 10343 " * везущая\n" 10344 " * хворосту\n" 10345 " * воз. */", 10346 format("/* Гляжу, поднимается медленно в гору\n" 10347 " * Лошадка, везущая хворосту воз. */", 10348 getLLVMStyleWithColumns(13))); 10349 EXPECT_EQ( 10350 "/* 一二三\n" 10351 " * 四五六七\n" 10352 " * 八 九\n" 10353 " * 十 */", 10354 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10355 EXPECT_EQ("/* \n" 10356 " * \n" 10357 " * - */", 10358 format("/* - */", getLLVMStyleWithColumns(12))); 10359 } 10360 10361 #endif // _MSC_VER 10362 10363 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10364 FormatStyle Style = getLLVMStyle(); 10365 10366 Style.ConstructorInitializerIndentWidth = 4; 10367 verifyFormat( 10368 "SomeClass::Constructor()\n" 10369 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10370 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10371 Style); 10372 10373 Style.ConstructorInitializerIndentWidth = 2; 10374 verifyFormat( 10375 "SomeClass::Constructor()\n" 10376 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10377 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10378 Style); 10379 10380 Style.ConstructorInitializerIndentWidth = 0; 10381 verifyFormat( 10382 "SomeClass::Constructor()\n" 10383 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10384 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10385 Style); 10386 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10387 verifyFormat( 10388 "SomeLongTemplateVariableName<\n" 10389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10390 Style); 10391 verifyFormat( 10392 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10394 Style); 10395 } 10396 10397 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10398 FormatStyle Style = getLLVMStyle(); 10399 Style.BreakConstructorInitializersBeforeComma = true; 10400 Style.ConstructorInitializerIndentWidth = 4; 10401 verifyFormat("SomeClass::Constructor()\n" 10402 " : a(a)\n" 10403 " , b(b)\n" 10404 " , c(c) {}", 10405 Style); 10406 verifyFormat("SomeClass::Constructor()\n" 10407 " : a(a) {}", 10408 Style); 10409 10410 Style.ColumnLimit = 0; 10411 verifyFormat("SomeClass::Constructor()\n" 10412 " : a(a) {}", 10413 Style); 10414 verifyFormat("SomeClass::Constructor() noexcept\n" 10415 " : a(a) {}", 10416 Style); 10417 verifyFormat("SomeClass::Constructor()\n" 10418 " : a(a)\n" 10419 " , b(b)\n" 10420 " , c(c) {}", 10421 Style); 10422 verifyFormat("SomeClass::Constructor()\n" 10423 " : a(a) {\n" 10424 " foo();\n" 10425 " bar();\n" 10426 "}", 10427 Style); 10428 10429 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10430 verifyFormat("SomeClass::Constructor()\n" 10431 " : a(a)\n" 10432 " , b(b)\n" 10433 " , c(c) {\n}", 10434 Style); 10435 verifyFormat("SomeClass::Constructor()\n" 10436 " : a(a) {\n}", 10437 Style); 10438 10439 Style.ColumnLimit = 80; 10440 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10441 Style.ConstructorInitializerIndentWidth = 2; 10442 verifyFormat("SomeClass::Constructor()\n" 10443 " : a(a)\n" 10444 " , b(b)\n" 10445 " , c(c) {}", 10446 Style); 10447 10448 Style.ConstructorInitializerIndentWidth = 0; 10449 verifyFormat("SomeClass::Constructor()\n" 10450 ": a(a)\n" 10451 ", b(b)\n" 10452 ", c(c) {}", 10453 Style); 10454 10455 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10456 Style.ConstructorInitializerIndentWidth = 4; 10457 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10458 verifyFormat( 10459 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10460 Style); 10461 verifyFormat( 10462 "SomeClass::Constructor()\n" 10463 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10464 Style); 10465 Style.ConstructorInitializerIndentWidth = 4; 10466 Style.ColumnLimit = 60; 10467 verifyFormat("SomeClass::Constructor()\n" 10468 " : aaaaaaaa(aaaaaaaa)\n" 10469 " , aaaaaaaa(aaaaaaaa)\n" 10470 " , aaaaaaaa(aaaaaaaa) {}", 10471 Style); 10472 } 10473 10474 TEST_F(FormatTest, Destructors) { 10475 verifyFormat("void F(int &i) { i.~int(); }"); 10476 verifyFormat("void F(int &i) { i->~int(); }"); 10477 } 10478 10479 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10480 FormatStyle Style = getWebKitStyle(); 10481 10482 // Don't indent in outer namespaces. 10483 verifyFormat("namespace outer {\n" 10484 "int i;\n" 10485 "namespace inner {\n" 10486 " int i;\n" 10487 "} // namespace inner\n" 10488 "} // namespace outer\n" 10489 "namespace other_outer {\n" 10490 "int i;\n" 10491 "}", 10492 Style); 10493 10494 // Don't indent case labels. 10495 verifyFormat("switch (variable) {\n" 10496 "case 1:\n" 10497 "case 2:\n" 10498 " doSomething();\n" 10499 " break;\n" 10500 "default:\n" 10501 " ++variable;\n" 10502 "}", 10503 Style); 10504 10505 // Wrap before binary operators. 10506 EXPECT_EQ("void f()\n" 10507 "{\n" 10508 " if (aaaaaaaaaaaaaaaa\n" 10509 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10510 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10511 " return;\n" 10512 "}", 10513 format("void f() {\n" 10514 "if (aaaaaaaaaaaaaaaa\n" 10515 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10516 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10517 "return;\n" 10518 "}", 10519 Style)); 10520 10521 // Allow functions on a single line. 10522 verifyFormat("void f() { return; }", Style); 10523 10524 // Constructor initializers are formatted one per line with the "," on the 10525 // new line. 10526 verifyFormat("Constructor()\n" 10527 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10528 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10529 " aaaaaaaaaaaaaa)\n" 10530 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10531 "{\n" 10532 "}", 10533 Style); 10534 verifyFormat("SomeClass::Constructor()\n" 10535 " : a(a)\n" 10536 "{\n" 10537 "}", 10538 Style); 10539 EXPECT_EQ("SomeClass::Constructor()\n" 10540 " : a(a)\n" 10541 "{\n" 10542 "}", 10543 format("SomeClass::Constructor():a(a){}", Style)); 10544 verifyFormat("SomeClass::Constructor()\n" 10545 " : a(a)\n" 10546 " , b(b)\n" 10547 " , c(c)\n" 10548 "{\n" 10549 "}", 10550 Style); 10551 verifyFormat("SomeClass::Constructor()\n" 10552 " : a(a)\n" 10553 "{\n" 10554 " foo();\n" 10555 " bar();\n" 10556 "}", 10557 Style); 10558 10559 // Access specifiers should be aligned left. 10560 verifyFormat("class C {\n" 10561 "public:\n" 10562 " int i;\n" 10563 "};", 10564 Style); 10565 10566 // Do not align comments. 10567 verifyFormat("int a; // Do not\n" 10568 "double b; // align comments.", 10569 Style); 10570 10571 // Do not align operands. 10572 EXPECT_EQ("ASSERT(aaaa\n" 10573 " || bbbb);", 10574 format("ASSERT ( aaaa\n||bbbb);", Style)); 10575 10576 // Accept input's line breaks. 10577 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10578 " || bbbbbbbbbbbbbbb) {\n" 10579 " i++;\n" 10580 "}", 10581 format("if (aaaaaaaaaaaaaaa\n" 10582 "|| bbbbbbbbbbbbbbb) { i++; }", 10583 Style)); 10584 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10585 " i++;\n" 10586 "}", 10587 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10588 10589 // Don't automatically break all macro definitions (llvm.org/PR17842). 10590 verifyFormat("#define aNumber 10", Style); 10591 // However, generally keep the line breaks that the user authored. 10592 EXPECT_EQ("#define aNumber \\\n" 10593 " 10", 10594 format("#define aNumber \\\n" 10595 " 10", 10596 Style)); 10597 10598 // Keep empty and one-element array literals on a single line. 10599 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10600 " copyItems:YES];", 10601 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10602 "copyItems:YES];", 10603 Style)); 10604 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10605 " copyItems:YES];", 10606 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10607 " copyItems:YES];", 10608 Style)); 10609 // FIXME: This does not seem right, there should be more indentation before 10610 // the array literal's entries. Nested blocks have the same problem. 10611 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10612 " @\"a\",\n" 10613 " @\"a\"\n" 10614 "]\n" 10615 " copyItems:YES];", 10616 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10617 " @\"a\",\n" 10618 " @\"a\"\n" 10619 " ]\n" 10620 " copyItems:YES];", 10621 Style)); 10622 EXPECT_EQ( 10623 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10624 " copyItems:YES];", 10625 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10626 " copyItems:YES];", 10627 Style)); 10628 10629 verifyFormat("[self.a b:c c:d];", Style); 10630 EXPECT_EQ("[self.a b:c\n" 10631 " c:d];", 10632 format("[self.a b:c\n" 10633 "c:d];", 10634 Style)); 10635 } 10636 10637 TEST_F(FormatTest, FormatsLambdas) { 10638 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10639 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10640 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10641 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10642 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10643 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10644 verifyFormat("void f() {\n" 10645 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10646 "}\n"); 10647 verifyFormat("void f() {\n" 10648 " other(x.begin(), //\n" 10649 " x.end(), //\n" 10650 " [&](int, int) { return 1; });\n" 10651 "}\n"); 10652 verifyFormat("SomeFunction([]() { // A cool function...\n" 10653 " return 43;\n" 10654 "});"); 10655 EXPECT_EQ("SomeFunction([]() {\n" 10656 "#define A a\n" 10657 " return 43;\n" 10658 "});", 10659 format("SomeFunction([](){\n" 10660 "#define A a\n" 10661 "return 43;\n" 10662 "});")); 10663 verifyFormat("void f() {\n" 10664 " SomeFunction([](decltype(x), A *a) {});\n" 10665 "}"); 10666 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10667 " [](const aaaaaaaaaa &a) { return a; });"); 10668 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10669 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10670 "});"); 10671 verifyFormat("Constructor()\n" 10672 " : Field([] { // comment\n" 10673 " int i;\n" 10674 " }) {}"); 10675 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10676 " return some_parameter.size();\n" 10677 "};"); 10678 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10679 " [](const string &s) { return s; };"); 10680 verifyFormat("int i = aaaaaa ? 1 //\n" 10681 " : [] {\n" 10682 " return 2; //\n" 10683 " }();"); 10684 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10685 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10686 " return x == 2; // force break\n" 10687 " });"); 10688 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10689 " int iiiiiiiiiiii) {\n" 10690 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10691 "});", 10692 getLLVMStyleWithColumns(60)); 10693 verifyFormat("SomeFunction({[&] {\n" 10694 " // comment\n" 10695 " },\n" 10696 " [&] {\n" 10697 " // comment\n" 10698 " }});"); 10699 verifyFormat("SomeFunction({[&] {\n" 10700 " // comment\n" 10701 "}});"); 10702 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10703 " [&]() { return true; },\n" 10704 " aaaaa aaaaaaaaa);"); 10705 10706 // Lambdas with return types. 10707 verifyFormat("int c = []() -> int { return 2; }();\n"); 10708 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10709 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10710 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10711 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10712 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10713 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10714 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10715 verifyFormat("[a, a]() -> a<1> {};"); 10716 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10717 " int j) -> int {\n" 10718 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10719 "};"); 10720 verifyFormat( 10721 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10722 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10723 " return aaaaaaaaaaaaaaaaa;\n" 10724 " });", 10725 getLLVMStyleWithColumns(70)); 10726 10727 // Multiple lambdas in the same parentheses change indentation rules. 10728 verifyFormat("SomeFunction(\n" 10729 " []() {\n" 10730 " int i = 42;\n" 10731 " return i;\n" 10732 " },\n" 10733 " []() {\n" 10734 " int j = 43;\n" 10735 " return j;\n" 10736 " });"); 10737 10738 // More complex introducers. 10739 verifyFormat("return [i, args...] {};"); 10740 10741 // Not lambdas. 10742 verifyFormat("constexpr char hello[]{\"hello\"};"); 10743 verifyFormat("double &operator[](int i) { return 0; }\n" 10744 "int i;"); 10745 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10746 verifyFormat("int i = a[a][a]->f();"); 10747 verifyFormat("int i = (*b)[a]->f();"); 10748 10749 // Other corner cases. 10750 verifyFormat("void f() {\n" 10751 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10752 " );\n" 10753 "}"); 10754 10755 // Lambdas created through weird macros. 10756 verifyFormat("void f() {\n" 10757 " MACRO((const AA &a) { return 1; });\n" 10758 " MACRO((AA &a) { return 1; });\n" 10759 "}"); 10760 10761 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10762 " doo_dah();\n" 10763 " doo_dah();\n" 10764 " })) {\n" 10765 "}"); 10766 verifyFormat("auto lambda = []() {\n" 10767 " int a = 2\n" 10768 "#if A\n" 10769 " + 2\n" 10770 "#endif\n" 10771 " ;\n" 10772 "};"); 10773 } 10774 10775 TEST_F(FormatTest, FormatsBlocks) { 10776 FormatStyle ShortBlocks = getLLVMStyle(); 10777 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10778 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10779 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10780 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10781 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10782 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10783 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10784 10785 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10786 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10787 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10788 10789 verifyFormat("[operation setCompletionBlock:^{\n" 10790 " [self onOperationDone];\n" 10791 "}];"); 10792 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10793 " [self onOperationDone];\n" 10794 "}]};"); 10795 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10796 " f();\n" 10797 "}];"); 10798 verifyFormat("int a = [operation block:^int(int *i) {\n" 10799 " return 1;\n" 10800 "}];"); 10801 verifyFormat("[myObject doSomethingWith:arg1\n" 10802 " aaa:^int(int *a) {\n" 10803 " return 1;\n" 10804 " }\n" 10805 " bbb:f(a * bbbbbbbb)];"); 10806 10807 verifyFormat("[operation setCompletionBlock:^{\n" 10808 " [self.delegate newDataAvailable];\n" 10809 "}];", 10810 getLLVMStyleWithColumns(60)); 10811 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10812 " NSString *path = [self sessionFilePath];\n" 10813 " if (path) {\n" 10814 " // ...\n" 10815 " }\n" 10816 "});"); 10817 verifyFormat("[[SessionService sharedService]\n" 10818 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10819 " if (window) {\n" 10820 " [self windowDidLoad:window];\n" 10821 " } else {\n" 10822 " [self errorLoadingWindow];\n" 10823 " }\n" 10824 " }];"); 10825 verifyFormat("void (^largeBlock)(void) = ^{\n" 10826 " // ...\n" 10827 "};\n", 10828 getLLVMStyleWithColumns(40)); 10829 verifyFormat("[[SessionService sharedService]\n" 10830 " loadWindowWithCompletionBlock: //\n" 10831 " ^(SessionWindow *window) {\n" 10832 " if (window) {\n" 10833 " [self windowDidLoad:window];\n" 10834 " } else {\n" 10835 " [self errorLoadingWindow];\n" 10836 " }\n" 10837 " }];", 10838 getLLVMStyleWithColumns(60)); 10839 verifyFormat("[myObject doSomethingWith:arg1\n" 10840 " firstBlock:^(Foo *a) {\n" 10841 " // ...\n" 10842 " int i;\n" 10843 " }\n" 10844 " secondBlock:^(Bar *b) {\n" 10845 " // ...\n" 10846 " int i;\n" 10847 " }\n" 10848 " thirdBlock:^Foo(Bar *b) {\n" 10849 " // ...\n" 10850 " int i;\n" 10851 " }];"); 10852 verifyFormat("[myObject doSomethingWith:arg1\n" 10853 " firstBlock:-1\n" 10854 " secondBlock:^(Bar *b) {\n" 10855 " // ...\n" 10856 " int i;\n" 10857 " }];"); 10858 10859 verifyFormat("f(^{\n" 10860 " @autoreleasepool {\n" 10861 " if (a) {\n" 10862 " g();\n" 10863 " }\n" 10864 " }\n" 10865 "});"); 10866 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10867 10868 FormatStyle FourIndent = getLLVMStyle(); 10869 FourIndent.ObjCBlockIndentWidth = 4; 10870 verifyFormat("[operation setCompletionBlock:^{\n" 10871 " [self onOperationDone];\n" 10872 "}];", 10873 FourIndent); 10874 } 10875 10876 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10877 FormatStyle ZeroColumn = getLLVMStyle(); 10878 ZeroColumn.ColumnLimit = 0; 10879 10880 verifyFormat("[[SessionService sharedService] " 10881 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10882 " if (window) {\n" 10883 " [self windowDidLoad:window];\n" 10884 " } else {\n" 10885 " [self errorLoadingWindow];\n" 10886 " }\n" 10887 "}];", 10888 ZeroColumn); 10889 EXPECT_EQ("[[SessionService sharedService]\n" 10890 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10891 " if (window) {\n" 10892 " [self windowDidLoad:window];\n" 10893 " } else {\n" 10894 " [self errorLoadingWindow];\n" 10895 " }\n" 10896 " }];", 10897 format("[[SessionService sharedService]\n" 10898 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10899 " if (window) {\n" 10900 " [self windowDidLoad:window];\n" 10901 " } else {\n" 10902 " [self errorLoadingWindow];\n" 10903 " }\n" 10904 "}];", 10905 ZeroColumn)); 10906 verifyFormat("[myObject doSomethingWith:arg1\n" 10907 " firstBlock:^(Foo *a) {\n" 10908 " // ...\n" 10909 " int i;\n" 10910 " }\n" 10911 " secondBlock:^(Bar *b) {\n" 10912 " // ...\n" 10913 " int i;\n" 10914 " }\n" 10915 " thirdBlock:^Foo(Bar *b) {\n" 10916 " // ...\n" 10917 " int i;\n" 10918 " }];", 10919 ZeroColumn); 10920 verifyFormat("f(^{\n" 10921 " @autoreleasepool {\n" 10922 " if (a) {\n" 10923 " g();\n" 10924 " }\n" 10925 " }\n" 10926 "});", 10927 ZeroColumn); 10928 verifyFormat("void (^largeBlock)(void) = ^{\n" 10929 " // ...\n" 10930 "};", 10931 ZeroColumn); 10932 10933 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10934 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10935 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10936 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10937 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10938 " int i;\n" 10939 "};", 10940 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10941 } 10942 10943 TEST_F(FormatTest, SupportsCRLF) { 10944 EXPECT_EQ("int a;\r\n" 10945 "int b;\r\n" 10946 "int c;\r\n", 10947 format("int a;\r\n" 10948 " int b;\r\n" 10949 " int c;\r\n", 10950 getLLVMStyle())); 10951 EXPECT_EQ("int a;\r\n" 10952 "int b;\r\n" 10953 "int c;\r\n", 10954 format("int a;\r\n" 10955 " int b;\n" 10956 " int c;\r\n", 10957 getLLVMStyle())); 10958 EXPECT_EQ("int a;\n" 10959 "int b;\n" 10960 "int c;\n", 10961 format("int a;\r\n" 10962 " int b;\n" 10963 " int c;\n", 10964 getLLVMStyle())); 10965 EXPECT_EQ("\"aaaaaaa \"\r\n" 10966 "\"bbbbbbb\";\r\n", 10967 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10968 EXPECT_EQ("#define A \\\r\n" 10969 " b; \\\r\n" 10970 " c; \\\r\n" 10971 " d;\r\n", 10972 format("#define A \\\r\n" 10973 " b; \\\r\n" 10974 " c; d; \r\n", 10975 getGoogleStyle())); 10976 10977 EXPECT_EQ("/*\r\n" 10978 "multi line block comments\r\n" 10979 "should not introduce\r\n" 10980 "an extra carriage return\r\n" 10981 "*/\r\n", 10982 format("/*\r\n" 10983 "multi line block comments\r\n" 10984 "should not introduce\r\n" 10985 "an extra carriage return\r\n" 10986 "*/\r\n")); 10987 } 10988 10989 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10990 verifyFormat("MY_CLASS(C) {\n" 10991 " int i;\n" 10992 " int j;\n" 10993 "};"); 10994 } 10995 10996 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10997 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10998 TwoIndent.ContinuationIndentWidth = 2; 10999 11000 EXPECT_EQ("int i =\n" 11001 " longFunction(\n" 11002 " arg);", 11003 format("int i = longFunction(arg);", TwoIndent)); 11004 11005 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11006 SixIndent.ContinuationIndentWidth = 6; 11007 11008 EXPECT_EQ("int i =\n" 11009 " longFunction(\n" 11010 " arg);", 11011 format("int i = longFunction(arg);", SixIndent)); 11012 } 11013 11014 TEST_F(FormatTest, SpacesInAngles) { 11015 FormatStyle Spaces = getLLVMStyle(); 11016 Spaces.SpacesInAngles = true; 11017 11018 verifyFormat("static_cast< int >(arg);", Spaces); 11019 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11020 verifyFormat("f< int, float >();", Spaces); 11021 verifyFormat("template <> g() {}", Spaces); 11022 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11023 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11024 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11025 Spaces); 11026 11027 Spaces.Standard = FormatStyle::LS_Cpp03; 11028 Spaces.SpacesInAngles = true; 11029 verifyFormat("A< A< int > >();", Spaces); 11030 11031 Spaces.SpacesInAngles = false; 11032 verifyFormat("A<A<int> >();", Spaces); 11033 11034 Spaces.Standard = FormatStyle::LS_Cpp11; 11035 Spaces.SpacesInAngles = true; 11036 verifyFormat("A< A< int > >();", Spaces); 11037 11038 Spaces.SpacesInAngles = false; 11039 verifyFormat("A<A<int>>();", Spaces); 11040 } 11041 11042 TEST_F(FormatTest, TripleAngleBrackets) { 11043 verifyFormat("f<<<1, 1>>>();"); 11044 verifyFormat("f<<<1, 1, 1, s>>>();"); 11045 verifyFormat("f<<<a, b, c, d>>>();"); 11046 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11047 verifyFormat("f<param><<<1, 1>>>();"); 11048 verifyFormat("f<1><<<1, 1>>>();"); 11049 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11050 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11051 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11052 } 11053 11054 TEST_F(FormatTest, MergeLessLessAtEnd) { 11055 verifyFormat("<<"); 11056 EXPECT_EQ("< < <", format("\\\n<<<")); 11057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11058 "aaallvm::outs() <<"); 11059 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11060 "aaaallvm::outs()\n <<"); 11061 } 11062 11063 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11064 std::string code = "#if A\n" 11065 "#if B\n" 11066 "a.\n" 11067 "#endif\n" 11068 " a = 1;\n" 11069 "#else\n" 11070 "#endif\n" 11071 "#if C\n" 11072 "#else\n" 11073 "#endif\n"; 11074 EXPECT_EQ(code, format(code)); 11075 } 11076 11077 TEST_F(FormatTest, HandleConflictMarkers) { 11078 // Git/SVN conflict markers. 11079 EXPECT_EQ("int a;\n" 11080 "void f() {\n" 11081 " callme(some(parameter1,\n" 11082 "<<<<<<< text by the vcs\n" 11083 " parameter2),\n" 11084 "||||||| text by the vcs\n" 11085 " parameter2),\n" 11086 " parameter3,\n" 11087 "======= text by the vcs\n" 11088 " parameter2, parameter3),\n" 11089 ">>>>>>> text by the vcs\n" 11090 " otherparameter);\n", 11091 format("int a;\n" 11092 "void f() {\n" 11093 " callme(some(parameter1,\n" 11094 "<<<<<<< text by the vcs\n" 11095 " parameter2),\n" 11096 "||||||| text by the vcs\n" 11097 " parameter2),\n" 11098 " parameter3,\n" 11099 "======= text by the vcs\n" 11100 " parameter2,\n" 11101 " parameter3),\n" 11102 ">>>>>>> text by the vcs\n" 11103 " otherparameter);\n")); 11104 11105 // Perforce markers. 11106 EXPECT_EQ("void f() {\n" 11107 " function(\n" 11108 ">>>> text by the vcs\n" 11109 " parameter,\n" 11110 "==== text by the vcs\n" 11111 " parameter,\n" 11112 "==== text by the vcs\n" 11113 " parameter,\n" 11114 "<<<< text by the vcs\n" 11115 " parameter);\n", 11116 format("void f() {\n" 11117 " function(\n" 11118 ">>>> text by the vcs\n" 11119 " parameter,\n" 11120 "==== text by the vcs\n" 11121 " parameter,\n" 11122 "==== text by the vcs\n" 11123 " parameter,\n" 11124 "<<<< text by the vcs\n" 11125 " parameter);\n")); 11126 11127 EXPECT_EQ("<<<<<<<\n" 11128 "|||||||\n" 11129 "=======\n" 11130 ">>>>>>>", 11131 format("<<<<<<<\n" 11132 "|||||||\n" 11133 "=======\n" 11134 ">>>>>>>")); 11135 11136 EXPECT_EQ("<<<<<<<\n" 11137 "|||||||\n" 11138 "int i;\n" 11139 "=======\n" 11140 ">>>>>>>", 11141 format("<<<<<<<\n" 11142 "|||||||\n" 11143 "int i;\n" 11144 "=======\n" 11145 ">>>>>>>")); 11146 11147 // FIXME: Handle parsing of macros around conflict markers correctly: 11148 EXPECT_EQ("#define Macro \\\n" 11149 "<<<<<<<\n" 11150 "Something \\\n" 11151 "|||||||\n" 11152 "Else \\\n" 11153 "=======\n" 11154 "Other \\\n" 11155 ">>>>>>>\n" 11156 " End int i;\n", 11157 format("#define Macro \\\n" 11158 "<<<<<<<\n" 11159 " Something \\\n" 11160 "|||||||\n" 11161 " Else \\\n" 11162 "=======\n" 11163 " Other \\\n" 11164 ">>>>>>>\n" 11165 " End\n" 11166 "int i;\n")); 11167 } 11168 11169 TEST_F(FormatTest, DisableRegions) { 11170 EXPECT_EQ("int i;\n" 11171 "// clang-format off\n" 11172 " int j;\n" 11173 "// clang-format on\n" 11174 "int k;", 11175 format(" int i;\n" 11176 " // clang-format off\n" 11177 " int j;\n" 11178 " // clang-format on\n" 11179 " int k;")); 11180 EXPECT_EQ("int i;\n" 11181 "/* clang-format off */\n" 11182 " int j;\n" 11183 "/* clang-format on */\n" 11184 "int k;", 11185 format(" int i;\n" 11186 " /* clang-format off */\n" 11187 " int j;\n" 11188 " /* clang-format on */\n" 11189 " int k;")); 11190 } 11191 11192 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11193 format("? ) ="); 11194 verifyNoCrash("#define a\\\n /**/}"); 11195 } 11196 11197 TEST_F(FormatTest, FormatsTableGenCode) { 11198 FormatStyle Style = getLLVMStyle(); 11199 Style.Language = FormatStyle::LK_TableGen; 11200 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11201 } 11202 11203 // Since this test case uses UNIX-style file path. We disable it for MS 11204 // compiler. 11205 #if !defined(_MSC_VER) && !defined(__MINGW32__) 11206 11207 TEST(FormatStyle, GetStyleOfFile) { 11208 vfs::InMemoryFileSystem FS; 11209 // Test 1: format file in the same directory. 11210 ASSERT_TRUE( 11211 FS.addFile("/a/.clang-format", 0, 11212 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11213 ASSERT_TRUE( 11214 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11215 auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS); 11216 ASSERT_EQ(Style1, getLLVMStyle()); 11217 11218 // Test 2: fallback to default. 11219 ASSERT_TRUE( 11220 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11221 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS); 11222 ASSERT_EQ(Style2, getMozillaStyle()); 11223 11224 // Test 3: format file in parent directory. 11225 ASSERT_TRUE( 11226 FS.addFile("/c/.clang-format", 0, 11227 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11228 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11229 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11230 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS); 11231 ASSERT_EQ(Style3, getGoogleStyle()); 11232 } 11233 11234 #endif // _MSC_VER 11235 11236 class ReplacementTest : public ::testing::Test { 11237 protected: 11238 tooling::Replacement createReplacement(SourceLocation Start, unsigned Length, 11239 llvm::StringRef ReplacementText) { 11240 return tooling::Replacement(Context.Sources, Start, Length, 11241 ReplacementText); 11242 } 11243 11244 RewriterTestContext Context; 11245 }; 11246 11247 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11248 // Column limit is 20. 11249 std::string Code = "Type *a =\n" 11250 " new Type();\n" 11251 "g(iiiii, 0, jjjjj,\n" 11252 " 0, kkkkk, 0, mm);\n" 11253 "int bad = format ;"; 11254 std::string Expected = "auto a = new Type();\n" 11255 "g(iiiii, nullptr,\n" 11256 " jjjjj, nullptr,\n" 11257 " kkkkk, nullptr,\n" 11258 " mm);\n" 11259 "int bad = format ;"; 11260 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11261 tooling::Replacements Replaces; 11262 Replaces.insert(tooling::Replacement( 11263 Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto ")); 11264 Replaces.insert(tooling::Replacement( 11265 Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr")); 11266 Replaces.insert(tooling::Replacement( 11267 Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr")); 11268 Replaces.insert(tooling::Replacement( 11269 Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr")); 11270 11271 format::FormatStyle Style = format::getLLVMStyle(); 11272 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11273 EXPECT_EQ(Expected, applyAllReplacementsAndFormat(Code, Replaces, Style)); 11274 } 11275 11276 } // end namespace 11277 } // end namespace format 11278 } // end namespace clang 11279