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 "FormatTestUtils.h" 11 #include "clang/Format/Format.h" 12 #include "llvm/Support/Debug.h" 13 #include "gtest/gtest.h" 14 15 #define DEBUG_TYPE "format-test" 16 17 namespace clang { 18 namespace format { 19 namespace { 20 21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 22 23 class FormatTest : public ::testing::Test { 24 protected: 25 enum IncompleteCheck { 26 IC_ExpectComplete, 27 IC_ExpectIncomplete, 28 IC_DoNotCheck 29 }; 30 31 std::string format(llvm::StringRef Code, 32 const FormatStyle &Style = getLLVMStyle(), 33 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 34 DEBUG(llvm::errs() << "---\n"); 35 DEBUG(llvm::errs() << Code << "\n\n"); 36 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 37 bool IncompleteFormat = false; 38 tooling::Replacements Replaces = 39 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 40 if (CheckIncomplete != IC_DoNotCheck) { 41 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 42 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 43 } 44 ReplacementCount = Replaces.size(); 45 std::string Result = applyAllReplacements(Code, Replaces); 46 EXPECT_NE("", Result); 47 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 48 return Result; 49 } 50 51 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 52 FormatStyle Style = getLLVMStyle(); 53 Style.ColumnLimit = ColumnLimit; 54 return Style; 55 } 56 57 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 58 FormatStyle Style = getGoogleStyle(); 59 Style.ColumnLimit = ColumnLimit; 60 return Style; 61 } 62 63 void verifyFormat(llvm::StringRef Code, 64 const FormatStyle &Style = getLLVMStyle()) { 65 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 66 } 67 68 void verifyIncompleteFormat(llvm::StringRef Code, 69 const FormatStyle &Style = getLLVMStyle()) { 70 EXPECT_EQ(Code.str(), 71 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 72 } 73 74 void verifyGoogleFormat(llvm::StringRef Code) { 75 verifyFormat(Code, getGoogleStyle()); 76 } 77 78 void verifyIndependentOfContext(llvm::StringRef text) { 79 verifyFormat(text); 80 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 81 } 82 83 /// \brief Verify that clang-format does not crash on the given input. 84 void verifyNoCrash(llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 format(Code, Style, IC_DoNotCheck); 87 } 88 89 int ReplacementCount; 90 }; 91 92 TEST_F(FormatTest, MessUp) { 93 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 94 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 95 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 96 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 97 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 98 } 99 100 //===----------------------------------------------------------------------===// 101 // Basic function tests. 102 //===----------------------------------------------------------------------===// 103 104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 105 EXPECT_EQ(";", format(";")); 106 } 107 108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 109 EXPECT_EQ("int i;", format(" int i;")); 110 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 111 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 112 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 113 } 114 115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 116 EXPECT_EQ("int i;", format("int\ni;")); 117 } 118 119 TEST_F(FormatTest, FormatsNestedBlockStatements) { 120 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 121 } 122 123 TEST_F(FormatTest, FormatsNestedCall) { 124 verifyFormat("Method(f1, f2(f3));"); 125 verifyFormat("Method(f1(f2, f3()));"); 126 verifyFormat("Method(f1(f2, (f3())));"); 127 } 128 129 TEST_F(FormatTest, NestedNameSpecifiers) { 130 verifyFormat("vector<::Type> v;"); 131 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 132 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 133 verifyFormat("bool a = 2 < ::SomeFunction();"); 134 } 135 136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 137 EXPECT_EQ("if (a) {\n" 138 " f();\n" 139 "}", 140 format("if(a){f();}")); 141 EXPECT_EQ(4, ReplacementCount); 142 EXPECT_EQ("if (a) {\n" 143 " f();\n" 144 "}", 145 format("if (a) {\n" 146 " f();\n" 147 "}")); 148 EXPECT_EQ(0, ReplacementCount); 149 EXPECT_EQ("/*\r\n" 150 "\r\n" 151 "*/\r\n", 152 format("/*\r\n" 153 "\r\n" 154 "*/\r\n")); 155 EXPECT_EQ(0, ReplacementCount); 156 } 157 158 TEST_F(FormatTest, RemovesEmptyLines) { 159 EXPECT_EQ("class C {\n" 160 " int i;\n" 161 "};", 162 format("class C {\n" 163 " int i;\n" 164 "\n" 165 "};")); 166 167 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 168 EXPECT_EQ("namespace N {\n" 169 "\n" 170 "int i;\n" 171 "}", 172 format("namespace N {\n" 173 "\n" 174 "int i;\n" 175 "}", 176 getGoogleStyle())); 177 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 178 "\n" 179 "int i;\n" 180 "}", 181 format("extern /**/ \"C\" /**/ {\n" 182 "\n" 183 "int i;\n" 184 "}", 185 getGoogleStyle())); 186 187 // ...but do keep inlining and removing empty lines for non-block extern "C" 188 // functions. 189 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 190 EXPECT_EQ("extern \"C\" int f() {\n" 191 " int i = 42;\n" 192 " return i;\n" 193 "}", 194 format("extern \"C\" int f() {\n" 195 "\n" 196 " int i = 42;\n" 197 " return i;\n" 198 "}", 199 getGoogleStyle())); 200 201 // Remove empty lines at the beginning and end of blocks. 202 EXPECT_EQ("void f() {\n" 203 "\n" 204 " if (a) {\n" 205 "\n" 206 " f();\n" 207 " }\n" 208 "}", 209 format("void f() {\n" 210 "\n" 211 " if (a) {\n" 212 "\n" 213 " f();\n" 214 "\n" 215 " }\n" 216 "\n" 217 "}", 218 getLLVMStyle())); 219 EXPECT_EQ("void f() {\n" 220 " if (a) {\n" 221 " f();\n" 222 " }\n" 223 "}", 224 format("void f() {\n" 225 "\n" 226 " if (a) {\n" 227 "\n" 228 " f();\n" 229 "\n" 230 " }\n" 231 "\n" 232 "}", 233 getGoogleStyle())); 234 235 // Don't remove empty lines in more complex control statements. 236 EXPECT_EQ("void f() {\n" 237 " if (a) {\n" 238 " f();\n" 239 "\n" 240 " } else if (b) {\n" 241 " f();\n" 242 " }\n" 243 "}", 244 format("void f() {\n" 245 " if (a) {\n" 246 " f();\n" 247 "\n" 248 " } else if (b) {\n" 249 " f();\n" 250 "\n" 251 " }\n" 252 "\n" 253 "}")); 254 255 // FIXME: This is slightly inconsistent. 256 EXPECT_EQ("namespace {\n" 257 "int i;\n" 258 "}", 259 format("namespace {\n" 260 "int i;\n" 261 "\n" 262 "}")); 263 EXPECT_EQ("namespace {\n" 264 "int i;\n" 265 "\n" 266 "} // namespace", 267 format("namespace {\n" 268 "int i;\n" 269 "\n" 270 "} // namespace")); 271 } 272 273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 274 verifyFormat("x = (a) and (b);"); 275 verifyFormat("x = (a) or (b);"); 276 verifyFormat("x = (a) bitand (b);"); 277 verifyFormat("x = (a) bitor (b);"); 278 verifyFormat("x = (a) not_eq (b);"); 279 verifyFormat("x = (a) and_eq (b);"); 280 verifyFormat("x = (a) or_eq (b);"); 281 verifyFormat("x = (a) xor (b);"); 282 } 283 284 //===----------------------------------------------------------------------===// 285 // Tests for control statements. 286 //===----------------------------------------------------------------------===// 287 288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 289 verifyFormat("if (true)\n f();\ng();"); 290 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 291 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 292 293 FormatStyle AllowsMergedIf = getLLVMStyle(); 294 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 295 verifyFormat("if (a)\n" 296 " // comment\n" 297 " f();", 298 AllowsMergedIf); 299 verifyFormat("if (a)\n" 300 " ;", 301 AllowsMergedIf); 302 verifyFormat("if (a)\n" 303 " if (b) return;", 304 AllowsMergedIf); 305 306 verifyFormat("if (a) // Can't merge this\n" 307 " f();\n", 308 AllowsMergedIf); 309 verifyFormat("if (a) /* still don't merge */\n" 310 " f();", 311 AllowsMergedIf); 312 verifyFormat("if (a) { // Never merge this\n" 313 " f();\n" 314 "}", 315 AllowsMergedIf); 316 verifyFormat("if (a) { /* Never merge this */\n" 317 " f();\n" 318 "}", 319 AllowsMergedIf); 320 321 AllowsMergedIf.ColumnLimit = 14; 322 verifyFormat("if (a) return;", AllowsMergedIf); 323 verifyFormat("if (aaaaaaaaa)\n" 324 " return;", 325 AllowsMergedIf); 326 327 AllowsMergedIf.ColumnLimit = 13; 328 verifyFormat("if (a)\n return;", AllowsMergedIf); 329 } 330 331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 332 FormatStyle AllowsMergedLoops = getLLVMStyle(); 333 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 334 verifyFormat("while (true) continue;", AllowsMergedLoops); 335 verifyFormat("for (;;) continue;", AllowsMergedLoops); 336 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 337 verifyFormat("while (true)\n" 338 " ;", 339 AllowsMergedLoops); 340 verifyFormat("for (;;)\n" 341 " ;", 342 AllowsMergedLoops); 343 verifyFormat("for (;;)\n" 344 " for (;;) continue;", 345 AllowsMergedLoops); 346 verifyFormat("for (;;) // Can't merge this\n" 347 " continue;", 348 AllowsMergedLoops); 349 verifyFormat("for (;;) /* still don't merge */\n" 350 " continue;", 351 AllowsMergedLoops); 352 } 353 354 TEST_F(FormatTest, FormatShortBracedStatements) { 355 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 356 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 357 358 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 359 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 360 361 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 362 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 363 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 364 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 365 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 366 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 367 verifyFormat("if (true) { //\n" 368 " f();\n" 369 "}", 370 AllowSimpleBracedStatements); 371 verifyFormat("if (true) {\n" 372 " f();\n" 373 " f();\n" 374 "}", 375 AllowSimpleBracedStatements); 376 verifyFormat("if (true) {\n" 377 " f();\n" 378 "} else {\n" 379 " f();\n" 380 "}", 381 AllowSimpleBracedStatements); 382 383 verifyFormat("template <int> struct A2 {\n" 384 " struct B {};\n" 385 "};", 386 AllowSimpleBracedStatements); 387 388 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 389 verifyFormat("if (true) {\n" 390 " f();\n" 391 "}", 392 AllowSimpleBracedStatements); 393 verifyFormat("if (true) {\n" 394 " f();\n" 395 "} else {\n" 396 " f();\n" 397 "}", 398 AllowSimpleBracedStatements); 399 400 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 401 verifyFormat("while (true) {\n" 402 " f();\n" 403 "}", 404 AllowSimpleBracedStatements); 405 verifyFormat("for (;;) {\n" 406 " f();\n" 407 "}", 408 AllowSimpleBracedStatements); 409 } 410 411 TEST_F(FormatTest, ParseIfElse) { 412 verifyFormat("if (true)\n" 413 " if (true)\n" 414 " if (true)\n" 415 " f();\n" 416 " else\n" 417 " g();\n" 418 " else\n" 419 " h();\n" 420 "else\n" 421 " i();"); 422 verifyFormat("if (true)\n" 423 " if (true)\n" 424 " if (true) {\n" 425 " if (true)\n" 426 " f();\n" 427 " } else {\n" 428 " g();\n" 429 " }\n" 430 " else\n" 431 " h();\n" 432 "else {\n" 433 " i();\n" 434 "}"); 435 verifyFormat("void f() {\n" 436 " if (a) {\n" 437 " } else {\n" 438 " }\n" 439 "}"); 440 } 441 442 TEST_F(FormatTest, ElseIf) { 443 verifyFormat("if (a) {\n} else if (b) {\n}"); 444 verifyFormat("if (a)\n" 445 " f();\n" 446 "else if (b)\n" 447 " g();\n" 448 "else\n" 449 " h();"); 450 verifyFormat("if (a) {\n" 451 " f();\n" 452 "}\n" 453 "// or else ..\n" 454 "else {\n" 455 " g()\n" 456 "}"); 457 458 verifyFormat("if (a) {\n" 459 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 461 "}"); 462 verifyFormat("if (a) {\n" 463 "} else if (\n" 464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 465 "}", 466 getLLVMStyleWithColumns(62)); 467 } 468 469 TEST_F(FormatTest, FormatsForLoop) { 470 verifyFormat( 471 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 472 " ++VeryVeryLongLoopVariable)\n" 473 " ;"); 474 verifyFormat("for (;;)\n" 475 " f();"); 476 verifyFormat("for (;;) {\n}"); 477 verifyFormat("for (;;) {\n" 478 " f();\n" 479 "}"); 480 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 481 482 verifyFormat( 483 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 484 " E = UnwrappedLines.end();\n" 485 " I != E; ++I) {\n}"); 486 487 verifyFormat( 488 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 489 " ++IIIII) {\n}"); 490 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 491 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 492 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 493 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 494 " I = FD->getDeclsInPrototypeScope().begin(),\n" 495 " E = FD->getDeclsInPrototypeScope().end();\n" 496 " I != E; ++I) {\n}"); 497 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 498 " I = Container.begin(),\n" 499 " E = Container.end();\n" 500 " I != E; ++I) {\n}", 501 getLLVMStyleWithColumns(76)); 502 503 verifyFormat( 504 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 508 " ++aaaaaaaaaaa) {\n}"); 509 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 510 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 511 " ++i) {\n}"); 512 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 513 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 514 "}"); 515 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 516 " aaaaaaaaaa);\n" 517 " iter; ++iter) {\n" 518 "}"); 519 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 521 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 522 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 523 524 FormatStyle NoBinPacking = getLLVMStyle(); 525 NoBinPacking.BinPackParameters = false; 526 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 527 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 528 " aaaaaaaaaaaaaaaa,\n" 529 " aaaaaaaaaaaaaaaa,\n" 530 " aaaaaaaaaaaaaaaa);\n" 531 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 532 "}", 533 NoBinPacking); 534 verifyFormat( 535 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 536 " E = UnwrappedLines.end();\n" 537 " I != E;\n" 538 " ++I) {\n}", 539 NoBinPacking); 540 } 541 542 TEST_F(FormatTest, RangeBasedForLoops) { 543 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 545 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 546 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 547 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 549 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 550 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 551 } 552 553 TEST_F(FormatTest, ForEachLoops) { 554 verifyFormat("void f() {\n" 555 " foreach (Item *item, itemlist) {}\n" 556 " Q_FOREACH (Item *item, itemlist) {}\n" 557 " BOOST_FOREACH (Item *item, itemlist) {}\n" 558 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 559 "}"); 560 561 // As function-like macros. 562 verifyFormat("#define foreach(x, y)\n" 563 "#define Q_FOREACH(x, y)\n" 564 "#define BOOST_FOREACH(x, y)\n" 565 "#define UNKNOWN_FOREACH(x, y)\n"); 566 567 // Not as function-like macros. 568 verifyFormat("#define foreach (x, y)\n" 569 "#define Q_FOREACH (x, y)\n" 570 "#define BOOST_FOREACH (x, y)\n" 571 "#define UNKNOWN_FOREACH (x, y)\n"); 572 } 573 574 TEST_F(FormatTest, FormatsWhileLoop) { 575 verifyFormat("while (true) {\n}"); 576 verifyFormat("while (true)\n" 577 " f();"); 578 verifyFormat("while () {\n}"); 579 verifyFormat("while () {\n" 580 " f();\n" 581 "}"); 582 } 583 584 TEST_F(FormatTest, FormatsDoWhile) { 585 verifyFormat("do {\n" 586 " do_something();\n" 587 "} while (something());"); 588 verifyFormat("do\n" 589 " do_something();\n" 590 "while (something());"); 591 } 592 593 TEST_F(FormatTest, FormatsSwitchStatement) { 594 verifyFormat("switch (x) {\n" 595 "case 1:\n" 596 " f();\n" 597 " break;\n" 598 "case kFoo:\n" 599 "case ns::kBar:\n" 600 "case kBaz:\n" 601 " break;\n" 602 "default:\n" 603 " g();\n" 604 " break;\n" 605 "}"); 606 verifyFormat("switch (x) {\n" 607 "case 1: {\n" 608 " f();\n" 609 " break;\n" 610 "}\n" 611 "case 2: {\n" 612 " break;\n" 613 "}\n" 614 "}"); 615 verifyFormat("switch (x) {\n" 616 "case 1: {\n" 617 " f();\n" 618 " {\n" 619 " g();\n" 620 " h();\n" 621 " }\n" 622 " break;\n" 623 "}\n" 624 "}"); 625 verifyFormat("switch (x) {\n" 626 "case 1: {\n" 627 " f();\n" 628 " if (foo) {\n" 629 " g();\n" 630 " h();\n" 631 " }\n" 632 " break;\n" 633 "}\n" 634 "}"); 635 verifyFormat("switch (x) {\n" 636 "case 1: {\n" 637 " f();\n" 638 " g();\n" 639 "} break;\n" 640 "}"); 641 verifyFormat("switch (test)\n" 642 " ;"); 643 verifyFormat("switch (x) {\n" 644 "default: {\n" 645 " // Do nothing.\n" 646 "}\n" 647 "}"); 648 verifyFormat("switch (x) {\n" 649 "// comment\n" 650 "// if 1, do f()\n" 651 "case 1:\n" 652 " f();\n" 653 "}"); 654 verifyFormat("switch (x) {\n" 655 "case 1:\n" 656 " // Do amazing stuff\n" 657 " {\n" 658 " f();\n" 659 " g();\n" 660 " }\n" 661 " break;\n" 662 "}"); 663 verifyFormat("#define A \\\n" 664 " switch (x) { \\\n" 665 " case a: \\\n" 666 " foo = b; \\\n" 667 " }", 668 getLLVMStyleWithColumns(20)); 669 verifyFormat("#define OPERATION_CASE(name) \\\n" 670 " case OP_name: \\\n" 671 " return operations::Operation##name\n", 672 getLLVMStyleWithColumns(40)); 673 verifyFormat("switch (x) {\n" 674 "case 1:;\n" 675 "default:;\n" 676 " int i;\n" 677 "}"); 678 679 verifyGoogleFormat("switch (x) {\n" 680 " case 1:\n" 681 " f();\n" 682 " break;\n" 683 " case kFoo:\n" 684 " case ns::kBar:\n" 685 " case kBaz:\n" 686 " break;\n" 687 " default:\n" 688 " g();\n" 689 " break;\n" 690 "}"); 691 verifyGoogleFormat("switch (x) {\n" 692 " case 1: {\n" 693 " f();\n" 694 " break;\n" 695 " }\n" 696 "}"); 697 verifyGoogleFormat("switch (test)\n" 698 " ;"); 699 700 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 701 " case OP_name: \\\n" 702 " return operations::Operation##name\n"); 703 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 704 " // Get the correction operation class.\n" 705 " switch (OpCode) {\n" 706 " CASE(Add);\n" 707 " CASE(Subtract);\n" 708 " default:\n" 709 " return operations::Unknown;\n" 710 " }\n" 711 "#undef OPERATION_CASE\n" 712 "}"); 713 verifyFormat("DEBUG({\n" 714 " switch (x) {\n" 715 " case A:\n" 716 " f();\n" 717 " break;\n" 718 " // On B:\n" 719 " case B:\n" 720 " g();\n" 721 " break;\n" 722 " }\n" 723 "});"); 724 verifyFormat("switch (a) {\n" 725 "case (b):\n" 726 " return;\n" 727 "}"); 728 729 verifyFormat("switch (a) {\n" 730 "case some_namespace::\n" 731 " some_constant:\n" 732 " return;\n" 733 "}", 734 getLLVMStyleWithColumns(34)); 735 } 736 737 TEST_F(FormatTest, CaseRanges) { 738 verifyFormat("switch (x) {\n" 739 "case 'A' ... 'Z':\n" 740 "case 1 ... 5:\n" 741 " break;\n" 742 "}"); 743 } 744 745 TEST_F(FormatTest, ShortCaseLabels) { 746 FormatStyle Style = getLLVMStyle(); 747 Style.AllowShortCaseLabelsOnASingleLine = true; 748 verifyFormat("switch (a) {\n" 749 "case 1: x = 1; break;\n" 750 "case 2: return;\n" 751 "case 3:\n" 752 "case 4:\n" 753 "case 5: return;\n" 754 "case 6: // comment\n" 755 " return;\n" 756 "case 7:\n" 757 " // comment\n" 758 " return;\n" 759 "case 8:\n" 760 " x = 8; // comment\n" 761 " break;\n" 762 "default: y = 1; break;\n" 763 "}", 764 Style); 765 verifyFormat("switch (a) {\n" 766 "#if FOO\n" 767 "case 0: return 0;\n" 768 "#endif\n" 769 "}", 770 Style); 771 verifyFormat("switch (a) {\n" 772 "case 1: {\n" 773 "}\n" 774 "case 2: {\n" 775 " return;\n" 776 "}\n" 777 "case 3: {\n" 778 " x = 1;\n" 779 " return;\n" 780 "}\n" 781 "case 4:\n" 782 " if (x)\n" 783 " return;\n" 784 "}", 785 Style); 786 Style.ColumnLimit = 21; 787 verifyFormat("switch (a) {\n" 788 "case 1: x = 1; break;\n" 789 "case 2: return;\n" 790 "case 3:\n" 791 "case 4:\n" 792 "case 5: return;\n" 793 "default:\n" 794 " y = 1;\n" 795 " break;\n" 796 "}", 797 Style); 798 } 799 800 TEST_F(FormatTest, FormatsLabels) { 801 verifyFormat("void f() {\n" 802 " some_code();\n" 803 "test_label:\n" 804 " some_other_code();\n" 805 " {\n" 806 " some_more_code();\n" 807 " another_label:\n" 808 " some_more_code();\n" 809 " }\n" 810 "}"); 811 verifyFormat("{\n" 812 " some_code();\n" 813 "test_label:\n" 814 " some_other_code();\n" 815 "}"); 816 verifyFormat("{\n" 817 " some_code();\n" 818 "test_label:;\n" 819 " int i = 0;\n" 820 "}"); 821 } 822 823 //===----------------------------------------------------------------------===// 824 // Tests for comments. 825 //===----------------------------------------------------------------------===// 826 827 TEST_F(FormatTest, UnderstandsSingleLineComments) { 828 verifyFormat("//* */"); 829 verifyFormat("// line 1\n" 830 "// line 2\n" 831 "void f() {}\n"); 832 833 verifyFormat("void f() {\n" 834 " // Doesn't do anything\n" 835 "}"); 836 verifyFormat("SomeObject\n" 837 " // Calling someFunction on SomeObject\n" 838 " .someFunction();"); 839 verifyFormat("auto result = SomeObject\n" 840 " // Calling someFunction on SomeObject\n" 841 " .someFunction();"); 842 verifyFormat("void f(int i, // some comment (probably for i)\n" 843 " int j, // some comment (probably for j)\n" 844 " int k); // some comment (probably for k)"); 845 verifyFormat("void f(int i,\n" 846 " // some comment (probably for j)\n" 847 " int j,\n" 848 " // some comment (probably for k)\n" 849 " int k);"); 850 851 verifyFormat("int i // This is a fancy variable\n" 852 " = 5; // with nicely aligned comment."); 853 854 verifyFormat("// Leading comment.\n" 855 "int a; // Trailing comment."); 856 verifyFormat("int a; // Trailing comment\n" 857 " // on 2\n" 858 " // or 3 lines.\n" 859 "int b;"); 860 verifyFormat("int a; // Trailing comment\n" 861 "\n" 862 "// Leading comment.\n" 863 "int b;"); 864 verifyFormat("int a; // Comment.\n" 865 " // More details.\n" 866 "int bbbb; // Another comment."); 867 verifyFormat( 868 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 869 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 870 "int cccccccccccccccccccccccccccccc; // comment\n" 871 "int ddd; // looooooooooooooooooooooooong comment\n" 872 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 873 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 874 "int ccccccccccccccccccc; // comment"); 875 876 verifyFormat("#include \"a\" // comment\n" 877 "#include \"a/b/c\" // comment"); 878 verifyFormat("#include <a> // comment\n" 879 "#include <a/b/c> // comment"); 880 EXPECT_EQ("#include \"a\" // comment\n" 881 "#include \"a/b/c\" // comment", 882 format("#include \\\n" 883 " \"a\" // comment\n" 884 "#include \"a/b/c\" // comment")); 885 886 verifyFormat("enum E {\n" 887 " // comment\n" 888 " VAL_A, // comment\n" 889 " VAL_B\n" 890 "};"); 891 892 verifyFormat( 893 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 894 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 895 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 896 " // Comment inside a statement.\n" 897 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 898 verifyFormat("SomeFunction(a,\n" 899 " // comment\n" 900 " b + x);"); 901 verifyFormat("SomeFunction(a, a,\n" 902 " // comment\n" 903 " b + x);"); 904 verifyFormat( 905 "bool aaaaaaaaaaaaa = // comment\n" 906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 908 909 verifyFormat("int aaaa; // aaaaa\n" 910 "int aa; // aaaaaaa", 911 getLLVMStyleWithColumns(20)); 912 913 EXPECT_EQ("void f() { // This does something ..\n" 914 "}\n" 915 "int a; // This is unrelated", 916 format("void f() { // This does something ..\n" 917 " }\n" 918 "int a; // This is unrelated")); 919 EXPECT_EQ("class C {\n" 920 " void f() { // This does something ..\n" 921 " } // awesome..\n" 922 "\n" 923 " int a; // This is unrelated\n" 924 "};", 925 format("class C{void f() { // This does something ..\n" 926 " } // awesome..\n" 927 " \n" 928 "int a; // This is unrelated\n" 929 "};")); 930 931 EXPECT_EQ("int i; // single line trailing comment", 932 format("int i;\\\n// single line trailing comment")); 933 934 verifyGoogleFormat("int a; // Trailing comment."); 935 936 verifyFormat("someFunction(anotherFunction( // Force break.\n" 937 " parameter));"); 938 939 verifyGoogleFormat("#endif // HEADER_GUARD"); 940 941 verifyFormat("const char *test[] = {\n" 942 " // A\n" 943 " \"aaaa\",\n" 944 " // B\n" 945 " \"aaaaa\"};"); 946 verifyGoogleFormat( 947 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 948 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 949 EXPECT_EQ("D(a, {\n" 950 " // test\n" 951 " int a;\n" 952 "});", 953 format("D(a, {\n" 954 "// test\n" 955 "int a;\n" 956 "});")); 957 958 EXPECT_EQ("lineWith(); // comment\n" 959 "// at start\n" 960 "otherLine();", 961 format("lineWith(); // comment\n" 962 "// at start\n" 963 "otherLine();")); 964 EXPECT_EQ("lineWith(); // comment\n" 965 "/*\n" 966 " * at start */\n" 967 "otherLine();", 968 format("lineWith(); // comment\n" 969 "/*\n" 970 " * at start */\n" 971 "otherLine();")); 972 EXPECT_EQ("lineWith(); // comment\n" 973 " // at start\n" 974 "otherLine();", 975 format("lineWith(); // comment\n" 976 " // at start\n" 977 "otherLine();")); 978 979 EXPECT_EQ("lineWith(); // comment\n" 980 "// at start\n" 981 "otherLine(); // comment", 982 format("lineWith(); // comment\n" 983 "// at start\n" 984 "otherLine(); // comment")); 985 EXPECT_EQ("lineWith();\n" 986 "// at start\n" 987 "otherLine(); // comment", 988 format("lineWith();\n" 989 " // at start\n" 990 "otherLine(); // comment")); 991 EXPECT_EQ("// first\n" 992 "// at start\n" 993 "otherLine(); // comment", 994 format("// first\n" 995 " // at start\n" 996 "otherLine(); // comment")); 997 EXPECT_EQ("f();\n" 998 "// first\n" 999 "// at start\n" 1000 "otherLine(); // comment", 1001 format("f();\n" 1002 "// first\n" 1003 " // at start\n" 1004 "otherLine(); // comment")); 1005 verifyFormat("f(); // comment\n" 1006 "// first\n" 1007 "// at start\n" 1008 "otherLine();"); 1009 EXPECT_EQ("f(); // comment\n" 1010 "// first\n" 1011 "// at start\n" 1012 "otherLine();", 1013 format("f(); // comment\n" 1014 "// first\n" 1015 " // at start\n" 1016 "otherLine();")); 1017 EXPECT_EQ("f(); // comment\n" 1018 " // first\n" 1019 "// at start\n" 1020 "otherLine();", 1021 format("f(); // comment\n" 1022 " // first\n" 1023 "// at start\n" 1024 "otherLine();")); 1025 EXPECT_EQ("void f() {\n" 1026 " lineWith(); // comment\n" 1027 " // at start\n" 1028 "}", 1029 format("void f() {\n" 1030 " lineWith(); // comment\n" 1031 " // at start\n" 1032 "}")); 1033 EXPECT_EQ("int xy; // a\n" 1034 "int z; // b", 1035 format("int xy; // a\n" 1036 "int z; //b")); 1037 EXPECT_EQ("int xy; // a\n" 1038 "int z; // bb", 1039 format("int xy; // a\n" 1040 "int z; //bb", 1041 getLLVMStyleWithColumns(12))); 1042 1043 verifyFormat("#define A \\\n" 1044 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1045 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1046 getLLVMStyleWithColumns(60)); 1047 verifyFormat( 1048 "#define A \\\n" 1049 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1050 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1051 getLLVMStyleWithColumns(61)); 1052 1053 verifyFormat("if ( // This is some comment\n" 1054 " x + 3) {\n" 1055 "}"); 1056 EXPECT_EQ("if ( // This is some comment\n" 1057 " // spanning two lines\n" 1058 " x + 3) {\n" 1059 "}", 1060 format("if( // This is some comment\n" 1061 " // spanning two lines\n" 1062 " x + 3) {\n" 1063 "}")); 1064 1065 verifyNoCrash("/\\\n/"); 1066 verifyNoCrash("/\\\n* */"); 1067 // The 0-character somehow makes the lexer return a proper comment. 1068 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1069 } 1070 1071 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1072 EXPECT_EQ("SomeFunction(a,\n" 1073 " b, // comment\n" 1074 " c);", 1075 format("SomeFunction(a,\n" 1076 " b, // comment\n" 1077 " c);")); 1078 EXPECT_EQ("SomeFunction(a, b,\n" 1079 " // comment\n" 1080 " c);", 1081 format("SomeFunction(a,\n" 1082 " b,\n" 1083 " // comment\n" 1084 " c);")); 1085 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1086 " c);", 1087 format("SomeFunction(a, b, // comment (unclear relation)\n" 1088 " c);")); 1089 EXPECT_EQ("SomeFunction(a, // comment\n" 1090 " b,\n" 1091 " c); // comment", 1092 format("SomeFunction(a, // comment\n" 1093 " b,\n" 1094 " c); // comment")); 1095 } 1096 1097 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1098 EXPECT_EQ("// comment", format("// comment ")); 1099 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1100 format("int aaaaaaa, bbbbbbb; // comment ", 1101 getLLVMStyleWithColumns(33))); 1102 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1103 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1104 } 1105 1106 TEST_F(FormatTest, UnderstandsBlockComments) { 1107 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1108 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1109 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1110 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1111 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1112 "/* Trailing comment for aa... */\n" 1113 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1114 EXPECT_EQ( 1115 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1116 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1117 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1118 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1119 EXPECT_EQ( 1120 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1121 " aaaaaaaaaaaaaaaaaa,\n" 1122 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1123 "}", 1124 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1125 " aaaaaaaaaaaaaaaaaa ,\n" 1126 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1127 "}")); 1128 1129 FormatStyle NoBinPacking = getLLVMStyle(); 1130 NoBinPacking.BinPackParameters = false; 1131 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1132 " /* parameter 2 */ aaaaaa,\n" 1133 " /* parameter 3 */ aaaaaa,\n" 1134 " /* parameter 4 */ aaaaaa);", 1135 NoBinPacking); 1136 1137 // Aligning block comments in macros. 1138 verifyGoogleFormat("#define A \\\n" 1139 " int i; /*a*/ \\\n" 1140 " int jjj; /*b*/"); 1141 } 1142 1143 TEST_F(FormatTest, AlignsBlockComments) { 1144 EXPECT_EQ("/*\n" 1145 " * Really multi-line\n" 1146 " * comment.\n" 1147 " */\n" 1148 "void f() {}", 1149 format(" /*\n" 1150 " * Really multi-line\n" 1151 " * comment.\n" 1152 " */\n" 1153 " void f() {}")); 1154 EXPECT_EQ("class C {\n" 1155 " /*\n" 1156 " * Another multi-line\n" 1157 " * comment.\n" 1158 " */\n" 1159 " void f() {}\n" 1160 "};", 1161 format("class C {\n" 1162 "/*\n" 1163 " * Another multi-line\n" 1164 " * comment.\n" 1165 " */\n" 1166 "void f() {}\n" 1167 "};")); 1168 EXPECT_EQ("/*\n" 1169 " 1. This is a comment with non-trivial formatting.\n" 1170 " 1.1. We have to indent/outdent all lines equally\n" 1171 " 1.1.1. to keep the formatting.\n" 1172 " */", 1173 format(" /*\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 EXPECT_EQ("/*\n" 1179 "Don't try to outdent if there's not enough indentation.\n" 1180 "*/", 1181 format(" /*\n" 1182 " Don't try to outdent if there's not enough indentation.\n" 1183 " */")); 1184 1185 EXPECT_EQ("int i; /* Comment with empty...\n" 1186 " *\n" 1187 " * line. */", 1188 format("int i; /* Comment with empty...\n" 1189 " *\n" 1190 " * line. */")); 1191 EXPECT_EQ("int foobar = 0; /* comment */\n" 1192 "int bar = 0; /* multiline\n" 1193 " comment 1 */\n" 1194 "int baz = 0; /* multiline\n" 1195 " comment 2 */\n" 1196 "int bzz = 0; /* multiline\n" 1197 " comment 3 */", 1198 format("int foobar = 0; /* comment */\n" 1199 "int bar = 0; /* multiline\n" 1200 " comment 1 */\n" 1201 "int baz = 0; /* multiline\n" 1202 " comment 2 */\n" 1203 "int bzz = 0; /* multiline\n" 1204 " comment 3 */")); 1205 EXPECT_EQ("int foobar = 0; /* comment */\n" 1206 "int bar = 0; /* multiline\n" 1207 " comment */\n" 1208 "int baz = 0; /* multiline\n" 1209 "comment */", 1210 format("int foobar = 0; /* comment */\n" 1211 "int bar = 0; /* multiline\n" 1212 "comment */\n" 1213 "int baz = 0; /* multiline\n" 1214 "comment */")); 1215 } 1216 1217 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1218 FormatStyle Style = getLLVMStyleWithColumns(20); 1219 Style.ReflowComments = false; 1220 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1221 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1222 } 1223 1224 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1225 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1227 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1228 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1229 EXPECT_EQ( 1230 "void ffffffffffff(\n" 1231 " int aaaaaaaa, int bbbbbbbb,\n" 1232 " int cccccccccccc) { /*\n" 1233 " aaaaaaaaaa\n" 1234 " aaaaaaaaaaaaa\n" 1235 " bbbbbbbbbbbbbb\n" 1236 " bbbbbbbbbb\n" 1237 " */\n" 1238 "}", 1239 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1240 "{ /*\n" 1241 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1242 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1243 " */\n" 1244 "}", 1245 getLLVMStyleWithColumns(40))); 1246 } 1247 1248 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1249 EXPECT_EQ("void ffffffffff(\n" 1250 " int aaaaa /* test */);", 1251 format("void ffffffffff(int aaaaa /* test */);", 1252 getLLVMStyleWithColumns(35))); 1253 } 1254 1255 TEST_F(FormatTest, SplitsLongCxxComments) { 1256 EXPECT_EQ("// A comment that\n" 1257 "// doesn't fit on\n" 1258 "// one line", 1259 format("// A comment that doesn't fit on one line", 1260 getLLVMStyleWithColumns(20))); 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 b c d\n" 1272 "// e f g\n" 1273 "// h i j k", 1274 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1275 EXPECT_EQ( 1276 "// a b c d\n" 1277 "// e f g\n" 1278 "// h i j k", 1279 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1280 EXPECT_EQ("if (true) // A comment that\n" 1281 " // doesn't fit on\n" 1282 " // one line", 1283 format("if (true) // A comment that doesn't fit on one line ", 1284 getLLVMStyleWithColumns(30))); 1285 EXPECT_EQ("// Don't_touch_leading_whitespace", 1286 format("// Don't_touch_leading_whitespace", 1287 getLLVMStyleWithColumns(20))); 1288 EXPECT_EQ("// Add leading\n" 1289 "// whitespace", 1290 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1291 EXPECT_EQ("/// Add leading\n" 1292 "/// whitespace", 1293 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1294 EXPECT_EQ("//! Add leading\n" 1295 "//! whitespace", 1296 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1297 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1298 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1299 "// limit", 1300 format("//Even if it makes the line exceed the column limit", 1301 getLLVMStyleWithColumns(51))); 1302 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1303 1304 EXPECT_EQ("// aa bb cc dd", 1305 format("// aa bb cc dd ", 1306 getLLVMStyleWithColumns(15))); 1307 1308 EXPECT_EQ("// A comment before\n" 1309 "// a macro\n" 1310 "// definition\n" 1311 "#define a b", 1312 format("// A comment before a macro definition\n" 1313 "#define a b", 1314 getLLVMStyleWithColumns(20))); 1315 EXPECT_EQ("void ffffff(\n" 1316 " int aaaaaaaaa, // wwww\n" 1317 " int bbbbbbbbbb, // xxxxxxx\n" 1318 " // yyyyyyyyyy\n" 1319 " int c, int d, int e) {}", 1320 format("void ffffff(\n" 1321 " int aaaaaaaaa, // wwww\n" 1322 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1323 " int c, int d, int e) {}", 1324 getLLVMStyleWithColumns(40))); 1325 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1326 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1327 getLLVMStyleWithColumns(20))); 1328 EXPECT_EQ( 1329 "#define XXX // a b c d\n" 1330 " // e f g h", 1331 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1332 EXPECT_EQ( 1333 "#define XXX // q w e r\n" 1334 " // t y u i", 1335 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1336 } 1337 1338 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1339 EXPECT_EQ("// A comment\n" 1340 "// that doesn't\n" 1341 "// fit on one\n" 1342 "// line", 1343 format("// A comment that doesn't fit on one line", 1344 getLLVMStyleWithColumns(20))); 1345 EXPECT_EQ("/// A comment\n" 1346 "/// that doesn't\n" 1347 "/// fit on one\n" 1348 "/// line", 1349 format("/// A comment that doesn't fit on one line", 1350 getLLVMStyleWithColumns(20))); 1351 } 1352 1353 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1354 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1355 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1356 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1357 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1358 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1359 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1360 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1361 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1362 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1363 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1364 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1365 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1366 getLLVMStyleWithColumns(50))); 1367 // FIXME: One day we might want to implement adjustment of leading whitespace 1368 // of the consecutive lines in this kind of comment: 1369 EXPECT_EQ("double\n" 1370 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1371 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1372 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1373 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1374 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1375 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1376 getLLVMStyleWithColumns(49))); 1377 } 1378 1379 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1380 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1381 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1382 EXPECT_EQ( 1383 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1384 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1385 EXPECT_EQ( 1386 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1387 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1388 } 1389 1390 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1391 EXPECT_EQ("if (xxx ==\n" 1392 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1393 " zzz)\n" 1394 " q();", 1395 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1396 " zzz) q();", 1397 getLLVMStyleWithColumns(40))); 1398 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1399 " yyy && // aaaaaa bbbbbbbb cccc\n" 1400 " zzz)\n" 1401 " q();", 1402 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1403 " zzz) q();", 1404 getLLVMStyleWithColumns(40))); 1405 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1406 " yyy || // aaaaaa bbbbbbbb cccc\n" 1407 " zzz)\n" 1408 " q();", 1409 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1410 " zzz) q();", 1411 getLLVMStyleWithColumns(40))); 1412 EXPECT_EQ("fffffffff(\n" 1413 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1414 " zzz);", 1415 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1416 " zzz);", 1417 getLLVMStyleWithColumns(40))); 1418 } 1419 1420 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1421 EXPECT_EQ("#define A(x) /* \\\n" 1422 " a comment \\\n" 1423 " inside */ \\\n" 1424 " f();", 1425 format("#define A(x) /* \\\n" 1426 " a comment \\\n" 1427 " inside */ \\\n" 1428 " f();", 1429 getLLVMStyleWithColumns(17))); 1430 EXPECT_EQ("#define A( \\\n" 1431 " x) /* \\\n" 1432 " a comment \\\n" 1433 " inside */ \\\n" 1434 " f();", 1435 format("#define A( \\\n" 1436 " x) /* \\\n" 1437 " a comment \\\n" 1438 " inside */ \\\n" 1439 " f();", 1440 getLLVMStyleWithColumns(17))); 1441 } 1442 1443 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1444 EXPECT_EQ("namespace {}\n// Test\n#define A", 1445 format("namespace {}\n // Test\n#define A")); 1446 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1447 format("namespace {}\n /* Test */\n#define A")); 1448 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1449 format("namespace {}\n /* Test */ #define A")); 1450 } 1451 1452 TEST_F(FormatTest, SplitsLongLinesInComments) { 1453 EXPECT_EQ("/* This is a long\n" 1454 " * comment that\n" 1455 " * doesn't\n" 1456 " * fit on one line.\n" 1457 " */", 1458 format("/* " 1459 "This is a long " 1460 "comment that " 1461 "doesn't " 1462 "fit on one line. */", 1463 getLLVMStyleWithColumns(20))); 1464 EXPECT_EQ( 1465 "/* a b c d\n" 1466 " * e f g\n" 1467 " * h i j k\n" 1468 " */", 1469 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1470 EXPECT_EQ( 1471 "/* a b c d\n" 1472 " * e f g\n" 1473 " * h i j k\n" 1474 " */", 1475 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1476 EXPECT_EQ("/*\n" 1477 "This is a long\n" 1478 "comment that doesn't\n" 1479 "fit on one line.\n" 1480 "*/", 1481 format("/*\n" 1482 "This is a long " 1483 "comment that doesn't " 1484 "fit on one line. \n" 1485 "*/", 1486 getLLVMStyleWithColumns(20))); 1487 EXPECT_EQ("/*\n" 1488 " * This is a long\n" 1489 " * comment that\n" 1490 " * doesn't fit on\n" 1491 " * one line.\n" 1492 " */", 1493 format("/* \n" 1494 " * This is a long " 1495 " comment that " 1496 " doesn't fit on " 1497 " one line. \n" 1498 " */", 1499 getLLVMStyleWithColumns(20))); 1500 EXPECT_EQ("/*\n" 1501 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1502 " * so_it_should_be_broken\n" 1503 " * wherever_a_space_occurs\n" 1504 " */", 1505 format("/*\n" 1506 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1507 " so_it_should_be_broken " 1508 " wherever_a_space_occurs \n" 1509 " */", 1510 getLLVMStyleWithColumns(20))); 1511 EXPECT_EQ("/*\n" 1512 " * This_comment_can_not_be_broken_into_lines\n" 1513 " */", 1514 format("/*\n" 1515 " * This_comment_can_not_be_broken_into_lines\n" 1516 " */", 1517 getLLVMStyleWithColumns(20))); 1518 EXPECT_EQ("{\n" 1519 " /*\n" 1520 " This is another\n" 1521 " long comment that\n" 1522 " doesn't fit on one\n" 1523 " line 1234567890\n" 1524 " */\n" 1525 "}", 1526 format("{\n" 1527 "/*\n" 1528 "This is another " 1529 " long comment that " 1530 " doesn't fit on one" 1531 " line 1234567890\n" 1532 "*/\n" 1533 "}", 1534 getLLVMStyleWithColumns(20))); 1535 EXPECT_EQ("{\n" 1536 " /*\n" 1537 " * This i s\n" 1538 " * another comment\n" 1539 " * t hat doesn' t\n" 1540 " * fit on one l i\n" 1541 " * n e\n" 1542 " */\n" 1543 "}", 1544 format("{\n" 1545 "/*\n" 1546 " * This i s" 1547 " another comment" 1548 " t hat doesn' t" 1549 " fit on one l i" 1550 " n e\n" 1551 " */\n" 1552 "}", 1553 getLLVMStyleWithColumns(20))); 1554 EXPECT_EQ("/*\n" 1555 " * This is a long\n" 1556 " * comment that\n" 1557 " * doesn't fit on\n" 1558 " * one line\n" 1559 " */", 1560 format(" /*\n" 1561 " * This is a long comment that doesn't fit on one line\n" 1562 " */", 1563 getLLVMStyleWithColumns(20))); 1564 EXPECT_EQ("{\n" 1565 " if (something) /* This is a\n" 1566 " long\n" 1567 " comment */\n" 1568 " ;\n" 1569 "}", 1570 format("{\n" 1571 " if (something) /* This is a long comment */\n" 1572 " ;\n" 1573 "}", 1574 getLLVMStyleWithColumns(30))); 1575 1576 EXPECT_EQ("/* A comment before\n" 1577 " * a macro\n" 1578 " * definition */\n" 1579 "#define a b", 1580 format("/* A comment before a macro definition */\n" 1581 "#define a b", 1582 getLLVMStyleWithColumns(20))); 1583 1584 EXPECT_EQ("/* some comment\n" 1585 " * a comment\n" 1586 "* that we break\n" 1587 " * another comment\n" 1588 "* we have to break\n" 1589 "* a left comment\n" 1590 " */", 1591 format(" /* some comment\n" 1592 " * a comment that we break\n" 1593 " * another comment we have to break\n" 1594 "* a left comment\n" 1595 " */", 1596 getLLVMStyleWithColumns(20))); 1597 1598 EXPECT_EQ("/**\n" 1599 " * multiline block\n" 1600 " * comment\n" 1601 " *\n" 1602 " */", 1603 format("/**\n" 1604 " * multiline block comment\n" 1605 " *\n" 1606 " */", 1607 getLLVMStyleWithColumns(20))); 1608 1609 EXPECT_EQ("/*\n" 1610 "\n" 1611 "\n" 1612 " */\n", 1613 format(" /* \n" 1614 " \n" 1615 " \n" 1616 " */\n")); 1617 1618 EXPECT_EQ("/* a a */", 1619 format("/* a a */", getLLVMStyleWithColumns(15))); 1620 EXPECT_EQ("/* a a bc */", 1621 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1622 EXPECT_EQ("/* aaa aaa\n" 1623 " * aaaaa */", 1624 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1625 EXPECT_EQ("/* aaa aaa\n" 1626 " * aaaaa */", 1627 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1628 } 1629 1630 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1631 EXPECT_EQ("#define X \\\n" 1632 " /* \\\n" 1633 " Test \\\n" 1634 " Macro comment \\\n" 1635 " with a long \\\n" 1636 " line \\\n" 1637 " */ \\\n" 1638 " A + B", 1639 format("#define X \\\n" 1640 " /*\n" 1641 " Test\n" 1642 " Macro comment with a long line\n" 1643 " */ \\\n" 1644 " A + B", 1645 getLLVMStyleWithColumns(20))); 1646 EXPECT_EQ("#define X \\\n" 1647 " /* Macro comment \\\n" 1648 " with a long \\\n" 1649 " line */ \\\n" 1650 " A + B", 1651 format("#define X \\\n" 1652 " /* Macro comment with a long\n" 1653 " line */ \\\n" 1654 " A + B", 1655 getLLVMStyleWithColumns(20))); 1656 EXPECT_EQ("#define X \\\n" 1657 " /* Macro comment \\\n" 1658 " * with a long \\\n" 1659 " * line */ \\\n" 1660 " A + B", 1661 format("#define X \\\n" 1662 " /* Macro comment with a long line */ \\\n" 1663 " A + B", 1664 getLLVMStyleWithColumns(20))); 1665 } 1666 1667 TEST_F(FormatTest, CommentsInStaticInitializers) { 1668 EXPECT_EQ( 1669 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1670 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1671 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1672 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1673 " aaaaaaaaaaaaaaaaaaaa};", 1674 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1675 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1676 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1677 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1678 " aaaaaaaaaaaaaaaaaaaa };")); 1679 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1680 " bbbbbbbbbbb, ccccccccccc};"); 1681 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1682 " // comment for bb....\n" 1683 " bbbbbbbbbbb, ccccccccccc};"); 1684 verifyGoogleFormat( 1685 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1686 " bbbbbbbbbbb, ccccccccccc};"); 1687 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1688 " // comment for bb....\n" 1689 " bbbbbbbbbbb, ccccccccccc};"); 1690 1691 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1692 " {d, e, f}, // Group #2\n" 1693 " {g, h, i}}; // Group #3"); 1694 verifyFormat("S s = {{// Group #1\n" 1695 " a, b, c},\n" 1696 " {// Group #2\n" 1697 " d, e, f},\n" 1698 " {// Group #3\n" 1699 " g, h, i}};"); 1700 1701 EXPECT_EQ("S s = {\n" 1702 " // Some comment\n" 1703 " a,\n" 1704 "\n" 1705 " // Comment after empty line\n" 1706 " b}", 1707 format("S s = {\n" 1708 " // Some comment\n" 1709 " a,\n" 1710 " \n" 1711 " // Comment after empty line\n" 1712 " b\n" 1713 "}")); 1714 EXPECT_EQ("S s = {\n" 1715 " /* Some comment */\n" 1716 " a,\n" 1717 "\n" 1718 " /* Comment after empty line */\n" 1719 " b}", 1720 format("S s = {\n" 1721 " /* Some comment */\n" 1722 " a,\n" 1723 " \n" 1724 " /* Comment after empty line */\n" 1725 " b\n" 1726 "}")); 1727 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1728 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1729 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1730 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1731 } 1732 1733 TEST_F(FormatTest, IgnoresIf0Contents) { 1734 EXPECT_EQ("#if 0\n" 1735 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1736 "#endif\n" 1737 "void f() {}", 1738 format("#if 0\n" 1739 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1740 "#endif\n" 1741 "void f( ) { }")); 1742 EXPECT_EQ("#if false\n" 1743 "void f( ) { }\n" 1744 "#endif\n" 1745 "void g() {}\n", 1746 format("#if false\n" 1747 "void f( ) { }\n" 1748 "#endif\n" 1749 "void g( ) { }\n")); 1750 EXPECT_EQ("enum E {\n" 1751 " One,\n" 1752 " Two,\n" 1753 "#if 0\n" 1754 "Three,\n" 1755 " Four,\n" 1756 "#endif\n" 1757 " Five\n" 1758 "};", 1759 format("enum E {\n" 1760 " One,Two,\n" 1761 "#if 0\n" 1762 "Three,\n" 1763 " Four,\n" 1764 "#endif\n" 1765 " Five};")); 1766 EXPECT_EQ("enum F {\n" 1767 " One,\n" 1768 "#if 1\n" 1769 " Two,\n" 1770 "#if 0\n" 1771 "Three,\n" 1772 " Four,\n" 1773 "#endif\n" 1774 " Five\n" 1775 "#endif\n" 1776 "};", 1777 format("enum F {\n" 1778 "One,\n" 1779 "#if 1\n" 1780 "Two,\n" 1781 "#if 0\n" 1782 "Three,\n" 1783 " Four,\n" 1784 "#endif\n" 1785 "Five\n" 1786 "#endif\n" 1787 "};")); 1788 EXPECT_EQ("enum G {\n" 1789 " One,\n" 1790 "#if 0\n" 1791 "Two,\n" 1792 "#else\n" 1793 " Three,\n" 1794 "#endif\n" 1795 " Four\n" 1796 "};", 1797 format("enum G {\n" 1798 "One,\n" 1799 "#if 0\n" 1800 "Two,\n" 1801 "#else\n" 1802 "Three,\n" 1803 "#endif\n" 1804 "Four\n" 1805 "};")); 1806 EXPECT_EQ("enum H {\n" 1807 " One,\n" 1808 "#if 0\n" 1809 "#ifdef Q\n" 1810 "Two,\n" 1811 "#else\n" 1812 "Three,\n" 1813 "#endif\n" 1814 "#endif\n" 1815 " Four\n" 1816 "};", 1817 format("enum H {\n" 1818 "One,\n" 1819 "#if 0\n" 1820 "#ifdef Q\n" 1821 "Two,\n" 1822 "#else\n" 1823 "Three,\n" 1824 "#endif\n" 1825 "#endif\n" 1826 "Four\n" 1827 "};")); 1828 EXPECT_EQ("enum I {\n" 1829 " One,\n" 1830 "#if /* test */ 0 || 1\n" 1831 "Two,\n" 1832 "Three,\n" 1833 "#endif\n" 1834 " Four\n" 1835 "};", 1836 format("enum I {\n" 1837 "One,\n" 1838 "#if /* test */ 0 || 1\n" 1839 "Two,\n" 1840 "Three,\n" 1841 "#endif\n" 1842 "Four\n" 1843 "};")); 1844 EXPECT_EQ("enum J {\n" 1845 " One,\n" 1846 "#if 0\n" 1847 "#if 0\n" 1848 "Two,\n" 1849 "#else\n" 1850 "Three,\n" 1851 "#endif\n" 1852 "Four,\n" 1853 "#endif\n" 1854 " Five\n" 1855 "};", 1856 format("enum J {\n" 1857 "One,\n" 1858 "#if 0\n" 1859 "#if 0\n" 1860 "Two,\n" 1861 "#else\n" 1862 "Three,\n" 1863 "#endif\n" 1864 "Four,\n" 1865 "#endif\n" 1866 "Five\n" 1867 "};")); 1868 } 1869 1870 //===----------------------------------------------------------------------===// 1871 // Tests for classes, namespaces, etc. 1872 //===----------------------------------------------------------------------===// 1873 1874 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1875 verifyFormat("class A {};"); 1876 } 1877 1878 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1879 verifyFormat("class A {\n" 1880 "public:\n" 1881 "public: // comment\n" 1882 "protected:\n" 1883 "private:\n" 1884 " void f() {}\n" 1885 "};"); 1886 verifyGoogleFormat("class A {\n" 1887 " public:\n" 1888 " protected:\n" 1889 " private:\n" 1890 " void f() {}\n" 1891 "};"); 1892 verifyFormat("class A {\n" 1893 "public slots:\n" 1894 " void f1() {}\n" 1895 "public Q_SLOTS:\n" 1896 " void f2() {}\n" 1897 "protected slots:\n" 1898 " void f3() {}\n" 1899 "protected Q_SLOTS:\n" 1900 " void f4() {}\n" 1901 "private slots:\n" 1902 " void f5() {}\n" 1903 "private Q_SLOTS:\n" 1904 " void f6() {}\n" 1905 "signals:\n" 1906 " void g1();\n" 1907 "Q_SIGNALS:\n" 1908 " void g2();\n" 1909 "};"); 1910 1911 // Don't interpret 'signals' the wrong way. 1912 verifyFormat("signals.set();"); 1913 verifyFormat("for (Signals signals : f()) {\n}"); 1914 verifyFormat("{\n" 1915 " signals.set(); // This needs indentation.\n" 1916 "}"); 1917 } 1918 1919 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1920 EXPECT_EQ("class A {\n" 1921 "public:\n" 1922 " void f();\n" 1923 "\n" 1924 "private:\n" 1925 " void g() {}\n" 1926 " // test\n" 1927 "protected:\n" 1928 " int h;\n" 1929 "};", 1930 format("class A {\n" 1931 "public:\n" 1932 "void f();\n" 1933 "private:\n" 1934 "void g() {}\n" 1935 "// test\n" 1936 "protected:\n" 1937 "int h;\n" 1938 "};")); 1939 EXPECT_EQ("class A {\n" 1940 "protected:\n" 1941 "public:\n" 1942 " void f();\n" 1943 "};", 1944 format("class A {\n" 1945 "protected:\n" 1946 "\n" 1947 "public:\n" 1948 "\n" 1949 " void f();\n" 1950 "};")); 1951 1952 // Even ensure proper spacing inside macros. 1953 EXPECT_EQ("#define B \\\n" 1954 " class A { \\\n" 1955 " protected: \\\n" 1956 " public: \\\n" 1957 " void f(); \\\n" 1958 " };", 1959 format("#define B \\\n" 1960 " class A { \\\n" 1961 " protected: \\\n" 1962 " \\\n" 1963 " public: \\\n" 1964 " \\\n" 1965 " void f(); \\\n" 1966 " };", 1967 getGoogleStyle())); 1968 // But don't remove empty lines after macros ending in access specifiers. 1969 EXPECT_EQ("#define A private:\n" 1970 "\n" 1971 "int i;", 1972 format("#define A private:\n" 1973 "\n" 1974 "int i;")); 1975 } 1976 1977 TEST_F(FormatTest, FormatsClasses) { 1978 verifyFormat("class A : public B {};"); 1979 verifyFormat("class A : public ::B {};"); 1980 1981 verifyFormat( 1982 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1983 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1984 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1985 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1986 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1987 verifyFormat( 1988 "class A : public B, public C, public D, public E, public F {};"); 1989 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1990 " public C,\n" 1991 " public D,\n" 1992 " public E,\n" 1993 " public F,\n" 1994 " public G {};"); 1995 1996 verifyFormat("class\n" 1997 " ReallyReallyLongClassName {\n" 1998 " int i;\n" 1999 "};", 2000 getLLVMStyleWithColumns(32)); 2001 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2002 " aaaaaaaaaaaaaaaa> {};"); 2003 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2004 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2005 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2006 verifyFormat("template <class R, class C>\n" 2007 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2008 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2009 verifyFormat("class ::A::B {};"); 2010 } 2011 2012 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2013 verifyFormat("class A {\n} a, b;"); 2014 verifyFormat("struct A {\n} a, b;"); 2015 verifyFormat("union A {\n} a;"); 2016 } 2017 2018 TEST_F(FormatTest, FormatsEnum) { 2019 verifyFormat("enum {\n" 2020 " Zero,\n" 2021 " One = 1,\n" 2022 " Two = One + 1,\n" 2023 " Three = (One + Two),\n" 2024 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2025 " Five = (One, Two, Three, Four, 5)\n" 2026 "};"); 2027 verifyGoogleFormat("enum {\n" 2028 " Zero,\n" 2029 " One = 1,\n" 2030 " Two = One + 1,\n" 2031 " Three = (One + Two),\n" 2032 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2033 " Five = (One, Two, Three, Four, 5)\n" 2034 "};"); 2035 verifyFormat("enum Enum {};"); 2036 verifyFormat("enum {};"); 2037 verifyFormat("enum X E {} d;"); 2038 verifyFormat("enum __attribute__((...)) E {} d;"); 2039 verifyFormat("enum __declspec__((...)) E {} d;"); 2040 verifyFormat("enum {\n" 2041 " Bar = Foo<int, int>::value\n" 2042 "};", 2043 getLLVMStyleWithColumns(30)); 2044 2045 verifyFormat("enum ShortEnum { A, B, C };"); 2046 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2047 2048 EXPECT_EQ("enum KeepEmptyLines {\n" 2049 " ONE,\n" 2050 "\n" 2051 " TWO,\n" 2052 "\n" 2053 " THREE\n" 2054 "}", 2055 format("enum KeepEmptyLines {\n" 2056 " ONE,\n" 2057 "\n" 2058 " TWO,\n" 2059 "\n" 2060 "\n" 2061 " THREE\n" 2062 "}")); 2063 verifyFormat("enum E { // comment\n" 2064 " ONE,\n" 2065 " TWO\n" 2066 "};\n" 2067 "int i;"); 2068 // Not enums. 2069 verifyFormat("enum X f() {\n" 2070 " a();\n" 2071 " return 42;\n" 2072 "}"); 2073 verifyFormat("enum X Type::f() {\n" 2074 " a();\n" 2075 " return 42;\n" 2076 "}"); 2077 verifyFormat("enum ::X f() {\n" 2078 " a();\n" 2079 " return 42;\n" 2080 "}"); 2081 verifyFormat("enum ns::X f() {\n" 2082 " a();\n" 2083 " return 42;\n" 2084 "}"); 2085 } 2086 2087 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2088 verifyFormat("enum Type {\n" 2089 " One = 0; // These semicolons should be commas.\n" 2090 " Two = 1;\n" 2091 "};"); 2092 verifyFormat("namespace n {\n" 2093 "enum Type {\n" 2094 " One,\n" 2095 " Two, // missing };\n" 2096 " int i;\n" 2097 "}\n" 2098 "void g() {}"); 2099 } 2100 2101 TEST_F(FormatTest, FormatsEnumStruct) { 2102 verifyFormat("enum struct {\n" 2103 " Zero,\n" 2104 " One = 1,\n" 2105 " Two = One + 1,\n" 2106 " Three = (One + Two),\n" 2107 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2108 " Five = (One, Two, Three, Four, 5)\n" 2109 "};"); 2110 verifyFormat("enum struct Enum {};"); 2111 verifyFormat("enum struct {};"); 2112 verifyFormat("enum struct X E {} d;"); 2113 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2114 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2115 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2116 } 2117 2118 TEST_F(FormatTest, FormatsEnumClass) { 2119 verifyFormat("enum class {\n" 2120 " Zero,\n" 2121 " One = 1,\n" 2122 " Two = One + 1,\n" 2123 " Three = (One + Two),\n" 2124 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2125 " Five = (One, Two, Three, Four, 5)\n" 2126 "};"); 2127 verifyFormat("enum class Enum {};"); 2128 verifyFormat("enum class {};"); 2129 verifyFormat("enum class X E {} d;"); 2130 verifyFormat("enum class __attribute__((...)) E {} d;"); 2131 verifyFormat("enum class __declspec__((...)) E {} d;"); 2132 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2133 } 2134 2135 TEST_F(FormatTest, FormatsEnumTypes) { 2136 verifyFormat("enum X : int {\n" 2137 " A, // Force multiple lines.\n" 2138 " B\n" 2139 "};"); 2140 verifyFormat("enum X : int { A, B };"); 2141 verifyFormat("enum X : std::uint32_t { A, B };"); 2142 } 2143 2144 TEST_F(FormatTest, FormatsNSEnums) { 2145 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2146 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2147 " // Information about someDecentlyLongValue.\n" 2148 " someDecentlyLongValue,\n" 2149 " // Information about anotherDecentlyLongValue.\n" 2150 " anotherDecentlyLongValue,\n" 2151 " // Information about aThirdDecentlyLongValue.\n" 2152 " aThirdDecentlyLongValue\n" 2153 "};"); 2154 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2155 " a = 1,\n" 2156 " b = 2,\n" 2157 " c = 3,\n" 2158 "};"); 2159 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2160 " a = 1,\n" 2161 " b = 2,\n" 2162 " c = 3,\n" 2163 "};"); 2164 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2165 " a = 1,\n" 2166 " b = 2,\n" 2167 " c = 3,\n" 2168 "};"); 2169 } 2170 2171 TEST_F(FormatTest, FormatsBitfields) { 2172 verifyFormat("struct Bitfields {\n" 2173 " unsigned sClass : 8;\n" 2174 " unsigned ValueKind : 2;\n" 2175 "};"); 2176 verifyFormat("struct A {\n" 2177 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2178 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2179 "};"); 2180 verifyFormat("struct MyStruct {\n" 2181 " uchar data;\n" 2182 " uchar : 8;\n" 2183 " uchar : 8;\n" 2184 " uchar other;\n" 2185 "};"); 2186 } 2187 2188 TEST_F(FormatTest, FormatsNamespaces) { 2189 verifyFormat("namespace some_namespace {\n" 2190 "class A {};\n" 2191 "void f() { f(); }\n" 2192 "}"); 2193 verifyFormat("namespace {\n" 2194 "class A {};\n" 2195 "void f() { f(); }\n" 2196 "}"); 2197 verifyFormat("inline namespace X {\n" 2198 "class A {};\n" 2199 "void f() { f(); }\n" 2200 "}"); 2201 verifyFormat("using namespace some_namespace;\n" 2202 "class A {};\n" 2203 "void f() { f(); }"); 2204 2205 // This code is more common than we thought; if we 2206 // layout this correctly the semicolon will go into 2207 // its own line, which is undesirable. 2208 verifyFormat("namespace {};"); 2209 verifyFormat("namespace {\n" 2210 "class A {};\n" 2211 "};"); 2212 2213 verifyFormat("namespace {\n" 2214 "int SomeVariable = 0; // comment\n" 2215 "} // namespace"); 2216 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2217 "#define HEADER_GUARD\n" 2218 "namespace my_namespace {\n" 2219 "int i;\n" 2220 "} // my_namespace\n" 2221 "#endif // HEADER_GUARD", 2222 format("#ifndef HEADER_GUARD\n" 2223 " #define HEADER_GUARD\n" 2224 " namespace my_namespace {\n" 2225 "int i;\n" 2226 "} // my_namespace\n" 2227 "#endif // HEADER_GUARD")); 2228 2229 EXPECT_EQ("namespace A::B {\n" 2230 "class C {};\n" 2231 "}", 2232 format("namespace A::B {\n" 2233 "class C {};\n" 2234 "}")); 2235 2236 FormatStyle Style = getLLVMStyle(); 2237 Style.NamespaceIndentation = FormatStyle::NI_All; 2238 EXPECT_EQ("namespace out {\n" 2239 " int i;\n" 2240 " namespace in {\n" 2241 " int i;\n" 2242 " } // namespace\n" 2243 "} // namespace", 2244 format("namespace out {\n" 2245 "int i;\n" 2246 "namespace in {\n" 2247 "int i;\n" 2248 "} // namespace\n" 2249 "} // namespace", 2250 Style)); 2251 2252 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2253 EXPECT_EQ("namespace out {\n" 2254 "int i;\n" 2255 "namespace in {\n" 2256 " int i;\n" 2257 "} // namespace\n" 2258 "} // namespace", 2259 format("namespace out {\n" 2260 "int i;\n" 2261 "namespace in {\n" 2262 "int i;\n" 2263 "} // namespace\n" 2264 "} // namespace", 2265 Style)); 2266 } 2267 2268 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2269 2270 TEST_F(FormatTest, FormatsInlineASM) { 2271 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2272 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2273 verifyFormat( 2274 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2275 " \"cpuid\\n\\t\"\n" 2276 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2277 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2278 " : \"a\"(value));"); 2279 EXPECT_EQ( 2280 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2281 " __asm {\n" 2282 " mov edx,[that] // vtable in edx\n" 2283 " mov eax,methodIndex\n" 2284 " call [edx][eax*4] // stdcall\n" 2285 " }\n" 2286 "}", 2287 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2288 " __asm {\n" 2289 " mov edx,[that] // vtable in edx\n" 2290 " mov eax,methodIndex\n" 2291 " call [edx][eax*4] // stdcall\n" 2292 " }\n" 2293 "}")); 2294 EXPECT_EQ("_asm {\n" 2295 " xor eax, eax;\n" 2296 " cpuid;\n" 2297 "}", 2298 format("_asm {\n" 2299 " xor eax, eax;\n" 2300 " cpuid;\n" 2301 "}")); 2302 verifyFormat("void function() {\n" 2303 " // comment\n" 2304 " asm(\"\");\n" 2305 "}"); 2306 EXPECT_EQ("__asm {\n" 2307 "}\n" 2308 "int i;", 2309 format("__asm {\n" 2310 "}\n" 2311 "int i;")); 2312 } 2313 2314 TEST_F(FormatTest, FormatTryCatch) { 2315 verifyFormat("try {\n" 2316 " throw a * b;\n" 2317 "} catch (int a) {\n" 2318 " // Do nothing.\n" 2319 "} catch (...) {\n" 2320 " exit(42);\n" 2321 "}"); 2322 2323 // Function-level try statements. 2324 verifyFormat("int f() try { return 4; } catch (...) {\n" 2325 " return 5;\n" 2326 "}"); 2327 verifyFormat("class A {\n" 2328 " int a;\n" 2329 " A() try : a(0) {\n" 2330 " } catch (...) {\n" 2331 " throw;\n" 2332 " }\n" 2333 "};\n"); 2334 2335 // Incomplete try-catch blocks. 2336 verifyIncompleteFormat("try {} catch ("); 2337 } 2338 2339 TEST_F(FormatTest, FormatSEHTryCatch) { 2340 verifyFormat("__try {\n" 2341 " int a = b * c;\n" 2342 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2343 " // Do nothing.\n" 2344 "}"); 2345 2346 verifyFormat("__try {\n" 2347 " int a = b * c;\n" 2348 "} __finally {\n" 2349 " // Do nothing.\n" 2350 "}"); 2351 2352 verifyFormat("DEBUG({\n" 2353 " __try {\n" 2354 " } __finally {\n" 2355 " }\n" 2356 "});\n"); 2357 } 2358 2359 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2360 verifyFormat("try {\n" 2361 " f();\n" 2362 "} catch {\n" 2363 " g();\n" 2364 "}"); 2365 verifyFormat("try {\n" 2366 " f();\n" 2367 "} catch (A a) MACRO(x) {\n" 2368 " g();\n" 2369 "} catch (B b) MACRO(x) {\n" 2370 " g();\n" 2371 "}"); 2372 } 2373 2374 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2375 FormatStyle Style = getLLVMStyle(); 2376 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2377 FormatStyle::BS_WebKit}) { 2378 Style.BreakBeforeBraces = BraceStyle; 2379 verifyFormat("try {\n" 2380 " // something\n" 2381 "} catch (...) {\n" 2382 " // something\n" 2383 "}", 2384 Style); 2385 } 2386 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2387 verifyFormat("try {\n" 2388 " // something\n" 2389 "}\n" 2390 "catch (...) {\n" 2391 " // something\n" 2392 "}", 2393 Style); 2394 verifyFormat("__try {\n" 2395 " // something\n" 2396 "}\n" 2397 "__finally {\n" 2398 " // something\n" 2399 "}", 2400 Style); 2401 verifyFormat("@try {\n" 2402 " // something\n" 2403 "}\n" 2404 "@finally {\n" 2405 " // something\n" 2406 "}", 2407 Style); 2408 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2409 verifyFormat("try\n" 2410 "{\n" 2411 " // something\n" 2412 "}\n" 2413 "catch (...)\n" 2414 "{\n" 2415 " // something\n" 2416 "}", 2417 Style); 2418 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2419 verifyFormat("try\n" 2420 " {\n" 2421 " // something\n" 2422 " }\n" 2423 "catch (...)\n" 2424 " {\n" 2425 " // something\n" 2426 " }", 2427 Style); 2428 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2429 Style.BraceWrapping.BeforeCatch = true; 2430 verifyFormat("try {\n" 2431 " // something\n" 2432 "}\n" 2433 "catch (...) {\n" 2434 " // something\n" 2435 "}", 2436 Style); 2437 } 2438 2439 TEST_F(FormatTest, FormatObjCTryCatch) { 2440 verifyFormat("@try {\n" 2441 " f();\n" 2442 "} @catch (NSException e) {\n" 2443 " @throw;\n" 2444 "} @finally {\n" 2445 " exit(42);\n" 2446 "}"); 2447 verifyFormat("DEBUG({\n" 2448 " @try {\n" 2449 " } @finally {\n" 2450 " }\n" 2451 "});\n"); 2452 } 2453 2454 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2455 FormatStyle Style = getLLVMStyle(); 2456 verifyFormat("@autoreleasepool {\n" 2457 " f();\n" 2458 "}\n" 2459 "@autoreleasepool {\n" 2460 " f();\n" 2461 "}\n", 2462 Style); 2463 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2464 verifyFormat("@autoreleasepool\n" 2465 "{\n" 2466 " f();\n" 2467 "}\n" 2468 "@autoreleasepool\n" 2469 "{\n" 2470 " f();\n" 2471 "}\n", 2472 Style); 2473 } 2474 2475 TEST_F(FormatTest, StaticInitializers) { 2476 verifyFormat("static SomeClass SC = {1, 'a'};"); 2477 2478 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2479 " 100000000, " 2480 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2481 2482 // Here, everything other than the "}" would fit on a line. 2483 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2484 " 10000000000000000000000000};"); 2485 EXPECT_EQ("S s = {a,\n" 2486 "\n" 2487 " b};", 2488 format("S s = {\n" 2489 " a,\n" 2490 "\n" 2491 " b\n" 2492 "};")); 2493 2494 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2495 // line. However, the formatting looks a bit off and this probably doesn't 2496 // happen often in practice. 2497 verifyFormat("static int Variable[1] = {\n" 2498 " {1000000000000000000000000000000000000}};", 2499 getLLVMStyleWithColumns(40)); 2500 } 2501 2502 TEST_F(FormatTest, DesignatedInitializers) { 2503 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2504 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2505 " .bbbbbbbbbb = 2,\n" 2506 " .cccccccccc = 3,\n" 2507 " .dddddddddd = 4,\n" 2508 " .eeeeeeeeee = 5};"); 2509 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2510 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2511 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2512 " .ccccccccccccccccccccccccccc = 3,\n" 2513 " .ddddddddddddddddddddddddddd = 4,\n" 2514 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2515 2516 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2517 } 2518 2519 TEST_F(FormatTest, NestedStaticInitializers) { 2520 verifyFormat("static A x = {{{}}};\n"); 2521 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2522 " {init1, init2, init3, init4}}};", 2523 getLLVMStyleWithColumns(50)); 2524 2525 verifyFormat("somes Status::global_reps[3] = {\n" 2526 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2527 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2528 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2529 getLLVMStyleWithColumns(60)); 2530 verifyGoogleFormat("SomeType 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 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2535 " {rect.fRight - rect.fLeft, rect.fBottom - " 2536 "rect.fTop}};"); 2537 2538 verifyFormat( 2539 "SomeArrayOfSomeType a = {\n" 2540 " {{1, 2, 3},\n" 2541 " {1, 2, 3},\n" 2542 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2543 " 333333333333333333333333333333},\n" 2544 " {1, 2, 3},\n" 2545 " {1, 2, 3}}};"); 2546 verifyFormat( 2547 "SomeArrayOfSomeType a = {\n" 2548 " {{1, 2, 3}},\n" 2549 " {{1, 2, 3}},\n" 2550 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2551 " 333333333333333333333333333333}},\n" 2552 " {{1, 2, 3}},\n" 2553 " {{1, 2, 3}}};"); 2554 2555 verifyFormat("struct {\n" 2556 " unsigned bit;\n" 2557 " const char *const name;\n" 2558 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2559 " {kOsWin, \"Windows\"},\n" 2560 " {kOsLinux, \"Linux\"},\n" 2561 " {kOsCrOS, \"Chrome OS\"}};"); 2562 verifyFormat("struct {\n" 2563 " unsigned bit;\n" 2564 " const char *const name;\n" 2565 "} kBitsToOs[] = {\n" 2566 " {kOsMac, \"Mac\"},\n" 2567 " {kOsWin, \"Windows\"},\n" 2568 " {kOsLinux, \"Linux\"},\n" 2569 " {kOsCrOS, \"Chrome OS\"},\n" 2570 "};"); 2571 } 2572 2573 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2574 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2575 " \\\n" 2576 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2577 } 2578 2579 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2580 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2581 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2582 2583 // Do break defaulted and deleted functions. 2584 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2585 " default;", 2586 getLLVMStyleWithColumns(40)); 2587 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2588 " delete;", 2589 getLLVMStyleWithColumns(40)); 2590 } 2591 2592 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2593 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2594 getLLVMStyleWithColumns(40)); 2595 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2596 getLLVMStyleWithColumns(40)); 2597 EXPECT_EQ("#define Q \\\n" 2598 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2599 " \"aaaaaaaa.cpp\"", 2600 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2601 getLLVMStyleWithColumns(40))); 2602 } 2603 2604 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2605 EXPECT_EQ("# 123 \"A string literal\"", 2606 format(" # 123 \"A string literal\"")); 2607 } 2608 2609 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2610 EXPECT_EQ("#;", format("#;")); 2611 verifyFormat("#\n;\n;\n;"); 2612 } 2613 2614 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2615 EXPECT_EQ("#line 42 \"test\"\n", 2616 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2617 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2618 getLLVMStyleWithColumns(12))); 2619 } 2620 2621 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2622 EXPECT_EQ("#line 42 \"test\"", 2623 format("# \\\n line \\\n 42 \\\n \"test\"")); 2624 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2625 } 2626 2627 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2628 verifyFormat("#define A \\x20"); 2629 verifyFormat("#define A \\ x20"); 2630 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2631 verifyFormat("#define A ''"); 2632 verifyFormat("#define A ''qqq"); 2633 verifyFormat("#define A `qqq"); 2634 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2635 EXPECT_EQ("const char *c = STRINGIFY(\n" 2636 "\\na : b);", 2637 format("const char * c = STRINGIFY(\n" 2638 "\\na : b);")); 2639 2640 verifyFormat("a\r\\"); 2641 verifyFormat("a\v\\"); 2642 verifyFormat("a\f\\"); 2643 } 2644 2645 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2646 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2647 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2648 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2649 // FIXME: We never break before the macro name. 2650 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2651 2652 verifyFormat("#define A A\n#define A A"); 2653 verifyFormat("#define A(X) A\n#define A A"); 2654 2655 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2656 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2657 } 2658 2659 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2660 EXPECT_EQ("// somecomment\n" 2661 "#include \"a.h\"\n" 2662 "#define A( \\\n" 2663 " A, B)\n" 2664 "#include \"b.h\"\n" 2665 "// somecomment\n", 2666 format(" // somecomment\n" 2667 " #include \"a.h\"\n" 2668 "#define A(A,\\\n" 2669 " B)\n" 2670 " #include \"b.h\"\n" 2671 " // somecomment\n", 2672 getLLVMStyleWithColumns(13))); 2673 } 2674 2675 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2676 2677 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2678 EXPECT_EQ("#define A \\\n" 2679 " c; \\\n" 2680 " e;\n" 2681 "f;", 2682 format("#define A c; e;\n" 2683 "f;", 2684 getLLVMStyleWithColumns(14))); 2685 } 2686 2687 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2688 2689 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2690 EXPECT_EQ("int x,\n" 2691 "#define A\n" 2692 " y;", 2693 format("int x,\n#define A\ny;")); 2694 } 2695 2696 TEST_F(FormatTest, HashInMacroDefinition) { 2697 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2698 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2699 verifyFormat("#define A \\\n" 2700 " { \\\n" 2701 " f(#c); \\\n" 2702 " }", 2703 getLLVMStyleWithColumns(11)); 2704 2705 verifyFormat("#define A(X) \\\n" 2706 " void function##X()", 2707 getLLVMStyleWithColumns(22)); 2708 2709 verifyFormat("#define A(a, b, c) \\\n" 2710 " void a##b##c()", 2711 getLLVMStyleWithColumns(22)); 2712 2713 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2714 } 2715 2716 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2717 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2718 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2719 } 2720 2721 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2722 EXPECT_EQ("#define A b;", format("#define A \\\n" 2723 " \\\n" 2724 " b;", 2725 getLLVMStyleWithColumns(25))); 2726 EXPECT_EQ("#define A \\\n" 2727 " \\\n" 2728 " a; \\\n" 2729 " b;", 2730 format("#define A \\\n" 2731 " \\\n" 2732 " a; \\\n" 2733 " b;", 2734 getLLVMStyleWithColumns(11))); 2735 EXPECT_EQ("#define A \\\n" 2736 " a; \\\n" 2737 " \\\n" 2738 " b;", 2739 format("#define A \\\n" 2740 " a; \\\n" 2741 " \\\n" 2742 " b;", 2743 getLLVMStyleWithColumns(11))); 2744 } 2745 2746 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2747 verifyIncompleteFormat("#define A :"); 2748 verifyFormat("#define SOMECASES \\\n" 2749 " case 1: \\\n" 2750 " case 2\n", 2751 getLLVMStyleWithColumns(20)); 2752 verifyFormat("#define A template <typename T>"); 2753 verifyIncompleteFormat("#define STR(x) #x\n" 2754 "f(STR(this_is_a_string_literal{));"); 2755 verifyFormat("#pragma omp threadprivate( \\\n" 2756 " y)), // expected-warning", 2757 getLLVMStyleWithColumns(28)); 2758 verifyFormat("#d, = };"); 2759 verifyFormat("#if \"a"); 2760 verifyIncompleteFormat("({\n" 2761 "#define b \\\n" 2762 " } \\\n" 2763 " a\n" 2764 "a", 2765 getLLVMStyleWithColumns(15)); 2766 verifyFormat("#define A \\\n" 2767 " { \\\n" 2768 " {\n" 2769 "#define B \\\n" 2770 " } \\\n" 2771 " }", 2772 getLLVMStyleWithColumns(15)); 2773 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2774 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2775 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2776 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2777 } 2778 2779 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2780 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2781 EXPECT_EQ("class A : public QObject {\n" 2782 " Q_OBJECT\n" 2783 "\n" 2784 " A() {}\n" 2785 "};", 2786 format("class A : public QObject {\n" 2787 " Q_OBJECT\n" 2788 "\n" 2789 " A() {\n}\n" 2790 "} ;")); 2791 EXPECT_EQ("MACRO\n" 2792 "/*static*/ int i;", 2793 format("MACRO\n" 2794 " /*static*/ int i;")); 2795 EXPECT_EQ("SOME_MACRO\n" 2796 "namespace {\n" 2797 "void f();\n" 2798 "}", 2799 format("SOME_MACRO\n" 2800 " namespace {\n" 2801 "void f( );\n" 2802 "}")); 2803 // Only if the identifier contains at least 5 characters. 2804 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2805 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2806 // Only if everything is upper case. 2807 EXPECT_EQ("class A : public QObject {\n" 2808 " Q_Object A() {}\n" 2809 "};", 2810 format("class A : public QObject {\n" 2811 " Q_Object\n" 2812 " A() {\n}\n" 2813 "} ;")); 2814 2815 // Only if the next line can actually start an unwrapped line. 2816 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2817 format("SOME_WEIRD_LOG_MACRO\n" 2818 "<< SomeThing;")); 2819 2820 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2821 "(n, buffers))\n", 2822 getChromiumStyle(FormatStyle::LK_Cpp)); 2823 } 2824 2825 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2826 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2827 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2828 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2829 "class X {};\n" 2830 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2831 "int *createScopDetectionPass() { return 0; }", 2832 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2833 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2834 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2835 " class X {};\n" 2836 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2837 " int *createScopDetectionPass() { return 0; }")); 2838 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2839 // braces, so that inner block is indented one level more. 2840 EXPECT_EQ("int q() {\n" 2841 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2842 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2843 " IPC_END_MESSAGE_MAP()\n" 2844 "}", 2845 format("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 2851 // Same inside macros. 2852 EXPECT_EQ("#define LIST(L) \\\n" 2853 " L(A) \\\n" 2854 " L(B) \\\n" 2855 " L(C)", 2856 format("#define LIST(L) \\\n" 2857 " L(A) \\\n" 2858 " L(B) \\\n" 2859 " L(C)", 2860 getGoogleStyle())); 2861 2862 // These must not be recognized as macros. 2863 EXPECT_EQ("int q() {\n" 2864 " f(x);\n" 2865 " f(x) {}\n" 2866 " f(x)->g();\n" 2867 " f(x)->*g();\n" 2868 " f(x).g();\n" 2869 " f(x) = x;\n" 2870 " f(x) += x;\n" 2871 " f(x) -= x;\n" 2872 " f(x) *= x;\n" 2873 " f(x) /= x;\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)[y].z();\n" 2881 " LOG(INFO) << x;\n" 2882 " ifstream(x) >> x;\n" 2883 "}\n", 2884 format("int q() {\n" 2885 " f(x)\n;\n" 2886 " f(x)\n {}\n" 2887 " f(x)\n->g();\n" 2888 " f(x)\n->*g();\n" 2889 " f(x)\n.g();\n" 2890 " f(x)\n = x;\n" 2891 " f(x)\n += x;\n" 2892 " f(x)\n -= x;\n" 2893 " f(x)\n *= x;\n" 2894 " f(x)\n /= x;\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[y].z();\n" 2902 " LOG(INFO)\n << x;\n" 2903 " ifstream(x)\n >> x;\n" 2904 "}\n")); 2905 EXPECT_EQ("int q() {\n" 2906 " F(x)\n" 2907 " if (1) {\n" 2908 " }\n" 2909 " F(x)\n" 2910 " while (1) {\n" 2911 " }\n" 2912 " F(x)\n" 2913 " G(x);\n" 2914 " F(x)\n" 2915 " try {\n" 2916 " Q();\n" 2917 " } catch (...) {\n" 2918 " }\n" 2919 "}\n", 2920 format("int q() {\n" 2921 "F(x)\n" 2922 "if (1) {}\n" 2923 "F(x)\n" 2924 "while (1) {}\n" 2925 "F(x)\n" 2926 "G(x);\n" 2927 "F(x)\n" 2928 "try { Q(); } catch (...) {}\n" 2929 "}\n")); 2930 EXPECT_EQ("class A {\n" 2931 " A() : t(0) {}\n" 2932 " A(int i) noexcept() : {}\n" 2933 " A(X x)\n" // FIXME: function-level try blocks are broken. 2934 " try : t(0) {\n" 2935 " } catch (...) {\n" 2936 " }\n" 2937 "};", 2938 format("class A {\n" 2939 " A()\n : t(0) {}\n" 2940 " A(int i)\n noexcept() : {}\n" 2941 " A(X x)\n" 2942 " try : t(0) {} catch (...) {}\n" 2943 "};")); 2944 EXPECT_EQ("class SomeClass {\n" 2945 "public:\n" 2946 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2947 "};", 2948 format("class SomeClass {\n" 2949 "public:\n" 2950 " SomeClass()\n" 2951 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2952 "};")); 2953 EXPECT_EQ("class SomeClass {\n" 2954 "public:\n" 2955 " SomeClass()\n" 2956 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2957 "};", 2958 format("class SomeClass {\n" 2959 "public:\n" 2960 " SomeClass()\n" 2961 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2962 "};", 2963 getLLVMStyleWithColumns(40))); 2964 2965 verifyFormat("MACRO(>)"); 2966 } 2967 2968 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2969 verifyFormat("#define A \\\n" 2970 " f({ \\\n" 2971 " g(); \\\n" 2972 " });", 2973 getLLVMStyleWithColumns(11)); 2974 } 2975 2976 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2977 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2978 } 2979 2980 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2981 verifyFormat("{\n { a #c; }\n}"); 2982 } 2983 2984 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2985 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2986 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2987 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2988 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2989 } 2990 2991 TEST_F(FormatTest, EscapedNewlines) { 2992 EXPECT_EQ( 2993 "#define A \\\n int i; \\\n int j;", 2994 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2995 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2996 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2997 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 2998 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2999 } 3000 3001 TEST_F(FormatTest, DontCrashOnBlockComments) { 3002 EXPECT_EQ( 3003 "int xxxxxxxxx; /* " 3004 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3005 "zzzzzz\n" 3006 "0*/", 3007 format("int xxxxxxxxx; /* " 3008 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3009 "0*/")); 3010 } 3011 3012 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3013 verifyFormat("#define A \\\n" 3014 " int v( \\\n" 3015 " a); \\\n" 3016 " int i;", 3017 getLLVMStyleWithColumns(11)); 3018 } 3019 3020 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3021 EXPECT_EQ( 3022 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3023 " \\\n" 3024 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3025 "\n" 3026 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3027 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3028 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3029 "\\\n" 3030 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3031 " \n" 3032 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3033 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3034 } 3035 3036 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3037 EXPECT_EQ("int\n" 3038 "#define A\n" 3039 " a;", 3040 format("int\n#define A\na;")); 3041 verifyFormat("functionCallTo(\n" 3042 " someOtherFunction(\n" 3043 " withSomeParameters, whichInSequence,\n" 3044 " areLongerThanALine(andAnotherCall,\n" 3045 "#define A B\n" 3046 " withMoreParamters,\n" 3047 " whichStronglyInfluenceTheLayout),\n" 3048 " andMoreParameters),\n" 3049 " trailing);", 3050 getLLVMStyleWithColumns(69)); 3051 verifyFormat("Foo::Foo()\n" 3052 "#ifdef BAR\n" 3053 " : baz(0)\n" 3054 "#endif\n" 3055 "{\n" 3056 "}"); 3057 verifyFormat("void f() {\n" 3058 " if (true)\n" 3059 "#ifdef A\n" 3060 " f(42);\n" 3061 " x();\n" 3062 "#else\n" 3063 " g();\n" 3064 " x();\n" 3065 "#endif\n" 3066 "}"); 3067 verifyFormat("void f(param1, param2,\n" 3068 " param3,\n" 3069 "#ifdef A\n" 3070 " param4(param5,\n" 3071 "#ifdef A1\n" 3072 " param6,\n" 3073 "#ifdef A2\n" 3074 " param7),\n" 3075 "#else\n" 3076 " param8),\n" 3077 " param9,\n" 3078 "#endif\n" 3079 " param10,\n" 3080 "#endif\n" 3081 " param11)\n" 3082 "#else\n" 3083 " param12)\n" 3084 "#endif\n" 3085 "{\n" 3086 " x();\n" 3087 "}", 3088 getLLVMStyleWithColumns(28)); 3089 verifyFormat("#if 1\n" 3090 "int i;"); 3091 verifyFormat("#if 1\n" 3092 "#endif\n" 3093 "#if 1\n" 3094 "#else\n" 3095 "#endif\n"); 3096 verifyFormat("DEBUG({\n" 3097 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3099 "});\n" 3100 "#if a\n" 3101 "#else\n" 3102 "#endif"); 3103 3104 verifyIncompleteFormat("void f(\n" 3105 "#if A\n" 3106 " );\n" 3107 "#else\n" 3108 "#endif"); 3109 } 3110 3111 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3112 verifyFormat("#endif\n" 3113 "#if B"); 3114 } 3115 3116 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3117 FormatStyle SingleLine = getLLVMStyle(); 3118 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3119 verifyFormat("#if 0\n" 3120 "#elif 1\n" 3121 "#endif\n" 3122 "void foo() {\n" 3123 " if (test) foo2();\n" 3124 "}", 3125 SingleLine); 3126 } 3127 3128 TEST_F(FormatTest, LayoutBlockInsideParens) { 3129 verifyFormat("functionCall({ int i; });"); 3130 verifyFormat("functionCall({\n" 3131 " int i;\n" 3132 " int j;\n" 3133 "});"); 3134 verifyFormat("functionCall(\n" 3135 " {\n" 3136 " int i;\n" 3137 " int j;\n" 3138 " },\n" 3139 " aaaa, bbbb, cccc);"); 3140 verifyFormat("functionA(functionB({\n" 3141 " int i;\n" 3142 " int j;\n" 3143 " }),\n" 3144 " aaaa, bbbb, cccc);"); 3145 verifyFormat("functionCall(\n" 3146 " {\n" 3147 " int i;\n" 3148 " int j;\n" 3149 " },\n" 3150 " aaaa, bbbb, // comment\n" 3151 " cccc);"); 3152 verifyFormat("functionA(functionB({\n" 3153 " int i;\n" 3154 " int j;\n" 3155 " }),\n" 3156 " aaaa, bbbb, // comment\n" 3157 " cccc);"); 3158 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3159 verifyFormat("functionCall(aaaa, bbbb, {\n" 3160 " int i;\n" 3161 " int j;\n" 3162 "});"); 3163 verifyFormat( 3164 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3165 " {\n" 3166 " int i; // break\n" 3167 " },\n" 3168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3169 " ccccccccccccccccc));"); 3170 verifyFormat("DEBUG({\n" 3171 " if (a)\n" 3172 " f();\n" 3173 "});"); 3174 } 3175 3176 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3177 EXPECT_EQ("SOME_MACRO { int i; }\n" 3178 "int i;", 3179 format(" SOME_MACRO {int i;} int i;")); 3180 } 3181 3182 TEST_F(FormatTest, LayoutNestedBlocks) { 3183 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3184 " struct s {\n" 3185 " int i;\n" 3186 " };\n" 3187 " s kBitsToOs[] = {{10}};\n" 3188 " for (int i = 0; i < 10; ++i)\n" 3189 " return;\n" 3190 "}"); 3191 verifyFormat("call(parameter, {\n" 3192 " something();\n" 3193 " // Comment using all columns.\n" 3194 " somethingelse();\n" 3195 "});", 3196 getLLVMStyleWithColumns(40)); 3197 verifyFormat("DEBUG( //\n" 3198 " { f(); }, a);"); 3199 verifyFormat("DEBUG( //\n" 3200 " {\n" 3201 " f(); //\n" 3202 " },\n" 3203 " a);"); 3204 3205 EXPECT_EQ("call(parameter, {\n" 3206 " something();\n" 3207 " // Comment too\n" 3208 " // looooooooooong.\n" 3209 " somethingElse();\n" 3210 "});", 3211 format("call(parameter, {\n" 3212 " something();\n" 3213 " // Comment too looooooooooong.\n" 3214 " somethingElse();\n" 3215 "});", 3216 getLLVMStyleWithColumns(29))); 3217 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3218 EXPECT_EQ("DEBUG({ // comment\n" 3219 " int i;\n" 3220 "});", 3221 format("DEBUG({ // comment\n" 3222 "int i;\n" 3223 "});")); 3224 EXPECT_EQ("DEBUG({\n" 3225 " int i;\n" 3226 "\n" 3227 " // comment\n" 3228 " int j;\n" 3229 "});", 3230 format("DEBUG({\n" 3231 " int i;\n" 3232 "\n" 3233 " // comment\n" 3234 " int j;\n" 3235 "});")); 3236 3237 verifyFormat("DEBUG({\n" 3238 " if (a)\n" 3239 " return;\n" 3240 "});"); 3241 verifyGoogleFormat("DEBUG({\n" 3242 " if (a) return;\n" 3243 "});"); 3244 FormatStyle Style = getGoogleStyle(); 3245 Style.ColumnLimit = 45; 3246 verifyFormat("Debug(aaaaa,\n" 3247 " {\n" 3248 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3249 " },\n" 3250 " a);", 3251 Style); 3252 3253 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3254 3255 verifyNoCrash("^{v^{a}}"); 3256 } 3257 3258 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3259 EXPECT_EQ("#define MACRO() \\\n" 3260 " Debug(aaa, /* force line break */ \\\n" 3261 " { \\\n" 3262 " int i; \\\n" 3263 " int j; \\\n" 3264 " })", 3265 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3266 " { int i; int j; })", 3267 getGoogleStyle())); 3268 3269 EXPECT_EQ("#define A \\\n" 3270 " [] { \\\n" 3271 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3272 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3273 " }", 3274 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3275 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3276 getGoogleStyle())); 3277 } 3278 3279 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3280 EXPECT_EQ("{}", format("{}")); 3281 verifyFormat("enum E {};"); 3282 verifyFormat("enum E {}"); 3283 } 3284 3285 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3286 FormatStyle Style = getLLVMStyle(); 3287 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3288 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3289 verifyFormat("FOO_BEGIN\n" 3290 " FOO_ENTRY\n" 3291 "FOO_END", Style); 3292 verifyFormat("FOO_BEGIN\n" 3293 " NESTED_FOO_BEGIN\n" 3294 " NESTED_FOO_ENTRY\n" 3295 " NESTED_FOO_END\n" 3296 "FOO_END", Style); 3297 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3298 " int x;\n" 3299 " x = 1;\n" 3300 "FOO_END(Baz)", Style); 3301 } 3302 3303 //===----------------------------------------------------------------------===// 3304 // Line break tests. 3305 //===----------------------------------------------------------------------===// 3306 3307 TEST_F(FormatTest, PreventConfusingIndents) { 3308 verifyFormat( 3309 "void f() {\n" 3310 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3311 " parameter, parameter, parameter)),\n" 3312 " SecondLongCall(parameter));\n" 3313 "}"); 3314 verifyFormat( 3315 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3316 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3318 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 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("int a = bbbb && ccc && fffff(\n" 3330 "#define A Just forcing a new line\n" 3331 " ddd);"); 3332 } 3333 3334 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3335 verifyFormat( 3336 "bool aaaaaaa =\n" 3337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3338 " bbbbbbbb();"); 3339 verifyFormat( 3340 "bool aaaaaaa =\n" 3341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3342 " bbbbbbbb();"); 3343 3344 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3346 " ccccccccc == ddddddddddd;"); 3347 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3349 " ccccccccc == ddddddddddd;"); 3350 verifyFormat( 3351 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3353 " ccccccccc == ddddddddddd;"); 3354 3355 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3356 " aaaaaa) &&\n" 3357 " bbbbbb && cccccc;"); 3358 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3359 " aaaaaa) >>\n" 3360 " bbbbbb;"); 3361 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3362 " SourceMgr.getSpellingColumnNumber(\n" 3363 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3364 " 1);"); 3365 3366 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3367 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3368 " cccccc) {\n}"); 3369 verifyFormat("b = a &&\n" 3370 " // Comment\n" 3371 " b.c && d;"); 3372 3373 // If the LHS of a comparison is not a binary expression itself, the 3374 // additional linebreak confuses many people. 3375 verifyFormat( 3376 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3378 "}"); 3379 verifyFormat( 3380 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3382 "}"); 3383 verifyFormat( 3384 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3386 "}"); 3387 // Even explicit parentheses stress the precedence enough to make the 3388 // additional break unnecessary. 3389 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3391 "}"); 3392 // This cases is borderline, but with the indentation it is still readable. 3393 verifyFormat( 3394 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3395 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3397 "}", 3398 getLLVMStyleWithColumns(75)); 3399 3400 // If the LHS is a binary expression, we should still use the additional break 3401 // as otherwise the formatting hides the operator precedence. 3402 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3404 " 5) {\n" 3405 "}"); 3406 3407 FormatStyle OnePerLine = getLLVMStyle(); 3408 OnePerLine.BinPackParameters = false; 3409 verifyFormat( 3410 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3413 OnePerLine); 3414 } 3415 3416 TEST_F(FormatTest, ExpressionIndentation) { 3417 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3421 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3422 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3425 " ccccccccccccccccccccccccccccccccccccccccc;"); 3426 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3429 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3430 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3433 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3434 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3437 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3438 verifyFormat("if () {\n" 3439 "} else if (aaaaa &&\n" 3440 " bbbbb > // break\n" 3441 " ccccc) {\n" 3442 "}"); 3443 3444 // Presence of a trailing comment used to change indentation of b. 3445 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3446 " b;\n" 3447 "return aaaaaaaaaaaaaaaaaaa +\n" 3448 " b; //", 3449 getLLVMStyleWithColumns(30)); 3450 } 3451 3452 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3453 // Not sure what the best system is here. Like this, the LHS can be found 3454 // immediately above an operator (everything with the same or a higher 3455 // indent). The RHS is aligned right of the operator and so compasses 3456 // everything until something with the same indent as the operator is found. 3457 // FIXME: Is this a good system? 3458 FormatStyle Style = getLLVMStyle(); 3459 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3460 verifyFormat( 3461 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3462 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3463 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3464 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3465 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3466 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3467 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3468 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3469 " > ccccccccccccccccccccccccccccccccccccccccc;", 3470 Style); 3471 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3472 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3473 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3474 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 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 () {\n" 3487 "} else if (aaaaa\n" 3488 " && bbbbb // break\n" 3489 " > ccccc) {\n" 3490 "}", 3491 Style); 3492 verifyFormat("return (a)\n" 3493 " // comment\n" 3494 " + b;", 3495 Style); 3496 verifyFormat( 3497 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3498 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3499 " + cc;", 3500 Style); 3501 3502 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3503 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3504 Style); 3505 3506 // Forced by comments. 3507 verifyFormat( 3508 "unsigned ContentSize =\n" 3509 " sizeof(int16_t) // DWARF ARange version number\n" 3510 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3511 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3512 " + sizeof(int8_t); // Segment Size (in bytes)"); 3513 3514 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3515 " == boost::fusion::at_c<1>(iiii).second;", 3516 Style); 3517 3518 Style.ColumnLimit = 60; 3519 verifyFormat("zzzzzzzzzz\n" 3520 " = bbbbbbbbbbbbbbbbb\n" 3521 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3522 Style); 3523 } 3524 3525 TEST_F(FormatTest, NoOperandAlignment) { 3526 FormatStyle Style = getLLVMStyle(); 3527 Style.AlignOperands = false; 3528 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3529 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3530 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3531 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3532 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3533 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3534 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3535 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3537 " > ccccccccccccccccccccccccccccccccccccccccc;", 3538 Style); 3539 3540 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3541 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3542 " + cc;", 3543 Style); 3544 verifyFormat("int a = aa\n" 3545 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3546 " * cccccccccccccccccccccccccccccccccccc;", 3547 Style); 3548 3549 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3550 verifyFormat("return (a > b\n" 3551 " // comment1\n" 3552 " // comment2\n" 3553 " || c);", 3554 Style); 3555 } 3556 3557 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3558 FormatStyle Style = getLLVMStyle(); 3559 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3560 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3562 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3563 Style); 3564 } 3565 3566 TEST_F(FormatTest, ConstructorInitializers) { 3567 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3568 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3569 getLLVMStyleWithColumns(45)); 3570 verifyFormat("Constructor()\n" 3571 " : Inttializer(FitsOnTheLine) {}", 3572 getLLVMStyleWithColumns(44)); 3573 verifyFormat("Constructor()\n" 3574 " : Inttializer(FitsOnTheLine) {}", 3575 getLLVMStyleWithColumns(43)); 3576 3577 verifyFormat("template <typename T>\n" 3578 "Constructor() : Initializer(FitsOnTheLine) {}", 3579 getLLVMStyleWithColumns(45)); 3580 3581 verifyFormat( 3582 "SomeClass::Constructor()\n" 3583 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3584 3585 verifyFormat( 3586 "SomeClass::Constructor()\n" 3587 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3588 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3589 verifyFormat( 3590 "SomeClass::Constructor()\n" 3591 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3592 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3593 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3594 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3595 " : aaaaaaaaaa(aaaaaa) {}"); 3596 3597 verifyFormat("Constructor()\n" 3598 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3599 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3600 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3601 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3602 3603 verifyFormat("Constructor()\n" 3604 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3606 3607 verifyFormat("Constructor(int Parameter = 0)\n" 3608 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3609 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3610 verifyFormat("Constructor()\n" 3611 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3612 "}", 3613 getLLVMStyleWithColumns(60)); 3614 verifyFormat("Constructor()\n" 3615 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3616 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3617 3618 // Here a line could be saved by splitting the second initializer onto two 3619 // lines, but that is not desirable. 3620 verifyFormat("Constructor()\n" 3621 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3622 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3623 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3624 3625 FormatStyle OnePerLine = getLLVMStyle(); 3626 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3627 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3628 verifyFormat("SomeClass::Constructor()\n" 3629 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3630 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3631 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3632 OnePerLine); 3633 verifyFormat("SomeClass::Constructor()\n" 3634 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3635 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3636 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3637 OnePerLine); 3638 verifyFormat("MyClass::MyClass(int var)\n" 3639 " : some_var_(var), // 4 space indent\n" 3640 " some_other_var_(var + 1) { // lined up\n" 3641 "}", 3642 OnePerLine); 3643 verifyFormat("Constructor()\n" 3644 " : aaaaa(aaaaaa),\n" 3645 " aaaaa(aaaaaa),\n" 3646 " aaaaa(aaaaaa),\n" 3647 " aaaaa(aaaaaa),\n" 3648 " aaaaa(aaaaaa) {}", 3649 OnePerLine); 3650 verifyFormat("Constructor()\n" 3651 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3652 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3653 OnePerLine); 3654 OnePerLine.BinPackParameters = false; 3655 verifyFormat( 3656 "Constructor()\n" 3657 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3658 " aaaaaaaaaaa().aaa(),\n" 3659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3660 OnePerLine); 3661 OnePerLine.ColumnLimit = 60; 3662 verifyFormat("Constructor()\n" 3663 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3664 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3665 OnePerLine); 3666 3667 EXPECT_EQ("Constructor()\n" 3668 " : // Comment forcing unwanted break.\n" 3669 " aaaa(aaaa) {}", 3670 format("Constructor() :\n" 3671 " // Comment forcing unwanted break.\n" 3672 " aaaa(aaaa) {}")); 3673 } 3674 3675 TEST_F(FormatTest, MemoizationTests) { 3676 // This breaks if the memoization lookup does not take \c Indent and 3677 // \c LastSpace into account. 3678 verifyFormat( 3679 "extern CFRunLoopTimerRef\n" 3680 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3681 " CFTimeInterval interval, CFOptionFlags flags,\n" 3682 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3683 " CFRunLoopTimerContext *context) {}"); 3684 3685 // Deep nesting somewhat works around our memoization. 3686 verifyFormat( 3687 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3688 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3689 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3690 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3691 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3692 getLLVMStyleWithColumns(65)); 3693 verifyFormat( 3694 "aaaaa(\n" 3695 " aaaaa,\n" 3696 " aaaaa(\n" 3697 " aaaaa,\n" 3698 " aaaaa(\n" 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))))))))))));", 3719 getLLVMStyleWithColumns(65)); 3720 verifyFormat( 3721 "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" 3722 " a),\n" 3723 " a),\n" 3724 " a),\n" 3725 " a),\n" 3726 " 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)", 3739 getLLVMStyleWithColumns(65)); 3740 3741 // This test takes VERY long when memoization is broken. 3742 FormatStyle OnePerLine = getLLVMStyle(); 3743 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3744 OnePerLine.BinPackParameters = false; 3745 std::string input = "Constructor()\n" 3746 " : aaaa(a,\n"; 3747 for (unsigned i = 0, e = 80; i != e; ++i) { 3748 input += " a,\n"; 3749 } 3750 input += " a) {}"; 3751 verifyFormat(input, OnePerLine); 3752 } 3753 3754 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3755 verifyFormat( 3756 "void f() {\n" 3757 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3758 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3759 " f();\n" 3760 "}"); 3761 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3762 " Intervals[i - 1].getRange().getLast()) {\n}"); 3763 } 3764 3765 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3766 // Principially, we break function declarations in a certain order: 3767 // 1) break amongst arguments. 3768 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3769 " Cccccccccccccc cccccccccccccc);"); 3770 verifyFormat("template <class TemplateIt>\n" 3771 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3772 " TemplateIt *stop) {}"); 3773 3774 // 2) break after return type. 3775 verifyFormat( 3776 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3777 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3778 getGoogleStyle()); 3779 3780 // 3) break after (. 3781 verifyFormat( 3782 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3783 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3784 getGoogleStyle()); 3785 3786 // 4) break before after nested name specifiers. 3787 verifyFormat( 3788 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3789 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3790 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3791 getGoogleStyle()); 3792 3793 // However, there are exceptions, if a sufficient amount of lines can be 3794 // saved. 3795 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3796 // more adjusting. 3797 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3798 " Cccccccccccccc cccccccccc,\n" 3799 " Cccccccccccccc cccccccccc,\n" 3800 " Cccccccccccccc cccccccccc,\n" 3801 " Cccccccccccccc cccccccccc);"); 3802 verifyFormat( 3803 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3804 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3805 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3806 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3807 getGoogleStyle()); 3808 verifyFormat( 3809 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3810 " Cccccccccccccc cccccccccc,\n" 3811 " Cccccccccccccc cccccccccc,\n" 3812 " Cccccccccccccc cccccccccc,\n" 3813 " Cccccccccccccc cccccccccc,\n" 3814 " Cccccccccccccc cccccccccc,\n" 3815 " Cccccccccccccc cccccccccc);"); 3816 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3817 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3818 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3819 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3820 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3821 3822 // Break after multi-line parameters. 3823 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3826 " bbbb bbbb);"); 3827 verifyFormat("void SomeLoooooooooooongFunction(\n" 3828 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3829 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3830 " int bbbbbbbbbbbbb);"); 3831 3832 // Treat overloaded operators like other functions. 3833 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3834 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3835 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3836 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3837 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3838 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3839 verifyGoogleFormat( 3840 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3841 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3842 verifyGoogleFormat( 3843 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3844 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3845 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3846 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3847 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3848 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3849 verifyGoogleFormat( 3850 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3851 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3852 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3853 verifyGoogleFormat( 3854 "template <typename T>\n" 3855 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3856 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3857 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3858 3859 FormatStyle Style = getLLVMStyle(); 3860 Style.PointerAlignment = FormatStyle::PAS_Left; 3861 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3862 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3863 Style); 3864 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3866 Style); 3867 } 3868 3869 TEST_F(FormatTest, TrailingReturnType) { 3870 verifyFormat("auto foo() -> int;\n"); 3871 verifyFormat("struct S {\n" 3872 " auto bar() const -> int;\n" 3873 "};"); 3874 verifyFormat("template <size_t Order, typename T>\n" 3875 "auto load_img(const std::string &filename)\n" 3876 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3877 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3878 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3879 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3880 verifyFormat("template <typename T>\n" 3881 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3882 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3883 3884 // Not trailing return types. 3885 verifyFormat("void f() { auto a = b->c(); }"); 3886 } 3887 3888 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3889 // Avoid breaking before trailing 'const' or other trailing annotations, if 3890 // they are not function-like. 3891 FormatStyle Style = getGoogleStyle(); 3892 Style.ColumnLimit = 47; 3893 verifyFormat("void someLongFunction(\n" 3894 " int someLoooooooooooooongParameter) const {\n}", 3895 getLLVMStyleWithColumns(47)); 3896 verifyFormat("LoooooongReturnType\n" 3897 "someLoooooooongFunction() const {}", 3898 getLLVMStyleWithColumns(47)); 3899 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3900 " const {}", 3901 Style); 3902 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3903 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3904 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3905 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3906 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3907 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3908 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3909 " aaaaaaaaaaa aaaaa) const override;"); 3910 verifyGoogleFormat( 3911 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3912 " const override;"); 3913 3914 // Even if the first parameter has to be wrapped. 3915 verifyFormat("void someLongFunction(\n" 3916 " int someLongParameter) const {}", 3917 getLLVMStyleWithColumns(46)); 3918 verifyFormat("void someLongFunction(\n" 3919 " int someLongParameter) const {}", 3920 Style); 3921 verifyFormat("void someLongFunction(\n" 3922 " int someLongParameter) override {}", 3923 Style); 3924 verifyFormat("void someLongFunction(\n" 3925 " int someLongParameter) OVERRIDE {}", 3926 Style); 3927 verifyFormat("void someLongFunction(\n" 3928 " int someLongParameter) final {}", 3929 Style); 3930 verifyFormat("void someLongFunction(\n" 3931 " int someLongParameter) FINAL {}", 3932 Style); 3933 verifyFormat("void someLongFunction(\n" 3934 " int parameter) const override {}", 3935 Style); 3936 3937 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3938 verifyFormat("void someLongFunction(\n" 3939 " int someLongParameter) const\n" 3940 "{\n" 3941 "}", 3942 Style); 3943 3944 // Unless these are unknown annotations. 3945 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3946 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3947 " LONG_AND_UGLY_ANNOTATION;"); 3948 3949 // Breaking before function-like trailing annotations is fine to keep them 3950 // close to their arguments. 3951 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3952 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3953 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3954 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3955 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3956 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3957 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3958 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3959 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3960 3961 verifyFormat( 3962 "void aaaaaaaaaaaaaaaaaa()\n" 3963 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3964 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3965 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3966 " __attribute__((unused));"); 3967 verifyGoogleFormat( 3968 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3969 " GUARDED_BY(aaaaaaaaaaaa);"); 3970 verifyGoogleFormat( 3971 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3972 " GUARDED_BY(aaaaaaaaaaaa);"); 3973 verifyGoogleFormat( 3974 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3975 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3976 verifyGoogleFormat( 3977 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3978 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3979 } 3980 3981 TEST_F(FormatTest, FunctionAnnotations) { 3982 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3983 "int OldFunction(const string ¶meter) {}"); 3984 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3985 "string OldFunction(const string ¶meter) {}"); 3986 verifyFormat("template <typename T>\n" 3987 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3988 "string OldFunction(const string ¶meter) {}"); 3989 3990 // Not function annotations. 3991 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3992 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3993 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3994 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3995 } 3996 3997 TEST_F(FormatTest, BreaksDesireably) { 3998 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3999 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4000 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4001 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4003 "}"); 4004 4005 verifyFormat( 4006 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4008 4009 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4012 4013 verifyFormat( 4014 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4016 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4018 4019 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4020 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4021 4022 verifyFormat( 4023 "void f() {\n" 4024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4026 "}"); 4027 verifyFormat( 4028 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4030 verifyFormat( 4031 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4033 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4036 4037 // Indent consistently independent of call expression and unary operator. 4038 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4039 " dddddddddddddddddddddddddddddd));"); 4040 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4041 " dddddddddddddddddddddddddddddd));"); 4042 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4043 " dddddddddddddddddddddddddddddd));"); 4044 4045 // This test case breaks on an incorrect memoization, i.e. an optimization not 4046 // taking into account the StopAt value. 4047 verifyFormat( 4048 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4049 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4050 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4051 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4052 4053 verifyFormat("{\n {\n {\n" 4054 " Annotation.SpaceRequiredBefore =\n" 4055 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4056 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4057 " }\n }\n}"); 4058 4059 // Break on an outer level if there was a break on an inner level. 4060 EXPECT_EQ("f(g(h(a, // comment\n" 4061 " b, c),\n" 4062 " d, e),\n" 4063 " x, y);", 4064 format("f(g(h(a, // comment\n" 4065 " b, c), d, e), x, y);")); 4066 4067 // Prefer breaking similar line breaks. 4068 verifyFormat( 4069 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4070 " NSTrackingMouseEnteredAndExited |\n" 4071 " NSTrackingActiveAlways;"); 4072 } 4073 4074 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4075 FormatStyle NoBinPacking = getGoogleStyle(); 4076 NoBinPacking.BinPackParameters = false; 4077 NoBinPacking.BinPackArguments = true; 4078 verifyFormat("void f() {\n" 4079 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4081 "}", 4082 NoBinPacking); 4083 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4084 " int aaaaaaaaaaaaaaaaaaaa,\n" 4085 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4086 NoBinPacking); 4087 4088 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4089 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4090 " vector<int> bbbbbbbbbbbbbbb);", 4091 NoBinPacking); 4092 // FIXME: This behavior difference is probably not wanted. However, currently 4093 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4094 // template arguments from BreakBeforeParameter being set because of the 4095 // one-per-line formatting. 4096 verifyFormat( 4097 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4098 " aaaaaaaaaa> aaaaaaaaaa);", 4099 NoBinPacking); 4100 verifyFormat( 4101 "void fffffffffff(\n" 4102 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4103 " aaaaaaaaaa);"); 4104 } 4105 4106 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4107 FormatStyle NoBinPacking = getGoogleStyle(); 4108 NoBinPacking.BinPackParameters = false; 4109 NoBinPacking.BinPackArguments = false; 4110 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4111 " aaaaaaaaaaaaaaaaaaaa,\n" 4112 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4113 NoBinPacking); 4114 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4115 " aaaaaaaaaaaaa,\n" 4116 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4117 NoBinPacking); 4118 verifyFormat( 4119 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4120 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4122 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4124 NoBinPacking); 4125 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4126 " .aaaaaaaaaaaaaaaaaa();", 4127 NoBinPacking); 4128 verifyFormat("void f() {\n" 4129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4130 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4131 "}", 4132 NoBinPacking); 4133 4134 verifyFormat( 4135 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4136 " aaaaaaaaaaaa,\n" 4137 " aaaaaaaaaaaa);", 4138 NoBinPacking); 4139 verifyFormat( 4140 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4141 " ddddddddddddddddddddddddddddd),\n" 4142 " test);", 4143 NoBinPacking); 4144 4145 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4146 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4147 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4148 " aaaaaaaaaaaaaaaaaa;", 4149 NoBinPacking); 4150 verifyFormat("a(\"a\"\n" 4151 " \"a\",\n" 4152 " a);"); 4153 4154 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4155 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4156 " aaaaaaaaa,\n" 4157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4158 NoBinPacking); 4159 verifyFormat( 4160 "void f() {\n" 4161 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4162 " .aaaaaaa();\n" 4163 "}", 4164 NoBinPacking); 4165 verifyFormat( 4166 "template <class SomeType, class SomeOtherType>\n" 4167 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4168 NoBinPacking); 4169 } 4170 4171 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4172 FormatStyle Style = getLLVMStyleWithColumns(15); 4173 Style.ExperimentalAutoDetectBinPacking = true; 4174 EXPECT_EQ("aaa(aaaa,\n" 4175 " aaaa,\n" 4176 " aaaa);\n" 4177 "aaa(aaaa,\n" 4178 " aaaa,\n" 4179 " aaaa);", 4180 format("aaa(aaaa,\n" // one-per-line 4181 " aaaa,\n" 4182 " aaaa );\n" 4183 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4184 Style)); 4185 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4186 " aaaa);\n" 4187 "aaa(aaaa, aaaa,\n" 4188 " aaaa);", 4189 format("aaa(aaaa, aaaa,\n" // bin-packed 4190 " aaaa );\n" 4191 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4192 Style)); 4193 } 4194 4195 TEST_F(FormatTest, FormatsBuilderPattern) { 4196 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4197 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4198 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4199 " .StartsWith(\".init\", ORDER_INIT)\n" 4200 " .StartsWith(\".fini\", ORDER_FINI)\n" 4201 " .StartsWith(\".hash\", ORDER_HASH)\n" 4202 " .Default(ORDER_TEXT);\n"); 4203 4204 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4205 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4206 verifyFormat( 4207 "aaaaaaa->aaaaaaa\n" 4208 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4210 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4211 verifyFormat( 4212 "aaaaaaa->aaaaaaa\n" 4213 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4214 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4215 verifyFormat( 4216 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4217 " aaaaaaaaaaaaaa);"); 4218 verifyFormat( 4219 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4220 " aaaaaa->aaaaaaaaaaaa()\n" 4221 " ->aaaaaaaaaaaaaaaa(\n" 4222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4223 " ->aaaaaaaaaaaaaaaaa();"); 4224 verifyGoogleFormat( 4225 "void f() {\n" 4226 " someo->Add((new util::filetools::Handler(dir))\n" 4227 " ->OnEvent1(NewPermanentCallback(\n" 4228 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4229 " ->OnEvent2(NewPermanentCallback(\n" 4230 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4231 " ->OnEvent3(NewPermanentCallback(\n" 4232 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4233 " ->OnEvent5(NewPermanentCallback(\n" 4234 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4235 " ->OnEvent6(NewPermanentCallback(\n" 4236 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4237 "}"); 4238 4239 verifyFormat( 4240 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4241 verifyFormat("aaaaaaaaaaaaaaa()\n" 4242 " .aaaaaaaaaaaaaaa()\n" 4243 " .aaaaaaaaaaaaaaa()\n" 4244 " .aaaaaaaaaaaaaaa()\n" 4245 " .aaaaaaaaaaaaaaa();"); 4246 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4247 " .aaaaaaaaaaaaaaa()\n" 4248 " .aaaaaaaaaaaaaaa()\n" 4249 " .aaaaaaaaaaaaaaa();"); 4250 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4251 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4252 " .aaaaaaaaaaaaaaa();"); 4253 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4254 " ->aaaaaaaaaaaaaae(0)\n" 4255 " ->aaaaaaaaaaaaaaa();"); 4256 4257 // Don't linewrap after very short segments. 4258 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4259 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4260 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4261 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4262 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4263 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4264 verifyFormat("aaa()\n" 4265 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4266 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4267 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4268 4269 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4270 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4271 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4272 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4273 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4275 4276 // Prefer not to break after empty parentheses. 4277 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4278 " First->LastNewlineOffset);"); 4279 4280 // Prefer not to create "hanging" indents. 4281 verifyFormat( 4282 "return !soooooooooooooome_map\n" 4283 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4284 " .second;"); 4285 verifyFormat( 4286 "return aaaaaaaaaaaaaaaa\n" 4287 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4288 " .aaaa(aaaaaaaaaaaaaa);"); 4289 // No hanging indent here. 4290 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4292 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4294 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4295 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4296 getLLVMStyleWithColumns(60)); 4297 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4298 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4299 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4300 getLLVMStyleWithColumns(59)); 4301 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4303 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4304 } 4305 4306 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4307 verifyFormat( 4308 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4309 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4310 verifyFormat( 4311 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4312 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4313 4314 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4315 " ccccccccccccccccccccccccc) {\n}"); 4316 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4317 " ccccccccccccccccccccccccc) {\n}"); 4318 4319 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4320 " ccccccccccccccccccccccccc) {\n}"); 4321 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4322 " ccccccccccccccccccccccccc) {\n}"); 4323 4324 verifyFormat( 4325 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4326 " ccccccccccccccccccccccccc) {\n}"); 4327 verifyFormat( 4328 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4329 " ccccccccccccccccccccccccc) {\n}"); 4330 4331 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4332 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4333 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4334 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4335 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4336 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4337 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4338 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4339 4340 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4341 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4342 " aaaaaaaaaaaaaaa != aa) {\n}"); 4343 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4344 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4345 " aaaaaaaaaaaaaaa != aa) {\n}"); 4346 } 4347 4348 TEST_F(FormatTest, BreaksAfterAssignments) { 4349 verifyFormat( 4350 "unsigned Cost =\n" 4351 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4352 " SI->getPointerAddressSpaceee());\n"); 4353 verifyFormat( 4354 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4355 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4356 4357 verifyFormat( 4358 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4359 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4360 verifyFormat("unsigned OriginalStartColumn =\n" 4361 " SourceMgr.getSpellingColumnNumber(\n" 4362 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4363 " 1;"); 4364 } 4365 4366 TEST_F(FormatTest, AlignsAfterAssignments) { 4367 verifyFormat( 4368 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4369 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4370 verifyFormat( 4371 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4372 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4373 verifyFormat( 4374 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4375 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4376 verifyFormat( 4377 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4378 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4379 verifyFormat( 4380 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4381 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4382 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4383 } 4384 4385 TEST_F(FormatTest, AlignsAfterReturn) { 4386 verifyFormat( 4387 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4388 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4389 verifyFormat( 4390 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4391 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4392 verifyFormat( 4393 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4394 " aaaaaaaaaaaaaaaaaaaaaa();"); 4395 verifyFormat( 4396 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4397 " aaaaaaaaaaaaaaaaaaaaaa());"); 4398 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4400 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4403 verifyFormat("return\n" 4404 " // true if code is one of a or b.\n" 4405 " code == a || code == b;"); 4406 } 4407 4408 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4409 verifyFormat( 4410 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4411 " aaaaaaaaa aaaaaaa) {}"); 4412 verifyFormat( 4413 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4414 " aaaaaaaaaaa aaaaaaaaa);"); 4415 verifyFormat( 4416 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4417 " aaaaaaaaaaaaaaaaaaaaa));"); 4418 FormatStyle Style = getLLVMStyle(); 4419 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4420 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4421 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4422 Style); 4423 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4424 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4425 Style); 4426 verifyFormat("SomeLongVariableName->someFunction(\n" 4427 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4428 Style); 4429 verifyFormat( 4430 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4431 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4432 Style); 4433 verifyFormat( 4434 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4435 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4436 Style); 4437 verifyFormat( 4438 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4439 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4440 Style); 4441 4442 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4443 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4444 " b));", 4445 Style); 4446 4447 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4448 Style.BinPackArguments = false; 4449 Style.BinPackParameters = false; 4450 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4451 " aaaaaaaaaaa aaaaaaaa,\n" 4452 " aaaaaaaaa aaaaaaa,\n" 4453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4454 Style); 4455 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4456 " aaaaaaaaaaa aaaaaaaaa,\n" 4457 " aaaaaaaaaaa aaaaaaaaa,\n" 4458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4459 Style); 4460 verifyFormat("SomeLongVariableName->someFunction(\n" 4461 " foooooooo(\n" 4462 " aaaaaaaaaaaaaaa,\n" 4463 " aaaaaaaaaaaaaaaaaaaaa,\n" 4464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4465 Style); 4466 } 4467 4468 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4469 FormatStyle Style = getLLVMStyleWithColumns(40); 4470 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4471 " bbbbbbbbbbbbbbbbbbbbbb);", 4472 Style); 4473 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4474 Style.AlignOperands = false; 4475 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4476 " bbbbbbbbbbbbbbbbbbbbbb);", 4477 Style); 4478 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4479 Style.AlignOperands = true; 4480 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4481 " bbbbbbbbbbbbbbbbbbbbbb);", 4482 Style); 4483 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4484 Style.AlignOperands = false; 4485 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4486 " bbbbbbbbbbbbbbbbbbbbbb);", 4487 Style); 4488 } 4489 4490 TEST_F(FormatTest, BreaksConditionalExpressions) { 4491 verifyFormat( 4492 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4493 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4494 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4495 verifyFormat( 4496 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4497 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4498 verifyFormat( 4499 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4500 " : aaaaaaaaaaaaa);"); 4501 verifyFormat( 4502 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4503 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4504 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4505 " aaaaaaaaaaaaa);"); 4506 verifyFormat( 4507 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4508 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4509 " aaaaaaaaaaaaa);"); 4510 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4511 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4513 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4515 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4517 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4519 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4521 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4522 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4524 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4526 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4527 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4528 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4529 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4530 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4532 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4533 " : aaaaaaaaaaaaaaaa;"); 4534 verifyFormat( 4535 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4536 " ? aaaaaaaaaaaaaaa\n" 4537 " : aaaaaaaaaaaaaaa;"); 4538 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4539 " aaaaaaaaa\n" 4540 " ? b\n" 4541 " : c);"); 4542 verifyFormat("return aaaa == bbbb\n" 4543 " // comment\n" 4544 " ? aaaa\n" 4545 " : bbbb;"); 4546 verifyFormat("unsigned Indent =\n" 4547 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4548 " ? IndentForLevel[TheLine.Level]\n" 4549 " : TheLine * 2,\n" 4550 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4551 getLLVMStyleWithColumns(70)); 4552 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4553 " ? aaaaaaaaaaaaaaa\n" 4554 " : bbbbbbbbbbbbbbb //\n" 4555 " ? ccccccccccccccc\n" 4556 " : ddddddddddddddd;"); 4557 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4558 " ? aaaaaaaaaaaaaaa\n" 4559 " : (bbbbbbbbbbbbbbb //\n" 4560 " ? ccccccccccccccc\n" 4561 " : ddddddddddddddd);"); 4562 verifyFormat( 4563 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4564 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4565 " aaaaaaaaaaaaaaaaaaaaa +\n" 4566 " aaaaaaaaaaaaaaaaaaaaa\n" 4567 " : aaaaaaaaaa;"); 4568 verifyFormat( 4569 "aaaaaa = aaaaaaaaaaaa\n" 4570 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4571 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4572 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4573 4574 FormatStyle NoBinPacking = getLLVMStyle(); 4575 NoBinPacking.BinPackArguments = false; 4576 verifyFormat( 4577 "void f() {\n" 4578 " g(aaa,\n" 4579 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4581 " ? aaaaaaaaaaaaaaa\n" 4582 " : aaaaaaaaaaaaaaa);\n" 4583 "}", 4584 NoBinPacking); 4585 verifyFormat( 4586 "void f() {\n" 4587 " g(aaa,\n" 4588 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4590 " ?: aaaaaaaaaaaaaaa);\n" 4591 "}", 4592 NoBinPacking); 4593 4594 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4595 " // comment.\n" 4596 " ccccccccccccccccccccccccccccccccccccccc\n" 4597 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4598 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4599 4600 // Assignments in conditional expressions. Apparently not uncommon :-(. 4601 verifyFormat("return a != b\n" 4602 " // comment\n" 4603 " ? a = b\n" 4604 " : a = b;"); 4605 verifyFormat("return a != b\n" 4606 " // comment\n" 4607 " ? a = a != b\n" 4608 " // comment\n" 4609 " ? a = b\n" 4610 " : a\n" 4611 " : a;\n"); 4612 verifyFormat("return a != b\n" 4613 " // comment\n" 4614 " ? a\n" 4615 " : a = a != b\n" 4616 " // comment\n" 4617 " ? a = b\n" 4618 " : a;"); 4619 } 4620 4621 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4622 FormatStyle Style = getLLVMStyle(); 4623 Style.BreakBeforeTernaryOperators = false; 4624 Style.ColumnLimit = 70; 4625 verifyFormat( 4626 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4629 Style); 4630 verifyFormat( 4631 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4633 Style); 4634 verifyFormat( 4635 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4636 " aaaaaaaaaaaaa);", 4637 Style); 4638 verifyFormat( 4639 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4640 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4642 " aaaaaaaaaaaaa);", 4643 Style); 4644 verifyFormat( 4645 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4646 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4647 " aaaaaaaaaaaaa);", 4648 Style); 4649 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4654 Style); 4655 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4661 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4662 Style); 4663 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4667 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4668 Style); 4669 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4671 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4672 Style); 4673 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4677 Style); 4678 verifyFormat( 4679 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4680 " aaaaaaaaaaaaaaa :\n" 4681 " aaaaaaaaaaaaaaa;", 4682 Style); 4683 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4684 " aaaaaaaaa ?\n" 4685 " b :\n" 4686 " c);", 4687 Style); 4688 verifyFormat( 4689 "unsigned Indent =\n" 4690 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4691 " IndentForLevel[TheLine.Level] :\n" 4692 " TheLine * 2,\n" 4693 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4694 Style); 4695 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4696 " aaaaaaaaaaaaaaa :\n" 4697 " bbbbbbbbbbbbbbb ? //\n" 4698 " ccccccccccccccc :\n" 4699 " ddddddddddddddd;", 4700 Style); 4701 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4702 " aaaaaaaaaaaaaaa :\n" 4703 " (bbbbbbbbbbbbbbb ? //\n" 4704 " ccccccccccccccc :\n" 4705 " ddddddddddddddd);", 4706 Style); 4707 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4708 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4709 " ccccccccccccccccccccccccccc;", 4710 Style); 4711 } 4712 4713 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4714 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4715 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4716 verifyFormat("bool a = true, b = false;"); 4717 4718 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4720 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4721 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4722 verifyFormat( 4723 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4724 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4725 " d = e && f;"); 4726 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4727 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4728 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4729 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4730 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4731 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4732 4733 FormatStyle Style = getGoogleStyle(); 4734 Style.PointerAlignment = FormatStyle::PAS_Left; 4735 Style.DerivePointerAlignment = false; 4736 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4737 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4738 " *b = bbbbbbbbbbbbbbbbbbb;", 4739 Style); 4740 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4741 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4742 Style); 4743 } 4744 4745 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4746 verifyFormat("arr[foo ? bar : baz];"); 4747 verifyFormat("f()[foo ? bar : baz];"); 4748 verifyFormat("(a + b)[foo ? bar : baz];"); 4749 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4750 } 4751 4752 TEST_F(FormatTest, AlignsStringLiterals) { 4753 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4754 " \"short literal\");"); 4755 verifyFormat( 4756 "looooooooooooooooooooooooongFunction(\n" 4757 " \"short literal\"\n" 4758 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4759 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4760 " \" string literals\",\n" 4761 " and, other, parameters);"); 4762 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4763 " \"5678\";", 4764 format("fun + \"1243\" /* comment */\n" 4765 " \"5678\";", 4766 getLLVMStyleWithColumns(28))); 4767 EXPECT_EQ( 4768 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4769 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4770 " \"aaaaaaaaaaaaaaaa\";", 4771 format("aaaaaa =" 4772 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4773 "aaaaaaaaaaaaaaaaaaaaa\" " 4774 "\"aaaaaaaaaaaaaaaa\";")); 4775 verifyFormat("a = a + \"a\"\n" 4776 " \"a\"\n" 4777 " \"a\";"); 4778 verifyFormat("f(\"a\", \"b\"\n" 4779 " \"c\");"); 4780 4781 verifyFormat( 4782 "#define LL_FORMAT \"ll\"\n" 4783 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4784 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4785 4786 verifyFormat("#define A(X) \\\n" 4787 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4788 " \"ccccc\"", 4789 getLLVMStyleWithColumns(23)); 4790 verifyFormat("#define A \"def\"\n" 4791 "f(\"abc\" A \"ghi\"\n" 4792 " \"jkl\");"); 4793 4794 verifyFormat("f(L\"a\"\n" 4795 " L\"b\");"); 4796 verifyFormat("#define A(X) \\\n" 4797 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4798 " L\"ccccc\"", 4799 getLLVMStyleWithColumns(25)); 4800 4801 verifyFormat("f(@\"a\"\n" 4802 " @\"b\");"); 4803 verifyFormat("NSString s = @\"a\"\n" 4804 " @\"b\"\n" 4805 " @\"c\";"); 4806 verifyFormat("NSString s = @\"a\"\n" 4807 " \"b\"\n" 4808 " \"c\";"); 4809 } 4810 4811 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4812 FormatStyle Style = getLLVMStyle(); 4813 // No declarations or definitions should be moved to own line. 4814 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4815 verifyFormat("class A {\n" 4816 " int f() { return 1; }\n" 4817 " int g();\n" 4818 "};\n" 4819 "int f() { return 1; }\n" 4820 "int g();\n", 4821 Style); 4822 4823 // All declarations and definitions should have the return type moved to its 4824 // own 4825 // line. 4826 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4827 verifyFormat("class E {\n" 4828 " int\n" 4829 " f() {\n" 4830 " return 1;\n" 4831 " }\n" 4832 " int\n" 4833 " g();\n" 4834 "};\n" 4835 "int\n" 4836 "f() {\n" 4837 " return 1;\n" 4838 "}\n" 4839 "int\n" 4840 "g();\n", 4841 Style); 4842 4843 // Top-level definitions, and no kinds of declarations should have the 4844 // return type moved to its own line. 4845 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4846 verifyFormat("class B {\n" 4847 " int f() { return 1; }\n" 4848 " int g();\n" 4849 "};\n" 4850 "int\n" 4851 "f() {\n" 4852 " return 1;\n" 4853 "}\n" 4854 "int g();\n", 4855 Style); 4856 4857 // Top-level definitions and declarations should have the return type moved 4858 // to its own line. 4859 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4860 verifyFormat("class C {\n" 4861 " int f() { return 1; }\n" 4862 " int g();\n" 4863 "};\n" 4864 "int\n" 4865 "f() {\n" 4866 " return 1;\n" 4867 "}\n" 4868 "int\n" 4869 "g();\n", 4870 Style); 4871 4872 // All definitions should have the return type moved to its own line, but no 4873 // kinds of declarations. 4874 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4875 verifyFormat("class D {\n" 4876 " int\n" 4877 " f() {\n" 4878 " return 1;\n" 4879 " }\n" 4880 " int g();\n" 4881 "};\n" 4882 "int\n" 4883 "f() {\n" 4884 " return 1;\n" 4885 "}\n" 4886 "int g();\n", 4887 Style); 4888 verifyFormat("const char *\n" 4889 "f(void) {\n" // Break here. 4890 " return \"\";\n" 4891 "}\n" 4892 "const char *bar(void);\n", // No break here. 4893 Style); 4894 verifyFormat("template <class T>\n" 4895 "T *\n" 4896 "f(T &c) {\n" // Break here. 4897 " return NULL;\n" 4898 "}\n" 4899 "template <class T> T *f(T &c);\n", // No break here. 4900 Style); 4901 verifyFormat("class C {\n" 4902 " int\n" 4903 " operator+() {\n" 4904 " return 1;\n" 4905 " }\n" 4906 " int\n" 4907 " operator()() {\n" 4908 " return 1;\n" 4909 " }\n" 4910 "};\n", 4911 Style); 4912 verifyFormat("void\n" 4913 "A::operator()() {}\n" 4914 "void\n" 4915 "A::operator>>() {}\n" 4916 "void\n" 4917 "A::operator+() {}\n", 4918 Style); 4919 verifyFormat("void *operator new(std::size_t s);", // No break here. 4920 Style); 4921 verifyFormat("void *\n" 4922 "operator new(std::size_t s) {}", 4923 Style); 4924 verifyFormat("void *\n" 4925 "operator delete[](void *ptr) {}", 4926 Style); 4927 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4928 verifyFormat("const char *\n" 4929 "f(void)\n" // Break here. 4930 "{\n" 4931 " return \"\";\n" 4932 "}\n" 4933 "const char *bar(void);\n", // No break here. 4934 Style); 4935 verifyFormat("template <class T>\n" 4936 "T *\n" // Problem here: no line break 4937 "f(T &c)\n" // Break here. 4938 "{\n" 4939 " return NULL;\n" 4940 "}\n" 4941 "template <class T> T *f(T &c);\n", // No break here. 4942 Style); 4943 } 4944 4945 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4946 FormatStyle NoBreak = getLLVMStyle(); 4947 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4948 FormatStyle Break = getLLVMStyle(); 4949 Break.AlwaysBreakBeforeMultilineStrings = true; 4950 verifyFormat("aaaa = \"bbbb\"\n" 4951 " \"cccc\";", 4952 NoBreak); 4953 verifyFormat("aaaa =\n" 4954 " \"bbbb\"\n" 4955 " \"cccc\";", 4956 Break); 4957 verifyFormat("aaaa(\"bbbb\"\n" 4958 " \"cccc\");", 4959 NoBreak); 4960 verifyFormat("aaaa(\n" 4961 " \"bbbb\"\n" 4962 " \"cccc\");", 4963 Break); 4964 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4965 " \"cccc\");", 4966 NoBreak); 4967 verifyFormat("aaaa(qqq,\n" 4968 " \"bbbb\"\n" 4969 " \"cccc\");", 4970 Break); 4971 verifyFormat("aaaa(qqq,\n" 4972 " L\"bbbb\"\n" 4973 " L\"cccc\");", 4974 Break); 4975 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4976 " \"bbbb\"));", 4977 Break); 4978 verifyFormat("string s = someFunction(\n" 4979 " \"abc\"\n" 4980 " \"abc\");", 4981 Break); 4982 4983 // As we break before unary operators, breaking right after them is bad. 4984 verifyFormat("string foo = abc ? \"x\"\n" 4985 " \"blah blah blah blah blah blah\"\n" 4986 " : \"y\";", 4987 Break); 4988 4989 // Don't break if there is no column gain. 4990 verifyFormat("f(\"aaaa\"\n" 4991 " \"bbbb\");", 4992 Break); 4993 4994 // Treat literals with escaped newlines like multi-line string literals. 4995 EXPECT_EQ("x = \"a\\\n" 4996 "b\\\n" 4997 "c\";", 4998 format("x = \"a\\\n" 4999 "b\\\n" 5000 "c\";", 5001 NoBreak)); 5002 EXPECT_EQ("xxxx =\n" 5003 " \"a\\\n" 5004 "b\\\n" 5005 "c\";", 5006 format("xxxx = \"a\\\n" 5007 "b\\\n" 5008 "c\";", 5009 Break)); 5010 5011 // Exempt ObjC strings for now. 5012 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 5013 " @\"bbbb\";", 5014 format("NSString *const kString = @\"aaaa\"\n" 5015 "@\"bbbb\";", 5016 Break)); 5017 5018 Break.ColumnLimit = 0; 5019 verifyFormat("const char *hello = \"hello llvm\";", Break); 5020 } 5021 5022 TEST_F(FormatTest, AlignsPipes) { 5023 verifyFormat( 5024 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5025 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5026 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5027 verifyFormat( 5028 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5029 " << aaaaaaaaaaaaaaaaaaaa;"); 5030 verifyFormat( 5031 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5032 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5033 verifyFormat( 5034 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5035 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5036 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5037 verifyFormat( 5038 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5039 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5040 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5041 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5044 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5045 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5046 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5047 verifyFormat( 5048 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5050 5051 verifyFormat("return out << \"somepacket = {\\n\"\n" 5052 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5053 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5054 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5055 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5056 " << \"}\";"); 5057 5058 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5059 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5060 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5061 verifyFormat( 5062 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5063 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5064 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5065 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5066 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5067 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5068 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5069 verifyFormat( 5070 "void f() {\n" 5071 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5072 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5073 "}"); 5074 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5075 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5076 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5078 " aaaaaaaaaaaaaaaaaaaaa)\n" 5079 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5080 verifyFormat("LOG_IF(aaa == //\n" 5081 " bbb)\n" 5082 " << a << b;"); 5083 5084 // Breaking before the first "<<" is generally not desirable. 5085 verifyFormat( 5086 "llvm::errs()\n" 5087 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5088 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5089 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5090 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5091 getLLVMStyleWithColumns(70)); 5092 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5093 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5094 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5095 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5096 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5097 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5098 getLLVMStyleWithColumns(70)); 5099 5100 // But sometimes, breaking before the first "<<" is desirable. 5101 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5102 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5103 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5104 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5105 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5106 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5107 " << BEF << IsTemplate << Description << E->getType();"); 5108 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5109 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5111 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5112 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5114 " << aaa;"); 5115 5116 verifyFormat( 5117 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5118 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5119 5120 // Incomplete string literal. 5121 EXPECT_EQ("llvm::errs() << \"\n" 5122 " << a;", 5123 format("llvm::errs() << \"\n<<a;")); 5124 5125 verifyFormat("void f() {\n" 5126 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5127 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5128 "}"); 5129 5130 // Handle 'endl'. 5131 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5132 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5133 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5134 5135 // Handle '\n'. 5136 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5137 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5138 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5139 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5140 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5141 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5142 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5143 } 5144 5145 TEST_F(FormatTest, UnderstandsEquals) { 5146 verifyFormat( 5147 "aaaaaaaaaaaaaaaaa =\n" 5148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5149 verifyFormat( 5150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5152 verifyFormat( 5153 "if (a) {\n" 5154 " f();\n" 5155 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5157 "}"); 5158 5159 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5160 " 100000000 + 10000000) {\n}"); 5161 } 5162 5163 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5164 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5165 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5166 5167 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5168 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5169 5170 verifyFormat( 5171 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5172 " Parameter2);"); 5173 5174 verifyFormat( 5175 "ShortObject->shortFunction(\n" 5176 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5177 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5178 5179 verifyFormat("loooooooooooooongFunction(\n" 5180 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5181 5182 verifyFormat( 5183 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5184 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5185 5186 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5187 " .WillRepeatedly(Return(SomeValue));"); 5188 verifyFormat("void f() {\n" 5189 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5190 " .Times(2)\n" 5191 " .WillRepeatedly(Return(SomeValue));\n" 5192 "}"); 5193 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5194 " ccccccccccccccccccccccc);"); 5195 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5197 " .aaaaa(aaaaa),\n" 5198 " aaaaaaaaaaaaaaaaaaaaa);"); 5199 verifyFormat("void f() {\n" 5200 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5201 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5202 "}"); 5203 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5205 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5206 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5207 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5208 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5209 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5210 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5211 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5212 "}"); 5213 5214 // Here, it is not necessary to wrap at "." or "->". 5215 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5216 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5217 verifyFormat( 5218 "aaaaaaaaaaa->aaaaaaaaa(\n" 5219 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5220 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5221 5222 verifyFormat( 5223 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5225 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5226 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5227 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5228 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5229 5230 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5232 " .a();"); 5233 5234 FormatStyle NoBinPacking = getLLVMStyle(); 5235 NoBinPacking.BinPackParameters = false; 5236 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5237 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5238 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5239 " aaaaaaaaaaaaaaaaaaa,\n" 5240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5241 NoBinPacking); 5242 5243 // If there is a subsequent call, change to hanging indentation. 5244 verifyFormat( 5245 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5246 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5247 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5248 verifyFormat( 5249 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5250 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5251 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5253 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5254 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5256 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5257 } 5258 5259 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5260 verifyFormat("template <typename T>\n" 5261 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5262 verifyFormat("template <typename T>\n" 5263 "// T should be one of {A, B}.\n" 5264 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5265 verifyFormat( 5266 "template <typename T>\n" 5267 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5268 verifyFormat("template <typename T>\n" 5269 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5270 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5271 verifyFormat( 5272 "template <typename T>\n" 5273 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5274 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5275 verifyFormat( 5276 "template <typename T>\n" 5277 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5278 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5280 verifyFormat("template <typename T>\n" 5281 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5282 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5283 verifyFormat( 5284 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5285 " typename T4 = char>\n" 5286 "void f();"); 5287 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5288 " template <typename> class cccccccccccccccccccccc,\n" 5289 " typename ddddddddddddd>\n" 5290 "class C {};"); 5291 verifyFormat( 5292 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5294 5295 verifyFormat("void f() {\n" 5296 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5297 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5298 "}"); 5299 5300 verifyFormat("template <typename T> class C {};"); 5301 verifyFormat("template <typename T> void f();"); 5302 verifyFormat("template <typename T> void f() {}"); 5303 verifyFormat( 5304 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5305 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5307 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5308 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5309 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5310 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5311 getLLVMStyleWithColumns(72)); 5312 EXPECT_EQ("static_cast<A< //\n" 5313 " B> *>(\n" 5314 "\n" 5315 " );", 5316 format("static_cast<A<//\n" 5317 " B>*>(\n" 5318 "\n" 5319 " );")); 5320 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5321 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5322 5323 FormatStyle AlwaysBreak = getLLVMStyle(); 5324 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5325 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5326 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5327 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5328 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5329 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5330 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5331 verifyFormat("template <template <typename> class Fooooooo,\n" 5332 " template <typename> class Baaaaaaar>\n" 5333 "struct C {};", 5334 AlwaysBreak); 5335 verifyFormat("template <typename T> // T can be A, B or C.\n" 5336 "struct C {};", 5337 AlwaysBreak); 5338 } 5339 5340 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5341 verifyFormat( 5342 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5344 verifyFormat( 5345 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5348 5349 // FIXME: Should we have the extra indent after the second break? 5350 verifyFormat( 5351 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5354 5355 verifyFormat( 5356 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5357 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5358 5359 // Breaking at nested name specifiers is generally not desirable. 5360 verifyFormat( 5361 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5362 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5363 5364 verifyFormat( 5365 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5367 " aaaaaaaaaaaaaaaaaaaaa);", 5368 getLLVMStyleWithColumns(74)); 5369 5370 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5372 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5373 } 5374 5375 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5376 verifyFormat("A<int> a;"); 5377 verifyFormat("A<A<A<int>>> a;"); 5378 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5379 verifyFormat("bool x = a < 1 || 2 > a;"); 5380 verifyFormat("bool x = 5 < f<int>();"); 5381 verifyFormat("bool x = f<int>() > 5;"); 5382 verifyFormat("bool x = 5 < a<int>::x;"); 5383 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5384 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5385 5386 verifyGoogleFormat("A<A<int>> a;"); 5387 verifyGoogleFormat("A<A<A<int>>> a;"); 5388 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5389 verifyGoogleFormat("A<A<int> > a;"); 5390 verifyGoogleFormat("A<A<A<int> > > a;"); 5391 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5392 verifyGoogleFormat("A<::A<int>> a;"); 5393 verifyGoogleFormat("A<::A> a;"); 5394 verifyGoogleFormat("A< ::A> a;"); 5395 verifyGoogleFormat("A< ::A<int> > a;"); 5396 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5397 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5398 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5399 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5400 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5401 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5402 5403 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5404 5405 verifyFormat("test >> a >> b;"); 5406 verifyFormat("test << a >> b;"); 5407 5408 verifyFormat("f<int>();"); 5409 verifyFormat("template <typename T> void f() {}"); 5410 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5411 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5412 "sizeof(char)>::type>;"); 5413 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5414 verifyFormat("f(a.operator()<A>());"); 5415 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5416 " .template operator()<A>());", 5417 getLLVMStyleWithColumns(35)); 5418 5419 // Not template parameters. 5420 verifyFormat("return a < b && c > d;"); 5421 verifyFormat("void f() {\n" 5422 " while (a < b && c > d) {\n" 5423 " }\n" 5424 "}"); 5425 verifyFormat("template <typename... Types>\n" 5426 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5427 5428 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5430 getLLVMStyleWithColumns(60)); 5431 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5432 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5433 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5434 } 5435 5436 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5437 verifyFormat("COMPARE(a, ==, b);"); 5438 } 5439 5440 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5441 verifyFormat("int A::*x;"); 5442 verifyFormat("int (S::*func)(void *);"); 5443 verifyFormat("void f() { int (S::*func)(void *); }"); 5444 verifyFormat("typedef bool *(Class::*Member)() const;"); 5445 verifyFormat("void f() {\n" 5446 " (a->*f)();\n" 5447 " a->*x;\n" 5448 " (a.*f)();\n" 5449 " ((*a).*f)();\n" 5450 " a.*x;\n" 5451 "}"); 5452 verifyFormat("void f() {\n" 5453 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5454 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5455 "}"); 5456 verifyFormat( 5457 "(aaaaaaaaaa->*bbbbbbb)(\n" 5458 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5459 FormatStyle Style = getLLVMStyle(); 5460 Style.PointerAlignment = FormatStyle::PAS_Left; 5461 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5462 } 5463 5464 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5465 verifyFormat("int a = -2;"); 5466 verifyFormat("f(-1, -2, -3);"); 5467 verifyFormat("a[-1] = 5;"); 5468 verifyFormat("int a = 5 + -2;"); 5469 verifyFormat("if (i == -1) {\n}"); 5470 verifyFormat("if (i != -1) {\n}"); 5471 verifyFormat("if (i > -1) {\n}"); 5472 verifyFormat("if (i < -1) {\n}"); 5473 verifyFormat("++(a->f());"); 5474 verifyFormat("--(a->f());"); 5475 verifyFormat("(a->f())++;"); 5476 verifyFormat("a[42]++;"); 5477 verifyFormat("if (!(a->f())) {\n}"); 5478 5479 verifyFormat("a-- > b;"); 5480 verifyFormat("b ? -a : c;"); 5481 verifyFormat("n * sizeof char16;"); 5482 verifyFormat("n * alignof char16;", getGoogleStyle()); 5483 verifyFormat("sizeof(char);"); 5484 verifyFormat("alignof(char);", getGoogleStyle()); 5485 5486 verifyFormat("return -1;"); 5487 verifyFormat("switch (a) {\n" 5488 "case -1:\n" 5489 " break;\n" 5490 "}"); 5491 verifyFormat("#define X -1"); 5492 verifyFormat("#define X -kConstant"); 5493 5494 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5495 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5496 5497 verifyFormat("int a = /* confusing comment */ -1;"); 5498 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5499 verifyFormat("int a = i /* confusing comment */++;"); 5500 } 5501 5502 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5503 verifyFormat("if (!aaaaaaaaaa( // break\n" 5504 " aaaaa)) {\n" 5505 "}"); 5506 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5507 " aaaaa));"); 5508 verifyFormat("*aaa = aaaaaaa( // break\n" 5509 " bbbbbb);"); 5510 } 5511 5512 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5513 verifyFormat("bool operator<();"); 5514 verifyFormat("bool operator>();"); 5515 verifyFormat("bool operator=();"); 5516 verifyFormat("bool operator==();"); 5517 verifyFormat("bool operator!=();"); 5518 verifyFormat("int operator+();"); 5519 verifyFormat("int operator++();"); 5520 verifyFormat("bool operator,();"); 5521 verifyFormat("bool operator();"); 5522 verifyFormat("bool operator()();"); 5523 verifyFormat("bool operator[]();"); 5524 verifyFormat("operator bool();"); 5525 verifyFormat("operator int();"); 5526 verifyFormat("operator void *();"); 5527 verifyFormat("operator SomeType<int>();"); 5528 verifyFormat("operator SomeType<int, int>();"); 5529 verifyFormat("operator SomeType<SomeType<int>>();"); 5530 verifyFormat("void *operator new(std::size_t size);"); 5531 verifyFormat("void *operator new[](std::size_t size);"); 5532 verifyFormat("void operator delete(void *ptr);"); 5533 verifyFormat("void operator delete[](void *ptr);"); 5534 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5535 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5536 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5537 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5538 5539 verifyFormat( 5540 "ostream &operator<<(ostream &OutputStream,\n" 5541 " SomeReallyLongType WithSomeReallyLongValue);"); 5542 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5543 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5544 " return left.group < right.group;\n" 5545 "}"); 5546 verifyFormat("SomeType &operator=(const SomeType &S);"); 5547 verifyFormat("f.template operator()<int>();"); 5548 5549 verifyGoogleFormat("operator void*();"); 5550 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5551 verifyGoogleFormat("operator ::A();"); 5552 5553 verifyFormat("using A::operator+;"); 5554 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5555 "int i;"); 5556 } 5557 5558 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5559 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5560 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5561 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5562 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5563 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5564 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5565 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5566 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5567 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5568 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5569 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5570 5571 FormatStyle AlignLeft = getLLVMStyle(); 5572 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5573 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5574 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5575 AlignLeft); 5576 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5577 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5578 5579 FormatStyle Spaces = getLLVMStyle(); 5580 Spaces.SpacesInCStyleCastParentheses = true; 5581 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5582 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5583 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5584 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5585 5586 Spaces.SpacesInCStyleCastParentheses = false; 5587 Spaces.SpacesInParentheses = true; 5588 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5589 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5590 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5591 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5592 } 5593 5594 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5595 verifyFormat("void f() {\n" 5596 " A *a = new A;\n" 5597 " A *a = new (placement) A;\n" 5598 " delete a;\n" 5599 " delete (A *)a;\n" 5600 "}"); 5601 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5602 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5603 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5604 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5605 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5606 verifyFormat("delete[] h->p;"); 5607 } 5608 5609 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5610 verifyFormat("int *f(int *a) {}"); 5611 verifyFormat("int main(int argc, char **argv) {}"); 5612 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5613 verifyIndependentOfContext("f(a, *a);"); 5614 verifyFormat("void g() { f(*a); }"); 5615 verifyIndependentOfContext("int a = b * 10;"); 5616 verifyIndependentOfContext("int a = 10 * b;"); 5617 verifyIndependentOfContext("int a = b * c;"); 5618 verifyIndependentOfContext("int a += b * c;"); 5619 verifyIndependentOfContext("int a -= b * c;"); 5620 verifyIndependentOfContext("int a *= b * c;"); 5621 verifyIndependentOfContext("int a /= b * c;"); 5622 verifyIndependentOfContext("int a = *b;"); 5623 verifyIndependentOfContext("int a = *b * c;"); 5624 verifyIndependentOfContext("int a = b * *c;"); 5625 verifyIndependentOfContext("int a = b * (10);"); 5626 verifyIndependentOfContext("S << b * (10);"); 5627 verifyIndependentOfContext("return 10 * b;"); 5628 verifyIndependentOfContext("return *b * *c;"); 5629 verifyIndependentOfContext("return a & ~b;"); 5630 verifyIndependentOfContext("f(b ? *c : *d);"); 5631 verifyIndependentOfContext("int a = b ? *c : *d;"); 5632 verifyIndependentOfContext("*b = a;"); 5633 verifyIndependentOfContext("a * ~b;"); 5634 verifyIndependentOfContext("a * !b;"); 5635 verifyIndependentOfContext("a * +b;"); 5636 verifyIndependentOfContext("a * -b;"); 5637 verifyIndependentOfContext("a * ++b;"); 5638 verifyIndependentOfContext("a * --b;"); 5639 verifyIndependentOfContext("a[4] * b;"); 5640 verifyIndependentOfContext("a[a * a] = 1;"); 5641 verifyIndependentOfContext("f() * b;"); 5642 verifyIndependentOfContext("a * [self dostuff];"); 5643 verifyIndependentOfContext("int x = a * (a + b);"); 5644 verifyIndependentOfContext("(a *)(a + b);"); 5645 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5646 verifyIndependentOfContext("int *pa = (int *)&a;"); 5647 verifyIndependentOfContext("return sizeof(int **);"); 5648 verifyIndependentOfContext("return sizeof(int ******);"); 5649 verifyIndependentOfContext("return (int **&)a;"); 5650 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5651 verifyFormat("void f(Type (*parameter)[10]) {}"); 5652 verifyFormat("void f(Type (¶meter)[10]) {}"); 5653 verifyGoogleFormat("return sizeof(int**);"); 5654 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5655 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5656 verifyFormat("auto a = [](int **&, int ***) {};"); 5657 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5658 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5659 verifyFormat("[](const decltype(*a) &value) {}"); 5660 verifyFormat("decltype(a * b) F();"); 5661 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5662 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5663 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5664 verifyIndependentOfContext("int i{a * b};"); 5665 verifyIndependentOfContext("aaa && aaa->f();"); 5666 verifyIndependentOfContext("int x = ~*p;"); 5667 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5668 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5669 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5670 verifyFormat("void f() { f(a, c * d); }"); 5671 verifyFormat("void f() { f(new a(), c * d); }"); 5672 5673 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5674 5675 verifyIndependentOfContext("A<int *> a;"); 5676 verifyIndependentOfContext("A<int **> a;"); 5677 verifyIndependentOfContext("A<int *, int *> a;"); 5678 verifyIndependentOfContext("A<int *[]> a;"); 5679 verifyIndependentOfContext( 5680 "const char *const p = reinterpret_cast<const char *const>(q);"); 5681 verifyIndependentOfContext("A<int **, int **> a;"); 5682 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5683 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5684 verifyFormat("for (; a && b;) {\n}"); 5685 verifyFormat("bool foo = true && [] { return false; }();"); 5686 5687 verifyFormat( 5688 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5690 5691 verifyGoogleFormat("**outparam = 1;"); 5692 verifyGoogleFormat("*outparam = a * b;"); 5693 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5694 verifyGoogleFormat("A<int*> a;"); 5695 verifyGoogleFormat("A<int**> a;"); 5696 verifyGoogleFormat("A<int*, int*> a;"); 5697 verifyGoogleFormat("A<int**, int**> a;"); 5698 verifyGoogleFormat("f(b ? *c : *d);"); 5699 verifyGoogleFormat("int a = b ? *c : *d;"); 5700 verifyGoogleFormat("Type* t = **x;"); 5701 verifyGoogleFormat("Type* t = *++*x;"); 5702 verifyGoogleFormat("*++*x;"); 5703 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5704 verifyGoogleFormat("Type* t = x++ * y;"); 5705 verifyGoogleFormat( 5706 "const char* const p = reinterpret_cast<const char* const>(q);"); 5707 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5708 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5709 verifyGoogleFormat("template <typename T>\n" 5710 "void f(int i = 0, SomeType** temps = NULL);"); 5711 5712 FormatStyle Left = getLLVMStyle(); 5713 Left.PointerAlignment = FormatStyle::PAS_Left; 5714 verifyFormat("x = *a(x) = *a(y);", Left); 5715 verifyFormat("for (;; * = b) {\n}", Left); 5716 verifyFormat("return *this += 1;", Left); 5717 5718 verifyIndependentOfContext("a = *(x + y);"); 5719 verifyIndependentOfContext("a = &(x + y);"); 5720 verifyIndependentOfContext("*(x + y).call();"); 5721 verifyIndependentOfContext("&(x + y)->call();"); 5722 verifyFormat("void f() { &(*I).first; }"); 5723 5724 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5725 verifyFormat( 5726 "int *MyValues = {\n" 5727 " *A, // Operator detection might be confused by the '{'\n" 5728 " *BB // Operator detection might be confused by previous comment\n" 5729 "};"); 5730 5731 verifyIndependentOfContext("if (int *a = &b)"); 5732 verifyIndependentOfContext("if (int &a = *b)"); 5733 verifyIndependentOfContext("if (a & b[i])"); 5734 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5735 verifyIndependentOfContext("if (*b[i])"); 5736 verifyIndependentOfContext("if (int *a = (&b))"); 5737 verifyIndependentOfContext("while (int *a = &b)"); 5738 verifyIndependentOfContext("size = sizeof *a;"); 5739 verifyIndependentOfContext("if (a && (b = c))"); 5740 verifyFormat("void f() {\n" 5741 " for (const int &v : Values) {\n" 5742 " }\n" 5743 "}"); 5744 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5745 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5746 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5747 5748 verifyFormat("#define A (!a * b)"); 5749 verifyFormat("#define MACRO \\\n" 5750 " int *i = a * b; \\\n" 5751 " void f(a *b);", 5752 getLLVMStyleWithColumns(19)); 5753 5754 verifyIndependentOfContext("A = new SomeType *[Length];"); 5755 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5756 verifyIndependentOfContext("T **t = new T *;"); 5757 verifyIndependentOfContext("T **t = new T *();"); 5758 verifyGoogleFormat("A = new SomeType*[Length]();"); 5759 verifyGoogleFormat("A = new SomeType*[Length];"); 5760 verifyGoogleFormat("T** t = new T*;"); 5761 verifyGoogleFormat("T** t = new T*();"); 5762 5763 FormatStyle PointerLeft = getLLVMStyle(); 5764 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5765 verifyFormat("delete *x;", PointerLeft); 5766 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5767 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5768 verifyFormat("template <bool a, bool b> " 5769 "typename t::if<x && y>::type f() {}"); 5770 verifyFormat("template <int *y> f() {}"); 5771 verifyFormat("vector<int *> v;"); 5772 verifyFormat("vector<int *const> v;"); 5773 verifyFormat("vector<int *const **const *> v;"); 5774 verifyFormat("vector<int *volatile> v;"); 5775 verifyFormat("vector<a * b> v;"); 5776 verifyFormat("foo<b && false>();"); 5777 verifyFormat("foo<b & 1>();"); 5778 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5779 verifyFormat( 5780 "template <class T, class = typename std::enable_if<\n" 5781 " std::is_integral<T>::value &&\n" 5782 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5783 "void F();", 5784 getLLVMStyleWithColumns(76)); 5785 verifyFormat( 5786 "template <class T,\n" 5787 " class = typename ::std::enable_if<\n" 5788 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5789 "void F();", 5790 getGoogleStyleWithColumns(68)); 5791 5792 verifyIndependentOfContext("MACRO(int *i);"); 5793 verifyIndependentOfContext("MACRO(auto *a);"); 5794 verifyIndependentOfContext("MACRO(const A *a);"); 5795 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5796 // FIXME: Is there a way to make this work? 5797 // verifyIndependentOfContext("MACRO(A *a);"); 5798 5799 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5800 verifyFormat("return options != nullptr && operator==(*options);"); 5801 5802 EXPECT_EQ("#define OP(x) \\\n" 5803 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5804 " return s << a.DebugString(); \\\n" 5805 " }", 5806 format("#define OP(x) \\\n" 5807 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5808 " return s << a.DebugString(); \\\n" 5809 " }", 5810 getLLVMStyleWithColumns(50))); 5811 5812 // FIXME: We cannot handle this case yet; we might be able to figure out that 5813 // foo<x> d > v; doesn't make sense. 5814 verifyFormat("foo<a<b && c> d> v;"); 5815 5816 FormatStyle PointerMiddle = getLLVMStyle(); 5817 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5818 verifyFormat("delete *x;", PointerMiddle); 5819 verifyFormat("int * x;", PointerMiddle); 5820 verifyFormat("template <int * y> f() {}", PointerMiddle); 5821 verifyFormat("int * f(int * a) {}", PointerMiddle); 5822 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5823 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5824 verifyFormat("A<int *> a;", PointerMiddle); 5825 verifyFormat("A<int **> a;", PointerMiddle); 5826 verifyFormat("A<int *, int *> a;", PointerMiddle); 5827 verifyFormat("A<int * []> a;", PointerMiddle); 5828 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5829 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5830 verifyFormat("T ** t = new T *;", PointerMiddle); 5831 5832 // Member function reference qualifiers aren't binary operators. 5833 verifyFormat("string // break\n" 5834 "operator()() & {}"); 5835 verifyFormat("string // break\n" 5836 "operator()() && {}"); 5837 verifyGoogleFormat("template <typename T>\n" 5838 "auto x() & -> int {}"); 5839 } 5840 5841 TEST_F(FormatTest, UnderstandsAttributes) { 5842 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5843 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5844 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5845 FormatStyle AfterType = getLLVMStyle(); 5846 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5847 verifyFormat("__attribute__((nodebug)) void\n" 5848 "foo() {}\n", 5849 AfterType); 5850 } 5851 5852 TEST_F(FormatTest, UnderstandsEllipsis) { 5853 verifyFormat("int printf(const char *fmt, ...);"); 5854 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5855 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5856 5857 FormatStyle PointersLeft = getLLVMStyle(); 5858 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5859 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5860 } 5861 5862 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5863 EXPECT_EQ("int *a;\n" 5864 "int *a;\n" 5865 "int *a;", 5866 format("int *a;\n" 5867 "int* a;\n" 5868 "int *a;", 5869 getGoogleStyle())); 5870 EXPECT_EQ("int* a;\n" 5871 "int* a;\n" 5872 "int* a;", 5873 format("int* a;\n" 5874 "int* a;\n" 5875 "int *a;", 5876 getGoogleStyle())); 5877 EXPECT_EQ("int *a;\n" 5878 "int *a;\n" 5879 "int *a;", 5880 format("int *a;\n" 5881 "int * a;\n" 5882 "int * a;", 5883 getGoogleStyle())); 5884 EXPECT_EQ("auto x = [] {\n" 5885 " int *a;\n" 5886 " int *a;\n" 5887 " int *a;\n" 5888 "};", 5889 format("auto x=[]{int *a;\n" 5890 "int * a;\n" 5891 "int * a;};", 5892 getGoogleStyle())); 5893 } 5894 5895 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5896 verifyFormat("int f(int &&a) {}"); 5897 verifyFormat("int f(int a, char &&b) {}"); 5898 verifyFormat("void f() { int &&a = b; }"); 5899 verifyGoogleFormat("int f(int a, char&& b) {}"); 5900 verifyGoogleFormat("void f() { int&& a = b; }"); 5901 5902 verifyIndependentOfContext("A<int &&> a;"); 5903 verifyIndependentOfContext("A<int &&, int &&> a;"); 5904 verifyGoogleFormat("A<int&&> a;"); 5905 verifyGoogleFormat("A<int&&, int&&> a;"); 5906 5907 // Not rvalue references: 5908 verifyFormat("template <bool B, bool C> class A {\n" 5909 " static_assert(B && C, \"Something is wrong\");\n" 5910 "};"); 5911 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5912 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5913 verifyFormat("#define A(a, b) (a && b)"); 5914 } 5915 5916 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5917 verifyFormat("void f() {\n" 5918 " x[aaaaaaaaa -\n" 5919 " b] = 23;\n" 5920 "}", 5921 getLLVMStyleWithColumns(15)); 5922 } 5923 5924 TEST_F(FormatTest, FormatsCasts) { 5925 verifyFormat("Type *A = static_cast<Type *>(P);"); 5926 verifyFormat("Type *A = (Type *)P;"); 5927 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5928 verifyFormat("int a = (int)(2.0f);"); 5929 verifyFormat("int a = (int)2.0f;"); 5930 verifyFormat("x[(int32)y];"); 5931 verifyFormat("x = (int32)y;"); 5932 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5933 verifyFormat("int a = (int)*b;"); 5934 verifyFormat("int a = (int)2.0f;"); 5935 verifyFormat("int a = (int)~0;"); 5936 verifyFormat("int a = (int)++a;"); 5937 verifyFormat("int a = (int)sizeof(int);"); 5938 verifyFormat("int a = (int)+2;"); 5939 verifyFormat("my_int a = (my_int)2.0f;"); 5940 verifyFormat("my_int a = (my_int)sizeof(int);"); 5941 verifyFormat("return (my_int)aaa;"); 5942 verifyFormat("#define x ((int)-1)"); 5943 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5944 verifyFormat("#define p(q) ((int *)&q)"); 5945 verifyFormat("fn(a)(b) + 1;"); 5946 5947 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5948 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5949 verifyFormat("my_int a = (my_int)~0;"); 5950 verifyFormat("my_int a = (my_int)++a;"); 5951 verifyFormat("my_int a = (my_int)-2;"); 5952 verifyFormat("my_int a = (my_int)1;"); 5953 verifyFormat("my_int a = (my_int *)1;"); 5954 verifyFormat("my_int a = (const my_int)-1;"); 5955 verifyFormat("my_int a = (const my_int *)-1;"); 5956 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5957 verifyFormat("my_int a = (ns::my_int)-2;"); 5958 verifyFormat("case (my_int)ONE:"); 5959 5960 // FIXME: single value wrapped with paren will be treated as cast. 5961 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5962 5963 verifyFormat("{ (void)F; }"); 5964 5965 // Don't break after a cast's 5966 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5967 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5968 " bbbbbbbbbbbbbbbbbbbbbb);"); 5969 5970 // These are not casts. 5971 verifyFormat("void f(int *) {}"); 5972 verifyFormat("f(foo)->b;"); 5973 verifyFormat("f(foo).b;"); 5974 verifyFormat("f(foo)(b);"); 5975 verifyFormat("f(foo)[b];"); 5976 verifyFormat("[](foo) { return 4; }(bar);"); 5977 verifyFormat("(*funptr)(foo)[4];"); 5978 verifyFormat("funptrs[4](foo)[4];"); 5979 verifyFormat("void f(int *);"); 5980 verifyFormat("void f(int *) = 0;"); 5981 verifyFormat("void f(SmallVector<int>) {}"); 5982 verifyFormat("void f(SmallVector<int>);"); 5983 verifyFormat("void f(SmallVector<int>) = 0;"); 5984 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5985 verifyFormat("int a = sizeof(int) * b;"); 5986 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5987 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5988 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5989 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5990 5991 // These are not casts, but at some point were confused with casts. 5992 verifyFormat("virtual void foo(int *) override;"); 5993 verifyFormat("virtual void foo(char &) const;"); 5994 verifyFormat("virtual void foo(int *a, char *) const;"); 5995 verifyFormat("int a = sizeof(int *) + b;"); 5996 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5997 verifyFormat("bool b = f(g<int>) && c;"); 5998 verifyFormat("typedef void (*f)(int i) func;"); 5999 6000 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6001 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6002 // FIXME: The indentation here is not ideal. 6003 verifyFormat( 6004 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6005 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6006 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6007 } 6008 6009 TEST_F(FormatTest, FormatsFunctionTypes) { 6010 verifyFormat("A<bool()> a;"); 6011 verifyFormat("A<SomeType()> a;"); 6012 verifyFormat("A<void (*)(int, std::string)> a;"); 6013 verifyFormat("A<void *(int)>;"); 6014 verifyFormat("void *(*a)(int *, SomeType *);"); 6015 verifyFormat("int (*func)(void *);"); 6016 verifyFormat("void f() { int (*func)(void *); }"); 6017 verifyFormat("template <class CallbackClass>\n" 6018 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6019 6020 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6021 verifyGoogleFormat("void* (*a)(int);"); 6022 verifyGoogleFormat( 6023 "template <class CallbackClass>\n" 6024 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6025 6026 // Other constructs can look somewhat like function types: 6027 verifyFormat("A<sizeof(*x)> a;"); 6028 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6029 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6030 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6031 verifyFormat("int x = f(&h)();"); 6032 } 6033 6034 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6035 verifyFormat("A (*foo_)[6];"); 6036 verifyFormat("vector<int> (*foo_)[6];"); 6037 } 6038 6039 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6040 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6041 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6042 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6043 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6044 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6045 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6046 6047 // Different ways of ()-initializiation. 6048 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6049 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6050 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6051 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6052 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6053 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6054 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6055 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6056 } 6057 6058 TEST_F(FormatTest, BreaksLongDeclarations) { 6059 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6060 " AnotherNameForTheLongType;"); 6061 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6063 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6064 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6065 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6066 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6067 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6068 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6069 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6070 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6071 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6072 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6073 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6074 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6075 FormatStyle Indented = getLLVMStyle(); 6076 Indented.IndentWrappedFunctionNames = true; 6077 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6078 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6079 Indented); 6080 verifyFormat( 6081 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6082 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6083 Indented); 6084 verifyFormat( 6085 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6086 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6087 Indented); 6088 verifyFormat( 6089 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6090 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6091 Indented); 6092 6093 // FIXME: Without the comment, this breaks after "(". 6094 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6095 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6096 getGoogleStyle()); 6097 6098 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6099 " int LoooooooooooooooooooongParam2) {}"); 6100 verifyFormat( 6101 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6102 " SourceLocation L, IdentifierIn *II,\n" 6103 " Type *T) {}"); 6104 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6105 "ReallyReaaallyLongFunctionName(\n" 6106 " const std::string &SomeParameter,\n" 6107 " const SomeType<string, SomeOtherTemplateParameter>\n" 6108 " &ReallyReallyLongParameterName,\n" 6109 " const SomeType<string, SomeOtherTemplateParameter>\n" 6110 " &AnotherLongParameterName) {}"); 6111 verifyFormat("template <typename A>\n" 6112 "SomeLoooooooooooooooooooooongType<\n" 6113 " typename some_namespace::SomeOtherType<A>::Type>\n" 6114 "Function() {}"); 6115 6116 verifyGoogleFormat( 6117 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6118 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6119 verifyGoogleFormat( 6120 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6121 " SourceLocation L) {}"); 6122 verifyGoogleFormat( 6123 "some_namespace::LongReturnType\n" 6124 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6125 " int first_long_parameter, int second_parameter) {}"); 6126 6127 verifyGoogleFormat("template <typename T>\n" 6128 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6129 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6130 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6131 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6132 6133 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6134 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6135 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6136 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6137 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6138 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6139 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6140 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6143 } 6144 6145 TEST_F(FormatTest, FormatsArrays) { 6146 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6147 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6148 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6149 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6150 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6151 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6152 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6153 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6154 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6155 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6156 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6157 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6158 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6159 verifyFormat( 6160 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6161 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6162 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6163 6164 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6166 verifyFormat( 6167 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6168 " .aaaaaaa[0]\n" 6169 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6170 verifyFormat("a[::b::c];"); 6171 6172 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6173 6174 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6175 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6176 } 6177 6178 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6179 verifyFormat("(a)->b();"); 6180 verifyFormat("--a;"); 6181 } 6182 6183 TEST_F(FormatTest, HandlesIncludeDirectives) { 6184 verifyFormat("#include <string>\n" 6185 "#include <a/b/c.h>\n" 6186 "#include \"a/b/string\"\n" 6187 "#include \"string.h\"\n" 6188 "#include \"string.h\"\n" 6189 "#include <a-a>\n" 6190 "#include < path with space >\n" 6191 "#include_next <test.h>" 6192 "#include \"abc.h\" // this is included for ABC\n" 6193 "#include \"some long include\" // with a comment\n" 6194 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6195 getLLVMStyleWithColumns(35)); 6196 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6197 EXPECT_EQ("#include <a>", format("#include<a>")); 6198 6199 verifyFormat("#import <string>"); 6200 verifyFormat("#import <a/b/c.h>"); 6201 verifyFormat("#import \"a/b/string\""); 6202 verifyFormat("#import \"string.h\""); 6203 verifyFormat("#import \"string.h\""); 6204 verifyFormat("#if __has_include(<strstream>)\n" 6205 "#include <strstream>\n" 6206 "#endif"); 6207 6208 verifyFormat("#define MY_IMPORT <a/b>"); 6209 6210 // Protocol buffer definition or missing "#". 6211 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6212 getLLVMStyleWithColumns(30)); 6213 6214 FormatStyle Style = getLLVMStyle(); 6215 Style.AlwaysBreakBeforeMultilineStrings = true; 6216 Style.ColumnLimit = 0; 6217 verifyFormat("#import \"abc.h\"", Style); 6218 6219 // But 'import' might also be a regular C++ namespace. 6220 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6222 } 6223 6224 //===----------------------------------------------------------------------===// 6225 // Error recovery tests. 6226 //===----------------------------------------------------------------------===// 6227 6228 TEST_F(FormatTest, IncompleteParameterLists) { 6229 FormatStyle NoBinPacking = getLLVMStyle(); 6230 NoBinPacking.BinPackParameters = false; 6231 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6232 " double *min_x,\n" 6233 " double *max_x,\n" 6234 " double *min_y,\n" 6235 " double *max_y,\n" 6236 " double *min_z,\n" 6237 " double *max_z, ) {}", 6238 NoBinPacking); 6239 } 6240 6241 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6242 verifyFormat("void f() { return; }\n42"); 6243 verifyFormat("void f() {\n" 6244 " if (0)\n" 6245 " return;\n" 6246 "}\n" 6247 "42"); 6248 verifyFormat("void f() { return }\n42"); 6249 verifyFormat("void f() {\n" 6250 " if (0)\n" 6251 " return\n" 6252 "}\n" 6253 "42"); 6254 } 6255 6256 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6257 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6258 EXPECT_EQ("void f() {\n" 6259 " if (a)\n" 6260 " return\n" 6261 "}", 6262 format("void f ( ) { if ( a ) return }")); 6263 EXPECT_EQ("namespace N {\n" 6264 "void f()\n" 6265 "}", 6266 format("namespace N { void f() }")); 6267 EXPECT_EQ("namespace N {\n" 6268 "void f() {}\n" 6269 "void g()\n" 6270 "}", 6271 format("namespace N { void f( ) { } void g( ) }")); 6272 } 6273 6274 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6275 verifyFormat("int aaaaaaaa =\n" 6276 " // Overlylongcomment\n" 6277 " b;", 6278 getLLVMStyleWithColumns(20)); 6279 verifyFormat("function(\n" 6280 " ShortArgument,\n" 6281 " LoooooooooooongArgument);\n", 6282 getLLVMStyleWithColumns(20)); 6283 } 6284 6285 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6286 verifyFormat("public:"); 6287 verifyFormat("class A {\n" 6288 "public\n" 6289 " void f() {}\n" 6290 "};"); 6291 verifyFormat("public\n" 6292 "int qwerty;"); 6293 verifyFormat("public\n" 6294 "B {}"); 6295 verifyFormat("public\n" 6296 "{}"); 6297 verifyFormat("public\n" 6298 "B { int x; }"); 6299 } 6300 6301 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6302 verifyFormat("{"); 6303 verifyFormat("#})"); 6304 verifyNoCrash("(/**/[:!] ?[)."); 6305 } 6306 6307 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6308 verifyFormat("do {\n}"); 6309 verifyFormat("do {\n}\n" 6310 "f();"); 6311 verifyFormat("do {\n}\n" 6312 "wheeee(fun);"); 6313 verifyFormat("do {\n" 6314 " f();\n" 6315 "}"); 6316 } 6317 6318 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6319 verifyFormat("if {\n foo;\n foo();\n}"); 6320 verifyFormat("switch {\n foo;\n foo();\n}"); 6321 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6322 verifyFormat("while {\n foo;\n foo();\n}"); 6323 verifyFormat("do {\n foo;\n foo();\n} while;"); 6324 } 6325 6326 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6327 verifyIncompleteFormat("namespace {\n" 6328 "class Foo { Foo (\n" 6329 "};\n" 6330 "} // comment"); 6331 } 6332 6333 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6334 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6335 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6336 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6337 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6338 6339 EXPECT_EQ("{\n" 6340 " {\n" 6341 " breakme(\n" 6342 " qwe);\n" 6343 " }\n", 6344 format("{\n" 6345 " {\n" 6346 " breakme(qwe);\n" 6347 "}\n", 6348 getLLVMStyleWithColumns(10))); 6349 } 6350 6351 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6352 verifyFormat("int x = {\n" 6353 " avariable,\n" 6354 " b(alongervariable)};", 6355 getLLVMStyleWithColumns(25)); 6356 } 6357 6358 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6359 verifyFormat("return (a)(b){1, 2, 3};"); 6360 } 6361 6362 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6363 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6364 verifyFormat("vector<int> x{\n" 6365 " 1, 2, 3, 4,\n" 6366 "};"); 6367 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6368 verifyFormat("f({1, 2});"); 6369 verifyFormat("auto v = Foo{-1};"); 6370 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6371 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6372 verifyFormat("new vector<int>{1, 2, 3};"); 6373 verifyFormat("new int[3]{1, 2, 3};"); 6374 verifyFormat("new int{1};"); 6375 verifyFormat("return {arg1, arg2};"); 6376 verifyFormat("return {arg1, SomeType{parameter}};"); 6377 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6378 verifyFormat("new T{arg1, arg2};"); 6379 verifyFormat("f(MyMap[{composite, key}]);"); 6380 verifyFormat("class Class {\n" 6381 " T member = {arg1, arg2};\n" 6382 "};"); 6383 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6384 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6385 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6386 6387 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6388 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6389 verifyFormat("auto i = decltype(x){};"); 6390 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6391 verifyFormat("Node n{1, Node{1000}, //\n" 6392 " 2};"); 6393 verifyFormat("Aaaa aaaaaaa{\n" 6394 " {\n" 6395 " aaaa,\n" 6396 " },\n" 6397 "};"); 6398 verifyFormat("class C : public D {\n" 6399 " SomeClass SC{2};\n" 6400 "};"); 6401 verifyFormat("class C : public A {\n" 6402 " class D : public B {\n" 6403 " void f() { int i{2}; }\n" 6404 " };\n" 6405 "};"); 6406 verifyFormat("#define A {a, a},"); 6407 6408 // In combination with BinPackArguments = false. 6409 FormatStyle NoBinPacking = getLLVMStyle(); 6410 NoBinPacking.BinPackArguments = false; 6411 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6412 " bbbbb,\n" 6413 " ccccc,\n" 6414 " ddddd,\n" 6415 " eeeee,\n" 6416 " ffffff,\n" 6417 " ggggg,\n" 6418 " hhhhhh,\n" 6419 " iiiiii,\n" 6420 " jjjjjj,\n" 6421 " kkkkkk};", 6422 NoBinPacking); 6423 verifyFormat("const Aaaaaa aaaaa = {\n" 6424 " aaaaa,\n" 6425 " bbbbb,\n" 6426 " ccccc,\n" 6427 " ddddd,\n" 6428 " eeeee,\n" 6429 " ffffff,\n" 6430 " ggggg,\n" 6431 " hhhhhh,\n" 6432 " iiiiii,\n" 6433 " jjjjjj,\n" 6434 " kkkkkk,\n" 6435 "};", 6436 NoBinPacking); 6437 verifyFormat( 6438 "const Aaaaaa aaaaa = {\n" 6439 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6440 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6441 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6442 "};", 6443 NoBinPacking); 6444 6445 // FIXME: The alignment of these trailing comments might be bad. Then again, 6446 // this might be utterly useless in real code. 6447 verifyFormat("Constructor::Constructor()\n" 6448 " : some_value{ //\n" 6449 " aaaaaaa, //\n" 6450 " bbbbbbb} {}"); 6451 6452 // In braced lists, the first comment is always assumed to belong to the 6453 // first element. Thus, it can be moved to the next or previous line as 6454 // appropriate. 6455 EXPECT_EQ("function({// First element:\n" 6456 " 1,\n" 6457 " // Second element:\n" 6458 " 2});", 6459 format("function({\n" 6460 " // First element:\n" 6461 " 1,\n" 6462 " // Second element:\n" 6463 " 2});")); 6464 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6465 " // First element:\n" 6466 " 1,\n" 6467 " // Second element:\n" 6468 " 2};", 6469 format("std::vector<int> MyNumbers{// First element:\n" 6470 " 1,\n" 6471 " // Second element:\n" 6472 " 2};", 6473 getLLVMStyleWithColumns(30))); 6474 // A trailing comma should still lead to an enforced line break. 6475 EXPECT_EQ("vector<int> SomeVector = {\n" 6476 " // aaa\n" 6477 " 1, 2,\n" 6478 "};", 6479 format("vector<int> SomeVector = { // aaa\n" 6480 " 1, 2, };")); 6481 6482 FormatStyle ExtraSpaces = getLLVMStyle(); 6483 ExtraSpaces.Cpp11BracedListStyle = false; 6484 ExtraSpaces.ColumnLimit = 75; 6485 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6486 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6487 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6488 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6489 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6490 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6491 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6492 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6493 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6494 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6495 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6496 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6497 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6498 verifyFormat("class Class {\n" 6499 " T member = { arg1, arg2 };\n" 6500 "};", 6501 ExtraSpaces); 6502 verifyFormat( 6503 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6504 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6505 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6506 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6507 ExtraSpaces); 6508 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6509 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6510 ExtraSpaces); 6511 verifyFormat( 6512 "someFunction(OtherParam,\n" 6513 " BracedList{ // comment 1 (Forcing interesting break)\n" 6514 " param1, param2,\n" 6515 " // comment 2\n" 6516 " param3, param4 });", 6517 ExtraSpaces); 6518 verifyFormat( 6519 "std::this_thread::sleep_for(\n" 6520 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6521 ExtraSpaces); 6522 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6523 " aaaaaaa,\n" 6524 " aaaaaaaaaa,\n" 6525 " aaaaa,\n" 6526 " aaaaaaaaaaaaaaa,\n" 6527 " aaa,\n" 6528 " aaaaaaaaaa,\n" 6529 " a,\n" 6530 " aaaaaaaaaaaaaaaaaaaaa,\n" 6531 " aaaaaaaaaaaa,\n" 6532 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6533 " aaaaaaa,\n" 6534 " a};"); 6535 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6536 } 6537 6538 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6539 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6540 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6541 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6542 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6543 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6544 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6545 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6546 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6547 " 1, 22, 333, 4444, 55555, //\n" 6548 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6549 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6550 verifyFormat( 6551 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6552 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6553 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6554 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6555 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6556 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6557 " 7777777};"); 6558 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6559 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6560 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6561 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6562 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6563 " // Separating comment.\n" 6564 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6565 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6566 " // Leading comment\n" 6567 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6568 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6569 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6570 " 1, 1, 1, 1};", 6571 getLLVMStyleWithColumns(39)); 6572 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6573 " 1, 1, 1, 1};", 6574 getLLVMStyleWithColumns(38)); 6575 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6576 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6577 getLLVMStyleWithColumns(43)); 6578 verifyFormat( 6579 "static unsigned SomeValues[10][3] = {\n" 6580 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6581 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6582 verifyFormat("static auto fields = new vector<string>{\n" 6583 " \"aaaaaaaaaaaaa\",\n" 6584 " \"aaaaaaaaaaaaa\",\n" 6585 " \"aaaaaaaaaaaa\",\n" 6586 " \"aaaaaaaaaaaaaa\",\n" 6587 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6588 " \"aaaaaaaaaaaa\",\n" 6589 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6590 "};"); 6591 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6592 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6593 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6594 " 3, cccccccccccccccccccccc};", 6595 getLLVMStyleWithColumns(60)); 6596 6597 // Trailing commas. 6598 verifyFormat("vector<int> x = {\n" 6599 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6600 "};", 6601 getLLVMStyleWithColumns(39)); 6602 verifyFormat("vector<int> x = {\n" 6603 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6604 "};", 6605 getLLVMStyleWithColumns(39)); 6606 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6607 " 1, 1, 1, 1,\n" 6608 " /**/ /**/};", 6609 getLLVMStyleWithColumns(39)); 6610 6611 // Trailing comment in the first line. 6612 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6613 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6614 " 111111111, 222222222, 3333333333, 444444444, //\n" 6615 " 11111111, 22222222, 333333333, 44444444};"); 6616 // Trailing comment in the last line. 6617 verifyFormat("int aaaaa[] = {\n" 6618 " 1, 2, 3, // comment\n" 6619 " 4, 5, 6 // comment\n" 6620 "};"); 6621 6622 // With nested lists, we should either format one item per line or all nested 6623 // lists one on line. 6624 // FIXME: For some nested lists, we can do better. 6625 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6626 " {aaaaaaaaaaaaaaaaaaa},\n" 6627 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6628 " {aaaaaaaaaaaaaaaaa}};", 6629 getLLVMStyleWithColumns(60)); 6630 verifyFormat( 6631 "SomeStruct my_struct_array = {\n" 6632 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6633 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6634 " {aaa, aaa},\n" 6635 " {aaa, aaa},\n" 6636 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6637 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6638 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6639 6640 // No column layout should be used here. 6641 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6642 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6643 6644 verifyNoCrash("a<,"); 6645 6646 // No braced initializer here. 6647 verifyFormat("void f() {\n" 6648 " struct Dummy {};\n" 6649 " f(v);\n" 6650 "}"); 6651 6652 // Long lists should be formatted in columns even if they are nested. 6653 verifyFormat( 6654 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6655 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6656 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6657 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6658 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6659 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6660 } 6661 6662 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6663 FormatStyle DoNotMerge = getLLVMStyle(); 6664 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6665 6666 verifyFormat("void f() { return 42; }"); 6667 verifyFormat("void f() {\n" 6668 " return 42;\n" 6669 "}", 6670 DoNotMerge); 6671 verifyFormat("void f() {\n" 6672 " // Comment\n" 6673 "}"); 6674 verifyFormat("{\n" 6675 "#error {\n" 6676 " int a;\n" 6677 "}"); 6678 verifyFormat("{\n" 6679 " int a;\n" 6680 "#error {\n" 6681 "}"); 6682 verifyFormat("void f() {} // comment"); 6683 verifyFormat("void f() { int a; } // comment"); 6684 verifyFormat("void f() {\n" 6685 "} // comment", 6686 DoNotMerge); 6687 verifyFormat("void f() {\n" 6688 " int a;\n" 6689 "} // comment", 6690 DoNotMerge); 6691 verifyFormat("void f() {\n" 6692 "} // comment", 6693 getLLVMStyleWithColumns(15)); 6694 6695 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6696 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6697 6698 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6699 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6700 verifyFormat("class C {\n" 6701 " C()\n" 6702 " : iiiiiiii(nullptr),\n" 6703 " kkkkkkk(nullptr),\n" 6704 " mmmmmmm(nullptr),\n" 6705 " nnnnnnn(nullptr) {}\n" 6706 "};", 6707 getGoogleStyle()); 6708 6709 FormatStyle NoColumnLimit = getLLVMStyle(); 6710 NoColumnLimit.ColumnLimit = 0; 6711 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6712 EXPECT_EQ("class C {\n" 6713 " A() : b(0) {}\n" 6714 "};", 6715 format("class C{A():b(0){}};", NoColumnLimit)); 6716 EXPECT_EQ("A()\n" 6717 " : b(0) {\n" 6718 "}", 6719 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6720 6721 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6722 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6723 FormatStyle::SFS_None; 6724 EXPECT_EQ("A()\n" 6725 " : b(0) {\n" 6726 "}", 6727 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6728 EXPECT_EQ("A()\n" 6729 " : b(0) {\n" 6730 "}", 6731 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6732 6733 verifyFormat("#define A \\\n" 6734 " void f() { \\\n" 6735 " int i; \\\n" 6736 " }", 6737 getLLVMStyleWithColumns(20)); 6738 verifyFormat("#define A \\\n" 6739 " void f() { int i; }", 6740 getLLVMStyleWithColumns(21)); 6741 verifyFormat("#define A \\\n" 6742 " void f() { \\\n" 6743 " int i; \\\n" 6744 " } \\\n" 6745 " int j;", 6746 getLLVMStyleWithColumns(22)); 6747 verifyFormat("#define A \\\n" 6748 " void f() { int i; } \\\n" 6749 " int j;", 6750 getLLVMStyleWithColumns(23)); 6751 } 6752 6753 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6754 FormatStyle MergeInlineOnly = getLLVMStyle(); 6755 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6756 verifyFormat("class C {\n" 6757 " int f() { return 42; }\n" 6758 "};", 6759 MergeInlineOnly); 6760 verifyFormat("int f() {\n" 6761 " return 42;\n" 6762 "}", 6763 MergeInlineOnly); 6764 } 6765 6766 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6767 // Elaborate type variable declarations. 6768 verifyFormat("struct foo a = {bar};\nint n;"); 6769 verifyFormat("class foo a = {bar};\nint n;"); 6770 verifyFormat("union foo a = {bar};\nint n;"); 6771 6772 // Elaborate types inside function definitions. 6773 verifyFormat("struct foo f() {}\nint n;"); 6774 verifyFormat("class foo f() {}\nint n;"); 6775 verifyFormat("union foo f() {}\nint n;"); 6776 6777 // Templates. 6778 verifyFormat("template <class X> void f() {}\nint n;"); 6779 verifyFormat("template <struct X> void f() {}\nint n;"); 6780 verifyFormat("template <union X> void f() {}\nint n;"); 6781 6782 // Actual definitions... 6783 verifyFormat("struct {\n} n;"); 6784 verifyFormat( 6785 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6786 verifyFormat("union Z {\n int n;\n} x;"); 6787 verifyFormat("class MACRO Z {\n} n;"); 6788 verifyFormat("class MACRO(X) Z {\n} n;"); 6789 verifyFormat("class __attribute__(X) Z {\n} n;"); 6790 verifyFormat("class __declspec(X) Z {\n} n;"); 6791 verifyFormat("class A##B##C {\n} n;"); 6792 verifyFormat("class alignas(16) Z {\n} n;"); 6793 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6794 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6795 6796 // Redefinition from nested context: 6797 verifyFormat("class A::B::C {\n} n;"); 6798 6799 // Template definitions. 6800 verifyFormat( 6801 "template <typename F>\n" 6802 "Matcher(const Matcher<F> &Other,\n" 6803 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6804 " !is_same<F, T>::value>::type * = 0)\n" 6805 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6806 6807 // FIXME: This is still incorrectly handled at the formatter side. 6808 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6809 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6810 6811 // FIXME: 6812 // This now gets parsed incorrectly as class definition. 6813 // verifyFormat("class A<int> f() {\n}\nint n;"); 6814 6815 // Elaborate types where incorrectly parsing the structural element would 6816 // break the indent. 6817 verifyFormat("if (true)\n" 6818 " class X x;\n" 6819 "else\n" 6820 " f();\n"); 6821 6822 // This is simply incomplete. Formatting is not important, but must not crash. 6823 verifyFormat("class A:"); 6824 } 6825 6826 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6827 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6828 format("#error Leave all white!!!!! space* alone!\n")); 6829 EXPECT_EQ( 6830 "#warning Leave all white!!!!! space* alone!\n", 6831 format("#warning Leave all white!!!!! space* alone!\n")); 6832 EXPECT_EQ("#error 1", format(" # error 1")); 6833 EXPECT_EQ("#warning 1", format(" # warning 1")); 6834 } 6835 6836 TEST_F(FormatTest, FormatHashIfExpressions) { 6837 verifyFormat("#if AAAA && BBBB"); 6838 verifyFormat("#if (AAAA && BBBB)"); 6839 verifyFormat("#elif (AAAA && BBBB)"); 6840 // FIXME: Come up with a better indentation for #elif. 6841 verifyFormat( 6842 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6843 " defined(BBBBBBBB)\n" 6844 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6845 " defined(BBBBBBBB)\n" 6846 "#endif", 6847 getLLVMStyleWithColumns(65)); 6848 } 6849 6850 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6851 FormatStyle AllowsMergedIf = getGoogleStyle(); 6852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6853 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6854 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6855 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6856 EXPECT_EQ("if (true) return 42;", 6857 format("if (true)\nreturn 42;", AllowsMergedIf)); 6858 FormatStyle ShortMergedIf = AllowsMergedIf; 6859 ShortMergedIf.ColumnLimit = 25; 6860 verifyFormat("#define A \\\n" 6861 " if (true) return 42;", 6862 ShortMergedIf); 6863 verifyFormat("#define A \\\n" 6864 " f(); \\\n" 6865 " if (true)\n" 6866 "#define B", 6867 ShortMergedIf); 6868 verifyFormat("#define A \\\n" 6869 " f(); \\\n" 6870 " if (true)\n" 6871 "g();", 6872 ShortMergedIf); 6873 verifyFormat("{\n" 6874 "#ifdef A\n" 6875 " // Comment\n" 6876 " if (true) continue;\n" 6877 "#endif\n" 6878 " // Comment\n" 6879 " if (true) continue;\n" 6880 "}", 6881 ShortMergedIf); 6882 ShortMergedIf.ColumnLimit = 29; 6883 verifyFormat("#define A \\\n" 6884 " if (aaaaaaaaaa) return 1; \\\n" 6885 " return 2;", 6886 ShortMergedIf); 6887 ShortMergedIf.ColumnLimit = 28; 6888 verifyFormat("#define A \\\n" 6889 " if (aaaaaaaaaa) \\\n" 6890 " return 1; \\\n" 6891 " return 2;", 6892 ShortMergedIf); 6893 } 6894 6895 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6896 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6897 " f();\n" 6898 "}"); 6899 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6900 " f();\n" 6901 "} /* another comment */ else /* comment #3 */ {\n" 6902 " g();\n" 6903 "}"); 6904 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6905 " f();\n" 6906 "}"); 6907 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6908 " f();\n" 6909 "}"); 6910 verifyFormat("do /* a comment in a strange place */ {\n" 6911 " f();\n" 6912 "} /* another comment */ while (0);"); 6913 } 6914 6915 TEST_F(FormatTest, BlockComments) { 6916 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6917 format("/* *//* */ /* */\n/* *//* */ /* */")); 6918 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6919 EXPECT_EQ("#define A /*123*/ \\\n" 6920 " b\n" 6921 "/* */\n" 6922 "someCall(\n" 6923 " parameter);", 6924 format("#define A /*123*/ b\n" 6925 "/* */\n" 6926 "someCall(parameter);", 6927 getLLVMStyleWithColumns(15))); 6928 6929 EXPECT_EQ("#define A\n" 6930 "/* */ someCall(\n" 6931 " parameter);", 6932 format("#define A\n" 6933 "/* */someCall(parameter);", 6934 getLLVMStyleWithColumns(15))); 6935 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6936 EXPECT_EQ("/*\n" 6937 "*\n" 6938 " * aaaaaa\n" 6939 " * aaaaaa\n" 6940 "*/", 6941 format("/*\n" 6942 "*\n" 6943 " * aaaaaa aaaaaa\n" 6944 "*/", 6945 getLLVMStyleWithColumns(10))); 6946 EXPECT_EQ("/*\n" 6947 "**\n" 6948 "* aaaaaa\n" 6949 "*aaaaaa\n" 6950 "*/", 6951 format("/*\n" 6952 "**\n" 6953 "* aaaaaa aaaaaa\n" 6954 "*/", 6955 getLLVMStyleWithColumns(10))); 6956 6957 FormatStyle NoBinPacking = getLLVMStyle(); 6958 NoBinPacking.BinPackParameters = false; 6959 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6960 " 2, /* comment 2 */\n" 6961 " 3, /* comment 3 */\n" 6962 " aaaa,\n" 6963 " bbbb);", 6964 format("someFunction (1, /* comment 1 */\n" 6965 " 2, /* comment 2 */ \n" 6966 " 3, /* comment 3 */\n" 6967 "aaaa, bbbb );", 6968 NoBinPacking)); 6969 verifyFormat( 6970 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6972 EXPECT_EQ( 6973 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6974 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 6976 format( 6977 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6978 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 6980 EXPECT_EQ( 6981 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6982 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6983 "int cccccccccccccccccccccccccccccc; /* comment */\n", 6984 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6985 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6986 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 6987 6988 verifyFormat("void f(int * /* unused */) {}"); 6989 6990 EXPECT_EQ("/*\n" 6991 " **\n" 6992 " */", 6993 format("/*\n" 6994 " **\n" 6995 " */")); 6996 EXPECT_EQ("/*\n" 6997 " *q\n" 6998 " */", 6999 format("/*\n" 7000 " *q\n" 7001 " */")); 7002 EXPECT_EQ("/*\n" 7003 " * q\n" 7004 " */", 7005 format("/*\n" 7006 " * q\n" 7007 " */")); 7008 EXPECT_EQ("/*\n" 7009 " **/", 7010 format("/*\n" 7011 " **/")); 7012 EXPECT_EQ("/*\n" 7013 " ***/", 7014 format("/*\n" 7015 " ***/")); 7016 } 7017 7018 TEST_F(FormatTest, BlockCommentsInMacros) { 7019 EXPECT_EQ("#define A \\\n" 7020 " { \\\n" 7021 " /* one line */ \\\n" 7022 " someCall();", 7023 format("#define A { \\\n" 7024 " /* one line */ \\\n" 7025 " someCall();", 7026 getLLVMStyleWithColumns(20))); 7027 EXPECT_EQ("#define A \\\n" 7028 " { \\\n" 7029 " /* previous */ \\\n" 7030 " /* one line */ \\\n" 7031 " someCall();", 7032 format("#define A { \\\n" 7033 " /* previous */ \\\n" 7034 " /* one line */ \\\n" 7035 " someCall();", 7036 getLLVMStyleWithColumns(20))); 7037 } 7038 7039 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7040 EXPECT_EQ("a = {\n" 7041 " 1111 /* */\n" 7042 "};", 7043 format("a = {1111 /* */\n" 7044 "};", 7045 getLLVMStyleWithColumns(15))); 7046 EXPECT_EQ("a = {\n" 7047 " 1111 /* */\n" 7048 "};", 7049 format("a = {1111 /* */\n" 7050 "};", 7051 getLLVMStyleWithColumns(15))); 7052 7053 // FIXME: The formatting is still wrong here. 7054 EXPECT_EQ("a = {\n" 7055 " 1111 /* a\n" 7056 " */\n" 7057 "};", 7058 format("a = {1111 /* a */\n" 7059 "};", 7060 getLLVMStyleWithColumns(15))); 7061 } 7062 7063 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7064 // FIXME: This is not what we want... 7065 verifyFormat("{\n" 7066 "// a" 7067 "// b"); 7068 } 7069 7070 TEST_F(FormatTest, FormatStarDependingOnContext) { 7071 verifyFormat("void f(int *a);"); 7072 verifyFormat("void f() { f(fint * b); }"); 7073 verifyFormat("class A {\n void f(int *a);\n};"); 7074 verifyFormat("class A {\n int *a;\n};"); 7075 verifyFormat("namespace a {\n" 7076 "namespace b {\n" 7077 "class A {\n" 7078 " void f() {}\n" 7079 " int *a;\n" 7080 "};\n" 7081 "}\n" 7082 "}"); 7083 } 7084 7085 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7086 verifyFormat("while"); 7087 verifyFormat("operator"); 7088 } 7089 7090 //===----------------------------------------------------------------------===// 7091 // Objective-C tests. 7092 //===----------------------------------------------------------------------===// 7093 7094 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7095 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7096 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7097 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7098 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7099 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7100 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7101 format("-(NSInteger)Method3:(id)anObject;")); 7102 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7103 format("-(NSInteger)Method4:(id)anObject;")); 7104 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7105 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7106 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7107 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7108 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7109 "forAllCells:(BOOL)flag;", 7110 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7111 "forAllCells:(BOOL)flag;")); 7112 7113 // Very long objectiveC method declaration. 7114 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7115 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7116 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7117 " inRange:(NSRange)range\n" 7118 " outRange:(NSRange)out_range\n" 7119 " outRange1:(NSRange)out_range1\n" 7120 " outRange2:(NSRange)out_range2\n" 7121 " outRange3:(NSRange)out_range3\n" 7122 " outRange4:(NSRange)out_range4\n" 7123 " outRange5:(NSRange)out_range5\n" 7124 " outRange6:(NSRange)out_range6\n" 7125 " outRange7:(NSRange)out_range7\n" 7126 " outRange8:(NSRange)out_range8\n" 7127 " outRange9:(NSRange)out_range9;"); 7128 7129 // When the function name has to be wrapped. 7130 FormatStyle Style = getLLVMStyle(); 7131 Style.IndentWrappedFunctionNames = false; 7132 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7133 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7134 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7135 "}", 7136 Style); 7137 Style.IndentWrappedFunctionNames = true; 7138 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7139 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7140 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7141 "}", 7142 Style); 7143 7144 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7145 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7146 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7147 // protocol lists (but not for template classes): 7148 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7149 7150 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7151 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7152 7153 // If there's no return type (very rare in practice!), LLVM and Google style 7154 // agree. 7155 verifyFormat("- foo;"); 7156 verifyFormat("- foo:(int)f;"); 7157 verifyGoogleFormat("- foo:(int)foo;"); 7158 } 7159 7160 TEST_F(FormatTest, FormatObjCInterface) { 7161 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7162 "@public\n" 7163 " int field1;\n" 7164 "@protected\n" 7165 " int field2;\n" 7166 "@private\n" 7167 " int field3;\n" 7168 "@package\n" 7169 " int field4;\n" 7170 "}\n" 7171 "+ (id)init;\n" 7172 "@end"); 7173 7174 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7175 " @public\n" 7176 " int field1;\n" 7177 " @protected\n" 7178 " int field2;\n" 7179 " @private\n" 7180 " int field3;\n" 7181 " @package\n" 7182 " int field4;\n" 7183 "}\n" 7184 "+ (id)init;\n" 7185 "@end"); 7186 7187 verifyFormat("@interface /* wait for it */ Foo\n" 7188 "+ (id)init;\n" 7189 "// Look, a comment!\n" 7190 "- (int)answerWith:(int)i;\n" 7191 "@end"); 7192 7193 verifyFormat("@interface Foo\n" 7194 "@end\n" 7195 "@interface Bar\n" 7196 "@end"); 7197 7198 verifyFormat("@interface Foo : Bar\n" 7199 "+ (id)init;\n" 7200 "@end"); 7201 7202 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7203 "+ (id)init;\n" 7204 "@end"); 7205 7206 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7207 "+ (id)init;\n" 7208 "@end"); 7209 7210 verifyFormat("@interface Foo (HackStuff)\n" 7211 "+ (id)init;\n" 7212 "@end"); 7213 7214 verifyFormat("@interface Foo ()\n" 7215 "+ (id)init;\n" 7216 "@end"); 7217 7218 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7219 "+ (id)init;\n" 7220 "@end"); 7221 7222 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7223 "+ (id)init;\n" 7224 "@end"); 7225 7226 verifyFormat("@interface Foo {\n" 7227 " int _i;\n" 7228 "}\n" 7229 "+ (id)init;\n" 7230 "@end"); 7231 7232 verifyFormat("@interface Foo : Bar {\n" 7233 " int _i;\n" 7234 "}\n" 7235 "+ (id)init;\n" 7236 "@end"); 7237 7238 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7239 " int _i;\n" 7240 "}\n" 7241 "+ (id)init;\n" 7242 "@end"); 7243 7244 verifyFormat("@interface Foo (HackStuff) {\n" 7245 " int _i;\n" 7246 "}\n" 7247 "+ (id)init;\n" 7248 "@end"); 7249 7250 verifyFormat("@interface Foo () {\n" 7251 " int _i;\n" 7252 "}\n" 7253 "+ (id)init;\n" 7254 "@end"); 7255 7256 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7257 " int _i;\n" 7258 "}\n" 7259 "+ (id)init;\n" 7260 "@end"); 7261 7262 FormatStyle OnePerLine = getGoogleStyle(); 7263 OnePerLine.BinPackParameters = false; 7264 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7269 "}", 7270 OnePerLine); 7271 } 7272 7273 TEST_F(FormatTest, FormatObjCImplementation) { 7274 verifyFormat("@implementation Foo : NSObject {\n" 7275 "@public\n" 7276 " int field1;\n" 7277 "@protected\n" 7278 " int field2;\n" 7279 "@private\n" 7280 " int field3;\n" 7281 "@package\n" 7282 " int field4;\n" 7283 "}\n" 7284 "+ (id)init {\n}\n" 7285 "@end"); 7286 7287 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7288 " @public\n" 7289 " int field1;\n" 7290 " @protected\n" 7291 " int field2;\n" 7292 " @private\n" 7293 " int field3;\n" 7294 " @package\n" 7295 " int field4;\n" 7296 "}\n" 7297 "+ (id)init {\n}\n" 7298 "@end"); 7299 7300 verifyFormat("@implementation Foo\n" 7301 "+ (id)init {\n" 7302 " if (true)\n" 7303 " return nil;\n" 7304 "}\n" 7305 "// Look, a comment!\n" 7306 "- (int)answerWith:(int)i {\n" 7307 " return i;\n" 7308 "}\n" 7309 "+ (int)answerWith:(int)i {\n" 7310 " return i;\n" 7311 "}\n" 7312 "@end"); 7313 7314 verifyFormat("@implementation Foo\n" 7315 "@end\n" 7316 "@implementation Bar\n" 7317 "@end"); 7318 7319 EXPECT_EQ("@implementation Foo : Bar\n" 7320 "+ (id)init {\n}\n" 7321 "- (void)foo {\n}\n" 7322 "@end", 7323 format("@implementation Foo : Bar\n" 7324 "+(id)init{}\n" 7325 "-(void)foo{}\n" 7326 "@end")); 7327 7328 verifyFormat("@implementation Foo {\n" 7329 " int _i;\n" 7330 "}\n" 7331 "+ (id)init {\n}\n" 7332 "@end"); 7333 7334 verifyFormat("@implementation Foo : Bar {\n" 7335 " int _i;\n" 7336 "}\n" 7337 "+ (id)init {\n}\n" 7338 "@end"); 7339 7340 verifyFormat("@implementation Foo (HackStuff)\n" 7341 "+ (id)init {\n}\n" 7342 "@end"); 7343 verifyFormat("@implementation ObjcClass\n" 7344 "- (void)method;\n" 7345 "{}\n" 7346 "@end"); 7347 } 7348 7349 TEST_F(FormatTest, FormatObjCProtocol) { 7350 verifyFormat("@protocol Foo\n" 7351 "@property(weak) id delegate;\n" 7352 "- (NSUInteger)numberOfThings;\n" 7353 "@end"); 7354 7355 verifyFormat("@protocol MyProtocol <NSObject>\n" 7356 "- (NSUInteger)numberOfThings;\n" 7357 "@end"); 7358 7359 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7360 "- (NSUInteger)numberOfThings;\n" 7361 "@end"); 7362 7363 verifyFormat("@protocol Foo;\n" 7364 "@protocol Bar;\n"); 7365 7366 verifyFormat("@protocol Foo\n" 7367 "@end\n" 7368 "@protocol Bar\n" 7369 "@end"); 7370 7371 verifyFormat("@protocol myProtocol\n" 7372 "- (void)mandatoryWithInt:(int)i;\n" 7373 "@optional\n" 7374 "- (void)optional;\n" 7375 "@required\n" 7376 "- (void)required;\n" 7377 "@optional\n" 7378 "@property(assign) int madProp;\n" 7379 "@end\n"); 7380 7381 verifyFormat("@property(nonatomic, assign, readonly)\n" 7382 " int *looooooooooooooooooooooooooooongNumber;\n" 7383 "@property(nonatomic, assign, readonly)\n" 7384 " NSString *looooooooooooooooooooooooooooongName;"); 7385 7386 verifyFormat("@implementation PR18406\n" 7387 "}\n" 7388 "@end"); 7389 } 7390 7391 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7392 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7393 " rect:(NSRect)theRect\n" 7394 " interval:(float)theInterval {\n" 7395 "}"); 7396 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7397 " longKeyword:(NSRect)theRect\n" 7398 " longerKeyword:(float)theInterval\n" 7399 " error:(NSError **)theError {\n" 7400 "}"); 7401 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7402 " longKeyword:(NSRect)theRect\n" 7403 " evenLongerKeyword:(float)theInterval\n" 7404 " error:(NSError **)theError {\n" 7405 "}"); 7406 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7407 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7408 " NS_DESIGNATED_INITIALIZER;", 7409 getLLVMStyleWithColumns(60)); 7410 7411 // Continuation indent width should win over aligning colons if the function 7412 // name is long. 7413 FormatStyle continuationStyle = getGoogleStyle(); 7414 continuationStyle.ColumnLimit = 40; 7415 continuationStyle.IndentWrappedFunctionNames = true; 7416 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7417 " dontAlignNamef:(NSRect)theRect {\n" 7418 "}", 7419 continuationStyle); 7420 7421 // Make sure we don't break aligning for short parameter names. 7422 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7423 " aShortf:(NSRect)theRect {\n" 7424 "}", 7425 continuationStyle); 7426 } 7427 7428 TEST_F(FormatTest, FormatObjCMethodExpr) { 7429 verifyFormat("[foo bar:baz];"); 7430 verifyFormat("return [foo bar:baz];"); 7431 verifyFormat("return (a)[foo bar:baz];"); 7432 verifyFormat("f([foo bar:baz]);"); 7433 verifyFormat("f(2, [foo bar:baz]);"); 7434 verifyFormat("f(2, a ? b : c);"); 7435 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7436 7437 // Unary operators. 7438 verifyFormat("int a = +[foo bar:baz];"); 7439 verifyFormat("int a = -[foo bar:baz];"); 7440 verifyFormat("int a = ![foo bar:baz];"); 7441 verifyFormat("int a = ~[foo bar:baz];"); 7442 verifyFormat("int a = ++[foo bar:baz];"); 7443 verifyFormat("int a = --[foo bar:baz];"); 7444 verifyFormat("int a = sizeof [foo bar:baz];"); 7445 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7446 verifyFormat("int a = &[foo bar:baz];"); 7447 verifyFormat("int a = *[foo bar:baz];"); 7448 // FIXME: Make casts work, without breaking f()[4]. 7449 // verifyFormat("int a = (int)[foo bar:baz];"); 7450 // verifyFormat("return (int)[foo bar:baz];"); 7451 // verifyFormat("(void)[foo bar:baz];"); 7452 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7453 7454 // Binary operators. 7455 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7456 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7457 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7458 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7459 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7460 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7461 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7462 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7463 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7464 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7465 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7466 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7467 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7468 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7469 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7470 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7471 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7472 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7473 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7474 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7475 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7476 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7477 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7478 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7479 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7480 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7481 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7482 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7483 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7484 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7485 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7486 // Whew! 7487 7488 verifyFormat("return in[42];"); 7489 verifyFormat("for (auto v : in[1]) {\n}"); 7490 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7491 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7492 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7493 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7494 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7495 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7496 "}"); 7497 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7498 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7499 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7500 7501 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7502 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7503 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7504 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7505 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7506 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7507 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7508 7509 verifyFormat("arr[[self indexForFoo:a]];"); 7510 verifyFormat("throw [self errorFor:a];"); 7511 verifyFormat("@throw [self errorFor:a];"); 7512 7513 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7514 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7515 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7516 7517 // This tests that the formatter doesn't break after "backing" but before ":", 7518 // which would be at 80 columns. 7519 verifyFormat( 7520 "void f() {\n" 7521 " if ((self = [super initWithContentRect:contentRect\n" 7522 " styleMask:styleMask ?: otherMask\n" 7523 " backing:NSBackingStoreBuffered\n" 7524 " defer:YES]))"); 7525 7526 verifyFormat( 7527 "[foo checkThatBreakingAfterColonWorksOk:\n" 7528 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7529 7530 verifyFormat("[myObj short:arg1 // Force line break\n" 7531 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7532 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7533 " error:arg4];"); 7534 verifyFormat( 7535 "void f() {\n" 7536 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7537 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7538 " pos.width(), pos.height())\n" 7539 " styleMask:NSBorderlessWindowMask\n" 7540 " backing:NSBackingStoreBuffered\n" 7541 " defer:NO]);\n" 7542 "}"); 7543 verifyFormat( 7544 "void f() {\n" 7545 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7546 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7547 " pos.width(), pos.height())\n" 7548 " syeMask:NSBorderlessWindowMask\n" 7549 " bking:NSBackingStoreBuffered\n" 7550 " der:NO]);\n" 7551 "}", 7552 getLLVMStyleWithColumns(70)); 7553 verifyFormat( 7554 "void f() {\n" 7555 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7556 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7557 " pos.width(), pos.height())\n" 7558 " styleMask:NSBorderlessWindowMask\n" 7559 " backing:NSBackingStoreBuffered\n" 7560 " defer:NO]);\n" 7561 "}", 7562 getChromiumStyle(FormatStyle::LK_Cpp)); 7563 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7564 " with:contentsNativeView];"); 7565 7566 verifyFormat( 7567 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7568 " owner:nillllll];"); 7569 7570 verifyFormat( 7571 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7572 " forType:kBookmarkButtonDragType];"); 7573 7574 verifyFormat("[defaultCenter addObserver:self\n" 7575 " selector:@selector(willEnterFullscreen)\n" 7576 " name:kWillEnterFullscreenNotification\n" 7577 " object:nil];"); 7578 verifyFormat("[image_rep drawInRect:drawRect\n" 7579 " fromRect:NSZeroRect\n" 7580 " operation:NSCompositeCopy\n" 7581 " fraction:1.0\n" 7582 " respectFlipped:NO\n" 7583 " hints:nil];"); 7584 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7586 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7588 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7589 " aaaaaaaaaaaaaaaaaaaaaa];"); 7590 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7591 " .aaaaaaaa];", // FIXME: Indentation seems off. 7592 getLLVMStyleWithColumns(60)); 7593 7594 verifyFormat( 7595 "scoped_nsobject<NSTextField> message(\n" 7596 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7597 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7598 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7599 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7600 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7601 " aaaa:bbb];"); 7602 verifyFormat("[self param:function( //\n" 7603 " parameter)]"); 7604 verifyFormat( 7605 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7606 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7607 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7608 7609 // FIXME: This violates the column limit. 7610 verifyFormat( 7611 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7612 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7613 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7614 getLLVMStyleWithColumns(60)); 7615 7616 // Variadic parameters. 7617 verifyFormat( 7618 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7619 verifyFormat( 7620 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7621 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7622 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7623 verifyFormat("[self // break\n" 7624 " a:a\n" 7625 " aaa:aaa];"); 7626 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7627 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7628 } 7629 7630 TEST_F(FormatTest, ObjCAt) { 7631 verifyFormat("@autoreleasepool"); 7632 verifyFormat("@catch"); 7633 verifyFormat("@class"); 7634 verifyFormat("@compatibility_alias"); 7635 verifyFormat("@defs"); 7636 verifyFormat("@dynamic"); 7637 verifyFormat("@encode"); 7638 verifyFormat("@end"); 7639 verifyFormat("@finally"); 7640 verifyFormat("@implementation"); 7641 verifyFormat("@import"); 7642 verifyFormat("@interface"); 7643 verifyFormat("@optional"); 7644 verifyFormat("@package"); 7645 verifyFormat("@private"); 7646 verifyFormat("@property"); 7647 verifyFormat("@protected"); 7648 verifyFormat("@protocol"); 7649 verifyFormat("@public"); 7650 verifyFormat("@required"); 7651 verifyFormat("@selector"); 7652 verifyFormat("@synchronized"); 7653 verifyFormat("@synthesize"); 7654 verifyFormat("@throw"); 7655 verifyFormat("@try"); 7656 7657 EXPECT_EQ("@interface", format("@ interface")); 7658 7659 // The precise formatting of this doesn't matter, nobody writes code like 7660 // this. 7661 verifyFormat("@ /*foo*/ interface"); 7662 } 7663 7664 TEST_F(FormatTest, ObjCSnippets) { 7665 verifyFormat("@autoreleasepool {\n" 7666 " foo();\n" 7667 "}"); 7668 verifyFormat("@class Foo, Bar;"); 7669 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7670 verifyFormat("@dynamic textColor;"); 7671 verifyFormat("char *buf1 = @encode(int *);"); 7672 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7673 verifyFormat("char *buf1 = @encode(int **);"); 7674 verifyFormat("Protocol *proto = @protocol(p1);"); 7675 verifyFormat("SEL s = @selector(foo:);"); 7676 verifyFormat("@synchronized(self) {\n" 7677 " f();\n" 7678 "}"); 7679 7680 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7681 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7682 7683 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7684 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7685 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7686 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7687 getMozillaStyle()); 7688 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7689 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7690 getWebKitStyle()); 7691 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7692 7693 verifyFormat("@import foo.bar;\n" 7694 "@import baz;"); 7695 } 7696 7697 TEST_F(FormatTest, ObjCForIn) { 7698 verifyFormat("- (void)test {\n" 7699 " for (NSString *n in arrayOfStrings) {\n" 7700 " foo(n);\n" 7701 " }\n" 7702 "}"); 7703 verifyFormat("- (void)test {\n" 7704 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7705 " foo(n);\n" 7706 " }\n" 7707 "}"); 7708 } 7709 7710 TEST_F(FormatTest, ObjCLiterals) { 7711 verifyFormat("@\"String\""); 7712 verifyFormat("@1"); 7713 verifyFormat("@+4.8"); 7714 verifyFormat("@-4"); 7715 verifyFormat("@1LL"); 7716 verifyFormat("@.5"); 7717 verifyFormat("@'c'"); 7718 verifyFormat("@true"); 7719 7720 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7721 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7722 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7723 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7724 7725 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7726 } 7727 7728 TEST_F(FormatTest, ObjCDictLiterals) { 7729 verifyFormat("@{"); 7730 verifyFormat("@{}"); 7731 verifyFormat("@{@\"one\" : @1}"); 7732 verifyFormat("return @{@\"one\" : @1;"); 7733 verifyFormat("@{@\"one\" : @1}"); 7734 7735 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7736 verifyFormat("@{\n" 7737 " @\"one\" : @{@2 : @1},\n" 7738 "}"); 7739 7740 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7741 verifyIncompleteFormat("[self setDict:@{}"); 7742 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7743 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7744 verifyFormat( 7745 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7746 verifyFormat( 7747 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7748 7749 verifyFormat("NSDictionary *d = @{\n" 7750 " @\"nam\" : NSUserNam(),\n" 7751 " @\"dte\" : [NSDate date],\n" 7752 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7753 "};"); 7754 verifyFormat( 7755 "@{\n" 7756 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7757 "regularFont,\n" 7758 "};"); 7759 verifyGoogleFormat( 7760 "@{\n" 7761 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7762 "regularFont,\n" 7763 "};"); 7764 verifyFormat( 7765 "@{\n" 7766 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7767 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7768 "};"); 7769 7770 // We should try to be robust in case someone forgets the "@". 7771 verifyFormat("NSDictionary *d = {\n" 7772 " @\"nam\" : NSUserNam(),\n" 7773 " @\"dte\" : [NSDate date],\n" 7774 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7775 "};"); 7776 verifyFormat("NSMutableDictionary *dictionary =\n" 7777 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7778 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7779 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7780 " cccccccccccccccc : ccccccccccccccc\n" 7781 " }];"); 7782 7783 // Ensure that casts before the key are kept on the same line as the key. 7784 verifyFormat( 7785 "NSDictionary *d = @{\n" 7786 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7787 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7788 "};"); 7789 } 7790 7791 TEST_F(FormatTest, ObjCArrayLiterals) { 7792 verifyIncompleteFormat("@["); 7793 verifyFormat("@[]"); 7794 verifyFormat( 7795 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7796 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7797 verifyFormat("NSArray *array = @[ [foo description] ];"); 7798 7799 verifyFormat( 7800 "NSArray *some_variable = @[\n" 7801 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7802 " @\"aaaaaaaaaaaaaaaaa\",\n" 7803 " @\"aaaaaaaaaaaaaaaaa\",\n" 7804 " @\"aaaaaaaaaaaaaaaaa\",\n" 7805 "];"); 7806 verifyFormat( 7807 "NSArray *some_variable = @[\n" 7808 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7809 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7810 "];"); 7811 verifyFormat("NSArray *some_variable = @[\n" 7812 " @\"aaaaaaaaaaaaaaaaa\",\n" 7813 " @\"aaaaaaaaaaaaaaaaa\",\n" 7814 " @\"aaaaaaaaaaaaaaaaa\",\n" 7815 " @\"aaaaaaaaaaaaaaaaa\",\n" 7816 "];"); 7817 verifyFormat("NSArray *array = @[\n" 7818 " @\"a\",\n" 7819 " @\"a\",\n" // Trailing comma -> one per line. 7820 "];"); 7821 7822 // We should try to be robust in case someone forgets the "@". 7823 verifyFormat("NSArray *some_variable = [\n" 7824 " @\"aaaaaaaaaaaaaaaaa\",\n" 7825 " @\"aaaaaaaaaaaaaaaaa\",\n" 7826 " @\"aaaaaaaaaaaaaaaaa\",\n" 7827 " @\"aaaaaaaaaaaaaaaaa\",\n" 7828 "];"); 7829 verifyFormat( 7830 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7831 " index:(NSUInteger)index\n" 7832 " nonDigitAttributes:\n" 7833 " (NSDictionary *)noDigitAttributes;"); 7834 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7835 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7836 "]];"); 7837 } 7838 7839 TEST_F(FormatTest, BreaksStringLiterals) { 7840 EXPECT_EQ("\"some text \"\n" 7841 "\"other\";", 7842 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7843 EXPECT_EQ("\"some text \"\n" 7844 "\"other\";", 7845 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7846 EXPECT_EQ( 7847 "#define A \\\n" 7848 " \"some \" \\\n" 7849 " \"text \" \\\n" 7850 " \"other\";", 7851 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7852 EXPECT_EQ( 7853 "#define A \\\n" 7854 " \"so \" \\\n" 7855 " \"text \" \\\n" 7856 " \"other\";", 7857 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7858 7859 EXPECT_EQ("\"some text\"", 7860 format("\"some text\"", getLLVMStyleWithColumns(1))); 7861 EXPECT_EQ("\"some text\"", 7862 format("\"some text\"", getLLVMStyleWithColumns(11))); 7863 EXPECT_EQ("\"some \"\n" 7864 "\"text\"", 7865 format("\"some text\"", getLLVMStyleWithColumns(10))); 7866 EXPECT_EQ("\"some \"\n" 7867 "\"text\"", 7868 format("\"some text\"", getLLVMStyleWithColumns(7))); 7869 EXPECT_EQ("\"some\"\n" 7870 "\" tex\"\n" 7871 "\"t\"", 7872 format("\"some text\"", getLLVMStyleWithColumns(6))); 7873 EXPECT_EQ("\"some\"\n" 7874 "\" tex\"\n" 7875 "\" and\"", 7876 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7877 EXPECT_EQ("\"some\"\n" 7878 "\"/tex\"\n" 7879 "\"/and\"", 7880 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7881 7882 EXPECT_EQ("variable =\n" 7883 " \"long string \"\n" 7884 " \"literal\";", 7885 format("variable = \"long string literal\";", 7886 getLLVMStyleWithColumns(20))); 7887 7888 EXPECT_EQ("variable = f(\n" 7889 " \"long string \"\n" 7890 " \"literal\",\n" 7891 " short,\n" 7892 " loooooooooooooooooooong);", 7893 format("variable = f(\"long string literal\", short, " 7894 "loooooooooooooooooooong);", 7895 getLLVMStyleWithColumns(20))); 7896 7897 EXPECT_EQ( 7898 "f(g(\"long string \"\n" 7899 " \"literal\"),\n" 7900 " b);", 7901 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7902 EXPECT_EQ("f(g(\"long string \"\n" 7903 " \"literal\",\n" 7904 " a),\n" 7905 " b);", 7906 format("f(g(\"long string literal\", a), b);", 7907 getLLVMStyleWithColumns(20))); 7908 EXPECT_EQ( 7909 "f(\"one two\".split(\n" 7910 " variable));", 7911 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7912 EXPECT_EQ("f(\"one two three four five six \"\n" 7913 " \"seven\".split(\n" 7914 " really_looooong_variable));", 7915 format("f(\"one two three four five six seven\"." 7916 "split(really_looooong_variable));", 7917 getLLVMStyleWithColumns(33))); 7918 7919 EXPECT_EQ("f(\"some \"\n" 7920 " \"text\",\n" 7921 " other);", 7922 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7923 7924 // Only break as a last resort. 7925 verifyFormat( 7926 "aaaaaaaaaaaaaaaaaaaa(\n" 7927 " aaaaaaaaaaaaaaaaaaaa,\n" 7928 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7929 7930 EXPECT_EQ("\"splitmea\"\n" 7931 "\"trandomp\"\n" 7932 "\"oint\"", 7933 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7934 7935 EXPECT_EQ("\"split/\"\n" 7936 "\"pathat/\"\n" 7937 "\"slashes\"", 7938 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7939 7940 EXPECT_EQ("\"split/\"\n" 7941 "\"pathat/\"\n" 7942 "\"slashes\"", 7943 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7944 EXPECT_EQ("\"split at \"\n" 7945 "\"spaces/at/\"\n" 7946 "\"slashes.at.any$\"\n" 7947 "\"non-alphanumeric%\"\n" 7948 "\"1111111111characte\"\n" 7949 "\"rs\"", 7950 format("\"split at " 7951 "spaces/at/" 7952 "slashes.at." 7953 "any$non-" 7954 "alphanumeric%" 7955 "1111111111characte" 7956 "rs\"", 7957 getLLVMStyleWithColumns(20))); 7958 7959 // Verify that splitting the strings understands 7960 // Style::AlwaysBreakBeforeMultilineStrings. 7961 EXPECT_EQ( 7962 "aaaaaaaaaaaa(\n" 7963 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7964 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7965 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7966 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7967 "aaaaaaaaaaaaaaaaaaaaaa\");", 7968 getGoogleStyle())); 7969 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7970 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7971 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7972 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7973 "aaaaaaaaaaaaaaaaaaaaaa\";", 7974 getGoogleStyle())); 7975 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7976 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7977 format("llvm::outs() << " 7978 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7979 "aaaaaaaaaaaaaaaaaaa\";")); 7980 EXPECT_EQ("ffff(\n" 7981 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7982 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7983 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7984 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7985 getGoogleStyle())); 7986 7987 FormatStyle Style = getLLVMStyleWithColumns(12); 7988 Style.BreakStringLiterals = false; 7989 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7990 7991 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7992 AlignLeft.AlignEscapedNewlinesLeft = true; 7993 EXPECT_EQ("#define A \\\n" 7994 " \"some \" \\\n" 7995 " \"text \" \\\n" 7996 " \"other\";", 7997 format("#define A \"some text other\";", AlignLeft)); 7998 } 7999 8000 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8001 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8002 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8003 EXPECT_EQ("int i = a(b());", 8004 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8005 } 8006 8007 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8008 EXPECT_EQ( 8009 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8010 "(\n" 8011 " \"x\t\");", 8012 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8013 "aaaaaaa(" 8014 "\"x\t\");")); 8015 } 8016 8017 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8018 EXPECT_EQ( 8019 "u8\"utf8 string \"\n" 8020 "u8\"literal\";", 8021 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8022 EXPECT_EQ( 8023 "u\"utf16 string \"\n" 8024 "u\"literal\";", 8025 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8026 EXPECT_EQ( 8027 "U\"utf32 string \"\n" 8028 "U\"literal\";", 8029 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8030 EXPECT_EQ("L\"wide string \"\n" 8031 "L\"literal\";", 8032 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8033 EXPECT_EQ("@\"NSString \"\n" 8034 "@\"literal\";", 8035 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8036 8037 // This input makes clang-format try to split the incomplete unicode escape 8038 // sequence, which used to lead to a crasher. 8039 verifyNoCrash( 8040 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8041 getLLVMStyleWithColumns(60)); 8042 } 8043 8044 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8045 FormatStyle Style = getGoogleStyleWithColumns(15); 8046 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8047 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8048 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8049 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8050 EXPECT_EQ("u8R\"x(raw literal)x\";", 8051 format("u8R\"x(raw literal)x\";", Style)); 8052 } 8053 8054 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8055 FormatStyle Style = getLLVMStyleWithColumns(20); 8056 EXPECT_EQ( 8057 "_T(\"aaaaaaaaaaaaaa\")\n" 8058 "_T(\"aaaaaaaaaaaaaa\")\n" 8059 "_T(\"aaaaaaaaaaaa\")", 8060 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8061 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8062 " _T(\"aaaaaa\"),\n" 8063 " z);", 8064 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8065 8066 // FIXME: Handle embedded spaces in one iteration. 8067 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8068 // "_T(\"aaaaaaaaaaaaa\")\n" 8069 // "_T(\"aaaaaaaaaaaaa\")\n" 8070 // "_T(\"a\")", 8071 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8072 // getLLVMStyleWithColumns(20))); 8073 EXPECT_EQ( 8074 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8075 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8076 EXPECT_EQ("f(\n" 8077 "#if !TEST\n" 8078 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8079 "#endif\n" 8080 " );", 8081 format("f(\n" 8082 "#if !TEST\n" 8083 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8084 "#endif\n" 8085 ");")); 8086 EXPECT_EQ("f(\n" 8087 "\n" 8088 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8089 format("f(\n" 8090 "\n" 8091 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8092 } 8093 8094 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8095 EXPECT_EQ( 8096 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8099 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8102 } 8103 8104 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8105 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8106 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8107 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8108 "multiline raw string literal xxxxxxxxxxxxxx\n" 8109 ")x\",\n" 8110 " a),\n" 8111 " b);", 8112 format("fffffffffff(g(R\"x(\n" 8113 "multiline raw string literal xxxxxxxxxxxxxx\n" 8114 ")x\", a), b);", 8115 getGoogleStyleWithColumns(20))); 8116 EXPECT_EQ("fffffffffff(\n" 8117 " g(R\"x(qqq\n" 8118 "multiline raw string literal xxxxxxxxxxxxxx\n" 8119 ")x\",\n" 8120 " a),\n" 8121 " b);", 8122 format("fffffffffff(g(R\"x(qqq\n" 8123 "multiline raw string literal xxxxxxxxxxxxxx\n" 8124 ")x\", a), b);", 8125 getGoogleStyleWithColumns(20))); 8126 8127 EXPECT_EQ("fffffffffff(R\"x(\n" 8128 "multiline raw string literal xxxxxxxxxxxxxx\n" 8129 ")x\");", 8130 format("fffffffffff(R\"x(\n" 8131 "multiline raw string literal xxxxxxxxxxxxxx\n" 8132 ")x\");", 8133 getGoogleStyleWithColumns(20))); 8134 EXPECT_EQ("fffffffffff(R\"x(\n" 8135 "multiline raw string literal xxxxxxxxxxxxxx\n" 8136 ")x\" + bbbbbb);", 8137 format("fffffffffff(R\"x(\n" 8138 "multiline raw string literal xxxxxxxxxxxxxx\n" 8139 ")x\" + bbbbbb);", 8140 getGoogleStyleWithColumns(20))); 8141 EXPECT_EQ("fffffffffff(\n" 8142 " R\"x(\n" 8143 "multiline raw string literal xxxxxxxxxxxxxx\n" 8144 ")x\" +\n" 8145 " bbbbbb);", 8146 format("fffffffffff(\n" 8147 " R\"x(\n" 8148 "multiline raw string literal xxxxxxxxxxxxxx\n" 8149 ")x\" + bbbbbb);", 8150 getGoogleStyleWithColumns(20))); 8151 } 8152 8153 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8154 verifyFormat("string a = \"unterminated;"); 8155 EXPECT_EQ("function(\"unterminated,\n" 8156 " OtherParameter);", 8157 format("function( \"unterminated,\n" 8158 " OtherParameter);")); 8159 } 8160 8161 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8162 FormatStyle Style = getLLVMStyle(); 8163 Style.Standard = FormatStyle::LS_Cpp03; 8164 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8165 format("#define x(_a) printf(\"foo\"_a);", Style)); 8166 } 8167 8168 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8169 8170 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8171 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8172 " \"ddeeefff\");", 8173 format("someFunction(\"aaabbbcccdddeeefff\");", 8174 getLLVMStyleWithColumns(25))); 8175 EXPECT_EQ("someFunction1234567890(\n" 8176 " \"aaabbbcccdddeeefff\");", 8177 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8178 getLLVMStyleWithColumns(26))); 8179 EXPECT_EQ("someFunction1234567890(\n" 8180 " \"aaabbbcccdddeeeff\"\n" 8181 " \"f\");", 8182 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8183 getLLVMStyleWithColumns(25))); 8184 EXPECT_EQ("someFunction1234567890(\n" 8185 " \"aaabbbcccdddeeeff\"\n" 8186 " \"f\");", 8187 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8188 getLLVMStyleWithColumns(24))); 8189 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8190 " \"ddde \"\n" 8191 " \"efff\");", 8192 format("someFunction(\"aaabbbcc ddde efff\");", 8193 getLLVMStyleWithColumns(25))); 8194 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8195 " \"ddeeefff\");", 8196 format("someFunction(\"aaabbbccc ddeeefff\");", 8197 getLLVMStyleWithColumns(25))); 8198 EXPECT_EQ("someFunction1234567890(\n" 8199 " \"aaabb \"\n" 8200 " \"cccdddeeefff\");", 8201 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8202 getLLVMStyleWithColumns(25))); 8203 EXPECT_EQ("#define A \\\n" 8204 " string s = \\\n" 8205 " \"123456789\" \\\n" 8206 " \"0\"; \\\n" 8207 " int i;", 8208 format("#define A string s = \"1234567890\"; int i;", 8209 getLLVMStyleWithColumns(20))); 8210 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8211 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8212 " \"dddeeeff\"\n" 8213 " \"f\");", 8214 format("someFunction(\"aaabbbcc dddeeefff\");", 8215 getLLVMStyleWithColumns(25))); 8216 } 8217 8218 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8219 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8220 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8221 EXPECT_EQ("\"test\"\n" 8222 "\"\\n\"", 8223 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8224 EXPECT_EQ("\"tes\\\\\"\n" 8225 "\"n\"", 8226 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8227 EXPECT_EQ("\"\\\\\\\\\"\n" 8228 "\"\\n\"", 8229 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8230 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8231 EXPECT_EQ("\"\\uff01\"\n" 8232 "\"test\"", 8233 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8234 EXPECT_EQ("\"\\Uff01ff02\"", 8235 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8236 EXPECT_EQ("\"\\x000000000001\"\n" 8237 "\"next\"", 8238 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8239 EXPECT_EQ("\"\\x000000000001next\"", 8240 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8241 EXPECT_EQ("\"\\x000000000001\"", 8242 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8243 EXPECT_EQ("\"test\"\n" 8244 "\"\\000000\"\n" 8245 "\"000001\"", 8246 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8247 EXPECT_EQ("\"test\\000\"\n" 8248 "\"00000000\"\n" 8249 "\"1\"", 8250 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8251 } 8252 8253 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8254 verifyFormat("void f() {\n" 8255 " return g() {}\n" 8256 " void h() {}"); 8257 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8258 "g();\n" 8259 "}"); 8260 } 8261 8262 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8263 verifyFormat( 8264 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8265 } 8266 8267 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8268 verifyFormat("class X {\n" 8269 " void f() {\n" 8270 " }\n" 8271 "};", 8272 getLLVMStyleWithColumns(12)); 8273 } 8274 8275 TEST_F(FormatTest, ConfigurableIndentWidth) { 8276 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8277 EightIndent.IndentWidth = 8; 8278 EightIndent.ContinuationIndentWidth = 8; 8279 verifyFormat("void f() {\n" 8280 " someFunction();\n" 8281 " if (true) {\n" 8282 " f();\n" 8283 " }\n" 8284 "}", 8285 EightIndent); 8286 verifyFormat("class X {\n" 8287 " void f() {\n" 8288 " }\n" 8289 "};", 8290 EightIndent); 8291 verifyFormat("int x[] = {\n" 8292 " call(),\n" 8293 " call()};", 8294 EightIndent); 8295 } 8296 8297 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8298 verifyFormat("double\n" 8299 "f();", 8300 getLLVMStyleWithColumns(8)); 8301 } 8302 8303 TEST_F(FormatTest, ConfigurableUseOfTab) { 8304 FormatStyle Tab = getLLVMStyleWithColumns(42); 8305 Tab.IndentWidth = 8; 8306 Tab.UseTab = FormatStyle::UT_Always; 8307 Tab.AlignEscapedNewlinesLeft = true; 8308 8309 EXPECT_EQ("if (aaaaaaaa && // q\n" 8310 " bb)\t\t// w\n" 8311 "\t;", 8312 format("if (aaaaaaaa &&// q\n" 8313 "bb)// w\n" 8314 ";", 8315 Tab)); 8316 EXPECT_EQ("if (aaa && bbb) // w\n" 8317 "\t;", 8318 format("if(aaa&&bbb)// w\n" 8319 ";", 8320 Tab)); 8321 8322 verifyFormat("class X {\n" 8323 "\tvoid f() {\n" 8324 "\t\tsomeFunction(parameter1,\n" 8325 "\t\t\t parameter2);\n" 8326 "\t}\n" 8327 "};", 8328 Tab); 8329 verifyFormat("#define A \\\n" 8330 "\tvoid f() { \\\n" 8331 "\t\tsomeFunction( \\\n" 8332 "\t\t parameter1, \\\n" 8333 "\t\t parameter2); \\\n" 8334 "\t}", 8335 Tab); 8336 8337 Tab.TabWidth = 4; 8338 Tab.IndentWidth = 8; 8339 verifyFormat("class TabWidth4Indent8 {\n" 8340 "\t\tvoid f() {\n" 8341 "\t\t\t\tsomeFunction(parameter1,\n" 8342 "\t\t\t\t\t\t\t parameter2);\n" 8343 "\t\t}\n" 8344 "};", 8345 Tab); 8346 8347 Tab.TabWidth = 4; 8348 Tab.IndentWidth = 4; 8349 verifyFormat("class TabWidth4Indent4 {\n" 8350 "\tvoid f() {\n" 8351 "\t\tsomeFunction(parameter1,\n" 8352 "\t\t\t\t\t parameter2);\n" 8353 "\t}\n" 8354 "};", 8355 Tab); 8356 8357 Tab.TabWidth = 8; 8358 Tab.IndentWidth = 4; 8359 verifyFormat("class TabWidth8Indent4 {\n" 8360 " void f() {\n" 8361 "\tsomeFunction(parameter1,\n" 8362 "\t\t parameter2);\n" 8363 " }\n" 8364 "};", 8365 Tab); 8366 8367 Tab.TabWidth = 8; 8368 Tab.IndentWidth = 8; 8369 EXPECT_EQ("/*\n" 8370 "\t a\t\tcomment\n" 8371 "\t in multiple lines\n" 8372 " */", 8373 format(" /*\t \t \n" 8374 " \t \t a\t\tcomment\t \t\n" 8375 " \t \t in multiple lines\t\n" 8376 " \t */", 8377 Tab)); 8378 8379 Tab.UseTab = FormatStyle::UT_ForIndentation; 8380 verifyFormat("{\n" 8381 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8382 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8383 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8384 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8385 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8386 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8387 "};", 8388 Tab); 8389 verifyFormat("enum AA {\n" 8390 "\ta1, // Force multiple lines\n" 8391 "\ta2,\n" 8392 "\ta3\n" 8393 "};", 8394 Tab); 8395 EXPECT_EQ("if (aaaaaaaa && // q\n" 8396 " bb) // w\n" 8397 "\t;", 8398 format("if (aaaaaaaa &&// q\n" 8399 "bb)// w\n" 8400 ";", 8401 Tab)); 8402 verifyFormat("class X {\n" 8403 "\tvoid f() {\n" 8404 "\t\tsomeFunction(parameter1,\n" 8405 "\t\t parameter2);\n" 8406 "\t}\n" 8407 "};", 8408 Tab); 8409 verifyFormat("{\n" 8410 "\tQ(\n" 8411 "\t {\n" 8412 "\t\t int a;\n" 8413 "\t\t someFunction(aaaaaaaa,\n" 8414 "\t\t bbbbbbb);\n" 8415 "\t },\n" 8416 "\t p);\n" 8417 "}", 8418 Tab); 8419 EXPECT_EQ("{\n" 8420 "\t/* aaaa\n" 8421 "\t bbbb */\n" 8422 "}", 8423 format("{\n" 8424 "/* aaaa\n" 8425 " bbbb */\n" 8426 "}", 8427 Tab)); 8428 EXPECT_EQ("{\n" 8429 "\t/*\n" 8430 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8431 "\t bbbbbbbbbbbbb\n" 8432 "\t*/\n" 8433 "}", 8434 format("{\n" 8435 "/*\n" 8436 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8437 "*/\n" 8438 "}", 8439 Tab)); 8440 EXPECT_EQ("{\n" 8441 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8442 "\t// bbbbbbbbbbbbb\n" 8443 "}", 8444 format("{\n" 8445 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8446 "}", 8447 Tab)); 8448 EXPECT_EQ("{\n" 8449 "\t/*\n" 8450 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8451 "\t bbbbbbbbbbbbb\n" 8452 "\t*/\n" 8453 "}", 8454 format("{\n" 8455 "\t/*\n" 8456 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8457 "\t*/\n" 8458 "}", 8459 Tab)); 8460 EXPECT_EQ("{\n" 8461 "\t/*\n" 8462 "\n" 8463 "\t*/\n" 8464 "}", 8465 format("{\n" 8466 "\t/*\n" 8467 "\n" 8468 "\t*/\n" 8469 "}", 8470 Tab)); 8471 EXPECT_EQ("{\n" 8472 "\t/*\n" 8473 " asdf\n" 8474 "\t*/\n" 8475 "}", 8476 format("{\n" 8477 "\t/*\n" 8478 " asdf\n" 8479 "\t*/\n" 8480 "}", 8481 Tab)); 8482 8483 Tab.UseTab = FormatStyle::UT_Never; 8484 EXPECT_EQ("/*\n" 8485 " a\t\tcomment\n" 8486 " in multiple lines\n" 8487 " */", 8488 format(" /*\t \t \n" 8489 " \t \t a\t\tcomment\t \t\n" 8490 " \t \t in multiple lines\t\n" 8491 " \t */", 8492 Tab)); 8493 EXPECT_EQ("/* some\n" 8494 " comment */", 8495 format(" \t \t /* some\n" 8496 " \t \t comment */", 8497 Tab)); 8498 EXPECT_EQ("int a; /* some\n" 8499 " comment */", 8500 format(" \t \t int a; /* some\n" 8501 " \t \t comment */", 8502 Tab)); 8503 8504 EXPECT_EQ("int a; /* some\n" 8505 "comment */", 8506 format(" \t \t int\ta; /* some\n" 8507 " \t \t comment */", 8508 Tab)); 8509 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8510 " comment */", 8511 format(" \t \t f(\"\t\t\"); /* some\n" 8512 " \t \t comment */", 8513 Tab)); 8514 EXPECT_EQ("{\n" 8515 " /*\n" 8516 " * Comment\n" 8517 " */\n" 8518 " int i;\n" 8519 "}", 8520 format("{\n" 8521 "\t/*\n" 8522 "\t * Comment\n" 8523 "\t */\n" 8524 "\t int i;\n" 8525 "}")); 8526 } 8527 8528 TEST_F(FormatTest, CalculatesOriginalColumn) { 8529 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8530 "q\"; /* some\n" 8531 " comment */", 8532 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8533 "q\"; /* some\n" 8534 " comment */", 8535 getLLVMStyle())); 8536 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8537 "/* some\n" 8538 " comment */", 8539 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8540 " /* some\n" 8541 " comment */", 8542 getLLVMStyle())); 8543 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8544 "qqq\n" 8545 "/* some\n" 8546 " comment */", 8547 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8548 "qqq\n" 8549 " /* some\n" 8550 " comment */", 8551 getLLVMStyle())); 8552 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8553 "wwww; /* some\n" 8554 " comment */", 8555 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8556 "wwww; /* some\n" 8557 " comment */", 8558 getLLVMStyle())); 8559 } 8560 8561 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8562 FormatStyle NoSpace = getLLVMStyle(); 8563 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8564 8565 verifyFormat("while(true)\n" 8566 " continue;", 8567 NoSpace); 8568 verifyFormat("for(;;)\n" 8569 " continue;", 8570 NoSpace); 8571 verifyFormat("if(true)\n" 8572 " f();\n" 8573 "else if(true)\n" 8574 " f();", 8575 NoSpace); 8576 verifyFormat("do {\n" 8577 " do_something();\n" 8578 "} while(something());", 8579 NoSpace); 8580 verifyFormat("switch(x) {\n" 8581 "default:\n" 8582 " break;\n" 8583 "}", 8584 NoSpace); 8585 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8586 verifyFormat("size_t x = sizeof(x);", NoSpace); 8587 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8588 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8589 verifyFormat("alignas(128) char a[128];", NoSpace); 8590 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8591 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8592 verifyFormat("int f() throw(Deprecated);", NoSpace); 8593 verifyFormat("typedef void (*cb)(int);", NoSpace); 8594 verifyFormat("T A::operator()();", NoSpace); 8595 verifyFormat("X A::operator++(T);", NoSpace); 8596 8597 FormatStyle Space = getLLVMStyle(); 8598 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8599 8600 verifyFormat("int f ();", Space); 8601 verifyFormat("void f (int a, T b) {\n" 8602 " while (true)\n" 8603 " continue;\n" 8604 "}", 8605 Space); 8606 verifyFormat("if (true)\n" 8607 " f ();\n" 8608 "else if (true)\n" 8609 " f ();", 8610 Space); 8611 verifyFormat("do {\n" 8612 " do_something ();\n" 8613 "} while (something ());", 8614 Space); 8615 verifyFormat("switch (x) {\n" 8616 "default:\n" 8617 " break;\n" 8618 "}", 8619 Space); 8620 verifyFormat("A::A () : a (1) {}", Space); 8621 verifyFormat("void f () __attribute__ ((asdf));", Space); 8622 verifyFormat("*(&a + 1);\n" 8623 "&((&a)[1]);\n" 8624 "a[(b + c) * d];\n" 8625 "(((a + 1) * 2) + 3) * 4;", 8626 Space); 8627 verifyFormat("#define A(x) x", Space); 8628 verifyFormat("#define A (x) x", Space); 8629 verifyFormat("#if defined(x)\n" 8630 "#endif", 8631 Space); 8632 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8633 verifyFormat("size_t x = sizeof (x);", Space); 8634 verifyFormat("auto f (int x) -> decltype (x);", Space); 8635 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8636 verifyFormat("alignas (128) char a[128];", Space); 8637 verifyFormat("size_t x = alignof (MyType);", Space); 8638 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8639 verifyFormat("int f () throw (Deprecated);", Space); 8640 verifyFormat("typedef void (*cb) (int);", Space); 8641 verifyFormat("T A::operator() ();", Space); 8642 verifyFormat("X A::operator++ (T);", Space); 8643 } 8644 8645 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8646 FormatStyle Spaces = getLLVMStyle(); 8647 8648 Spaces.SpacesInParentheses = true; 8649 verifyFormat("call( x, y, z );", Spaces); 8650 verifyFormat("call();", Spaces); 8651 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8652 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8653 Spaces); 8654 verifyFormat("while ( (bool)1 )\n" 8655 " continue;", 8656 Spaces); 8657 verifyFormat("for ( ;; )\n" 8658 " continue;", 8659 Spaces); 8660 verifyFormat("if ( true )\n" 8661 " f();\n" 8662 "else if ( true )\n" 8663 " f();", 8664 Spaces); 8665 verifyFormat("do {\n" 8666 " do_something( (int)i );\n" 8667 "} while ( something() );", 8668 Spaces); 8669 verifyFormat("switch ( x ) {\n" 8670 "default:\n" 8671 " break;\n" 8672 "}", 8673 Spaces); 8674 8675 Spaces.SpacesInParentheses = false; 8676 Spaces.SpacesInCStyleCastParentheses = true; 8677 verifyFormat("Type *A = ( Type * )P;", Spaces); 8678 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8679 verifyFormat("x = ( int32 )y;", Spaces); 8680 verifyFormat("int a = ( int )(2.0f);", Spaces); 8681 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8682 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8683 verifyFormat("#define x (( int )-1)", Spaces); 8684 8685 // Run the first set of tests again with: 8686 Spaces.SpacesInParentheses = false; 8687 Spaces.SpaceInEmptyParentheses = true; 8688 Spaces.SpacesInCStyleCastParentheses = true; 8689 verifyFormat("call(x, y, z);", Spaces); 8690 verifyFormat("call( );", Spaces); 8691 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8692 verifyFormat("while (( bool )1)\n" 8693 " continue;", 8694 Spaces); 8695 verifyFormat("for (;;)\n" 8696 " continue;", 8697 Spaces); 8698 verifyFormat("if (true)\n" 8699 " f( );\n" 8700 "else if (true)\n" 8701 " f( );", 8702 Spaces); 8703 verifyFormat("do {\n" 8704 " do_something(( int )i);\n" 8705 "} while (something( ));", 8706 Spaces); 8707 verifyFormat("switch (x) {\n" 8708 "default:\n" 8709 " break;\n" 8710 "}", 8711 Spaces); 8712 8713 // Run the first set of tests again with: 8714 Spaces.SpaceAfterCStyleCast = true; 8715 verifyFormat("call(x, y, z);", Spaces); 8716 verifyFormat("call( );", Spaces); 8717 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8718 verifyFormat("while (( bool ) 1)\n" 8719 " continue;", 8720 Spaces); 8721 verifyFormat("for (;;)\n" 8722 " continue;", 8723 Spaces); 8724 verifyFormat("if (true)\n" 8725 " f( );\n" 8726 "else if (true)\n" 8727 " f( );", 8728 Spaces); 8729 verifyFormat("do {\n" 8730 " do_something(( int ) i);\n" 8731 "} while (something( ));", 8732 Spaces); 8733 verifyFormat("switch (x) {\n" 8734 "default:\n" 8735 " break;\n" 8736 "}", 8737 Spaces); 8738 8739 // Run subset of tests again with: 8740 Spaces.SpacesInCStyleCastParentheses = false; 8741 Spaces.SpaceAfterCStyleCast = true; 8742 verifyFormat("while ((bool) 1)\n" 8743 " continue;", 8744 Spaces); 8745 verifyFormat("do {\n" 8746 " do_something((int) i);\n" 8747 "} while (something( ));", 8748 Spaces); 8749 } 8750 8751 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8752 verifyFormat("int a[5];"); 8753 verifyFormat("a[3] += 42;"); 8754 8755 FormatStyle Spaces = getLLVMStyle(); 8756 Spaces.SpacesInSquareBrackets = true; 8757 // Lambdas unchanged. 8758 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8759 verifyFormat("return [i, args...] {};", Spaces); 8760 8761 // Not lambdas. 8762 verifyFormat("int a[ 5 ];", Spaces); 8763 verifyFormat("a[ 3 ] += 42;", Spaces); 8764 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8765 verifyFormat("double &operator[](int i) { return 0; }\n" 8766 "int i;", 8767 Spaces); 8768 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8769 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8770 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8771 } 8772 8773 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8774 verifyFormat("int a = 5;"); 8775 verifyFormat("a += 42;"); 8776 verifyFormat("a or_eq 8;"); 8777 8778 FormatStyle Spaces = getLLVMStyle(); 8779 Spaces.SpaceBeforeAssignmentOperators = false; 8780 verifyFormat("int a= 5;", Spaces); 8781 verifyFormat("a+= 42;", Spaces); 8782 verifyFormat("a or_eq 8;", Spaces); 8783 } 8784 8785 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8786 FormatStyle Alignment = getLLVMStyle(); 8787 Alignment.AlignConsecutiveAssignments = false; 8788 verifyFormat("int a = 5;\n" 8789 "int oneTwoThree = 123;", 8790 Alignment); 8791 verifyFormat("int a = 5;\n" 8792 "int oneTwoThree = 123;", 8793 Alignment); 8794 8795 Alignment.AlignConsecutiveAssignments = true; 8796 verifyFormat("int a = 5;\n" 8797 "int oneTwoThree = 123;", 8798 Alignment); 8799 verifyFormat("int a = method();\n" 8800 "int oneTwoThree = 133;", 8801 Alignment); 8802 verifyFormat("a &= 5;\n" 8803 "bcd *= 5;\n" 8804 "ghtyf += 5;\n" 8805 "dvfvdb -= 5;\n" 8806 "a /= 5;\n" 8807 "vdsvsv %= 5;\n" 8808 "sfdbddfbdfbb ^= 5;\n" 8809 "dvsdsv |= 5;\n" 8810 "int dsvvdvsdvvv = 123;", 8811 Alignment); 8812 verifyFormat("int i = 1, j = 10;\n" 8813 "something = 2000;", 8814 Alignment); 8815 verifyFormat("something = 2000;\n" 8816 "int i = 1, j = 10;\n", 8817 Alignment); 8818 verifyFormat("something = 2000;\n" 8819 "another = 911;\n" 8820 "int i = 1, j = 10;\n" 8821 "oneMore = 1;\n" 8822 "i = 2;", 8823 Alignment); 8824 verifyFormat("int a = 5;\n" 8825 "int one = 1;\n" 8826 "method();\n" 8827 "int oneTwoThree = 123;\n" 8828 "int oneTwo = 12;", 8829 Alignment); 8830 verifyFormat("int oneTwoThree = 123;\n" 8831 "int oneTwo = 12;\n" 8832 "method();\n", 8833 Alignment); 8834 verifyFormat("int oneTwoThree = 123; // comment\n" 8835 "int oneTwo = 12; // comment", 8836 Alignment); 8837 EXPECT_EQ("int a = 5;\n" 8838 "\n" 8839 "int oneTwoThree = 123;", 8840 format("int a = 5;\n" 8841 "\n" 8842 "int oneTwoThree= 123;", 8843 Alignment)); 8844 EXPECT_EQ("int a = 5;\n" 8845 "int one = 1;\n" 8846 "\n" 8847 "int oneTwoThree = 123;", 8848 format("int a = 5;\n" 8849 "int one = 1;\n" 8850 "\n" 8851 "int oneTwoThree = 123;", 8852 Alignment)); 8853 EXPECT_EQ("int a = 5;\n" 8854 "int one = 1;\n" 8855 "\n" 8856 "int oneTwoThree = 123;\n" 8857 "int oneTwo = 12;", 8858 format("int a = 5;\n" 8859 "int one = 1;\n" 8860 "\n" 8861 "int oneTwoThree = 123;\n" 8862 "int oneTwo = 12;", 8863 Alignment)); 8864 Alignment.AlignEscapedNewlinesLeft = true; 8865 verifyFormat("#define A \\\n" 8866 " int aaaa = 12; \\\n" 8867 " int b = 23; \\\n" 8868 " int ccc = 234; \\\n" 8869 " int dddddddddd = 2345;", 8870 Alignment); 8871 Alignment.AlignEscapedNewlinesLeft = false; 8872 verifyFormat("#define A " 8873 " \\\n" 8874 " int aaaa = 12; " 8875 " \\\n" 8876 " int b = 23; " 8877 " \\\n" 8878 " int ccc = 234; " 8879 " \\\n" 8880 " int dddddddddd = 2345;", 8881 Alignment); 8882 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8883 "k = 4, int l = 5,\n" 8884 " int m = 6) {\n" 8885 " int j = 10;\n" 8886 " otherThing = 1;\n" 8887 "}", 8888 Alignment); 8889 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8890 " int i = 1;\n" 8891 " int j = 2;\n" 8892 " int big = 10000;\n" 8893 "}", 8894 Alignment); 8895 verifyFormat("class C {\n" 8896 "public:\n" 8897 " int i = 1;\n" 8898 " virtual void f() = 0;\n" 8899 "};", 8900 Alignment); 8901 verifyFormat("int i = 1;\n" 8902 "if (SomeType t = getSomething()) {\n" 8903 "}\n" 8904 "int j = 2;\n" 8905 "int big = 10000;", 8906 Alignment); 8907 verifyFormat("int j = 7;\n" 8908 "for (int k = 0; k < N; ++k) {\n" 8909 "}\n" 8910 "int j = 2;\n" 8911 "int big = 10000;\n" 8912 "}", 8913 Alignment); 8914 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8915 verifyFormat("int i = 1;\n" 8916 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8917 " = someLooooooooooooooooongFunction();\n" 8918 "int j = 2;", 8919 Alignment); 8920 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8921 verifyFormat("int i = 1;\n" 8922 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8923 " someLooooooooooooooooongFunction();\n" 8924 "int j = 2;", 8925 Alignment); 8926 8927 verifyFormat("auto lambda = []() {\n" 8928 " auto i = 0;\n" 8929 " return 0;\n" 8930 "};\n" 8931 "int i = 0;\n" 8932 "auto v = type{\n" 8933 " i = 1, //\n" 8934 " (i = 2), //\n" 8935 " i = 3 //\n" 8936 "};", 8937 Alignment); 8938 8939 // FIXME: Should align all three assignments 8940 verifyFormat( 8941 "int i = 1;\n" 8942 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8943 " loooooooooooooooooooooongParameterB);\n" 8944 "int j = 2;", 8945 Alignment); 8946 8947 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8948 " typename B = very_long_type_name_1,\n" 8949 " typename T_2 = very_long_type_name_2>\n" 8950 "auto foo() {}\n", 8951 Alignment); 8952 verifyFormat("int a, b = 1;\n" 8953 "int c = 2;\n" 8954 "int dd = 3;\n", 8955 Alignment); 8956 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8957 "float b[1][] = {{3.f}};\n", 8958 Alignment); 8959 } 8960 8961 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8962 FormatStyle Alignment = getLLVMStyle(); 8963 Alignment.AlignConsecutiveDeclarations = false; 8964 verifyFormat("float const a = 5;\n" 8965 "int oneTwoThree = 123;", 8966 Alignment); 8967 verifyFormat("int a = 5;\n" 8968 "float const oneTwoThree = 123;", 8969 Alignment); 8970 8971 Alignment.AlignConsecutiveDeclarations = true; 8972 verifyFormat("float const a = 5;\n" 8973 "int oneTwoThree = 123;", 8974 Alignment); 8975 verifyFormat("int a = method();\n" 8976 "float const oneTwoThree = 133;", 8977 Alignment); 8978 verifyFormat("int i = 1, j = 10;\n" 8979 "something = 2000;", 8980 Alignment); 8981 verifyFormat("something = 2000;\n" 8982 "int i = 1, j = 10;\n", 8983 Alignment); 8984 verifyFormat("float something = 2000;\n" 8985 "double another = 911;\n" 8986 "int i = 1, j = 10;\n" 8987 "const int *oneMore = 1;\n" 8988 "unsigned i = 2;", 8989 Alignment); 8990 verifyFormat("float a = 5;\n" 8991 "int one = 1;\n" 8992 "method();\n" 8993 "const double oneTwoThree = 123;\n" 8994 "const unsigned int oneTwo = 12;", 8995 Alignment); 8996 verifyFormat("int oneTwoThree{0}; // comment\n" 8997 "unsigned oneTwo; // comment", 8998 Alignment); 8999 EXPECT_EQ("float const a = 5;\n" 9000 "\n" 9001 "int oneTwoThree = 123;", 9002 format("float const a = 5;\n" 9003 "\n" 9004 "int oneTwoThree= 123;", 9005 Alignment)); 9006 EXPECT_EQ("float a = 5;\n" 9007 "int one = 1;\n" 9008 "\n" 9009 "unsigned oneTwoThree = 123;", 9010 format("float a = 5;\n" 9011 "int one = 1;\n" 9012 "\n" 9013 "unsigned oneTwoThree = 123;", 9014 Alignment)); 9015 EXPECT_EQ("float a = 5;\n" 9016 "int one = 1;\n" 9017 "\n" 9018 "unsigned oneTwoThree = 123;\n" 9019 "int oneTwo = 12;", 9020 format("float a = 5;\n" 9021 "int one = 1;\n" 9022 "\n" 9023 "unsigned oneTwoThree = 123;\n" 9024 "int oneTwo = 12;", 9025 Alignment)); 9026 Alignment.AlignConsecutiveAssignments = true; 9027 verifyFormat("float something = 2000;\n" 9028 "double another = 911;\n" 9029 "int i = 1, j = 10;\n" 9030 "const int *oneMore = 1;\n" 9031 "unsigned i = 2;", 9032 Alignment); 9033 verifyFormat("int oneTwoThree = {0}; // comment\n" 9034 "unsigned oneTwo = 0; // comment", 9035 Alignment); 9036 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9037 " int const i = 1;\n" 9038 " int * j = 2;\n" 9039 " int big = 10000;\n" 9040 "\n" 9041 " unsigned oneTwoThree = 123;\n" 9042 " int oneTwo = 12;\n" 9043 " method();\n" 9044 " float k = 2;\n" 9045 " int ll = 10000;\n" 9046 "}", 9047 format("void SomeFunction(int parameter= 0) {\n" 9048 " int const i= 1;\n" 9049 " int *j=2;\n" 9050 " int big = 10000;\n" 9051 "\n" 9052 "unsigned oneTwoThree =123;\n" 9053 "int oneTwo = 12;\n" 9054 " method();\n" 9055 "float k= 2;\n" 9056 "int ll=10000;\n" 9057 "}", 9058 Alignment)); 9059 Alignment.AlignConsecutiveAssignments = false; 9060 Alignment.AlignEscapedNewlinesLeft = true; 9061 verifyFormat("#define A \\\n" 9062 " int aaaa = 12; \\\n" 9063 " float b = 23; \\\n" 9064 " const int ccc = 234; \\\n" 9065 " unsigned dddddddddd = 2345;", 9066 Alignment); 9067 Alignment.AlignEscapedNewlinesLeft = false; 9068 Alignment.ColumnLimit = 30; 9069 verifyFormat("#define A \\\n" 9070 " int aaaa = 12; \\\n" 9071 " float b = 23; \\\n" 9072 " const int ccc = 234; \\\n" 9073 " int dddddddddd = 2345;", 9074 Alignment); 9075 Alignment.ColumnLimit = 80; 9076 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9077 "k = 4, int l = 5,\n" 9078 " int m = 6) {\n" 9079 " const int j = 10;\n" 9080 " otherThing = 1;\n" 9081 "}", 9082 Alignment); 9083 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9084 " int const i = 1;\n" 9085 " int * j = 2;\n" 9086 " int big = 10000;\n" 9087 "}", 9088 Alignment); 9089 verifyFormat("class C {\n" 9090 "public:\n" 9091 " int i = 1;\n" 9092 " virtual void f() = 0;\n" 9093 "};", 9094 Alignment); 9095 verifyFormat("float i = 1;\n" 9096 "if (SomeType t = getSomething()) {\n" 9097 "}\n" 9098 "const unsigned j = 2;\n" 9099 "int big = 10000;", 9100 Alignment); 9101 verifyFormat("float j = 7;\n" 9102 "for (int k = 0; k < N; ++k) {\n" 9103 "}\n" 9104 "unsigned j = 2;\n" 9105 "int big = 10000;\n" 9106 "}", 9107 Alignment); 9108 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9109 verifyFormat("float i = 1;\n" 9110 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9111 " = someLooooooooooooooooongFunction();\n" 9112 "int j = 2;", 9113 Alignment); 9114 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9115 verifyFormat("int i = 1;\n" 9116 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9117 " someLooooooooooooooooongFunction();\n" 9118 "int j = 2;", 9119 Alignment); 9120 9121 Alignment.AlignConsecutiveAssignments = true; 9122 verifyFormat("auto lambda = []() {\n" 9123 " auto ii = 0;\n" 9124 " float j = 0;\n" 9125 " return 0;\n" 9126 "};\n" 9127 "int i = 0;\n" 9128 "float i2 = 0;\n" 9129 "auto v = type{\n" 9130 " i = 1, //\n" 9131 " (i = 2), //\n" 9132 " i = 3 //\n" 9133 "};", 9134 Alignment); 9135 Alignment.AlignConsecutiveAssignments = false; 9136 9137 // FIXME: Should align all three declarations 9138 verifyFormat( 9139 "int i = 1;\n" 9140 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9141 " loooooooooooooooooooooongParameterB);\n" 9142 "int j = 2;", 9143 Alignment); 9144 9145 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9146 // We expect declarations and assignments to align, as long as it doesn't 9147 // exceed the column limit, starting a new alignemnt sequence whenever it 9148 // happens. 9149 Alignment.AlignConsecutiveAssignments = true; 9150 Alignment.ColumnLimit = 30; 9151 verifyFormat("float ii = 1;\n" 9152 "unsigned j = 2;\n" 9153 "int someVerylongVariable = 1;\n" 9154 "AnotherLongType ll = 123456;\n" 9155 "VeryVeryLongType k = 2;\n" 9156 "int myvar = 1;", 9157 Alignment); 9158 Alignment.ColumnLimit = 80; 9159 Alignment.AlignConsecutiveAssignments = false; 9160 9161 verifyFormat( 9162 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9163 " typename LongType, typename B>\n" 9164 "auto foo() {}\n", 9165 Alignment); 9166 verifyFormat("float a, b = 1;\n" 9167 "int c = 2;\n" 9168 "int dd = 3;\n", 9169 Alignment); 9170 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9171 "float b[1][] = {{3.f}};\n", 9172 Alignment); 9173 Alignment.AlignConsecutiveAssignments = true; 9174 verifyFormat("float a, b = 1;\n" 9175 "int c = 2;\n" 9176 "int dd = 3;\n", 9177 Alignment); 9178 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9179 "float b[1][] = {{3.f}};\n", 9180 Alignment); 9181 Alignment.AlignConsecutiveAssignments = false; 9182 9183 Alignment.ColumnLimit = 30; 9184 Alignment.BinPackParameters = false; 9185 verifyFormat("void foo(float a,\n" 9186 " float b,\n" 9187 " int c,\n" 9188 " uint32_t *d) {\n" 9189 " int * e = 0;\n" 9190 " float f = 0;\n" 9191 " double g = 0;\n" 9192 "}\n" 9193 "void bar(ino_t a,\n" 9194 " int b,\n" 9195 " uint32_t *c,\n" 9196 " bool d) {}\n", 9197 Alignment); 9198 Alignment.BinPackParameters = true; 9199 Alignment.ColumnLimit = 80; 9200 } 9201 9202 TEST_F(FormatTest, LinuxBraceBreaking) { 9203 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9204 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9205 verifyFormat("namespace a\n" 9206 "{\n" 9207 "class A\n" 9208 "{\n" 9209 " void f()\n" 9210 " {\n" 9211 " if (true) {\n" 9212 " a();\n" 9213 " b();\n" 9214 " } else {\n" 9215 " a();\n" 9216 " }\n" 9217 " }\n" 9218 " void g() { return; }\n" 9219 "};\n" 9220 "struct B {\n" 9221 " int x;\n" 9222 "};\n" 9223 "}\n", 9224 LinuxBraceStyle); 9225 verifyFormat("enum X {\n" 9226 " Y = 0,\n" 9227 "}\n", 9228 LinuxBraceStyle); 9229 verifyFormat("struct S {\n" 9230 " int Type;\n" 9231 " union {\n" 9232 " int x;\n" 9233 " double y;\n" 9234 " } Value;\n" 9235 " class C\n" 9236 " {\n" 9237 " MyFavoriteType Value;\n" 9238 " } Class;\n" 9239 "}\n", 9240 LinuxBraceStyle); 9241 } 9242 9243 TEST_F(FormatTest, MozillaBraceBreaking) { 9244 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9245 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9246 verifyFormat("namespace a {\n" 9247 "class A\n" 9248 "{\n" 9249 " void f()\n" 9250 " {\n" 9251 " if (true) {\n" 9252 " a();\n" 9253 " b();\n" 9254 " }\n" 9255 " }\n" 9256 " void g() { return; }\n" 9257 "};\n" 9258 "enum E\n" 9259 "{\n" 9260 " A,\n" 9261 " // foo\n" 9262 " B,\n" 9263 " C\n" 9264 "};\n" 9265 "struct B\n" 9266 "{\n" 9267 " int x;\n" 9268 "};\n" 9269 "}\n", 9270 MozillaBraceStyle); 9271 verifyFormat("struct S\n" 9272 "{\n" 9273 " int Type;\n" 9274 " union\n" 9275 " {\n" 9276 " int x;\n" 9277 " double y;\n" 9278 " } Value;\n" 9279 " class C\n" 9280 " {\n" 9281 " MyFavoriteType Value;\n" 9282 " } Class;\n" 9283 "}\n", 9284 MozillaBraceStyle); 9285 } 9286 9287 TEST_F(FormatTest, StroustrupBraceBreaking) { 9288 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9289 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9290 verifyFormat("namespace a {\n" 9291 "class A {\n" 9292 " void f()\n" 9293 " {\n" 9294 " if (true) {\n" 9295 " a();\n" 9296 " b();\n" 9297 " }\n" 9298 " }\n" 9299 " void g() { return; }\n" 9300 "};\n" 9301 "struct B {\n" 9302 " int x;\n" 9303 "};\n" 9304 "}\n", 9305 StroustrupBraceStyle); 9306 9307 verifyFormat("void foo()\n" 9308 "{\n" 9309 " if (a) {\n" 9310 " a();\n" 9311 " }\n" 9312 " else {\n" 9313 " b();\n" 9314 " }\n" 9315 "}\n", 9316 StroustrupBraceStyle); 9317 9318 verifyFormat("#ifdef _DEBUG\n" 9319 "int foo(int i = 0)\n" 9320 "#else\n" 9321 "int foo(int i = 5)\n" 9322 "#endif\n" 9323 "{\n" 9324 " return i;\n" 9325 "}", 9326 StroustrupBraceStyle); 9327 9328 verifyFormat("void foo() {}\n" 9329 "void bar()\n" 9330 "#ifdef _DEBUG\n" 9331 "{\n" 9332 " foo();\n" 9333 "}\n" 9334 "#else\n" 9335 "{\n" 9336 "}\n" 9337 "#endif", 9338 StroustrupBraceStyle); 9339 9340 verifyFormat("void foobar() { int i = 5; }\n" 9341 "#ifdef _DEBUG\n" 9342 "void bar() {}\n" 9343 "#else\n" 9344 "void bar() { foobar(); }\n" 9345 "#endif", 9346 StroustrupBraceStyle); 9347 } 9348 9349 TEST_F(FormatTest, AllmanBraceBreaking) { 9350 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9351 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9352 verifyFormat("namespace a\n" 9353 "{\n" 9354 "class A\n" 9355 "{\n" 9356 " void f()\n" 9357 " {\n" 9358 " if (true)\n" 9359 " {\n" 9360 " a();\n" 9361 " b();\n" 9362 " }\n" 9363 " }\n" 9364 " void g() { return; }\n" 9365 "};\n" 9366 "struct B\n" 9367 "{\n" 9368 " int x;\n" 9369 "};\n" 9370 "}", 9371 AllmanBraceStyle); 9372 9373 verifyFormat("void f()\n" 9374 "{\n" 9375 " if (true)\n" 9376 " {\n" 9377 " a();\n" 9378 " }\n" 9379 " else if (false)\n" 9380 " {\n" 9381 " b();\n" 9382 " }\n" 9383 " else\n" 9384 " {\n" 9385 " c();\n" 9386 " }\n" 9387 "}\n", 9388 AllmanBraceStyle); 9389 9390 verifyFormat("void f()\n" 9391 "{\n" 9392 " for (int i = 0; i < 10; ++i)\n" 9393 " {\n" 9394 " a();\n" 9395 " }\n" 9396 " while (false)\n" 9397 " {\n" 9398 " b();\n" 9399 " }\n" 9400 " do\n" 9401 " {\n" 9402 " c();\n" 9403 " } while (false)\n" 9404 "}\n", 9405 AllmanBraceStyle); 9406 9407 verifyFormat("void f(int a)\n" 9408 "{\n" 9409 " switch (a)\n" 9410 " {\n" 9411 " case 0:\n" 9412 " break;\n" 9413 " case 1:\n" 9414 " {\n" 9415 " break;\n" 9416 " }\n" 9417 " case 2:\n" 9418 " {\n" 9419 " }\n" 9420 " break;\n" 9421 " default:\n" 9422 " break;\n" 9423 " }\n" 9424 "}\n", 9425 AllmanBraceStyle); 9426 9427 verifyFormat("enum X\n" 9428 "{\n" 9429 " Y = 0,\n" 9430 "}\n", 9431 AllmanBraceStyle); 9432 verifyFormat("enum X\n" 9433 "{\n" 9434 " Y = 0\n" 9435 "}\n", 9436 AllmanBraceStyle); 9437 9438 verifyFormat("@interface BSApplicationController ()\n" 9439 "{\n" 9440 "@private\n" 9441 " id _extraIvar;\n" 9442 "}\n" 9443 "@end\n", 9444 AllmanBraceStyle); 9445 9446 verifyFormat("#ifdef _DEBUG\n" 9447 "int foo(int i = 0)\n" 9448 "#else\n" 9449 "int foo(int i = 5)\n" 9450 "#endif\n" 9451 "{\n" 9452 " return i;\n" 9453 "}", 9454 AllmanBraceStyle); 9455 9456 verifyFormat("void foo() {}\n" 9457 "void bar()\n" 9458 "#ifdef _DEBUG\n" 9459 "{\n" 9460 " foo();\n" 9461 "}\n" 9462 "#else\n" 9463 "{\n" 9464 "}\n" 9465 "#endif", 9466 AllmanBraceStyle); 9467 9468 verifyFormat("void foobar() { int i = 5; }\n" 9469 "#ifdef _DEBUG\n" 9470 "void bar() {}\n" 9471 "#else\n" 9472 "void bar() { foobar(); }\n" 9473 "#endif", 9474 AllmanBraceStyle); 9475 9476 // This shouldn't affect ObjC blocks.. 9477 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9478 " // ...\n" 9479 " int i;\n" 9480 "}];", 9481 AllmanBraceStyle); 9482 verifyFormat("void (^block)(void) = ^{\n" 9483 " // ...\n" 9484 " int i;\n" 9485 "};", 9486 AllmanBraceStyle); 9487 // .. or dict literals. 9488 verifyFormat("void f()\n" 9489 "{\n" 9490 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9491 "}", 9492 AllmanBraceStyle); 9493 verifyFormat("int f()\n" 9494 "{ // comment\n" 9495 " return 42;\n" 9496 "}", 9497 AllmanBraceStyle); 9498 9499 AllmanBraceStyle.ColumnLimit = 19; 9500 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9501 AllmanBraceStyle.ColumnLimit = 18; 9502 verifyFormat("void f()\n" 9503 "{\n" 9504 " int i;\n" 9505 "}", 9506 AllmanBraceStyle); 9507 AllmanBraceStyle.ColumnLimit = 80; 9508 9509 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9510 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9511 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9512 verifyFormat("void f(bool b)\n" 9513 "{\n" 9514 " if (b)\n" 9515 " {\n" 9516 " return;\n" 9517 " }\n" 9518 "}\n", 9519 BreakBeforeBraceShortIfs); 9520 verifyFormat("void f(bool b)\n" 9521 "{\n" 9522 " if (b) return;\n" 9523 "}\n", 9524 BreakBeforeBraceShortIfs); 9525 verifyFormat("void f(bool b)\n" 9526 "{\n" 9527 " while (b)\n" 9528 " {\n" 9529 " return;\n" 9530 " }\n" 9531 "}\n", 9532 BreakBeforeBraceShortIfs); 9533 } 9534 9535 TEST_F(FormatTest, GNUBraceBreaking) { 9536 FormatStyle GNUBraceStyle = getLLVMStyle(); 9537 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9538 verifyFormat("namespace a\n" 9539 "{\n" 9540 "class A\n" 9541 "{\n" 9542 " void f()\n" 9543 " {\n" 9544 " int a;\n" 9545 " {\n" 9546 " int b;\n" 9547 " }\n" 9548 " if (true)\n" 9549 " {\n" 9550 " a();\n" 9551 " b();\n" 9552 " }\n" 9553 " }\n" 9554 " void g() { return; }\n" 9555 "}\n" 9556 "}", 9557 GNUBraceStyle); 9558 9559 verifyFormat("void f()\n" 9560 "{\n" 9561 " if (true)\n" 9562 " {\n" 9563 " a();\n" 9564 " }\n" 9565 " else if (false)\n" 9566 " {\n" 9567 " b();\n" 9568 " }\n" 9569 " else\n" 9570 " {\n" 9571 " c();\n" 9572 " }\n" 9573 "}\n", 9574 GNUBraceStyle); 9575 9576 verifyFormat("void f()\n" 9577 "{\n" 9578 " for (int i = 0; i < 10; ++i)\n" 9579 " {\n" 9580 " a();\n" 9581 " }\n" 9582 " while (false)\n" 9583 " {\n" 9584 " b();\n" 9585 " }\n" 9586 " do\n" 9587 " {\n" 9588 " c();\n" 9589 " }\n" 9590 " while (false);\n" 9591 "}\n", 9592 GNUBraceStyle); 9593 9594 verifyFormat("void f(int a)\n" 9595 "{\n" 9596 " switch (a)\n" 9597 " {\n" 9598 " case 0:\n" 9599 " break;\n" 9600 " case 1:\n" 9601 " {\n" 9602 " break;\n" 9603 " }\n" 9604 " case 2:\n" 9605 " {\n" 9606 " }\n" 9607 " break;\n" 9608 " default:\n" 9609 " break;\n" 9610 " }\n" 9611 "}\n", 9612 GNUBraceStyle); 9613 9614 verifyFormat("enum X\n" 9615 "{\n" 9616 " Y = 0,\n" 9617 "}\n", 9618 GNUBraceStyle); 9619 9620 verifyFormat("@interface BSApplicationController ()\n" 9621 "{\n" 9622 "@private\n" 9623 " id _extraIvar;\n" 9624 "}\n" 9625 "@end\n", 9626 GNUBraceStyle); 9627 9628 verifyFormat("#ifdef _DEBUG\n" 9629 "int foo(int i = 0)\n" 9630 "#else\n" 9631 "int foo(int i = 5)\n" 9632 "#endif\n" 9633 "{\n" 9634 " return i;\n" 9635 "}", 9636 GNUBraceStyle); 9637 9638 verifyFormat("void foo() {}\n" 9639 "void bar()\n" 9640 "#ifdef _DEBUG\n" 9641 "{\n" 9642 " foo();\n" 9643 "}\n" 9644 "#else\n" 9645 "{\n" 9646 "}\n" 9647 "#endif", 9648 GNUBraceStyle); 9649 9650 verifyFormat("void foobar() { int i = 5; }\n" 9651 "#ifdef _DEBUG\n" 9652 "void bar() {}\n" 9653 "#else\n" 9654 "void bar() { foobar(); }\n" 9655 "#endif", 9656 GNUBraceStyle); 9657 } 9658 9659 TEST_F(FormatTest, WebKitBraceBreaking) { 9660 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9661 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9662 verifyFormat("namespace a {\n" 9663 "class A {\n" 9664 " void f()\n" 9665 " {\n" 9666 " if (true) {\n" 9667 " a();\n" 9668 " b();\n" 9669 " }\n" 9670 " }\n" 9671 " void g() { return; }\n" 9672 "};\n" 9673 "enum E {\n" 9674 " A,\n" 9675 " // foo\n" 9676 " B,\n" 9677 " C\n" 9678 "};\n" 9679 "struct B {\n" 9680 " int x;\n" 9681 "};\n" 9682 "}\n", 9683 WebKitBraceStyle); 9684 verifyFormat("struct S {\n" 9685 " int Type;\n" 9686 " union {\n" 9687 " int x;\n" 9688 " double y;\n" 9689 " } Value;\n" 9690 " class C {\n" 9691 " MyFavoriteType Value;\n" 9692 " } Class;\n" 9693 "};\n", 9694 WebKitBraceStyle); 9695 } 9696 9697 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9698 verifyFormat("void f() {\n" 9699 " try {\n" 9700 " } catch (const Exception &e) {\n" 9701 " }\n" 9702 "}\n", 9703 getLLVMStyle()); 9704 } 9705 9706 TEST_F(FormatTest, UnderstandsPragmas) { 9707 verifyFormat("#pragma omp reduction(| : var)"); 9708 verifyFormat("#pragma omp reduction(+ : var)"); 9709 9710 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9711 "(including parentheses).", 9712 format("#pragma mark Any non-hyphenated or hyphenated string " 9713 "(including parentheses).")); 9714 } 9715 9716 TEST_F(FormatTest, UnderstandPragmaOption) { 9717 verifyFormat("#pragma option -C -A"); 9718 9719 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9720 } 9721 9722 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9723 for (size_t i = 1; i < Styles.size(); ++i) \ 9724 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9725 << " differs from Style #0" 9726 9727 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9728 SmallVector<FormatStyle, 3> Styles; 9729 Styles.resize(3); 9730 9731 Styles[0] = getLLVMStyle(); 9732 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9733 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9734 EXPECT_ALL_STYLES_EQUAL(Styles); 9735 9736 Styles[0] = getGoogleStyle(); 9737 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9738 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9739 EXPECT_ALL_STYLES_EQUAL(Styles); 9740 9741 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9742 EXPECT_TRUE( 9743 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9744 EXPECT_TRUE( 9745 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9746 EXPECT_ALL_STYLES_EQUAL(Styles); 9747 9748 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9749 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9750 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9751 EXPECT_ALL_STYLES_EQUAL(Styles); 9752 9753 Styles[0] = getMozillaStyle(); 9754 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9755 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9756 EXPECT_ALL_STYLES_EQUAL(Styles); 9757 9758 Styles[0] = getWebKitStyle(); 9759 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9760 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9761 EXPECT_ALL_STYLES_EQUAL(Styles); 9762 9763 Styles[0] = getGNUStyle(); 9764 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9765 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9766 EXPECT_ALL_STYLES_EQUAL(Styles); 9767 9768 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9769 } 9770 9771 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9772 SmallVector<FormatStyle, 8> Styles; 9773 Styles.resize(2); 9774 9775 Styles[0] = getGoogleStyle(); 9776 Styles[1] = getLLVMStyle(); 9777 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9778 EXPECT_ALL_STYLES_EQUAL(Styles); 9779 9780 Styles.resize(5); 9781 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9782 Styles[1] = getLLVMStyle(); 9783 Styles[1].Language = FormatStyle::LK_JavaScript; 9784 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9785 9786 Styles[2] = getLLVMStyle(); 9787 Styles[2].Language = FormatStyle::LK_JavaScript; 9788 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9789 "BasedOnStyle: Google", 9790 &Styles[2]) 9791 .value()); 9792 9793 Styles[3] = getLLVMStyle(); 9794 Styles[3].Language = FormatStyle::LK_JavaScript; 9795 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9796 "Language: JavaScript", 9797 &Styles[3]) 9798 .value()); 9799 9800 Styles[4] = getLLVMStyle(); 9801 Styles[4].Language = FormatStyle::LK_JavaScript; 9802 EXPECT_EQ(0, parseConfiguration("---\n" 9803 "BasedOnStyle: LLVM\n" 9804 "IndentWidth: 123\n" 9805 "---\n" 9806 "BasedOnStyle: Google\n" 9807 "Language: JavaScript", 9808 &Styles[4]) 9809 .value()); 9810 EXPECT_ALL_STYLES_EQUAL(Styles); 9811 } 9812 9813 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9814 Style.FIELD = false; \ 9815 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9816 EXPECT_TRUE(Style.FIELD); \ 9817 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9818 EXPECT_FALSE(Style.FIELD); 9819 9820 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9821 9822 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9823 Style.STRUCT.FIELD = false; \ 9824 EXPECT_EQ(0, \ 9825 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9826 .value()); \ 9827 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9828 EXPECT_EQ(0, \ 9829 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9830 .value()); \ 9831 EXPECT_FALSE(Style.STRUCT.FIELD); 9832 9833 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9834 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9835 9836 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9837 EXPECT_NE(VALUE, Style.FIELD); \ 9838 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9839 EXPECT_EQ(VALUE, Style.FIELD) 9840 9841 TEST_F(FormatTest, ParsesConfigurationBools) { 9842 FormatStyle Style = {}; 9843 Style.Language = FormatStyle::LK_Cpp; 9844 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9845 CHECK_PARSE_BOOL(AlignOperands); 9846 CHECK_PARSE_BOOL(AlignTrailingComments); 9847 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9848 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9849 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9850 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9851 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9852 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9853 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9854 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9855 CHECK_PARSE_BOOL(BinPackArguments); 9856 CHECK_PARSE_BOOL(BinPackParameters); 9857 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9858 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9859 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9860 CHECK_PARSE_BOOL(BreakStringLiterals); 9861 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9862 CHECK_PARSE_BOOL(DerivePointerAlignment); 9863 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9864 CHECK_PARSE_BOOL(DisableFormat); 9865 CHECK_PARSE_BOOL(IndentCaseLabels); 9866 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9867 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9868 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9869 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9870 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9871 CHECK_PARSE_BOOL(ReflowComments); 9872 CHECK_PARSE_BOOL(SortIncludes); 9873 CHECK_PARSE_BOOL(SpacesInParentheses); 9874 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9875 CHECK_PARSE_BOOL(SpacesInAngles); 9876 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9877 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9878 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9879 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9880 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9881 9882 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9883 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9884 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9885 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9886 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9887 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9888 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9889 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9890 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9891 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9892 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9893 } 9894 9895 #undef CHECK_PARSE_BOOL 9896 9897 TEST_F(FormatTest, ParsesConfiguration) { 9898 FormatStyle Style = {}; 9899 Style.Language = FormatStyle::LK_Cpp; 9900 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9901 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9902 ConstructorInitializerIndentWidth, 1234u); 9903 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9904 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9905 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9906 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9907 PenaltyBreakBeforeFirstCallParameter, 1234u); 9908 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9909 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9910 PenaltyReturnTypeOnItsOwnLine, 1234u); 9911 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9912 SpacesBeforeTrailingComments, 1234u); 9913 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9914 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9915 9916 Style.PointerAlignment = FormatStyle::PAS_Middle; 9917 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9918 FormatStyle::PAS_Left); 9919 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9920 FormatStyle::PAS_Right); 9921 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9922 FormatStyle::PAS_Middle); 9923 // For backward compatibility: 9924 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9925 FormatStyle::PAS_Left); 9926 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9927 FormatStyle::PAS_Right); 9928 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9929 FormatStyle::PAS_Middle); 9930 9931 Style.Standard = FormatStyle::LS_Auto; 9932 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9933 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9934 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9935 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9936 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9937 9938 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9939 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9940 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9941 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9942 FormatStyle::BOS_None); 9943 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9944 FormatStyle::BOS_All); 9945 // For backward compatibility: 9946 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9947 FormatStyle::BOS_None); 9948 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9949 FormatStyle::BOS_All); 9950 9951 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9952 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9953 FormatStyle::BAS_Align); 9954 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9955 FormatStyle::BAS_DontAlign); 9956 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9957 FormatStyle::BAS_AlwaysBreak); 9958 // For backward compatibility: 9959 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9960 FormatStyle::BAS_DontAlign); 9961 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9962 FormatStyle::BAS_Align); 9963 9964 Style.UseTab = FormatStyle::UT_ForIndentation; 9965 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9966 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9967 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9968 // For backward compatibility: 9969 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9970 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9971 9972 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9973 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9974 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9975 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9976 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9977 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9978 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9979 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9980 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9981 // For backward compatibility: 9982 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9983 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9984 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9985 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9986 9987 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9988 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9989 FormatStyle::SBPO_Never); 9990 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9991 FormatStyle::SBPO_Always); 9992 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9993 FormatStyle::SBPO_ControlStatements); 9994 // For backward compatibility: 9995 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9996 FormatStyle::SBPO_Never); 9997 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9998 FormatStyle::SBPO_ControlStatements); 9999 10000 Style.ColumnLimit = 123; 10001 FormatStyle BaseStyle = getLLVMStyle(); 10002 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10003 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10004 10005 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10006 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10007 FormatStyle::BS_Attach); 10008 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10009 FormatStyle::BS_Linux); 10010 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10011 FormatStyle::BS_Mozilla); 10012 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10013 FormatStyle::BS_Stroustrup); 10014 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10015 FormatStyle::BS_Allman); 10016 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10017 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10018 FormatStyle::BS_WebKit); 10019 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10020 FormatStyle::BS_Custom); 10021 10022 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10023 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10024 FormatStyle::RTBS_None); 10025 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10026 FormatStyle::RTBS_All); 10027 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10028 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10029 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10030 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10031 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10032 AlwaysBreakAfterReturnType, 10033 FormatStyle::RTBS_TopLevelDefinitions); 10034 10035 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10036 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10037 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10038 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10039 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10040 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10041 AlwaysBreakAfterDefinitionReturnType, 10042 FormatStyle::DRTBS_TopLevel); 10043 10044 Style.NamespaceIndentation = FormatStyle::NI_All; 10045 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10046 FormatStyle::NI_None); 10047 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10048 FormatStyle::NI_Inner); 10049 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10050 FormatStyle::NI_All); 10051 10052 // FIXME: This is required because parsing a configuration simply overwrites 10053 // the first N elements of the list instead of resetting it. 10054 Style.ForEachMacros.clear(); 10055 std::vector<std::string> BoostForeach; 10056 BoostForeach.push_back("BOOST_FOREACH"); 10057 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10058 std::vector<std::string> BoostAndQForeach; 10059 BoostAndQForeach.push_back("BOOST_FOREACH"); 10060 BoostAndQForeach.push_back("Q_FOREACH"); 10061 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10062 BoostAndQForeach); 10063 10064 Style.IncludeCategories.clear(); 10065 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10066 {".*", 1}}; 10067 CHECK_PARSE("IncludeCategories:\n" 10068 " - Regex: abc/.*\n" 10069 " Priority: 2\n" 10070 " - Regex: .*\n" 10071 " Priority: 1", 10072 IncludeCategories, ExpectedCategories); 10073 } 10074 10075 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10076 FormatStyle Style = {}; 10077 Style.Language = FormatStyle::LK_Cpp; 10078 CHECK_PARSE("Language: Cpp\n" 10079 "IndentWidth: 12", 10080 IndentWidth, 12u); 10081 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10082 "IndentWidth: 34", 10083 &Style), 10084 ParseError::Unsuitable); 10085 EXPECT_EQ(12u, Style.IndentWidth); 10086 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10087 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10088 10089 Style.Language = FormatStyle::LK_JavaScript; 10090 CHECK_PARSE("Language: JavaScript\n" 10091 "IndentWidth: 12", 10092 IndentWidth, 12u); 10093 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10094 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10095 "IndentWidth: 34", 10096 &Style), 10097 ParseError::Unsuitable); 10098 EXPECT_EQ(23u, Style.IndentWidth); 10099 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10100 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10101 10102 CHECK_PARSE("BasedOnStyle: LLVM\n" 10103 "IndentWidth: 67", 10104 IndentWidth, 67u); 10105 10106 CHECK_PARSE("---\n" 10107 "Language: JavaScript\n" 10108 "IndentWidth: 12\n" 10109 "---\n" 10110 "Language: Cpp\n" 10111 "IndentWidth: 34\n" 10112 "...\n", 10113 IndentWidth, 12u); 10114 10115 Style.Language = FormatStyle::LK_Cpp; 10116 CHECK_PARSE("---\n" 10117 "Language: JavaScript\n" 10118 "IndentWidth: 12\n" 10119 "---\n" 10120 "Language: Cpp\n" 10121 "IndentWidth: 34\n" 10122 "...\n", 10123 IndentWidth, 34u); 10124 CHECK_PARSE("---\n" 10125 "IndentWidth: 78\n" 10126 "---\n" 10127 "Language: JavaScript\n" 10128 "IndentWidth: 56\n" 10129 "...\n", 10130 IndentWidth, 78u); 10131 10132 Style.ColumnLimit = 123; 10133 Style.IndentWidth = 234; 10134 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10135 Style.TabWidth = 345; 10136 EXPECT_FALSE(parseConfiguration("---\n" 10137 "IndentWidth: 456\n" 10138 "BreakBeforeBraces: Allman\n" 10139 "---\n" 10140 "Language: JavaScript\n" 10141 "IndentWidth: 111\n" 10142 "TabWidth: 111\n" 10143 "---\n" 10144 "Language: Cpp\n" 10145 "BreakBeforeBraces: Stroustrup\n" 10146 "TabWidth: 789\n" 10147 "...\n", 10148 &Style)); 10149 EXPECT_EQ(123u, Style.ColumnLimit); 10150 EXPECT_EQ(456u, Style.IndentWidth); 10151 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10152 EXPECT_EQ(789u, Style.TabWidth); 10153 10154 EXPECT_EQ(parseConfiguration("---\n" 10155 "Language: JavaScript\n" 10156 "IndentWidth: 56\n" 10157 "---\n" 10158 "IndentWidth: 78\n" 10159 "...\n", 10160 &Style), 10161 ParseError::Error); 10162 EXPECT_EQ(parseConfiguration("---\n" 10163 "Language: JavaScript\n" 10164 "IndentWidth: 56\n" 10165 "---\n" 10166 "Language: JavaScript\n" 10167 "IndentWidth: 78\n" 10168 "...\n", 10169 &Style), 10170 ParseError::Error); 10171 10172 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10173 } 10174 10175 #undef CHECK_PARSE 10176 10177 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10178 FormatStyle Style = {}; 10179 Style.Language = FormatStyle::LK_JavaScript; 10180 Style.BreakBeforeTernaryOperators = true; 10181 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10182 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10183 10184 Style.BreakBeforeTernaryOperators = true; 10185 EXPECT_EQ(0, parseConfiguration("---\n" 10186 "BasedOnStyle: Google\n" 10187 "---\n" 10188 "Language: JavaScript\n" 10189 "IndentWidth: 76\n" 10190 "...\n", 10191 &Style) 10192 .value()); 10193 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10194 EXPECT_EQ(76u, Style.IndentWidth); 10195 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10196 } 10197 10198 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10199 FormatStyle Style = getLLVMStyle(); 10200 std::string YAML = configurationAsText(Style); 10201 FormatStyle ParsedStyle = {}; 10202 ParsedStyle.Language = FormatStyle::LK_Cpp; 10203 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10204 EXPECT_EQ(Style, ParsedStyle); 10205 } 10206 10207 TEST_F(FormatTest, WorksFor8bitEncodings) { 10208 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10209 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10210 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10211 "\"\xef\xee\xf0\xf3...\"", 10212 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10213 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10214 "\xef\xee\xf0\xf3...\"", 10215 getLLVMStyleWithColumns(12))); 10216 } 10217 10218 TEST_F(FormatTest, HandlesUTF8BOM) { 10219 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10220 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10221 format("\xef\xbb\xbf#include <iostream>")); 10222 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10223 format("\xef\xbb\xbf\n#include <iostream>")); 10224 } 10225 10226 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10227 #if !defined(_MSC_VER) 10228 10229 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10230 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10231 getLLVMStyleWithColumns(35)); 10232 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10233 getLLVMStyleWithColumns(31)); 10234 verifyFormat("// Однажды в студёную зимнюю пору...", 10235 getLLVMStyleWithColumns(36)); 10236 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10237 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10238 getLLVMStyleWithColumns(39)); 10239 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10240 getLLVMStyleWithColumns(35)); 10241 } 10242 10243 TEST_F(FormatTest, SplitsUTF8Strings) { 10244 // Non-printable characters' width is currently considered to be the length in 10245 // bytes in UTF8. The characters can be displayed in very different manner 10246 // (zero-width, single width with a substitution glyph, expanded to their code 10247 // (e.g. "<8d>"), so there's no single correct way to handle them. 10248 EXPECT_EQ("\"aaaaÄ\"\n" 10249 "\"\xc2\x8d\";", 10250 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10251 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10252 "\"\xc2\x8d\";", 10253 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10254 EXPECT_EQ("\"Однажды, в \"\n" 10255 "\"студёную \"\n" 10256 "\"зимнюю \"\n" 10257 "\"пору,\"", 10258 format("\"Однажды, в студёную зимнюю пору,\"", 10259 getLLVMStyleWithColumns(13))); 10260 EXPECT_EQ( 10261 "\"一 二 三 \"\n" 10262 "\"四 五六 \"\n" 10263 "\"七 八 九 \"\n" 10264 "\"十\"", 10265 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10266 EXPECT_EQ("\"一\t二 \"\n" 10267 "\"\t三 \"\n" 10268 "\"四 五\t六 \"\n" 10269 "\"\t七 \"\n" 10270 "\"八九十\tqq\"", 10271 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10272 getLLVMStyleWithColumns(11))); 10273 10274 // UTF8 character in an escape sequence. 10275 EXPECT_EQ("\"aaaaaa\"\n" 10276 "\"\\\xC2\x8D\"", 10277 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10278 } 10279 10280 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10281 EXPECT_EQ("const char *sssss =\n" 10282 " \"一二三四五六七八\\\n" 10283 " 九 十\";", 10284 format("const char *sssss = \"一二三四五六七八\\\n" 10285 " 九 十\";", 10286 getLLVMStyleWithColumns(30))); 10287 } 10288 10289 TEST_F(FormatTest, SplitsUTF8LineComments) { 10290 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10291 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10292 EXPECT_EQ("// Я из лесу\n" 10293 "// вышел; был\n" 10294 "// сильный\n" 10295 "// мороз.", 10296 format("// Я из лесу вышел; был сильный мороз.", 10297 getLLVMStyleWithColumns(13))); 10298 EXPECT_EQ("// 一二三\n" 10299 "// 四五六七\n" 10300 "// 八 九\n" 10301 "// 十", 10302 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10303 } 10304 10305 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10306 EXPECT_EQ("/* Гляжу,\n" 10307 " * поднимается\n" 10308 " * медленно в\n" 10309 " * гору\n" 10310 " * Лошадка,\n" 10311 " * везущая\n" 10312 " * хворосту\n" 10313 " * воз. */", 10314 format("/* Гляжу, поднимается медленно в гору\n" 10315 " * Лошадка, везущая хворосту воз. */", 10316 getLLVMStyleWithColumns(13))); 10317 EXPECT_EQ( 10318 "/* 一二三\n" 10319 " * 四五六七\n" 10320 " * 八 九\n" 10321 " * 十 */", 10322 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10323 EXPECT_EQ("/* \n" 10324 " * \n" 10325 " * - */", 10326 format("/* - */", getLLVMStyleWithColumns(12))); 10327 } 10328 10329 #endif // _MSC_VER 10330 10331 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10332 FormatStyle Style = getLLVMStyle(); 10333 10334 Style.ConstructorInitializerIndentWidth = 4; 10335 verifyFormat( 10336 "SomeClass::Constructor()\n" 10337 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10338 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10339 Style); 10340 10341 Style.ConstructorInitializerIndentWidth = 2; 10342 verifyFormat( 10343 "SomeClass::Constructor()\n" 10344 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10345 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10346 Style); 10347 10348 Style.ConstructorInitializerIndentWidth = 0; 10349 verifyFormat( 10350 "SomeClass::Constructor()\n" 10351 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10352 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10353 Style); 10354 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10355 verifyFormat( 10356 "SomeLongTemplateVariableName<\n" 10357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10358 Style); 10359 verifyFormat( 10360 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10362 Style); 10363 } 10364 10365 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10366 FormatStyle Style = getLLVMStyle(); 10367 Style.BreakConstructorInitializersBeforeComma = true; 10368 Style.ConstructorInitializerIndentWidth = 4; 10369 verifyFormat("SomeClass::Constructor()\n" 10370 " : a(a)\n" 10371 " , b(b)\n" 10372 " , c(c) {}", 10373 Style); 10374 verifyFormat("SomeClass::Constructor()\n" 10375 " : a(a) {}", 10376 Style); 10377 10378 Style.ColumnLimit = 0; 10379 verifyFormat("SomeClass::Constructor()\n" 10380 " : a(a) {}", 10381 Style); 10382 verifyFormat("SomeClass::Constructor()\n" 10383 " : a(a)\n" 10384 " , b(b)\n" 10385 " , c(c) {}", 10386 Style); 10387 verifyFormat("SomeClass::Constructor()\n" 10388 " : a(a) {\n" 10389 " foo();\n" 10390 " bar();\n" 10391 "}", 10392 Style); 10393 10394 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10395 verifyFormat("SomeClass::Constructor()\n" 10396 " : a(a)\n" 10397 " , b(b)\n" 10398 " , c(c) {\n}", 10399 Style); 10400 verifyFormat("SomeClass::Constructor()\n" 10401 " : a(a) {\n}", 10402 Style); 10403 10404 Style.ColumnLimit = 80; 10405 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10406 Style.ConstructorInitializerIndentWidth = 2; 10407 verifyFormat("SomeClass::Constructor()\n" 10408 " : a(a)\n" 10409 " , b(b)\n" 10410 " , c(c) {}", 10411 Style); 10412 10413 Style.ConstructorInitializerIndentWidth = 0; 10414 verifyFormat("SomeClass::Constructor()\n" 10415 ": a(a)\n" 10416 ", b(b)\n" 10417 ", c(c) {}", 10418 Style); 10419 10420 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10421 Style.ConstructorInitializerIndentWidth = 4; 10422 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10423 verifyFormat( 10424 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10425 Style); 10426 verifyFormat( 10427 "SomeClass::Constructor()\n" 10428 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10429 Style); 10430 Style.ConstructorInitializerIndentWidth = 4; 10431 Style.ColumnLimit = 60; 10432 verifyFormat("SomeClass::Constructor()\n" 10433 " : aaaaaaaa(aaaaaaaa)\n" 10434 " , aaaaaaaa(aaaaaaaa)\n" 10435 " , aaaaaaaa(aaaaaaaa) {}", 10436 Style); 10437 } 10438 10439 TEST_F(FormatTest, Destructors) { 10440 verifyFormat("void F(int &i) { i.~int(); }"); 10441 verifyFormat("void F(int &i) { i->~int(); }"); 10442 } 10443 10444 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10445 FormatStyle Style = getWebKitStyle(); 10446 10447 // Don't indent in outer namespaces. 10448 verifyFormat("namespace outer {\n" 10449 "int i;\n" 10450 "namespace inner {\n" 10451 " int i;\n" 10452 "} // namespace inner\n" 10453 "} // namespace outer\n" 10454 "namespace other_outer {\n" 10455 "int i;\n" 10456 "}", 10457 Style); 10458 10459 // Don't indent case labels. 10460 verifyFormat("switch (variable) {\n" 10461 "case 1:\n" 10462 "case 2:\n" 10463 " doSomething();\n" 10464 " break;\n" 10465 "default:\n" 10466 " ++variable;\n" 10467 "}", 10468 Style); 10469 10470 // Wrap before binary operators. 10471 EXPECT_EQ("void f()\n" 10472 "{\n" 10473 " if (aaaaaaaaaaaaaaaa\n" 10474 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10475 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10476 " return;\n" 10477 "}", 10478 format("void f() {\n" 10479 "if (aaaaaaaaaaaaaaaa\n" 10480 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10481 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10482 "return;\n" 10483 "}", 10484 Style)); 10485 10486 // Allow functions on a single line. 10487 verifyFormat("void f() { return; }", Style); 10488 10489 // Constructor initializers are formatted one per line with the "," on the 10490 // new line. 10491 verifyFormat("Constructor()\n" 10492 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10493 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10494 " aaaaaaaaaaaaaa)\n" 10495 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10496 "{\n" 10497 "}", 10498 Style); 10499 verifyFormat("SomeClass::Constructor()\n" 10500 " : a(a)\n" 10501 "{\n" 10502 "}", 10503 Style); 10504 EXPECT_EQ("SomeClass::Constructor()\n" 10505 " : a(a)\n" 10506 "{\n" 10507 "}", 10508 format("SomeClass::Constructor():a(a){}", Style)); 10509 verifyFormat("SomeClass::Constructor()\n" 10510 " : a(a)\n" 10511 " , b(b)\n" 10512 " , c(c)\n" 10513 "{\n" 10514 "}", 10515 Style); 10516 verifyFormat("SomeClass::Constructor()\n" 10517 " : a(a)\n" 10518 "{\n" 10519 " foo();\n" 10520 " bar();\n" 10521 "}", 10522 Style); 10523 10524 // Access specifiers should be aligned left. 10525 verifyFormat("class C {\n" 10526 "public:\n" 10527 " int i;\n" 10528 "};", 10529 Style); 10530 10531 // Do not align comments. 10532 verifyFormat("int a; // Do not\n" 10533 "double b; // align comments.", 10534 Style); 10535 10536 // Do not align operands. 10537 EXPECT_EQ("ASSERT(aaaa\n" 10538 " || bbbb);", 10539 format("ASSERT ( aaaa\n||bbbb);", Style)); 10540 10541 // Accept input's line breaks. 10542 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10543 " || bbbbbbbbbbbbbbb) {\n" 10544 " i++;\n" 10545 "}", 10546 format("if (aaaaaaaaaaaaaaa\n" 10547 "|| bbbbbbbbbbbbbbb) { i++; }", 10548 Style)); 10549 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10550 " i++;\n" 10551 "}", 10552 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10553 10554 // Don't automatically break all macro definitions (llvm.org/PR17842). 10555 verifyFormat("#define aNumber 10", Style); 10556 // However, generally keep the line breaks that the user authored. 10557 EXPECT_EQ("#define aNumber \\\n" 10558 " 10", 10559 format("#define aNumber \\\n" 10560 " 10", 10561 Style)); 10562 10563 // Keep empty and one-element array literals on a single line. 10564 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10565 " copyItems:YES];", 10566 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10567 "copyItems:YES];", 10568 Style)); 10569 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10570 " copyItems:YES];", 10571 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10572 " copyItems:YES];", 10573 Style)); 10574 // FIXME: This does not seem right, there should be more indentation before 10575 // the array literal's entries. Nested blocks have the same problem. 10576 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10577 " @\"a\",\n" 10578 " @\"a\"\n" 10579 "]\n" 10580 " copyItems:YES];", 10581 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10582 " @\"a\",\n" 10583 " @\"a\"\n" 10584 " ]\n" 10585 " copyItems:YES];", 10586 Style)); 10587 EXPECT_EQ( 10588 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10589 " copyItems:YES];", 10590 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10591 " copyItems:YES];", 10592 Style)); 10593 10594 verifyFormat("[self.a b:c c:d];", Style); 10595 EXPECT_EQ("[self.a b:c\n" 10596 " c:d];", 10597 format("[self.a b:c\n" 10598 "c:d];", 10599 Style)); 10600 } 10601 10602 TEST_F(FormatTest, FormatsLambdas) { 10603 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10604 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10605 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10606 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10607 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10608 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10609 verifyFormat("void f() {\n" 10610 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10611 "}\n"); 10612 verifyFormat("void f() {\n" 10613 " other(x.begin(), //\n" 10614 " x.end(), //\n" 10615 " [&](int, int) { return 1; });\n" 10616 "}\n"); 10617 verifyFormat("SomeFunction([]() { // A cool function...\n" 10618 " return 43;\n" 10619 "});"); 10620 EXPECT_EQ("SomeFunction([]() {\n" 10621 "#define A a\n" 10622 " return 43;\n" 10623 "});", 10624 format("SomeFunction([](){\n" 10625 "#define A a\n" 10626 "return 43;\n" 10627 "});")); 10628 verifyFormat("void f() {\n" 10629 " SomeFunction([](decltype(x), A *a) {});\n" 10630 "}"); 10631 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10632 " [](const aaaaaaaaaa &a) { return a; });"); 10633 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10634 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10635 "});"); 10636 verifyFormat("Constructor()\n" 10637 " : Field([] { // comment\n" 10638 " int i;\n" 10639 " }) {}"); 10640 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10641 " return some_parameter.size();\n" 10642 "};"); 10643 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10644 " [](const string &s) { return s; };"); 10645 verifyFormat("int i = aaaaaa ? 1 //\n" 10646 " : [] {\n" 10647 " return 2; //\n" 10648 " }();"); 10649 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10650 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10651 " return x == 2; // force break\n" 10652 " });"); 10653 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10654 " int iiiiiiiiiiii) {\n" 10655 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10656 "});", 10657 getLLVMStyleWithColumns(60)); 10658 verifyFormat("SomeFunction({[&] {\n" 10659 " // comment\n" 10660 " },\n" 10661 " [&] {\n" 10662 " // comment\n" 10663 " }});"); 10664 verifyFormat("SomeFunction({[&] {\n" 10665 " // comment\n" 10666 "}});"); 10667 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10668 " [&]() { return true; },\n" 10669 " aaaaa aaaaaaaaa);"); 10670 10671 // Lambdas with return types. 10672 verifyFormat("int c = []() -> int { return 2; }();\n"); 10673 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10674 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10675 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10676 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10677 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10678 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10679 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10680 verifyFormat("[a, a]() -> a<1> {};"); 10681 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10682 " int j) -> int {\n" 10683 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10684 "};"); 10685 verifyFormat( 10686 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10687 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10688 " return aaaaaaaaaaaaaaaaa;\n" 10689 " });", 10690 getLLVMStyleWithColumns(70)); 10691 10692 // Multiple lambdas in the same parentheses change indentation rules. 10693 verifyFormat("SomeFunction(\n" 10694 " []() {\n" 10695 " int i = 42;\n" 10696 " return i;\n" 10697 " },\n" 10698 " []() {\n" 10699 " int j = 43;\n" 10700 " return j;\n" 10701 " });"); 10702 10703 // More complex introducers. 10704 verifyFormat("return [i, args...] {};"); 10705 10706 // Not lambdas. 10707 verifyFormat("constexpr char hello[]{\"hello\"};"); 10708 verifyFormat("double &operator[](int i) { return 0; }\n" 10709 "int i;"); 10710 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10711 verifyFormat("int i = a[a][a]->f();"); 10712 verifyFormat("int i = (*b)[a]->f();"); 10713 10714 // Other corner cases. 10715 verifyFormat("void f() {\n" 10716 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10717 " );\n" 10718 "}"); 10719 10720 // Lambdas created through weird macros. 10721 verifyFormat("void f() {\n" 10722 " MACRO((const AA &a) { return 1; });\n" 10723 " MACRO((AA &a) { return 1; });\n" 10724 "}"); 10725 10726 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10727 " doo_dah();\n" 10728 " doo_dah();\n" 10729 " })) {\n" 10730 "}"); 10731 verifyFormat("auto lambda = []() {\n" 10732 " int a = 2\n" 10733 "#if A\n" 10734 " + 2\n" 10735 "#endif\n" 10736 " ;\n" 10737 "};"); 10738 } 10739 10740 TEST_F(FormatTest, FormatsBlocks) { 10741 FormatStyle ShortBlocks = getLLVMStyle(); 10742 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10743 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10744 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10745 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10746 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10747 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10748 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10749 10750 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10751 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10752 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10753 10754 verifyFormat("[operation setCompletionBlock:^{\n" 10755 " [self onOperationDone];\n" 10756 "}];"); 10757 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10758 " [self onOperationDone];\n" 10759 "}]};"); 10760 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10761 " f();\n" 10762 "}];"); 10763 verifyFormat("int a = [operation block:^int(int *i) {\n" 10764 " return 1;\n" 10765 "}];"); 10766 verifyFormat("[myObject doSomethingWith:arg1\n" 10767 " aaa:^int(int *a) {\n" 10768 " return 1;\n" 10769 " }\n" 10770 " bbb:f(a * bbbbbbbb)];"); 10771 10772 verifyFormat("[operation setCompletionBlock:^{\n" 10773 " [self.delegate newDataAvailable];\n" 10774 "}];", 10775 getLLVMStyleWithColumns(60)); 10776 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10777 " NSString *path = [self sessionFilePath];\n" 10778 " if (path) {\n" 10779 " // ...\n" 10780 " }\n" 10781 "});"); 10782 verifyFormat("[[SessionService sharedService]\n" 10783 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10784 " if (window) {\n" 10785 " [self windowDidLoad:window];\n" 10786 " } else {\n" 10787 " [self errorLoadingWindow];\n" 10788 " }\n" 10789 " }];"); 10790 verifyFormat("void (^largeBlock)(void) = ^{\n" 10791 " // ...\n" 10792 "};\n", 10793 getLLVMStyleWithColumns(40)); 10794 verifyFormat("[[SessionService sharedService]\n" 10795 " loadWindowWithCompletionBlock: //\n" 10796 " ^(SessionWindow *window) {\n" 10797 " if (window) {\n" 10798 " [self windowDidLoad:window];\n" 10799 " } else {\n" 10800 " [self errorLoadingWindow];\n" 10801 " }\n" 10802 " }];", 10803 getLLVMStyleWithColumns(60)); 10804 verifyFormat("[myObject doSomethingWith:arg1\n" 10805 " firstBlock:^(Foo *a) {\n" 10806 " // ...\n" 10807 " int i;\n" 10808 " }\n" 10809 " secondBlock:^(Bar *b) {\n" 10810 " // ...\n" 10811 " int i;\n" 10812 " }\n" 10813 " thirdBlock:^Foo(Bar *b) {\n" 10814 " // ...\n" 10815 " int i;\n" 10816 " }];"); 10817 verifyFormat("[myObject doSomethingWith:arg1\n" 10818 " firstBlock:-1\n" 10819 " secondBlock:^(Bar *b) {\n" 10820 " // ...\n" 10821 " int i;\n" 10822 " }];"); 10823 10824 verifyFormat("f(^{\n" 10825 " @autoreleasepool {\n" 10826 " if (a) {\n" 10827 " g();\n" 10828 " }\n" 10829 " }\n" 10830 "});"); 10831 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10832 10833 FormatStyle FourIndent = getLLVMStyle(); 10834 FourIndent.ObjCBlockIndentWidth = 4; 10835 verifyFormat("[operation setCompletionBlock:^{\n" 10836 " [self onOperationDone];\n" 10837 "}];", 10838 FourIndent); 10839 } 10840 10841 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10842 FormatStyle ZeroColumn = getLLVMStyle(); 10843 ZeroColumn.ColumnLimit = 0; 10844 10845 verifyFormat("[[SessionService sharedService] " 10846 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10847 " if (window) {\n" 10848 " [self windowDidLoad:window];\n" 10849 " } else {\n" 10850 " [self errorLoadingWindow];\n" 10851 " }\n" 10852 "}];", 10853 ZeroColumn); 10854 EXPECT_EQ("[[SessionService sharedService]\n" 10855 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10856 " if (window) {\n" 10857 " [self windowDidLoad:window];\n" 10858 " } else {\n" 10859 " [self errorLoadingWindow];\n" 10860 " }\n" 10861 " }];", 10862 format("[[SessionService sharedService]\n" 10863 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10864 " if (window) {\n" 10865 " [self windowDidLoad:window];\n" 10866 " } else {\n" 10867 " [self errorLoadingWindow];\n" 10868 " }\n" 10869 "}];", 10870 ZeroColumn)); 10871 verifyFormat("[myObject doSomethingWith:arg1\n" 10872 " firstBlock:^(Foo *a) {\n" 10873 " // ...\n" 10874 " int i;\n" 10875 " }\n" 10876 " secondBlock:^(Bar *b) {\n" 10877 " // ...\n" 10878 " int i;\n" 10879 " }\n" 10880 " thirdBlock:^Foo(Bar *b) {\n" 10881 " // ...\n" 10882 " int i;\n" 10883 " }];", 10884 ZeroColumn); 10885 verifyFormat("f(^{\n" 10886 " @autoreleasepool {\n" 10887 " if (a) {\n" 10888 " g();\n" 10889 " }\n" 10890 " }\n" 10891 "});", 10892 ZeroColumn); 10893 verifyFormat("void (^largeBlock)(void) = ^{\n" 10894 " // ...\n" 10895 "};", 10896 ZeroColumn); 10897 10898 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10899 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10900 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10901 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10902 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10903 " int i;\n" 10904 "};", 10905 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10906 } 10907 10908 TEST_F(FormatTest, SupportsCRLF) { 10909 EXPECT_EQ("int a;\r\n" 10910 "int b;\r\n" 10911 "int c;\r\n", 10912 format("int a;\r\n" 10913 " int b;\r\n" 10914 " int c;\r\n", 10915 getLLVMStyle())); 10916 EXPECT_EQ("int a;\r\n" 10917 "int b;\r\n" 10918 "int c;\r\n", 10919 format("int a;\r\n" 10920 " int b;\n" 10921 " int c;\r\n", 10922 getLLVMStyle())); 10923 EXPECT_EQ("int a;\n" 10924 "int b;\n" 10925 "int c;\n", 10926 format("int a;\r\n" 10927 " int b;\n" 10928 " int c;\n", 10929 getLLVMStyle())); 10930 EXPECT_EQ("\"aaaaaaa \"\r\n" 10931 "\"bbbbbbb\";\r\n", 10932 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10933 EXPECT_EQ("#define A \\\r\n" 10934 " b; \\\r\n" 10935 " c; \\\r\n" 10936 " d;\r\n", 10937 format("#define A \\\r\n" 10938 " b; \\\r\n" 10939 " c; d; \r\n", 10940 getGoogleStyle())); 10941 10942 EXPECT_EQ("/*\r\n" 10943 "multi line block comments\r\n" 10944 "should not introduce\r\n" 10945 "an extra carriage return\r\n" 10946 "*/\r\n", 10947 format("/*\r\n" 10948 "multi line block comments\r\n" 10949 "should not introduce\r\n" 10950 "an extra carriage return\r\n" 10951 "*/\r\n")); 10952 } 10953 10954 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10955 verifyFormat("MY_CLASS(C) {\n" 10956 " int i;\n" 10957 " int j;\n" 10958 "};"); 10959 } 10960 10961 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10962 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10963 TwoIndent.ContinuationIndentWidth = 2; 10964 10965 EXPECT_EQ("int i =\n" 10966 " longFunction(\n" 10967 " arg);", 10968 format("int i = longFunction(arg);", TwoIndent)); 10969 10970 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10971 SixIndent.ContinuationIndentWidth = 6; 10972 10973 EXPECT_EQ("int i =\n" 10974 " longFunction(\n" 10975 " arg);", 10976 format("int i = longFunction(arg);", SixIndent)); 10977 } 10978 10979 TEST_F(FormatTest, SpacesInAngles) { 10980 FormatStyle Spaces = getLLVMStyle(); 10981 Spaces.SpacesInAngles = true; 10982 10983 verifyFormat("static_cast< int >(arg);", Spaces); 10984 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10985 verifyFormat("f< int, float >();", Spaces); 10986 verifyFormat("template <> g() {}", Spaces); 10987 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10988 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10989 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10990 Spaces); 10991 10992 Spaces.Standard = FormatStyle::LS_Cpp03; 10993 Spaces.SpacesInAngles = true; 10994 verifyFormat("A< A< int > >();", Spaces); 10995 10996 Spaces.SpacesInAngles = false; 10997 verifyFormat("A<A<int> >();", Spaces); 10998 10999 Spaces.Standard = FormatStyle::LS_Cpp11; 11000 Spaces.SpacesInAngles = true; 11001 verifyFormat("A< A< int > >();", Spaces); 11002 11003 Spaces.SpacesInAngles = false; 11004 verifyFormat("A<A<int>>();", Spaces); 11005 } 11006 11007 TEST_F(FormatTest, TripleAngleBrackets) { 11008 verifyFormat("f<<<1, 1>>>();"); 11009 verifyFormat("f<<<1, 1, 1, s>>>();"); 11010 verifyFormat("f<<<a, b, c, d>>>();"); 11011 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11012 verifyFormat("f<param><<<1, 1>>>();"); 11013 verifyFormat("f<1><<<1, 1>>>();"); 11014 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11015 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11016 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11017 } 11018 11019 TEST_F(FormatTest, MergeLessLessAtEnd) { 11020 verifyFormat("<<"); 11021 EXPECT_EQ("< < <", format("\\\n<<<")); 11022 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11023 "aaallvm::outs() <<"); 11024 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11025 "aaaallvm::outs()\n <<"); 11026 } 11027 11028 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11029 std::string code = "#if A\n" 11030 "#if B\n" 11031 "a.\n" 11032 "#endif\n" 11033 " a = 1;\n" 11034 "#else\n" 11035 "#endif\n" 11036 "#if C\n" 11037 "#else\n" 11038 "#endif\n"; 11039 EXPECT_EQ(code, format(code)); 11040 } 11041 11042 TEST_F(FormatTest, HandleConflictMarkers) { 11043 // Git/SVN conflict markers. 11044 EXPECT_EQ("int a;\n" 11045 "void f() {\n" 11046 " callme(some(parameter1,\n" 11047 "<<<<<<< text by the vcs\n" 11048 " parameter2),\n" 11049 "||||||| text by the vcs\n" 11050 " parameter2),\n" 11051 " parameter3,\n" 11052 "======= text by the vcs\n" 11053 " parameter2, parameter3),\n" 11054 ">>>>>>> text by the vcs\n" 11055 " otherparameter);\n", 11056 format("int a;\n" 11057 "void f() {\n" 11058 " callme(some(parameter1,\n" 11059 "<<<<<<< text by the vcs\n" 11060 " parameter2),\n" 11061 "||||||| text by the vcs\n" 11062 " parameter2),\n" 11063 " parameter3,\n" 11064 "======= text by the vcs\n" 11065 " parameter2,\n" 11066 " parameter3),\n" 11067 ">>>>>>> text by the vcs\n" 11068 " otherparameter);\n")); 11069 11070 // Perforce markers. 11071 EXPECT_EQ("void f() {\n" 11072 " function(\n" 11073 ">>>> text by the vcs\n" 11074 " parameter,\n" 11075 "==== text by the vcs\n" 11076 " parameter,\n" 11077 "==== text by the vcs\n" 11078 " parameter,\n" 11079 "<<<< text by the vcs\n" 11080 " parameter);\n", 11081 format("void f() {\n" 11082 " function(\n" 11083 ">>>> text by the vcs\n" 11084 " parameter,\n" 11085 "==== text by the vcs\n" 11086 " parameter,\n" 11087 "==== text by the vcs\n" 11088 " parameter,\n" 11089 "<<<< text by the vcs\n" 11090 " parameter);\n")); 11091 11092 EXPECT_EQ("<<<<<<<\n" 11093 "|||||||\n" 11094 "=======\n" 11095 ">>>>>>>", 11096 format("<<<<<<<\n" 11097 "|||||||\n" 11098 "=======\n" 11099 ">>>>>>>")); 11100 11101 EXPECT_EQ("<<<<<<<\n" 11102 "|||||||\n" 11103 "int i;\n" 11104 "=======\n" 11105 ">>>>>>>", 11106 format("<<<<<<<\n" 11107 "|||||||\n" 11108 "int i;\n" 11109 "=======\n" 11110 ">>>>>>>")); 11111 11112 // FIXME: Handle parsing of macros around conflict markers correctly: 11113 EXPECT_EQ("#define Macro \\\n" 11114 "<<<<<<<\n" 11115 "Something \\\n" 11116 "|||||||\n" 11117 "Else \\\n" 11118 "=======\n" 11119 "Other \\\n" 11120 ">>>>>>>\n" 11121 " End int i;\n", 11122 format("#define Macro \\\n" 11123 "<<<<<<<\n" 11124 " Something \\\n" 11125 "|||||||\n" 11126 " Else \\\n" 11127 "=======\n" 11128 " Other \\\n" 11129 ">>>>>>>\n" 11130 " End\n" 11131 "int i;\n")); 11132 } 11133 11134 TEST_F(FormatTest, DisableRegions) { 11135 EXPECT_EQ("int i;\n" 11136 "// clang-format off\n" 11137 " int j;\n" 11138 "// clang-format on\n" 11139 "int k;", 11140 format(" int i;\n" 11141 " // clang-format off\n" 11142 " int j;\n" 11143 " // clang-format on\n" 11144 " int k;")); 11145 EXPECT_EQ("int i;\n" 11146 "/* clang-format off */\n" 11147 " int j;\n" 11148 "/* clang-format on */\n" 11149 "int k;", 11150 format(" int i;\n" 11151 " /* clang-format off */\n" 11152 " int j;\n" 11153 " /* clang-format on */\n" 11154 " int k;")); 11155 } 11156 11157 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11158 format("? ) ="); 11159 verifyNoCrash("#define a\\\n /**/}"); 11160 } 11161 11162 TEST_F(FormatTest, FormatsTableGenCode) { 11163 FormatStyle Style = getLLVMStyle(); 11164 Style.Language = FormatStyle::LK_TableGen; 11165 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11166 } 11167 11168 } // end namespace 11169 } // end namespace format 11170 } // end namespace clang 11171