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 " // at start\n" 966 "otherLine();", 967 format("lineWith(); // comment\n" 968 " // at start\n" 969 "otherLine();")); 970 971 EXPECT_EQ("lineWith(); // comment\n" 972 "// at start\n" 973 "otherLine(); // comment", 974 format("lineWith(); // comment\n" 975 "// at start\n" 976 "otherLine(); // comment")); 977 EXPECT_EQ("lineWith();\n" 978 "// at start\n" 979 "otherLine(); // comment", 980 format("lineWith();\n" 981 " // at start\n" 982 "otherLine(); // comment")); 983 EXPECT_EQ("// first\n" 984 "// at start\n" 985 "otherLine(); // comment", 986 format("// first\n" 987 " // at start\n" 988 "otherLine(); // comment")); 989 EXPECT_EQ("f();\n" 990 "// first\n" 991 "// at start\n" 992 "otherLine(); // comment", 993 format("f();\n" 994 "// first\n" 995 " // at start\n" 996 "otherLine(); // comment")); 997 verifyFormat("f(); // comment\n" 998 "// first\n" 999 "// at start\n" 1000 "otherLine();"); 1001 EXPECT_EQ("f(); // comment\n" 1002 "// first\n" 1003 "// at start\n" 1004 "otherLine();", 1005 format("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("void f() {\n" 1018 " lineWith(); // comment\n" 1019 " // at start\n" 1020 "}", 1021 format("void f() {\n" 1022 " lineWith(); // comment\n" 1023 " // at start\n" 1024 "}")); 1025 EXPECT_EQ("int xy; // a\n" 1026 "int z; // b", 1027 format("int xy; // a\n" 1028 "int z; //b")); 1029 EXPECT_EQ("int xy; // a\n" 1030 "int z; // bb", 1031 format("int xy; // a\n" 1032 "int z; //bb", 1033 getLLVMStyleWithColumns(12))); 1034 1035 verifyFormat("#define A \\\n" 1036 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1037 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1038 getLLVMStyleWithColumns(60)); 1039 verifyFormat( 1040 "#define A \\\n" 1041 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1042 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1043 getLLVMStyleWithColumns(61)); 1044 1045 verifyFormat("if ( // This is some comment\n" 1046 " x + 3) {\n" 1047 "}"); 1048 EXPECT_EQ("if ( // This is some comment\n" 1049 " // spanning two lines\n" 1050 " x + 3) {\n" 1051 "}", 1052 format("if( // This is some comment\n" 1053 " // spanning two lines\n" 1054 " x + 3) {\n" 1055 "}")); 1056 1057 verifyNoCrash("/\\\n/"); 1058 verifyNoCrash("/\\\n* */"); 1059 // The 0-character somehow makes the lexer return a proper comment. 1060 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1061 } 1062 1063 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1064 EXPECT_EQ("SomeFunction(a,\n" 1065 " b, // comment\n" 1066 " c);", 1067 format("SomeFunction(a,\n" 1068 " b, // comment\n" 1069 " c);")); 1070 EXPECT_EQ("SomeFunction(a, b,\n" 1071 " // comment\n" 1072 " c);", 1073 format("SomeFunction(a,\n" 1074 " b,\n" 1075 " // comment\n" 1076 " c);")); 1077 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1078 " c);", 1079 format("SomeFunction(a, b, // comment (unclear relation)\n" 1080 " c);")); 1081 EXPECT_EQ("SomeFunction(a, // comment\n" 1082 " b,\n" 1083 " c); // comment", 1084 format("SomeFunction(a, // comment\n" 1085 " b,\n" 1086 " c); // comment")); 1087 } 1088 1089 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1090 EXPECT_EQ("// comment", format("// comment ")); 1091 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1092 format("int aaaaaaa, bbbbbbb; // comment ", 1093 getLLVMStyleWithColumns(33))); 1094 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1095 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1096 } 1097 1098 TEST_F(FormatTest, UnderstandsBlockComments) { 1099 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1100 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1101 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1102 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1103 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1104 "/* Trailing comment for aa... */\n" 1105 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1106 EXPECT_EQ( 1107 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1108 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1109 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1110 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1111 EXPECT_EQ( 1112 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1113 " aaaaaaaaaaaaaaaaaa,\n" 1114 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1115 "}", 1116 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1117 " aaaaaaaaaaaaaaaaaa ,\n" 1118 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1119 "}")); 1120 1121 FormatStyle NoBinPacking = getLLVMStyle(); 1122 NoBinPacking.BinPackParameters = false; 1123 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1124 " /* parameter 2 */ aaaaaa,\n" 1125 " /* parameter 3 */ aaaaaa,\n" 1126 " /* parameter 4 */ aaaaaa);", 1127 NoBinPacking); 1128 1129 // Aligning block comments in macros. 1130 verifyGoogleFormat("#define A \\\n" 1131 " int i; /*a*/ \\\n" 1132 " int jjj; /*b*/"); 1133 } 1134 1135 TEST_F(FormatTest, AlignsBlockComments) { 1136 EXPECT_EQ("/*\n" 1137 " * Really multi-line\n" 1138 " * comment.\n" 1139 " */\n" 1140 "void f() {}", 1141 format(" /*\n" 1142 " * Really multi-line\n" 1143 " * comment.\n" 1144 " */\n" 1145 " void f() {}")); 1146 EXPECT_EQ("class C {\n" 1147 " /*\n" 1148 " * Another multi-line\n" 1149 " * comment.\n" 1150 " */\n" 1151 " void f() {}\n" 1152 "};", 1153 format("class C {\n" 1154 "/*\n" 1155 " * Another multi-line\n" 1156 " * comment.\n" 1157 " */\n" 1158 "void f() {}\n" 1159 "};")); 1160 EXPECT_EQ("/*\n" 1161 " 1. This is a comment with non-trivial formatting.\n" 1162 " 1.1. We have to indent/outdent all lines equally\n" 1163 " 1.1.1. to keep the formatting.\n" 1164 " */", 1165 format(" /*\n" 1166 " 1. This is a comment with non-trivial formatting.\n" 1167 " 1.1. We have to indent/outdent all lines equally\n" 1168 " 1.1.1. to keep the formatting.\n" 1169 " */")); 1170 EXPECT_EQ("/*\n" 1171 "Don't try to outdent if there's not enough indentation.\n" 1172 "*/", 1173 format(" /*\n" 1174 " Don't try to outdent if there's not enough indentation.\n" 1175 " */")); 1176 1177 EXPECT_EQ("int i; /* Comment with empty...\n" 1178 " *\n" 1179 " * line. */", 1180 format("int i; /* Comment with empty...\n" 1181 " *\n" 1182 " * line. */")); 1183 EXPECT_EQ("int foobar = 0; /* comment */\n" 1184 "int bar = 0; /* multiline\n" 1185 " comment 1 */\n" 1186 "int baz = 0; /* multiline\n" 1187 " comment 2 */\n" 1188 "int bzz = 0; /* multiline\n" 1189 " comment 3 */", 1190 format("int foobar = 0; /* comment */\n" 1191 "int bar = 0; /* multiline\n" 1192 " comment 1 */\n" 1193 "int baz = 0; /* multiline\n" 1194 " comment 2 */\n" 1195 "int bzz = 0; /* multiline\n" 1196 " comment 3 */")); 1197 EXPECT_EQ("int foobar = 0; /* comment */\n" 1198 "int bar = 0; /* multiline\n" 1199 " comment */\n" 1200 "int baz = 0; /* multiline\n" 1201 "comment */", 1202 format("int foobar = 0; /* comment */\n" 1203 "int bar = 0; /* multiline\n" 1204 "comment */\n" 1205 "int baz = 0; /* multiline\n" 1206 "comment */")); 1207 } 1208 1209 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { 1210 FormatStyle Style = getLLVMStyleWithColumns(20); 1211 Style.ReflowComments = false; 1212 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 1213 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 1214 } 1215 1216 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1217 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1219 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1221 EXPECT_EQ( 1222 "void ffffffffffff(\n" 1223 " int aaaaaaaa, int bbbbbbbb,\n" 1224 " int cccccccccccc) { /*\n" 1225 " aaaaaaaaaa\n" 1226 " aaaaaaaaaaaaa\n" 1227 " bbbbbbbbbbbbbb\n" 1228 " bbbbbbbbbb\n" 1229 " */\n" 1230 "}", 1231 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1232 "{ /*\n" 1233 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1234 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1235 " */\n" 1236 "}", 1237 getLLVMStyleWithColumns(40))); 1238 } 1239 1240 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1241 EXPECT_EQ("void ffffffffff(\n" 1242 " int aaaaa /* test */);", 1243 format("void ffffffffff(int aaaaa /* test */);", 1244 getLLVMStyleWithColumns(35))); 1245 } 1246 1247 TEST_F(FormatTest, SplitsLongCxxComments) { 1248 EXPECT_EQ("// A comment that\n" 1249 "// doesn't fit on\n" 1250 "// one line", 1251 format("// A comment that doesn't fit on one line", 1252 getLLVMStyleWithColumns(20))); 1253 EXPECT_EQ("/// A comment that\n" 1254 "/// doesn't fit on\n" 1255 "/// one line", 1256 format("/// A comment that doesn't fit on one line", 1257 getLLVMStyleWithColumns(20))); 1258 EXPECT_EQ("//! A comment that\n" 1259 "//! doesn't fit on\n" 1260 "//! one line", 1261 format("//! A comment that doesn't fit on one line", 1262 getLLVMStyleWithColumns(20))); 1263 EXPECT_EQ("// a b c d\n" 1264 "// e f g\n" 1265 "// h i j k", 1266 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1267 EXPECT_EQ( 1268 "// a b c d\n" 1269 "// e f g\n" 1270 "// h i j k", 1271 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1272 EXPECT_EQ("if (true) // A comment that\n" 1273 " // doesn't fit on\n" 1274 " // one line", 1275 format("if (true) // A comment that doesn't fit on one line ", 1276 getLLVMStyleWithColumns(30))); 1277 EXPECT_EQ("// Don't_touch_leading_whitespace", 1278 format("// Don't_touch_leading_whitespace", 1279 getLLVMStyleWithColumns(20))); 1280 EXPECT_EQ("// Add leading\n" 1281 "// whitespace", 1282 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1283 EXPECT_EQ("/// Add leading\n" 1284 "/// whitespace", 1285 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1286 EXPECT_EQ("//! Add leading\n" 1287 "//! whitespace", 1288 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1289 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1290 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1291 "// limit", 1292 format("//Even if it makes the line exceed the column limit", 1293 getLLVMStyleWithColumns(51))); 1294 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1295 1296 EXPECT_EQ("// aa bb cc dd", 1297 format("// aa bb cc dd ", 1298 getLLVMStyleWithColumns(15))); 1299 1300 EXPECT_EQ("// A comment before\n" 1301 "// a macro\n" 1302 "// definition\n" 1303 "#define a b", 1304 format("// A comment before a macro definition\n" 1305 "#define a b", 1306 getLLVMStyleWithColumns(20))); 1307 EXPECT_EQ("void ffffff(\n" 1308 " int aaaaaaaaa, // wwww\n" 1309 " int bbbbbbbbbb, // xxxxxxx\n" 1310 " // yyyyyyyyyy\n" 1311 " int c, int d, int e) {}", 1312 format("void ffffff(\n" 1313 " int aaaaaaaaa, // wwww\n" 1314 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1315 " int c, int d, int e) {}", 1316 getLLVMStyleWithColumns(40))); 1317 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1318 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1319 getLLVMStyleWithColumns(20))); 1320 EXPECT_EQ( 1321 "#define XXX // a b c d\n" 1322 " // e f g h", 1323 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1324 EXPECT_EQ( 1325 "#define XXX // q w e r\n" 1326 " // t y u i", 1327 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1328 } 1329 1330 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1331 EXPECT_EQ("// A comment\n" 1332 "// that doesn't\n" 1333 "// fit on one\n" 1334 "// line", 1335 format("// A comment that doesn't fit on one line", 1336 getLLVMStyleWithColumns(20))); 1337 EXPECT_EQ("/// A comment\n" 1338 "/// that doesn't\n" 1339 "/// fit on one\n" 1340 "/// line", 1341 format("/// A comment that doesn't fit on one line", 1342 getLLVMStyleWithColumns(20))); 1343 } 1344 1345 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1346 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1347 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1348 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1349 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1350 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1351 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1352 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1353 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1354 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1355 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1356 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1357 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1358 getLLVMStyleWithColumns(50))); 1359 // FIXME: One day we might want to implement adjustment of leading whitespace 1360 // of the consecutive lines in this kind of comment: 1361 EXPECT_EQ("double\n" 1362 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1363 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1364 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1365 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1366 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1367 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1368 getLLVMStyleWithColumns(49))); 1369 } 1370 1371 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1372 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1373 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1374 EXPECT_EQ( 1375 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1376 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1377 EXPECT_EQ( 1378 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1379 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1380 } 1381 1382 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1383 EXPECT_EQ("if (xxx ==\n" 1384 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1385 " zzz)\n" 1386 " q();", 1387 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1388 " zzz) q();", 1389 getLLVMStyleWithColumns(40))); 1390 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1391 " yyy && // aaaaaa bbbbbbbb cccc\n" 1392 " zzz)\n" 1393 " q();", 1394 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1395 " zzz) q();", 1396 getLLVMStyleWithColumns(40))); 1397 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1398 " yyy || // aaaaaa bbbbbbbb cccc\n" 1399 " zzz)\n" 1400 " q();", 1401 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1402 " zzz) q();", 1403 getLLVMStyleWithColumns(40))); 1404 EXPECT_EQ("fffffffff(\n" 1405 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1406 " zzz);", 1407 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1408 " zzz);", 1409 getLLVMStyleWithColumns(40))); 1410 } 1411 1412 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1413 EXPECT_EQ("#define A(x) /* \\\n" 1414 " a comment \\\n" 1415 " inside */ \\\n" 1416 " f();", 1417 format("#define A(x) /* \\\n" 1418 " a comment \\\n" 1419 " inside */ \\\n" 1420 " f();", 1421 getLLVMStyleWithColumns(17))); 1422 EXPECT_EQ("#define A( \\\n" 1423 " x) /* \\\n" 1424 " a comment \\\n" 1425 " inside */ \\\n" 1426 " f();", 1427 format("#define A( \\\n" 1428 " x) /* \\\n" 1429 " a comment \\\n" 1430 " inside */ \\\n" 1431 " f();", 1432 getLLVMStyleWithColumns(17))); 1433 } 1434 1435 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1436 EXPECT_EQ("namespace {}\n// Test\n#define A", 1437 format("namespace {}\n // Test\n#define A")); 1438 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1439 format("namespace {}\n /* Test */\n#define A")); 1440 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1441 format("namespace {}\n /* Test */ #define A")); 1442 } 1443 1444 TEST_F(FormatTest, SplitsLongLinesInComments) { 1445 EXPECT_EQ("/* This is a long\n" 1446 " * comment that\n" 1447 " * doesn't\n" 1448 " * fit on one line.\n" 1449 " */", 1450 format("/* " 1451 "This is a long " 1452 "comment that " 1453 "doesn't " 1454 "fit on one line. */", 1455 getLLVMStyleWithColumns(20))); 1456 EXPECT_EQ( 1457 "/* a b c d\n" 1458 " * e f g\n" 1459 " * h i j k\n" 1460 " */", 1461 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1462 EXPECT_EQ( 1463 "/* a b c d\n" 1464 " * e f g\n" 1465 " * h i j k\n" 1466 " */", 1467 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1468 EXPECT_EQ("/*\n" 1469 "This is a long\n" 1470 "comment that doesn't\n" 1471 "fit on one line.\n" 1472 "*/", 1473 format("/*\n" 1474 "This is a long " 1475 "comment that doesn't " 1476 "fit on one line. \n" 1477 "*/", 1478 getLLVMStyleWithColumns(20))); 1479 EXPECT_EQ("/*\n" 1480 " * This is a long\n" 1481 " * comment that\n" 1482 " * doesn't fit on\n" 1483 " * one line.\n" 1484 " */", 1485 format("/* \n" 1486 " * This is a long " 1487 " comment that " 1488 " doesn't fit on " 1489 " one line. \n" 1490 " */", 1491 getLLVMStyleWithColumns(20))); 1492 EXPECT_EQ("/*\n" 1493 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1494 " * so_it_should_be_broken\n" 1495 " * wherever_a_space_occurs\n" 1496 " */", 1497 format("/*\n" 1498 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1499 " so_it_should_be_broken " 1500 " wherever_a_space_occurs \n" 1501 " */", 1502 getLLVMStyleWithColumns(20))); 1503 EXPECT_EQ("/*\n" 1504 " * This_comment_can_not_be_broken_into_lines\n" 1505 " */", 1506 format("/*\n" 1507 " * This_comment_can_not_be_broken_into_lines\n" 1508 " */", 1509 getLLVMStyleWithColumns(20))); 1510 EXPECT_EQ("{\n" 1511 " /*\n" 1512 " This is another\n" 1513 " long comment that\n" 1514 " doesn't fit on one\n" 1515 " line 1234567890\n" 1516 " */\n" 1517 "}", 1518 format("{\n" 1519 "/*\n" 1520 "This is another " 1521 " long comment that " 1522 " doesn't fit on one" 1523 " line 1234567890\n" 1524 "*/\n" 1525 "}", 1526 getLLVMStyleWithColumns(20))); 1527 EXPECT_EQ("{\n" 1528 " /*\n" 1529 " * This i s\n" 1530 " * another comment\n" 1531 " * t hat doesn' t\n" 1532 " * fit on one l i\n" 1533 " * n e\n" 1534 " */\n" 1535 "}", 1536 format("{\n" 1537 "/*\n" 1538 " * This i s" 1539 " another comment" 1540 " t hat doesn' t" 1541 " fit on one l i" 1542 " n e\n" 1543 " */\n" 1544 "}", 1545 getLLVMStyleWithColumns(20))); 1546 EXPECT_EQ("/*\n" 1547 " * This is a long\n" 1548 " * comment that\n" 1549 " * doesn't fit on\n" 1550 " * one line\n" 1551 " */", 1552 format(" /*\n" 1553 " * This is a long comment that doesn't fit on one line\n" 1554 " */", 1555 getLLVMStyleWithColumns(20))); 1556 EXPECT_EQ("{\n" 1557 " if (something) /* This is a\n" 1558 " long\n" 1559 " comment */\n" 1560 " ;\n" 1561 "}", 1562 format("{\n" 1563 " if (something) /* This is a long comment */\n" 1564 " ;\n" 1565 "}", 1566 getLLVMStyleWithColumns(30))); 1567 1568 EXPECT_EQ("/* A comment before\n" 1569 " * a macro\n" 1570 " * definition */\n" 1571 "#define a b", 1572 format("/* A comment before a macro definition */\n" 1573 "#define a b", 1574 getLLVMStyleWithColumns(20))); 1575 1576 EXPECT_EQ("/* some comment\n" 1577 " * a comment\n" 1578 "* that we break\n" 1579 " * another comment\n" 1580 "* we have to break\n" 1581 "* a left comment\n" 1582 " */", 1583 format(" /* some comment\n" 1584 " * a comment that we break\n" 1585 " * another comment we have to break\n" 1586 "* a left comment\n" 1587 " */", 1588 getLLVMStyleWithColumns(20))); 1589 1590 EXPECT_EQ("/**\n" 1591 " * multiline block\n" 1592 " * comment\n" 1593 " *\n" 1594 " */", 1595 format("/**\n" 1596 " * multiline block comment\n" 1597 " *\n" 1598 " */", 1599 getLLVMStyleWithColumns(20))); 1600 1601 EXPECT_EQ("/*\n" 1602 "\n" 1603 "\n" 1604 " */\n", 1605 format(" /* \n" 1606 " \n" 1607 " \n" 1608 " */\n")); 1609 1610 EXPECT_EQ("/* a a */", 1611 format("/* a a */", getLLVMStyleWithColumns(15))); 1612 EXPECT_EQ("/* a a bc */", 1613 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1614 EXPECT_EQ("/* aaa aaa\n" 1615 " * aaaaa */", 1616 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1617 EXPECT_EQ("/* aaa aaa\n" 1618 " * aaaaa */", 1619 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1620 } 1621 1622 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1623 EXPECT_EQ("#define X \\\n" 1624 " /* \\\n" 1625 " Test \\\n" 1626 " Macro comment \\\n" 1627 " with a long \\\n" 1628 " line \\\n" 1629 " */ \\\n" 1630 " A + B", 1631 format("#define X \\\n" 1632 " /*\n" 1633 " Test\n" 1634 " Macro comment with a long line\n" 1635 " */ \\\n" 1636 " A + B", 1637 getLLVMStyleWithColumns(20))); 1638 EXPECT_EQ("#define X \\\n" 1639 " /* Macro comment \\\n" 1640 " with a long \\\n" 1641 " line */ \\\n" 1642 " A + B", 1643 format("#define X \\\n" 1644 " /* Macro comment with a long\n" 1645 " line */ \\\n" 1646 " A + B", 1647 getLLVMStyleWithColumns(20))); 1648 EXPECT_EQ("#define X \\\n" 1649 " /* Macro comment \\\n" 1650 " * with a long \\\n" 1651 " * line */ \\\n" 1652 " A + B", 1653 format("#define X \\\n" 1654 " /* Macro comment with a long line */ \\\n" 1655 " A + B", 1656 getLLVMStyleWithColumns(20))); 1657 } 1658 1659 TEST_F(FormatTest, CommentsInStaticInitializers) { 1660 EXPECT_EQ( 1661 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1662 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1663 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1664 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1665 " aaaaaaaaaaaaaaaaaaaa};", 1666 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1667 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1668 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1669 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1670 " aaaaaaaaaaaaaaaaaaaa };")); 1671 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1672 " bbbbbbbbbbb, ccccccccccc};"); 1673 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1674 " // comment for bb....\n" 1675 " bbbbbbbbbbb, ccccccccccc};"); 1676 verifyGoogleFormat( 1677 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1678 " bbbbbbbbbbb, ccccccccccc};"); 1679 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1680 " // comment for bb....\n" 1681 " bbbbbbbbbbb, ccccccccccc};"); 1682 1683 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1684 " {d, e, f}, // Group #2\n" 1685 " {g, h, i}}; // Group #3"); 1686 verifyFormat("S s = {{// Group #1\n" 1687 " a, b, c},\n" 1688 " {// Group #2\n" 1689 " d, e, f},\n" 1690 " {// Group #3\n" 1691 " g, h, i}};"); 1692 1693 EXPECT_EQ("S s = {\n" 1694 " // Some comment\n" 1695 " a,\n" 1696 "\n" 1697 " // Comment after empty line\n" 1698 " b}", 1699 format("S s = {\n" 1700 " // Some comment\n" 1701 " a,\n" 1702 " \n" 1703 " // Comment after empty line\n" 1704 " b\n" 1705 "}")); 1706 EXPECT_EQ("S s = {\n" 1707 " /* Some comment */\n" 1708 " a,\n" 1709 "\n" 1710 " /* Comment after empty line */\n" 1711 " b}", 1712 format("S s = {\n" 1713 " /* Some comment */\n" 1714 " a,\n" 1715 " \n" 1716 " /* Comment after empty line */\n" 1717 " b\n" 1718 "}")); 1719 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1720 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1721 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1722 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1723 } 1724 1725 TEST_F(FormatTest, IgnoresIf0Contents) { 1726 EXPECT_EQ("#if 0\n" 1727 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1728 "#endif\n" 1729 "void f() {}", 1730 format("#if 0\n" 1731 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1732 "#endif\n" 1733 "void f( ) { }")); 1734 EXPECT_EQ("#if false\n" 1735 "void f( ) { }\n" 1736 "#endif\n" 1737 "void g() {}\n", 1738 format("#if false\n" 1739 "void f( ) { }\n" 1740 "#endif\n" 1741 "void g( ) { }\n")); 1742 EXPECT_EQ("enum E {\n" 1743 " One,\n" 1744 " Two,\n" 1745 "#if 0\n" 1746 "Three,\n" 1747 " Four,\n" 1748 "#endif\n" 1749 " Five\n" 1750 "};", 1751 format("enum E {\n" 1752 " One,Two,\n" 1753 "#if 0\n" 1754 "Three,\n" 1755 " Four,\n" 1756 "#endif\n" 1757 " Five};")); 1758 EXPECT_EQ("enum F {\n" 1759 " One,\n" 1760 "#if 1\n" 1761 " Two,\n" 1762 "#if 0\n" 1763 "Three,\n" 1764 " Four,\n" 1765 "#endif\n" 1766 " Five\n" 1767 "#endif\n" 1768 "};", 1769 format("enum F {\n" 1770 "One,\n" 1771 "#if 1\n" 1772 "Two,\n" 1773 "#if 0\n" 1774 "Three,\n" 1775 " Four,\n" 1776 "#endif\n" 1777 "Five\n" 1778 "#endif\n" 1779 "};")); 1780 EXPECT_EQ("enum G {\n" 1781 " One,\n" 1782 "#if 0\n" 1783 "Two,\n" 1784 "#else\n" 1785 " Three,\n" 1786 "#endif\n" 1787 " Four\n" 1788 "};", 1789 format("enum G {\n" 1790 "One,\n" 1791 "#if 0\n" 1792 "Two,\n" 1793 "#else\n" 1794 "Three,\n" 1795 "#endif\n" 1796 "Four\n" 1797 "};")); 1798 EXPECT_EQ("enum H {\n" 1799 " One,\n" 1800 "#if 0\n" 1801 "#ifdef Q\n" 1802 "Two,\n" 1803 "#else\n" 1804 "Three,\n" 1805 "#endif\n" 1806 "#endif\n" 1807 " Four\n" 1808 "};", 1809 format("enum H {\n" 1810 "One,\n" 1811 "#if 0\n" 1812 "#ifdef Q\n" 1813 "Two,\n" 1814 "#else\n" 1815 "Three,\n" 1816 "#endif\n" 1817 "#endif\n" 1818 "Four\n" 1819 "};")); 1820 EXPECT_EQ("enum I {\n" 1821 " One,\n" 1822 "#if /* test */ 0 || 1\n" 1823 "Two,\n" 1824 "Three,\n" 1825 "#endif\n" 1826 " Four\n" 1827 "};", 1828 format("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 EXPECT_EQ("enum J {\n" 1837 " One,\n" 1838 "#if 0\n" 1839 "#if 0\n" 1840 "Two,\n" 1841 "#else\n" 1842 "Three,\n" 1843 "#endif\n" 1844 "Four,\n" 1845 "#endif\n" 1846 " Five\n" 1847 "};", 1848 format("enum J {\n" 1849 "One,\n" 1850 "#if 0\n" 1851 "#if 0\n" 1852 "Two,\n" 1853 "#else\n" 1854 "Three,\n" 1855 "#endif\n" 1856 "Four,\n" 1857 "#endif\n" 1858 "Five\n" 1859 "};")); 1860 } 1861 1862 //===----------------------------------------------------------------------===// 1863 // Tests for classes, namespaces, etc. 1864 //===----------------------------------------------------------------------===// 1865 1866 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1867 verifyFormat("class A {};"); 1868 } 1869 1870 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1871 verifyFormat("class A {\n" 1872 "public:\n" 1873 "public: // comment\n" 1874 "protected:\n" 1875 "private:\n" 1876 " void f() {}\n" 1877 "};"); 1878 verifyGoogleFormat("class A {\n" 1879 " public:\n" 1880 " protected:\n" 1881 " private:\n" 1882 " void f() {}\n" 1883 "};"); 1884 verifyFormat("class A {\n" 1885 "public slots:\n" 1886 " void f1() {}\n" 1887 "public Q_SLOTS:\n" 1888 " void f2() {}\n" 1889 "protected slots:\n" 1890 " void f3() {}\n" 1891 "protected Q_SLOTS:\n" 1892 " void f4() {}\n" 1893 "private slots:\n" 1894 " void f5() {}\n" 1895 "private Q_SLOTS:\n" 1896 " void f6() {}\n" 1897 "signals:\n" 1898 " void g1();\n" 1899 "Q_SIGNALS:\n" 1900 " void g2();\n" 1901 "};"); 1902 1903 // Don't interpret 'signals' the wrong way. 1904 verifyFormat("signals.set();"); 1905 verifyFormat("for (Signals signals : f()) {\n}"); 1906 verifyFormat("{\n" 1907 " signals.set(); // This needs indentation.\n" 1908 "}"); 1909 } 1910 1911 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1912 EXPECT_EQ("class A {\n" 1913 "public:\n" 1914 " void f();\n" 1915 "\n" 1916 "private:\n" 1917 " void g() {}\n" 1918 " // test\n" 1919 "protected:\n" 1920 " int h;\n" 1921 "};", 1922 format("class A {\n" 1923 "public:\n" 1924 "void f();\n" 1925 "private:\n" 1926 "void g() {}\n" 1927 "// test\n" 1928 "protected:\n" 1929 "int h;\n" 1930 "};")); 1931 EXPECT_EQ("class A {\n" 1932 "protected:\n" 1933 "public:\n" 1934 " void f();\n" 1935 "};", 1936 format("class A {\n" 1937 "protected:\n" 1938 "\n" 1939 "public:\n" 1940 "\n" 1941 " void f();\n" 1942 "};")); 1943 1944 // Even ensure proper spacing inside macros. 1945 EXPECT_EQ("#define B \\\n" 1946 " class A { \\\n" 1947 " protected: \\\n" 1948 " public: \\\n" 1949 " void f(); \\\n" 1950 " };", 1951 format("#define B \\\n" 1952 " class A { \\\n" 1953 " protected: \\\n" 1954 " \\\n" 1955 " public: \\\n" 1956 " \\\n" 1957 " void f(); \\\n" 1958 " };", 1959 getGoogleStyle())); 1960 // But don't remove empty lines after macros ending in access specifiers. 1961 EXPECT_EQ("#define A private:\n" 1962 "\n" 1963 "int i;", 1964 format("#define A private:\n" 1965 "\n" 1966 "int i;")); 1967 } 1968 1969 TEST_F(FormatTest, FormatsClasses) { 1970 verifyFormat("class A : public B {};"); 1971 verifyFormat("class A : public ::B {};"); 1972 1973 verifyFormat( 1974 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1975 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1976 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1977 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1978 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1979 verifyFormat( 1980 "class A : public B, public C, public D, public E, public F {};"); 1981 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1982 " public C,\n" 1983 " public D,\n" 1984 " public E,\n" 1985 " public F,\n" 1986 " public G {};"); 1987 1988 verifyFormat("class\n" 1989 " ReallyReallyLongClassName {\n" 1990 " int i;\n" 1991 "};", 1992 getLLVMStyleWithColumns(32)); 1993 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1994 " aaaaaaaaaaaaaaaa> {};"); 1995 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1996 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1997 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1998 verifyFormat("template <class R, class C>\n" 1999 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2000 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2001 verifyFormat("class ::A::B {};"); 2002 } 2003 2004 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2005 verifyFormat("class A {\n} a, b;"); 2006 verifyFormat("struct A {\n} a, b;"); 2007 verifyFormat("union A {\n} a;"); 2008 } 2009 2010 TEST_F(FormatTest, FormatsEnum) { 2011 verifyFormat("enum {\n" 2012 " Zero,\n" 2013 " One = 1,\n" 2014 " Two = One + 1,\n" 2015 " Three = (One + Two),\n" 2016 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2017 " Five = (One, Two, Three, Four, 5)\n" 2018 "};"); 2019 verifyGoogleFormat("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 verifyFormat("enum Enum {};"); 2028 verifyFormat("enum {};"); 2029 verifyFormat("enum X E {} d;"); 2030 verifyFormat("enum __attribute__((...)) E {} d;"); 2031 verifyFormat("enum __declspec__((...)) E {} d;"); 2032 verifyFormat("enum {\n" 2033 " Bar = Foo<int, int>::value\n" 2034 "};", 2035 getLLVMStyleWithColumns(30)); 2036 2037 verifyFormat("enum ShortEnum { A, B, C };"); 2038 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2039 2040 EXPECT_EQ("enum KeepEmptyLines {\n" 2041 " ONE,\n" 2042 "\n" 2043 " TWO,\n" 2044 "\n" 2045 " THREE\n" 2046 "}", 2047 format("enum KeepEmptyLines {\n" 2048 " ONE,\n" 2049 "\n" 2050 " TWO,\n" 2051 "\n" 2052 "\n" 2053 " THREE\n" 2054 "}")); 2055 verifyFormat("enum E { // comment\n" 2056 " ONE,\n" 2057 " TWO\n" 2058 "};\n" 2059 "int i;"); 2060 // Not enums. 2061 verifyFormat("enum X f() {\n" 2062 " a();\n" 2063 " return 42;\n" 2064 "}"); 2065 verifyFormat("enum X Type::f() {\n" 2066 " a();\n" 2067 " return 42;\n" 2068 "}"); 2069 verifyFormat("enum ::X f() {\n" 2070 " a();\n" 2071 " return 42;\n" 2072 "}"); 2073 verifyFormat("enum ns::X f() {\n" 2074 " a();\n" 2075 " return 42;\n" 2076 "}"); 2077 } 2078 2079 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2080 verifyFormat("enum Type {\n" 2081 " One = 0; // These semicolons should be commas.\n" 2082 " Two = 1;\n" 2083 "};"); 2084 verifyFormat("namespace n {\n" 2085 "enum Type {\n" 2086 " One,\n" 2087 " Two, // missing };\n" 2088 " int i;\n" 2089 "}\n" 2090 "void g() {}"); 2091 } 2092 2093 TEST_F(FormatTest, FormatsEnumStruct) { 2094 verifyFormat("enum struct {\n" 2095 " Zero,\n" 2096 " One = 1,\n" 2097 " Two = One + 1,\n" 2098 " Three = (One + Two),\n" 2099 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2100 " Five = (One, Two, Three, Four, 5)\n" 2101 "};"); 2102 verifyFormat("enum struct Enum {};"); 2103 verifyFormat("enum struct {};"); 2104 verifyFormat("enum struct X E {} d;"); 2105 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2106 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2107 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2108 } 2109 2110 TEST_F(FormatTest, FormatsEnumClass) { 2111 verifyFormat("enum class {\n" 2112 " Zero,\n" 2113 " One = 1,\n" 2114 " Two = One + 1,\n" 2115 " Three = (One + Two),\n" 2116 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2117 " Five = (One, Two, Three, Four, 5)\n" 2118 "};"); 2119 verifyFormat("enum class Enum {};"); 2120 verifyFormat("enum class {};"); 2121 verifyFormat("enum class X E {} d;"); 2122 verifyFormat("enum class __attribute__((...)) E {} d;"); 2123 verifyFormat("enum class __declspec__((...)) E {} d;"); 2124 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2125 } 2126 2127 TEST_F(FormatTest, FormatsEnumTypes) { 2128 verifyFormat("enum X : int {\n" 2129 " A, // Force multiple lines.\n" 2130 " B\n" 2131 "};"); 2132 verifyFormat("enum X : int { A, B };"); 2133 verifyFormat("enum X : std::uint32_t { A, B };"); 2134 } 2135 2136 TEST_F(FormatTest, FormatsNSEnums) { 2137 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2138 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2139 " // Information about someDecentlyLongValue.\n" 2140 " someDecentlyLongValue,\n" 2141 " // Information about anotherDecentlyLongValue.\n" 2142 " anotherDecentlyLongValue,\n" 2143 " // Information about aThirdDecentlyLongValue.\n" 2144 " aThirdDecentlyLongValue\n" 2145 "};"); 2146 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2147 " a = 1,\n" 2148 " b = 2,\n" 2149 " c = 3,\n" 2150 "};"); 2151 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2152 " a = 1,\n" 2153 " b = 2,\n" 2154 " c = 3,\n" 2155 "};"); 2156 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2157 " a = 1,\n" 2158 " b = 2,\n" 2159 " c = 3,\n" 2160 "};"); 2161 } 2162 2163 TEST_F(FormatTest, FormatsBitfields) { 2164 verifyFormat("struct Bitfields {\n" 2165 " unsigned sClass : 8;\n" 2166 " unsigned ValueKind : 2;\n" 2167 "};"); 2168 verifyFormat("struct A {\n" 2169 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2170 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2171 "};"); 2172 verifyFormat("struct MyStruct {\n" 2173 " uchar data;\n" 2174 " uchar : 8;\n" 2175 " uchar : 8;\n" 2176 " uchar other;\n" 2177 "};"); 2178 } 2179 2180 TEST_F(FormatTest, FormatsNamespaces) { 2181 verifyFormat("namespace some_namespace {\n" 2182 "class A {};\n" 2183 "void f() { f(); }\n" 2184 "}"); 2185 verifyFormat("namespace {\n" 2186 "class A {};\n" 2187 "void f() { f(); }\n" 2188 "}"); 2189 verifyFormat("inline namespace X {\n" 2190 "class A {};\n" 2191 "void f() { f(); }\n" 2192 "}"); 2193 verifyFormat("using namespace some_namespace;\n" 2194 "class A {};\n" 2195 "void f() { f(); }"); 2196 2197 // This code is more common than we thought; if we 2198 // layout this correctly the semicolon will go into 2199 // its own line, which is undesirable. 2200 verifyFormat("namespace {};"); 2201 verifyFormat("namespace {\n" 2202 "class A {};\n" 2203 "};"); 2204 2205 verifyFormat("namespace {\n" 2206 "int SomeVariable = 0; // comment\n" 2207 "} // namespace"); 2208 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2209 "#define HEADER_GUARD\n" 2210 "namespace my_namespace {\n" 2211 "int i;\n" 2212 "} // my_namespace\n" 2213 "#endif // HEADER_GUARD", 2214 format("#ifndef HEADER_GUARD\n" 2215 " #define HEADER_GUARD\n" 2216 " namespace my_namespace {\n" 2217 "int i;\n" 2218 "} // my_namespace\n" 2219 "#endif // HEADER_GUARD")); 2220 2221 EXPECT_EQ("namespace A::B {\n" 2222 "class C {};\n" 2223 "}", 2224 format("namespace A::B {\n" 2225 "class C {};\n" 2226 "}")); 2227 2228 FormatStyle Style = getLLVMStyle(); 2229 Style.NamespaceIndentation = FormatStyle::NI_All; 2230 EXPECT_EQ("namespace out {\n" 2231 " int i;\n" 2232 " namespace in {\n" 2233 " int i;\n" 2234 " } // namespace\n" 2235 "} // namespace", 2236 format("namespace out {\n" 2237 "int i;\n" 2238 "namespace in {\n" 2239 "int i;\n" 2240 "} // namespace\n" 2241 "} // namespace", 2242 Style)); 2243 2244 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2245 EXPECT_EQ("namespace out {\n" 2246 "int i;\n" 2247 "namespace in {\n" 2248 " int i;\n" 2249 "} // namespace\n" 2250 "} // namespace", 2251 format("namespace out {\n" 2252 "int i;\n" 2253 "namespace in {\n" 2254 "int i;\n" 2255 "} // namespace\n" 2256 "} // namespace", 2257 Style)); 2258 } 2259 2260 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2261 2262 TEST_F(FormatTest, FormatsInlineASM) { 2263 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2264 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2265 verifyFormat( 2266 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2267 " \"cpuid\\n\\t\"\n" 2268 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2269 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2270 " : \"a\"(value));"); 2271 EXPECT_EQ( 2272 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2273 " __asm {\n" 2274 " mov edx,[that] // vtable in edx\n" 2275 " mov eax,methodIndex\n" 2276 " call [edx][eax*4] // stdcall\n" 2277 " }\n" 2278 "}", 2279 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2280 " __asm {\n" 2281 " mov edx,[that] // vtable in edx\n" 2282 " mov eax,methodIndex\n" 2283 " call [edx][eax*4] // stdcall\n" 2284 " }\n" 2285 "}")); 2286 EXPECT_EQ("_asm {\n" 2287 " xor eax, eax;\n" 2288 " cpuid;\n" 2289 "}", 2290 format("_asm {\n" 2291 " xor eax, eax;\n" 2292 " cpuid;\n" 2293 "}")); 2294 verifyFormat("void function() {\n" 2295 " // comment\n" 2296 " asm(\"\");\n" 2297 "}"); 2298 EXPECT_EQ("__asm {\n" 2299 "}\n" 2300 "int i;", 2301 format("__asm {\n" 2302 "}\n" 2303 "int i;")); 2304 } 2305 2306 TEST_F(FormatTest, FormatTryCatch) { 2307 verifyFormat("try {\n" 2308 " throw a * b;\n" 2309 "} catch (int a) {\n" 2310 " // Do nothing.\n" 2311 "} catch (...) {\n" 2312 " exit(42);\n" 2313 "}"); 2314 2315 // Function-level try statements. 2316 verifyFormat("int f() try { return 4; } catch (...) {\n" 2317 " return 5;\n" 2318 "}"); 2319 verifyFormat("class A {\n" 2320 " int a;\n" 2321 " A() try : a(0) {\n" 2322 " } catch (...) {\n" 2323 " throw;\n" 2324 " }\n" 2325 "};\n"); 2326 2327 // Incomplete try-catch blocks. 2328 verifyIncompleteFormat("try {} catch ("); 2329 } 2330 2331 TEST_F(FormatTest, FormatSEHTryCatch) { 2332 verifyFormat("__try {\n" 2333 " int a = b * c;\n" 2334 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2335 " // Do nothing.\n" 2336 "}"); 2337 2338 verifyFormat("__try {\n" 2339 " int a = b * c;\n" 2340 "} __finally {\n" 2341 " // Do nothing.\n" 2342 "}"); 2343 2344 verifyFormat("DEBUG({\n" 2345 " __try {\n" 2346 " } __finally {\n" 2347 " }\n" 2348 "});\n"); 2349 } 2350 2351 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2352 verifyFormat("try {\n" 2353 " f();\n" 2354 "} catch {\n" 2355 " g();\n" 2356 "}"); 2357 verifyFormat("try {\n" 2358 " f();\n" 2359 "} catch (A a) MACRO(x) {\n" 2360 " g();\n" 2361 "} catch (B b) MACRO(x) {\n" 2362 " g();\n" 2363 "}"); 2364 } 2365 2366 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2367 FormatStyle Style = getLLVMStyle(); 2368 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2369 FormatStyle::BS_WebKit}) { 2370 Style.BreakBeforeBraces = BraceStyle; 2371 verifyFormat("try {\n" 2372 " // something\n" 2373 "} catch (...) {\n" 2374 " // something\n" 2375 "}", 2376 Style); 2377 } 2378 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2379 verifyFormat("try {\n" 2380 " // something\n" 2381 "}\n" 2382 "catch (...) {\n" 2383 " // something\n" 2384 "}", 2385 Style); 2386 verifyFormat("__try {\n" 2387 " // something\n" 2388 "}\n" 2389 "__finally {\n" 2390 " // something\n" 2391 "}", 2392 Style); 2393 verifyFormat("@try {\n" 2394 " // something\n" 2395 "}\n" 2396 "@finally {\n" 2397 " // something\n" 2398 "}", 2399 Style); 2400 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2401 verifyFormat("try\n" 2402 "{\n" 2403 " // something\n" 2404 "}\n" 2405 "catch (...)\n" 2406 "{\n" 2407 " // something\n" 2408 "}", 2409 Style); 2410 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2411 verifyFormat("try\n" 2412 " {\n" 2413 " // something\n" 2414 " }\n" 2415 "catch (...)\n" 2416 " {\n" 2417 " // something\n" 2418 " }", 2419 Style); 2420 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2421 Style.BraceWrapping.BeforeCatch = true; 2422 verifyFormat("try {\n" 2423 " // something\n" 2424 "}\n" 2425 "catch (...) {\n" 2426 " // something\n" 2427 "}", 2428 Style); 2429 } 2430 2431 TEST_F(FormatTest, FormatObjCTryCatch) { 2432 verifyFormat("@try {\n" 2433 " f();\n" 2434 "} @catch (NSException e) {\n" 2435 " @throw;\n" 2436 "} @finally {\n" 2437 " exit(42);\n" 2438 "}"); 2439 verifyFormat("DEBUG({\n" 2440 " @try {\n" 2441 " } @finally {\n" 2442 " }\n" 2443 "});\n"); 2444 } 2445 2446 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2447 FormatStyle Style = getLLVMStyle(); 2448 verifyFormat("@autoreleasepool {\n" 2449 " f();\n" 2450 "}\n" 2451 "@autoreleasepool {\n" 2452 " f();\n" 2453 "}\n", 2454 Style); 2455 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2456 verifyFormat("@autoreleasepool\n" 2457 "{\n" 2458 " f();\n" 2459 "}\n" 2460 "@autoreleasepool\n" 2461 "{\n" 2462 " f();\n" 2463 "}\n", 2464 Style); 2465 } 2466 2467 TEST_F(FormatTest, StaticInitializers) { 2468 verifyFormat("static SomeClass SC = {1, 'a'};"); 2469 2470 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2471 " 100000000, " 2472 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2473 2474 // Here, everything other than the "}" would fit on a line. 2475 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2476 " 10000000000000000000000000};"); 2477 EXPECT_EQ("S s = {a,\n" 2478 "\n" 2479 " b};", 2480 format("S s = {\n" 2481 " a,\n" 2482 "\n" 2483 " b\n" 2484 "};")); 2485 2486 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2487 // line. However, the formatting looks a bit off and this probably doesn't 2488 // happen often in practice. 2489 verifyFormat("static int Variable[1] = {\n" 2490 " {1000000000000000000000000000000000000}};", 2491 getLLVMStyleWithColumns(40)); 2492 } 2493 2494 TEST_F(FormatTest, DesignatedInitializers) { 2495 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2496 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2497 " .bbbbbbbbbb = 2,\n" 2498 " .cccccccccc = 3,\n" 2499 " .dddddddddd = 4,\n" 2500 " .eeeeeeeeee = 5};"); 2501 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2502 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2503 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2504 " .ccccccccccccccccccccccccccc = 3,\n" 2505 " .ddddddddddddddddddddddddddd = 4,\n" 2506 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2507 2508 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2509 } 2510 2511 TEST_F(FormatTest, NestedStaticInitializers) { 2512 verifyFormat("static A x = {{{}}};\n"); 2513 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2514 " {init1, init2, init3, init4}}};", 2515 getLLVMStyleWithColumns(50)); 2516 2517 verifyFormat("somes Status::global_reps[3] = {\n" 2518 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2519 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2520 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2521 getLLVMStyleWithColumns(60)); 2522 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2523 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2524 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2525 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2526 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2527 " {rect.fRight - rect.fLeft, rect.fBottom - " 2528 "rect.fTop}};"); 2529 2530 verifyFormat( 2531 "SomeArrayOfSomeType a = {\n" 2532 " {{1, 2, 3},\n" 2533 " {1, 2, 3},\n" 2534 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2535 " 333333333333333333333333333333},\n" 2536 " {1, 2, 3},\n" 2537 " {1, 2, 3}}};"); 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 2547 verifyFormat("struct {\n" 2548 " unsigned bit;\n" 2549 " const char *const name;\n" 2550 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2551 " {kOsWin, \"Windows\"},\n" 2552 " {kOsLinux, \"Linux\"},\n" 2553 " {kOsCrOS, \"Chrome OS\"}};"); 2554 verifyFormat("struct {\n" 2555 " unsigned bit;\n" 2556 " const char *const name;\n" 2557 "} kBitsToOs[] = {\n" 2558 " {kOsMac, \"Mac\"},\n" 2559 " {kOsWin, \"Windows\"},\n" 2560 " {kOsLinux, \"Linux\"},\n" 2561 " {kOsCrOS, \"Chrome OS\"},\n" 2562 "};"); 2563 } 2564 2565 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2566 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2567 " \\\n" 2568 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2569 } 2570 2571 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2572 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2573 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2574 2575 // Do break defaulted and deleted functions. 2576 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2577 " default;", 2578 getLLVMStyleWithColumns(40)); 2579 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2580 " delete;", 2581 getLLVMStyleWithColumns(40)); 2582 } 2583 2584 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2585 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2586 getLLVMStyleWithColumns(40)); 2587 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2588 getLLVMStyleWithColumns(40)); 2589 EXPECT_EQ("#define Q \\\n" 2590 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2591 " \"aaaaaaaa.cpp\"", 2592 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2593 getLLVMStyleWithColumns(40))); 2594 } 2595 2596 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2597 EXPECT_EQ("# 123 \"A string literal\"", 2598 format(" # 123 \"A string literal\"")); 2599 } 2600 2601 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2602 EXPECT_EQ("#;", format("#;")); 2603 verifyFormat("#\n;\n;\n;"); 2604 } 2605 2606 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2607 EXPECT_EQ("#line 42 \"test\"\n", 2608 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2609 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2610 getLLVMStyleWithColumns(12))); 2611 } 2612 2613 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2614 EXPECT_EQ("#line 42 \"test\"", 2615 format("# \\\n line \\\n 42 \\\n \"test\"")); 2616 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2617 } 2618 2619 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2620 verifyFormat("#define A \\x20"); 2621 verifyFormat("#define A \\ x20"); 2622 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2623 verifyFormat("#define A ''"); 2624 verifyFormat("#define A ''qqq"); 2625 verifyFormat("#define A `qqq"); 2626 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2627 EXPECT_EQ("const char *c = STRINGIFY(\n" 2628 "\\na : b);", 2629 format("const char * c = STRINGIFY(\n" 2630 "\\na : b);")); 2631 2632 verifyFormat("a\r\\"); 2633 verifyFormat("a\v\\"); 2634 verifyFormat("a\f\\"); 2635 } 2636 2637 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2638 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2639 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2640 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2641 // FIXME: We never break before the macro name. 2642 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2643 2644 verifyFormat("#define A A\n#define A A"); 2645 verifyFormat("#define A(X) A\n#define A A"); 2646 2647 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2648 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2649 } 2650 2651 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2652 EXPECT_EQ("// somecomment\n" 2653 "#include \"a.h\"\n" 2654 "#define A( \\\n" 2655 " A, B)\n" 2656 "#include \"b.h\"\n" 2657 "// somecomment\n", 2658 format(" // somecomment\n" 2659 " #include \"a.h\"\n" 2660 "#define A(A,\\\n" 2661 " B)\n" 2662 " #include \"b.h\"\n" 2663 " // somecomment\n", 2664 getLLVMStyleWithColumns(13))); 2665 } 2666 2667 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2668 2669 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2670 EXPECT_EQ("#define A \\\n" 2671 " c; \\\n" 2672 " e;\n" 2673 "f;", 2674 format("#define A c; e;\n" 2675 "f;", 2676 getLLVMStyleWithColumns(14))); 2677 } 2678 2679 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2680 2681 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2682 EXPECT_EQ("int x,\n" 2683 "#define A\n" 2684 " y;", 2685 format("int x,\n#define A\ny;")); 2686 } 2687 2688 TEST_F(FormatTest, HashInMacroDefinition) { 2689 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2690 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2691 verifyFormat("#define A \\\n" 2692 " { \\\n" 2693 " f(#c); \\\n" 2694 " }", 2695 getLLVMStyleWithColumns(11)); 2696 2697 verifyFormat("#define A(X) \\\n" 2698 " void function##X()", 2699 getLLVMStyleWithColumns(22)); 2700 2701 verifyFormat("#define A(a, b, c) \\\n" 2702 " void a##b##c()", 2703 getLLVMStyleWithColumns(22)); 2704 2705 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2706 } 2707 2708 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2709 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2710 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2711 } 2712 2713 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2714 EXPECT_EQ("#define A b;", format("#define A \\\n" 2715 " \\\n" 2716 " b;", 2717 getLLVMStyleWithColumns(25))); 2718 EXPECT_EQ("#define A \\\n" 2719 " \\\n" 2720 " a; \\\n" 2721 " b;", 2722 format("#define A \\\n" 2723 " \\\n" 2724 " a; \\\n" 2725 " b;", 2726 getLLVMStyleWithColumns(11))); 2727 EXPECT_EQ("#define A \\\n" 2728 " a; \\\n" 2729 " \\\n" 2730 " b;", 2731 format("#define A \\\n" 2732 " a; \\\n" 2733 " \\\n" 2734 " b;", 2735 getLLVMStyleWithColumns(11))); 2736 } 2737 2738 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2739 verifyIncompleteFormat("#define A :"); 2740 verifyFormat("#define SOMECASES \\\n" 2741 " case 1: \\\n" 2742 " case 2\n", 2743 getLLVMStyleWithColumns(20)); 2744 verifyFormat("#define A template <typename T>"); 2745 verifyIncompleteFormat("#define STR(x) #x\n" 2746 "f(STR(this_is_a_string_literal{));"); 2747 verifyFormat("#pragma omp threadprivate( \\\n" 2748 " y)), // expected-warning", 2749 getLLVMStyleWithColumns(28)); 2750 verifyFormat("#d, = };"); 2751 verifyFormat("#if \"a"); 2752 verifyIncompleteFormat("({\n" 2753 "#define b \\\n" 2754 " } \\\n" 2755 " a\n" 2756 "a", 2757 getLLVMStyleWithColumns(15)); 2758 verifyFormat("#define A \\\n" 2759 " { \\\n" 2760 " {\n" 2761 "#define B \\\n" 2762 " } \\\n" 2763 " }", 2764 getLLVMStyleWithColumns(15)); 2765 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2766 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2767 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2768 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2769 } 2770 2771 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2772 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2773 EXPECT_EQ("class A : public QObject {\n" 2774 " Q_OBJECT\n" 2775 "\n" 2776 " A() {}\n" 2777 "};", 2778 format("class A : public QObject {\n" 2779 " Q_OBJECT\n" 2780 "\n" 2781 " A() {\n}\n" 2782 "} ;")); 2783 EXPECT_EQ("MACRO\n" 2784 "/*static*/ int i;", 2785 format("MACRO\n" 2786 " /*static*/ int i;")); 2787 EXPECT_EQ("SOME_MACRO\n" 2788 "namespace {\n" 2789 "void f();\n" 2790 "}", 2791 format("SOME_MACRO\n" 2792 " namespace {\n" 2793 "void f( );\n" 2794 "}")); 2795 // Only if the identifier contains at least 5 characters. 2796 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2797 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2798 // Only if everything is upper case. 2799 EXPECT_EQ("class A : public QObject {\n" 2800 " Q_Object A() {}\n" 2801 "};", 2802 format("class A : public QObject {\n" 2803 " Q_Object\n" 2804 " A() {\n}\n" 2805 "} ;")); 2806 2807 // Only if the next line can actually start an unwrapped line. 2808 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2809 format("SOME_WEIRD_LOG_MACRO\n" 2810 "<< SomeThing;")); 2811 2812 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2813 "(n, buffers))\n", 2814 getChromiumStyle(FormatStyle::LK_Cpp)); 2815 } 2816 2817 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2818 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2819 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2820 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2821 "class X {};\n" 2822 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2823 "int *createScopDetectionPass() { return 0; }", 2824 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2825 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2826 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2827 " class X {};\n" 2828 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2829 " int *createScopDetectionPass() { return 0; }")); 2830 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2831 // braces, so that inner block is indented one level more. 2832 EXPECT_EQ("int q() {\n" 2833 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2834 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2835 " IPC_END_MESSAGE_MAP()\n" 2836 "}", 2837 format("int q() {\n" 2838 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2839 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2840 " IPC_END_MESSAGE_MAP()\n" 2841 "}")); 2842 2843 // Same inside macros. 2844 EXPECT_EQ("#define LIST(L) \\\n" 2845 " L(A) \\\n" 2846 " L(B) \\\n" 2847 " L(C)", 2848 format("#define LIST(L) \\\n" 2849 " L(A) \\\n" 2850 " L(B) \\\n" 2851 " L(C)", 2852 getGoogleStyle())); 2853 2854 // These must not be recognized as macros. 2855 EXPECT_EQ("int q() {\n" 2856 " f(x);\n" 2857 " f(x) {}\n" 2858 " f(x)->g();\n" 2859 " f(x)->*g();\n" 2860 " f(x).g();\n" 2861 " f(x) = x;\n" 2862 " f(x) += x;\n" 2863 " f(x) -= x;\n" 2864 " f(x) *= x;\n" 2865 " f(x) /= x;\n" 2866 " f(x) %= x;\n" 2867 " f(x) &= x;\n" 2868 " f(x) |= x;\n" 2869 " f(x) ^= x;\n" 2870 " f(x) >>= x;\n" 2871 " f(x) <<= x;\n" 2872 " f(x)[y].z();\n" 2873 " LOG(INFO) << x;\n" 2874 " ifstream(x) >> x;\n" 2875 "}\n", 2876 format("int q() {\n" 2877 " f(x)\n;\n" 2878 " f(x)\n {}\n" 2879 " f(x)\n->g();\n" 2880 " f(x)\n->*g();\n" 2881 " f(x)\n.g();\n" 2882 " f(x)\n = x;\n" 2883 " f(x)\n += x;\n" 2884 " f(x)\n -= x;\n" 2885 " f(x)\n *= x;\n" 2886 " f(x)\n /= x;\n" 2887 " f(x)\n %= x;\n" 2888 " f(x)\n &= x;\n" 2889 " f(x)\n |= x;\n" 2890 " f(x)\n ^= x;\n" 2891 " f(x)\n >>= x;\n" 2892 " f(x)\n <<= x;\n" 2893 " f(x)\n[y].z();\n" 2894 " LOG(INFO)\n << x;\n" 2895 " ifstream(x)\n >> x;\n" 2896 "}\n")); 2897 EXPECT_EQ("int q() {\n" 2898 " F(x)\n" 2899 " if (1) {\n" 2900 " }\n" 2901 " F(x)\n" 2902 " while (1) {\n" 2903 " }\n" 2904 " F(x)\n" 2905 " G(x);\n" 2906 " F(x)\n" 2907 " try {\n" 2908 " Q();\n" 2909 " } catch (...) {\n" 2910 " }\n" 2911 "}\n", 2912 format("int q() {\n" 2913 "F(x)\n" 2914 "if (1) {}\n" 2915 "F(x)\n" 2916 "while (1) {}\n" 2917 "F(x)\n" 2918 "G(x);\n" 2919 "F(x)\n" 2920 "try { Q(); } catch (...) {}\n" 2921 "}\n")); 2922 EXPECT_EQ("class A {\n" 2923 " A() : t(0) {}\n" 2924 " A(int i) noexcept() : {}\n" 2925 " A(X x)\n" // FIXME: function-level try blocks are broken. 2926 " try : t(0) {\n" 2927 " } catch (...) {\n" 2928 " }\n" 2929 "};", 2930 format("class A {\n" 2931 " A()\n : t(0) {}\n" 2932 " A(int i)\n noexcept() : {}\n" 2933 " A(X x)\n" 2934 " try : t(0) {} catch (...) {}\n" 2935 "};")); 2936 EXPECT_EQ("class SomeClass {\n" 2937 "public:\n" 2938 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2939 "};", 2940 format("class SomeClass {\n" 2941 "public:\n" 2942 " SomeClass()\n" 2943 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2944 "};")); 2945 EXPECT_EQ("class SomeClass {\n" 2946 "public:\n" 2947 " SomeClass()\n" 2948 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2949 "};", 2950 format("class SomeClass {\n" 2951 "public:\n" 2952 " SomeClass()\n" 2953 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2954 "};", 2955 getLLVMStyleWithColumns(40))); 2956 2957 verifyFormat("MACRO(>)"); 2958 } 2959 2960 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2961 verifyFormat("#define A \\\n" 2962 " f({ \\\n" 2963 " g(); \\\n" 2964 " });", 2965 getLLVMStyleWithColumns(11)); 2966 } 2967 2968 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2969 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2970 } 2971 2972 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2973 verifyFormat("{\n { a #c; }\n}"); 2974 } 2975 2976 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2977 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2978 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2979 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2980 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2981 } 2982 2983 TEST_F(FormatTest, EscapedNewlines) { 2984 EXPECT_EQ( 2985 "#define A \\\n int i; \\\n int j;", 2986 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2987 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2988 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2989 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 2990 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2991 } 2992 2993 TEST_F(FormatTest, DontCrashOnBlockComments) { 2994 EXPECT_EQ( 2995 "int xxxxxxxxx; /* " 2996 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 2997 "zzzzzz\n" 2998 "0*/", 2999 format("int xxxxxxxxx; /* " 3000 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3001 "0*/")); 3002 } 3003 3004 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3005 verifyFormat("#define A \\\n" 3006 " int v( \\\n" 3007 " a); \\\n" 3008 " int i;", 3009 getLLVMStyleWithColumns(11)); 3010 } 3011 3012 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3013 EXPECT_EQ( 3014 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3015 " \\\n" 3016 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3017 "\n" 3018 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3019 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3020 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3021 "\\\n" 3022 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3023 " \n" 3024 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3025 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3026 } 3027 3028 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3029 EXPECT_EQ("int\n" 3030 "#define A\n" 3031 " a;", 3032 format("int\n#define A\na;")); 3033 verifyFormat("functionCallTo(\n" 3034 " someOtherFunction(\n" 3035 " withSomeParameters, whichInSequence,\n" 3036 " areLongerThanALine(andAnotherCall,\n" 3037 "#define A B\n" 3038 " withMoreParamters,\n" 3039 " whichStronglyInfluenceTheLayout),\n" 3040 " andMoreParameters),\n" 3041 " trailing);", 3042 getLLVMStyleWithColumns(69)); 3043 verifyFormat("Foo::Foo()\n" 3044 "#ifdef BAR\n" 3045 " : baz(0)\n" 3046 "#endif\n" 3047 "{\n" 3048 "}"); 3049 verifyFormat("void f() {\n" 3050 " if (true)\n" 3051 "#ifdef A\n" 3052 " f(42);\n" 3053 " x();\n" 3054 "#else\n" 3055 " g();\n" 3056 " x();\n" 3057 "#endif\n" 3058 "}"); 3059 verifyFormat("void f(param1, param2,\n" 3060 " param3,\n" 3061 "#ifdef A\n" 3062 " param4(param5,\n" 3063 "#ifdef A1\n" 3064 " param6,\n" 3065 "#ifdef A2\n" 3066 " param7),\n" 3067 "#else\n" 3068 " param8),\n" 3069 " param9,\n" 3070 "#endif\n" 3071 " param10,\n" 3072 "#endif\n" 3073 " param11)\n" 3074 "#else\n" 3075 " param12)\n" 3076 "#endif\n" 3077 "{\n" 3078 " x();\n" 3079 "}", 3080 getLLVMStyleWithColumns(28)); 3081 verifyFormat("#if 1\n" 3082 "int i;"); 3083 verifyFormat("#if 1\n" 3084 "#endif\n" 3085 "#if 1\n" 3086 "#else\n" 3087 "#endif\n"); 3088 verifyFormat("DEBUG({\n" 3089 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3091 "});\n" 3092 "#if a\n" 3093 "#else\n" 3094 "#endif"); 3095 3096 verifyIncompleteFormat("void f(\n" 3097 "#if A\n" 3098 " );\n" 3099 "#else\n" 3100 "#endif"); 3101 } 3102 3103 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3104 verifyFormat("#endif\n" 3105 "#if B"); 3106 } 3107 3108 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3109 FormatStyle SingleLine = getLLVMStyle(); 3110 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3111 verifyFormat("#if 0\n" 3112 "#elif 1\n" 3113 "#endif\n" 3114 "void foo() {\n" 3115 " if (test) foo2();\n" 3116 "}", 3117 SingleLine); 3118 } 3119 3120 TEST_F(FormatTest, LayoutBlockInsideParens) { 3121 verifyFormat("functionCall({ int i; });"); 3122 verifyFormat("functionCall({\n" 3123 " int i;\n" 3124 " int j;\n" 3125 "});"); 3126 verifyFormat("functionCall(\n" 3127 " {\n" 3128 " int i;\n" 3129 " int j;\n" 3130 " },\n" 3131 " aaaa, bbbb, cccc);"); 3132 verifyFormat("functionA(functionB({\n" 3133 " int i;\n" 3134 " int j;\n" 3135 " }),\n" 3136 " aaaa, bbbb, cccc);"); 3137 verifyFormat("functionCall(\n" 3138 " {\n" 3139 " int i;\n" 3140 " int j;\n" 3141 " },\n" 3142 " aaaa, bbbb, // comment\n" 3143 " cccc);"); 3144 verifyFormat("functionA(functionB({\n" 3145 " int i;\n" 3146 " int j;\n" 3147 " }),\n" 3148 " aaaa, bbbb, // comment\n" 3149 " cccc);"); 3150 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3151 verifyFormat("functionCall(aaaa, bbbb, {\n" 3152 " int i;\n" 3153 " int j;\n" 3154 "});"); 3155 verifyFormat( 3156 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3157 " {\n" 3158 " int i; // break\n" 3159 " },\n" 3160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3161 " ccccccccccccccccc));"); 3162 verifyFormat("DEBUG({\n" 3163 " if (a)\n" 3164 " f();\n" 3165 "});"); 3166 } 3167 3168 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3169 EXPECT_EQ("SOME_MACRO { int i; }\n" 3170 "int i;", 3171 format(" SOME_MACRO {int i;} int i;")); 3172 } 3173 3174 TEST_F(FormatTest, LayoutNestedBlocks) { 3175 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3176 " struct s {\n" 3177 " int i;\n" 3178 " };\n" 3179 " s kBitsToOs[] = {{10}};\n" 3180 " for (int i = 0; i < 10; ++i)\n" 3181 " return;\n" 3182 "}"); 3183 verifyFormat("call(parameter, {\n" 3184 " something();\n" 3185 " // Comment using all columns.\n" 3186 " somethingelse();\n" 3187 "});", 3188 getLLVMStyleWithColumns(40)); 3189 verifyFormat("DEBUG( //\n" 3190 " { f(); }, a);"); 3191 verifyFormat("DEBUG( //\n" 3192 " {\n" 3193 " f(); //\n" 3194 " },\n" 3195 " a);"); 3196 3197 EXPECT_EQ("call(parameter, {\n" 3198 " something();\n" 3199 " // Comment too\n" 3200 " // looooooooooong.\n" 3201 " somethingElse();\n" 3202 "});", 3203 format("call(parameter, {\n" 3204 " something();\n" 3205 " // Comment too looooooooooong.\n" 3206 " somethingElse();\n" 3207 "});", 3208 getLLVMStyleWithColumns(29))); 3209 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3210 EXPECT_EQ("DEBUG({ // comment\n" 3211 " int i;\n" 3212 "});", 3213 format("DEBUG({ // comment\n" 3214 "int i;\n" 3215 "});")); 3216 EXPECT_EQ("DEBUG({\n" 3217 " int i;\n" 3218 "\n" 3219 " // comment\n" 3220 " int j;\n" 3221 "});", 3222 format("DEBUG({\n" 3223 " int i;\n" 3224 "\n" 3225 " // comment\n" 3226 " int j;\n" 3227 "});")); 3228 3229 verifyFormat("DEBUG({\n" 3230 " if (a)\n" 3231 " return;\n" 3232 "});"); 3233 verifyGoogleFormat("DEBUG({\n" 3234 " if (a) return;\n" 3235 "});"); 3236 FormatStyle Style = getGoogleStyle(); 3237 Style.ColumnLimit = 45; 3238 verifyFormat("Debug(aaaaa,\n" 3239 " {\n" 3240 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3241 " },\n" 3242 " a);", 3243 Style); 3244 3245 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3246 3247 verifyNoCrash("^{v^{a}}"); 3248 } 3249 3250 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3251 EXPECT_EQ("#define MACRO() \\\n" 3252 " Debug(aaa, /* force line break */ \\\n" 3253 " { \\\n" 3254 " int i; \\\n" 3255 " int j; \\\n" 3256 " })", 3257 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3258 " { int i; int j; })", 3259 getGoogleStyle())); 3260 3261 EXPECT_EQ("#define A \\\n" 3262 " [] { \\\n" 3263 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3264 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3265 " }", 3266 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3267 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3268 getGoogleStyle())); 3269 } 3270 3271 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3272 EXPECT_EQ("{}", format("{}")); 3273 verifyFormat("enum E {};"); 3274 verifyFormat("enum E {}"); 3275 } 3276 3277 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3278 FormatStyle Style = getLLVMStyle(); 3279 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3280 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3281 verifyFormat("FOO_BEGIN\n" 3282 " FOO_ENTRY\n" 3283 "FOO_END", Style); 3284 verifyFormat("FOO_BEGIN\n" 3285 " NESTED_FOO_BEGIN\n" 3286 " NESTED_FOO_ENTRY\n" 3287 " NESTED_FOO_END\n" 3288 "FOO_END", Style); 3289 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3290 " int x;\n" 3291 " x = 1;\n" 3292 "FOO_END(Baz)", Style); 3293 } 3294 3295 //===----------------------------------------------------------------------===// 3296 // Line break tests. 3297 //===----------------------------------------------------------------------===// 3298 3299 TEST_F(FormatTest, PreventConfusingIndents) { 3300 verifyFormat( 3301 "void f() {\n" 3302 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3303 " parameter, parameter, parameter)),\n" 3304 " SecondLongCall(parameter));\n" 3305 "}"); 3306 verifyFormat( 3307 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3308 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3309 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3310 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3311 verifyFormat( 3312 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3313 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3314 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3315 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3316 verifyFormat( 3317 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3318 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3319 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3320 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3321 verifyFormat("int a = bbbb && ccc && fffff(\n" 3322 "#define A Just forcing a new line\n" 3323 " ddd);"); 3324 } 3325 3326 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3327 verifyFormat( 3328 "bool aaaaaaa =\n" 3329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3330 " bbbbbbbb();"); 3331 verifyFormat( 3332 "bool aaaaaaa =\n" 3333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3334 " bbbbbbbb();"); 3335 3336 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3338 " ccccccccc == ddddddddddd;"); 3339 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3341 " ccccccccc == ddddddddddd;"); 3342 verifyFormat( 3343 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3345 " ccccccccc == ddddddddddd;"); 3346 3347 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3348 " aaaaaa) &&\n" 3349 " bbbbbb && cccccc;"); 3350 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3351 " aaaaaa) >>\n" 3352 " bbbbbb;"); 3353 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3354 " SourceMgr.getSpellingColumnNumber(\n" 3355 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3356 " 1);"); 3357 3358 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3359 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3360 " cccccc) {\n}"); 3361 verifyFormat("b = a &&\n" 3362 " // Comment\n" 3363 " b.c && d;"); 3364 3365 // If the LHS of a comparison is not a binary expression itself, the 3366 // additional linebreak confuses many people. 3367 verifyFormat( 3368 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3369 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3370 "}"); 3371 verifyFormat( 3372 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3374 "}"); 3375 verifyFormat( 3376 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3378 "}"); 3379 // Even explicit parentheses stress the precedence enough to make the 3380 // additional break unnecessary. 3381 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3383 "}"); 3384 // This cases is borderline, but with the indentation it is still readable. 3385 verifyFormat( 3386 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3387 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3389 "}", 3390 getLLVMStyleWithColumns(75)); 3391 3392 // If the LHS is a binary expression, we should still use the additional break 3393 // as otherwise the formatting hides the operator precedence. 3394 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3396 " 5) {\n" 3397 "}"); 3398 3399 FormatStyle OnePerLine = getLLVMStyle(); 3400 OnePerLine.BinPackParameters = false; 3401 verifyFormat( 3402 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3405 OnePerLine); 3406 } 3407 3408 TEST_F(FormatTest, ExpressionIndentation) { 3409 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3413 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3414 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3417 " ccccccccccccccccccccccccccccccccccccccccc;"); 3418 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3421 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3422 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3425 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3426 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3429 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3430 verifyFormat("if () {\n" 3431 "} else if (aaaaa &&\n" 3432 " bbbbb > // break\n" 3433 " ccccc) {\n" 3434 "}"); 3435 3436 // Presence of a trailing comment used to change indentation of b. 3437 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3438 " b;\n" 3439 "return aaaaaaaaaaaaaaaaaaa +\n" 3440 " b; //", 3441 getLLVMStyleWithColumns(30)); 3442 } 3443 3444 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3445 // Not sure what the best system is here. Like this, the LHS can be found 3446 // immediately above an operator (everything with the same or a higher 3447 // indent). The RHS is aligned right of the operator and so compasses 3448 // everything until something with the same indent as the operator is found. 3449 // FIXME: Is this a good system? 3450 FormatStyle Style = getLLVMStyle(); 3451 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3452 verifyFormat( 3453 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3454 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3455 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3456 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3457 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3458 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3459 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3460 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3461 " > ccccccccccccccccccccccccccccccccccccccccc;", 3462 Style); 3463 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3464 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3465 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3466 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3467 Style); 3468 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3469 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3470 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3471 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3472 Style); 3473 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3474 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3475 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3476 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3477 Style); 3478 verifyFormat("if () {\n" 3479 "} else if (aaaaa\n" 3480 " && bbbbb // break\n" 3481 " > ccccc) {\n" 3482 "}", 3483 Style); 3484 verifyFormat("return (a)\n" 3485 " // comment\n" 3486 " + b;", 3487 Style); 3488 verifyFormat( 3489 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3490 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3491 " + cc;", 3492 Style); 3493 3494 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3495 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3496 Style); 3497 3498 // Forced by comments. 3499 verifyFormat( 3500 "unsigned ContentSize =\n" 3501 " sizeof(int16_t) // DWARF ARange version number\n" 3502 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3503 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3504 " + sizeof(int8_t); // Segment Size (in bytes)"); 3505 3506 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3507 " == boost::fusion::at_c<1>(iiii).second;", 3508 Style); 3509 3510 Style.ColumnLimit = 60; 3511 verifyFormat("zzzzzzzzzz\n" 3512 " = bbbbbbbbbbbbbbbbb\n" 3513 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3514 Style); 3515 } 3516 3517 TEST_F(FormatTest, NoOperandAlignment) { 3518 FormatStyle Style = getLLVMStyle(); 3519 Style.AlignOperands = false; 3520 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3521 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3522 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3523 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3524 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3525 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3526 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3527 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3528 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3529 " > ccccccccccccccccccccccccccccccccccccccccc;", 3530 Style); 3531 3532 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3533 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3534 " + cc;", 3535 Style); 3536 verifyFormat("int a = aa\n" 3537 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3538 " * cccccccccccccccccccccccccccccccccccc;", 3539 Style); 3540 3541 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3542 verifyFormat("return (a > b\n" 3543 " // comment1\n" 3544 " // comment2\n" 3545 " || c);", 3546 Style); 3547 } 3548 3549 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3550 FormatStyle Style = getLLVMStyle(); 3551 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3552 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3554 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3555 Style); 3556 } 3557 3558 TEST_F(FormatTest, ConstructorInitializers) { 3559 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3560 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3561 getLLVMStyleWithColumns(45)); 3562 verifyFormat("Constructor()\n" 3563 " : Inttializer(FitsOnTheLine) {}", 3564 getLLVMStyleWithColumns(44)); 3565 verifyFormat("Constructor()\n" 3566 " : Inttializer(FitsOnTheLine) {}", 3567 getLLVMStyleWithColumns(43)); 3568 3569 verifyFormat("template <typename T>\n" 3570 "Constructor() : Initializer(FitsOnTheLine) {}", 3571 getLLVMStyleWithColumns(45)); 3572 3573 verifyFormat( 3574 "SomeClass::Constructor()\n" 3575 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3576 3577 verifyFormat( 3578 "SomeClass::Constructor()\n" 3579 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3580 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3581 verifyFormat( 3582 "SomeClass::Constructor()\n" 3583 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3584 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3585 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3586 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3587 " : aaaaaaaaaa(aaaaaa) {}"); 3588 3589 verifyFormat("Constructor()\n" 3590 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3591 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3592 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3593 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3594 3595 verifyFormat("Constructor()\n" 3596 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3598 3599 verifyFormat("Constructor(int Parameter = 0)\n" 3600 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3601 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3602 verifyFormat("Constructor()\n" 3603 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3604 "}", 3605 getLLVMStyleWithColumns(60)); 3606 verifyFormat("Constructor()\n" 3607 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3608 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3609 3610 // Here a line could be saved by splitting the second initializer onto two 3611 // lines, but that is not desirable. 3612 verifyFormat("Constructor()\n" 3613 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3614 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3615 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3616 3617 FormatStyle OnePerLine = getLLVMStyle(); 3618 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3619 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3620 verifyFormat("SomeClass::Constructor()\n" 3621 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3622 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3623 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3624 OnePerLine); 3625 verifyFormat("SomeClass::Constructor()\n" 3626 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3627 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3628 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3629 OnePerLine); 3630 verifyFormat("MyClass::MyClass(int var)\n" 3631 " : some_var_(var), // 4 space indent\n" 3632 " some_other_var_(var + 1) { // lined up\n" 3633 "}", 3634 OnePerLine); 3635 verifyFormat("Constructor()\n" 3636 " : aaaaa(aaaaaa),\n" 3637 " aaaaa(aaaaaa),\n" 3638 " aaaaa(aaaaaa),\n" 3639 " aaaaa(aaaaaa),\n" 3640 " aaaaa(aaaaaa) {}", 3641 OnePerLine); 3642 verifyFormat("Constructor()\n" 3643 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3644 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3645 OnePerLine); 3646 OnePerLine.BinPackParameters = false; 3647 verifyFormat( 3648 "Constructor()\n" 3649 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3650 " aaaaaaaaaaa().aaa(),\n" 3651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3652 OnePerLine); 3653 OnePerLine.ColumnLimit = 60; 3654 verifyFormat("Constructor()\n" 3655 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3656 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3657 OnePerLine); 3658 3659 EXPECT_EQ("Constructor()\n" 3660 " : // Comment forcing unwanted break.\n" 3661 " aaaa(aaaa) {}", 3662 format("Constructor() :\n" 3663 " // Comment forcing unwanted break.\n" 3664 " aaaa(aaaa) {}")); 3665 } 3666 3667 TEST_F(FormatTest, MemoizationTests) { 3668 // This breaks if the memoization lookup does not take \c Indent and 3669 // \c LastSpace into account. 3670 verifyFormat( 3671 "extern CFRunLoopTimerRef\n" 3672 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3673 " CFTimeInterval interval, CFOptionFlags flags,\n" 3674 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3675 " CFRunLoopTimerContext *context) {}"); 3676 3677 // Deep nesting somewhat works around our memoization. 3678 verifyFormat( 3679 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3680 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3681 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3682 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3683 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3684 getLLVMStyleWithColumns(65)); 3685 verifyFormat( 3686 "aaaaa(\n" 3687 " aaaaa,\n" 3688 " aaaaa(\n" 3689 " aaaaa,\n" 3690 " aaaaa(\n" 3691 " aaaaa,\n" 3692 " aaaaa(\n" 3693 " aaaaa,\n" 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))))))))))));", 3711 getLLVMStyleWithColumns(65)); 3712 verifyFormat( 3713 "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" 3714 " a),\n" 3715 " a),\n" 3716 " a),\n" 3717 " a),\n" 3718 " a),\n" 3719 " a),\n" 3720 " a),\n" 3721 " 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)", 3731 getLLVMStyleWithColumns(65)); 3732 3733 // This test takes VERY long when memoization is broken. 3734 FormatStyle OnePerLine = getLLVMStyle(); 3735 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3736 OnePerLine.BinPackParameters = false; 3737 std::string input = "Constructor()\n" 3738 " : aaaa(a,\n"; 3739 for (unsigned i = 0, e = 80; i != e; ++i) { 3740 input += " a,\n"; 3741 } 3742 input += " a) {}"; 3743 verifyFormat(input, OnePerLine); 3744 } 3745 3746 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3747 verifyFormat( 3748 "void f() {\n" 3749 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3750 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3751 " f();\n" 3752 "}"); 3753 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3754 " Intervals[i - 1].getRange().getLast()) {\n}"); 3755 } 3756 3757 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3758 // Principially, we break function declarations in a certain order: 3759 // 1) break amongst arguments. 3760 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3761 " Cccccccccccccc cccccccccccccc);"); 3762 verifyFormat("template <class TemplateIt>\n" 3763 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3764 " TemplateIt *stop) {}"); 3765 3766 // 2) break after return type. 3767 verifyFormat( 3768 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3769 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3770 getGoogleStyle()); 3771 3772 // 3) break after (. 3773 verifyFormat( 3774 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3775 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3776 getGoogleStyle()); 3777 3778 // 4) break before after nested name specifiers. 3779 verifyFormat( 3780 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3781 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3782 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3783 getGoogleStyle()); 3784 3785 // However, there are exceptions, if a sufficient amount of lines can be 3786 // saved. 3787 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3788 // more adjusting. 3789 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3790 " Cccccccccccccc cccccccccc,\n" 3791 " Cccccccccccccc cccccccccc,\n" 3792 " Cccccccccccccc cccccccccc,\n" 3793 " Cccccccccccccc cccccccccc);"); 3794 verifyFormat( 3795 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3796 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3797 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3798 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3799 getGoogleStyle()); 3800 verifyFormat( 3801 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3802 " Cccccccccccccc cccccccccc,\n" 3803 " Cccccccccccccc cccccccccc,\n" 3804 " Cccccccccccccc cccccccccc,\n" 3805 " Cccccccccccccc cccccccccc,\n" 3806 " Cccccccccccccc cccccccccc,\n" 3807 " Cccccccccccccc cccccccccc);"); 3808 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3809 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3810 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3811 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3812 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3813 3814 // Break after multi-line parameters. 3815 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3817 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3818 " bbbb bbbb);"); 3819 verifyFormat("void SomeLoooooooooooongFunction(\n" 3820 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3821 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3822 " int bbbbbbbbbbbbb);"); 3823 3824 // Treat overloaded operators like other functions. 3825 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3826 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3827 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3828 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3829 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3830 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3831 verifyGoogleFormat( 3832 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3833 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3834 verifyGoogleFormat( 3835 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3836 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3837 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3838 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3839 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3840 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3841 verifyGoogleFormat( 3842 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3843 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3844 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3845 3846 FormatStyle Style = getLLVMStyle(); 3847 Style.PointerAlignment = FormatStyle::PAS_Left; 3848 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3849 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3850 Style); 3851 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3853 Style); 3854 } 3855 3856 TEST_F(FormatTest, TrailingReturnType) { 3857 verifyFormat("auto foo() -> int;\n"); 3858 verifyFormat("struct S {\n" 3859 " auto bar() const -> int;\n" 3860 "};"); 3861 verifyFormat("template <size_t Order, typename T>\n" 3862 "auto load_img(const std::string &filename)\n" 3863 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3864 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3865 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3866 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3867 verifyFormat("template <typename T>\n" 3868 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3869 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3870 3871 // Not trailing return types. 3872 verifyFormat("void f() { auto a = b->c(); }"); 3873 } 3874 3875 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3876 // Avoid breaking before trailing 'const' or other trailing annotations, if 3877 // they are not function-like. 3878 FormatStyle Style = getGoogleStyle(); 3879 Style.ColumnLimit = 47; 3880 verifyFormat("void someLongFunction(\n" 3881 " int someLoooooooooooooongParameter) const {\n}", 3882 getLLVMStyleWithColumns(47)); 3883 verifyFormat("LoooooongReturnType\n" 3884 "someLoooooooongFunction() const {}", 3885 getLLVMStyleWithColumns(47)); 3886 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3887 " const {}", 3888 Style); 3889 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3890 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3891 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3892 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3893 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3894 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3895 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3896 " aaaaaaaaaaa aaaaa) const override;"); 3897 verifyGoogleFormat( 3898 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3899 " const override;"); 3900 3901 // Even if the first parameter has to be wrapped. 3902 verifyFormat("void someLongFunction(\n" 3903 " int someLongParameter) const {}", 3904 getLLVMStyleWithColumns(46)); 3905 verifyFormat("void someLongFunction(\n" 3906 " int someLongParameter) const {}", 3907 Style); 3908 verifyFormat("void someLongFunction(\n" 3909 " int someLongParameter) override {}", 3910 Style); 3911 verifyFormat("void someLongFunction(\n" 3912 " int someLongParameter) OVERRIDE {}", 3913 Style); 3914 verifyFormat("void someLongFunction(\n" 3915 " int someLongParameter) final {}", 3916 Style); 3917 verifyFormat("void someLongFunction(\n" 3918 " int someLongParameter) FINAL {}", 3919 Style); 3920 verifyFormat("void someLongFunction(\n" 3921 " int parameter) const override {}", 3922 Style); 3923 3924 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3925 verifyFormat("void someLongFunction(\n" 3926 " int someLongParameter) const\n" 3927 "{\n" 3928 "}", 3929 Style); 3930 3931 // Unless these are unknown annotations. 3932 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3933 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3934 " LONG_AND_UGLY_ANNOTATION;"); 3935 3936 // Breaking before function-like trailing annotations is fine to keep them 3937 // close to their arguments. 3938 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3939 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3940 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3941 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3942 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3943 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3944 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3945 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3946 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3947 3948 verifyFormat( 3949 "void aaaaaaaaaaaaaaaaaa()\n" 3950 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3951 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3952 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3953 " __attribute__((unused));"); 3954 verifyGoogleFormat( 3955 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3956 " GUARDED_BY(aaaaaaaaaaaa);"); 3957 verifyGoogleFormat( 3958 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3959 " GUARDED_BY(aaaaaaaaaaaa);"); 3960 verifyGoogleFormat( 3961 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3962 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3963 verifyGoogleFormat( 3964 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3965 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3966 } 3967 3968 TEST_F(FormatTest, FunctionAnnotations) { 3969 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3970 "int OldFunction(const string ¶meter) {}"); 3971 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3972 "string OldFunction(const string ¶meter) {}"); 3973 verifyFormat("template <typename T>\n" 3974 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3975 "string OldFunction(const string ¶meter) {}"); 3976 3977 // Not function annotations. 3978 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3979 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3980 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3981 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3982 } 3983 3984 TEST_F(FormatTest, BreaksDesireably) { 3985 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3986 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3987 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3988 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3990 "}"); 3991 3992 verifyFormat( 3993 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3995 3996 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3997 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3998 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3999 4000 verifyFormat( 4001 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4003 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4005 4006 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4007 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4008 4009 verifyFormat( 4010 "void f() {\n" 4011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4013 "}"); 4014 verifyFormat( 4015 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4017 verifyFormat( 4018 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4020 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4023 4024 // Indent consistently independent of call expression and unary operator. 4025 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4026 " dddddddddddddddddddddddddddddd));"); 4027 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4028 " dddddddddddddddddddddddddddddd));"); 4029 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4030 " dddddddddddddddddddddddddddddd));"); 4031 4032 // This test case breaks on an incorrect memoization, i.e. an optimization not 4033 // taking into account the StopAt value. 4034 verifyFormat( 4035 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4036 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4037 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4038 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4039 4040 verifyFormat("{\n {\n {\n" 4041 " Annotation.SpaceRequiredBefore =\n" 4042 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4043 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4044 " }\n }\n}"); 4045 4046 // Break on an outer level if there was a break on an inner level. 4047 EXPECT_EQ("f(g(h(a, // comment\n" 4048 " b, c),\n" 4049 " d, e),\n" 4050 " x, y);", 4051 format("f(g(h(a, // comment\n" 4052 " b, c), d, e), x, y);")); 4053 4054 // Prefer breaking similar line breaks. 4055 verifyFormat( 4056 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4057 " NSTrackingMouseEnteredAndExited |\n" 4058 " NSTrackingActiveAlways;"); 4059 } 4060 4061 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4062 FormatStyle NoBinPacking = getGoogleStyle(); 4063 NoBinPacking.BinPackParameters = false; 4064 NoBinPacking.BinPackArguments = true; 4065 verifyFormat("void f() {\n" 4066 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4068 "}", 4069 NoBinPacking); 4070 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4071 " int aaaaaaaaaaaaaaaaaaaa,\n" 4072 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4073 NoBinPacking); 4074 4075 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4076 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4077 " vector<int> bbbbbbbbbbbbbbb);", 4078 NoBinPacking); 4079 // FIXME: This behavior difference is probably not wanted. However, currently 4080 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4081 // template arguments from BreakBeforeParameter being set because of the 4082 // one-per-line formatting. 4083 verifyFormat( 4084 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4085 " aaaaaaaaaa> aaaaaaaaaa);", 4086 NoBinPacking); 4087 verifyFormat( 4088 "void fffffffffff(\n" 4089 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4090 " aaaaaaaaaa);"); 4091 } 4092 4093 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4094 FormatStyle NoBinPacking = getGoogleStyle(); 4095 NoBinPacking.BinPackParameters = false; 4096 NoBinPacking.BinPackArguments = false; 4097 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4098 " aaaaaaaaaaaaaaaaaaaa,\n" 4099 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4100 NoBinPacking); 4101 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4102 " aaaaaaaaaaaaa,\n" 4103 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4104 NoBinPacking); 4105 verifyFormat( 4106 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4107 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4109 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4111 NoBinPacking); 4112 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4113 " .aaaaaaaaaaaaaaaaaa();", 4114 NoBinPacking); 4115 verifyFormat("void f() {\n" 4116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4117 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4118 "}", 4119 NoBinPacking); 4120 4121 verifyFormat( 4122 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4123 " aaaaaaaaaaaa,\n" 4124 " aaaaaaaaaaaa);", 4125 NoBinPacking); 4126 verifyFormat( 4127 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4128 " ddddddddddddddddddddddddddddd),\n" 4129 " test);", 4130 NoBinPacking); 4131 4132 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4133 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4135 " aaaaaaaaaaaaaaaaaa;", 4136 NoBinPacking); 4137 verifyFormat("a(\"a\"\n" 4138 " \"a\",\n" 4139 " a);"); 4140 4141 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4142 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4143 " aaaaaaaaa,\n" 4144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4145 NoBinPacking); 4146 verifyFormat( 4147 "void f() {\n" 4148 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4149 " .aaaaaaa();\n" 4150 "}", 4151 NoBinPacking); 4152 verifyFormat( 4153 "template <class SomeType, class SomeOtherType>\n" 4154 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4155 NoBinPacking); 4156 } 4157 4158 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4159 FormatStyle Style = getLLVMStyleWithColumns(15); 4160 Style.ExperimentalAutoDetectBinPacking = true; 4161 EXPECT_EQ("aaa(aaaa,\n" 4162 " aaaa,\n" 4163 " aaaa);\n" 4164 "aaa(aaaa,\n" 4165 " aaaa,\n" 4166 " aaaa);", 4167 format("aaa(aaaa,\n" // one-per-line 4168 " aaaa,\n" 4169 " aaaa );\n" 4170 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4171 Style)); 4172 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4173 " aaaa);\n" 4174 "aaa(aaaa, aaaa,\n" 4175 " aaaa);", 4176 format("aaa(aaaa, aaaa,\n" // bin-packed 4177 " aaaa );\n" 4178 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4179 Style)); 4180 } 4181 4182 TEST_F(FormatTest, FormatsBuilderPattern) { 4183 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4184 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4185 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4186 " .StartsWith(\".init\", ORDER_INIT)\n" 4187 " .StartsWith(\".fini\", ORDER_FINI)\n" 4188 " .StartsWith(\".hash\", ORDER_HASH)\n" 4189 " .Default(ORDER_TEXT);\n"); 4190 4191 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4192 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4193 verifyFormat( 4194 "aaaaaaa->aaaaaaa\n" 4195 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4197 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4198 verifyFormat( 4199 "aaaaaaa->aaaaaaa\n" 4200 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4201 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4202 verifyFormat( 4203 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4204 " aaaaaaaaaaaaaa);"); 4205 verifyFormat( 4206 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4207 " aaaaaa->aaaaaaaaaaaa()\n" 4208 " ->aaaaaaaaaaaaaaaa(\n" 4209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4210 " ->aaaaaaaaaaaaaaaaa();"); 4211 verifyGoogleFormat( 4212 "void f() {\n" 4213 " someo->Add((new util::filetools::Handler(dir))\n" 4214 " ->OnEvent1(NewPermanentCallback(\n" 4215 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4216 " ->OnEvent2(NewPermanentCallback(\n" 4217 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4218 " ->OnEvent3(NewPermanentCallback(\n" 4219 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4220 " ->OnEvent5(NewPermanentCallback(\n" 4221 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4222 " ->OnEvent6(NewPermanentCallback(\n" 4223 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4224 "}"); 4225 4226 verifyFormat( 4227 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4228 verifyFormat("aaaaaaaaaaaaaaa()\n" 4229 " .aaaaaaaaaaaaaaa()\n" 4230 " .aaaaaaaaaaaaaaa()\n" 4231 " .aaaaaaaaaaaaaaa()\n" 4232 " .aaaaaaaaaaaaaaa();"); 4233 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4234 " .aaaaaaaaaaaaaaa()\n" 4235 " .aaaaaaaaaaaaaaa()\n" 4236 " .aaaaaaaaaaaaaaa();"); 4237 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4238 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4239 " .aaaaaaaaaaaaaaa();"); 4240 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4241 " ->aaaaaaaaaaaaaae(0)\n" 4242 " ->aaaaaaaaaaaaaaa();"); 4243 4244 // Don't linewrap after very short segments. 4245 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4246 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4247 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4248 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4249 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4250 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4251 verifyFormat("aaa()\n" 4252 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4253 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4254 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4255 4256 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4257 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4258 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4259 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4260 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4262 4263 // Prefer not to break after empty parentheses. 4264 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4265 " First->LastNewlineOffset);"); 4266 4267 // Prefer not to create "hanging" indents. 4268 verifyFormat( 4269 "return !soooooooooooooome_map\n" 4270 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4271 " .second;"); 4272 verifyFormat( 4273 "return aaaaaaaaaaaaaaaa\n" 4274 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4275 " .aaaa(aaaaaaaaaaaaaa);"); 4276 // No hanging indent here. 4277 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4279 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4281 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4282 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4283 getLLVMStyleWithColumns(60)); 4284 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4285 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4286 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4287 getLLVMStyleWithColumns(59)); 4288 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4290 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4291 } 4292 4293 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4294 verifyFormat( 4295 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4296 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4297 verifyFormat( 4298 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4299 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4300 4301 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4302 " ccccccccccccccccccccccccc) {\n}"); 4303 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4304 " ccccccccccccccccccccccccc) {\n}"); 4305 4306 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4307 " ccccccccccccccccccccccccc) {\n}"); 4308 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4309 " ccccccccccccccccccccccccc) {\n}"); 4310 4311 verifyFormat( 4312 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4313 " ccccccccccccccccccccccccc) {\n}"); 4314 verifyFormat( 4315 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4316 " ccccccccccccccccccccccccc) {\n}"); 4317 4318 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4319 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4320 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4321 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4322 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4323 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4324 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4325 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4326 4327 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4328 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4329 " aaaaaaaaaaaaaaa != aa) {\n}"); 4330 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4331 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4332 " aaaaaaaaaaaaaaa != aa) {\n}"); 4333 } 4334 4335 TEST_F(FormatTest, BreaksAfterAssignments) { 4336 verifyFormat( 4337 "unsigned Cost =\n" 4338 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4339 " SI->getPointerAddressSpaceee());\n"); 4340 verifyFormat( 4341 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4342 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4343 4344 verifyFormat( 4345 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4346 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4347 verifyFormat("unsigned OriginalStartColumn =\n" 4348 " SourceMgr.getSpellingColumnNumber(\n" 4349 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4350 " 1;"); 4351 } 4352 4353 TEST_F(FormatTest, AlignsAfterAssignments) { 4354 verifyFormat( 4355 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4356 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4357 verifyFormat( 4358 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4359 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4360 verifyFormat( 4361 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4362 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4363 verifyFormat( 4364 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4365 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4366 verifyFormat( 4367 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4368 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4369 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4370 } 4371 4372 TEST_F(FormatTest, AlignsAfterReturn) { 4373 verifyFormat( 4374 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4375 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4376 verifyFormat( 4377 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4378 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4379 verifyFormat( 4380 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4381 " aaaaaaaaaaaaaaaaaaaaaa();"); 4382 verifyFormat( 4383 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4384 " aaaaaaaaaaaaaaaaaaaaaa());"); 4385 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4387 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4390 verifyFormat("return\n" 4391 " // true if code is one of a or b.\n" 4392 " code == a || code == b;"); 4393 } 4394 4395 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4396 verifyFormat( 4397 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4398 " aaaaaaaaa aaaaaaa) {}"); 4399 verifyFormat( 4400 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4401 " aaaaaaaaaaa aaaaaaaaa);"); 4402 verifyFormat( 4403 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4404 " aaaaaaaaaaaaaaaaaaaaa));"); 4405 FormatStyle Style = getLLVMStyle(); 4406 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4407 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4408 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4409 Style); 4410 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4411 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4412 Style); 4413 verifyFormat("SomeLongVariableName->someFunction(\n" 4414 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4415 Style); 4416 verifyFormat( 4417 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4418 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4419 Style); 4420 verifyFormat( 4421 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4422 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4423 Style); 4424 verifyFormat( 4425 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4426 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4427 Style); 4428 4429 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4430 Style.BinPackArguments = false; 4431 Style.BinPackParameters = false; 4432 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4433 " aaaaaaaaaaa aaaaaaaa,\n" 4434 " aaaaaaaaa aaaaaaa,\n" 4435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4436 Style); 4437 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4438 " aaaaaaaaaaa aaaaaaaaa,\n" 4439 " aaaaaaaaaaa aaaaaaaaa,\n" 4440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4441 Style); 4442 verifyFormat("SomeLongVariableName->someFunction(\n" 4443 " foooooooo(\n" 4444 " aaaaaaaaaaaaaaa,\n" 4445 " aaaaaaaaaaaaaaaaaaaaa,\n" 4446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4447 Style); 4448 } 4449 4450 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4451 FormatStyle Style = getLLVMStyleWithColumns(40); 4452 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4453 " bbbbbbbbbbbbbbbbbbbbbb);", 4454 Style); 4455 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4456 Style.AlignOperands = false; 4457 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4458 " bbbbbbbbbbbbbbbbbbbbbb);", 4459 Style); 4460 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4461 Style.AlignOperands = true; 4462 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4463 " bbbbbbbbbbbbbbbbbbbbbb);", 4464 Style); 4465 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4466 Style.AlignOperands = false; 4467 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4468 " bbbbbbbbbbbbbbbbbbbbbb);", 4469 Style); 4470 } 4471 4472 TEST_F(FormatTest, BreaksConditionalExpressions) { 4473 verifyFormat( 4474 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4475 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4476 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4477 verifyFormat( 4478 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4479 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4480 verifyFormat( 4481 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4482 " : aaaaaaaaaaaaa);"); 4483 verifyFormat( 4484 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4485 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4486 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4487 " aaaaaaaaaaaaa);"); 4488 verifyFormat( 4489 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4490 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4491 " aaaaaaaaaaaaa);"); 4492 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4493 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4495 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4497 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4499 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4501 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4503 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4504 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4506 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4508 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4509 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4510 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4511 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4512 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4514 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4515 " : aaaaaaaaaaaaaaaa;"); 4516 verifyFormat( 4517 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4518 " ? aaaaaaaaaaaaaaa\n" 4519 " : aaaaaaaaaaaaaaa;"); 4520 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4521 " aaaaaaaaa\n" 4522 " ? b\n" 4523 " : c);"); 4524 verifyFormat("return aaaa == bbbb\n" 4525 " // comment\n" 4526 " ? aaaa\n" 4527 " : bbbb;"); 4528 verifyFormat("unsigned Indent =\n" 4529 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4530 " ? IndentForLevel[TheLine.Level]\n" 4531 " : TheLine * 2,\n" 4532 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4533 getLLVMStyleWithColumns(70)); 4534 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4535 " ? aaaaaaaaaaaaaaa\n" 4536 " : bbbbbbbbbbbbbbb //\n" 4537 " ? ccccccccccccccc\n" 4538 " : ddddddddddddddd;"); 4539 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4540 " ? aaaaaaaaaaaaaaa\n" 4541 " : (bbbbbbbbbbbbbbb //\n" 4542 " ? ccccccccccccccc\n" 4543 " : ddddddddddddddd);"); 4544 verifyFormat( 4545 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4546 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4547 " aaaaaaaaaaaaaaaaaaaaa +\n" 4548 " aaaaaaaaaaaaaaaaaaaaa\n" 4549 " : aaaaaaaaaa;"); 4550 verifyFormat( 4551 "aaaaaa = aaaaaaaaaaaa\n" 4552 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4553 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4554 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4555 4556 FormatStyle NoBinPacking = getLLVMStyle(); 4557 NoBinPacking.BinPackArguments = false; 4558 verifyFormat( 4559 "void f() {\n" 4560 " g(aaa,\n" 4561 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4563 " ? aaaaaaaaaaaaaaa\n" 4564 " : aaaaaaaaaaaaaaa);\n" 4565 "}", 4566 NoBinPacking); 4567 verifyFormat( 4568 "void f() {\n" 4569 " g(aaa,\n" 4570 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4572 " ?: aaaaaaaaaaaaaaa);\n" 4573 "}", 4574 NoBinPacking); 4575 4576 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4577 " // comment.\n" 4578 " ccccccccccccccccccccccccccccccccccccccc\n" 4579 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4580 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4581 4582 // Assignments in conditional expressions. Apparently not uncommon :-(. 4583 verifyFormat("return a != b\n" 4584 " // comment\n" 4585 " ? a = b\n" 4586 " : a = b;"); 4587 verifyFormat("return a != b\n" 4588 " // comment\n" 4589 " ? a = a != b\n" 4590 " // comment\n" 4591 " ? a = b\n" 4592 " : a\n" 4593 " : a;\n"); 4594 verifyFormat("return a != b\n" 4595 " // comment\n" 4596 " ? a\n" 4597 " : a = a != b\n" 4598 " // comment\n" 4599 " ? a = b\n" 4600 " : a;"); 4601 } 4602 4603 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4604 FormatStyle Style = getLLVMStyle(); 4605 Style.BreakBeforeTernaryOperators = false; 4606 Style.ColumnLimit = 70; 4607 verifyFormat( 4608 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4611 Style); 4612 verifyFormat( 4613 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4615 Style); 4616 verifyFormat( 4617 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4618 " aaaaaaaaaaaaa);", 4619 Style); 4620 verifyFormat( 4621 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4622 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4624 " aaaaaaaaaaaaa);", 4625 Style); 4626 verifyFormat( 4627 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4628 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4629 " aaaaaaaaaaaaa);", 4630 Style); 4631 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4633 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4636 Style); 4637 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4643 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4644 Style); 4645 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4649 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4650 Style); 4651 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4652 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4653 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4654 Style); 4655 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4659 Style); 4660 verifyFormat( 4661 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4662 " aaaaaaaaaaaaaaa :\n" 4663 " aaaaaaaaaaaaaaa;", 4664 Style); 4665 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4666 " aaaaaaaaa ?\n" 4667 " b :\n" 4668 " c);", 4669 Style); 4670 verifyFormat( 4671 "unsigned Indent =\n" 4672 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4673 " IndentForLevel[TheLine.Level] :\n" 4674 " TheLine * 2,\n" 4675 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4676 Style); 4677 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4678 " aaaaaaaaaaaaaaa :\n" 4679 " bbbbbbbbbbbbbbb ? //\n" 4680 " ccccccccccccccc :\n" 4681 " ddddddddddddddd;", 4682 Style); 4683 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4684 " aaaaaaaaaaaaaaa :\n" 4685 " (bbbbbbbbbbbbbbb ? //\n" 4686 " ccccccccccccccc :\n" 4687 " ddddddddddddddd);", 4688 Style); 4689 } 4690 4691 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4692 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4693 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4694 verifyFormat("bool a = true, b = false;"); 4695 4696 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4698 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4699 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4700 verifyFormat( 4701 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4702 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4703 " d = e && f;"); 4704 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4705 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4706 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4707 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4708 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4709 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4710 4711 FormatStyle Style = getGoogleStyle(); 4712 Style.PointerAlignment = FormatStyle::PAS_Left; 4713 Style.DerivePointerAlignment = false; 4714 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4715 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4716 " *b = bbbbbbbbbbbbbbbbbbb;", 4717 Style); 4718 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4719 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4720 Style); 4721 } 4722 4723 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4724 verifyFormat("arr[foo ? bar : baz];"); 4725 verifyFormat("f()[foo ? bar : baz];"); 4726 verifyFormat("(a + b)[foo ? bar : baz];"); 4727 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4728 } 4729 4730 TEST_F(FormatTest, AlignsStringLiterals) { 4731 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4732 " \"short literal\");"); 4733 verifyFormat( 4734 "looooooooooooooooooooooooongFunction(\n" 4735 " \"short literal\"\n" 4736 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4737 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4738 " \" string literals\",\n" 4739 " and, other, parameters);"); 4740 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4741 " \"5678\";", 4742 format("fun + \"1243\" /* comment */\n" 4743 " \"5678\";", 4744 getLLVMStyleWithColumns(28))); 4745 EXPECT_EQ( 4746 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4747 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4748 " \"aaaaaaaaaaaaaaaa\";", 4749 format("aaaaaa =" 4750 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4751 "aaaaaaaaaaaaaaaaaaaaa\" " 4752 "\"aaaaaaaaaaaaaaaa\";")); 4753 verifyFormat("a = a + \"a\"\n" 4754 " \"a\"\n" 4755 " \"a\";"); 4756 verifyFormat("f(\"a\", \"b\"\n" 4757 " \"c\");"); 4758 4759 verifyFormat( 4760 "#define LL_FORMAT \"ll\"\n" 4761 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4762 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4763 4764 verifyFormat("#define A(X) \\\n" 4765 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4766 " \"ccccc\"", 4767 getLLVMStyleWithColumns(23)); 4768 verifyFormat("#define A \"def\"\n" 4769 "f(\"abc\" A \"ghi\"\n" 4770 " \"jkl\");"); 4771 4772 verifyFormat("f(L\"a\"\n" 4773 " L\"b\");"); 4774 verifyFormat("#define A(X) \\\n" 4775 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4776 " L\"ccccc\"", 4777 getLLVMStyleWithColumns(25)); 4778 4779 verifyFormat("f(@\"a\"\n" 4780 " @\"b\");"); 4781 verifyFormat("NSString s = @\"a\"\n" 4782 " @\"b\"\n" 4783 " @\"c\";"); 4784 verifyFormat("NSString s = @\"a\"\n" 4785 " \"b\"\n" 4786 " \"c\";"); 4787 } 4788 4789 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4790 FormatStyle Style = getLLVMStyle(); 4791 // No declarations or definitions should be moved to own line. 4792 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4793 verifyFormat("class A {\n" 4794 " int f() { return 1; }\n" 4795 " int g();\n" 4796 "};\n" 4797 "int f() { return 1; }\n" 4798 "int g();\n", 4799 Style); 4800 4801 // All declarations and definitions should have the return type moved to its 4802 // own 4803 // line. 4804 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4805 verifyFormat("class E {\n" 4806 " int\n" 4807 " f() {\n" 4808 " return 1;\n" 4809 " }\n" 4810 " int\n" 4811 " g();\n" 4812 "};\n" 4813 "int\n" 4814 "f() {\n" 4815 " return 1;\n" 4816 "}\n" 4817 "int\n" 4818 "g();\n", 4819 Style); 4820 4821 // Top-level definitions, and no kinds of declarations should have the 4822 // return type moved to its own line. 4823 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4824 verifyFormat("class B {\n" 4825 " int f() { return 1; }\n" 4826 " int g();\n" 4827 "};\n" 4828 "int\n" 4829 "f() {\n" 4830 " return 1;\n" 4831 "}\n" 4832 "int g();\n", 4833 Style); 4834 4835 // Top-level definitions and declarations should have the return type moved 4836 // to its own line. 4837 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4838 verifyFormat("class C {\n" 4839 " int f() { return 1; }\n" 4840 " int g();\n" 4841 "};\n" 4842 "int\n" 4843 "f() {\n" 4844 " return 1;\n" 4845 "}\n" 4846 "int\n" 4847 "g();\n", 4848 Style); 4849 4850 // All definitions should have the return type moved to its own line, but no 4851 // kinds of declarations. 4852 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4853 verifyFormat("class D {\n" 4854 " int\n" 4855 " f() {\n" 4856 " return 1;\n" 4857 " }\n" 4858 " int g();\n" 4859 "};\n" 4860 "int\n" 4861 "f() {\n" 4862 " return 1;\n" 4863 "}\n" 4864 "int g();\n", 4865 Style); 4866 verifyFormat("const char *\n" 4867 "f(void) {\n" // Break here. 4868 " return \"\";\n" 4869 "}\n" 4870 "const char *bar(void);\n", // No break here. 4871 Style); 4872 verifyFormat("template <class T>\n" 4873 "T *\n" 4874 "f(T &c) {\n" // Break here. 4875 " return NULL;\n" 4876 "}\n" 4877 "template <class T> T *f(T &c);\n", // No break here. 4878 Style); 4879 verifyFormat("class C {\n" 4880 " int\n" 4881 " operator+() {\n" 4882 " return 1;\n" 4883 " }\n" 4884 " int\n" 4885 " operator()() {\n" 4886 " return 1;\n" 4887 " }\n" 4888 "};\n", 4889 Style); 4890 verifyFormat("void\n" 4891 "A::operator()() {}\n" 4892 "void\n" 4893 "A::operator>>() {}\n" 4894 "void\n" 4895 "A::operator+() {}\n", 4896 Style); 4897 verifyFormat("void *operator new(std::size_t s);", // No break here. 4898 Style); 4899 verifyFormat("void *\n" 4900 "operator new(std::size_t s) {}", 4901 Style); 4902 verifyFormat("void *\n" 4903 "operator delete[](void *ptr) {}", 4904 Style); 4905 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4906 verifyFormat("const char *\n" 4907 "f(void)\n" // Break here. 4908 "{\n" 4909 " return \"\";\n" 4910 "}\n" 4911 "const char *bar(void);\n", // No break here. 4912 Style); 4913 verifyFormat("template <class T>\n" 4914 "T *\n" // Problem here: no line break 4915 "f(T &c)\n" // Break here. 4916 "{\n" 4917 " return NULL;\n" 4918 "}\n" 4919 "template <class T> T *f(T &c);\n", // No break here. 4920 Style); 4921 } 4922 4923 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4924 FormatStyle NoBreak = getLLVMStyle(); 4925 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4926 FormatStyle Break = getLLVMStyle(); 4927 Break.AlwaysBreakBeforeMultilineStrings = true; 4928 verifyFormat("aaaa = \"bbbb\"\n" 4929 " \"cccc\";", 4930 NoBreak); 4931 verifyFormat("aaaa =\n" 4932 " \"bbbb\"\n" 4933 " \"cccc\";", 4934 Break); 4935 verifyFormat("aaaa(\"bbbb\"\n" 4936 " \"cccc\");", 4937 NoBreak); 4938 verifyFormat("aaaa(\n" 4939 " \"bbbb\"\n" 4940 " \"cccc\");", 4941 Break); 4942 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4943 " \"cccc\");", 4944 NoBreak); 4945 verifyFormat("aaaa(qqq,\n" 4946 " \"bbbb\"\n" 4947 " \"cccc\");", 4948 Break); 4949 verifyFormat("aaaa(qqq,\n" 4950 " L\"bbbb\"\n" 4951 " L\"cccc\");", 4952 Break); 4953 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4954 " \"bbbb\"));", 4955 Break); 4956 verifyFormat("string s = someFunction(\n" 4957 " \"abc\"\n" 4958 " \"abc\");", 4959 Break); 4960 4961 // As we break before unary operators, breaking right after them is bad. 4962 verifyFormat("string foo = abc ? \"x\"\n" 4963 " \"blah blah blah blah blah blah\"\n" 4964 " : \"y\";", 4965 Break); 4966 4967 // Don't break if there is no column gain. 4968 verifyFormat("f(\"aaaa\"\n" 4969 " \"bbbb\");", 4970 Break); 4971 4972 // Treat literals with escaped newlines like multi-line string literals. 4973 EXPECT_EQ("x = \"a\\\n" 4974 "b\\\n" 4975 "c\";", 4976 format("x = \"a\\\n" 4977 "b\\\n" 4978 "c\";", 4979 NoBreak)); 4980 EXPECT_EQ("xxxx =\n" 4981 " \"a\\\n" 4982 "b\\\n" 4983 "c\";", 4984 format("xxxx = \"a\\\n" 4985 "b\\\n" 4986 "c\";", 4987 Break)); 4988 4989 // Exempt ObjC strings for now. 4990 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 4991 " @\"bbbb\";", 4992 format("NSString *const kString = @\"aaaa\"\n" 4993 "@\"bbbb\";", 4994 Break)); 4995 4996 Break.ColumnLimit = 0; 4997 verifyFormat("const char *hello = \"hello llvm\";", Break); 4998 } 4999 5000 TEST_F(FormatTest, AlignsPipes) { 5001 verifyFormat( 5002 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5003 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5004 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5005 verifyFormat( 5006 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5007 " << aaaaaaaaaaaaaaaaaaaa;"); 5008 verifyFormat( 5009 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5010 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5011 verifyFormat( 5012 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5013 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5014 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5015 verifyFormat( 5016 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5017 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5018 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5019 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5022 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5023 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5024 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5025 verifyFormat( 5026 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5028 5029 verifyFormat("return out << \"somepacket = {\\n\"\n" 5030 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5031 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5032 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5033 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5034 " << \"}\";"); 5035 5036 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5037 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5038 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5039 verifyFormat( 5040 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5041 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5042 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5043 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5044 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5045 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5046 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5047 verifyFormat( 5048 "void f() {\n" 5049 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5050 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5051 "}"); 5052 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5053 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5054 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5056 " aaaaaaaaaaaaaaaaaaaaa)\n" 5057 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5058 verifyFormat("LOG_IF(aaa == //\n" 5059 " bbb)\n" 5060 " << a << b;"); 5061 5062 // Breaking before the first "<<" is generally not desirable. 5063 verifyFormat( 5064 "llvm::errs()\n" 5065 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5066 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5067 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5068 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5069 getLLVMStyleWithColumns(70)); 5070 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5071 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5072 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5073 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5074 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5075 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5076 getLLVMStyleWithColumns(70)); 5077 5078 // But sometimes, breaking before the first "<<" is desirable. 5079 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5080 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5081 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5082 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5083 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5084 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5085 " << BEF << IsTemplate << Description << E->getType();"); 5086 5087 verifyFormat( 5088 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5089 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5090 5091 // Incomplete string literal. 5092 EXPECT_EQ("llvm::errs() << \"\n" 5093 " << a;", 5094 format("llvm::errs() << \"\n<<a;")); 5095 5096 verifyFormat("void f() {\n" 5097 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5098 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5099 "}"); 5100 5101 // Handle 'endl'. 5102 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5103 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5104 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5105 5106 // Handle '\n'. 5107 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5108 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5109 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5110 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5111 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5112 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5113 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5114 } 5115 5116 TEST_F(FormatTest, UnderstandsEquals) { 5117 verifyFormat( 5118 "aaaaaaaaaaaaaaaaa =\n" 5119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5120 verifyFormat( 5121 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5123 verifyFormat( 5124 "if (a) {\n" 5125 " f();\n" 5126 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5128 "}"); 5129 5130 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5131 " 100000000 + 10000000) {\n}"); 5132 } 5133 5134 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5135 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5136 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5137 5138 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5139 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5140 5141 verifyFormat( 5142 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5143 " Parameter2);"); 5144 5145 verifyFormat( 5146 "ShortObject->shortFunction(\n" 5147 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5148 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5149 5150 verifyFormat("loooooooooooooongFunction(\n" 5151 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5152 5153 verifyFormat( 5154 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5155 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5156 5157 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5158 " .WillRepeatedly(Return(SomeValue));"); 5159 verifyFormat("void f() {\n" 5160 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5161 " .Times(2)\n" 5162 " .WillRepeatedly(Return(SomeValue));\n" 5163 "}"); 5164 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5165 " ccccccccccccccccccccccc);"); 5166 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5168 " .aaaaa(aaaaa),\n" 5169 " aaaaaaaaaaaaaaaaaaaaa);"); 5170 verifyFormat("void f() {\n" 5171 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5173 "}"); 5174 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5176 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5177 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5178 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5179 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5180 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5181 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5182 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5183 "}"); 5184 5185 // Here, it is not necessary to wrap at "." or "->". 5186 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5187 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5188 verifyFormat( 5189 "aaaaaaaaaaa->aaaaaaaaa(\n" 5190 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5191 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5192 5193 verifyFormat( 5194 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5196 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5197 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5198 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5199 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5200 5201 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5203 " .a();"); 5204 5205 FormatStyle NoBinPacking = getLLVMStyle(); 5206 NoBinPacking.BinPackParameters = false; 5207 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5208 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5209 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5210 " aaaaaaaaaaaaaaaaaaa,\n" 5211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5212 NoBinPacking); 5213 5214 // If there is a subsequent call, change to hanging indentation. 5215 verifyFormat( 5216 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5217 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5218 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5219 verifyFormat( 5220 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5221 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5222 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5224 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5225 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5227 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5228 } 5229 5230 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5231 verifyFormat("template <typename T>\n" 5232 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5233 verifyFormat("template <typename T>\n" 5234 "// T should be one of {A, B}.\n" 5235 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5236 verifyFormat( 5237 "template <typename T>\n" 5238 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5239 verifyFormat("template <typename T>\n" 5240 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5241 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5242 verifyFormat( 5243 "template <typename T>\n" 5244 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5245 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5246 verifyFormat( 5247 "template <typename T>\n" 5248 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5249 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5250 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5251 verifyFormat("template <typename T>\n" 5252 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5253 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5254 verifyFormat( 5255 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5256 " typename T4 = char>\n" 5257 "void f();"); 5258 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5259 " template <typename> class cccccccccccccccccccccc,\n" 5260 " typename ddddddddddddd>\n" 5261 "class C {};"); 5262 verifyFormat( 5263 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5265 5266 verifyFormat("void f() {\n" 5267 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5268 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5269 "}"); 5270 5271 verifyFormat("template <typename T> class C {};"); 5272 verifyFormat("template <typename T> void f();"); 5273 verifyFormat("template <typename T> void f() {}"); 5274 verifyFormat( 5275 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5278 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5281 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5282 getLLVMStyleWithColumns(72)); 5283 EXPECT_EQ("static_cast<A< //\n" 5284 " B> *>(\n" 5285 "\n" 5286 " );", 5287 format("static_cast<A<//\n" 5288 " B>*>(\n" 5289 "\n" 5290 " );")); 5291 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5292 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5293 5294 FormatStyle AlwaysBreak = getLLVMStyle(); 5295 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5296 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5297 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5298 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5299 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5300 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5301 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5302 verifyFormat("template <template <typename> class Fooooooo,\n" 5303 " template <typename> class Baaaaaaar>\n" 5304 "struct C {};", 5305 AlwaysBreak); 5306 verifyFormat("template <typename T> // T can be A, B or C.\n" 5307 "struct C {};", 5308 AlwaysBreak); 5309 } 5310 5311 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5312 verifyFormat( 5313 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5315 verifyFormat( 5316 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5319 5320 // FIXME: Should we have the extra indent after the second break? 5321 verifyFormat( 5322 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5325 5326 verifyFormat( 5327 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5328 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5329 5330 // Breaking at nested name specifiers is generally not desirable. 5331 verifyFormat( 5332 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5333 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5334 5335 verifyFormat( 5336 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5338 " aaaaaaaaaaaaaaaaaaaaa);", 5339 getLLVMStyleWithColumns(74)); 5340 5341 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5343 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5344 } 5345 5346 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5347 verifyFormat("A<int> a;"); 5348 verifyFormat("A<A<A<int>>> a;"); 5349 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5350 verifyFormat("bool x = a < 1 || 2 > a;"); 5351 verifyFormat("bool x = 5 < f<int>();"); 5352 verifyFormat("bool x = f<int>() > 5;"); 5353 verifyFormat("bool x = 5 < a<int>::x;"); 5354 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5355 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5356 5357 verifyGoogleFormat("A<A<int>> a;"); 5358 verifyGoogleFormat("A<A<A<int>>> a;"); 5359 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5360 verifyGoogleFormat("A<A<int> > a;"); 5361 verifyGoogleFormat("A<A<A<int> > > a;"); 5362 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5363 verifyGoogleFormat("A<::A<int>> a;"); 5364 verifyGoogleFormat("A<::A> a;"); 5365 verifyGoogleFormat("A< ::A> a;"); 5366 verifyGoogleFormat("A< ::A<int> > a;"); 5367 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5368 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5369 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5370 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5371 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5372 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5373 5374 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5375 5376 verifyFormat("test >> a >> b;"); 5377 verifyFormat("test << a >> b;"); 5378 5379 verifyFormat("f<int>();"); 5380 verifyFormat("template <typename T> void f() {}"); 5381 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5382 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5383 "sizeof(char)>::type>;"); 5384 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5385 5386 // Not template parameters. 5387 verifyFormat("return a < b && c > d;"); 5388 verifyFormat("void f() {\n" 5389 " while (a < b && c > d) {\n" 5390 " }\n" 5391 "}"); 5392 verifyFormat("template <typename... Types>\n" 5393 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5394 5395 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5397 getLLVMStyleWithColumns(60)); 5398 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5399 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5400 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5401 } 5402 5403 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5404 verifyFormat("COMPARE(a, ==, b);"); 5405 } 5406 5407 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5408 verifyFormat("int A::*x;"); 5409 verifyFormat("int (S::*func)(void *);"); 5410 verifyFormat("void f() { int (S::*func)(void *); }"); 5411 verifyFormat("typedef bool *(Class::*Member)() const;"); 5412 verifyFormat("void f() {\n" 5413 " (a->*f)();\n" 5414 " a->*x;\n" 5415 " (a.*f)();\n" 5416 " ((*a).*f)();\n" 5417 " a.*x;\n" 5418 "}"); 5419 verifyFormat("void f() {\n" 5420 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5421 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5422 "}"); 5423 verifyFormat( 5424 "(aaaaaaaaaa->*bbbbbbb)(\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5426 FormatStyle Style = getLLVMStyle(); 5427 Style.PointerAlignment = FormatStyle::PAS_Left; 5428 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5429 } 5430 5431 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5432 verifyFormat("int a = -2;"); 5433 verifyFormat("f(-1, -2, -3);"); 5434 verifyFormat("a[-1] = 5;"); 5435 verifyFormat("int a = 5 + -2;"); 5436 verifyFormat("if (i == -1) {\n}"); 5437 verifyFormat("if (i != -1) {\n}"); 5438 verifyFormat("if (i > -1) {\n}"); 5439 verifyFormat("if (i < -1) {\n}"); 5440 verifyFormat("++(a->f());"); 5441 verifyFormat("--(a->f());"); 5442 verifyFormat("(a->f())++;"); 5443 verifyFormat("a[42]++;"); 5444 verifyFormat("if (!(a->f())) {\n}"); 5445 5446 verifyFormat("a-- > b;"); 5447 verifyFormat("b ? -a : c;"); 5448 verifyFormat("n * sizeof char16;"); 5449 verifyFormat("n * alignof char16;", getGoogleStyle()); 5450 verifyFormat("sizeof(char);"); 5451 verifyFormat("alignof(char);", getGoogleStyle()); 5452 5453 verifyFormat("return -1;"); 5454 verifyFormat("switch (a) {\n" 5455 "case -1:\n" 5456 " break;\n" 5457 "}"); 5458 verifyFormat("#define X -1"); 5459 verifyFormat("#define X -kConstant"); 5460 5461 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5462 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5463 5464 verifyFormat("int a = /* confusing comment */ -1;"); 5465 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5466 verifyFormat("int a = i /* confusing comment */++;"); 5467 } 5468 5469 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5470 verifyFormat("if (!aaaaaaaaaa( // break\n" 5471 " aaaaa)) {\n" 5472 "}"); 5473 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5474 " aaaaa));"); 5475 verifyFormat("*aaa = aaaaaaa( // break\n" 5476 " bbbbbb);"); 5477 } 5478 5479 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5480 verifyFormat("bool operator<();"); 5481 verifyFormat("bool operator>();"); 5482 verifyFormat("bool operator=();"); 5483 verifyFormat("bool operator==();"); 5484 verifyFormat("bool operator!=();"); 5485 verifyFormat("int operator+();"); 5486 verifyFormat("int operator++();"); 5487 verifyFormat("bool operator,();"); 5488 verifyFormat("bool operator();"); 5489 verifyFormat("bool operator()();"); 5490 verifyFormat("bool operator[]();"); 5491 verifyFormat("operator bool();"); 5492 verifyFormat("operator int();"); 5493 verifyFormat("operator void *();"); 5494 verifyFormat("operator SomeType<int>();"); 5495 verifyFormat("operator SomeType<int, int>();"); 5496 verifyFormat("operator SomeType<SomeType<int>>();"); 5497 verifyFormat("void *operator new(std::size_t size);"); 5498 verifyFormat("void *operator new[](std::size_t size);"); 5499 verifyFormat("void operator delete(void *ptr);"); 5500 verifyFormat("void operator delete[](void *ptr);"); 5501 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5502 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5503 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5504 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5505 5506 verifyFormat( 5507 "ostream &operator<<(ostream &OutputStream,\n" 5508 " SomeReallyLongType WithSomeReallyLongValue);"); 5509 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5510 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5511 " return left.group < right.group;\n" 5512 "}"); 5513 verifyFormat("SomeType &operator=(const SomeType &S);"); 5514 verifyFormat("f.template operator()<int>();"); 5515 5516 verifyGoogleFormat("operator void*();"); 5517 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5518 verifyGoogleFormat("operator ::A();"); 5519 5520 verifyFormat("using A::operator+;"); 5521 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5522 "int i;"); 5523 } 5524 5525 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5526 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5527 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5528 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5529 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5530 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5531 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5532 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5533 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5534 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5535 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5536 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5537 5538 FormatStyle AlignLeft = getLLVMStyle(); 5539 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5540 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5541 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5542 AlignLeft); 5543 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5544 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5545 5546 FormatStyle Spaces = getLLVMStyle(); 5547 Spaces.SpacesInCStyleCastParentheses = true; 5548 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5549 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5550 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5551 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5552 5553 Spaces.SpacesInCStyleCastParentheses = false; 5554 Spaces.SpacesInParentheses = true; 5555 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5556 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5557 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5558 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5559 } 5560 5561 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5562 verifyFormat("void f() {\n" 5563 " A *a = new A;\n" 5564 " A *a = new (placement) A;\n" 5565 " delete a;\n" 5566 " delete (A *)a;\n" 5567 "}"); 5568 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5569 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5570 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5571 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5572 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5573 verifyFormat("delete[] h->p;"); 5574 } 5575 5576 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5577 verifyFormat("int *f(int *a) {}"); 5578 verifyFormat("int main(int argc, char **argv) {}"); 5579 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5580 verifyIndependentOfContext("f(a, *a);"); 5581 verifyFormat("void g() { f(*a); }"); 5582 verifyIndependentOfContext("int a = b * 10;"); 5583 verifyIndependentOfContext("int a = 10 * b;"); 5584 verifyIndependentOfContext("int a = b * c;"); 5585 verifyIndependentOfContext("int a += b * c;"); 5586 verifyIndependentOfContext("int a -= b * c;"); 5587 verifyIndependentOfContext("int a *= b * c;"); 5588 verifyIndependentOfContext("int a /= b * c;"); 5589 verifyIndependentOfContext("int a = *b;"); 5590 verifyIndependentOfContext("int a = *b * c;"); 5591 verifyIndependentOfContext("int a = b * *c;"); 5592 verifyIndependentOfContext("int a = b * (10);"); 5593 verifyIndependentOfContext("S << b * (10);"); 5594 verifyIndependentOfContext("return 10 * b;"); 5595 verifyIndependentOfContext("return *b * *c;"); 5596 verifyIndependentOfContext("return a & ~b;"); 5597 verifyIndependentOfContext("f(b ? *c : *d);"); 5598 verifyIndependentOfContext("int a = b ? *c : *d;"); 5599 verifyIndependentOfContext("*b = a;"); 5600 verifyIndependentOfContext("a * ~b;"); 5601 verifyIndependentOfContext("a * !b;"); 5602 verifyIndependentOfContext("a * +b;"); 5603 verifyIndependentOfContext("a * -b;"); 5604 verifyIndependentOfContext("a * ++b;"); 5605 verifyIndependentOfContext("a * --b;"); 5606 verifyIndependentOfContext("a[4] * b;"); 5607 verifyIndependentOfContext("a[a * a] = 1;"); 5608 verifyIndependentOfContext("f() * b;"); 5609 verifyIndependentOfContext("a * [self dostuff];"); 5610 verifyIndependentOfContext("int x = a * (a + b);"); 5611 verifyIndependentOfContext("(a *)(a + b);"); 5612 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5613 verifyIndependentOfContext("int *pa = (int *)&a;"); 5614 verifyIndependentOfContext("return sizeof(int **);"); 5615 verifyIndependentOfContext("return sizeof(int ******);"); 5616 verifyIndependentOfContext("return (int **&)a;"); 5617 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5618 verifyFormat("void f(Type (*parameter)[10]) {}"); 5619 verifyFormat("void f(Type (¶meter)[10]) {}"); 5620 verifyGoogleFormat("return sizeof(int**);"); 5621 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5622 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5623 verifyFormat("auto a = [](int **&, int ***) {};"); 5624 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5625 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5626 verifyFormat("[](const decltype(*a) &value) {}"); 5627 verifyFormat("decltype(a * b) F();"); 5628 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5629 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5630 verifyIndependentOfContext("int i{a * b};"); 5631 verifyIndependentOfContext("aaa && aaa->f();"); 5632 verifyIndependentOfContext("int x = ~*p;"); 5633 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5634 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5635 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5636 verifyFormat("void f() { f(a, c * d); }"); 5637 verifyFormat("void f() { f(new a(), c * d); }"); 5638 5639 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5640 5641 verifyIndependentOfContext("A<int *> a;"); 5642 verifyIndependentOfContext("A<int **> a;"); 5643 verifyIndependentOfContext("A<int *, int *> a;"); 5644 verifyIndependentOfContext("A<int *[]> a;"); 5645 verifyIndependentOfContext( 5646 "const char *const p = reinterpret_cast<const char *const>(q);"); 5647 verifyIndependentOfContext("A<int **, int **> a;"); 5648 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5649 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5650 verifyFormat("for (; a && b;) {\n}"); 5651 verifyFormat("bool foo = true && [] { return false; }();"); 5652 5653 verifyFormat( 5654 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5656 5657 verifyGoogleFormat("**outparam = 1;"); 5658 verifyGoogleFormat("*outparam = a * b;"); 5659 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5660 verifyGoogleFormat("A<int*> a;"); 5661 verifyGoogleFormat("A<int**> a;"); 5662 verifyGoogleFormat("A<int*, int*> a;"); 5663 verifyGoogleFormat("A<int**, int**> a;"); 5664 verifyGoogleFormat("f(b ? *c : *d);"); 5665 verifyGoogleFormat("int a = b ? *c : *d;"); 5666 verifyGoogleFormat("Type* t = **x;"); 5667 verifyGoogleFormat("Type* t = *++*x;"); 5668 verifyGoogleFormat("*++*x;"); 5669 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5670 verifyGoogleFormat("Type* t = x++ * y;"); 5671 verifyGoogleFormat( 5672 "const char* const p = reinterpret_cast<const char* const>(q);"); 5673 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5674 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5675 verifyGoogleFormat("template <typename T>\n" 5676 "void f(int i = 0, SomeType** temps = NULL);"); 5677 5678 FormatStyle Left = getLLVMStyle(); 5679 Left.PointerAlignment = FormatStyle::PAS_Left; 5680 verifyFormat("x = *a(x) = *a(y);", Left); 5681 verifyFormat("for (;; * = b) {\n}", Left); 5682 verifyFormat("return *this += 1;", Left); 5683 5684 verifyIndependentOfContext("a = *(x + y);"); 5685 verifyIndependentOfContext("a = &(x + y);"); 5686 verifyIndependentOfContext("*(x + y).call();"); 5687 verifyIndependentOfContext("&(x + y)->call();"); 5688 verifyFormat("void f() { &(*I).first; }"); 5689 5690 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5691 verifyFormat( 5692 "int *MyValues = {\n" 5693 " *A, // Operator detection might be confused by the '{'\n" 5694 " *BB // Operator detection might be confused by previous comment\n" 5695 "};"); 5696 5697 verifyIndependentOfContext("if (int *a = &b)"); 5698 verifyIndependentOfContext("if (int &a = *b)"); 5699 verifyIndependentOfContext("if (a & b[i])"); 5700 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5701 verifyIndependentOfContext("if (*b[i])"); 5702 verifyIndependentOfContext("if (int *a = (&b))"); 5703 verifyIndependentOfContext("while (int *a = &b)"); 5704 verifyIndependentOfContext("size = sizeof *a;"); 5705 verifyIndependentOfContext("if (a && (b = c))"); 5706 verifyFormat("void f() {\n" 5707 " for (const int &v : Values) {\n" 5708 " }\n" 5709 "}"); 5710 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5711 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5712 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5713 5714 verifyFormat("#define A (!a * b)"); 5715 verifyFormat("#define MACRO \\\n" 5716 " int *i = a * b; \\\n" 5717 " void f(a *b);", 5718 getLLVMStyleWithColumns(19)); 5719 5720 verifyIndependentOfContext("A = new SomeType *[Length];"); 5721 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5722 verifyIndependentOfContext("T **t = new T *;"); 5723 verifyIndependentOfContext("T **t = new T *();"); 5724 verifyGoogleFormat("A = new SomeType*[Length]();"); 5725 verifyGoogleFormat("A = new SomeType*[Length];"); 5726 verifyGoogleFormat("T** t = new T*;"); 5727 verifyGoogleFormat("T** t = new T*();"); 5728 5729 FormatStyle PointerLeft = getLLVMStyle(); 5730 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5731 verifyFormat("delete *x;", PointerLeft); 5732 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5733 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5734 verifyFormat("template <bool a, bool b> " 5735 "typename t::if<x && y>::type f() {}"); 5736 verifyFormat("template <int *y> f() {}"); 5737 verifyFormat("vector<int *> v;"); 5738 verifyFormat("vector<int *const> v;"); 5739 verifyFormat("vector<int *const **const *> v;"); 5740 verifyFormat("vector<int *volatile> v;"); 5741 verifyFormat("vector<a * b> v;"); 5742 verifyFormat("foo<b && false>();"); 5743 verifyFormat("foo<b & 1>();"); 5744 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5745 verifyFormat( 5746 "template <class T, class = typename std::enable_if<\n" 5747 " std::is_integral<T>::value &&\n" 5748 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5749 "void F();", 5750 getLLVMStyleWithColumns(76)); 5751 verifyFormat( 5752 "template <class T,\n" 5753 " class = typename ::std::enable_if<\n" 5754 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5755 "void F();", 5756 getGoogleStyleWithColumns(68)); 5757 5758 verifyIndependentOfContext("MACRO(int *i);"); 5759 verifyIndependentOfContext("MACRO(auto *a);"); 5760 verifyIndependentOfContext("MACRO(const A *a);"); 5761 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5762 // FIXME: Is there a way to make this work? 5763 // verifyIndependentOfContext("MACRO(A *a);"); 5764 5765 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5766 verifyFormat("return options != nullptr && operator==(*options);"); 5767 5768 EXPECT_EQ("#define OP(x) \\\n" 5769 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5770 " return s << a.DebugString(); \\\n" 5771 " }", 5772 format("#define OP(x) \\\n" 5773 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5774 " return s << a.DebugString(); \\\n" 5775 " }", 5776 getLLVMStyleWithColumns(50))); 5777 5778 // FIXME: We cannot handle this case yet; we might be able to figure out that 5779 // foo<x> d > v; doesn't make sense. 5780 verifyFormat("foo<a<b && c> d> v;"); 5781 5782 FormatStyle PointerMiddle = getLLVMStyle(); 5783 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5784 verifyFormat("delete *x;", PointerMiddle); 5785 verifyFormat("int * x;", PointerMiddle); 5786 verifyFormat("template <int * y> f() {}", PointerMiddle); 5787 verifyFormat("int * f(int * a) {}", PointerMiddle); 5788 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5789 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5790 verifyFormat("A<int *> a;", PointerMiddle); 5791 verifyFormat("A<int **> a;", PointerMiddle); 5792 verifyFormat("A<int *, int *> a;", PointerMiddle); 5793 verifyFormat("A<int * []> a;", PointerMiddle); 5794 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5795 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5796 verifyFormat("T ** t = new T *;", PointerMiddle); 5797 5798 // Member function reference qualifiers aren't binary operators. 5799 verifyFormat("string // break\n" 5800 "operator()() & {}"); 5801 verifyFormat("string // break\n" 5802 "operator()() && {}"); 5803 verifyGoogleFormat("template <typename T>\n" 5804 "auto x() & -> int {}"); 5805 } 5806 5807 TEST_F(FormatTest, UnderstandsAttributes) { 5808 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5809 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5810 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5811 FormatStyle AfterType = getLLVMStyle(); 5812 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5813 verifyFormat("__attribute__((nodebug)) void\n" 5814 "foo() {}\n", 5815 AfterType); 5816 } 5817 5818 TEST_F(FormatTest, UnderstandsEllipsis) { 5819 verifyFormat("int printf(const char *fmt, ...);"); 5820 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5821 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5822 5823 FormatStyle PointersLeft = getLLVMStyle(); 5824 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5825 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5826 } 5827 5828 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5829 EXPECT_EQ("int *a;\n" 5830 "int *a;\n" 5831 "int *a;", 5832 format("int *a;\n" 5833 "int* a;\n" 5834 "int *a;", 5835 getGoogleStyle())); 5836 EXPECT_EQ("int* a;\n" 5837 "int* a;\n" 5838 "int* a;", 5839 format("int* a;\n" 5840 "int* a;\n" 5841 "int *a;", 5842 getGoogleStyle())); 5843 EXPECT_EQ("int *a;\n" 5844 "int *a;\n" 5845 "int *a;", 5846 format("int *a;\n" 5847 "int * a;\n" 5848 "int * a;", 5849 getGoogleStyle())); 5850 EXPECT_EQ("auto x = [] {\n" 5851 " int *a;\n" 5852 " int *a;\n" 5853 " int *a;\n" 5854 "};", 5855 format("auto x=[]{int *a;\n" 5856 "int * a;\n" 5857 "int * a;};", 5858 getGoogleStyle())); 5859 } 5860 5861 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5862 verifyFormat("int f(int &&a) {}"); 5863 verifyFormat("int f(int a, char &&b) {}"); 5864 verifyFormat("void f() { int &&a = b; }"); 5865 verifyGoogleFormat("int f(int a, char&& b) {}"); 5866 verifyGoogleFormat("void f() { int&& a = b; }"); 5867 5868 verifyIndependentOfContext("A<int &&> a;"); 5869 verifyIndependentOfContext("A<int &&, int &&> a;"); 5870 verifyGoogleFormat("A<int&&> a;"); 5871 verifyGoogleFormat("A<int&&, int&&> a;"); 5872 5873 // Not rvalue references: 5874 verifyFormat("template <bool B, bool C> class A {\n" 5875 " static_assert(B && C, \"Something is wrong\");\n" 5876 "};"); 5877 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5878 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5879 verifyFormat("#define A(a, b) (a && b)"); 5880 } 5881 5882 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5883 verifyFormat("void f() {\n" 5884 " x[aaaaaaaaa -\n" 5885 " b] = 23;\n" 5886 "}", 5887 getLLVMStyleWithColumns(15)); 5888 } 5889 5890 TEST_F(FormatTest, FormatsCasts) { 5891 verifyFormat("Type *A = static_cast<Type *>(P);"); 5892 verifyFormat("Type *A = (Type *)P;"); 5893 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5894 verifyFormat("int a = (int)(2.0f);"); 5895 verifyFormat("int a = (int)2.0f;"); 5896 verifyFormat("x[(int32)y];"); 5897 verifyFormat("x = (int32)y;"); 5898 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5899 verifyFormat("int a = (int)*b;"); 5900 verifyFormat("int a = (int)2.0f;"); 5901 verifyFormat("int a = (int)~0;"); 5902 verifyFormat("int a = (int)++a;"); 5903 verifyFormat("int a = (int)sizeof(int);"); 5904 verifyFormat("int a = (int)+2;"); 5905 verifyFormat("my_int a = (my_int)2.0f;"); 5906 verifyFormat("my_int a = (my_int)sizeof(int);"); 5907 verifyFormat("return (my_int)aaa;"); 5908 verifyFormat("#define x ((int)-1)"); 5909 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5910 verifyFormat("#define p(q) ((int *)&q)"); 5911 verifyFormat("fn(a)(b) + 1;"); 5912 5913 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5914 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5915 verifyFormat("my_int a = (my_int)~0;"); 5916 verifyFormat("my_int a = (my_int)++a;"); 5917 verifyFormat("my_int a = (my_int)-2;"); 5918 verifyFormat("my_int a = (my_int)1;"); 5919 verifyFormat("my_int a = (my_int *)1;"); 5920 verifyFormat("my_int a = (const my_int)-1;"); 5921 verifyFormat("my_int a = (const my_int *)-1;"); 5922 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5923 verifyFormat("my_int a = (ns::my_int)-2;"); 5924 verifyFormat("case (my_int)ONE:"); 5925 5926 // FIXME: single value wrapped with paren will be treated as cast. 5927 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5928 5929 verifyFormat("{ (void)F; }"); 5930 5931 // Don't break after a cast's 5932 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5933 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5934 " bbbbbbbbbbbbbbbbbbbbbb);"); 5935 5936 // These are not casts. 5937 verifyFormat("void f(int *) {}"); 5938 verifyFormat("f(foo)->b;"); 5939 verifyFormat("f(foo).b;"); 5940 verifyFormat("f(foo)(b);"); 5941 verifyFormat("f(foo)[b];"); 5942 verifyFormat("[](foo) { return 4; }(bar);"); 5943 verifyFormat("(*funptr)(foo)[4];"); 5944 verifyFormat("funptrs[4](foo)[4];"); 5945 verifyFormat("void f(int *);"); 5946 verifyFormat("void f(int *) = 0;"); 5947 verifyFormat("void f(SmallVector<int>) {}"); 5948 verifyFormat("void f(SmallVector<int>);"); 5949 verifyFormat("void f(SmallVector<int>) = 0;"); 5950 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5951 verifyFormat("int a = sizeof(int) * b;"); 5952 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5953 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5954 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5955 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5956 5957 // These are not casts, but at some point were confused with casts. 5958 verifyFormat("virtual void foo(int *) override;"); 5959 verifyFormat("virtual void foo(char &) const;"); 5960 verifyFormat("virtual void foo(int *a, char *) const;"); 5961 verifyFormat("int a = sizeof(int *) + b;"); 5962 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5963 verifyFormat("bool b = f(g<int>) && c;"); 5964 verifyFormat("typedef void (*f)(int i) func;"); 5965 5966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5967 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5968 // FIXME: The indentation here is not ideal. 5969 verifyFormat( 5970 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5971 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5972 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5973 } 5974 5975 TEST_F(FormatTest, FormatsFunctionTypes) { 5976 verifyFormat("A<bool()> a;"); 5977 verifyFormat("A<SomeType()> a;"); 5978 verifyFormat("A<void (*)(int, std::string)> a;"); 5979 verifyFormat("A<void *(int)>;"); 5980 verifyFormat("void *(*a)(int *, SomeType *);"); 5981 verifyFormat("int (*func)(void *);"); 5982 verifyFormat("void f() { int (*func)(void *); }"); 5983 verifyFormat("template <class CallbackClass>\n" 5984 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5985 5986 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5987 verifyGoogleFormat("void* (*a)(int);"); 5988 verifyGoogleFormat( 5989 "template <class CallbackClass>\n" 5990 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5991 5992 // Other constructs can look somewhat like function types: 5993 verifyFormat("A<sizeof(*x)> a;"); 5994 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5995 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5996 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5997 verifyFormat("int x = f(&h)();"); 5998 } 5999 6000 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6001 verifyFormat("A (*foo_)[6];"); 6002 verifyFormat("vector<int> (*foo_)[6];"); 6003 } 6004 6005 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6006 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6007 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6008 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6009 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6010 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6011 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6012 6013 // Different ways of ()-initializiation. 6014 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6015 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6016 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6017 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6018 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6019 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6020 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6021 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6022 } 6023 6024 TEST_F(FormatTest, BreaksLongDeclarations) { 6025 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6026 " AnotherNameForTheLongType;"); 6027 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6029 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6030 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6031 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6032 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6033 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6034 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6035 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6036 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6037 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6038 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6039 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6040 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6041 FormatStyle Indented = getLLVMStyle(); 6042 Indented.IndentWrappedFunctionNames = true; 6043 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6044 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6045 Indented); 6046 verifyFormat( 6047 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6048 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6049 Indented); 6050 verifyFormat( 6051 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6052 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6053 Indented); 6054 verifyFormat( 6055 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6056 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6057 Indented); 6058 6059 // FIXME: Without the comment, this breaks after "(". 6060 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6061 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6062 getGoogleStyle()); 6063 6064 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6065 " int LoooooooooooooooooooongParam2) {}"); 6066 verifyFormat( 6067 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6068 " SourceLocation L, IdentifierIn *II,\n" 6069 " Type *T) {}"); 6070 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6071 "ReallyReaaallyLongFunctionName(\n" 6072 " const std::string &SomeParameter,\n" 6073 " const SomeType<string, SomeOtherTemplateParameter>\n" 6074 " &ReallyReallyLongParameterName,\n" 6075 " const SomeType<string, SomeOtherTemplateParameter>\n" 6076 " &AnotherLongParameterName) {}"); 6077 verifyFormat("template <typename A>\n" 6078 "SomeLoooooooooooooooooooooongType<\n" 6079 " typename some_namespace::SomeOtherType<A>::Type>\n" 6080 "Function() {}"); 6081 6082 verifyGoogleFormat( 6083 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6084 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6085 verifyGoogleFormat( 6086 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6087 " SourceLocation L) {}"); 6088 verifyGoogleFormat( 6089 "some_namespace::LongReturnType\n" 6090 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6091 " int first_long_parameter, int second_parameter) {}"); 6092 6093 verifyGoogleFormat("template <typename T>\n" 6094 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6095 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6096 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6097 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6098 6099 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6100 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6101 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6102 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6103 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6104 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6105 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6106 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6109 } 6110 6111 TEST_F(FormatTest, FormatsArrays) { 6112 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6113 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6114 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6115 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6116 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6117 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6118 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6119 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6120 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6121 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6122 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6123 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6124 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6125 verifyFormat( 6126 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6127 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6128 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6129 6130 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6132 verifyFormat( 6133 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6134 " .aaaaaaa[0]\n" 6135 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6136 verifyFormat("a[::b::c];"); 6137 6138 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6139 6140 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6141 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6142 } 6143 6144 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6145 verifyFormat("(a)->b();"); 6146 verifyFormat("--a;"); 6147 } 6148 6149 TEST_F(FormatTest, HandlesIncludeDirectives) { 6150 verifyFormat("#include <string>\n" 6151 "#include <a/b/c.h>\n" 6152 "#include \"a/b/string\"\n" 6153 "#include \"string.h\"\n" 6154 "#include \"string.h\"\n" 6155 "#include <a-a>\n" 6156 "#include < path with space >\n" 6157 "#include_next <test.h>" 6158 "#include \"abc.h\" // this is included for ABC\n" 6159 "#include \"some long include\" // with a comment\n" 6160 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6161 getLLVMStyleWithColumns(35)); 6162 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6163 EXPECT_EQ("#include <a>", format("#include<a>")); 6164 6165 verifyFormat("#import <string>"); 6166 verifyFormat("#import <a/b/c.h>"); 6167 verifyFormat("#import \"a/b/string\""); 6168 verifyFormat("#import \"string.h\""); 6169 verifyFormat("#import \"string.h\""); 6170 verifyFormat("#if __has_include(<strstream>)\n" 6171 "#include <strstream>\n" 6172 "#endif"); 6173 6174 verifyFormat("#define MY_IMPORT <a/b>"); 6175 6176 // Protocol buffer definition or missing "#". 6177 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6178 getLLVMStyleWithColumns(30)); 6179 6180 FormatStyle Style = getLLVMStyle(); 6181 Style.AlwaysBreakBeforeMultilineStrings = true; 6182 Style.ColumnLimit = 0; 6183 verifyFormat("#import \"abc.h\"", Style); 6184 6185 // But 'import' might also be a regular C++ namespace. 6186 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6188 } 6189 6190 //===----------------------------------------------------------------------===// 6191 // Error recovery tests. 6192 //===----------------------------------------------------------------------===// 6193 6194 TEST_F(FormatTest, IncompleteParameterLists) { 6195 FormatStyle NoBinPacking = getLLVMStyle(); 6196 NoBinPacking.BinPackParameters = false; 6197 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6198 " double *min_x,\n" 6199 " double *max_x,\n" 6200 " double *min_y,\n" 6201 " double *max_y,\n" 6202 " double *min_z,\n" 6203 " double *max_z, ) {}", 6204 NoBinPacking); 6205 } 6206 6207 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6208 verifyFormat("void f() { return; }\n42"); 6209 verifyFormat("void f() {\n" 6210 " if (0)\n" 6211 " return;\n" 6212 "}\n" 6213 "42"); 6214 verifyFormat("void f() { return }\n42"); 6215 verifyFormat("void f() {\n" 6216 " if (0)\n" 6217 " return\n" 6218 "}\n" 6219 "42"); 6220 } 6221 6222 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6223 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6224 EXPECT_EQ("void f() {\n" 6225 " if (a)\n" 6226 " return\n" 6227 "}", 6228 format("void f ( ) { if ( a ) return }")); 6229 EXPECT_EQ("namespace N {\n" 6230 "void f()\n" 6231 "}", 6232 format("namespace N { void f() }")); 6233 EXPECT_EQ("namespace N {\n" 6234 "void f() {}\n" 6235 "void g()\n" 6236 "}", 6237 format("namespace N { void f( ) { } void g( ) }")); 6238 } 6239 6240 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6241 verifyFormat("int aaaaaaaa =\n" 6242 " // Overlylongcomment\n" 6243 " b;", 6244 getLLVMStyleWithColumns(20)); 6245 verifyFormat("function(\n" 6246 " ShortArgument,\n" 6247 " LoooooooooooongArgument);\n", 6248 getLLVMStyleWithColumns(20)); 6249 } 6250 6251 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6252 verifyFormat("public:"); 6253 verifyFormat("class A {\n" 6254 "public\n" 6255 " void f() {}\n" 6256 "};"); 6257 verifyFormat("public\n" 6258 "int qwerty;"); 6259 verifyFormat("public\n" 6260 "B {}"); 6261 verifyFormat("public\n" 6262 "{}"); 6263 verifyFormat("public\n" 6264 "B { int x; }"); 6265 } 6266 6267 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6268 verifyFormat("{"); 6269 verifyFormat("#})"); 6270 verifyNoCrash("(/**/[:!] ?[)."); 6271 } 6272 6273 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6274 verifyFormat("do {\n}"); 6275 verifyFormat("do {\n}\n" 6276 "f();"); 6277 verifyFormat("do {\n}\n" 6278 "wheeee(fun);"); 6279 verifyFormat("do {\n" 6280 " f();\n" 6281 "}"); 6282 } 6283 6284 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6285 verifyFormat("if {\n foo;\n foo();\n}"); 6286 verifyFormat("switch {\n foo;\n foo();\n}"); 6287 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6288 verifyFormat("while {\n foo;\n foo();\n}"); 6289 verifyFormat("do {\n foo;\n foo();\n} while;"); 6290 } 6291 6292 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6293 verifyIncompleteFormat("namespace {\n" 6294 "class Foo { Foo (\n" 6295 "};\n" 6296 "} // comment"); 6297 } 6298 6299 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6300 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6301 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6302 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6303 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6304 6305 EXPECT_EQ("{\n" 6306 " {\n" 6307 " breakme(\n" 6308 " qwe);\n" 6309 " }\n", 6310 format("{\n" 6311 " {\n" 6312 " breakme(qwe);\n" 6313 "}\n", 6314 getLLVMStyleWithColumns(10))); 6315 } 6316 6317 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6318 verifyFormat("int x = {\n" 6319 " avariable,\n" 6320 " b(alongervariable)};", 6321 getLLVMStyleWithColumns(25)); 6322 } 6323 6324 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6325 verifyFormat("return (a)(b){1, 2, 3};"); 6326 } 6327 6328 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6329 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6330 verifyFormat("vector<int> x{\n" 6331 " 1, 2, 3, 4,\n" 6332 "};"); 6333 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6334 verifyFormat("f({1, 2});"); 6335 verifyFormat("auto v = Foo{-1};"); 6336 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6337 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6338 verifyFormat("new vector<int>{1, 2, 3};"); 6339 verifyFormat("new int[3]{1, 2, 3};"); 6340 verifyFormat("new int{1};"); 6341 verifyFormat("return {arg1, arg2};"); 6342 verifyFormat("return {arg1, SomeType{parameter}};"); 6343 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6344 verifyFormat("new T{arg1, arg2};"); 6345 verifyFormat("f(MyMap[{composite, key}]);"); 6346 verifyFormat("class Class {\n" 6347 " T member = {arg1, arg2};\n" 6348 "};"); 6349 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6350 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6351 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6352 6353 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6354 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6355 verifyFormat("auto i = decltype(x){};"); 6356 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6357 verifyFormat("Node n{1, Node{1000}, //\n" 6358 " 2};"); 6359 verifyFormat("Aaaa aaaaaaa{\n" 6360 " {\n" 6361 " aaaa,\n" 6362 " },\n" 6363 "};"); 6364 verifyFormat("class C : public D {\n" 6365 " SomeClass SC{2};\n" 6366 "};"); 6367 verifyFormat("class C : public A {\n" 6368 " class D : public B {\n" 6369 " void f() { int i{2}; }\n" 6370 " };\n" 6371 "};"); 6372 verifyFormat("#define A {a, a},"); 6373 6374 // In combination with BinPackArguments = false. 6375 FormatStyle NoBinPacking = getLLVMStyle(); 6376 NoBinPacking.BinPackArguments = false; 6377 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6378 " bbbbb,\n" 6379 " ccccc,\n" 6380 " ddddd,\n" 6381 " eeeee,\n" 6382 " ffffff,\n" 6383 " ggggg,\n" 6384 " hhhhhh,\n" 6385 " iiiiii,\n" 6386 " jjjjjj,\n" 6387 " kkkkkk};", 6388 NoBinPacking); 6389 verifyFormat("const Aaaaaa aaaaa = {\n" 6390 " aaaaa,\n" 6391 " bbbbb,\n" 6392 " ccccc,\n" 6393 " ddddd,\n" 6394 " eeeee,\n" 6395 " ffffff,\n" 6396 " ggggg,\n" 6397 " hhhhhh,\n" 6398 " iiiiii,\n" 6399 " jjjjjj,\n" 6400 " kkkkkk,\n" 6401 "};", 6402 NoBinPacking); 6403 verifyFormat( 6404 "const Aaaaaa aaaaa = {\n" 6405 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6406 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6407 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6408 "};", 6409 NoBinPacking); 6410 6411 // FIXME: The alignment of these trailing comments might be bad. Then again, 6412 // this might be utterly useless in real code. 6413 verifyFormat("Constructor::Constructor()\n" 6414 " : some_value{ //\n" 6415 " aaaaaaa, //\n" 6416 " bbbbbbb} {}"); 6417 6418 // In braced lists, the first comment is always assumed to belong to the 6419 // first element. Thus, it can be moved to the next or previous line as 6420 // appropriate. 6421 EXPECT_EQ("function({// First element:\n" 6422 " 1,\n" 6423 " // Second element:\n" 6424 " 2});", 6425 format("function({\n" 6426 " // First element:\n" 6427 " 1,\n" 6428 " // Second element:\n" 6429 " 2});")); 6430 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6431 " // First element:\n" 6432 " 1,\n" 6433 " // Second element:\n" 6434 " 2};", 6435 format("std::vector<int> MyNumbers{// First element:\n" 6436 " 1,\n" 6437 " // Second element:\n" 6438 " 2};", 6439 getLLVMStyleWithColumns(30))); 6440 // A trailing comma should still lead to an enforced line break. 6441 EXPECT_EQ("vector<int> SomeVector = {\n" 6442 " // aaa\n" 6443 " 1, 2,\n" 6444 "};", 6445 format("vector<int> SomeVector = { // aaa\n" 6446 " 1, 2, };")); 6447 6448 FormatStyle ExtraSpaces = getLLVMStyle(); 6449 ExtraSpaces.Cpp11BracedListStyle = false; 6450 ExtraSpaces.ColumnLimit = 75; 6451 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6452 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6453 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6454 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6455 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6456 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6457 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6458 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6459 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6460 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6461 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6462 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6463 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6464 verifyFormat("class Class {\n" 6465 " T member = { arg1, arg2 };\n" 6466 "};", 6467 ExtraSpaces); 6468 verifyFormat( 6469 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6470 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6471 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6472 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6473 ExtraSpaces); 6474 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6475 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6476 ExtraSpaces); 6477 verifyFormat( 6478 "someFunction(OtherParam,\n" 6479 " BracedList{ // comment 1 (Forcing interesting break)\n" 6480 " param1, param2,\n" 6481 " // comment 2\n" 6482 " param3, param4 });", 6483 ExtraSpaces); 6484 verifyFormat( 6485 "std::this_thread::sleep_for(\n" 6486 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6487 ExtraSpaces); 6488 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6489 " aaaaaaa,\n" 6490 " aaaaaaaaaa,\n" 6491 " aaaaa,\n" 6492 " aaaaaaaaaaaaaaa,\n" 6493 " aaa,\n" 6494 " aaaaaaaaaa,\n" 6495 " a,\n" 6496 " aaaaaaaaaaaaaaaaaaaaa,\n" 6497 " aaaaaaaaaaaa,\n" 6498 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6499 " aaaaaaa,\n" 6500 " a};"); 6501 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6502 } 6503 6504 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6505 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6506 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6507 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6508 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6509 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6510 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6511 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6512 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6513 " 1, 22, 333, 4444, 55555, //\n" 6514 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6515 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6516 verifyFormat( 6517 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6518 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6519 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6520 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6521 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6522 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6523 " 7777777};"); 6524 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6525 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6526 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6527 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6528 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6529 " // Separating comment.\n" 6530 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6531 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6532 " // Leading comment\n" 6533 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6534 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6535 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6536 " 1, 1, 1, 1};", 6537 getLLVMStyleWithColumns(39)); 6538 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6539 " 1, 1, 1, 1};", 6540 getLLVMStyleWithColumns(38)); 6541 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6542 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6543 getLLVMStyleWithColumns(43)); 6544 verifyFormat( 6545 "static unsigned SomeValues[10][3] = {\n" 6546 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6547 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6548 verifyFormat("static auto fields = new vector<string>{\n" 6549 " \"aaaaaaaaaaaaa\",\n" 6550 " \"aaaaaaaaaaaaa\",\n" 6551 " \"aaaaaaaaaaaa\",\n" 6552 " \"aaaaaaaaaaaaaa\",\n" 6553 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6554 " \"aaaaaaaaaaaa\",\n" 6555 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6556 "};"); 6557 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6558 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6559 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6560 " 3, cccccccccccccccccccccc};", 6561 getLLVMStyleWithColumns(60)); 6562 6563 // Trailing commas. 6564 verifyFormat("vector<int> x = {\n" 6565 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6566 "};", 6567 getLLVMStyleWithColumns(39)); 6568 verifyFormat("vector<int> x = {\n" 6569 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6570 "};", 6571 getLLVMStyleWithColumns(39)); 6572 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6573 " 1, 1, 1, 1,\n" 6574 " /**/ /**/};", 6575 getLLVMStyleWithColumns(39)); 6576 6577 // Trailing comment in the first line. 6578 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6579 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6580 " 111111111, 222222222, 3333333333, 444444444, //\n" 6581 " 11111111, 22222222, 333333333, 44444444};"); 6582 // Trailing comment in the last line. 6583 verifyFormat("int aaaaa[] = {\n" 6584 " 1, 2, 3, // comment\n" 6585 " 4, 5, 6 // comment\n" 6586 "};"); 6587 6588 // With nested lists, we should either format one item per line or all nested 6589 // lists one on line. 6590 // FIXME: For some nested lists, we can do better. 6591 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6592 " {aaaaaaaaaaaaaaaaaaa},\n" 6593 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6594 " {aaaaaaaaaaaaaaaaa}};", 6595 getLLVMStyleWithColumns(60)); 6596 verifyFormat( 6597 "SomeStruct my_struct_array = {\n" 6598 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6599 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6600 " {aaa, aaa},\n" 6601 " {aaa, aaa},\n" 6602 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6603 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6604 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6605 6606 // No column layout should be used here. 6607 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6608 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6609 6610 verifyNoCrash("a<,"); 6611 6612 // No braced initializer here. 6613 verifyFormat("void f() {\n" 6614 " struct Dummy {};\n" 6615 " f(v);\n" 6616 "}"); 6617 6618 // Long lists should be formatted in columns even if they are nested. 6619 verifyFormat( 6620 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6621 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6622 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6623 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6624 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6625 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6626 } 6627 6628 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6629 FormatStyle DoNotMerge = getLLVMStyle(); 6630 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6631 6632 verifyFormat("void f() { return 42; }"); 6633 verifyFormat("void f() {\n" 6634 " return 42;\n" 6635 "}", 6636 DoNotMerge); 6637 verifyFormat("void f() {\n" 6638 " // Comment\n" 6639 "}"); 6640 verifyFormat("{\n" 6641 "#error {\n" 6642 " int a;\n" 6643 "}"); 6644 verifyFormat("{\n" 6645 " int a;\n" 6646 "#error {\n" 6647 "}"); 6648 verifyFormat("void f() {} // comment"); 6649 verifyFormat("void f() { int a; } // comment"); 6650 verifyFormat("void f() {\n" 6651 "} // comment", 6652 DoNotMerge); 6653 verifyFormat("void f() {\n" 6654 " int a;\n" 6655 "} // comment", 6656 DoNotMerge); 6657 verifyFormat("void f() {\n" 6658 "} // comment", 6659 getLLVMStyleWithColumns(15)); 6660 6661 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6662 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6663 6664 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6665 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6666 verifyFormat("class C {\n" 6667 " C()\n" 6668 " : iiiiiiii(nullptr),\n" 6669 " kkkkkkk(nullptr),\n" 6670 " mmmmmmm(nullptr),\n" 6671 " nnnnnnn(nullptr) {}\n" 6672 "};", 6673 getGoogleStyle()); 6674 6675 FormatStyle NoColumnLimit = getLLVMStyle(); 6676 NoColumnLimit.ColumnLimit = 0; 6677 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6678 EXPECT_EQ("class C {\n" 6679 " A() : b(0) {}\n" 6680 "};", 6681 format("class C{A():b(0){}};", NoColumnLimit)); 6682 EXPECT_EQ("A()\n" 6683 " : b(0) {\n" 6684 "}", 6685 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6686 6687 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6688 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6689 FormatStyle::SFS_None; 6690 EXPECT_EQ("A()\n" 6691 " : b(0) {\n" 6692 "}", 6693 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6694 EXPECT_EQ("A()\n" 6695 " : b(0) {\n" 6696 "}", 6697 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6698 6699 verifyFormat("#define A \\\n" 6700 " void f() { \\\n" 6701 " int i; \\\n" 6702 " }", 6703 getLLVMStyleWithColumns(20)); 6704 verifyFormat("#define A \\\n" 6705 " void f() { int i; }", 6706 getLLVMStyleWithColumns(21)); 6707 verifyFormat("#define A \\\n" 6708 " void f() { \\\n" 6709 " int i; \\\n" 6710 " } \\\n" 6711 " int j;", 6712 getLLVMStyleWithColumns(22)); 6713 verifyFormat("#define A \\\n" 6714 " void f() { int i; } \\\n" 6715 " int j;", 6716 getLLVMStyleWithColumns(23)); 6717 } 6718 6719 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6720 FormatStyle MergeInlineOnly = getLLVMStyle(); 6721 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6722 verifyFormat("class C {\n" 6723 " int f() { return 42; }\n" 6724 "};", 6725 MergeInlineOnly); 6726 verifyFormat("int f() {\n" 6727 " return 42;\n" 6728 "}", 6729 MergeInlineOnly); 6730 } 6731 6732 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6733 // Elaborate type variable declarations. 6734 verifyFormat("struct foo a = {bar};\nint n;"); 6735 verifyFormat("class foo a = {bar};\nint n;"); 6736 verifyFormat("union foo a = {bar};\nint n;"); 6737 6738 // Elaborate types inside function definitions. 6739 verifyFormat("struct foo f() {}\nint n;"); 6740 verifyFormat("class foo f() {}\nint n;"); 6741 verifyFormat("union foo f() {}\nint n;"); 6742 6743 // Templates. 6744 verifyFormat("template <class X> void f() {}\nint n;"); 6745 verifyFormat("template <struct X> void f() {}\nint n;"); 6746 verifyFormat("template <union X> void f() {}\nint n;"); 6747 6748 // Actual definitions... 6749 verifyFormat("struct {\n} n;"); 6750 verifyFormat( 6751 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6752 verifyFormat("union Z {\n int n;\n} x;"); 6753 verifyFormat("class MACRO Z {\n} n;"); 6754 verifyFormat("class MACRO(X) Z {\n} n;"); 6755 verifyFormat("class __attribute__(X) Z {\n} n;"); 6756 verifyFormat("class __declspec(X) Z {\n} n;"); 6757 verifyFormat("class A##B##C {\n} n;"); 6758 verifyFormat("class alignas(16) Z {\n} n;"); 6759 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6760 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6761 6762 // Redefinition from nested context: 6763 verifyFormat("class A::B::C {\n} n;"); 6764 6765 // Template definitions. 6766 verifyFormat( 6767 "template <typename F>\n" 6768 "Matcher(const Matcher<F> &Other,\n" 6769 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6770 " !is_same<F, T>::value>::type * = 0)\n" 6771 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6772 6773 // FIXME: This is still incorrectly handled at the formatter side. 6774 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6775 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6776 6777 // FIXME: 6778 // This now gets parsed incorrectly as class definition. 6779 // verifyFormat("class A<int> f() {\n}\nint n;"); 6780 6781 // Elaborate types where incorrectly parsing the structural element would 6782 // break the indent. 6783 verifyFormat("if (true)\n" 6784 " class X x;\n" 6785 "else\n" 6786 " f();\n"); 6787 6788 // This is simply incomplete. Formatting is not important, but must not crash. 6789 verifyFormat("class A:"); 6790 } 6791 6792 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6793 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6794 format("#error Leave all white!!!!! space* alone!\n")); 6795 EXPECT_EQ( 6796 "#warning Leave all white!!!!! space* alone!\n", 6797 format("#warning Leave all white!!!!! space* alone!\n")); 6798 EXPECT_EQ("#error 1", format(" # error 1")); 6799 EXPECT_EQ("#warning 1", format(" # warning 1")); 6800 } 6801 6802 TEST_F(FormatTest, FormatHashIfExpressions) { 6803 verifyFormat("#if AAAA && BBBB"); 6804 verifyFormat("#if (AAAA && BBBB)"); 6805 verifyFormat("#elif (AAAA && BBBB)"); 6806 // FIXME: Come up with a better indentation for #elif. 6807 verifyFormat( 6808 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6809 " defined(BBBBBBBB)\n" 6810 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6811 " defined(BBBBBBBB)\n" 6812 "#endif", 6813 getLLVMStyleWithColumns(65)); 6814 } 6815 6816 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6817 FormatStyle AllowsMergedIf = getGoogleStyle(); 6818 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6819 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6820 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6821 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6822 EXPECT_EQ("if (true) return 42;", 6823 format("if (true)\nreturn 42;", AllowsMergedIf)); 6824 FormatStyle ShortMergedIf = AllowsMergedIf; 6825 ShortMergedIf.ColumnLimit = 25; 6826 verifyFormat("#define A \\\n" 6827 " if (true) return 42;", 6828 ShortMergedIf); 6829 verifyFormat("#define A \\\n" 6830 " f(); \\\n" 6831 " if (true)\n" 6832 "#define B", 6833 ShortMergedIf); 6834 verifyFormat("#define A \\\n" 6835 " f(); \\\n" 6836 " if (true)\n" 6837 "g();", 6838 ShortMergedIf); 6839 verifyFormat("{\n" 6840 "#ifdef A\n" 6841 " // Comment\n" 6842 " if (true) continue;\n" 6843 "#endif\n" 6844 " // Comment\n" 6845 " if (true) continue;\n" 6846 "}", 6847 ShortMergedIf); 6848 ShortMergedIf.ColumnLimit = 29; 6849 verifyFormat("#define A \\\n" 6850 " if (aaaaaaaaaa) return 1; \\\n" 6851 " return 2;", 6852 ShortMergedIf); 6853 ShortMergedIf.ColumnLimit = 28; 6854 verifyFormat("#define A \\\n" 6855 " if (aaaaaaaaaa) \\\n" 6856 " return 1; \\\n" 6857 " return 2;", 6858 ShortMergedIf); 6859 } 6860 6861 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6862 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6863 " f();\n" 6864 "}"); 6865 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6866 " f();\n" 6867 "} /* another comment */ else /* comment #3 */ {\n" 6868 " g();\n" 6869 "}"); 6870 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6871 " f();\n" 6872 "}"); 6873 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6874 " f();\n" 6875 "}"); 6876 verifyFormat("do /* a comment in a strange place */ {\n" 6877 " f();\n" 6878 "} /* another comment */ while (0);"); 6879 } 6880 6881 TEST_F(FormatTest, BlockComments) { 6882 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6883 format("/* *//* */ /* */\n/* *//* */ /* */")); 6884 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6885 EXPECT_EQ("#define A /*123*/ \\\n" 6886 " b\n" 6887 "/* */\n" 6888 "someCall(\n" 6889 " parameter);", 6890 format("#define A /*123*/ b\n" 6891 "/* */\n" 6892 "someCall(parameter);", 6893 getLLVMStyleWithColumns(15))); 6894 6895 EXPECT_EQ("#define A\n" 6896 "/* */ someCall(\n" 6897 " parameter);", 6898 format("#define A\n" 6899 "/* */someCall(parameter);", 6900 getLLVMStyleWithColumns(15))); 6901 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6902 EXPECT_EQ("/*\n" 6903 "*\n" 6904 " * aaaaaa\n" 6905 " * aaaaaa\n" 6906 "*/", 6907 format("/*\n" 6908 "*\n" 6909 " * aaaaaa aaaaaa\n" 6910 "*/", 6911 getLLVMStyleWithColumns(10))); 6912 EXPECT_EQ("/*\n" 6913 "**\n" 6914 "* aaaaaa\n" 6915 "*aaaaaa\n" 6916 "*/", 6917 format("/*\n" 6918 "**\n" 6919 "* aaaaaa aaaaaa\n" 6920 "*/", 6921 getLLVMStyleWithColumns(10))); 6922 6923 FormatStyle NoBinPacking = getLLVMStyle(); 6924 NoBinPacking.BinPackParameters = false; 6925 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6926 " 2, /* comment 2 */\n" 6927 " 3, /* comment 3 */\n" 6928 " aaaa,\n" 6929 " bbbb);", 6930 format("someFunction (1, /* comment 1 */\n" 6931 " 2, /* comment 2 */ \n" 6932 " 3, /* comment 3 */\n" 6933 "aaaa, bbbb );", 6934 NoBinPacking)); 6935 verifyFormat( 6936 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6938 EXPECT_EQ( 6939 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6940 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 6942 format( 6943 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6944 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 6946 EXPECT_EQ( 6947 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6948 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6949 "int cccccccccccccccccccccccccccccc; /* comment */\n", 6950 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6951 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6952 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 6953 6954 verifyFormat("void f(int * /* unused */) {}"); 6955 6956 EXPECT_EQ("/*\n" 6957 " **\n" 6958 " */", 6959 format("/*\n" 6960 " **\n" 6961 " */")); 6962 EXPECT_EQ("/*\n" 6963 " *q\n" 6964 " */", 6965 format("/*\n" 6966 " *q\n" 6967 " */")); 6968 EXPECT_EQ("/*\n" 6969 " * q\n" 6970 " */", 6971 format("/*\n" 6972 " * q\n" 6973 " */")); 6974 EXPECT_EQ("/*\n" 6975 " **/", 6976 format("/*\n" 6977 " **/")); 6978 EXPECT_EQ("/*\n" 6979 " ***/", 6980 format("/*\n" 6981 " ***/")); 6982 } 6983 6984 TEST_F(FormatTest, BlockCommentsInMacros) { 6985 EXPECT_EQ("#define A \\\n" 6986 " { \\\n" 6987 " /* one line */ \\\n" 6988 " someCall();", 6989 format("#define A { \\\n" 6990 " /* one line */ \\\n" 6991 " someCall();", 6992 getLLVMStyleWithColumns(20))); 6993 EXPECT_EQ("#define A \\\n" 6994 " { \\\n" 6995 " /* previous */ \\\n" 6996 " /* one line */ \\\n" 6997 " someCall();", 6998 format("#define A { \\\n" 6999 " /* previous */ \\\n" 7000 " /* one line */ \\\n" 7001 " someCall();", 7002 getLLVMStyleWithColumns(20))); 7003 } 7004 7005 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 7006 EXPECT_EQ("a = {\n" 7007 " 1111 /* */\n" 7008 "};", 7009 format("a = {1111 /* */\n" 7010 "};", 7011 getLLVMStyleWithColumns(15))); 7012 EXPECT_EQ("a = {\n" 7013 " 1111 /* */\n" 7014 "};", 7015 format("a = {1111 /* */\n" 7016 "};", 7017 getLLVMStyleWithColumns(15))); 7018 7019 // FIXME: The formatting is still wrong here. 7020 EXPECT_EQ("a = {\n" 7021 " 1111 /* a\n" 7022 " */\n" 7023 "};", 7024 format("a = {1111 /* a */\n" 7025 "};", 7026 getLLVMStyleWithColumns(15))); 7027 } 7028 7029 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 7030 // FIXME: This is not what we want... 7031 verifyFormat("{\n" 7032 "// a" 7033 "// b"); 7034 } 7035 7036 TEST_F(FormatTest, FormatStarDependingOnContext) { 7037 verifyFormat("void f(int *a);"); 7038 verifyFormat("void f() { f(fint * b); }"); 7039 verifyFormat("class A {\n void f(int *a);\n};"); 7040 verifyFormat("class A {\n int *a;\n};"); 7041 verifyFormat("namespace a {\n" 7042 "namespace b {\n" 7043 "class A {\n" 7044 " void f() {}\n" 7045 " int *a;\n" 7046 "};\n" 7047 "}\n" 7048 "}"); 7049 } 7050 7051 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7052 verifyFormat("while"); 7053 verifyFormat("operator"); 7054 } 7055 7056 //===----------------------------------------------------------------------===// 7057 // Objective-C tests. 7058 //===----------------------------------------------------------------------===// 7059 7060 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7061 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7062 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7063 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7064 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7065 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7066 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7067 format("-(NSInteger)Method3:(id)anObject;")); 7068 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7069 format("-(NSInteger)Method4:(id)anObject;")); 7070 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7071 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7072 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7073 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7074 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7075 "forAllCells:(BOOL)flag;", 7076 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7077 "forAllCells:(BOOL)flag;")); 7078 7079 // Very long objectiveC method declaration. 7080 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7081 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7082 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7083 " inRange:(NSRange)range\n" 7084 " outRange:(NSRange)out_range\n" 7085 " outRange1:(NSRange)out_range1\n" 7086 " outRange2:(NSRange)out_range2\n" 7087 " outRange3:(NSRange)out_range3\n" 7088 " outRange4:(NSRange)out_range4\n" 7089 " outRange5:(NSRange)out_range5\n" 7090 " outRange6:(NSRange)out_range6\n" 7091 " outRange7:(NSRange)out_range7\n" 7092 " outRange8:(NSRange)out_range8\n" 7093 " outRange9:(NSRange)out_range9;"); 7094 7095 // When the function name has to be wrapped. 7096 FormatStyle Style = getLLVMStyle(); 7097 Style.IndentWrappedFunctionNames = false; 7098 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7099 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7100 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7101 "}", 7102 Style); 7103 Style.IndentWrappedFunctionNames = true; 7104 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7105 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7106 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7107 "}", 7108 Style); 7109 7110 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7111 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7112 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7113 // protocol lists (but not for template classes): 7114 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7115 7116 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7117 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7118 7119 // If there's no return type (very rare in practice!), LLVM and Google style 7120 // agree. 7121 verifyFormat("- foo;"); 7122 verifyFormat("- foo:(int)f;"); 7123 verifyGoogleFormat("- foo:(int)foo;"); 7124 } 7125 7126 TEST_F(FormatTest, FormatObjCInterface) { 7127 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 7128 "@public\n" 7129 " int field1;\n" 7130 "@protected\n" 7131 " int field2;\n" 7132 "@private\n" 7133 " int field3;\n" 7134 "@package\n" 7135 " int field4;\n" 7136 "}\n" 7137 "+ (id)init;\n" 7138 "@end"); 7139 7140 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7141 " @public\n" 7142 " int field1;\n" 7143 " @protected\n" 7144 " int field2;\n" 7145 " @private\n" 7146 " int field3;\n" 7147 " @package\n" 7148 " int field4;\n" 7149 "}\n" 7150 "+ (id)init;\n" 7151 "@end"); 7152 7153 verifyFormat("@interface /* wait for it */ Foo\n" 7154 "+ (id)init;\n" 7155 "// Look, a comment!\n" 7156 "- (int)answerWith:(int)i;\n" 7157 "@end"); 7158 7159 verifyFormat("@interface Foo\n" 7160 "@end\n" 7161 "@interface Bar\n" 7162 "@end"); 7163 7164 verifyFormat("@interface Foo : Bar\n" 7165 "+ (id)init;\n" 7166 "@end"); 7167 7168 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7169 "+ (id)init;\n" 7170 "@end"); 7171 7172 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7173 "+ (id)init;\n" 7174 "@end"); 7175 7176 verifyFormat("@interface Foo (HackStuff)\n" 7177 "+ (id)init;\n" 7178 "@end"); 7179 7180 verifyFormat("@interface Foo ()\n" 7181 "+ (id)init;\n" 7182 "@end"); 7183 7184 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7185 "+ (id)init;\n" 7186 "@end"); 7187 7188 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7189 "+ (id)init;\n" 7190 "@end"); 7191 7192 verifyFormat("@interface Foo {\n" 7193 " int _i;\n" 7194 "}\n" 7195 "+ (id)init;\n" 7196 "@end"); 7197 7198 verifyFormat("@interface Foo : Bar {\n" 7199 " int _i;\n" 7200 "}\n" 7201 "+ (id)init;\n" 7202 "@end"); 7203 7204 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7205 " int _i;\n" 7206 "}\n" 7207 "+ (id)init;\n" 7208 "@end"); 7209 7210 verifyFormat("@interface Foo (HackStuff) {\n" 7211 " int _i;\n" 7212 "}\n" 7213 "+ (id)init;\n" 7214 "@end"); 7215 7216 verifyFormat("@interface Foo () {\n" 7217 " int _i;\n" 7218 "}\n" 7219 "+ (id)init;\n" 7220 "@end"); 7221 7222 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7223 " int _i;\n" 7224 "}\n" 7225 "+ (id)init;\n" 7226 "@end"); 7227 7228 FormatStyle OnePerLine = getGoogleStyle(); 7229 OnePerLine.BinPackParameters = false; 7230 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7234 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7235 "}", 7236 OnePerLine); 7237 } 7238 7239 TEST_F(FormatTest, FormatObjCImplementation) { 7240 verifyFormat("@implementation Foo : NSObject {\n" 7241 "@public\n" 7242 " int field1;\n" 7243 "@protected\n" 7244 " int field2;\n" 7245 "@private\n" 7246 " int field3;\n" 7247 "@package\n" 7248 " int field4;\n" 7249 "}\n" 7250 "+ (id)init {\n}\n" 7251 "@end"); 7252 7253 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7254 " @public\n" 7255 " int field1;\n" 7256 " @protected\n" 7257 " int field2;\n" 7258 " @private\n" 7259 " int field3;\n" 7260 " @package\n" 7261 " int field4;\n" 7262 "}\n" 7263 "+ (id)init {\n}\n" 7264 "@end"); 7265 7266 verifyFormat("@implementation Foo\n" 7267 "+ (id)init {\n" 7268 " if (true)\n" 7269 " return nil;\n" 7270 "}\n" 7271 "// Look, a comment!\n" 7272 "- (int)answerWith:(int)i {\n" 7273 " return i;\n" 7274 "}\n" 7275 "+ (int)answerWith:(int)i {\n" 7276 " return i;\n" 7277 "}\n" 7278 "@end"); 7279 7280 verifyFormat("@implementation Foo\n" 7281 "@end\n" 7282 "@implementation Bar\n" 7283 "@end"); 7284 7285 EXPECT_EQ("@implementation Foo : Bar\n" 7286 "+ (id)init {\n}\n" 7287 "- (void)foo {\n}\n" 7288 "@end", 7289 format("@implementation Foo : Bar\n" 7290 "+(id)init{}\n" 7291 "-(void)foo{}\n" 7292 "@end")); 7293 7294 verifyFormat("@implementation Foo {\n" 7295 " int _i;\n" 7296 "}\n" 7297 "+ (id)init {\n}\n" 7298 "@end"); 7299 7300 verifyFormat("@implementation Foo : Bar {\n" 7301 " int _i;\n" 7302 "}\n" 7303 "+ (id)init {\n}\n" 7304 "@end"); 7305 7306 verifyFormat("@implementation Foo (HackStuff)\n" 7307 "+ (id)init {\n}\n" 7308 "@end"); 7309 verifyFormat("@implementation ObjcClass\n" 7310 "- (void)method;\n" 7311 "{}\n" 7312 "@end"); 7313 } 7314 7315 TEST_F(FormatTest, FormatObjCProtocol) { 7316 verifyFormat("@protocol Foo\n" 7317 "@property(weak) id delegate;\n" 7318 "- (NSUInteger)numberOfThings;\n" 7319 "@end"); 7320 7321 verifyFormat("@protocol MyProtocol <NSObject>\n" 7322 "- (NSUInteger)numberOfThings;\n" 7323 "@end"); 7324 7325 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7326 "- (NSUInteger)numberOfThings;\n" 7327 "@end"); 7328 7329 verifyFormat("@protocol Foo;\n" 7330 "@protocol Bar;\n"); 7331 7332 verifyFormat("@protocol Foo\n" 7333 "@end\n" 7334 "@protocol Bar\n" 7335 "@end"); 7336 7337 verifyFormat("@protocol myProtocol\n" 7338 "- (void)mandatoryWithInt:(int)i;\n" 7339 "@optional\n" 7340 "- (void)optional;\n" 7341 "@required\n" 7342 "- (void)required;\n" 7343 "@optional\n" 7344 "@property(assign) int madProp;\n" 7345 "@end\n"); 7346 7347 verifyFormat("@property(nonatomic, assign, readonly)\n" 7348 " int *looooooooooooooooooooooooooooongNumber;\n" 7349 "@property(nonatomic, assign, readonly)\n" 7350 " NSString *looooooooooooooooooooooooooooongName;"); 7351 7352 verifyFormat("@implementation PR18406\n" 7353 "}\n" 7354 "@end"); 7355 } 7356 7357 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7358 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7359 " rect:(NSRect)theRect\n" 7360 " interval:(float)theInterval {\n" 7361 "}"); 7362 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7363 " longKeyword:(NSRect)theRect\n" 7364 " longerKeyword:(float)theInterval\n" 7365 " error:(NSError **)theError {\n" 7366 "}"); 7367 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7368 " longKeyword:(NSRect)theRect\n" 7369 " evenLongerKeyword:(float)theInterval\n" 7370 " error:(NSError **)theError {\n" 7371 "}"); 7372 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7373 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7374 " NS_DESIGNATED_INITIALIZER;", 7375 getLLVMStyleWithColumns(60)); 7376 7377 // Continuation indent width should win over aligning colons if the function 7378 // name is long. 7379 FormatStyle continuationStyle = getGoogleStyle(); 7380 continuationStyle.ColumnLimit = 40; 7381 continuationStyle.IndentWrappedFunctionNames = true; 7382 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7383 " dontAlignNamef:(NSRect)theRect {\n" 7384 "}", 7385 continuationStyle); 7386 7387 // Make sure we don't break aligning for short parameter names. 7388 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7389 " aShortf:(NSRect)theRect {\n" 7390 "}", 7391 continuationStyle); 7392 } 7393 7394 TEST_F(FormatTest, FormatObjCMethodExpr) { 7395 verifyFormat("[foo bar:baz];"); 7396 verifyFormat("return [foo bar:baz];"); 7397 verifyFormat("return (a)[foo bar:baz];"); 7398 verifyFormat("f([foo bar:baz]);"); 7399 verifyFormat("f(2, [foo bar:baz]);"); 7400 verifyFormat("f(2, a ? b : c);"); 7401 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7402 7403 // Unary operators. 7404 verifyFormat("int a = +[foo bar:baz];"); 7405 verifyFormat("int a = -[foo bar:baz];"); 7406 verifyFormat("int a = ![foo bar:baz];"); 7407 verifyFormat("int a = ~[foo bar:baz];"); 7408 verifyFormat("int a = ++[foo bar:baz];"); 7409 verifyFormat("int a = --[foo bar:baz];"); 7410 verifyFormat("int a = sizeof [foo bar:baz];"); 7411 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7412 verifyFormat("int a = &[foo bar:baz];"); 7413 verifyFormat("int a = *[foo bar:baz];"); 7414 // FIXME: Make casts work, without breaking f()[4]. 7415 // verifyFormat("int a = (int)[foo bar:baz];"); 7416 // verifyFormat("return (int)[foo bar:baz];"); 7417 // verifyFormat("(void)[foo bar:baz];"); 7418 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7419 7420 // Binary operators. 7421 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7422 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7423 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7424 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7425 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7426 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7427 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7428 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7429 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7430 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7431 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7432 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7433 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7434 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7435 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7436 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7437 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7438 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7439 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7440 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7441 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7442 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7443 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7444 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7445 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7446 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7447 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7448 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7449 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7450 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7451 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7452 // Whew! 7453 7454 verifyFormat("return in[42];"); 7455 verifyFormat("for (auto v : in[1]) {\n}"); 7456 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7457 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7458 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7459 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7460 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7461 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7462 "}"); 7463 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7464 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7465 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7466 7467 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7468 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7469 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7470 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7471 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7472 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7473 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7474 7475 verifyFormat("arr[[self indexForFoo:a]];"); 7476 verifyFormat("throw [self errorFor:a];"); 7477 verifyFormat("@throw [self errorFor:a];"); 7478 7479 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7480 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7481 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7482 7483 // This tests that the formatter doesn't break after "backing" but before ":", 7484 // which would be at 80 columns. 7485 verifyFormat( 7486 "void f() {\n" 7487 " if ((self = [super initWithContentRect:contentRect\n" 7488 " styleMask:styleMask ?: otherMask\n" 7489 " backing:NSBackingStoreBuffered\n" 7490 " defer:YES]))"); 7491 7492 verifyFormat( 7493 "[foo checkThatBreakingAfterColonWorksOk:\n" 7494 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7495 7496 verifyFormat("[myObj short:arg1 // Force line break\n" 7497 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7498 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7499 " error:arg4];"); 7500 verifyFormat( 7501 "void f() {\n" 7502 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7503 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7504 " pos.width(), pos.height())\n" 7505 " styleMask:NSBorderlessWindowMask\n" 7506 " backing:NSBackingStoreBuffered\n" 7507 " defer:NO]);\n" 7508 "}"); 7509 verifyFormat( 7510 "void f() {\n" 7511 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7512 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7513 " pos.width(), pos.height())\n" 7514 " syeMask:NSBorderlessWindowMask\n" 7515 " bking:NSBackingStoreBuffered\n" 7516 " der:NO]);\n" 7517 "}", 7518 getLLVMStyleWithColumns(70)); 7519 verifyFormat( 7520 "void f() {\n" 7521 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7522 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7523 " pos.width(), pos.height())\n" 7524 " styleMask:NSBorderlessWindowMask\n" 7525 " backing:NSBackingStoreBuffered\n" 7526 " defer:NO]);\n" 7527 "}", 7528 getChromiumStyle(FormatStyle::LK_Cpp)); 7529 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7530 " with:contentsNativeView];"); 7531 7532 verifyFormat( 7533 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7534 " owner:nillllll];"); 7535 7536 verifyFormat( 7537 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7538 " forType:kBookmarkButtonDragType];"); 7539 7540 verifyFormat("[defaultCenter addObserver:self\n" 7541 " selector:@selector(willEnterFullscreen)\n" 7542 " name:kWillEnterFullscreenNotification\n" 7543 " object:nil];"); 7544 verifyFormat("[image_rep drawInRect:drawRect\n" 7545 " fromRect:NSZeroRect\n" 7546 " operation:NSCompositeCopy\n" 7547 " fraction:1.0\n" 7548 " respectFlipped:NO\n" 7549 " hints:nil];"); 7550 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7552 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7554 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7555 " aaaaaaaaaaaaaaaaaaaaaa];"); 7556 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7557 " .aaaaaaaa];", // FIXME: Indentation seems off. 7558 getLLVMStyleWithColumns(60)); 7559 7560 verifyFormat( 7561 "scoped_nsobject<NSTextField> message(\n" 7562 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7563 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7564 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7565 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7566 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7567 " aaaa:bbb];"); 7568 verifyFormat("[self param:function( //\n" 7569 " parameter)]"); 7570 verifyFormat( 7571 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7572 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7573 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7574 7575 // FIXME: This violates the column limit. 7576 verifyFormat( 7577 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7578 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7579 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7580 getLLVMStyleWithColumns(60)); 7581 7582 // Variadic parameters. 7583 verifyFormat( 7584 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7585 verifyFormat( 7586 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7587 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7588 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7589 verifyFormat("[self // break\n" 7590 " a:a\n" 7591 " aaa:aaa];"); 7592 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7593 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7594 } 7595 7596 TEST_F(FormatTest, ObjCAt) { 7597 verifyFormat("@autoreleasepool"); 7598 verifyFormat("@catch"); 7599 verifyFormat("@class"); 7600 verifyFormat("@compatibility_alias"); 7601 verifyFormat("@defs"); 7602 verifyFormat("@dynamic"); 7603 verifyFormat("@encode"); 7604 verifyFormat("@end"); 7605 verifyFormat("@finally"); 7606 verifyFormat("@implementation"); 7607 verifyFormat("@import"); 7608 verifyFormat("@interface"); 7609 verifyFormat("@optional"); 7610 verifyFormat("@package"); 7611 verifyFormat("@private"); 7612 verifyFormat("@property"); 7613 verifyFormat("@protected"); 7614 verifyFormat("@protocol"); 7615 verifyFormat("@public"); 7616 verifyFormat("@required"); 7617 verifyFormat("@selector"); 7618 verifyFormat("@synchronized"); 7619 verifyFormat("@synthesize"); 7620 verifyFormat("@throw"); 7621 verifyFormat("@try"); 7622 7623 EXPECT_EQ("@interface", format("@ interface")); 7624 7625 // The precise formatting of this doesn't matter, nobody writes code like 7626 // this. 7627 verifyFormat("@ /*foo*/ interface"); 7628 } 7629 7630 TEST_F(FormatTest, ObjCSnippets) { 7631 verifyFormat("@autoreleasepool {\n" 7632 " foo();\n" 7633 "}"); 7634 verifyFormat("@class Foo, Bar;"); 7635 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7636 verifyFormat("@dynamic textColor;"); 7637 verifyFormat("char *buf1 = @encode(int *);"); 7638 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7639 verifyFormat("char *buf1 = @encode(int **);"); 7640 verifyFormat("Protocol *proto = @protocol(p1);"); 7641 verifyFormat("SEL s = @selector(foo:);"); 7642 verifyFormat("@synchronized(self) {\n" 7643 " f();\n" 7644 "}"); 7645 7646 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7647 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7648 7649 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7650 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7651 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7652 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7653 getMozillaStyle()); 7654 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7655 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7656 getWebKitStyle()); 7657 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7658 7659 verifyFormat("@import foo.bar;\n" 7660 "@import baz;"); 7661 } 7662 7663 TEST_F(FormatTest, ObjCForIn) { 7664 verifyFormat("- (void)test {\n" 7665 " for (NSString *n in arrayOfStrings) {\n" 7666 " foo(n);\n" 7667 " }\n" 7668 "}"); 7669 verifyFormat("- (void)test {\n" 7670 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7671 " foo(n);\n" 7672 " }\n" 7673 "}"); 7674 } 7675 7676 TEST_F(FormatTest, ObjCLiterals) { 7677 verifyFormat("@\"String\""); 7678 verifyFormat("@1"); 7679 verifyFormat("@+4.8"); 7680 verifyFormat("@-4"); 7681 verifyFormat("@1LL"); 7682 verifyFormat("@.5"); 7683 verifyFormat("@'c'"); 7684 verifyFormat("@true"); 7685 7686 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7687 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7688 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7689 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7690 7691 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7692 } 7693 7694 TEST_F(FormatTest, ObjCDictLiterals) { 7695 verifyFormat("@{"); 7696 verifyFormat("@{}"); 7697 verifyFormat("@{@\"one\" : @1}"); 7698 verifyFormat("return @{@\"one\" : @1;"); 7699 verifyFormat("@{@\"one\" : @1}"); 7700 7701 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7702 verifyFormat("@{\n" 7703 " @\"one\" : @{@2 : @1},\n" 7704 "}"); 7705 7706 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7707 verifyIncompleteFormat("[self setDict:@{}"); 7708 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7709 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7710 verifyFormat( 7711 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7712 verifyFormat( 7713 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7714 7715 verifyFormat("NSDictionary *d = @{\n" 7716 " @\"nam\" : NSUserNam(),\n" 7717 " @\"dte\" : [NSDate date],\n" 7718 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7719 "};"); 7720 verifyFormat( 7721 "@{\n" 7722 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7723 "regularFont,\n" 7724 "};"); 7725 verifyGoogleFormat( 7726 "@{\n" 7727 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7728 "regularFont,\n" 7729 "};"); 7730 verifyFormat( 7731 "@{\n" 7732 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7733 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7734 "};"); 7735 7736 // We should try to be robust in case someone forgets the "@". 7737 verifyFormat("NSDictionary *d = {\n" 7738 " @\"nam\" : NSUserNam(),\n" 7739 " @\"dte\" : [NSDate date],\n" 7740 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7741 "};"); 7742 verifyFormat("NSMutableDictionary *dictionary =\n" 7743 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7744 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7745 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7746 " cccccccccccccccc : ccccccccccccccc\n" 7747 " }];"); 7748 7749 // Ensure that casts before the key are kept on the same line as the key. 7750 verifyFormat( 7751 "NSDictionary *d = @{\n" 7752 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7753 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7754 "};"); 7755 } 7756 7757 TEST_F(FormatTest, ObjCArrayLiterals) { 7758 verifyIncompleteFormat("@["); 7759 verifyFormat("@[]"); 7760 verifyFormat( 7761 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7762 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7763 verifyFormat("NSArray *array = @[ [foo description] ];"); 7764 7765 verifyFormat( 7766 "NSArray *some_variable = @[\n" 7767 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7768 " @\"aaaaaaaaaaaaaaaaa\",\n" 7769 " @\"aaaaaaaaaaaaaaaaa\",\n" 7770 " @\"aaaaaaaaaaaaaaaaa\",\n" 7771 "];"); 7772 verifyFormat( 7773 "NSArray *some_variable = @[\n" 7774 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7775 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 7776 "];"); 7777 verifyFormat("NSArray *some_variable = @[\n" 7778 " @\"aaaaaaaaaaaaaaaaa\",\n" 7779 " @\"aaaaaaaaaaaaaaaaa\",\n" 7780 " @\"aaaaaaaaaaaaaaaaa\",\n" 7781 " @\"aaaaaaaaaaaaaaaaa\",\n" 7782 "];"); 7783 verifyFormat("NSArray *array = @[\n" 7784 " @\"a\",\n" 7785 " @\"a\",\n" // Trailing comma -> one per line. 7786 "];"); 7787 7788 // We should try to be robust in case someone forgets the "@". 7789 verifyFormat("NSArray *some_variable = [\n" 7790 " @\"aaaaaaaaaaaaaaaaa\",\n" 7791 " @\"aaaaaaaaaaaaaaaaa\",\n" 7792 " @\"aaaaaaaaaaaaaaaaa\",\n" 7793 " @\"aaaaaaaaaaaaaaaaa\",\n" 7794 "];"); 7795 verifyFormat( 7796 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7797 " index:(NSUInteger)index\n" 7798 " nonDigitAttributes:\n" 7799 " (NSDictionary *)noDigitAttributes;"); 7800 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7801 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7802 "]];"); 7803 } 7804 7805 TEST_F(FormatTest, BreaksStringLiterals) { 7806 EXPECT_EQ("\"some text \"\n" 7807 "\"other\";", 7808 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7809 EXPECT_EQ("\"some text \"\n" 7810 "\"other\";", 7811 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7812 EXPECT_EQ( 7813 "#define A \\\n" 7814 " \"some \" \\\n" 7815 " \"text \" \\\n" 7816 " \"other\";", 7817 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7818 EXPECT_EQ( 7819 "#define A \\\n" 7820 " \"so \" \\\n" 7821 " \"text \" \\\n" 7822 " \"other\";", 7823 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7824 7825 EXPECT_EQ("\"some text\"", 7826 format("\"some text\"", getLLVMStyleWithColumns(1))); 7827 EXPECT_EQ("\"some text\"", 7828 format("\"some text\"", getLLVMStyleWithColumns(11))); 7829 EXPECT_EQ("\"some \"\n" 7830 "\"text\"", 7831 format("\"some text\"", getLLVMStyleWithColumns(10))); 7832 EXPECT_EQ("\"some \"\n" 7833 "\"text\"", 7834 format("\"some text\"", getLLVMStyleWithColumns(7))); 7835 EXPECT_EQ("\"some\"\n" 7836 "\" tex\"\n" 7837 "\"t\"", 7838 format("\"some text\"", getLLVMStyleWithColumns(6))); 7839 EXPECT_EQ("\"some\"\n" 7840 "\" tex\"\n" 7841 "\" and\"", 7842 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7843 EXPECT_EQ("\"some\"\n" 7844 "\"/tex\"\n" 7845 "\"/and\"", 7846 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7847 7848 EXPECT_EQ("variable =\n" 7849 " \"long string \"\n" 7850 " \"literal\";", 7851 format("variable = \"long string literal\";", 7852 getLLVMStyleWithColumns(20))); 7853 7854 EXPECT_EQ("variable = f(\n" 7855 " \"long string \"\n" 7856 " \"literal\",\n" 7857 " short,\n" 7858 " loooooooooooooooooooong);", 7859 format("variable = f(\"long string literal\", short, " 7860 "loooooooooooooooooooong);", 7861 getLLVMStyleWithColumns(20))); 7862 7863 EXPECT_EQ( 7864 "f(g(\"long string \"\n" 7865 " \"literal\"),\n" 7866 " b);", 7867 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7868 EXPECT_EQ("f(g(\"long string \"\n" 7869 " \"literal\",\n" 7870 " a),\n" 7871 " b);", 7872 format("f(g(\"long string literal\", a), b);", 7873 getLLVMStyleWithColumns(20))); 7874 EXPECT_EQ( 7875 "f(\"one two\".split(\n" 7876 " variable));", 7877 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7878 EXPECT_EQ("f(\"one two three four five six \"\n" 7879 " \"seven\".split(\n" 7880 " really_looooong_variable));", 7881 format("f(\"one two three four five six seven\"." 7882 "split(really_looooong_variable));", 7883 getLLVMStyleWithColumns(33))); 7884 7885 EXPECT_EQ("f(\"some \"\n" 7886 " \"text\",\n" 7887 " other);", 7888 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7889 7890 // Only break as a last resort. 7891 verifyFormat( 7892 "aaaaaaaaaaaaaaaaaaaa(\n" 7893 " aaaaaaaaaaaaaaaaaaaa,\n" 7894 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7895 7896 EXPECT_EQ("\"splitmea\"\n" 7897 "\"trandomp\"\n" 7898 "\"oint\"", 7899 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7900 7901 EXPECT_EQ("\"split/\"\n" 7902 "\"pathat/\"\n" 7903 "\"slashes\"", 7904 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7905 7906 EXPECT_EQ("\"split/\"\n" 7907 "\"pathat/\"\n" 7908 "\"slashes\"", 7909 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7910 EXPECT_EQ("\"split at \"\n" 7911 "\"spaces/at/\"\n" 7912 "\"slashes.at.any$\"\n" 7913 "\"non-alphanumeric%\"\n" 7914 "\"1111111111characte\"\n" 7915 "\"rs\"", 7916 format("\"split at " 7917 "spaces/at/" 7918 "slashes.at." 7919 "any$non-" 7920 "alphanumeric%" 7921 "1111111111characte" 7922 "rs\"", 7923 getLLVMStyleWithColumns(20))); 7924 7925 // Verify that splitting the strings understands 7926 // Style::AlwaysBreakBeforeMultilineStrings. 7927 EXPECT_EQ( 7928 "aaaaaaaaaaaa(\n" 7929 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7930 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7931 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7932 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7933 "aaaaaaaaaaaaaaaaaaaaaa\");", 7934 getGoogleStyle())); 7935 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7936 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7937 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7938 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7939 "aaaaaaaaaaaaaaaaaaaaaa\";", 7940 getGoogleStyle())); 7941 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7942 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7943 format("llvm::outs() << " 7944 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7945 "aaaaaaaaaaaaaaaaaaa\";")); 7946 EXPECT_EQ("ffff(\n" 7947 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7948 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7949 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7950 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7951 getGoogleStyle())); 7952 7953 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7954 AlignLeft.AlignEscapedNewlinesLeft = true; 7955 EXPECT_EQ("#define A \\\n" 7956 " \"some \" \\\n" 7957 " \"text \" \\\n" 7958 " \"other\";", 7959 format("#define A \"some text other\";", AlignLeft)); 7960 } 7961 7962 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7963 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7964 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7965 EXPECT_EQ("int i = a(b());", 7966 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7967 } 7968 7969 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7970 EXPECT_EQ( 7971 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7972 "(\n" 7973 " \"x\t\");", 7974 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7975 "aaaaaaa(" 7976 "\"x\t\");")); 7977 } 7978 7979 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7980 EXPECT_EQ( 7981 "u8\"utf8 string \"\n" 7982 "u8\"literal\";", 7983 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7984 EXPECT_EQ( 7985 "u\"utf16 string \"\n" 7986 "u\"literal\";", 7987 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7988 EXPECT_EQ( 7989 "U\"utf32 string \"\n" 7990 "U\"literal\";", 7991 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7992 EXPECT_EQ("L\"wide string \"\n" 7993 "L\"literal\";", 7994 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7995 EXPECT_EQ("@\"NSString \"\n" 7996 "@\"literal\";", 7997 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7998 7999 // This input makes clang-format try to split the incomplete unicode escape 8000 // sequence, which used to lead to a crasher. 8001 verifyNoCrash( 8002 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8003 getLLVMStyleWithColumns(60)); 8004 } 8005 8006 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8007 FormatStyle Style = getGoogleStyleWithColumns(15); 8008 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8009 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8010 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8011 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8012 EXPECT_EQ("u8R\"x(raw literal)x\";", 8013 format("u8R\"x(raw literal)x\";", Style)); 8014 } 8015 8016 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8017 FormatStyle Style = getLLVMStyleWithColumns(20); 8018 EXPECT_EQ( 8019 "_T(\"aaaaaaaaaaaaaa\")\n" 8020 "_T(\"aaaaaaaaaaaaaa\")\n" 8021 "_T(\"aaaaaaaaaaaa\")", 8022 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8023 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 8024 " _T(\"aaaaaa\"),\n" 8025 " z);", 8026 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8027 8028 // FIXME: Handle embedded spaces in one iteration. 8029 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8030 // "_T(\"aaaaaaaaaaaaa\")\n" 8031 // "_T(\"aaaaaaaaaaaaa\")\n" 8032 // "_T(\"a\")", 8033 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8034 // getLLVMStyleWithColumns(20))); 8035 EXPECT_EQ( 8036 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8037 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8038 EXPECT_EQ("f(\n" 8039 "#if !TEST\n" 8040 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8041 "#endif\n" 8042 " );", 8043 format("f(\n" 8044 "#if !TEST\n" 8045 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8046 "#endif\n" 8047 ");")); 8048 EXPECT_EQ("f(\n" 8049 "\n" 8050 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8051 format("f(\n" 8052 "\n" 8053 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8054 } 8055 8056 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8057 EXPECT_EQ( 8058 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8061 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8064 } 8065 8066 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8067 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8068 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8069 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8070 "multiline raw string literal xxxxxxxxxxxxxx\n" 8071 ")x\",\n" 8072 " a),\n" 8073 " b);", 8074 format("fffffffffff(g(R\"x(\n" 8075 "multiline raw string literal xxxxxxxxxxxxxx\n" 8076 ")x\", a), b);", 8077 getGoogleStyleWithColumns(20))); 8078 EXPECT_EQ("fffffffffff(\n" 8079 " g(R\"x(qqq\n" 8080 "multiline raw string literal xxxxxxxxxxxxxx\n" 8081 ")x\",\n" 8082 " a),\n" 8083 " b);", 8084 format("fffffffffff(g(R\"x(qqq\n" 8085 "multiline raw string literal xxxxxxxxxxxxxx\n" 8086 ")x\", a), b);", 8087 getGoogleStyleWithColumns(20))); 8088 8089 EXPECT_EQ("fffffffffff(R\"x(\n" 8090 "multiline raw string literal xxxxxxxxxxxxxx\n" 8091 ")x\");", 8092 format("fffffffffff(R\"x(\n" 8093 "multiline raw string literal xxxxxxxxxxxxxx\n" 8094 ")x\");", 8095 getGoogleStyleWithColumns(20))); 8096 EXPECT_EQ("fffffffffff(R\"x(\n" 8097 "multiline raw string literal xxxxxxxxxxxxxx\n" 8098 ")x\" + bbbbbb);", 8099 format("fffffffffff(R\"x(\n" 8100 "multiline raw string literal xxxxxxxxxxxxxx\n" 8101 ")x\" + bbbbbb);", 8102 getGoogleStyleWithColumns(20))); 8103 EXPECT_EQ("fffffffffff(\n" 8104 " R\"x(\n" 8105 "multiline raw string literal xxxxxxxxxxxxxx\n" 8106 ")x\" +\n" 8107 " bbbbbb);", 8108 format("fffffffffff(\n" 8109 " R\"x(\n" 8110 "multiline raw string literal xxxxxxxxxxxxxx\n" 8111 ")x\" + bbbbbb);", 8112 getGoogleStyleWithColumns(20))); 8113 } 8114 8115 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8116 verifyFormat("string a = \"unterminated;"); 8117 EXPECT_EQ("function(\"unterminated,\n" 8118 " OtherParameter);", 8119 format("function( \"unterminated,\n" 8120 " OtherParameter);")); 8121 } 8122 8123 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8124 FormatStyle Style = getLLVMStyle(); 8125 Style.Standard = FormatStyle::LS_Cpp03; 8126 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8127 format("#define x(_a) printf(\"foo\"_a);", Style)); 8128 } 8129 8130 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8131 8132 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8133 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8134 " \"ddeeefff\");", 8135 format("someFunction(\"aaabbbcccdddeeefff\");", 8136 getLLVMStyleWithColumns(25))); 8137 EXPECT_EQ("someFunction1234567890(\n" 8138 " \"aaabbbcccdddeeefff\");", 8139 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8140 getLLVMStyleWithColumns(26))); 8141 EXPECT_EQ("someFunction1234567890(\n" 8142 " \"aaabbbcccdddeeeff\"\n" 8143 " \"f\");", 8144 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8145 getLLVMStyleWithColumns(25))); 8146 EXPECT_EQ("someFunction1234567890(\n" 8147 " \"aaabbbcccdddeeeff\"\n" 8148 " \"f\");", 8149 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8150 getLLVMStyleWithColumns(24))); 8151 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8152 " \"ddde \"\n" 8153 " \"efff\");", 8154 format("someFunction(\"aaabbbcc ddde efff\");", 8155 getLLVMStyleWithColumns(25))); 8156 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8157 " \"ddeeefff\");", 8158 format("someFunction(\"aaabbbccc ddeeefff\");", 8159 getLLVMStyleWithColumns(25))); 8160 EXPECT_EQ("someFunction1234567890(\n" 8161 " \"aaabb \"\n" 8162 " \"cccdddeeefff\");", 8163 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8164 getLLVMStyleWithColumns(25))); 8165 EXPECT_EQ("#define A \\\n" 8166 " string s = \\\n" 8167 " \"123456789\" \\\n" 8168 " \"0\"; \\\n" 8169 " int i;", 8170 format("#define A string s = \"1234567890\"; int i;", 8171 getLLVMStyleWithColumns(20))); 8172 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8173 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8174 " \"dddeeeff\"\n" 8175 " \"f\");", 8176 format("someFunction(\"aaabbbcc dddeeefff\");", 8177 getLLVMStyleWithColumns(25))); 8178 } 8179 8180 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8181 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8182 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8183 EXPECT_EQ("\"test\"\n" 8184 "\"\\n\"", 8185 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8186 EXPECT_EQ("\"tes\\\\\"\n" 8187 "\"n\"", 8188 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8189 EXPECT_EQ("\"\\\\\\\\\"\n" 8190 "\"\\n\"", 8191 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8192 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8193 EXPECT_EQ("\"\\uff01\"\n" 8194 "\"test\"", 8195 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8196 EXPECT_EQ("\"\\Uff01ff02\"", 8197 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8198 EXPECT_EQ("\"\\x000000000001\"\n" 8199 "\"next\"", 8200 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8201 EXPECT_EQ("\"\\x000000000001next\"", 8202 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8203 EXPECT_EQ("\"\\x000000000001\"", 8204 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8205 EXPECT_EQ("\"test\"\n" 8206 "\"\\000000\"\n" 8207 "\"000001\"", 8208 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8209 EXPECT_EQ("\"test\\000\"\n" 8210 "\"00000000\"\n" 8211 "\"1\"", 8212 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8213 } 8214 8215 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8216 verifyFormat("void f() {\n" 8217 " return g() {}\n" 8218 " void h() {}"); 8219 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8220 "g();\n" 8221 "}"); 8222 } 8223 8224 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8225 verifyFormat( 8226 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8227 } 8228 8229 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8230 verifyFormat("class X {\n" 8231 " void f() {\n" 8232 " }\n" 8233 "};", 8234 getLLVMStyleWithColumns(12)); 8235 } 8236 8237 TEST_F(FormatTest, ConfigurableIndentWidth) { 8238 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8239 EightIndent.IndentWidth = 8; 8240 EightIndent.ContinuationIndentWidth = 8; 8241 verifyFormat("void f() {\n" 8242 " someFunction();\n" 8243 " if (true) {\n" 8244 " f();\n" 8245 " }\n" 8246 "}", 8247 EightIndent); 8248 verifyFormat("class X {\n" 8249 " void f() {\n" 8250 " }\n" 8251 "};", 8252 EightIndent); 8253 verifyFormat("int x[] = {\n" 8254 " call(),\n" 8255 " call()};", 8256 EightIndent); 8257 } 8258 8259 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8260 verifyFormat("double\n" 8261 "f();", 8262 getLLVMStyleWithColumns(8)); 8263 } 8264 8265 TEST_F(FormatTest, ConfigurableUseOfTab) { 8266 FormatStyle Tab = getLLVMStyleWithColumns(42); 8267 Tab.IndentWidth = 8; 8268 Tab.UseTab = FormatStyle::UT_Always; 8269 Tab.AlignEscapedNewlinesLeft = true; 8270 8271 EXPECT_EQ("if (aaaaaaaa && // q\n" 8272 " bb)\t\t// w\n" 8273 "\t;", 8274 format("if (aaaaaaaa &&// q\n" 8275 "bb)// w\n" 8276 ";", 8277 Tab)); 8278 EXPECT_EQ("if (aaa && bbb) // w\n" 8279 "\t;", 8280 format("if(aaa&&bbb)// w\n" 8281 ";", 8282 Tab)); 8283 8284 verifyFormat("class X {\n" 8285 "\tvoid f() {\n" 8286 "\t\tsomeFunction(parameter1,\n" 8287 "\t\t\t parameter2);\n" 8288 "\t}\n" 8289 "};", 8290 Tab); 8291 verifyFormat("#define A \\\n" 8292 "\tvoid f() { \\\n" 8293 "\t\tsomeFunction( \\\n" 8294 "\t\t parameter1, \\\n" 8295 "\t\t parameter2); \\\n" 8296 "\t}", 8297 Tab); 8298 8299 Tab.TabWidth = 4; 8300 Tab.IndentWidth = 8; 8301 verifyFormat("class TabWidth4Indent8 {\n" 8302 "\t\tvoid f() {\n" 8303 "\t\t\t\tsomeFunction(parameter1,\n" 8304 "\t\t\t\t\t\t\t parameter2);\n" 8305 "\t\t}\n" 8306 "};", 8307 Tab); 8308 8309 Tab.TabWidth = 4; 8310 Tab.IndentWidth = 4; 8311 verifyFormat("class TabWidth4Indent4 {\n" 8312 "\tvoid f() {\n" 8313 "\t\tsomeFunction(parameter1,\n" 8314 "\t\t\t\t\t parameter2);\n" 8315 "\t}\n" 8316 "};", 8317 Tab); 8318 8319 Tab.TabWidth = 8; 8320 Tab.IndentWidth = 4; 8321 verifyFormat("class TabWidth8Indent4 {\n" 8322 " void f() {\n" 8323 "\tsomeFunction(parameter1,\n" 8324 "\t\t parameter2);\n" 8325 " }\n" 8326 "};", 8327 Tab); 8328 8329 Tab.TabWidth = 8; 8330 Tab.IndentWidth = 8; 8331 EXPECT_EQ("/*\n" 8332 "\t a\t\tcomment\n" 8333 "\t in multiple lines\n" 8334 " */", 8335 format(" /*\t \t \n" 8336 " \t \t a\t\tcomment\t \t\n" 8337 " \t \t in multiple lines\t\n" 8338 " \t */", 8339 Tab)); 8340 8341 Tab.UseTab = FormatStyle::UT_ForIndentation; 8342 verifyFormat("{\n" 8343 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8344 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8345 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8346 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8347 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8348 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8349 "};", 8350 Tab); 8351 verifyFormat("enum AA {\n" 8352 "\ta1, // Force multiple lines\n" 8353 "\ta2,\n" 8354 "\ta3\n" 8355 "};", 8356 Tab); 8357 EXPECT_EQ("if (aaaaaaaa && // q\n" 8358 " bb) // w\n" 8359 "\t;", 8360 format("if (aaaaaaaa &&// q\n" 8361 "bb)// w\n" 8362 ";", 8363 Tab)); 8364 verifyFormat("class X {\n" 8365 "\tvoid f() {\n" 8366 "\t\tsomeFunction(parameter1,\n" 8367 "\t\t parameter2);\n" 8368 "\t}\n" 8369 "};", 8370 Tab); 8371 verifyFormat("{\n" 8372 "\tQ(\n" 8373 "\t {\n" 8374 "\t\t int a;\n" 8375 "\t\t someFunction(aaaaaaaa,\n" 8376 "\t\t bbbbbbb);\n" 8377 "\t },\n" 8378 "\t p);\n" 8379 "}", 8380 Tab); 8381 EXPECT_EQ("{\n" 8382 "\t/* aaaa\n" 8383 "\t bbbb */\n" 8384 "}", 8385 format("{\n" 8386 "/* aaaa\n" 8387 " bbbb */\n" 8388 "}", 8389 Tab)); 8390 EXPECT_EQ("{\n" 8391 "\t/*\n" 8392 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8393 "\t bbbbbbbbbbbbb\n" 8394 "\t*/\n" 8395 "}", 8396 format("{\n" 8397 "/*\n" 8398 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8399 "*/\n" 8400 "}", 8401 Tab)); 8402 EXPECT_EQ("{\n" 8403 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8404 "\t// bbbbbbbbbbbbb\n" 8405 "}", 8406 format("{\n" 8407 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8408 "}", 8409 Tab)); 8410 EXPECT_EQ("{\n" 8411 "\t/*\n" 8412 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8413 "\t bbbbbbbbbbbbb\n" 8414 "\t*/\n" 8415 "}", 8416 format("{\n" 8417 "\t/*\n" 8418 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8419 "\t*/\n" 8420 "}", 8421 Tab)); 8422 EXPECT_EQ("{\n" 8423 "\t/*\n" 8424 "\n" 8425 "\t*/\n" 8426 "}", 8427 format("{\n" 8428 "\t/*\n" 8429 "\n" 8430 "\t*/\n" 8431 "}", 8432 Tab)); 8433 EXPECT_EQ("{\n" 8434 "\t/*\n" 8435 " asdf\n" 8436 "\t*/\n" 8437 "}", 8438 format("{\n" 8439 "\t/*\n" 8440 " asdf\n" 8441 "\t*/\n" 8442 "}", 8443 Tab)); 8444 8445 Tab.UseTab = FormatStyle::UT_Never; 8446 EXPECT_EQ("/*\n" 8447 " a\t\tcomment\n" 8448 " in multiple lines\n" 8449 " */", 8450 format(" /*\t \t \n" 8451 " \t \t a\t\tcomment\t \t\n" 8452 " \t \t in multiple lines\t\n" 8453 " \t */", 8454 Tab)); 8455 EXPECT_EQ("/* some\n" 8456 " comment */", 8457 format(" \t \t /* some\n" 8458 " \t \t comment */", 8459 Tab)); 8460 EXPECT_EQ("int a; /* some\n" 8461 " comment */", 8462 format(" \t \t int a; /* some\n" 8463 " \t \t comment */", 8464 Tab)); 8465 8466 EXPECT_EQ("int a; /* some\n" 8467 "comment */", 8468 format(" \t \t int\ta; /* some\n" 8469 " \t \t comment */", 8470 Tab)); 8471 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8472 " comment */", 8473 format(" \t \t f(\"\t\t\"); /* some\n" 8474 " \t \t comment */", 8475 Tab)); 8476 EXPECT_EQ("{\n" 8477 " /*\n" 8478 " * Comment\n" 8479 " */\n" 8480 " int i;\n" 8481 "}", 8482 format("{\n" 8483 "\t/*\n" 8484 "\t * Comment\n" 8485 "\t */\n" 8486 "\t int i;\n" 8487 "}")); 8488 } 8489 8490 TEST_F(FormatTest, CalculatesOriginalColumn) { 8491 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8492 "q\"; /* some\n" 8493 " comment */", 8494 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8495 "q\"; /* some\n" 8496 " comment */", 8497 getLLVMStyle())); 8498 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8499 "/* some\n" 8500 " comment */", 8501 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8502 " /* some\n" 8503 " comment */", 8504 getLLVMStyle())); 8505 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8506 "qqq\n" 8507 "/* some\n" 8508 " comment */", 8509 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8510 "qqq\n" 8511 " /* some\n" 8512 " comment */", 8513 getLLVMStyle())); 8514 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8515 "wwww; /* some\n" 8516 " comment */", 8517 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8518 "wwww; /* some\n" 8519 " comment */", 8520 getLLVMStyle())); 8521 } 8522 8523 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8524 FormatStyle NoSpace = getLLVMStyle(); 8525 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8526 8527 verifyFormat("while(true)\n" 8528 " continue;", 8529 NoSpace); 8530 verifyFormat("for(;;)\n" 8531 " continue;", 8532 NoSpace); 8533 verifyFormat("if(true)\n" 8534 " f();\n" 8535 "else if(true)\n" 8536 " f();", 8537 NoSpace); 8538 verifyFormat("do {\n" 8539 " do_something();\n" 8540 "} while(something());", 8541 NoSpace); 8542 verifyFormat("switch(x) {\n" 8543 "default:\n" 8544 " break;\n" 8545 "}", 8546 NoSpace); 8547 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8548 verifyFormat("size_t x = sizeof(x);", NoSpace); 8549 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8550 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8551 verifyFormat("alignas(128) char a[128];", NoSpace); 8552 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8553 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8554 verifyFormat("int f() throw(Deprecated);", NoSpace); 8555 verifyFormat("typedef void (*cb)(int);", NoSpace); 8556 verifyFormat("T A::operator()();", NoSpace); 8557 verifyFormat("X A::operator++(T);", NoSpace); 8558 8559 FormatStyle Space = getLLVMStyle(); 8560 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8561 8562 verifyFormat("int f ();", Space); 8563 verifyFormat("void f (int a, T b) {\n" 8564 " while (true)\n" 8565 " continue;\n" 8566 "}", 8567 Space); 8568 verifyFormat("if (true)\n" 8569 " f ();\n" 8570 "else if (true)\n" 8571 " f ();", 8572 Space); 8573 verifyFormat("do {\n" 8574 " do_something ();\n" 8575 "} while (something ());", 8576 Space); 8577 verifyFormat("switch (x) {\n" 8578 "default:\n" 8579 " break;\n" 8580 "}", 8581 Space); 8582 verifyFormat("A::A () : a (1) {}", Space); 8583 verifyFormat("void f () __attribute__ ((asdf));", Space); 8584 verifyFormat("*(&a + 1);\n" 8585 "&((&a)[1]);\n" 8586 "a[(b + c) * d];\n" 8587 "(((a + 1) * 2) + 3) * 4;", 8588 Space); 8589 verifyFormat("#define A(x) x", Space); 8590 verifyFormat("#define A (x) x", Space); 8591 verifyFormat("#if defined(x)\n" 8592 "#endif", 8593 Space); 8594 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8595 verifyFormat("size_t x = sizeof (x);", Space); 8596 verifyFormat("auto f (int x) -> decltype (x);", Space); 8597 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8598 verifyFormat("alignas (128) char a[128];", Space); 8599 verifyFormat("size_t x = alignof (MyType);", Space); 8600 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8601 verifyFormat("int f () throw (Deprecated);", Space); 8602 verifyFormat("typedef void (*cb) (int);", Space); 8603 verifyFormat("T A::operator() ();", Space); 8604 verifyFormat("X A::operator++ (T);", Space); 8605 } 8606 8607 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8608 FormatStyle Spaces = getLLVMStyle(); 8609 8610 Spaces.SpacesInParentheses = true; 8611 verifyFormat("call( x, y, z );", Spaces); 8612 verifyFormat("call();", Spaces); 8613 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8614 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8615 Spaces); 8616 verifyFormat("while ( (bool)1 )\n" 8617 " continue;", 8618 Spaces); 8619 verifyFormat("for ( ;; )\n" 8620 " continue;", 8621 Spaces); 8622 verifyFormat("if ( true )\n" 8623 " f();\n" 8624 "else if ( true )\n" 8625 " f();", 8626 Spaces); 8627 verifyFormat("do {\n" 8628 " do_something( (int)i );\n" 8629 "} while ( something() );", 8630 Spaces); 8631 verifyFormat("switch ( x ) {\n" 8632 "default:\n" 8633 " break;\n" 8634 "}", 8635 Spaces); 8636 8637 Spaces.SpacesInParentheses = false; 8638 Spaces.SpacesInCStyleCastParentheses = true; 8639 verifyFormat("Type *A = ( Type * )P;", Spaces); 8640 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8641 verifyFormat("x = ( int32 )y;", Spaces); 8642 verifyFormat("int a = ( int )(2.0f);", Spaces); 8643 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8644 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8645 verifyFormat("#define x (( int )-1)", Spaces); 8646 8647 // Run the first set of tests again with: 8648 Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true; 8649 Spaces.SpacesInCStyleCastParentheses = true; 8650 verifyFormat("call(x, y, z);", Spaces); 8651 verifyFormat("call( );", Spaces); 8652 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8653 verifyFormat("while (( bool )1)\n" 8654 " continue;", 8655 Spaces); 8656 verifyFormat("for (;;)\n" 8657 " continue;", 8658 Spaces); 8659 verifyFormat("if (true)\n" 8660 " f( );\n" 8661 "else if (true)\n" 8662 " f( );", 8663 Spaces); 8664 verifyFormat("do {\n" 8665 " do_something(( int )i);\n" 8666 "} while (something( ));", 8667 Spaces); 8668 verifyFormat("switch (x) {\n" 8669 "default:\n" 8670 " break;\n" 8671 "}", 8672 Spaces); 8673 8674 // Run the first set of tests again with: 8675 Spaces.SpaceAfterCStyleCast = true; 8676 verifyFormat("call(x, y, z);", Spaces); 8677 verifyFormat("call( );", Spaces); 8678 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8679 verifyFormat("while (( bool ) 1)\n" 8680 " continue;", 8681 Spaces); 8682 verifyFormat("for (;;)\n" 8683 " continue;", 8684 Spaces); 8685 verifyFormat("if (true)\n" 8686 " f( );\n" 8687 "else if (true)\n" 8688 " f( );", 8689 Spaces); 8690 verifyFormat("do {\n" 8691 " do_something(( int ) i);\n" 8692 "} while (something( ));", 8693 Spaces); 8694 verifyFormat("switch (x) {\n" 8695 "default:\n" 8696 " break;\n" 8697 "}", 8698 Spaces); 8699 8700 // Run subset of tests again with: 8701 Spaces.SpacesInCStyleCastParentheses = false; 8702 Spaces.SpaceAfterCStyleCast = true; 8703 verifyFormat("while ((bool) 1)\n" 8704 " continue;", 8705 Spaces); 8706 verifyFormat("do {\n" 8707 " do_something((int) i);\n" 8708 "} while (something( ));", 8709 Spaces); 8710 } 8711 8712 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8713 verifyFormat("int a[5];"); 8714 verifyFormat("a[3] += 42;"); 8715 8716 FormatStyle Spaces = getLLVMStyle(); 8717 Spaces.SpacesInSquareBrackets = true; 8718 // Lambdas unchanged. 8719 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8720 verifyFormat("return [i, args...] {};", Spaces); 8721 8722 // Not lambdas. 8723 verifyFormat("int a[ 5 ];", Spaces); 8724 verifyFormat("a[ 3 ] += 42;", Spaces); 8725 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8726 verifyFormat("double &operator[](int i) { return 0; }\n" 8727 "int i;", 8728 Spaces); 8729 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8730 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8731 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8732 } 8733 8734 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8735 verifyFormat("int a = 5;"); 8736 verifyFormat("a += 42;"); 8737 verifyFormat("a or_eq 8;"); 8738 8739 FormatStyle Spaces = getLLVMStyle(); 8740 Spaces.SpaceBeforeAssignmentOperators = false; 8741 verifyFormat("int a= 5;", Spaces); 8742 verifyFormat("a+= 42;", Spaces); 8743 verifyFormat("a or_eq 8;", Spaces); 8744 } 8745 8746 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8747 FormatStyle Alignment = getLLVMStyle(); 8748 Alignment.AlignConsecutiveAssignments = false; 8749 verifyFormat("int a = 5;\n" 8750 "int oneTwoThree = 123;", 8751 Alignment); 8752 verifyFormat("int a = 5;\n" 8753 "int oneTwoThree = 123;", 8754 Alignment); 8755 8756 Alignment.AlignConsecutiveAssignments = true; 8757 verifyFormat("int a = 5;\n" 8758 "int oneTwoThree = 123;", 8759 Alignment); 8760 verifyFormat("int a = method();\n" 8761 "int oneTwoThree = 133;", 8762 Alignment); 8763 verifyFormat("a &= 5;\n" 8764 "bcd *= 5;\n" 8765 "ghtyf += 5;\n" 8766 "dvfvdb -= 5;\n" 8767 "a /= 5;\n" 8768 "vdsvsv %= 5;\n" 8769 "sfdbddfbdfbb ^= 5;\n" 8770 "dvsdsv |= 5;\n" 8771 "int dsvvdvsdvvv = 123;", 8772 Alignment); 8773 verifyFormat("int i = 1, j = 10;\n" 8774 "something = 2000;", 8775 Alignment); 8776 verifyFormat("something = 2000;\n" 8777 "int i = 1, j = 10;\n", 8778 Alignment); 8779 verifyFormat("something = 2000;\n" 8780 "another = 911;\n" 8781 "int i = 1, j = 10;\n" 8782 "oneMore = 1;\n" 8783 "i = 2;", 8784 Alignment); 8785 verifyFormat("int a = 5;\n" 8786 "int one = 1;\n" 8787 "method();\n" 8788 "int oneTwoThree = 123;\n" 8789 "int oneTwo = 12;", 8790 Alignment); 8791 verifyFormat("int oneTwoThree = 123;\n" 8792 "int oneTwo = 12;\n" 8793 "method();\n", 8794 Alignment); 8795 verifyFormat("int oneTwoThree = 123; // comment\n" 8796 "int oneTwo = 12; // comment", 8797 Alignment); 8798 EXPECT_EQ("int a = 5;\n" 8799 "\n" 8800 "int oneTwoThree = 123;", 8801 format("int a = 5;\n" 8802 "\n" 8803 "int oneTwoThree= 123;", 8804 Alignment)); 8805 EXPECT_EQ("int a = 5;\n" 8806 "int one = 1;\n" 8807 "\n" 8808 "int oneTwoThree = 123;", 8809 format("int a = 5;\n" 8810 "int one = 1;\n" 8811 "\n" 8812 "int oneTwoThree = 123;", 8813 Alignment)); 8814 EXPECT_EQ("int a = 5;\n" 8815 "int one = 1;\n" 8816 "\n" 8817 "int oneTwoThree = 123;\n" 8818 "int oneTwo = 12;", 8819 format("int a = 5;\n" 8820 "int one = 1;\n" 8821 "\n" 8822 "int oneTwoThree = 123;\n" 8823 "int oneTwo = 12;", 8824 Alignment)); 8825 Alignment.AlignEscapedNewlinesLeft = true; 8826 verifyFormat("#define A \\\n" 8827 " int aaaa = 12; \\\n" 8828 " int b = 23; \\\n" 8829 " int ccc = 234; \\\n" 8830 " int dddddddddd = 2345;", 8831 Alignment); 8832 Alignment.AlignEscapedNewlinesLeft = false; 8833 verifyFormat("#define A " 8834 " \\\n" 8835 " int aaaa = 12; " 8836 " \\\n" 8837 " int b = 23; " 8838 " \\\n" 8839 " int ccc = 234; " 8840 " \\\n" 8841 " int dddddddddd = 2345;", 8842 Alignment); 8843 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8844 "k = 4, int l = 5,\n" 8845 " int m = 6) {\n" 8846 " int j = 10;\n" 8847 " otherThing = 1;\n" 8848 "}", 8849 Alignment); 8850 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8851 " int i = 1;\n" 8852 " int j = 2;\n" 8853 " int big = 10000;\n" 8854 "}", 8855 Alignment); 8856 verifyFormat("class C {\n" 8857 "public:\n" 8858 " int i = 1;\n" 8859 " virtual void f() = 0;\n" 8860 "};", 8861 Alignment); 8862 verifyFormat("int i = 1;\n" 8863 "if (SomeType t = getSomething()) {\n" 8864 "}\n" 8865 "int j = 2;\n" 8866 "int big = 10000;", 8867 Alignment); 8868 verifyFormat("int j = 7;\n" 8869 "for (int k = 0; k < N; ++k) {\n" 8870 "}\n" 8871 "int j = 2;\n" 8872 "int big = 10000;\n" 8873 "}", 8874 Alignment); 8875 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8876 verifyFormat("int i = 1;\n" 8877 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8878 " = someLooooooooooooooooongFunction();\n" 8879 "int j = 2;", 8880 Alignment); 8881 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8882 verifyFormat("int i = 1;\n" 8883 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8884 " someLooooooooooooooooongFunction();\n" 8885 "int j = 2;", 8886 Alignment); 8887 8888 verifyFormat("auto lambda = []() {\n" 8889 " auto i = 0;\n" 8890 " return 0;\n" 8891 "};\n" 8892 "int i = 0;\n" 8893 "auto v = type{\n" 8894 " i = 1, //\n" 8895 " (i = 2), //\n" 8896 " i = 3 //\n" 8897 "};", 8898 Alignment); 8899 8900 // FIXME: Should align all three assignments 8901 verifyFormat( 8902 "int i = 1;\n" 8903 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8904 " loooooooooooooooooooooongParameterB);\n" 8905 "int j = 2;", 8906 Alignment); 8907 8908 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8909 " typename B = very_long_type_name_1,\n" 8910 " typename T_2 = very_long_type_name_2>\n" 8911 "auto foo() {}\n", 8912 Alignment); 8913 verifyFormat("int a, b = 1;\n" 8914 "int c = 2;\n" 8915 "int dd = 3;\n", 8916 Alignment); 8917 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8918 "float b[1][] = {{3.f}};\n", 8919 Alignment); 8920 } 8921 8922 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8923 FormatStyle Alignment = getLLVMStyle(); 8924 Alignment.AlignConsecutiveDeclarations = false; 8925 verifyFormat("float const a = 5;\n" 8926 "int oneTwoThree = 123;", 8927 Alignment); 8928 verifyFormat("int a = 5;\n" 8929 "float const oneTwoThree = 123;", 8930 Alignment); 8931 8932 Alignment.AlignConsecutiveDeclarations = true; 8933 verifyFormat("float const a = 5;\n" 8934 "int oneTwoThree = 123;", 8935 Alignment); 8936 verifyFormat("int a = method();\n" 8937 "float const oneTwoThree = 133;", 8938 Alignment); 8939 verifyFormat("int i = 1, j = 10;\n" 8940 "something = 2000;", 8941 Alignment); 8942 verifyFormat("something = 2000;\n" 8943 "int i = 1, j = 10;\n", 8944 Alignment); 8945 verifyFormat("float something = 2000;\n" 8946 "double another = 911;\n" 8947 "int i = 1, j = 10;\n" 8948 "const int *oneMore = 1;\n" 8949 "unsigned i = 2;", 8950 Alignment); 8951 verifyFormat("float a = 5;\n" 8952 "int one = 1;\n" 8953 "method();\n" 8954 "const double oneTwoThree = 123;\n" 8955 "const unsigned int oneTwo = 12;", 8956 Alignment); 8957 verifyFormat("int oneTwoThree{0}; // comment\n" 8958 "unsigned oneTwo; // comment", 8959 Alignment); 8960 EXPECT_EQ("float const a = 5;\n" 8961 "\n" 8962 "int oneTwoThree = 123;", 8963 format("float const a = 5;\n" 8964 "\n" 8965 "int oneTwoThree= 123;", 8966 Alignment)); 8967 EXPECT_EQ("float a = 5;\n" 8968 "int one = 1;\n" 8969 "\n" 8970 "unsigned oneTwoThree = 123;", 8971 format("float a = 5;\n" 8972 "int one = 1;\n" 8973 "\n" 8974 "unsigned oneTwoThree = 123;", 8975 Alignment)); 8976 EXPECT_EQ("float a = 5;\n" 8977 "int one = 1;\n" 8978 "\n" 8979 "unsigned oneTwoThree = 123;\n" 8980 "int oneTwo = 12;", 8981 format("float a = 5;\n" 8982 "int one = 1;\n" 8983 "\n" 8984 "unsigned oneTwoThree = 123;\n" 8985 "int oneTwo = 12;", 8986 Alignment)); 8987 Alignment.AlignConsecutiveAssignments = true; 8988 verifyFormat("float something = 2000;\n" 8989 "double another = 911;\n" 8990 "int i = 1, j = 10;\n" 8991 "const int *oneMore = 1;\n" 8992 "unsigned i = 2;", 8993 Alignment); 8994 verifyFormat("int oneTwoThree = {0}; // comment\n" 8995 "unsigned oneTwo = 0; // comment", 8996 Alignment); 8997 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8998 " int const i = 1;\n" 8999 " int * j = 2;\n" 9000 " int big = 10000;\n" 9001 "\n" 9002 " unsigned oneTwoThree = 123;\n" 9003 " int oneTwo = 12;\n" 9004 " method();\n" 9005 " float k = 2;\n" 9006 " int ll = 10000;\n" 9007 "}", 9008 format("void SomeFunction(int parameter= 0) {\n" 9009 " int const i= 1;\n" 9010 " int *j=2;\n" 9011 " int big = 10000;\n" 9012 "\n" 9013 "unsigned oneTwoThree =123;\n" 9014 "int oneTwo = 12;\n" 9015 " method();\n" 9016 "float k= 2;\n" 9017 "int ll=10000;\n" 9018 "}", 9019 Alignment)); 9020 Alignment.AlignConsecutiveAssignments = false; 9021 Alignment.AlignEscapedNewlinesLeft = true; 9022 verifyFormat("#define A \\\n" 9023 " int aaaa = 12; \\\n" 9024 " float b = 23; \\\n" 9025 " const int ccc = 234; \\\n" 9026 " unsigned dddddddddd = 2345;", 9027 Alignment); 9028 Alignment.AlignEscapedNewlinesLeft = false; 9029 Alignment.ColumnLimit = 30; 9030 verifyFormat("#define A \\\n" 9031 " int aaaa = 12; \\\n" 9032 " float b = 23; \\\n" 9033 " const int ccc = 234; \\\n" 9034 " int dddddddddd = 2345;", 9035 Alignment); 9036 Alignment.ColumnLimit = 80; 9037 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9038 "k = 4, int l = 5,\n" 9039 " int m = 6) {\n" 9040 " const int j = 10;\n" 9041 " otherThing = 1;\n" 9042 "}", 9043 Alignment); 9044 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9045 " int const i = 1;\n" 9046 " int * j = 2;\n" 9047 " int big = 10000;\n" 9048 "}", 9049 Alignment); 9050 verifyFormat("class C {\n" 9051 "public:\n" 9052 " int i = 1;\n" 9053 " virtual void f() = 0;\n" 9054 "};", 9055 Alignment); 9056 verifyFormat("float i = 1;\n" 9057 "if (SomeType t = getSomething()) {\n" 9058 "}\n" 9059 "const unsigned j = 2;\n" 9060 "int big = 10000;", 9061 Alignment); 9062 verifyFormat("float j = 7;\n" 9063 "for (int k = 0; k < N; ++k) {\n" 9064 "}\n" 9065 "unsigned j = 2;\n" 9066 "int big = 10000;\n" 9067 "}", 9068 Alignment); 9069 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9070 verifyFormat("float i = 1;\n" 9071 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9072 " = someLooooooooooooooooongFunction();\n" 9073 "int j = 2;", 9074 Alignment); 9075 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9076 verifyFormat("int i = 1;\n" 9077 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9078 " someLooooooooooooooooongFunction();\n" 9079 "int j = 2;", 9080 Alignment); 9081 9082 Alignment.AlignConsecutiveAssignments = true; 9083 verifyFormat("auto lambda = []() {\n" 9084 " auto ii = 0;\n" 9085 " float j = 0;\n" 9086 " return 0;\n" 9087 "};\n" 9088 "int i = 0;\n" 9089 "float i2 = 0;\n" 9090 "auto v = type{\n" 9091 " i = 1, //\n" 9092 " (i = 2), //\n" 9093 " i = 3 //\n" 9094 "};", 9095 Alignment); 9096 Alignment.AlignConsecutiveAssignments = false; 9097 9098 // FIXME: Should align all three declarations 9099 verifyFormat( 9100 "int i = 1;\n" 9101 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9102 " loooooooooooooooooooooongParameterB);\n" 9103 "int j = 2;", 9104 Alignment); 9105 9106 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9107 // We expect declarations and assignments to align, as long as it doesn't 9108 // exceed the column limit, starting a new alignemnt sequence whenever it 9109 // happens. 9110 Alignment.AlignConsecutiveAssignments = true; 9111 Alignment.ColumnLimit = 30; 9112 verifyFormat("float ii = 1;\n" 9113 "unsigned j = 2;\n" 9114 "int someVerylongVariable = 1;\n" 9115 "AnotherLongType ll = 123456;\n" 9116 "VeryVeryLongType k = 2;\n" 9117 "int myvar = 1;", 9118 Alignment); 9119 Alignment.ColumnLimit = 80; 9120 Alignment.AlignConsecutiveAssignments = false; 9121 9122 verifyFormat( 9123 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9124 " typename LongType, typename B>\n" 9125 "auto foo() {}\n", 9126 Alignment); 9127 verifyFormat("float a, b = 1;\n" 9128 "int c = 2;\n" 9129 "int dd = 3;\n", 9130 Alignment); 9131 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9132 "float b[1][] = {{3.f}};\n", 9133 Alignment); 9134 Alignment.AlignConsecutiveAssignments = true; 9135 verifyFormat("float a, b = 1;\n" 9136 "int c = 2;\n" 9137 "int dd = 3;\n", 9138 Alignment); 9139 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9140 "float b[1][] = {{3.f}};\n", 9141 Alignment); 9142 Alignment.AlignConsecutiveAssignments = false; 9143 9144 Alignment.ColumnLimit = 30; 9145 Alignment.BinPackParameters = false; 9146 verifyFormat("void foo(float a,\n" 9147 " float b,\n" 9148 " int c,\n" 9149 " uint32_t *d) {\n" 9150 " int * e = 0;\n" 9151 " float f = 0;\n" 9152 " double g = 0;\n" 9153 "}\n" 9154 "void bar(ino_t a,\n" 9155 " int b,\n" 9156 " uint32_t *c,\n" 9157 " bool d) {}\n", 9158 Alignment); 9159 Alignment.BinPackParameters = true; 9160 Alignment.ColumnLimit = 80; 9161 } 9162 9163 TEST_F(FormatTest, LinuxBraceBreaking) { 9164 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9165 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9166 verifyFormat("namespace a\n" 9167 "{\n" 9168 "class A\n" 9169 "{\n" 9170 " void f()\n" 9171 " {\n" 9172 " if (true) {\n" 9173 " a();\n" 9174 " b();\n" 9175 " } else {\n" 9176 " a();\n" 9177 " }\n" 9178 " }\n" 9179 " void g() { return; }\n" 9180 "};\n" 9181 "struct B {\n" 9182 " int x;\n" 9183 "};\n" 9184 "}\n", 9185 LinuxBraceStyle); 9186 verifyFormat("enum X {\n" 9187 " Y = 0,\n" 9188 "}\n", 9189 LinuxBraceStyle); 9190 verifyFormat("struct S {\n" 9191 " int Type;\n" 9192 " union {\n" 9193 " int x;\n" 9194 " double y;\n" 9195 " } Value;\n" 9196 " class C\n" 9197 " {\n" 9198 " MyFavoriteType Value;\n" 9199 " } Class;\n" 9200 "}\n", 9201 LinuxBraceStyle); 9202 } 9203 9204 TEST_F(FormatTest, MozillaBraceBreaking) { 9205 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9206 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9207 verifyFormat("namespace a {\n" 9208 "class A\n" 9209 "{\n" 9210 " void f()\n" 9211 " {\n" 9212 " if (true) {\n" 9213 " a();\n" 9214 " b();\n" 9215 " }\n" 9216 " }\n" 9217 " void g() { return; }\n" 9218 "};\n" 9219 "enum E\n" 9220 "{\n" 9221 " A,\n" 9222 " // foo\n" 9223 " B,\n" 9224 " C\n" 9225 "};\n" 9226 "struct B\n" 9227 "{\n" 9228 " int x;\n" 9229 "};\n" 9230 "}\n", 9231 MozillaBraceStyle); 9232 verifyFormat("struct S\n" 9233 "{\n" 9234 " int Type;\n" 9235 " union\n" 9236 " {\n" 9237 " int x;\n" 9238 " double y;\n" 9239 " } Value;\n" 9240 " class C\n" 9241 " {\n" 9242 " MyFavoriteType Value;\n" 9243 " } Class;\n" 9244 "}\n", 9245 MozillaBraceStyle); 9246 } 9247 9248 TEST_F(FormatTest, StroustrupBraceBreaking) { 9249 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9250 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9251 verifyFormat("namespace a {\n" 9252 "class A {\n" 9253 " void f()\n" 9254 " {\n" 9255 " if (true) {\n" 9256 " a();\n" 9257 " b();\n" 9258 " }\n" 9259 " }\n" 9260 " void g() { return; }\n" 9261 "};\n" 9262 "struct B {\n" 9263 " int x;\n" 9264 "};\n" 9265 "}\n", 9266 StroustrupBraceStyle); 9267 9268 verifyFormat("void foo()\n" 9269 "{\n" 9270 " if (a) {\n" 9271 " a();\n" 9272 " }\n" 9273 " else {\n" 9274 " b();\n" 9275 " }\n" 9276 "}\n", 9277 StroustrupBraceStyle); 9278 9279 verifyFormat("#ifdef _DEBUG\n" 9280 "int foo(int i = 0)\n" 9281 "#else\n" 9282 "int foo(int i = 5)\n" 9283 "#endif\n" 9284 "{\n" 9285 " return i;\n" 9286 "}", 9287 StroustrupBraceStyle); 9288 9289 verifyFormat("void foo() {}\n" 9290 "void bar()\n" 9291 "#ifdef _DEBUG\n" 9292 "{\n" 9293 " foo();\n" 9294 "}\n" 9295 "#else\n" 9296 "{\n" 9297 "}\n" 9298 "#endif", 9299 StroustrupBraceStyle); 9300 9301 verifyFormat("void foobar() { int i = 5; }\n" 9302 "#ifdef _DEBUG\n" 9303 "void bar() {}\n" 9304 "#else\n" 9305 "void bar() { foobar(); }\n" 9306 "#endif", 9307 StroustrupBraceStyle); 9308 } 9309 9310 TEST_F(FormatTest, AllmanBraceBreaking) { 9311 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9312 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9313 verifyFormat("namespace a\n" 9314 "{\n" 9315 "class A\n" 9316 "{\n" 9317 " void f()\n" 9318 " {\n" 9319 " if (true)\n" 9320 " {\n" 9321 " a();\n" 9322 " b();\n" 9323 " }\n" 9324 " }\n" 9325 " void g() { return; }\n" 9326 "};\n" 9327 "struct B\n" 9328 "{\n" 9329 " int x;\n" 9330 "};\n" 9331 "}", 9332 AllmanBraceStyle); 9333 9334 verifyFormat("void f()\n" 9335 "{\n" 9336 " if (true)\n" 9337 " {\n" 9338 " a();\n" 9339 " }\n" 9340 " else if (false)\n" 9341 " {\n" 9342 " b();\n" 9343 " }\n" 9344 " else\n" 9345 " {\n" 9346 " c();\n" 9347 " }\n" 9348 "}\n", 9349 AllmanBraceStyle); 9350 9351 verifyFormat("void f()\n" 9352 "{\n" 9353 " for (int i = 0; i < 10; ++i)\n" 9354 " {\n" 9355 " a();\n" 9356 " }\n" 9357 " while (false)\n" 9358 " {\n" 9359 " b();\n" 9360 " }\n" 9361 " do\n" 9362 " {\n" 9363 " c();\n" 9364 " } while (false)\n" 9365 "}\n", 9366 AllmanBraceStyle); 9367 9368 verifyFormat("void f(int a)\n" 9369 "{\n" 9370 " switch (a)\n" 9371 " {\n" 9372 " case 0:\n" 9373 " break;\n" 9374 " case 1:\n" 9375 " {\n" 9376 " break;\n" 9377 " }\n" 9378 " case 2:\n" 9379 " {\n" 9380 " }\n" 9381 " break;\n" 9382 " default:\n" 9383 " break;\n" 9384 " }\n" 9385 "}\n", 9386 AllmanBraceStyle); 9387 9388 verifyFormat("enum X\n" 9389 "{\n" 9390 " Y = 0,\n" 9391 "}\n", 9392 AllmanBraceStyle); 9393 verifyFormat("enum X\n" 9394 "{\n" 9395 " Y = 0\n" 9396 "}\n", 9397 AllmanBraceStyle); 9398 9399 verifyFormat("@interface BSApplicationController ()\n" 9400 "{\n" 9401 "@private\n" 9402 " id _extraIvar;\n" 9403 "}\n" 9404 "@end\n", 9405 AllmanBraceStyle); 9406 9407 verifyFormat("#ifdef _DEBUG\n" 9408 "int foo(int i = 0)\n" 9409 "#else\n" 9410 "int foo(int i = 5)\n" 9411 "#endif\n" 9412 "{\n" 9413 " return i;\n" 9414 "}", 9415 AllmanBraceStyle); 9416 9417 verifyFormat("void foo() {}\n" 9418 "void bar()\n" 9419 "#ifdef _DEBUG\n" 9420 "{\n" 9421 " foo();\n" 9422 "}\n" 9423 "#else\n" 9424 "{\n" 9425 "}\n" 9426 "#endif", 9427 AllmanBraceStyle); 9428 9429 verifyFormat("void foobar() { int i = 5; }\n" 9430 "#ifdef _DEBUG\n" 9431 "void bar() {}\n" 9432 "#else\n" 9433 "void bar() { foobar(); }\n" 9434 "#endif", 9435 AllmanBraceStyle); 9436 9437 // This shouldn't affect ObjC blocks.. 9438 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9439 " // ...\n" 9440 " int i;\n" 9441 "}];", 9442 AllmanBraceStyle); 9443 verifyFormat("void (^block)(void) = ^{\n" 9444 " // ...\n" 9445 " int i;\n" 9446 "};", 9447 AllmanBraceStyle); 9448 // .. or dict literals. 9449 verifyFormat("void f()\n" 9450 "{\n" 9451 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9452 "}", 9453 AllmanBraceStyle); 9454 verifyFormat("int f()\n" 9455 "{ // comment\n" 9456 " return 42;\n" 9457 "}", 9458 AllmanBraceStyle); 9459 9460 AllmanBraceStyle.ColumnLimit = 19; 9461 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9462 AllmanBraceStyle.ColumnLimit = 18; 9463 verifyFormat("void f()\n" 9464 "{\n" 9465 " int i;\n" 9466 "}", 9467 AllmanBraceStyle); 9468 AllmanBraceStyle.ColumnLimit = 80; 9469 9470 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9471 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9472 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9473 verifyFormat("void f(bool b)\n" 9474 "{\n" 9475 " if (b)\n" 9476 " {\n" 9477 " return;\n" 9478 " }\n" 9479 "}\n", 9480 BreakBeforeBraceShortIfs); 9481 verifyFormat("void f(bool b)\n" 9482 "{\n" 9483 " if (b) return;\n" 9484 "}\n", 9485 BreakBeforeBraceShortIfs); 9486 verifyFormat("void f(bool b)\n" 9487 "{\n" 9488 " while (b)\n" 9489 " {\n" 9490 " return;\n" 9491 " }\n" 9492 "}\n", 9493 BreakBeforeBraceShortIfs); 9494 } 9495 9496 TEST_F(FormatTest, GNUBraceBreaking) { 9497 FormatStyle GNUBraceStyle = getLLVMStyle(); 9498 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9499 verifyFormat("namespace a\n" 9500 "{\n" 9501 "class A\n" 9502 "{\n" 9503 " void f()\n" 9504 " {\n" 9505 " int a;\n" 9506 " {\n" 9507 " int b;\n" 9508 " }\n" 9509 " if (true)\n" 9510 " {\n" 9511 " a();\n" 9512 " b();\n" 9513 " }\n" 9514 " }\n" 9515 " void g() { return; }\n" 9516 "}\n" 9517 "}", 9518 GNUBraceStyle); 9519 9520 verifyFormat("void f()\n" 9521 "{\n" 9522 " if (true)\n" 9523 " {\n" 9524 " a();\n" 9525 " }\n" 9526 " else if (false)\n" 9527 " {\n" 9528 " b();\n" 9529 " }\n" 9530 " else\n" 9531 " {\n" 9532 " c();\n" 9533 " }\n" 9534 "}\n", 9535 GNUBraceStyle); 9536 9537 verifyFormat("void f()\n" 9538 "{\n" 9539 " for (int i = 0; i < 10; ++i)\n" 9540 " {\n" 9541 " a();\n" 9542 " }\n" 9543 " while (false)\n" 9544 " {\n" 9545 " b();\n" 9546 " }\n" 9547 " do\n" 9548 " {\n" 9549 " c();\n" 9550 " }\n" 9551 " while (false);\n" 9552 "}\n", 9553 GNUBraceStyle); 9554 9555 verifyFormat("void f(int a)\n" 9556 "{\n" 9557 " switch (a)\n" 9558 " {\n" 9559 " case 0:\n" 9560 " break;\n" 9561 " case 1:\n" 9562 " {\n" 9563 " break;\n" 9564 " }\n" 9565 " case 2:\n" 9566 " {\n" 9567 " }\n" 9568 " break;\n" 9569 " default:\n" 9570 " break;\n" 9571 " }\n" 9572 "}\n", 9573 GNUBraceStyle); 9574 9575 verifyFormat("enum X\n" 9576 "{\n" 9577 " Y = 0,\n" 9578 "}\n", 9579 GNUBraceStyle); 9580 9581 verifyFormat("@interface BSApplicationController ()\n" 9582 "{\n" 9583 "@private\n" 9584 " id _extraIvar;\n" 9585 "}\n" 9586 "@end\n", 9587 GNUBraceStyle); 9588 9589 verifyFormat("#ifdef _DEBUG\n" 9590 "int foo(int i = 0)\n" 9591 "#else\n" 9592 "int foo(int i = 5)\n" 9593 "#endif\n" 9594 "{\n" 9595 " return i;\n" 9596 "}", 9597 GNUBraceStyle); 9598 9599 verifyFormat("void foo() {}\n" 9600 "void bar()\n" 9601 "#ifdef _DEBUG\n" 9602 "{\n" 9603 " foo();\n" 9604 "}\n" 9605 "#else\n" 9606 "{\n" 9607 "}\n" 9608 "#endif", 9609 GNUBraceStyle); 9610 9611 verifyFormat("void foobar() { int i = 5; }\n" 9612 "#ifdef _DEBUG\n" 9613 "void bar() {}\n" 9614 "#else\n" 9615 "void bar() { foobar(); }\n" 9616 "#endif", 9617 GNUBraceStyle); 9618 } 9619 9620 TEST_F(FormatTest, WebKitBraceBreaking) { 9621 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9622 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9623 verifyFormat("namespace a {\n" 9624 "class A {\n" 9625 " void f()\n" 9626 " {\n" 9627 " if (true) {\n" 9628 " a();\n" 9629 " b();\n" 9630 " }\n" 9631 " }\n" 9632 " void g() { return; }\n" 9633 "};\n" 9634 "enum E {\n" 9635 " A,\n" 9636 " // foo\n" 9637 " B,\n" 9638 " C\n" 9639 "};\n" 9640 "struct B {\n" 9641 " int x;\n" 9642 "};\n" 9643 "}\n", 9644 WebKitBraceStyle); 9645 verifyFormat("struct S {\n" 9646 " int Type;\n" 9647 " union {\n" 9648 " int x;\n" 9649 " double y;\n" 9650 " } Value;\n" 9651 " class C {\n" 9652 " MyFavoriteType Value;\n" 9653 " } Class;\n" 9654 "};\n", 9655 WebKitBraceStyle); 9656 } 9657 9658 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9659 verifyFormat("void f() {\n" 9660 " try {\n" 9661 " } catch (const Exception &e) {\n" 9662 " }\n" 9663 "}\n", 9664 getLLVMStyle()); 9665 } 9666 9667 TEST_F(FormatTest, UnderstandsPragmas) { 9668 verifyFormat("#pragma omp reduction(| : var)"); 9669 verifyFormat("#pragma omp reduction(+ : var)"); 9670 9671 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9672 "(including parentheses).", 9673 format("#pragma mark Any non-hyphenated or hyphenated string " 9674 "(including parentheses).")); 9675 } 9676 9677 TEST_F(FormatTest, UnderstandPragmaOption) { 9678 verifyFormat("#pragma option -C -A"); 9679 9680 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9681 } 9682 9683 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9684 for (size_t i = 1; i < Styles.size(); ++i) \ 9685 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9686 << " differs from Style #0" 9687 9688 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9689 SmallVector<FormatStyle, 3> Styles; 9690 Styles.resize(3); 9691 9692 Styles[0] = getLLVMStyle(); 9693 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9694 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9695 EXPECT_ALL_STYLES_EQUAL(Styles); 9696 9697 Styles[0] = getGoogleStyle(); 9698 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9699 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9700 EXPECT_ALL_STYLES_EQUAL(Styles); 9701 9702 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9703 EXPECT_TRUE( 9704 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9705 EXPECT_TRUE( 9706 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9707 EXPECT_ALL_STYLES_EQUAL(Styles); 9708 9709 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9710 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9711 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9712 EXPECT_ALL_STYLES_EQUAL(Styles); 9713 9714 Styles[0] = getMozillaStyle(); 9715 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9716 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9717 EXPECT_ALL_STYLES_EQUAL(Styles); 9718 9719 Styles[0] = getWebKitStyle(); 9720 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9721 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9722 EXPECT_ALL_STYLES_EQUAL(Styles); 9723 9724 Styles[0] = getGNUStyle(); 9725 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9726 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9727 EXPECT_ALL_STYLES_EQUAL(Styles); 9728 9729 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9730 } 9731 9732 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9733 SmallVector<FormatStyle, 8> Styles; 9734 Styles.resize(2); 9735 9736 Styles[0] = getGoogleStyle(); 9737 Styles[1] = getLLVMStyle(); 9738 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9739 EXPECT_ALL_STYLES_EQUAL(Styles); 9740 9741 Styles.resize(5); 9742 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9743 Styles[1] = getLLVMStyle(); 9744 Styles[1].Language = FormatStyle::LK_JavaScript; 9745 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9746 9747 Styles[2] = getLLVMStyle(); 9748 Styles[2].Language = FormatStyle::LK_JavaScript; 9749 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9750 "BasedOnStyle: Google", 9751 &Styles[2]) 9752 .value()); 9753 9754 Styles[3] = getLLVMStyle(); 9755 Styles[3].Language = FormatStyle::LK_JavaScript; 9756 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9757 "Language: JavaScript", 9758 &Styles[3]) 9759 .value()); 9760 9761 Styles[4] = getLLVMStyle(); 9762 Styles[4].Language = FormatStyle::LK_JavaScript; 9763 EXPECT_EQ(0, parseConfiguration("---\n" 9764 "BasedOnStyle: LLVM\n" 9765 "IndentWidth: 123\n" 9766 "---\n" 9767 "BasedOnStyle: Google\n" 9768 "Language: JavaScript", 9769 &Styles[4]) 9770 .value()); 9771 EXPECT_ALL_STYLES_EQUAL(Styles); 9772 } 9773 9774 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9775 Style.FIELD = false; \ 9776 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9777 EXPECT_TRUE(Style.FIELD); \ 9778 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9779 EXPECT_FALSE(Style.FIELD); 9780 9781 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9782 9783 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9784 Style.STRUCT.FIELD = false; \ 9785 EXPECT_EQ(0, \ 9786 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9787 .value()); \ 9788 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9789 EXPECT_EQ(0, \ 9790 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9791 .value()); \ 9792 EXPECT_FALSE(Style.STRUCT.FIELD); 9793 9794 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9795 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9796 9797 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9798 EXPECT_NE(VALUE, Style.FIELD); \ 9799 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9800 EXPECT_EQ(VALUE, Style.FIELD) 9801 9802 TEST_F(FormatTest, ParsesConfigurationBools) { 9803 FormatStyle Style = {}; 9804 Style.Language = FormatStyle::LK_Cpp; 9805 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9806 CHECK_PARSE_BOOL(AlignOperands); 9807 CHECK_PARSE_BOOL(AlignTrailingComments); 9808 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9809 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9810 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9811 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9812 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9813 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9814 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9815 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9816 CHECK_PARSE_BOOL(BinPackArguments); 9817 CHECK_PARSE_BOOL(BinPackParameters); 9818 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9819 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9820 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9821 CHECK_PARSE_BOOL(DerivePointerAlignment); 9822 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9823 CHECK_PARSE_BOOL(DisableFormat); 9824 CHECK_PARSE_BOOL(IndentCaseLabels); 9825 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9826 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9827 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9828 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9829 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9830 CHECK_PARSE_BOOL(ReflowComments); 9831 CHECK_PARSE_BOOL(SortIncludes); 9832 CHECK_PARSE_BOOL(SpacesInParentheses); 9833 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9834 CHECK_PARSE_BOOL(SpacesInAngles); 9835 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9836 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9837 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9838 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9839 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9840 9841 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9842 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9843 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9844 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9845 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9846 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9847 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9848 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9849 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9850 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9851 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9852 } 9853 9854 #undef CHECK_PARSE_BOOL 9855 9856 TEST_F(FormatTest, ParsesConfiguration) { 9857 FormatStyle Style = {}; 9858 Style.Language = FormatStyle::LK_Cpp; 9859 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9860 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9861 ConstructorInitializerIndentWidth, 1234u); 9862 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9863 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9864 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9865 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9866 PenaltyBreakBeforeFirstCallParameter, 1234u); 9867 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9868 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9869 PenaltyReturnTypeOnItsOwnLine, 1234u); 9870 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9871 SpacesBeforeTrailingComments, 1234u); 9872 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9873 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9874 9875 Style.PointerAlignment = FormatStyle::PAS_Middle; 9876 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9877 FormatStyle::PAS_Left); 9878 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9879 FormatStyle::PAS_Right); 9880 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9881 FormatStyle::PAS_Middle); 9882 // For backward compatibility: 9883 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9884 FormatStyle::PAS_Left); 9885 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9886 FormatStyle::PAS_Right); 9887 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9888 FormatStyle::PAS_Middle); 9889 9890 Style.Standard = FormatStyle::LS_Auto; 9891 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9892 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9893 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9894 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9895 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9896 9897 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9898 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9899 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9900 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9901 FormatStyle::BOS_None); 9902 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9903 FormatStyle::BOS_All); 9904 // For backward compatibility: 9905 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9906 FormatStyle::BOS_None); 9907 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9908 FormatStyle::BOS_All); 9909 9910 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9911 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9912 FormatStyle::BAS_Align); 9913 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9914 FormatStyle::BAS_DontAlign); 9915 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9916 FormatStyle::BAS_AlwaysBreak); 9917 // For backward compatibility: 9918 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9919 FormatStyle::BAS_DontAlign); 9920 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9921 FormatStyle::BAS_Align); 9922 9923 Style.UseTab = FormatStyle::UT_ForIndentation; 9924 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9925 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9926 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9927 // For backward compatibility: 9928 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9929 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9930 9931 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9932 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9933 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9934 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9935 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9936 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9937 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9938 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9939 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9940 // For backward compatibility: 9941 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9942 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9943 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9944 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9945 9946 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9947 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9948 FormatStyle::SBPO_Never); 9949 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9950 FormatStyle::SBPO_Always); 9951 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9952 FormatStyle::SBPO_ControlStatements); 9953 // For backward compatibility: 9954 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9955 FormatStyle::SBPO_Never); 9956 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9957 FormatStyle::SBPO_ControlStatements); 9958 9959 Style.ColumnLimit = 123; 9960 FormatStyle BaseStyle = getLLVMStyle(); 9961 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9962 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9963 9964 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9965 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9966 FormatStyle::BS_Attach); 9967 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9968 FormatStyle::BS_Linux); 9969 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9970 FormatStyle::BS_Mozilla); 9971 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9972 FormatStyle::BS_Stroustrup); 9973 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9974 FormatStyle::BS_Allman); 9975 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9976 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9977 FormatStyle::BS_WebKit); 9978 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9979 FormatStyle::BS_Custom); 9980 9981 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9982 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9983 FormatStyle::RTBS_None); 9984 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9985 FormatStyle::RTBS_All); 9986 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9987 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9988 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9989 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9990 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9991 AlwaysBreakAfterReturnType, 9992 FormatStyle::RTBS_TopLevelDefinitions); 9993 9994 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9995 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9996 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9997 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9998 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9999 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10000 AlwaysBreakAfterDefinitionReturnType, 10001 FormatStyle::DRTBS_TopLevel); 10002 10003 Style.NamespaceIndentation = FormatStyle::NI_All; 10004 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10005 FormatStyle::NI_None); 10006 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10007 FormatStyle::NI_Inner); 10008 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10009 FormatStyle::NI_All); 10010 10011 // FIXME: This is required because parsing a configuration simply overwrites 10012 // the first N elements of the list instead of resetting it. 10013 Style.ForEachMacros.clear(); 10014 std::vector<std::string> BoostForeach; 10015 BoostForeach.push_back("BOOST_FOREACH"); 10016 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10017 std::vector<std::string> BoostAndQForeach; 10018 BoostAndQForeach.push_back("BOOST_FOREACH"); 10019 BoostAndQForeach.push_back("Q_FOREACH"); 10020 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10021 BoostAndQForeach); 10022 10023 Style.IncludeCategories.clear(); 10024 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10025 {".*", 1}}; 10026 CHECK_PARSE("IncludeCategories:\n" 10027 " - Regex: abc/.*\n" 10028 " Priority: 2\n" 10029 " - Regex: .*\n" 10030 " Priority: 1", 10031 IncludeCategories, ExpectedCategories); 10032 } 10033 10034 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10035 FormatStyle Style = {}; 10036 Style.Language = FormatStyle::LK_Cpp; 10037 CHECK_PARSE("Language: Cpp\n" 10038 "IndentWidth: 12", 10039 IndentWidth, 12u); 10040 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10041 "IndentWidth: 34", 10042 &Style), 10043 ParseError::Unsuitable); 10044 EXPECT_EQ(12u, Style.IndentWidth); 10045 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10046 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10047 10048 Style.Language = FormatStyle::LK_JavaScript; 10049 CHECK_PARSE("Language: JavaScript\n" 10050 "IndentWidth: 12", 10051 IndentWidth, 12u); 10052 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10053 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10054 "IndentWidth: 34", 10055 &Style), 10056 ParseError::Unsuitable); 10057 EXPECT_EQ(23u, Style.IndentWidth); 10058 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10059 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10060 10061 CHECK_PARSE("BasedOnStyle: LLVM\n" 10062 "IndentWidth: 67", 10063 IndentWidth, 67u); 10064 10065 CHECK_PARSE("---\n" 10066 "Language: JavaScript\n" 10067 "IndentWidth: 12\n" 10068 "---\n" 10069 "Language: Cpp\n" 10070 "IndentWidth: 34\n" 10071 "...\n", 10072 IndentWidth, 12u); 10073 10074 Style.Language = FormatStyle::LK_Cpp; 10075 CHECK_PARSE("---\n" 10076 "Language: JavaScript\n" 10077 "IndentWidth: 12\n" 10078 "---\n" 10079 "Language: Cpp\n" 10080 "IndentWidth: 34\n" 10081 "...\n", 10082 IndentWidth, 34u); 10083 CHECK_PARSE("---\n" 10084 "IndentWidth: 78\n" 10085 "---\n" 10086 "Language: JavaScript\n" 10087 "IndentWidth: 56\n" 10088 "...\n", 10089 IndentWidth, 78u); 10090 10091 Style.ColumnLimit = 123; 10092 Style.IndentWidth = 234; 10093 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10094 Style.TabWidth = 345; 10095 EXPECT_FALSE(parseConfiguration("---\n" 10096 "IndentWidth: 456\n" 10097 "BreakBeforeBraces: Allman\n" 10098 "---\n" 10099 "Language: JavaScript\n" 10100 "IndentWidth: 111\n" 10101 "TabWidth: 111\n" 10102 "---\n" 10103 "Language: Cpp\n" 10104 "BreakBeforeBraces: Stroustrup\n" 10105 "TabWidth: 789\n" 10106 "...\n", 10107 &Style)); 10108 EXPECT_EQ(123u, Style.ColumnLimit); 10109 EXPECT_EQ(456u, Style.IndentWidth); 10110 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10111 EXPECT_EQ(789u, Style.TabWidth); 10112 10113 EXPECT_EQ(parseConfiguration("---\n" 10114 "Language: JavaScript\n" 10115 "IndentWidth: 56\n" 10116 "---\n" 10117 "IndentWidth: 78\n" 10118 "...\n", 10119 &Style), 10120 ParseError::Error); 10121 EXPECT_EQ(parseConfiguration("---\n" 10122 "Language: JavaScript\n" 10123 "IndentWidth: 56\n" 10124 "---\n" 10125 "Language: JavaScript\n" 10126 "IndentWidth: 78\n" 10127 "...\n", 10128 &Style), 10129 ParseError::Error); 10130 10131 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10132 } 10133 10134 #undef CHECK_PARSE 10135 10136 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10137 FormatStyle Style = {}; 10138 Style.Language = FormatStyle::LK_JavaScript; 10139 Style.BreakBeforeTernaryOperators = true; 10140 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10141 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10142 10143 Style.BreakBeforeTernaryOperators = true; 10144 EXPECT_EQ(0, parseConfiguration("---\n" 10145 "BasedOnStyle: Google\n" 10146 "---\n" 10147 "Language: JavaScript\n" 10148 "IndentWidth: 76\n" 10149 "...\n", 10150 &Style) 10151 .value()); 10152 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10153 EXPECT_EQ(76u, Style.IndentWidth); 10154 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10155 } 10156 10157 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10158 FormatStyle Style = getLLVMStyle(); 10159 std::string YAML = configurationAsText(Style); 10160 FormatStyle ParsedStyle = {}; 10161 ParsedStyle.Language = FormatStyle::LK_Cpp; 10162 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10163 EXPECT_EQ(Style, ParsedStyle); 10164 } 10165 10166 TEST_F(FormatTest, WorksFor8bitEncodings) { 10167 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10168 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10169 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10170 "\"\xef\xee\xf0\xf3...\"", 10171 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10172 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10173 "\xef\xee\xf0\xf3...\"", 10174 getLLVMStyleWithColumns(12))); 10175 } 10176 10177 TEST_F(FormatTest, HandlesUTF8BOM) { 10178 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10179 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10180 format("\xef\xbb\xbf#include <iostream>")); 10181 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10182 format("\xef\xbb\xbf\n#include <iostream>")); 10183 } 10184 10185 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10186 #if !defined(_MSC_VER) 10187 10188 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10189 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10190 getLLVMStyleWithColumns(35)); 10191 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10192 getLLVMStyleWithColumns(31)); 10193 verifyFormat("// Однажды в студёную зимнюю пору...", 10194 getLLVMStyleWithColumns(36)); 10195 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10196 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10197 getLLVMStyleWithColumns(39)); 10198 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10199 getLLVMStyleWithColumns(35)); 10200 } 10201 10202 TEST_F(FormatTest, SplitsUTF8Strings) { 10203 // Non-printable characters' width is currently considered to be the length in 10204 // bytes in UTF8. The characters can be displayed in very different manner 10205 // (zero-width, single width with a substitution glyph, expanded to their code 10206 // (e.g. "<8d>"), so there's no single correct way to handle them. 10207 EXPECT_EQ("\"aaaaÄ\"\n" 10208 "\"\xc2\x8d\";", 10209 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10210 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10211 "\"\xc2\x8d\";", 10212 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10213 EXPECT_EQ("\"Однажды, в \"\n" 10214 "\"студёную \"\n" 10215 "\"зимнюю \"\n" 10216 "\"пору,\"", 10217 format("\"Однажды, в студёную зимнюю пору,\"", 10218 getLLVMStyleWithColumns(13))); 10219 EXPECT_EQ( 10220 "\"一 二 三 \"\n" 10221 "\"四 五六 \"\n" 10222 "\"七 八 九 \"\n" 10223 "\"十\"", 10224 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10225 EXPECT_EQ("\"一\t二 \"\n" 10226 "\"\t三 \"\n" 10227 "\"四 五\t六 \"\n" 10228 "\"\t七 \"\n" 10229 "\"八九十\tqq\"", 10230 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10231 getLLVMStyleWithColumns(11))); 10232 10233 // UTF8 character in an escape sequence. 10234 EXPECT_EQ("\"aaaaaa\"\n" 10235 "\"\\\xC2\x8D\"", 10236 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10237 } 10238 10239 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10240 EXPECT_EQ("const char *sssss =\n" 10241 " \"一二三四五六七八\\\n" 10242 " 九 十\";", 10243 format("const char *sssss = \"一二三四五六七八\\\n" 10244 " 九 十\";", 10245 getLLVMStyleWithColumns(30))); 10246 } 10247 10248 TEST_F(FormatTest, SplitsUTF8LineComments) { 10249 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10250 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10251 EXPECT_EQ("// Я из лесу\n" 10252 "// вышел; был\n" 10253 "// сильный\n" 10254 "// мороз.", 10255 format("// Я из лесу вышел; был сильный мороз.", 10256 getLLVMStyleWithColumns(13))); 10257 EXPECT_EQ("// 一二三\n" 10258 "// 四五六七\n" 10259 "// 八 九\n" 10260 "// 十", 10261 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10262 } 10263 10264 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10265 EXPECT_EQ("/* Гляжу,\n" 10266 " * поднимается\n" 10267 " * медленно в\n" 10268 " * гору\n" 10269 " * Лошадка,\n" 10270 " * везущая\n" 10271 " * хворосту\n" 10272 " * воз. */", 10273 format("/* Гляжу, поднимается медленно в гору\n" 10274 " * Лошадка, везущая хворосту воз. */", 10275 getLLVMStyleWithColumns(13))); 10276 EXPECT_EQ( 10277 "/* 一二三\n" 10278 " * 四五六七\n" 10279 " * 八 九\n" 10280 " * 十 */", 10281 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10282 EXPECT_EQ("/* \n" 10283 " * \n" 10284 " * - */", 10285 format("/* - */", getLLVMStyleWithColumns(12))); 10286 } 10287 10288 #endif // _MSC_VER 10289 10290 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10291 FormatStyle Style = getLLVMStyle(); 10292 10293 Style.ConstructorInitializerIndentWidth = 4; 10294 verifyFormat( 10295 "SomeClass::Constructor()\n" 10296 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10297 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10298 Style); 10299 10300 Style.ConstructorInitializerIndentWidth = 2; 10301 verifyFormat( 10302 "SomeClass::Constructor()\n" 10303 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10304 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10305 Style); 10306 10307 Style.ConstructorInitializerIndentWidth = 0; 10308 verifyFormat( 10309 "SomeClass::Constructor()\n" 10310 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10311 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10312 Style); 10313 } 10314 10315 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10316 FormatStyle Style = getLLVMStyle(); 10317 Style.BreakConstructorInitializersBeforeComma = true; 10318 Style.ConstructorInitializerIndentWidth = 4; 10319 verifyFormat("SomeClass::Constructor()\n" 10320 " : a(a)\n" 10321 " , b(b)\n" 10322 " , c(c) {}", 10323 Style); 10324 verifyFormat("SomeClass::Constructor()\n" 10325 " : a(a) {}", 10326 Style); 10327 10328 Style.ColumnLimit = 0; 10329 verifyFormat("SomeClass::Constructor()\n" 10330 " : a(a) {}", 10331 Style); 10332 verifyFormat("SomeClass::Constructor()\n" 10333 " : a(a)\n" 10334 " , b(b)\n" 10335 " , c(c) {}", 10336 Style); 10337 verifyFormat("SomeClass::Constructor()\n" 10338 " : a(a) {\n" 10339 " foo();\n" 10340 " bar();\n" 10341 "}", 10342 Style); 10343 10344 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10345 verifyFormat("SomeClass::Constructor()\n" 10346 " : a(a)\n" 10347 " , b(b)\n" 10348 " , c(c) {\n}", 10349 Style); 10350 verifyFormat("SomeClass::Constructor()\n" 10351 " : a(a) {\n}", 10352 Style); 10353 10354 Style.ColumnLimit = 80; 10355 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10356 Style.ConstructorInitializerIndentWidth = 2; 10357 verifyFormat("SomeClass::Constructor()\n" 10358 " : a(a)\n" 10359 " , b(b)\n" 10360 " , c(c) {}", 10361 Style); 10362 10363 Style.ConstructorInitializerIndentWidth = 0; 10364 verifyFormat("SomeClass::Constructor()\n" 10365 ": a(a)\n" 10366 ", b(b)\n" 10367 ", c(c) {}", 10368 Style); 10369 10370 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10371 Style.ConstructorInitializerIndentWidth = 4; 10372 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10373 verifyFormat( 10374 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10375 Style); 10376 verifyFormat( 10377 "SomeClass::Constructor()\n" 10378 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10379 Style); 10380 Style.ConstructorInitializerIndentWidth = 4; 10381 Style.ColumnLimit = 60; 10382 verifyFormat("SomeClass::Constructor()\n" 10383 " : aaaaaaaa(aaaaaaaa)\n" 10384 " , aaaaaaaa(aaaaaaaa)\n" 10385 " , aaaaaaaa(aaaaaaaa) {}", 10386 Style); 10387 } 10388 10389 TEST_F(FormatTest, Destructors) { 10390 verifyFormat("void F(int &i) { i.~int(); }"); 10391 verifyFormat("void F(int &i) { i->~int(); }"); 10392 } 10393 10394 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10395 FormatStyle Style = getWebKitStyle(); 10396 10397 // Don't indent in outer namespaces. 10398 verifyFormat("namespace outer {\n" 10399 "int i;\n" 10400 "namespace inner {\n" 10401 " int i;\n" 10402 "} // namespace inner\n" 10403 "} // namespace outer\n" 10404 "namespace other_outer {\n" 10405 "int i;\n" 10406 "}", 10407 Style); 10408 10409 // Don't indent case labels. 10410 verifyFormat("switch (variable) {\n" 10411 "case 1:\n" 10412 "case 2:\n" 10413 " doSomething();\n" 10414 " break;\n" 10415 "default:\n" 10416 " ++variable;\n" 10417 "}", 10418 Style); 10419 10420 // Wrap before binary operators. 10421 EXPECT_EQ("void f()\n" 10422 "{\n" 10423 " if (aaaaaaaaaaaaaaaa\n" 10424 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10425 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10426 " return;\n" 10427 "}", 10428 format("void f() {\n" 10429 "if (aaaaaaaaaaaaaaaa\n" 10430 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10431 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10432 "return;\n" 10433 "}", 10434 Style)); 10435 10436 // Allow functions on a single line. 10437 verifyFormat("void f() { return; }", Style); 10438 10439 // Constructor initializers are formatted one per line with the "," on the 10440 // new line. 10441 verifyFormat("Constructor()\n" 10442 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10443 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10444 " aaaaaaaaaaaaaa)\n" 10445 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10446 "{\n" 10447 "}", 10448 Style); 10449 verifyFormat("SomeClass::Constructor()\n" 10450 " : a(a)\n" 10451 "{\n" 10452 "}", 10453 Style); 10454 EXPECT_EQ("SomeClass::Constructor()\n" 10455 " : a(a)\n" 10456 "{\n" 10457 "}", 10458 format("SomeClass::Constructor():a(a){}", Style)); 10459 verifyFormat("SomeClass::Constructor()\n" 10460 " : a(a)\n" 10461 " , b(b)\n" 10462 " , c(c)\n" 10463 "{\n" 10464 "}", 10465 Style); 10466 verifyFormat("SomeClass::Constructor()\n" 10467 " : a(a)\n" 10468 "{\n" 10469 " foo();\n" 10470 " bar();\n" 10471 "}", 10472 Style); 10473 10474 // Access specifiers should be aligned left. 10475 verifyFormat("class C {\n" 10476 "public:\n" 10477 " int i;\n" 10478 "};", 10479 Style); 10480 10481 // Do not align comments. 10482 verifyFormat("int a; // Do not\n" 10483 "double b; // align comments.", 10484 Style); 10485 10486 // Do not align operands. 10487 EXPECT_EQ("ASSERT(aaaa\n" 10488 " || bbbb);", 10489 format("ASSERT ( aaaa\n||bbbb);", Style)); 10490 10491 // Accept input's line breaks. 10492 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10493 " || bbbbbbbbbbbbbbb) {\n" 10494 " i++;\n" 10495 "}", 10496 format("if (aaaaaaaaaaaaaaa\n" 10497 "|| bbbbbbbbbbbbbbb) { i++; }", 10498 Style)); 10499 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10500 " i++;\n" 10501 "}", 10502 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10503 10504 // Don't automatically break all macro definitions (llvm.org/PR17842). 10505 verifyFormat("#define aNumber 10", Style); 10506 // However, generally keep the line breaks that the user authored. 10507 EXPECT_EQ("#define aNumber \\\n" 10508 " 10", 10509 format("#define aNumber \\\n" 10510 " 10", 10511 Style)); 10512 10513 // Keep empty and one-element array literals on a single line. 10514 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10515 " copyItems:YES];", 10516 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10517 "copyItems:YES];", 10518 Style)); 10519 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10520 " copyItems:YES];", 10521 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10522 " copyItems:YES];", 10523 Style)); 10524 // FIXME: This does not seem right, there should be more indentation before 10525 // the array literal's entries. Nested blocks have the same problem. 10526 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10527 " @\"a\",\n" 10528 " @\"a\"\n" 10529 "]\n" 10530 " copyItems:YES];", 10531 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10532 " @\"a\",\n" 10533 " @\"a\"\n" 10534 " ]\n" 10535 " copyItems:YES];", 10536 Style)); 10537 EXPECT_EQ( 10538 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10539 " copyItems:YES];", 10540 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10541 " copyItems:YES];", 10542 Style)); 10543 10544 verifyFormat("[self.a b:c c:d];", Style); 10545 EXPECT_EQ("[self.a b:c\n" 10546 " c:d];", 10547 format("[self.a b:c\n" 10548 "c:d];", 10549 Style)); 10550 } 10551 10552 TEST_F(FormatTest, FormatsLambdas) { 10553 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10554 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10555 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10556 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10557 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10558 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10559 verifyFormat("void f() {\n" 10560 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10561 "}\n"); 10562 verifyFormat("void f() {\n" 10563 " other(x.begin(), //\n" 10564 " x.end(), //\n" 10565 " [&](int, int) { return 1; });\n" 10566 "}\n"); 10567 verifyFormat("SomeFunction([]() { // A cool function...\n" 10568 " return 43;\n" 10569 "});"); 10570 EXPECT_EQ("SomeFunction([]() {\n" 10571 "#define A a\n" 10572 " return 43;\n" 10573 "});", 10574 format("SomeFunction([](){\n" 10575 "#define A a\n" 10576 "return 43;\n" 10577 "});")); 10578 verifyFormat("void f() {\n" 10579 " SomeFunction([](decltype(x), A *a) {});\n" 10580 "}"); 10581 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10582 " [](const aaaaaaaaaa &a) { return a; });"); 10583 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10584 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10585 "});"); 10586 verifyFormat("Constructor()\n" 10587 " : Field([] { // comment\n" 10588 " int i;\n" 10589 " }) {}"); 10590 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10591 " return some_parameter.size();\n" 10592 "};"); 10593 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10594 " [](const string &s) { return s; };"); 10595 verifyFormat("int i = aaaaaa ? 1 //\n" 10596 " : [] {\n" 10597 " return 2; //\n" 10598 " }();"); 10599 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10600 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10601 " return x == 2; // force break\n" 10602 " });"); 10603 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10604 " int iiiiiiiiiiii) {\n" 10605 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10606 "});", 10607 getLLVMStyleWithColumns(60)); 10608 verifyFormat("SomeFunction({[&] {\n" 10609 " // comment\n" 10610 " },\n" 10611 " [&] {\n" 10612 " // comment\n" 10613 " }});"); 10614 verifyFormat("SomeFunction({[&] {\n" 10615 " // comment\n" 10616 "}});"); 10617 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10618 " [&]() { return true; },\n" 10619 " aaaaa aaaaaaaaa);"); 10620 10621 // Lambdas with return types. 10622 verifyFormat("int c = []() -> int { return 2; }();\n"); 10623 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10624 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10625 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10626 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10627 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10628 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10629 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10630 verifyFormat("[a, a]() -> a<1> {};"); 10631 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10632 " int j) -> int {\n" 10633 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10634 "};"); 10635 verifyFormat( 10636 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10637 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10638 " return aaaaaaaaaaaaaaaaa;\n" 10639 " });", 10640 getLLVMStyleWithColumns(70)); 10641 10642 // Multiple lambdas in the same parentheses change indentation rules. 10643 verifyFormat("SomeFunction(\n" 10644 " []() {\n" 10645 " int i = 42;\n" 10646 " return i;\n" 10647 " },\n" 10648 " []() {\n" 10649 " int j = 43;\n" 10650 " return j;\n" 10651 " });"); 10652 10653 // More complex introducers. 10654 verifyFormat("return [i, args...] {};"); 10655 10656 // Not lambdas. 10657 verifyFormat("constexpr char hello[]{\"hello\"};"); 10658 verifyFormat("double &operator[](int i) { return 0; }\n" 10659 "int i;"); 10660 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10661 verifyFormat("int i = a[a][a]->f();"); 10662 verifyFormat("int i = (*b)[a]->f();"); 10663 10664 // Other corner cases. 10665 verifyFormat("void f() {\n" 10666 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10667 " );\n" 10668 "}"); 10669 10670 // Lambdas created through weird macros. 10671 verifyFormat("void f() {\n" 10672 " MACRO((const AA &a) { return 1; });\n" 10673 " MACRO((AA &a) { return 1; });\n" 10674 "}"); 10675 10676 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10677 " doo_dah();\n" 10678 " doo_dah();\n" 10679 " })) {\n" 10680 "}"); 10681 verifyFormat("auto lambda = []() {\n" 10682 " int a = 2\n" 10683 "#if A\n" 10684 " + 2\n" 10685 "#endif\n" 10686 " ;\n" 10687 "};"); 10688 } 10689 10690 TEST_F(FormatTest, FormatsBlocks) { 10691 FormatStyle ShortBlocks = getLLVMStyle(); 10692 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10693 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10694 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10695 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10696 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10697 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10698 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10699 10700 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10701 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10702 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10703 10704 verifyFormat("[operation setCompletionBlock:^{\n" 10705 " [self onOperationDone];\n" 10706 "}];"); 10707 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10708 " [self onOperationDone];\n" 10709 "}]};"); 10710 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10711 " f();\n" 10712 "}];"); 10713 verifyFormat("int a = [operation block:^int(int *i) {\n" 10714 " return 1;\n" 10715 "}];"); 10716 verifyFormat("[myObject doSomethingWith:arg1\n" 10717 " aaa:^int(int *a) {\n" 10718 " return 1;\n" 10719 " }\n" 10720 " bbb:f(a * bbbbbbbb)];"); 10721 10722 verifyFormat("[operation setCompletionBlock:^{\n" 10723 " [self.delegate newDataAvailable];\n" 10724 "}];", 10725 getLLVMStyleWithColumns(60)); 10726 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10727 " NSString *path = [self sessionFilePath];\n" 10728 " if (path) {\n" 10729 " // ...\n" 10730 " }\n" 10731 "});"); 10732 verifyFormat("[[SessionService sharedService]\n" 10733 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10734 " if (window) {\n" 10735 " [self windowDidLoad:window];\n" 10736 " } else {\n" 10737 " [self errorLoadingWindow];\n" 10738 " }\n" 10739 " }];"); 10740 verifyFormat("void (^largeBlock)(void) = ^{\n" 10741 " // ...\n" 10742 "};\n", 10743 getLLVMStyleWithColumns(40)); 10744 verifyFormat("[[SessionService sharedService]\n" 10745 " loadWindowWithCompletionBlock: //\n" 10746 " ^(SessionWindow *window) {\n" 10747 " if (window) {\n" 10748 " [self windowDidLoad:window];\n" 10749 " } else {\n" 10750 " [self errorLoadingWindow];\n" 10751 " }\n" 10752 " }];", 10753 getLLVMStyleWithColumns(60)); 10754 verifyFormat("[myObject doSomethingWith:arg1\n" 10755 " firstBlock:^(Foo *a) {\n" 10756 " // ...\n" 10757 " int i;\n" 10758 " }\n" 10759 " secondBlock:^(Bar *b) {\n" 10760 " // ...\n" 10761 " int i;\n" 10762 " }\n" 10763 " thirdBlock:^Foo(Bar *b) {\n" 10764 " // ...\n" 10765 " int i;\n" 10766 " }];"); 10767 verifyFormat("[myObject doSomethingWith:arg1\n" 10768 " firstBlock:-1\n" 10769 " secondBlock:^(Bar *b) {\n" 10770 " // ...\n" 10771 " int i;\n" 10772 " }];"); 10773 10774 verifyFormat("f(^{\n" 10775 " @autoreleasepool {\n" 10776 " if (a) {\n" 10777 " g();\n" 10778 " }\n" 10779 " }\n" 10780 "});"); 10781 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10782 10783 FormatStyle FourIndent = getLLVMStyle(); 10784 FourIndent.ObjCBlockIndentWidth = 4; 10785 verifyFormat("[operation setCompletionBlock:^{\n" 10786 " [self onOperationDone];\n" 10787 "}];", 10788 FourIndent); 10789 } 10790 10791 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10792 FormatStyle ZeroColumn = getLLVMStyle(); 10793 ZeroColumn.ColumnLimit = 0; 10794 10795 verifyFormat("[[SessionService sharedService] " 10796 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10797 " if (window) {\n" 10798 " [self windowDidLoad:window];\n" 10799 " } else {\n" 10800 " [self errorLoadingWindow];\n" 10801 " }\n" 10802 "}];", 10803 ZeroColumn); 10804 EXPECT_EQ("[[SessionService sharedService]\n" 10805 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10806 " if (window) {\n" 10807 " [self windowDidLoad:window];\n" 10808 " } else {\n" 10809 " [self errorLoadingWindow];\n" 10810 " }\n" 10811 " }];", 10812 format("[[SessionService sharedService]\n" 10813 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10814 " if (window) {\n" 10815 " [self windowDidLoad:window];\n" 10816 " } else {\n" 10817 " [self errorLoadingWindow];\n" 10818 " }\n" 10819 "}];", 10820 ZeroColumn)); 10821 verifyFormat("[myObject doSomethingWith:arg1\n" 10822 " firstBlock:^(Foo *a) {\n" 10823 " // ...\n" 10824 " int i;\n" 10825 " }\n" 10826 " secondBlock:^(Bar *b) {\n" 10827 " // ...\n" 10828 " int i;\n" 10829 " }\n" 10830 " thirdBlock:^Foo(Bar *b) {\n" 10831 " // ...\n" 10832 " int i;\n" 10833 " }];", 10834 ZeroColumn); 10835 verifyFormat("f(^{\n" 10836 " @autoreleasepool {\n" 10837 " if (a) {\n" 10838 " g();\n" 10839 " }\n" 10840 " }\n" 10841 "});", 10842 ZeroColumn); 10843 verifyFormat("void (^largeBlock)(void) = ^{\n" 10844 " // ...\n" 10845 "};", 10846 ZeroColumn); 10847 10848 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10849 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10850 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10851 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10852 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10853 " int i;\n" 10854 "};", 10855 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10856 } 10857 10858 TEST_F(FormatTest, SupportsCRLF) { 10859 EXPECT_EQ("int a;\r\n" 10860 "int b;\r\n" 10861 "int c;\r\n", 10862 format("int a;\r\n" 10863 " int b;\r\n" 10864 " int c;\r\n", 10865 getLLVMStyle())); 10866 EXPECT_EQ("int a;\r\n" 10867 "int b;\r\n" 10868 "int c;\r\n", 10869 format("int a;\r\n" 10870 " int b;\n" 10871 " int c;\r\n", 10872 getLLVMStyle())); 10873 EXPECT_EQ("int a;\n" 10874 "int b;\n" 10875 "int c;\n", 10876 format("int a;\r\n" 10877 " int b;\n" 10878 " int c;\n", 10879 getLLVMStyle())); 10880 EXPECT_EQ("\"aaaaaaa \"\r\n" 10881 "\"bbbbbbb\";\r\n", 10882 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10883 EXPECT_EQ("#define A \\\r\n" 10884 " b; \\\r\n" 10885 " c; \\\r\n" 10886 " d;\r\n", 10887 format("#define A \\\r\n" 10888 " b; \\\r\n" 10889 " c; d; \r\n", 10890 getGoogleStyle())); 10891 10892 EXPECT_EQ("/*\r\n" 10893 "multi line block comments\r\n" 10894 "should not introduce\r\n" 10895 "an extra carriage return\r\n" 10896 "*/\r\n", 10897 format("/*\r\n" 10898 "multi line block comments\r\n" 10899 "should not introduce\r\n" 10900 "an extra carriage return\r\n" 10901 "*/\r\n")); 10902 } 10903 10904 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10905 verifyFormat("MY_CLASS(C) {\n" 10906 " int i;\n" 10907 " int j;\n" 10908 "};"); 10909 } 10910 10911 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10912 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10913 TwoIndent.ContinuationIndentWidth = 2; 10914 10915 EXPECT_EQ("int i =\n" 10916 " longFunction(\n" 10917 " arg);", 10918 format("int i = longFunction(arg);", TwoIndent)); 10919 10920 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10921 SixIndent.ContinuationIndentWidth = 6; 10922 10923 EXPECT_EQ("int i =\n" 10924 " longFunction(\n" 10925 " arg);", 10926 format("int i = longFunction(arg);", SixIndent)); 10927 } 10928 10929 TEST_F(FormatTest, SpacesInAngles) { 10930 FormatStyle Spaces = getLLVMStyle(); 10931 Spaces.SpacesInAngles = true; 10932 10933 verifyFormat("static_cast< int >(arg);", Spaces); 10934 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10935 verifyFormat("f< int, float >();", Spaces); 10936 verifyFormat("template <> g() {}", Spaces); 10937 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10938 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10939 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10940 Spaces); 10941 10942 Spaces.Standard = FormatStyle::LS_Cpp03; 10943 Spaces.SpacesInAngles = true; 10944 verifyFormat("A< A< int > >();", Spaces); 10945 10946 Spaces.SpacesInAngles = false; 10947 verifyFormat("A<A<int> >();", Spaces); 10948 10949 Spaces.Standard = FormatStyle::LS_Cpp11; 10950 Spaces.SpacesInAngles = true; 10951 verifyFormat("A< A< int > >();", Spaces); 10952 10953 Spaces.SpacesInAngles = false; 10954 verifyFormat("A<A<int>>();", Spaces); 10955 } 10956 10957 TEST_F(FormatTest, TripleAngleBrackets) { 10958 verifyFormat("f<<<1, 1>>>();"); 10959 verifyFormat("f<<<1, 1, 1, s>>>();"); 10960 verifyFormat("f<<<a, b, c, d>>>();"); 10961 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10962 verifyFormat("f<param><<<1, 1>>>();"); 10963 verifyFormat("f<1><<<1, 1>>>();"); 10964 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10965 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10966 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10967 } 10968 10969 TEST_F(FormatTest, MergeLessLessAtEnd) { 10970 verifyFormat("<<"); 10971 EXPECT_EQ("< < <", format("\\\n<<<")); 10972 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10973 "aaallvm::outs() <<"); 10974 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10975 "aaaallvm::outs()\n <<"); 10976 } 10977 10978 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10979 std::string code = "#if A\n" 10980 "#if B\n" 10981 "a.\n" 10982 "#endif\n" 10983 " a = 1;\n" 10984 "#else\n" 10985 "#endif\n" 10986 "#if C\n" 10987 "#else\n" 10988 "#endif\n"; 10989 EXPECT_EQ(code, format(code)); 10990 } 10991 10992 TEST_F(FormatTest, HandleConflictMarkers) { 10993 // Git/SVN conflict markers. 10994 EXPECT_EQ("int a;\n" 10995 "void f() {\n" 10996 " callme(some(parameter1,\n" 10997 "<<<<<<< text by the vcs\n" 10998 " parameter2),\n" 10999 "||||||| text by the vcs\n" 11000 " parameter2),\n" 11001 " parameter3,\n" 11002 "======= text by the vcs\n" 11003 " parameter2, parameter3),\n" 11004 ">>>>>>> text by the vcs\n" 11005 " otherparameter);\n", 11006 format("int a;\n" 11007 "void f() {\n" 11008 " callme(some(parameter1,\n" 11009 "<<<<<<< text by the vcs\n" 11010 " parameter2),\n" 11011 "||||||| text by the vcs\n" 11012 " parameter2),\n" 11013 " parameter3,\n" 11014 "======= text by the vcs\n" 11015 " parameter2,\n" 11016 " parameter3),\n" 11017 ">>>>>>> text by the vcs\n" 11018 " otherparameter);\n")); 11019 11020 // Perforce markers. 11021 EXPECT_EQ("void f() {\n" 11022 " function(\n" 11023 ">>>> text by the vcs\n" 11024 " parameter,\n" 11025 "==== text by the vcs\n" 11026 " parameter,\n" 11027 "==== text by the vcs\n" 11028 " parameter,\n" 11029 "<<<< text by the vcs\n" 11030 " parameter);\n", 11031 format("void f() {\n" 11032 " function(\n" 11033 ">>>> text by the vcs\n" 11034 " parameter,\n" 11035 "==== text by the vcs\n" 11036 " parameter,\n" 11037 "==== text by the vcs\n" 11038 " parameter,\n" 11039 "<<<< text by the vcs\n" 11040 " parameter);\n")); 11041 11042 EXPECT_EQ("<<<<<<<\n" 11043 "|||||||\n" 11044 "=======\n" 11045 ">>>>>>>", 11046 format("<<<<<<<\n" 11047 "|||||||\n" 11048 "=======\n" 11049 ">>>>>>>")); 11050 11051 EXPECT_EQ("<<<<<<<\n" 11052 "|||||||\n" 11053 "int i;\n" 11054 "=======\n" 11055 ">>>>>>>", 11056 format("<<<<<<<\n" 11057 "|||||||\n" 11058 "int i;\n" 11059 "=======\n" 11060 ">>>>>>>")); 11061 11062 // FIXME: Handle parsing of macros around conflict markers correctly: 11063 EXPECT_EQ("#define Macro \\\n" 11064 "<<<<<<<\n" 11065 "Something \\\n" 11066 "|||||||\n" 11067 "Else \\\n" 11068 "=======\n" 11069 "Other \\\n" 11070 ">>>>>>>\n" 11071 " End int i;\n", 11072 format("#define Macro \\\n" 11073 "<<<<<<<\n" 11074 " Something \\\n" 11075 "|||||||\n" 11076 " Else \\\n" 11077 "=======\n" 11078 " Other \\\n" 11079 ">>>>>>>\n" 11080 " End\n" 11081 "int i;\n")); 11082 } 11083 11084 TEST_F(FormatTest, DisableRegions) { 11085 EXPECT_EQ("int i;\n" 11086 "// clang-format off\n" 11087 " int j;\n" 11088 "// clang-format on\n" 11089 "int k;", 11090 format(" int i;\n" 11091 " // clang-format off\n" 11092 " int j;\n" 11093 " // clang-format on\n" 11094 " int k;")); 11095 EXPECT_EQ("int i;\n" 11096 "/* clang-format off */\n" 11097 " int j;\n" 11098 "/* clang-format on */\n" 11099 "int k;", 11100 format(" int i;\n" 11101 " /* clang-format off */\n" 11102 " int j;\n" 11103 " /* clang-format on */\n" 11104 " int k;")); 11105 } 11106 11107 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11108 format("? ) ="); 11109 verifyNoCrash("#define a\\\n /**/}"); 11110 } 11111 11112 TEST_F(FormatTest, FormatsTableGenCode) { 11113 FormatStyle Style = getLLVMStyle(); 11114 Style.Language = FormatStyle::LK_TableGen; 11115 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11116 } 11117 11118 } // end namespace 11119 } // end namespace format 11120 } // end namespace clang 11121