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 1026 verifyFormat("#define A \\\n" 1027 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1028 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1029 getLLVMStyleWithColumns(60)); 1030 verifyFormat( 1031 "#define A \\\n" 1032 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1033 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1034 getLLVMStyleWithColumns(61)); 1035 1036 verifyFormat("if ( // This is some comment\n" 1037 " x + 3) {\n" 1038 "}"); 1039 EXPECT_EQ("if ( // This is some comment\n" 1040 " // spanning two lines\n" 1041 " x + 3) {\n" 1042 "}", 1043 format("if( // This is some comment\n" 1044 " // spanning two lines\n" 1045 " x + 3) {\n" 1046 "}")); 1047 1048 verifyNoCrash("/\\\n/"); 1049 verifyNoCrash("/\\\n* */"); 1050 // The 0-character somehow makes the lexer return a proper comment. 1051 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1052 } 1053 1054 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1055 EXPECT_EQ("SomeFunction(a,\n" 1056 " b, // comment\n" 1057 " c);", 1058 format("SomeFunction(a,\n" 1059 " b, // comment\n" 1060 " c);")); 1061 EXPECT_EQ("SomeFunction(a, b,\n" 1062 " // comment\n" 1063 " c);", 1064 format("SomeFunction(a,\n" 1065 " b,\n" 1066 " // comment\n" 1067 " c);")); 1068 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1069 " c);", 1070 format("SomeFunction(a, b, // comment (unclear relation)\n" 1071 " c);")); 1072 EXPECT_EQ("SomeFunction(a, // comment\n" 1073 " b,\n" 1074 " c); // comment", 1075 format("SomeFunction(a, // comment\n" 1076 " b,\n" 1077 " c); // comment")); 1078 } 1079 1080 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1081 EXPECT_EQ("// comment", format("// comment ")); 1082 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1083 format("int aaaaaaa, bbbbbbb; // comment ", 1084 getLLVMStyleWithColumns(33))); 1085 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1086 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1087 } 1088 1089 TEST_F(FormatTest, UnderstandsBlockComments) { 1090 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1091 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1092 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1093 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1094 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1095 "/* Trailing comment for aa... */\n" 1096 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1097 EXPECT_EQ( 1098 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1099 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1100 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1101 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1102 EXPECT_EQ( 1103 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1104 " aaaaaaaaaaaaaaaaaa,\n" 1105 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1106 "}", 1107 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1108 " aaaaaaaaaaaaaaaaaa ,\n" 1109 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1110 "}")); 1111 1112 FormatStyle NoBinPacking = getLLVMStyle(); 1113 NoBinPacking.BinPackParameters = false; 1114 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1115 " /* parameter 2 */ aaaaaa,\n" 1116 " /* parameter 3 */ aaaaaa,\n" 1117 " /* parameter 4 */ aaaaaa);", 1118 NoBinPacking); 1119 1120 // Aligning block comments in macros. 1121 verifyGoogleFormat("#define A \\\n" 1122 " int i; /*a*/ \\\n" 1123 " int jjj; /*b*/"); 1124 } 1125 1126 TEST_F(FormatTest, AlignsBlockComments) { 1127 EXPECT_EQ("/*\n" 1128 " * Really multi-line\n" 1129 " * comment.\n" 1130 " */\n" 1131 "void f() {}", 1132 format(" /*\n" 1133 " * Really multi-line\n" 1134 " * comment.\n" 1135 " */\n" 1136 " void f() {}")); 1137 EXPECT_EQ("class C {\n" 1138 " /*\n" 1139 " * Another multi-line\n" 1140 " * comment.\n" 1141 " */\n" 1142 " void f() {}\n" 1143 "};", 1144 format("class C {\n" 1145 "/*\n" 1146 " * Another multi-line\n" 1147 " * comment.\n" 1148 " */\n" 1149 "void f() {}\n" 1150 "};")); 1151 EXPECT_EQ("/*\n" 1152 " 1. This is a comment with non-trivial formatting.\n" 1153 " 1.1. We have to indent/outdent all lines equally\n" 1154 " 1.1.1. to keep the formatting.\n" 1155 " */", 1156 format(" /*\n" 1157 " 1. This is a comment with non-trivial formatting.\n" 1158 " 1.1. We have to indent/outdent all lines equally\n" 1159 " 1.1.1. to keep the formatting.\n" 1160 " */")); 1161 EXPECT_EQ("/*\n" 1162 "Don't try to outdent if there's not enough indentation.\n" 1163 "*/", 1164 format(" /*\n" 1165 " Don't try to outdent if there's not enough indentation.\n" 1166 " */")); 1167 1168 EXPECT_EQ("int i; /* Comment with empty...\n" 1169 " *\n" 1170 " * line. */", 1171 format("int i; /* Comment with empty...\n" 1172 " *\n" 1173 " * line. */")); 1174 EXPECT_EQ("int foobar = 0; /* comment */\n" 1175 "int bar = 0; /* multiline\n" 1176 " comment 1 */\n" 1177 "int baz = 0; /* multiline\n" 1178 " comment 2 */\n" 1179 "int bzz = 0; /* multiline\n" 1180 " comment 3 */", 1181 format("int foobar = 0; /* comment */\n" 1182 "int bar = 0; /* multiline\n" 1183 " comment 1 */\n" 1184 "int baz = 0; /* multiline\n" 1185 " comment 2 */\n" 1186 "int bzz = 0; /* multiline\n" 1187 " comment 3 */")); 1188 EXPECT_EQ("int foobar = 0; /* comment */\n" 1189 "int bar = 0; /* multiline\n" 1190 " comment */\n" 1191 "int baz = 0; /* multiline\n" 1192 "comment */", 1193 format("int foobar = 0; /* comment */\n" 1194 "int bar = 0; /* multiline\n" 1195 "comment */\n" 1196 "int baz = 0; /* multiline\n" 1197 "comment */")); 1198 } 1199 1200 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1201 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1203 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1205 EXPECT_EQ( 1206 "void ffffffffffff(\n" 1207 " int aaaaaaaa, int bbbbbbbb,\n" 1208 " int cccccccccccc) { /*\n" 1209 " aaaaaaaaaa\n" 1210 " aaaaaaaaaaaaa\n" 1211 " bbbbbbbbbbbbbb\n" 1212 " bbbbbbbbbb\n" 1213 " */\n" 1214 "}", 1215 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1216 "{ /*\n" 1217 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1218 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1219 " */\n" 1220 "}", 1221 getLLVMStyleWithColumns(40))); 1222 } 1223 1224 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1225 EXPECT_EQ("void ffffffffff(\n" 1226 " int aaaaa /* test */);", 1227 format("void ffffffffff(int aaaaa /* test */);", 1228 getLLVMStyleWithColumns(35))); 1229 } 1230 1231 TEST_F(FormatTest, SplitsLongCxxComments) { 1232 EXPECT_EQ("// A comment that\n" 1233 "// doesn't fit on\n" 1234 "// one line", 1235 format("// A comment that doesn't fit on one line", 1236 getLLVMStyleWithColumns(20))); 1237 EXPECT_EQ("/// A comment that\n" 1238 "/// doesn't fit on\n" 1239 "/// one line", 1240 format("/// A comment that doesn't fit on one line", 1241 getLLVMStyleWithColumns(20))); 1242 EXPECT_EQ("//! A comment that\n" 1243 "//! doesn't fit on\n" 1244 "//! one line", 1245 format("//! A comment that doesn't fit on one line", 1246 getLLVMStyleWithColumns(20))); 1247 EXPECT_EQ("// a b c d\n" 1248 "// e f g\n" 1249 "// h i j k", 1250 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1251 EXPECT_EQ( 1252 "// a b c d\n" 1253 "// e f g\n" 1254 "// h i j k", 1255 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1256 EXPECT_EQ("if (true) // A comment that\n" 1257 " // doesn't fit on\n" 1258 " // one line", 1259 format("if (true) // A comment that doesn't fit on one line ", 1260 getLLVMStyleWithColumns(30))); 1261 EXPECT_EQ("// Don't_touch_leading_whitespace", 1262 format("// Don't_touch_leading_whitespace", 1263 getLLVMStyleWithColumns(20))); 1264 EXPECT_EQ("// Add leading\n" 1265 "// whitespace", 1266 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1267 EXPECT_EQ("/// Add leading\n" 1268 "/// whitespace", 1269 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1270 EXPECT_EQ("//! Add leading\n" 1271 "//! whitespace", 1272 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1273 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1274 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1275 "// limit", 1276 format("//Even if it makes the line exceed the column limit", 1277 getLLVMStyleWithColumns(51))); 1278 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1279 1280 EXPECT_EQ("// aa bb cc dd", 1281 format("// aa bb cc dd ", 1282 getLLVMStyleWithColumns(15))); 1283 1284 EXPECT_EQ("// A comment before\n" 1285 "// a macro\n" 1286 "// definition\n" 1287 "#define a b", 1288 format("// A comment before a macro definition\n" 1289 "#define a b", 1290 getLLVMStyleWithColumns(20))); 1291 EXPECT_EQ("void ffffff(\n" 1292 " int aaaaaaaaa, // wwww\n" 1293 " int bbbbbbbbbb, // xxxxxxx\n" 1294 " // yyyyyyyyyy\n" 1295 " int c, int d, int e) {}", 1296 format("void ffffff(\n" 1297 " int aaaaaaaaa, // wwww\n" 1298 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1299 " int c, int d, int e) {}", 1300 getLLVMStyleWithColumns(40))); 1301 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1302 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1303 getLLVMStyleWithColumns(20))); 1304 EXPECT_EQ( 1305 "#define XXX // a b c d\n" 1306 " // e f g h", 1307 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1308 EXPECT_EQ( 1309 "#define XXX // q w e r\n" 1310 " // t y u i", 1311 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1312 } 1313 1314 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1315 EXPECT_EQ("// A comment\n" 1316 "// that doesn't\n" 1317 "// fit on one\n" 1318 "// line", 1319 format("// A comment that doesn't fit on one line", 1320 getLLVMStyleWithColumns(20))); 1321 EXPECT_EQ("/// A comment\n" 1322 "/// that doesn't\n" 1323 "/// fit on one\n" 1324 "/// line", 1325 format("/// A comment that doesn't fit on one line", 1326 getLLVMStyleWithColumns(20))); 1327 } 1328 1329 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1330 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1331 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1332 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1333 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1334 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1335 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1336 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1337 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1338 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1339 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1340 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1341 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1342 getLLVMStyleWithColumns(50))); 1343 // FIXME: One day we might want to implement adjustment of leading whitespace 1344 // of the consecutive lines in this kind of comment: 1345 EXPECT_EQ("double\n" 1346 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1347 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1348 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1349 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1350 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1351 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1352 getLLVMStyleWithColumns(49))); 1353 } 1354 1355 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1356 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1357 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1358 EXPECT_EQ( 1359 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1360 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1361 EXPECT_EQ( 1362 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1363 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1364 } 1365 1366 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1367 EXPECT_EQ("if (xxx ==\n" 1368 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1369 " zzz)\n" 1370 " q();", 1371 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1372 " zzz) q();", 1373 getLLVMStyleWithColumns(40))); 1374 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1375 " yyy && // aaaaaa bbbbbbbb cccc\n" 1376 " zzz)\n" 1377 " q();", 1378 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1379 " zzz) q();", 1380 getLLVMStyleWithColumns(40))); 1381 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1382 " yyy || // aaaaaa bbbbbbbb cccc\n" 1383 " zzz)\n" 1384 " q();", 1385 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1386 " zzz) q();", 1387 getLLVMStyleWithColumns(40))); 1388 EXPECT_EQ("fffffffff(\n" 1389 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1390 " zzz);", 1391 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1392 " zzz);", 1393 getLLVMStyleWithColumns(40))); 1394 } 1395 1396 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1397 EXPECT_EQ("#define A(x) /* \\\n" 1398 " a comment \\\n" 1399 " inside */ \\\n" 1400 " f();", 1401 format("#define A(x) /* \\\n" 1402 " a comment \\\n" 1403 " inside */ \\\n" 1404 " f();", 1405 getLLVMStyleWithColumns(17))); 1406 EXPECT_EQ("#define A( \\\n" 1407 " x) /* \\\n" 1408 " a comment \\\n" 1409 " inside */ \\\n" 1410 " f();", 1411 format("#define A( \\\n" 1412 " x) /* \\\n" 1413 " a comment \\\n" 1414 " inside */ \\\n" 1415 " f();", 1416 getLLVMStyleWithColumns(17))); 1417 } 1418 1419 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1420 EXPECT_EQ("namespace {}\n// Test\n#define A", 1421 format("namespace {}\n // Test\n#define A")); 1422 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1423 format("namespace {}\n /* Test */\n#define A")); 1424 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1425 format("namespace {}\n /* Test */ #define A")); 1426 } 1427 1428 TEST_F(FormatTest, SplitsLongLinesInComments) { 1429 EXPECT_EQ("/* This is a long\n" 1430 " * comment that\n" 1431 " * doesn't\n" 1432 " * fit on one line.\n" 1433 " */", 1434 format("/* " 1435 "This is a long " 1436 "comment that " 1437 "doesn't " 1438 "fit on one line. */", 1439 getLLVMStyleWithColumns(20))); 1440 EXPECT_EQ( 1441 "/* a b c d\n" 1442 " * e f g\n" 1443 " * h i j k\n" 1444 " */", 1445 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1446 EXPECT_EQ( 1447 "/* a b c d\n" 1448 " * e f g\n" 1449 " * h i j k\n" 1450 " */", 1451 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1452 EXPECT_EQ("/*\n" 1453 "This is a long\n" 1454 "comment that doesn't\n" 1455 "fit on one line.\n" 1456 "*/", 1457 format("/*\n" 1458 "This is a long " 1459 "comment that doesn't " 1460 "fit on one line. \n" 1461 "*/", 1462 getLLVMStyleWithColumns(20))); 1463 EXPECT_EQ("/*\n" 1464 " * This is a long\n" 1465 " * comment that\n" 1466 " * doesn't fit on\n" 1467 " * one line.\n" 1468 " */", 1469 format("/* \n" 1470 " * This is a long " 1471 " comment that " 1472 " doesn't fit on " 1473 " one line. \n" 1474 " */", 1475 getLLVMStyleWithColumns(20))); 1476 EXPECT_EQ("/*\n" 1477 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1478 " * so_it_should_be_broken\n" 1479 " * wherever_a_space_occurs\n" 1480 " */", 1481 format("/*\n" 1482 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1483 " so_it_should_be_broken " 1484 " wherever_a_space_occurs \n" 1485 " */", 1486 getLLVMStyleWithColumns(20))); 1487 EXPECT_EQ("/*\n" 1488 " * This_comment_can_not_be_broken_into_lines\n" 1489 " */", 1490 format("/*\n" 1491 " * This_comment_can_not_be_broken_into_lines\n" 1492 " */", 1493 getLLVMStyleWithColumns(20))); 1494 EXPECT_EQ("{\n" 1495 " /*\n" 1496 " This is another\n" 1497 " long comment that\n" 1498 " doesn't fit on one\n" 1499 " line 1234567890\n" 1500 " */\n" 1501 "}", 1502 format("{\n" 1503 "/*\n" 1504 "This is another " 1505 " long comment that " 1506 " doesn't fit on one" 1507 " line 1234567890\n" 1508 "*/\n" 1509 "}", 1510 getLLVMStyleWithColumns(20))); 1511 EXPECT_EQ("{\n" 1512 " /*\n" 1513 " * This i s\n" 1514 " * another comment\n" 1515 " * t hat doesn' t\n" 1516 " * fit on one l i\n" 1517 " * n e\n" 1518 " */\n" 1519 "}", 1520 format("{\n" 1521 "/*\n" 1522 " * This i s" 1523 " another comment" 1524 " t hat doesn' t" 1525 " fit on one l i" 1526 " n e\n" 1527 " */\n" 1528 "}", 1529 getLLVMStyleWithColumns(20))); 1530 EXPECT_EQ("/*\n" 1531 " * This is a long\n" 1532 " * comment that\n" 1533 " * doesn't fit on\n" 1534 " * one line\n" 1535 " */", 1536 format(" /*\n" 1537 " * This is a long comment that doesn't fit on one line\n" 1538 " */", 1539 getLLVMStyleWithColumns(20))); 1540 EXPECT_EQ("{\n" 1541 " if (something) /* This is a\n" 1542 " long\n" 1543 " comment */\n" 1544 " ;\n" 1545 "}", 1546 format("{\n" 1547 " if (something) /* This is a long comment */\n" 1548 " ;\n" 1549 "}", 1550 getLLVMStyleWithColumns(30))); 1551 1552 EXPECT_EQ("/* A comment before\n" 1553 " * a macro\n" 1554 " * definition */\n" 1555 "#define a b", 1556 format("/* A comment before a macro definition */\n" 1557 "#define a b", 1558 getLLVMStyleWithColumns(20))); 1559 1560 EXPECT_EQ("/* some comment\n" 1561 " * a comment\n" 1562 "* that we break\n" 1563 " * another comment\n" 1564 "* we have to break\n" 1565 "* a left comment\n" 1566 " */", 1567 format(" /* some comment\n" 1568 " * a comment that we break\n" 1569 " * another comment we have to break\n" 1570 "* a left comment\n" 1571 " */", 1572 getLLVMStyleWithColumns(20))); 1573 1574 EXPECT_EQ("/**\n" 1575 " * multiline block\n" 1576 " * comment\n" 1577 " *\n" 1578 " */", 1579 format("/**\n" 1580 " * multiline block comment\n" 1581 " *\n" 1582 " */", 1583 getLLVMStyleWithColumns(20))); 1584 1585 EXPECT_EQ("/*\n" 1586 "\n" 1587 "\n" 1588 " */\n", 1589 format(" /* \n" 1590 " \n" 1591 " \n" 1592 " */\n")); 1593 1594 EXPECT_EQ("/* a a */", 1595 format("/* a a */", getLLVMStyleWithColumns(15))); 1596 EXPECT_EQ("/* a a bc */", 1597 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1598 EXPECT_EQ("/* aaa aaa\n" 1599 " * aaaaa */", 1600 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1601 EXPECT_EQ("/* aaa aaa\n" 1602 " * aaaaa */", 1603 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1604 } 1605 1606 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1607 EXPECT_EQ("#define X \\\n" 1608 " /* \\\n" 1609 " Test \\\n" 1610 " Macro comment \\\n" 1611 " with a long \\\n" 1612 " line \\\n" 1613 " */ \\\n" 1614 " A + B", 1615 format("#define X \\\n" 1616 " /*\n" 1617 " Test\n" 1618 " Macro comment with a long line\n" 1619 " */ \\\n" 1620 " A + B", 1621 getLLVMStyleWithColumns(20))); 1622 EXPECT_EQ("#define X \\\n" 1623 " /* Macro comment \\\n" 1624 " with a long \\\n" 1625 " line */ \\\n" 1626 " A + B", 1627 format("#define X \\\n" 1628 " /* Macro comment with a long\n" 1629 " line */ \\\n" 1630 " A + B", 1631 getLLVMStyleWithColumns(20))); 1632 EXPECT_EQ("#define X \\\n" 1633 " /* Macro comment \\\n" 1634 " * with a long \\\n" 1635 " * line */ \\\n" 1636 " A + B", 1637 format("#define X \\\n" 1638 " /* Macro comment with a long line */ \\\n" 1639 " A + B", 1640 getLLVMStyleWithColumns(20))); 1641 } 1642 1643 TEST_F(FormatTest, CommentsInStaticInitializers) { 1644 EXPECT_EQ( 1645 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1646 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1647 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1648 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1649 " aaaaaaaaaaaaaaaaaaaa};", 1650 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1651 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1652 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1653 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1654 " aaaaaaaaaaaaaaaaaaaa };")); 1655 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1656 " bbbbbbbbbbb, ccccccccccc};"); 1657 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1658 " // comment for bb....\n" 1659 " bbbbbbbbbbb, ccccccccccc};"); 1660 verifyGoogleFormat( 1661 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1662 " bbbbbbbbbbb, ccccccccccc};"); 1663 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1664 " // comment for bb....\n" 1665 " bbbbbbbbbbb, ccccccccccc};"); 1666 1667 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1668 " {d, e, f}, // Group #2\n" 1669 " {g, h, i}}; // Group #3"); 1670 verifyFormat("S s = {{// Group #1\n" 1671 " a, b, c},\n" 1672 " {// Group #2\n" 1673 " d, e, f},\n" 1674 " {// Group #3\n" 1675 " g, h, i}};"); 1676 1677 EXPECT_EQ("S s = {\n" 1678 " // Some comment\n" 1679 " a,\n" 1680 "\n" 1681 " // Comment after empty line\n" 1682 " b}", 1683 format("S s = {\n" 1684 " // Some comment\n" 1685 " a,\n" 1686 " \n" 1687 " // Comment after empty line\n" 1688 " b\n" 1689 "}")); 1690 EXPECT_EQ("S s = {\n" 1691 " /* Some comment */\n" 1692 " a,\n" 1693 "\n" 1694 " /* Comment after empty line */\n" 1695 " b}", 1696 format("S s = {\n" 1697 " /* Some comment */\n" 1698 " a,\n" 1699 " \n" 1700 " /* Comment after empty line */\n" 1701 " b\n" 1702 "}")); 1703 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1704 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1705 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1706 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1707 } 1708 1709 TEST_F(FormatTest, IgnoresIf0Contents) { 1710 EXPECT_EQ("#if 0\n" 1711 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1712 "#endif\n" 1713 "void f() {}", 1714 format("#if 0\n" 1715 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1716 "#endif\n" 1717 "void f( ) { }")); 1718 EXPECT_EQ("#if false\n" 1719 "void f( ) { }\n" 1720 "#endif\n" 1721 "void g() {}\n", 1722 format("#if false\n" 1723 "void f( ) { }\n" 1724 "#endif\n" 1725 "void g( ) { }\n")); 1726 EXPECT_EQ("enum E {\n" 1727 " One,\n" 1728 " Two,\n" 1729 "#if 0\n" 1730 "Three,\n" 1731 " Four,\n" 1732 "#endif\n" 1733 " Five\n" 1734 "};", 1735 format("enum E {\n" 1736 " One,Two,\n" 1737 "#if 0\n" 1738 "Three,\n" 1739 " Four,\n" 1740 "#endif\n" 1741 " Five};")); 1742 EXPECT_EQ("enum F {\n" 1743 " One,\n" 1744 "#if 1\n" 1745 " Two,\n" 1746 "#if 0\n" 1747 "Three,\n" 1748 " Four,\n" 1749 "#endif\n" 1750 " Five\n" 1751 "#endif\n" 1752 "};", 1753 format("enum F {\n" 1754 "One,\n" 1755 "#if 1\n" 1756 "Two,\n" 1757 "#if 0\n" 1758 "Three,\n" 1759 " Four,\n" 1760 "#endif\n" 1761 "Five\n" 1762 "#endif\n" 1763 "};")); 1764 EXPECT_EQ("enum G {\n" 1765 " One,\n" 1766 "#if 0\n" 1767 "Two,\n" 1768 "#else\n" 1769 " Three,\n" 1770 "#endif\n" 1771 " Four\n" 1772 "};", 1773 format("enum G {\n" 1774 "One,\n" 1775 "#if 0\n" 1776 "Two,\n" 1777 "#else\n" 1778 "Three,\n" 1779 "#endif\n" 1780 "Four\n" 1781 "};")); 1782 EXPECT_EQ("enum H {\n" 1783 " One,\n" 1784 "#if 0\n" 1785 "#ifdef Q\n" 1786 "Two,\n" 1787 "#else\n" 1788 "Three,\n" 1789 "#endif\n" 1790 "#endif\n" 1791 " Four\n" 1792 "};", 1793 format("enum H {\n" 1794 "One,\n" 1795 "#if 0\n" 1796 "#ifdef Q\n" 1797 "Two,\n" 1798 "#else\n" 1799 "Three,\n" 1800 "#endif\n" 1801 "#endif\n" 1802 "Four\n" 1803 "};")); 1804 EXPECT_EQ("enum I {\n" 1805 " One,\n" 1806 "#if /* test */ 0 || 1\n" 1807 "Two,\n" 1808 "Three,\n" 1809 "#endif\n" 1810 " Four\n" 1811 "};", 1812 format("enum I {\n" 1813 "One,\n" 1814 "#if /* test */ 0 || 1\n" 1815 "Two,\n" 1816 "Three,\n" 1817 "#endif\n" 1818 "Four\n" 1819 "};")); 1820 EXPECT_EQ("enum J {\n" 1821 " One,\n" 1822 "#if 0\n" 1823 "#if 0\n" 1824 "Two,\n" 1825 "#else\n" 1826 "Three,\n" 1827 "#endif\n" 1828 "Four,\n" 1829 "#endif\n" 1830 " Five\n" 1831 "};", 1832 format("enum J {\n" 1833 "One,\n" 1834 "#if 0\n" 1835 "#if 0\n" 1836 "Two,\n" 1837 "#else\n" 1838 "Three,\n" 1839 "#endif\n" 1840 "Four,\n" 1841 "#endif\n" 1842 "Five\n" 1843 "};")); 1844 } 1845 1846 //===----------------------------------------------------------------------===// 1847 // Tests for classes, namespaces, etc. 1848 //===----------------------------------------------------------------------===// 1849 1850 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1851 verifyFormat("class A {};"); 1852 } 1853 1854 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1855 verifyFormat("class A {\n" 1856 "public:\n" 1857 "public: // comment\n" 1858 "protected:\n" 1859 "private:\n" 1860 " void f() {}\n" 1861 "};"); 1862 verifyGoogleFormat("class A {\n" 1863 " public:\n" 1864 " protected:\n" 1865 " private:\n" 1866 " void f() {}\n" 1867 "};"); 1868 verifyFormat("class A {\n" 1869 "public slots:\n" 1870 " void f() {}\n" 1871 "public Q_SLOTS:\n" 1872 " void f() {}\n" 1873 "signals:\n" 1874 " void g();\n" 1875 "};"); 1876 1877 // Don't interpret 'signals' the wrong way. 1878 verifyFormat("signals.set();"); 1879 verifyFormat("for (Signals signals : f()) {\n}"); 1880 verifyFormat("{\n" 1881 " signals.set(); // This needs indentation.\n" 1882 "}"); 1883 } 1884 1885 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1886 EXPECT_EQ("class A {\n" 1887 "public:\n" 1888 " void f();\n" 1889 "\n" 1890 "private:\n" 1891 " void g() {}\n" 1892 " // test\n" 1893 "protected:\n" 1894 " int h;\n" 1895 "};", 1896 format("class A {\n" 1897 "public:\n" 1898 "void f();\n" 1899 "private:\n" 1900 "void g() {}\n" 1901 "// test\n" 1902 "protected:\n" 1903 "int h;\n" 1904 "};")); 1905 EXPECT_EQ("class A {\n" 1906 "protected:\n" 1907 "public:\n" 1908 " void f();\n" 1909 "};", 1910 format("class A {\n" 1911 "protected:\n" 1912 "\n" 1913 "public:\n" 1914 "\n" 1915 " void f();\n" 1916 "};")); 1917 1918 // Even ensure proper spacing inside macros. 1919 EXPECT_EQ("#define B \\\n" 1920 " class A { \\\n" 1921 " protected: \\\n" 1922 " public: \\\n" 1923 " void f(); \\\n" 1924 " };", 1925 format("#define B \\\n" 1926 " class A { \\\n" 1927 " protected: \\\n" 1928 " \\\n" 1929 " public: \\\n" 1930 " \\\n" 1931 " void f(); \\\n" 1932 " };", 1933 getGoogleStyle())); 1934 // But don't remove empty lines after macros ending in access specifiers. 1935 EXPECT_EQ("#define A private:\n" 1936 "\n" 1937 "int i;", 1938 format("#define A private:\n" 1939 "\n" 1940 "int i;")); 1941 } 1942 1943 TEST_F(FormatTest, FormatsClasses) { 1944 verifyFormat("class A : public B {};"); 1945 verifyFormat("class A : public ::B {};"); 1946 1947 verifyFormat( 1948 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1949 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1950 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1951 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1952 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1953 verifyFormat( 1954 "class A : public B, public C, public D, public E, public F {};"); 1955 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1956 " public C,\n" 1957 " public D,\n" 1958 " public E,\n" 1959 " public F,\n" 1960 " public G {};"); 1961 1962 verifyFormat("class\n" 1963 " ReallyReallyLongClassName {\n" 1964 " int i;\n" 1965 "};", 1966 getLLVMStyleWithColumns(32)); 1967 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1968 " aaaaaaaaaaaaaaaa> {};"); 1969 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1970 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1971 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1972 verifyFormat("template <class R, class C>\n" 1973 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1974 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1975 verifyFormat("class ::A::B {};"); 1976 } 1977 1978 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1979 verifyFormat("class A {\n} a, b;"); 1980 verifyFormat("struct A {\n} a, b;"); 1981 verifyFormat("union A {\n} a;"); 1982 } 1983 1984 TEST_F(FormatTest, FormatsEnum) { 1985 verifyFormat("enum {\n" 1986 " Zero,\n" 1987 " One = 1,\n" 1988 " Two = One + 1,\n" 1989 " Three = (One + Two),\n" 1990 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1991 " Five = (One, Two, Three, Four, 5)\n" 1992 "};"); 1993 verifyGoogleFormat("enum {\n" 1994 " Zero,\n" 1995 " One = 1,\n" 1996 " Two = One + 1,\n" 1997 " Three = (One + Two),\n" 1998 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1999 " Five = (One, Two, Three, Four, 5)\n" 2000 "};"); 2001 verifyFormat("enum Enum {};"); 2002 verifyFormat("enum {};"); 2003 verifyFormat("enum X E {} d;"); 2004 verifyFormat("enum __attribute__((...)) E {} d;"); 2005 verifyFormat("enum __declspec__((...)) E {} d;"); 2006 verifyFormat("enum {\n" 2007 " Bar = Foo<int, int>::value\n" 2008 "};", 2009 getLLVMStyleWithColumns(30)); 2010 2011 verifyFormat("enum ShortEnum { A, B, C };"); 2012 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2013 2014 EXPECT_EQ("enum KeepEmptyLines {\n" 2015 " ONE,\n" 2016 "\n" 2017 " TWO,\n" 2018 "\n" 2019 " THREE\n" 2020 "}", 2021 format("enum KeepEmptyLines {\n" 2022 " ONE,\n" 2023 "\n" 2024 " TWO,\n" 2025 "\n" 2026 "\n" 2027 " THREE\n" 2028 "}")); 2029 verifyFormat("enum E { // comment\n" 2030 " ONE,\n" 2031 " TWO\n" 2032 "};\n" 2033 "int i;"); 2034 // Not enums. 2035 verifyFormat("enum X f() {\n" 2036 " a();\n" 2037 " return 42;\n" 2038 "}"); 2039 verifyFormat("enum X Type::f() {\n" 2040 " a();\n" 2041 " return 42;\n" 2042 "}"); 2043 verifyFormat("enum ::X f() {\n" 2044 " a();\n" 2045 " return 42;\n" 2046 "}"); 2047 verifyFormat("enum ns::X f() {\n" 2048 " a();\n" 2049 " return 42;\n" 2050 "}"); 2051 } 2052 2053 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2054 verifyFormat("enum Type {\n" 2055 " One = 0; // These semicolons should be commas.\n" 2056 " Two = 1;\n" 2057 "};"); 2058 verifyFormat("namespace n {\n" 2059 "enum Type {\n" 2060 " One,\n" 2061 " Two, // missing };\n" 2062 " int i;\n" 2063 "}\n" 2064 "void g() {}"); 2065 } 2066 2067 TEST_F(FormatTest, FormatsEnumStruct) { 2068 verifyFormat("enum struct {\n" 2069 " Zero,\n" 2070 " One = 1,\n" 2071 " Two = One + 1,\n" 2072 " Three = (One + Two),\n" 2073 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2074 " Five = (One, Two, Three, Four, 5)\n" 2075 "};"); 2076 verifyFormat("enum struct Enum {};"); 2077 verifyFormat("enum struct {};"); 2078 verifyFormat("enum struct X E {} d;"); 2079 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2080 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2081 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2082 } 2083 2084 TEST_F(FormatTest, FormatsEnumClass) { 2085 verifyFormat("enum class {\n" 2086 " Zero,\n" 2087 " One = 1,\n" 2088 " Two = One + 1,\n" 2089 " Three = (One + Two),\n" 2090 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2091 " Five = (One, Two, Three, Four, 5)\n" 2092 "};"); 2093 verifyFormat("enum class Enum {};"); 2094 verifyFormat("enum class {};"); 2095 verifyFormat("enum class X E {} d;"); 2096 verifyFormat("enum class __attribute__((...)) E {} d;"); 2097 verifyFormat("enum class __declspec__((...)) E {} d;"); 2098 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2099 } 2100 2101 TEST_F(FormatTest, FormatsEnumTypes) { 2102 verifyFormat("enum X : int {\n" 2103 " A, // Force multiple lines.\n" 2104 " B\n" 2105 "};"); 2106 verifyFormat("enum X : int { A, B };"); 2107 verifyFormat("enum X : std::uint32_t { A, B };"); 2108 } 2109 2110 TEST_F(FormatTest, FormatsNSEnums) { 2111 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2112 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2113 " // Information about someDecentlyLongValue.\n" 2114 " someDecentlyLongValue,\n" 2115 " // Information about anotherDecentlyLongValue.\n" 2116 " anotherDecentlyLongValue,\n" 2117 " // Information about aThirdDecentlyLongValue.\n" 2118 " aThirdDecentlyLongValue\n" 2119 "};"); 2120 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2121 " a = 1,\n" 2122 " b = 2,\n" 2123 " c = 3,\n" 2124 "};"); 2125 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2126 " a = 1,\n" 2127 " b = 2,\n" 2128 " c = 3,\n" 2129 "};"); 2130 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2131 " a = 1,\n" 2132 " b = 2,\n" 2133 " c = 3,\n" 2134 "};"); 2135 } 2136 2137 TEST_F(FormatTest, FormatsBitfields) { 2138 verifyFormat("struct Bitfields {\n" 2139 " unsigned sClass : 8;\n" 2140 " unsigned ValueKind : 2;\n" 2141 "};"); 2142 verifyFormat("struct A {\n" 2143 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2144 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2145 "};"); 2146 verifyFormat("struct MyStruct {\n" 2147 " uchar data;\n" 2148 " uchar : 8;\n" 2149 " uchar : 8;\n" 2150 " uchar other;\n" 2151 "};"); 2152 } 2153 2154 TEST_F(FormatTest, FormatsNamespaces) { 2155 verifyFormat("namespace some_namespace {\n" 2156 "class A {};\n" 2157 "void f() { f(); }\n" 2158 "}"); 2159 verifyFormat("namespace {\n" 2160 "class A {};\n" 2161 "void f() { f(); }\n" 2162 "}"); 2163 verifyFormat("inline namespace X {\n" 2164 "class A {};\n" 2165 "void f() { f(); }\n" 2166 "}"); 2167 verifyFormat("using namespace some_namespace;\n" 2168 "class A {};\n" 2169 "void f() { f(); }"); 2170 2171 // This code is more common than we thought; if we 2172 // layout this correctly the semicolon will go into 2173 // its own line, which is undesirable. 2174 verifyFormat("namespace {};"); 2175 verifyFormat("namespace {\n" 2176 "class A {};\n" 2177 "};"); 2178 2179 verifyFormat("namespace {\n" 2180 "int SomeVariable = 0; // comment\n" 2181 "} // namespace"); 2182 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2183 "#define HEADER_GUARD\n" 2184 "namespace my_namespace {\n" 2185 "int i;\n" 2186 "} // my_namespace\n" 2187 "#endif // HEADER_GUARD", 2188 format("#ifndef HEADER_GUARD\n" 2189 " #define HEADER_GUARD\n" 2190 " namespace my_namespace {\n" 2191 "int i;\n" 2192 "} // my_namespace\n" 2193 "#endif // HEADER_GUARD")); 2194 2195 EXPECT_EQ("namespace A::B {\n" 2196 "class C {};\n" 2197 "}", 2198 format("namespace A::B {\n" 2199 "class C {};\n" 2200 "}")); 2201 2202 FormatStyle Style = getLLVMStyle(); 2203 Style.NamespaceIndentation = FormatStyle::NI_All; 2204 EXPECT_EQ("namespace out {\n" 2205 " int i;\n" 2206 " namespace in {\n" 2207 " int i;\n" 2208 " } // namespace\n" 2209 "} // namespace", 2210 format("namespace out {\n" 2211 "int i;\n" 2212 "namespace in {\n" 2213 "int i;\n" 2214 "} // namespace\n" 2215 "} // namespace", 2216 Style)); 2217 2218 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2219 EXPECT_EQ("namespace out {\n" 2220 "int i;\n" 2221 "namespace in {\n" 2222 " int i;\n" 2223 "} // namespace\n" 2224 "} // namespace", 2225 format("namespace out {\n" 2226 "int i;\n" 2227 "namespace in {\n" 2228 "int i;\n" 2229 "} // namespace\n" 2230 "} // namespace", 2231 Style)); 2232 } 2233 2234 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2235 2236 TEST_F(FormatTest, FormatsInlineASM) { 2237 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2238 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2239 verifyFormat( 2240 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2241 " \"cpuid\\n\\t\"\n" 2242 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2243 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2244 " : \"a\"(value));"); 2245 EXPECT_EQ( 2246 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2247 " __asm {\n" 2248 " mov edx,[that] // vtable in edx\n" 2249 " mov eax,methodIndex\n" 2250 " call [edx][eax*4] // stdcall\n" 2251 " }\n" 2252 "}", 2253 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2254 " __asm {\n" 2255 " mov edx,[that] // vtable in edx\n" 2256 " mov eax,methodIndex\n" 2257 " call [edx][eax*4] // stdcall\n" 2258 " }\n" 2259 "}")); 2260 EXPECT_EQ("_asm {\n" 2261 " xor eax, eax;\n" 2262 " cpuid;\n" 2263 "}", 2264 format("_asm {\n" 2265 " xor eax, eax;\n" 2266 " cpuid;\n" 2267 "}")); 2268 verifyFormat("void function() {\n" 2269 " // comment\n" 2270 " asm(\"\");\n" 2271 "}"); 2272 EXPECT_EQ("__asm {\n" 2273 "}\n" 2274 "int i;", 2275 format("__asm {\n" 2276 "}\n" 2277 "int i;")); 2278 } 2279 2280 TEST_F(FormatTest, FormatTryCatch) { 2281 verifyFormat("try {\n" 2282 " throw a * b;\n" 2283 "} catch (int a) {\n" 2284 " // Do nothing.\n" 2285 "} catch (...) {\n" 2286 " exit(42);\n" 2287 "}"); 2288 2289 // Function-level try statements. 2290 verifyFormat("int f() try { return 4; } catch (...) {\n" 2291 " return 5;\n" 2292 "}"); 2293 verifyFormat("class A {\n" 2294 " int a;\n" 2295 " A() try : a(0) {\n" 2296 " } catch (...) {\n" 2297 " throw;\n" 2298 " }\n" 2299 "};\n"); 2300 2301 // Incomplete try-catch blocks. 2302 verifyIncompleteFormat("try {} catch ("); 2303 } 2304 2305 TEST_F(FormatTest, FormatSEHTryCatch) { 2306 verifyFormat("__try {\n" 2307 " int a = b * c;\n" 2308 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2309 " // Do nothing.\n" 2310 "}"); 2311 2312 verifyFormat("__try {\n" 2313 " int a = b * c;\n" 2314 "} __finally {\n" 2315 " // Do nothing.\n" 2316 "}"); 2317 2318 verifyFormat("DEBUG({\n" 2319 " __try {\n" 2320 " } __finally {\n" 2321 " }\n" 2322 "});\n"); 2323 } 2324 2325 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2326 verifyFormat("try {\n" 2327 " f();\n" 2328 "} catch {\n" 2329 " g();\n" 2330 "}"); 2331 verifyFormat("try {\n" 2332 " f();\n" 2333 "} catch (A a) MACRO(x) {\n" 2334 " g();\n" 2335 "} catch (B b) MACRO(x) {\n" 2336 " g();\n" 2337 "}"); 2338 } 2339 2340 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2341 FormatStyle Style = getLLVMStyle(); 2342 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2343 FormatStyle::BS_WebKit}) { 2344 Style.BreakBeforeBraces = BraceStyle; 2345 verifyFormat("try {\n" 2346 " // something\n" 2347 "} catch (...) {\n" 2348 " // something\n" 2349 "}", 2350 Style); 2351 } 2352 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2353 verifyFormat("try {\n" 2354 " // something\n" 2355 "}\n" 2356 "catch (...) {\n" 2357 " // something\n" 2358 "}", 2359 Style); 2360 verifyFormat("__try {\n" 2361 " // something\n" 2362 "}\n" 2363 "__finally {\n" 2364 " // something\n" 2365 "}", 2366 Style); 2367 verifyFormat("@try {\n" 2368 " // something\n" 2369 "}\n" 2370 "@finally {\n" 2371 " // something\n" 2372 "}", 2373 Style); 2374 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2375 verifyFormat("try\n" 2376 "{\n" 2377 " // something\n" 2378 "}\n" 2379 "catch (...)\n" 2380 "{\n" 2381 " // something\n" 2382 "}", 2383 Style); 2384 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2385 verifyFormat("try\n" 2386 " {\n" 2387 " // something\n" 2388 " }\n" 2389 "catch (...)\n" 2390 " {\n" 2391 " // something\n" 2392 " }", 2393 Style); 2394 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2395 Style.BraceWrapping.BeforeCatch = true; 2396 verifyFormat("try {\n" 2397 " // something\n" 2398 "}\n" 2399 "catch (...) {\n" 2400 " // something\n" 2401 "}", 2402 Style); 2403 } 2404 2405 TEST_F(FormatTest, FormatObjCTryCatch) { 2406 verifyFormat("@try {\n" 2407 " f();\n" 2408 "} @catch (NSException e) {\n" 2409 " @throw;\n" 2410 "} @finally {\n" 2411 " exit(42);\n" 2412 "}"); 2413 verifyFormat("DEBUG({\n" 2414 " @try {\n" 2415 " } @finally {\n" 2416 " }\n" 2417 "});\n"); 2418 } 2419 2420 TEST_F(FormatTest, FormatObjCAutoreleasepool) { 2421 FormatStyle Style = getLLVMStyle(); 2422 verifyFormat("@autoreleasepool {\n" 2423 " f();\n" 2424 "}\n" 2425 "@autoreleasepool {\n" 2426 " f();\n" 2427 "}\n", 2428 Style); 2429 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2430 verifyFormat("@autoreleasepool\n" 2431 "{\n" 2432 " f();\n" 2433 "}\n" 2434 "@autoreleasepool\n" 2435 "{\n" 2436 " f();\n" 2437 "}\n", 2438 Style); 2439 } 2440 2441 TEST_F(FormatTest, StaticInitializers) { 2442 verifyFormat("static SomeClass SC = {1, 'a'};"); 2443 2444 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2445 " 100000000, " 2446 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2447 2448 // Here, everything other than the "}" would fit on a line. 2449 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2450 " 10000000000000000000000000};"); 2451 EXPECT_EQ("S s = {a,\n" 2452 "\n" 2453 " b};", 2454 format("S s = {\n" 2455 " a,\n" 2456 "\n" 2457 " b\n" 2458 "};")); 2459 2460 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2461 // line. However, the formatting looks a bit off and this probably doesn't 2462 // happen often in practice. 2463 verifyFormat("static int Variable[1] = {\n" 2464 " {1000000000000000000000000000000000000}};", 2465 getLLVMStyleWithColumns(40)); 2466 } 2467 2468 TEST_F(FormatTest, DesignatedInitializers) { 2469 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2470 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2471 " .bbbbbbbbbb = 2,\n" 2472 " .cccccccccc = 3,\n" 2473 " .dddddddddd = 4,\n" 2474 " .eeeeeeeeee = 5};"); 2475 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2476 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2477 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2478 " .ccccccccccccccccccccccccccc = 3,\n" 2479 " .ddddddddddddddddddddddddddd = 4,\n" 2480 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2481 2482 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2483 } 2484 2485 TEST_F(FormatTest, NestedStaticInitializers) { 2486 verifyFormat("static A x = {{{}}};\n"); 2487 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2488 " {init1, init2, init3, init4}}};", 2489 getLLVMStyleWithColumns(50)); 2490 2491 verifyFormat("somes Status::global_reps[3] = {\n" 2492 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2493 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2494 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2495 getLLVMStyleWithColumns(60)); 2496 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2497 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2498 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2499 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2500 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2501 " {rect.fRight - rect.fLeft, rect.fBottom - " 2502 "rect.fTop}};"); 2503 2504 verifyFormat( 2505 "SomeArrayOfSomeType a = {\n" 2506 " {{1, 2, 3},\n" 2507 " {1, 2, 3},\n" 2508 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2509 " 333333333333333333333333333333},\n" 2510 " {1, 2, 3},\n" 2511 " {1, 2, 3}}};"); 2512 verifyFormat( 2513 "SomeArrayOfSomeType a = {\n" 2514 " {{1, 2, 3}},\n" 2515 " {{1, 2, 3}},\n" 2516 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2517 " 333333333333333333333333333333}},\n" 2518 " {{1, 2, 3}},\n" 2519 " {{1, 2, 3}}};"); 2520 2521 verifyFormat("struct {\n" 2522 " unsigned bit;\n" 2523 " const char *const name;\n" 2524 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2525 " {kOsWin, \"Windows\"},\n" 2526 " {kOsLinux, \"Linux\"},\n" 2527 " {kOsCrOS, \"Chrome OS\"}};"); 2528 verifyFormat("struct {\n" 2529 " unsigned bit;\n" 2530 " const char *const name;\n" 2531 "} kBitsToOs[] = {\n" 2532 " {kOsMac, \"Mac\"},\n" 2533 " {kOsWin, \"Windows\"},\n" 2534 " {kOsLinux, \"Linux\"},\n" 2535 " {kOsCrOS, \"Chrome OS\"},\n" 2536 "};"); 2537 } 2538 2539 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2540 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2541 " \\\n" 2542 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2543 } 2544 2545 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2546 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2547 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2548 2549 // Do break defaulted and deleted functions. 2550 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2551 " default;", 2552 getLLVMStyleWithColumns(40)); 2553 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2554 " delete;", 2555 getLLVMStyleWithColumns(40)); 2556 } 2557 2558 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2559 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2560 getLLVMStyleWithColumns(40)); 2561 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2562 getLLVMStyleWithColumns(40)); 2563 EXPECT_EQ("#define Q \\\n" 2564 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2565 " \"aaaaaaaa.cpp\"", 2566 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2567 getLLVMStyleWithColumns(40))); 2568 } 2569 2570 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2571 EXPECT_EQ("# 123 \"A string literal\"", 2572 format(" # 123 \"A string literal\"")); 2573 } 2574 2575 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2576 EXPECT_EQ("#;", format("#;")); 2577 verifyFormat("#\n;\n;\n;"); 2578 } 2579 2580 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2581 EXPECT_EQ("#line 42 \"test\"\n", 2582 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2583 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2584 getLLVMStyleWithColumns(12))); 2585 } 2586 2587 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2588 EXPECT_EQ("#line 42 \"test\"", 2589 format("# \\\n line \\\n 42 \\\n \"test\"")); 2590 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2591 } 2592 2593 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2594 verifyFormat("#define A \\x20"); 2595 verifyFormat("#define A \\ x20"); 2596 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2597 verifyFormat("#define A ''"); 2598 verifyFormat("#define A ''qqq"); 2599 verifyFormat("#define A `qqq"); 2600 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2601 EXPECT_EQ("const char *c = STRINGIFY(\n" 2602 "\\na : b);", 2603 format("const char * c = STRINGIFY(\n" 2604 "\\na : b);")); 2605 2606 verifyFormat("a\r\\"); 2607 verifyFormat("a\v\\"); 2608 verifyFormat("a\f\\"); 2609 } 2610 2611 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2612 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2613 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2614 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2615 // FIXME: We never break before the macro name. 2616 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2617 2618 verifyFormat("#define A A\n#define A A"); 2619 verifyFormat("#define A(X) A\n#define A A"); 2620 2621 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2622 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2623 } 2624 2625 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2626 EXPECT_EQ("// somecomment\n" 2627 "#include \"a.h\"\n" 2628 "#define A( \\\n" 2629 " A, B)\n" 2630 "#include \"b.h\"\n" 2631 "// somecomment\n", 2632 format(" // somecomment\n" 2633 " #include \"a.h\"\n" 2634 "#define A(A,\\\n" 2635 " B)\n" 2636 " #include \"b.h\"\n" 2637 " // somecomment\n", 2638 getLLVMStyleWithColumns(13))); 2639 } 2640 2641 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2642 2643 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2644 EXPECT_EQ("#define A \\\n" 2645 " c; \\\n" 2646 " e;\n" 2647 "f;", 2648 format("#define A c; e;\n" 2649 "f;", 2650 getLLVMStyleWithColumns(14))); 2651 } 2652 2653 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2654 2655 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2656 EXPECT_EQ("int x,\n" 2657 "#define A\n" 2658 " y;", 2659 format("int x,\n#define A\ny;")); 2660 } 2661 2662 TEST_F(FormatTest, HashInMacroDefinition) { 2663 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2664 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2665 verifyFormat("#define A \\\n" 2666 " { \\\n" 2667 " f(#c); \\\n" 2668 " }", 2669 getLLVMStyleWithColumns(11)); 2670 2671 verifyFormat("#define A(X) \\\n" 2672 " void function##X()", 2673 getLLVMStyleWithColumns(22)); 2674 2675 verifyFormat("#define A(a, b, c) \\\n" 2676 " void a##b##c()", 2677 getLLVMStyleWithColumns(22)); 2678 2679 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2680 } 2681 2682 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2683 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2684 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2685 } 2686 2687 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2688 EXPECT_EQ("#define A b;", format("#define A \\\n" 2689 " \\\n" 2690 " b;", 2691 getLLVMStyleWithColumns(25))); 2692 EXPECT_EQ("#define A \\\n" 2693 " \\\n" 2694 " a; \\\n" 2695 " b;", 2696 format("#define A \\\n" 2697 " \\\n" 2698 " a; \\\n" 2699 " b;", 2700 getLLVMStyleWithColumns(11))); 2701 EXPECT_EQ("#define A \\\n" 2702 " a; \\\n" 2703 " \\\n" 2704 " b;", 2705 format("#define A \\\n" 2706 " a; \\\n" 2707 " \\\n" 2708 " b;", 2709 getLLVMStyleWithColumns(11))); 2710 } 2711 2712 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2713 verifyIncompleteFormat("#define A :"); 2714 verifyFormat("#define SOMECASES \\\n" 2715 " case 1: \\\n" 2716 " case 2\n", 2717 getLLVMStyleWithColumns(20)); 2718 verifyFormat("#define A template <typename T>"); 2719 verifyIncompleteFormat("#define STR(x) #x\n" 2720 "f(STR(this_is_a_string_literal{));"); 2721 verifyFormat("#pragma omp threadprivate( \\\n" 2722 " y)), // expected-warning", 2723 getLLVMStyleWithColumns(28)); 2724 verifyFormat("#d, = };"); 2725 verifyFormat("#if \"a"); 2726 verifyIncompleteFormat("({\n" 2727 "#define b \\\n" 2728 " } \\\n" 2729 " a\n" 2730 "a", 2731 getLLVMStyleWithColumns(15)); 2732 verifyFormat("#define A \\\n" 2733 " { \\\n" 2734 " {\n" 2735 "#define B \\\n" 2736 " } \\\n" 2737 " }", 2738 getLLVMStyleWithColumns(15)); 2739 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2740 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2741 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2742 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2743 } 2744 2745 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2746 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2747 EXPECT_EQ("class A : public QObject {\n" 2748 " Q_OBJECT\n" 2749 "\n" 2750 " A() {}\n" 2751 "};", 2752 format("class A : public QObject {\n" 2753 " Q_OBJECT\n" 2754 "\n" 2755 " A() {\n}\n" 2756 "} ;")); 2757 EXPECT_EQ("MACRO\n" 2758 "/*static*/ int i;", 2759 format("MACRO\n" 2760 " /*static*/ int i;")); 2761 EXPECT_EQ("SOME_MACRO\n" 2762 "namespace {\n" 2763 "void f();\n" 2764 "}", 2765 format("SOME_MACRO\n" 2766 " namespace {\n" 2767 "void f( );\n" 2768 "}")); 2769 // Only if the identifier contains at least 5 characters. 2770 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2771 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2772 // Only if everything is upper case. 2773 EXPECT_EQ("class A : public QObject {\n" 2774 " Q_Object A() {}\n" 2775 "};", 2776 format("class A : public QObject {\n" 2777 " Q_Object\n" 2778 " A() {\n}\n" 2779 "} ;")); 2780 2781 // Only if the next line can actually start an unwrapped line. 2782 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2783 format("SOME_WEIRD_LOG_MACRO\n" 2784 "<< SomeThing;")); 2785 2786 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2787 "(n, buffers))\n", 2788 getChromiumStyle(FormatStyle::LK_Cpp)); 2789 } 2790 2791 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2792 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2793 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2794 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2795 "class X {};\n" 2796 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2797 "int *createScopDetectionPass() { return 0; }", 2798 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2799 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2800 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2801 " class X {};\n" 2802 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2803 " int *createScopDetectionPass() { return 0; }")); 2804 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2805 // braces, so that inner block is indented one level more. 2806 EXPECT_EQ("int q() {\n" 2807 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2808 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2809 " IPC_END_MESSAGE_MAP()\n" 2810 "}", 2811 format("int q() {\n" 2812 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2813 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2814 " IPC_END_MESSAGE_MAP()\n" 2815 "}")); 2816 2817 // Same inside macros. 2818 EXPECT_EQ("#define LIST(L) \\\n" 2819 " L(A) \\\n" 2820 " L(B) \\\n" 2821 " L(C)", 2822 format("#define LIST(L) \\\n" 2823 " L(A) \\\n" 2824 " L(B) \\\n" 2825 " L(C)", 2826 getGoogleStyle())); 2827 2828 // These must not be recognized as macros. 2829 EXPECT_EQ("int q() {\n" 2830 " f(x);\n" 2831 " f(x) {}\n" 2832 " f(x)->g();\n" 2833 " f(x)->*g();\n" 2834 " f(x).g();\n" 2835 " f(x) = x;\n" 2836 " f(x) += x;\n" 2837 " f(x) -= x;\n" 2838 " f(x) *= x;\n" 2839 " f(x) /= x;\n" 2840 " f(x) %= x;\n" 2841 " f(x) &= x;\n" 2842 " f(x) |= x;\n" 2843 " f(x) ^= x;\n" 2844 " f(x) >>= x;\n" 2845 " f(x) <<= x;\n" 2846 " f(x)[y].z();\n" 2847 " LOG(INFO) << x;\n" 2848 " ifstream(x) >> x;\n" 2849 "}\n", 2850 format("int q() {\n" 2851 " f(x)\n;\n" 2852 " f(x)\n {}\n" 2853 " f(x)\n->g();\n" 2854 " f(x)\n->*g();\n" 2855 " f(x)\n.g();\n" 2856 " f(x)\n = x;\n" 2857 " f(x)\n += x;\n" 2858 " f(x)\n -= x;\n" 2859 " f(x)\n *= x;\n" 2860 " f(x)\n /= x;\n" 2861 " f(x)\n %= x;\n" 2862 " f(x)\n &= x;\n" 2863 " f(x)\n |= x;\n" 2864 " f(x)\n ^= x;\n" 2865 " f(x)\n >>= x;\n" 2866 " f(x)\n <<= x;\n" 2867 " f(x)\n[y].z();\n" 2868 " LOG(INFO)\n << x;\n" 2869 " ifstream(x)\n >> x;\n" 2870 "}\n")); 2871 EXPECT_EQ("int q() {\n" 2872 " F(x)\n" 2873 " if (1) {\n" 2874 " }\n" 2875 " F(x)\n" 2876 " while (1) {\n" 2877 " }\n" 2878 " F(x)\n" 2879 " G(x);\n" 2880 " F(x)\n" 2881 " try {\n" 2882 " Q();\n" 2883 " } catch (...) {\n" 2884 " }\n" 2885 "}\n", 2886 format("int q() {\n" 2887 "F(x)\n" 2888 "if (1) {}\n" 2889 "F(x)\n" 2890 "while (1) {}\n" 2891 "F(x)\n" 2892 "G(x);\n" 2893 "F(x)\n" 2894 "try { Q(); } catch (...) {}\n" 2895 "}\n")); 2896 EXPECT_EQ("class A {\n" 2897 " A() : t(0) {}\n" 2898 " A(int i) noexcept() : {}\n" 2899 " A(X x)\n" // FIXME: function-level try blocks are broken. 2900 " try : t(0) {\n" 2901 " } catch (...) {\n" 2902 " }\n" 2903 "};", 2904 format("class A {\n" 2905 " A()\n : t(0) {}\n" 2906 " A(int i)\n noexcept() : {}\n" 2907 " A(X x)\n" 2908 " try : t(0) {} catch (...) {}\n" 2909 "};")); 2910 EXPECT_EQ("class SomeClass {\n" 2911 "public:\n" 2912 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2913 "};", 2914 format("class SomeClass {\n" 2915 "public:\n" 2916 " SomeClass()\n" 2917 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2918 "};")); 2919 EXPECT_EQ("class SomeClass {\n" 2920 "public:\n" 2921 " SomeClass()\n" 2922 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2923 "};", 2924 format("class SomeClass {\n" 2925 "public:\n" 2926 " SomeClass()\n" 2927 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2928 "};", 2929 getLLVMStyleWithColumns(40))); 2930 2931 verifyFormat("MACRO(>)"); 2932 } 2933 2934 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2935 verifyFormat("#define A \\\n" 2936 " f({ \\\n" 2937 " g(); \\\n" 2938 " });", 2939 getLLVMStyleWithColumns(11)); 2940 } 2941 2942 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2943 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2944 } 2945 2946 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2947 verifyFormat("{\n { a #c; }\n}"); 2948 } 2949 2950 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2951 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2952 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2953 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2954 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2955 } 2956 2957 TEST_F(FormatTest, EscapedNewlines) { 2958 EXPECT_EQ( 2959 "#define A \\\n int i; \\\n int j;", 2960 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2961 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2962 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2963 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 2964 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2965 } 2966 2967 TEST_F(FormatTest, DontCrashOnBlockComments) { 2968 EXPECT_EQ( 2969 "int xxxxxxxxx; /* " 2970 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 2971 "zzzzzz\n" 2972 "0*/", 2973 format("int xxxxxxxxx; /* " 2974 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 2975 "0*/")); 2976 } 2977 2978 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2979 verifyFormat("#define A \\\n" 2980 " int v( \\\n" 2981 " a); \\\n" 2982 " int i;", 2983 getLLVMStyleWithColumns(11)); 2984 } 2985 2986 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2987 EXPECT_EQ( 2988 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2989 " \\\n" 2990 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2991 "\n" 2992 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2993 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2994 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2995 "\\\n" 2996 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2997 " \n" 2998 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2999 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3000 } 3001 3002 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3003 EXPECT_EQ("int\n" 3004 "#define A\n" 3005 " a;", 3006 format("int\n#define A\na;")); 3007 verifyFormat("functionCallTo(\n" 3008 " someOtherFunction(\n" 3009 " withSomeParameters, whichInSequence,\n" 3010 " areLongerThanALine(andAnotherCall,\n" 3011 "#define A B\n" 3012 " withMoreParamters,\n" 3013 " whichStronglyInfluenceTheLayout),\n" 3014 " andMoreParameters),\n" 3015 " trailing);", 3016 getLLVMStyleWithColumns(69)); 3017 verifyFormat("Foo::Foo()\n" 3018 "#ifdef BAR\n" 3019 " : baz(0)\n" 3020 "#endif\n" 3021 "{\n" 3022 "}"); 3023 verifyFormat("void f() {\n" 3024 " if (true)\n" 3025 "#ifdef A\n" 3026 " f(42);\n" 3027 " x();\n" 3028 "#else\n" 3029 " g();\n" 3030 " x();\n" 3031 "#endif\n" 3032 "}"); 3033 verifyFormat("void f(param1, param2,\n" 3034 " param3,\n" 3035 "#ifdef A\n" 3036 " param4(param5,\n" 3037 "#ifdef A1\n" 3038 " param6,\n" 3039 "#ifdef A2\n" 3040 " param7),\n" 3041 "#else\n" 3042 " param8),\n" 3043 " param9,\n" 3044 "#endif\n" 3045 " param10,\n" 3046 "#endif\n" 3047 " param11)\n" 3048 "#else\n" 3049 " param12)\n" 3050 "#endif\n" 3051 "{\n" 3052 " x();\n" 3053 "}", 3054 getLLVMStyleWithColumns(28)); 3055 verifyFormat("#if 1\n" 3056 "int i;"); 3057 verifyFormat("#if 1\n" 3058 "#endif\n" 3059 "#if 1\n" 3060 "#else\n" 3061 "#endif\n"); 3062 verifyFormat("DEBUG({\n" 3063 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3065 "});\n" 3066 "#if a\n" 3067 "#else\n" 3068 "#endif"); 3069 3070 verifyIncompleteFormat("void f(\n" 3071 "#if A\n" 3072 " );\n" 3073 "#else\n" 3074 "#endif"); 3075 } 3076 3077 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3078 verifyFormat("#endif\n" 3079 "#if B"); 3080 } 3081 3082 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3083 FormatStyle SingleLine = getLLVMStyle(); 3084 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3085 verifyFormat("#if 0\n" 3086 "#elif 1\n" 3087 "#endif\n" 3088 "void foo() {\n" 3089 " if (test) foo2();\n" 3090 "}", 3091 SingleLine); 3092 } 3093 3094 TEST_F(FormatTest, LayoutBlockInsideParens) { 3095 verifyFormat("functionCall({ int i; });"); 3096 verifyFormat("functionCall({\n" 3097 " int i;\n" 3098 " int j;\n" 3099 "});"); 3100 verifyFormat("functionCall(\n" 3101 " {\n" 3102 " int i;\n" 3103 " int j;\n" 3104 " },\n" 3105 " aaaa, bbbb, cccc);"); 3106 verifyFormat("functionA(functionB({\n" 3107 " int i;\n" 3108 " int j;\n" 3109 " }),\n" 3110 " aaaa, bbbb, cccc);"); 3111 verifyFormat("functionCall(\n" 3112 " {\n" 3113 " int i;\n" 3114 " int j;\n" 3115 " },\n" 3116 " aaaa, bbbb, // comment\n" 3117 " cccc);"); 3118 verifyFormat("functionA(functionB({\n" 3119 " int i;\n" 3120 " int j;\n" 3121 " }),\n" 3122 " aaaa, bbbb, // comment\n" 3123 " cccc);"); 3124 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3125 verifyFormat("functionCall(aaaa, bbbb, {\n" 3126 " int i;\n" 3127 " int j;\n" 3128 "});"); 3129 verifyFormat( 3130 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3131 " {\n" 3132 " int i; // break\n" 3133 " },\n" 3134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3135 " ccccccccccccccccc));"); 3136 verifyFormat("DEBUG({\n" 3137 " if (a)\n" 3138 " f();\n" 3139 "});"); 3140 } 3141 3142 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3143 EXPECT_EQ("SOME_MACRO { int i; }\n" 3144 "int i;", 3145 format(" SOME_MACRO {int i;} int i;")); 3146 } 3147 3148 TEST_F(FormatTest, LayoutNestedBlocks) { 3149 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3150 " struct s {\n" 3151 " int i;\n" 3152 " };\n" 3153 " s kBitsToOs[] = {{10}};\n" 3154 " for (int i = 0; i < 10; ++i)\n" 3155 " return;\n" 3156 "}"); 3157 verifyFormat("call(parameter, {\n" 3158 " something();\n" 3159 " // Comment using all columns.\n" 3160 " somethingelse();\n" 3161 "});", 3162 getLLVMStyleWithColumns(40)); 3163 verifyFormat("DEBUG( //\n" 3164 " { f(); }, a);"); 3165 verifyFormat("DEBUG( //\n" 3166 " {\n" 3167 " f(); //\n" 3168 " },\n" 3169 " a);"); 3170 3171 EXPECT_EQ("call(parameter, {\n" 3172 " something();\n" 3173 " // Comment too\n" 3174 " // looooooooooong.\n" 3175 " somethingElse();\n" 3176 "});", 3177 format("call(parameter, {\n" 3178 " something();\n" 3179 " // Comment too looooooooooong.\n" 3180 " somethingElse();\n" 3181 "});", 3182 getLLVMStyleWithColumns(29))); 3183 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3184 EXPECT_EQ("DEBUG({ // comment\n" 3185 " int i;\n" 3186 "});", 3187 format("DEBUG({ // comment\n" 3188 "int i;\n" 3189 "});")); 3190 EXPECT_EQ("DEBUG({\n" 3191 " int i;\n" 3192 "\n" 3193 " // comment\n" 3194 " int j;\n" 3195 "});", 3196 format("DEBUG({\n" 3197 " int i;\n" 3198 "\n" 3199 " // comment\n" 3200 " int j;\n" 3201 "});")); 3202 3203 verifyFormat("DEBUG({\n" 3204 " if (a)\n" 3205 " return;\n" 3206 "});"); 3207 verifyGoogleFormat("DEBUG({\n" 3208 " if (a) return;\n" 3209 "});"); 3210 FormatStyle Style = getGoogleStyle(); 3211 Style.ColumnLimit = 45; 3212 verifyFormat("Debug(aaaaa,\n" 3213 " {\n" 3214 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3215 " },\n" 3216 " a);", 3217 Style); 3218 3219 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3220 3221 verifyNoCrash("^{v^{a}}"); 3222 } 3223 3224 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3225 EXPECT_EQ("#define MACRO() \\\n" 3226 " Debug(aaa, /* force line break */ \\\n" 3227 " { \\\n" 3228 " int i; \\\n" 3229 " int j; \\\n" 3230 " })", 3231 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3232 " { int i; int j; })", 3233 getGoogleStyle())); 3234 3235 EXPECT_EQ("#define A \\\n" 3236 " [] { \\\n" 3237 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3238 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3239 " }", 3240 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3241 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3242 getGoogleStyle())); 3243 } 3244 3245 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3246 EXPECT_EQ("{}", format("{}")); 3247 verifyFormat("enum E {};"); 3248 verifyFormat("enum E {}"); 3249 } 3250 3251 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3252 FormatStyle Style = getLLVMStyle(); 3253 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3254 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3255 verifyFormat("FOO_BEGIN\n" 3256 " FOO_ENTRY\n" 3257 "FOO_END", Style); 3258 verifyFormat("FOO_BEGIN\n" 3259 " NESTED_FOO_BEGIN\n" 3260 " NESTED_FOO_ENTRY\n" 3261 " NESTED_FOO_END\n" 3262 "FOO_END", Style); 3263 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3264 " int x;\n" 3265 " x = 1;\n" 3266 "FOO_END(Baz)", Style); 3267 } 3268 3269 //===----------------------------------------------------------------------===// 3270 // Line break tests. 3271 //===----------------------------------------------------------------------===// 3272 3273 TEST_F(FormatTest, PreventConfusingIndents) { 3274 verifyFormat( 3275 "void f() {\n" 3276 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3277 " parameter, parameter, parameter)),\n" 3278 " SecondLongCall(parameter));\n" 3279 "}"); 3280 verifyFormat( 3281 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3282 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3284 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3285 verifyFormat( 3286 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3287 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3288 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3289 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3290 verifyFormat( 3291 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3292 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3294 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3295 verifyFormat("int a = bbbb && ccc && fffff(\n" 3296 "#define A Just forcing a new line\n" 3297 " ddd);"); 3298 } 3299 3300 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3301 verifyFormat( 3302 "bool aaaaaaa =\n" 3303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3304 " bbbbbbbb();"); 3305 verifyFormat( 3306 "bool aaaaaaa =\n" 3307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3308 " bbbbbbbb();"); 3309 3310 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3312 " ccccccccc == ddddddddddd;"); 3313 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3315 " ccccccccc == ddddddddddd;"); 3316 verifyFormat( 3317 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3319 " ccccccccc == ddddddddddd;"); 3320 3321 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3322 " aaaaaa) &&\n" 3323 " bbbbbb && cccccc;"); 3324 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3325 " aaaaaa) >>\n" 3326 " bbbbbb;"); 3327 verifyFormat("Whitespaces.addUntouchableComment(\n" 3328 " SourceMgr.getSpellingColumnNumber(\n" 3329 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3330 " 1);"); 3331 3332 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3333 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3334 " cccccc) {\n}"); 3335 verifyFormat("b = a &&\n" 3336 " // Comment\n" 3337 " b.c && d;"); 3338 3339 // If the LHS of a comparison is not a binary expression itself, the 3340 // additional linebreak confuses many people. 3341 verifyFormat( 3342 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3344 "}"); 3345 verifyFormat( 3346 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3348 "}"); 3349 verifyFormat( 3350 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3352 "}"); 3353 // Even explicit parentheses stress the precedence enough to make the 3354 // additional break unnecessary. 3355 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3357 "}"); 3358 // This cases is borderline, but with the indentation it is still readable. 3359 verifyFormat( 3360 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3361 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3362 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3363 "}", 3364 getLLVMStyleWithColumns(75)); 3365 3366 // If the LHS is a binary expression, we should still use the additional break 3367 // as otherwise the formatting hides the operator precedence. 3368 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3369 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3370 " 5) {\n" 3371 "}"); 3372 3373 FormatStyle OnePerLine = getLLVMStyle(); 3374 OnePerLine.BinPackParameters = false; 3375 verifyFormat( 3376 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3379 OnePerLine); 3380 } 3381 3382 TEST_F(FormatTest, ExpressionIndentation) { 3383 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3387 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3388 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3391 " ccccccccccccccccccccccccccccccccccccccccc;"); 3392 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3395 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3396 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3397 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3399 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3400 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3403 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3404 verifyFormat("if () {\n" 3405 "} else if (aaaaa &&\n" 3406 " bbbbb > // break\n" 3407 " ccccc) {\n" 3408 "}"); 3409 3410 // Presence of a trailing comment used to change indentation of b. 3411 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3412 " b;\n" 3413 "return aaaaaaaaaaaaaaaaaaa +\n" 3414 " b; //", 3415 getLLVMStyleWithColumns(30)); 3416 } 3417 3418 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3419 // Not sure what the best system is here. Like this, the LHS can be found 3420 // immediately above an operator (everything with the same or a higher 3421 // indent). The RHS is aligned right of the operator and so compasses 3422 // everything until something with the same indent as the operator is found. 3423 // FIXME: Is this a good system? 3424 FormatStyle Style = getLLVMStyle(); 3425 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3426 verifyFormat( 3427 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3428 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3429 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3430 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3431 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3432 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3433 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3434 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3435 " > ccccccccccccccccccccccccccccccccccccccccc;", 3436 Style); 3437 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3438 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3439 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3440 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3441 Style); 3442 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3443 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3444 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3445 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3446 Style); 3447 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3448 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3449 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3450 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3451 Style); 3452 verifyFormat("if () {\n" 3453 "} else if (aaaaa\n" 3454 " && bbbbb // break\n" 3455 " > ccccc) {\n" 3456 "}", 3457 Style); 3458 verifyFormat("return (a)\n" 3459 " // comment\n" 3460 " + b;", 3461 Style); 3462 verifyFormat( 3463 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3464 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3465 " + cc;", 3466 Style); 3467 3468 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3469 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3470 Style); 3471 3472 // Forced by comments. 3473 verifyFormat( 3474 "unsigned ContentSize =\n" 3475 " sizeof(int16_t) // DWARF ARange version number\n" 3476 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3477 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3478 " + sizeof(int8_t); // Segment Size (in bytes)"); 3479 3480 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3481 " == boost::fusion::at_c<1>(iiii).second;", 3482 Style); 3483 3484 Style.ColumnLimit = 60; 3485 verifyFormat("zzzzzzzzzz\n" 3486 " = bbbbbbbbbbbbbbbbb\n" 3487 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3488 Style); 3489 } 3490 3491 TEST_F(FormatTest, NoOperandAlignment) { 3492 FormatStyle Style = getLLVMStyle(); 3493 Style.AlignOperands = false; 3494 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3495 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3496 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3497 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3498 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3499 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3500 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3501 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3502 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3503 " > ccccccccccccccccccccccccccccccccccccccccc;", 3504 Style); 3505 3506 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3507 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3508 " + cc;", 3509 Style); 3510 verifyFormat("int a = aa\n" 3511 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3512 " * cccccccccccccccccccccccccccccccccccc;", 3513 Style); 3514 3515 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3516 verifyFormat("return (a > b\n" 3517 " // comment1\n" 3518 " // comment2\n" 3519 " || c);", 3520 Style); 3521 } 3522 3523 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3524 FormatStyle Style = getLLVMStyle(); 3525 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3526 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3528 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3529 Style); 3530 } 3531 3532 TEST_F(FormatTest, ConstructorInitializers) { 3533 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3534 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3535 getLLVMStyleWithColumns(45)); 3536 verifyFormat("Constructor()\n" 3537 " : Inttializer(FitsOnTheLine) {}", 3538 getLLVMStyleWithColumns(44)); 3539 verifyFormat("Constructor()\n" 3540 " : Inttializer(FitsOnTheLine) {}", 3541 getLLVMStyleWithColumns(43)); 3542 3543 verifyFormat("template <typename T>\n" 3544 "Constructor() : Initializer(FitsOnTheLine) {}", 3545 getLLVMStyleWithColumns(45)); 3546 3547 verifyFormat( 3548 "SomeClass::Constructor()\n" 3549 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3550 3551 verifyFormat( 3552 "SomeClass::Constructor()\n" 3553 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3554 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3555 verifyFormat( 3556 "SomeClass::Constructor()\n" 3557 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3558 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3559 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3560 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3561 " : aaaaaaaaaa(aaaaaa) {}"); 3562 3563 verifyFormat("Constructor()\n" 3564 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3565 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3566 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3567 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3568 3569 verifyFormat("Constructor()\n" 3570 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3572 3573 verifyFormat("Constructor(int Parameter = 0)\n" 3574 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3575 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3576 verifyFormat("Constructor()\n" 3577 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3578 "}", 3579 getLLVMStyleWithColumns(60)); 3580 verifyFormat("Constructor()\n" 3581 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3582 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3583 3584 // Here a line could be saved by splitting the second initializer onto two 3585 // lines, but that is not desirable. 3586 verifyFormat("Constructor()\n" 3587 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3588 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3589 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3590 3591 FormatStyle OnePerLine = getLLVMStyle(); 3592 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3593 verifyFormat("SomeClass::Constructor()\n" 3594 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3595 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3596 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3597 OnePerLine); 3598 verifyFormat("SomeClass::Constructor()\n" 3599 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3600 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3601 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3602 OnePerLine); 3603 verifyFormat("MyClass::MyClass(int var)\n" 3604 " : some_var_(var), // 4 space indent\n" 3605 " some_other_var_(var + 1) { // lined up\n" 3606 "}", 3607 OnePerLine); 3608 verifyFormat("Constructor()\n" 3609 " : aaaaa(aaaaaa),\n" 3610 " aaaaa(aaaaaa),\n" 3611 " aaaaa(aaaaaa),\n" 3612 " aaaaa(aaaaaa),\n" 3613 " aaaaa(aaaaaa) {}", 3614 OnePerLine); 3615 verifyFormat("Constructor()\n" 3616 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3617 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3618 OnePerLine); 3619 OnePerLine.ColumnLimit = 60; 3620 verifyFormat("Constructor()\n" 3621 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3622 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3623 OnePerLine); 3624 3625 EXPECT_EQ("Constructor()\n" 3626 " : // Comment forcing unwanted break.\n" 3627 " aaaa(aaaa) {}", 3628 format("Constructor() :\n" 3629 " // Comment forcing unwanted break.\n" 3630 " aaaa(aaaa) {}")); 3631 } 3632 3633 TEST_F(FormatTest, MemoizationTests) { 3634 // This breaks if the memoization lookup does not take \c Indent and 3635 // \c LastSpace into account. 3636 verifyFormat( 3637 "extern CFRunLoopTimerRef\n" 3638 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3639 " CFTimeInterval interval, CFOptionFlags flags,\n" 3640 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3641 " CFRunLoopTimerContext *context) {}"); 3642 3643 // Deep nesting somewhat works around our memoization. 3644 verifyFormat( 3645 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3646 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3647 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3648 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3649 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3650 getLLVMStyleWithColumns(65)); 3651 verifyFormat( 3652 "aaaaa(\n" 3653 " aaaaa,\n" 3654 " aaaaa(\n" 3655 " aaaaa,\n" 3656 " aaaaa(\n" 3657 " aaaaa,\n" 3658 " aaaaa(\n" 3659 " aaaaa,\n" 3660 " aaaaa(\n" 3661 " aaaaa,\n" 3662 " aaaaa(\n" 3663 " aaaaa,\n" 3664 " aaaaa(\n" 3665 " aaaaa,\n" 3666 " aaaaa(\n" 3667 " aaaaa,\n" 3668 " aaaaa(\n" 3669 " aaaaa,\n" 3670 " aaaaa(\n" 3671 " aaaaa,\n" 3672 " aaaaa(\n" 3673 " aaaaa,\n" 3674 " aaaaa(\n" 3675 " aaaaa,\n" 3676 " aaaaa))))))))))));", 3677 getLLVMStyleWithColumns(65)); 3678 verifyFormat( 3679 "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" 3680 " a),\n" 3681 " a),\n" 3682 " a),\n" 3683 " a),\n" 3684 " a),\n" 3685 " a),\n" 3686 " a),\n" 3687 " a),\n" 3688 " a),\n" 3689 " a),\n" 3690 " a),\n" 3691 " a),\n" 3692 " a),\n" 3693 " a),\n" 3694 " a),\n" 3695 " a),\n" 3696 " a)", 3697 getLLVMStyleWithColumns(65)); 3698 3699 // This test takes VERY long when memoization is broken. 3700 FormatStyle OnePerLine = getLLVMStyle(); 3701 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3702 OnePerLine.BinPackParameters = false; 3703 std::string input = "Constructor()\n" 3704 " : aaaa(a,\n"; 3705 for (unsigned i = 0, e = 80; i != e; ++i) { 3706 input += " a,\n"; 3707 } 3708 input += " a) {}"; 3709 verifyFormat(input, OnePerLine); 3710 } 3711 3712 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3713 verifyFormat( 3714 "void f() {\n" 3715 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3716 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3717 " f();\n" 3718 "}"); 3719 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3720 " Intervals[i - 1].getRange().getLast()) {\n}"); 3721 } 3722 3723 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3724 // Principially, we break function declarations in a certain order: 3725 // 1) break amongst arguments. 3726 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3727 " Cccccccccccccc cccccccccccccc);"); 3728 verifyFormat("template <class TemplateIt>\n" 3729 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3730 " TemplateIt *stop) {}"); 3731 3732 // 2) break after return type. 3733 verifyFormat( 3734 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3735 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3736 getGoogleStyle()); 3737 3738 // 3) break after (. 3739 verifyFormat( 3740 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3741 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3742 getGoogleStyle()); 3743 3744 // 4) break before after nested name specifiers. 3745 verifyFormat( 3746 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3747 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3748 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3749 getGoogleStyle()); 3750 3751 // However, there are exceptions, if a sufficient amount of lines can be 3752 // saved. 3753 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3754 // more adjusting. 3755 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3756 " Cccccccccccccc cccccccccc,\n" 3757 " Cccccccccccccc cccccccccc,\n" 3758 " Cccccccccccccc cccccccccc,\n" 3759 " Cccccccccccccc cccccccccc);"); 3760 verifyFormat( 3761 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3762 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3763 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3764 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3765 getGoogleStyle()); 3766 verifyFormat( 3767 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3768 " Cccccccccccccc cccccccccc,\n" 3769 " Cccccccccccccc cccccccccc,\n" 3770 " Cccccccccccccc cccccccccc,\n" 3771 " Cccccccccccccc cccccccccc,\n" 3772 " Cccccccccccccc cccccccccc,\n" 3773 " Cccccccccccccc cccccccccc);"); 3774 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3775 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3776 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3777 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3778 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3779 3780 // Break after multi-line parameters. 3781 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3782 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3784 " bbbb bbbb);"); 3785 verifyFormat("void SomeLoooooooooooongFunction(\n" 3786 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3787 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3788 " int bbbbbbbbbbbbb);"); 3789 3790 // Treat overloaded operators like other functions. 3791 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3792 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3793 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3794 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3795 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3796 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3797 verifyGoogleFormat( 3798 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3799 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3800 verifyGoogleFormat( 3801 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3802 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3803 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3804 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3805 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3806 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3807 verifyGoogleFormat( 3808 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3809 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3810 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3811 3812 FormatStyle Style = getLLVMStyle(); 3813 Style.PointerAlignment = FormatStyle::PAS_Left; 3814 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3815 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3816 Style); 3817 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3818 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3819 Style); 3820 } 3821 3822 TEST_F(FormatTest, TrailingReturnType) { 3823 verifyFormat("auto foo() -> int;\n"); 3824 verifyFormat("struct S {\n" 3825 " auto bar() const -> int;\n" 3826 "};"); 3827 verifyFormat("template <size_t Order, typename T>\n" 3828 "auto load_img(const std::string &filename)\n" 3829 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3830 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3831 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3832 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3833 verifyFormat("template <typename T>\n" 3834 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3835 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3836 3837 // Not trailing return types. 3838 verifyFormat("void f() { auto a = b->c(); }"); 3839 } 3840 3841 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3842 // Avoid breaking before trailing 'const' or other trailing annotations, if 3843 // they are not function-like. 3844 FormatStyle Style = getGoogleStyle(); 3845 Style.ColumnLimit = 47; 3846 verifyFormat("void someLongFunction(\n" 3847 " int someLoooooooooooooongParameter) const {\n}", 3848 getLLVMStyleWithColumns(47)); 3849 verifyFormat("LoooooongReturnType\n" 3850 "someLoooooooongFunction() const {}", 3851 getLLVMStyleWithColumns(47)); 3852 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3853 " const {}", 3854 Style); 3855 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3856 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3857 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3858 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3859 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3860 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3861 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3862 " aaaaaaaaaaa aaaaa) const override;"); 3863 verifyGoogleFormat( 3864 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3865 " const override;"); 3866 3867 // Even if the first parameter has to be wrapped. 3868 verifyFormat("void someLongFunction(\n" 3869 " int someLongParameter) const {}", 3870 getLLVMStyleWithColumns(46)); 3871 verifyFormat("void someLongFunction(\n" 3872 " int someLongParameter) const {}", 3873 Style); 3874 verifyFormat("void someLongFunction(\n" 3875 " int someLongParameter) override {}", 3876 Style); 3877 verifyFormat("void someLongFunction(\n" 3878 " int someLongParameter) OVERRIDE {}", 3879 Style); 3880 verifyFormat("void someLongFunction(\n" 3881 " int someLongParameter) final {}", 3882 Style); 3883 verifyFormat("void someLongFunction(\n" 3884 " int someLongParameter) FINAL {}", 3885 Style); 3886 verifyFormat("void someLongFunction(\n" 3887 " int parameter) const override {}", 3888 Style); 3889 3890 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3891 verifyFormat("void someLongFunction(\n" 3892 " int someLongParameter) const\n" 3893 "{\n" 3894 "}", 3895 Style); 3896 3897 // Unless these are unknown annotations. 3898 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3899 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3900 " LONG_AND_UGLY_ANNOTATION;"); 3901 3902 // Breaking before function-like trailing annotations is fine to keep them 3903 // close to their arguments. 3904 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3905 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3906 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3907 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3908 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3909 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3910 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3911 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3912 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3913 3914 verifyFormat( 3915 "void aaaaaaaaaaaaaaaaaa()\n" 3916 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3917 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3918 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3919 " __attribute__((unused));"); 3920 verifyGoogleFormat( 3921 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3922 " GUARDED_BY(aaaaaaaaaaaa);"); 3923 verifyGoogleFormat( 3924 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3925 " GUARDED_BY(aaaaaaaaaaaa);"); 3926 verifyGoogleFormat( 3927 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3928 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3929 verifyGoogleFormat( 3930 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3931 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3932 } 3933 3934 TEST_F(FormatTest, FunctionAnnotations) { 3935 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3936 "int OldFunction(const string ¶meter) {}"); 3937 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3938 "string OldFunction(const string ¶meter) {}"); 3939 verifyFormat("template <typename T>\n" 3940 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3941 "string OldFunction(const string ¶meter) {}"); 3942 3943 // Not function annotations. 3944 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3945 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3946 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3947 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3948 } 3949 3950 TEST_F(FormatTest, BreaksDesireably) { 3951 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3952 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3953 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3954 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3956 "}"); 3957 3958 verifyFormat( 3959 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3961 3962 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3964 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3965 3966 verifyFormat( 3967 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3969 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3971 3972 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3973 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3974 3975 verifyFormat( 3976 "void f() {\n" 3977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3978 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3979 "}"); 3980 verifyFormat( 3981 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3983 verifyFormat( 3984 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3986 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3989 3990 // Indent consistently independent of call expression and unary operator. 3991 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3992 " dddddddddddddddddddddddddddddd));"); 3993 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3994 " dddddddddddddddddddddddddddddd));"); 3995 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3996 " dddddddddddddddddddddddddddddd));"); 3997 3998 // This test case breaks on an incorrect memoization, i.e. an optimization not 3999 // taking into account the StopAt value. 4000 verifyFormat( 4001 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4002 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4003 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4004 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4005 4006 verifyFormat("{\n {\n {\n" 4007 " Annotation.SpaceRequiredBefore =\n" 4008 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4009 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4010 " }\n }\n}"); 4011 4012 // Break on an outer level if there was a break on an inner level. 4013 EXPECT_EQ("f(g(h(a, // comment\n" 4014 " b, c),\n" 4015 " d, e),\n" 4016 " x, y);", 4017 format("f(g(h(a, // comment\n" 4018 " b, c), d, e), x, y);")); 4019 4020 // Prefer breaking similar line breaks. 4021 verifyFormat( 4022 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4023 " NSTrackingMouseEnteredAndExited |\n" 4024 " NSTrackingActiveAlways;"); 4025 } 4026 4027 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4028 FormatStyle NoBinPacking = getGoogleStyle(); 4029 NoBinPacking.BinPackParameters = false; 4030 NoBinPacking.BinPackArguments = true; 4031 verifyFormat("void f() {\n" 4032 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4034 "}", 4035 NoBinPacking); 4036 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4037 " int aaaaaaaaaaaaaaaaaaaa,\n" 4038 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4039 NoBinPacking); 4040 } 4041 4042 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4043 FormatStyle NoBinPacking = getGoogleStyle(); 4044 NoBinPacking.BinPackParameters = false; 4045 NoBinPacking.BinPackArguments = false; 4046 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4047 " aaaaaaaaaaaaaaaaaaaa,\n" 4048 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4049 NoBinPacking); 4050 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4051 " aaaaaaaaaaaaa,\n" 4052 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4053 NoBinPacking); 4054 verifyFormat( 4055 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4056 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4058 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4060 NoBinPacking); 4061 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4062 " .aaaaaaaaaaaaaaaaaa();", 4063 NoBinPacking); 4064 verifyFormat("void f() {\n" 4065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4066 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4067 "}", 4068 NoBinPacking); 4069 4070 verifyFormat( 4071 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4072 " aaaaaaaaaaaa,\n" 4073 " aaaaaaaaaaaa);", 4074 NoBinPacking); 4075 verifyFormat( 4076 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4077 " ddddddddddddddddddddddddddddd),\n" 4078 " test);", 4079 NoBinPacking); 4080 4081 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4082 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4083 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4084 " aaaaaaaaaaaaaaaaaa;", 4085 NoBinPacking); 4086 verifyFormat("a(\"a\"\n" 4087 " \"a\",\n" 4088 " a);"); 4089 4090 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4091 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4092 " aaaaaaaaa,\n" 4093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4094 NoBinPacking); 4095 verifyFormat( 4096 "void f() {\n" 4097 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4098 " .aaaaaaa();\n" 4099 "}", 4100 NoBinPacking); 4101 verifyFormat( 4102 "template <class SomeType, class SomeOtherType>\n" 4103 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4104 NoBinPacking); 4105 } 4106 4107 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4108 FormatStyle Style = getLLVMStyleWithColumns(15); 4109 Style.ExperimentalAutoDetectBinPacking = true; 4110 EXPECT_EQ("aaa(aaaa,\n" 4111 " aaaa,\n" 4112 " aaaa);\n" 4113 "aaa(aaaa,\n" 4114 " aaaa,\n" 4115 " aaaa);", 4116 format("aaa(aaaa,\n" // one-per-line 4117 " aaaa,\n" 4118 " aaaa );\n" 4119 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4120 Style)); 4121 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4122 " aaaa);\n" 4123 "aaa(aaaa, aaaa,\n" 4124 " aaaa);", 4125 format("aaa(aaaa, aaaa,\n" // bin-packed 4126 " aaaa );\n" 4127 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4128 Style)); 4129 } 4130 4131 TEST_F(FormatTest, FormatsBuilderPattern) { 4132 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4133 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4134 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4135 " .StartsWith(\".init\", ORDER_INIT)\n" 4136 " .StartsWith(\".fini\", ORDER_FINI)\n" 4137 " .StartsWith(\".hash\", ORDER_HASH)\n" 4138 " .Default(ORDER_TEXT);\n"); 4139 4140 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4141 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4142 verifyFormat( 4143 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n" 4144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4145 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4146 verifyFormat( 4147 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4148 " aaaaaaaaaaaaaa);"); 4149 verifyFormat( 4150 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4151 " aaaaaa->aaaaaaaaaaaa()\n" 4152 " ->aaaaaaaaaaaaaaaa(\n" 4153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4154 " ->aaaaaaaaaaaaaaaaa();"); 4155 verifyGoogleFormat( 4156 "void f() {\n" 4157 " someo->Add((new util::filetools::Handler(dir))\n" 4158 " ->OnEvent1(NewPermanentCallback(\n" 4159 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4160 " ->OnEvent2(NewPermanentCallback(\n" 4161 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4162 " ->OnEvent3(NewPermanentCallback(\n" 4163 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4164 " ->OnEvent5(NewPermanentCallback(\n" 4165 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4166 " ->OnEvent6(NewPermanentCallback(\n" 4167 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4168 "}"); 4169 4170 verifyFormat( 4171 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4172 verifyFormat("aaaaaaaaaaaaaaa()\n" 4173 " .aaaaaaaaaaaaaaa()\n" 4174 " .aaaaaaaaaaaaaaa()\n" 4175 " .aaaaaaaaaaaaaaa()\n" 4176 " .aaaaaaaaaaaaaaa();"); 4177 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4178 " .aaaaaaaaaaaaaaa()\n" 4179 " .aaaaaaaaaaaaaaa()\n" 4180 " .aaaaaaaaaaaaaaa();"); 4181 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4182 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4183 " .aaaaaaaaaaaaaaa();"); 4184 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4185 " ->aaaaaaaaaaaaaae(0)\n" 4186 " ->aaaaaaaaaaaaaaa();"); 4187 4188 // Don't linewrap after very short segments. 4189 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4190 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4191 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4192 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4193 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4194 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4195 verifyFormat("aaa()\n" 4196 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4197 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4198 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4199 4200 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4201 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4202 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4203 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4204 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4205 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4206 4207 // Prefer not to break after empty parentheses. 4208 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4209 " First->LastNewlineOffset);"); 4210 } 4211 4212 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4213 verifyFormat( 4214 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4215 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4216 verifyFormat( 4217 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4218 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4219 4220 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4221 " ccccccccccccccccccccccccc) {\n}"); 4222 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4223 " ccccccccccccccccccccccccc) {\n}"); 4224 4225 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4226 " ccccccccccccccccccccccccc) {\n}"); 4227 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4228 " ccccccccccccccccccccccccc) {\n}"); 4229 4230 verifyFormat( 4231 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4232 " ccccccccccccccccccccccccc) {\n}"); 4233 verifyFormat( 4234 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4235 " ccccccccccccccccccccccccc) {\n}"); 4236 4237 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4238 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4239 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4240 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4241 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4242 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4243 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4244 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4245 4246 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4247 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4248 " aaaaaaaaaaaaaaa != aa) {\n}"); 4249 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4250 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4251 " aaaaaaaaaaaaaaa != aa) {\n}"); 4252 } 4253 4254 TEST_F(FormatTest, BreaksAfterAssignments) { 4255 verifyFormat( 4256 "unsigned Cost =\n" 4257 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4258 " SI->getPointerAddressSpaceee());\n"); 4259 verifyFormat( 4260 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4261 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4262 4263 verifyFormat( 4264 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4265 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4266 verifyFormat("unsigned OriginalStartColumn =\n" 4267 " SourceMgr.getSpellingColumnNumber(\n" 4268 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4269 " 1;"); 4270 } 4271 4272 TEST_F(FormatTest, AlignsAfterAssignments) { 4273 verifyFormat( 4274 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4275 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4276 verifyFormat( 4277 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4278 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4279 verifyFormat( 4280 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4281 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4282 verifyFormat( 4283 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4284 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4285 verifyFormat( 4286 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4287 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4288 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4289 } 4290 4291 TEST_F(FormatTest, AlignsAfterReturn) { 4292 verifyFormat( 4293 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4294 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4295 verifyFormat( 4296 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4297 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4298 verifyFormat( 4299 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4300 " aaaaaaaaaaaaaaaaaaaaaa();"); 4301 verifyFormat( 4302 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4303 " aaaaaaaaaaaaaaaaaaaaaa());"); 4304 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4305 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4306 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4308 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4309 verifyFormat("return\n" 4310 " // true if code is one of a or b.\n" 4311 " code == a || code == b;"); 4312 } 4313 4314 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4315 verifyFormat( 4316 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4317 " aaaaaaaaa aaaaaaa) {}"); 4318 verifyFormat( 4319 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4320 " aaaaaaaaaaa aaaaaaaaa);"); 4321 verifyFormat( 4322 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4323 " aaaaaaaaaaaaaaaaaaaaa));"); 4324 FormatStyle Style = getLLVMStyle(); 4325 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4326 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4327 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4328 Style); 4329 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4330 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4331 Style); 4332 verifyFormat("SomeLongVariableName->someFunction(\n" 4333 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4334 Style); 4335 verifyFormat( 4336 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4337 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4338 Style); 4339 verifyFormat( 4340 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4341 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4342 Style); 4343 verifyFormat( 4344 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4345 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4346 Style); 4347 } 4348 4349 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4350 FormatStyle Style = getLLVMStyleWithColumns(40); 4351 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4352 " bbbbbbbbbbbbbbbbbbbbbb);", 4353 Style); 4354 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4355 Style.AlignOperands = false; 4356 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4357 " bbbbbbbbbbbbbbbbbbbbbb);", 4358 Style); 4359 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4360 Style.AlignOperands = true; 4361 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4362 " bbbbbbbbbbbbbbbbbbbbbb);", 4363 Style); 4364 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4365 Style.AlignOperands = false; 4366 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4367 " bbbbbbbbbbbbbbbbbbbbbb);", 4368 Style); 4369 } 4370 4371 TEST_F(FormatTest, BreaksConditionalExpressions) { 4372 verifyFormat( 4373 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4374 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4375 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4376 verifyFormat( 4377 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4378 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4379 verifyFormat( 4380 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4381 " : aaaaaaaaaaaaa);"); 4382 verifyFormat( 4383 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4384 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4385 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4386 " aaaaaaaaaaaaa);"); 4387 verifyFormat( 4388 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4389 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4390 " aaaaaaaaaaaaa);"); 4391 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4392 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4394 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4396 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4397 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4398 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4400 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4402 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4403 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4405 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4407 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4408 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4409 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4410 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4411 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4413 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4414 " : aaaaaaaaaaaaaaaa;"); 4415 verifyFormat( 4416 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4417 " ? aaaaaaaaaaaaaaa\n" 4418 " : aaaaaaaaaaaaaaa;"); 4419 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4420 " aaaaaaaaa\n" 4421 " ? b\n" 4422 " : c);"); 4423 verifyFormat("return aaaa == bbbb\n" 4424 " // comment\n" 4425 " ? aaaa\n" 4426 " : bbbb;"); 4427 verifyFormat("unsigned Indent =\n" 4428 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4429 " ? IndentForLevel[TheLine.Level]\n" 4430 " : TheLine * 2,\n" 4431 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4432 getLLVMStyleWithColumns(70)); 4433 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4434 " ? aaaaaaaaaaaaaaa\n" 4435 " : bbbbbbbbbbbbbbb //\n" 4436 " ? ccccccccccccccc\n" 4437 " : ddddddddddddddd;"); 4438 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4439 " ? aaaaaaaaaaaaaaa\n" 4440 " : (bbbbbbbbbbbbbbb //\n" 4441 " ? ccccccccccccccc\n" 4442 " : ddddddddddddddd);"); 4443 verifyFormat( 4444 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4445 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4446 " aaaaaaaaaaaaaaaaaaaaa +\n" 4447 " aaaaaaaaaaaaaaaaaaaaa\n" 4448 " : aaaaaaaaaa;"); 4449 verifyFormat( 4450 "aaaaaa = aaaaaaaaaaaa\n" 4451 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4452 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4453 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4454 4455 FormatStyle NoBinPacking = getLLVMStyle(); 4456 NoBinPacking.BinPackArguments = false; 4457 verifyFormat( 4458 "void f() {\n" 4459 " g(aaa,\n" 4460 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4462 " ? aaaaaaaaaaaaaaa\n" 4463 " : aaaaaaaaaaaaaaa);\n" 4464 "}", 4465 NoBinPacking); 4466 verifyFormat( 4467 "void f() {\n" 4468 " g(aaa,\n" 4469 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4471 " ?: aaaaaaaaaaaaaaa);\n" 4472 "}", 4473 NoBinPacking); 4474 4475 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4476 " // comment.\n" 4477 " ccccccccccccccccccccccccccccccccccccccc\n" 4478 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4479 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4480 4481 // Assignments in conditional expressions. Apparently not uncommon :-(. 4482 verifyFormat("return a != b\n" 4483 " // comment\n" 4484 " ? a = b\n" 4485 " : a = b;"); 4486 verifyFormat("return a != b\n" 4487 " // comment\n" 4488 " ? a = a != b\n" 4489 " // comment\n" 4490 " ? a = b\n" 4491 " : a\n" 4492 " : a;\n"); 4493 verifyFormat("return a != b\n" 4494 " // comment\n" 4495 " ? a\n" 4496 " : a = a != b\n" 4497 " // comment\n" 4498 " ? a = b\n" 4499 " : a;"); 4500 } 4501 4502 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4503 FormatStyle Style = getLLVMStyle(); 4504 Style.BreakBeforeTernaryOperators = false; 4505 Style.ColumnLimit = 70; 4506 verifyFormat( 4507 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4510 Style); 4511 verifyFormat( 4512 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4514 Style); 4515 verifyFormat( 4516 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4517 " aaaaaaaaaaaaa);", 4518 Style); 4519 verifyFormat( 4520 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4521 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4523 " aaaaaaaaaaaaa);", 4524 Style); 4525 verifyFormat( 4526 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4527 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4528 " aaaaaaaaaaaaa);", 4529 Style); 4530 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4535 Style); 4536 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4539 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4541 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4542 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4543 Style); 4544 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4548 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4549 Style); 4550 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4551 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4552 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4553 Style); 4554 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4558 Style); 4559 verifyFormat( 4560 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4561 " aaaaaaaaaaaaaaa :\n" 4562 " aaaaaaaaaaaaaaa;", 4563 Style); 4564 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4565 " aaaaaaaaa ?\n" 4566 " b :\n" 4567 " c);", 4568 Style); 4569 verifyFormat( 4570 "unsigned Indent =\n" 4571 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4572 " IndentForLevel[TheLine.Level] :\n" 4573 " TheLine * 2,\n" 4574 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4575 Style); 4576 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4577 " aaaaaaaaaaaaaaa :\n" 4578 " bbbbbbbbbbbbbbb ? //\n" 4579 " ccccccccccccccc :\n" 4580 " ddddddddddddddd;", 4581 Style); 4582 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4583 " aaaaaaaaaaaaaaa :\n" 4584 " (bbbbbbbbbbbbbbb ? //\n" 4585 " ccccccccccccccc :\n" 4586 " ddddddddddddddd);", 4587 Style); 4588 } 4589 4590 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4591 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4592 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4593 verifyFormat("bool a = true, b = false;"); 4594 4595 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4597 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4598 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4599 verifyFormat( 4600 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4601 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4602 " d = e && f;"); 4603 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4604 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4605 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4606 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4607 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4608 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4609 4610 FormatStyle Style = getGoogleStyle(); 4611 Style.PointerAlignment = FormatStyle::PAS_Left; 4612 Style.DerivePointerAlignment = false; 4613 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4614 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4615 " *b = bbbbbbbbbbbbbbbbbbb;", 4616 Style); 4617 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4618 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4619 Style); 4620 } 4621 4622 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4623 verifyFormat("arr[foo ? bar : baz];"); 4624 verifyFormat("f()[foo ? bar : baz];"); 4625 verifyFormat("(a + b)[foo ? bar : baz];"); 4626 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4627 } 4628 4629 TEST_F(FormatTest, AlignsStringLiterals) { 4630 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4631 " \"short literal\");"); 4632 verifyFormat( 4633 "looooooooooooooooooooooooongFunction(\n" 4634 " \"short literal\"\n" 4635 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4636 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4637 " \" string literals\",\n" 4638 " and, other, parameters);"); 4639 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4640 " \"5678\";", 4641 format("fun + \"1243\" /* comment */\n" 4642 " \"5678\";", 4643 getLLVMStyleWithColumns(28))); 4644 EXPECT_EQ( 4645 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4646 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4647 " \"aaaaaaaaaaaaaaaa\";", 4648 format("aaaaaa =" 4649 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4650 "aaaaaaaaaaaaaaaaaaaaa\" " 4651 "\"aaaaaaaaaaaaaaaa\";")); 4652 verifyFormat("a = a + \"a\"\n" 4653 " \"a\"\n" 4654 " \"a\";"); 4655 verifyFormat("f(\"a\", \"b\"\n" 4656 " \"c\");"); 4657 4658 verifyFormat( 4659 "#define LL_FORMAT \"ll\"\n" 4660 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4661 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4662 4663 verifyFormat("#define A(X) \\\n" 4664 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4665 " \"ccccc\"", 4666 getLLVMStyleWithColumns(23)); 4667 verifyFormat("#define A \"def\"\n" 4668 "f(\"abc\" A \"ghi\"\n" 4669 " \"jkl\");"); 4670 4671 verifyFormat("f(L\"a\"\n" 4672 " L\"b\");"); 4673 verifyFormat("#define A(X) \\\n" 4674 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4675 " L\"ccccc\"", 4676 getLLVMStyleWithColumns(25)); 4677 4678 verifyFormat("f(@\"a\"\n" 4679 " @\"b\");"); 4680 verifyFormat("NSString s = @\"a\"\n" 4681 " @\"b\"\n" 4682 " @\"c\";"); 4683 verifyFormat("NSString s = @\"a\"\n" 4684 " \"b\"\n" 4685 " \"c\";"); 4686 } 4687 4688 TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) { 4689 FormatStyle Style = getLLVMStyle(); 4690 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel; 4691 verifyFormat("class C {\n" 4692 " int f() { return 1; }\n" 4693 "};\n" 4694 "int\n" 4695 "f() {\n" 4696 " return 1;\n" 4697 "}", 4698 Style); 4699 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 4700 verifyFormat("class C {\n" 4701 " int\n" 4702 " f() {\n" 4703 " return 1;\n" 4704 " }\n" 4705 "};\n" 4706 "int\n" 4707 "f() {\n" 4708 " return 1;\n" 4709 "}", 4710 Style); 4711 verifyFormat("const char *\n" 4712 "f(void) {\n" // Break here. 4713 " return \"\";\n" 4714 "}\n" 4715 "const char *bar(void);\n", // No break here. 4716 Style); 4717 verifyFormat("template <class T>\n" 4718 "T *\n" 4719 "f(T &c) {\n" // Break here. 4720 " return NULL;\n" 4721 "}\n" 4722 "template <class T> T *f(T &c);\n", // No break here. 4723 Style); 4724 verifyFormat("class C {\n" 4725 " int\n" 4726 " operator+() {\n" 4727 " return 1;\n" 4728 " }\n" 4729 " int\n" 4730 " operator()() {\n" 4731 " return 1;\n" 4732 " }\n" 4733 "};\n", 4734 Style); 4735 verifyFormat("void\n" 4736 "A::operator()() {}\n" 4737 "void\n" 4738 "A::operator>>() {}\n" 4739 "void\n" 4740 "A::operator+() {}\n", 4741 Style); 4742 verifyFormat("void *operator new(std::size_t s);", // No break here. 4743 Style); 4744 verifyFormat("void *\n" 4745 "operator new(std::size_t s) {}", 4746 Style); 4747 verifyFormat("void *\n" 4748 "operator delete[](void *ptr) {}", 4749 Style); 4750 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4751 verifyFormat("const char *\n" 4752 "f(void)\n" // Break here. 4753 "{\n" 4754 " return \"\";\n" 4755 "}\n" 4756 "const char *bar(void);\n", // No break here. 4757 Style); 4758 verifyFormat("template <class T>\n" 4759 "T *\n" // Problem here: no line break 4760 "f(T &c)\n" // Break here. 4761 "{\n" 4762 " return NULL;\n" 4763 "}\n" 4764 "template <class T> T *f(T &c);\n", // No break here. 4765 Style); 4766 } 4767 4768 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4769 FormatStyle NoBreak = getLLVMStyle(); 4770 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4771 FormatStyle Break = getLLVMStyle(); 4772 Break.AlwaysBreakBeforeMultilineStrings = true; 4773 verifyFormat("aaaa = \"bbbb\"\n" 4774 " \"cccc\";", 4775 NoBreak); 4776 verifyFormat("aaaa =\n" 4777 " \"bbbb\"\n" 4778 " \"cccc\";", 4779 Break); 4780 verifyFormat("aaaa(\"bbbb\"\n" 4781 " \"cccc\");", 4782 NoBreak); 4783 verifyFormat("aaaa(\n" 4784 " \"bbbb\"\n" 4785 " \"cccc\");", 4786 Break); 4787 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4788 " \"cccc\");", 4789 NoBreak); 4790 verifyFormat("aaaa(qqq,\n" 4791 " \"bbbb\"\n" 4792 " \"cccc\");", 4793 Break); 4794 verifyFormat("aaaa(qqq,\n" 4795 " L\"bbbb\"\n" 4796 " L\"cccc\");", 4797 Break); 4798 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4799 " \"bbbb\"));", 4800 Break); 4801 verifyFormat("string s = someFunction(\n" 4802 " \"abc\"\n" 4803 " \"abc\");", 4804 Break); 4805 4806 // As we break before unary operators, breaking right after them is bad. 4807 verifyFormat("string foo = abc ? \"x\"\n" 4808 " \"blah blah blah blah blah blah\"\n" 4809 " : \"y\";", 4810 Break); 4811 4812 // Don't break if there is no column gain. 4813 verifyFormat("f(\"aaaa\"\n" 4814 " \"bbbb\");", 4815 Break); 4816 4817 // Treat literals with escaped newlines like multi-line string literals. 4818 EXPECT_EQ("x = \"a\\\n" 4819 "b\\\n" 4820 "c\";", 4821 format("x = \"a\\\n" 4822 "b\\\n" 4823 "c\";", 4824 NoBreak)); 4825 EXPECT_EQ("xxxx =\n" 4826 " \"a\\\n" 4827 "b\\\n" 4828 "c\";", 4829 format("xxxx = \"a\\\n" 4830 "b\\\n" 4831 "c\";", 4832 Break)); 4833 4834 // Exempt ObjC strings for now. 4835 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 4836 " @\"bbbb\";", 4837 format("NSString *const kString = @\"aaaa\"\n" 4838 "@\"bbbb\";", 4839 Break)); 4840 4841 Break.ColumnLimit = 0; 4842 verifyFormat("const char *hello = \"hello llvm\";", Break); 4843 } 4844 4845 TEST_F(FormatTest, AlignsPipes) { 4846 verifyFormat( 4847 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4848 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4849 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4850 verifyFormat( 4851 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4852 " << aaaaaaaaaaaaaaaaaaaa;"); 4853 verifyFormat( 4854 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4855 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4856 verifyFormat( 4857 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4858 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4859 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4860 verifyFormat( 4861 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4862 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4863 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4864 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4867 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4868 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4869 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4870 verifyFormat( 4871 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4873 4874 verifyFormat("return out << \"somepacket = {\\n\"\n" 4875 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4876 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4877 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4878 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4879 " << \"}\";"); 4880 4881 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4882 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4883 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4884 verifyFormat( 4885 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4886 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4887 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4888 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4889 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4890 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4891 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4892 verifyFormat( 4893 "void f() {\n" 4894 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4895 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4896 "}"); 4897 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4898 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4899 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4901 " aaaaaaaaaaaaaaaaaaaaa)\n" 4902 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4903 verifyFormat("LOG_IF(aaa == //\n" 4904 " bbb)\n" 4905 " << a << b;"); 4906 4907 // Breaking before the first "<<" is generally not desirable. 4908 verifyFormat( 4909 "llvm::errs()\n" 4910 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4911 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4912 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4913 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4914 getLLVMStyleWithColumns(70)); 4915 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4916 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4917 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4918 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4919 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4920 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4921 getLLVMStyleWithColumns(70)); 4922 4923 // But sometimes, breaking before the first "<<" is desirable. 4924 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4925 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4926 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4927 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4928 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4929 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4930 " << BEF << IsTemplate << Description << E->getType();"); 4931 4932 verifyFormat( 4933 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4934 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4935 4936 // Incomplete string literal. 4937 EXPECT_EQ("llvm::errs() << \"\n" 4938 " << a;", 4939 format("llvm::errs() << \"\n<<a;")); 4940 4941 verifyFormat("void f() {\n" 4942 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4943 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4944 "}"); 4945 4946 // Handle 'endl'. 4947 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4948 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4949 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4950 } 4951 4952 TEST_F(FormatTest, UnderstandsEquals) { 4953 verifyFormat( 4954 "aaaaaaaaaaaaaaaaa =\n" 4955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4956 verifyFormat( 4957 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4959 verifyFormat( 4960 "if (a) {\n" 4961 " f();\n" 4962 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4964 "}"); 4965 4966 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4967 " 100000000 + 10000000) {\n}"); 4968 } 4969 4970 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4971 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4972 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4973 4974 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4975 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4976 4977 verifyFormat( 4978 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4979 " Parameter2);"); 4980 4981 verifyFormat( 4982 "ShortObject->shortFunction(\n" 4983 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4984 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4985 4986 verifyFormat("loooooooooooooongFunction(\n" 4987 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4988 4989 verifyFormat( 4990 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4991 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4992 4993 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4994 " .WillRepeatedly(Return(SomeValue));"); 4995 verifyFormat("void f() {\n" 4996 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4997 " .Times(2)\n" 4998 " .WillRepeatedly(Return(SomeValue));\n" 4999 "}"); 5000 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5001 " ccccccccccccccccccccccc);"); 5002 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5004 " .aaaaa(aaaaa),\n" 5005 " aaaaaaaaaaaaaaaaaaaaa);"); 5006 verifyFormat("void f() {\n" 5007 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5008 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5009 "}"); 5010 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5012 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5013 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5014 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5015 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5016 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5017 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5018 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5019 "}"); 5020 5021 // Here, it is not necessary to wrap at "." or "->". 5022 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5023 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5024 verifyFormat( 5025 "aaaaaaaaaaa->aaaaaaaaa(\n" 5026 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5027 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5028 5029 verifyFormat( 5030 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5032 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5033 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5034 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5035 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5036 5037 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5039 " .a();"); 5040 5041 FormatStyle NoBinPacking = getLLVMStyle(); 5042 NoBinPacking.BinPackParameters = false; 5043 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5044 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5045 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5046 " aaaaaaaaaaaaaaaaaaa,\n" 5047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5048 NoBinPacking); 5049 5050 // If there is a subsequent call, change to hanging indentation. 5051 verifyFormat( 5052 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5053 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5054 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5055 verifyFormat( 5056 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5057 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5058 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5060 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5061 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5063 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5064 } 5065 5066 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5067 verifyFormat("template <typename T>\n" 5068 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5069 verifyFormat("template <typename T>\n" 5070 "// T should be one of {A, B}.\n" 5071 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5072 verifyFormat( 5073 "template <typename T>\n" 5074 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5075 verifyFormat("template <typename T>\n" 5076 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5077 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5078 verifyFormat( 5079 "template <typename T>\n" 5080 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5081 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5082 verifyFormat( 5083 "template <typename T>\n" 5084 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5085 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5087 verifyFormat("template <typename T>\n" 5088 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5089 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5090 verifyFormat( 5091 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5092 " typename T4 = char>\n" 5093 "void f();"); 5094 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5095 " template <typename> class cccccccccccccccccccccc,\n" 5096 " typename ddddddddddddd>\n" 5097 "class C {};"); 5098 verifyFormat( 5099 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5101 5102 verifyFormat("void f() {\n" 5103 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5104 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5105 "}"); 5106 5107 verifyFormat("template <typename T> class C {};"); 5108 verifyFormat("template <typename T> void f();"); 5109 verifyFormat("template <typename T> void f() {}"); 5110 verifyFormat( 5111 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5114 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5117 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5118 getLLVMStyleWithColumns(72)); 5119 EXPECT_EQ("static_cast<A< //\n" 5120 " B> *>(\n" 5121 "\n" 5122 " );", 5123 format("static_cast<A<//\n" 5124 " B>*>(\n" 5125 "\n" 5126 " );")); 5127 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5128 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5129 5130 FormatStyle AlwaysBreak = getLLVMStyle(); 5131 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5132 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5133 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5134 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5135 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5136 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5137 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5138 verifyFormat("template <template <typename> class Fooooooo,\n" 5139 " template <typename> class Baaaaaaar>\n" 5140 "struct C {};", 5141 AlwaysBreak); 5142 verifyFormat("template <typename T> // T can be A, B or C.\n" 5143 "struct C {};", 5144 AlwaysBreak); 5145 } 5146 5147 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5148 verifyFormat( 5149 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5151 verifyFormat( 5152 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5155 5156 // FIXME: Should we have the extra indent after the second break? 5157 verifyFormat( 5158 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5161 5162 verifyFormat( 5163 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5164 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5165 5166 // Breaking at nested name specifiers is generally not desirable. 5167 verifyFormat( 5168 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5169 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5170 5171 verifyFormat( 5172 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5174 " aaaaaaaaaaaaaaaaaaaaa);", 5175 getLLVMStyleWithColumns(74)); 5176 5177 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5179 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5180 } 5181 5182 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5183 verifyFormat("A<int> a;"); 5184 verifyFormat("A<A<A<int>>> a;"); 5185 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5186 verifyFormat("bool x = a < 1 || 2 > a;"); 5187 verifyFormat("bool x = 5 < f<int>();"); 5188 verifyFormat("bool x = f<int>() > 5;"); 5189 verifyFormat("bool x = 5 < a<int>::x;"); 5190 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5191 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5192 5193 verifyGoogleFormat("A<A<int>> a;"); 5194 verifyGoogleFormat("A<A<A<int>>> a;"); 5195 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5196 verifyGoogleFormat("A<A<int> > a;"); 5197 verifyGoogleFormat("A<A<A<int> > > a;"); 5198 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5199 verifyGoogleFormat("A<::A<int>> a;"); 5200 verifyGoogleFormat("A<::A> a;"); 5201 verifyGoogleFormat("A< ::A> a;"); 5202 verifyGoogleFormat("A< ::A<int> > a;"); 5203 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5204 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5205 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5206 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5207 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5208 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5209 5210 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5211 5212 verifyFormat("test >> a >> b;"); 5213 verifyFormat("test << a >> b;"); 5214 5215 verifyFormat("f<int>();"); 5216 verifyFormat("template <typename T> void f() {}"); 5217 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5218 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5219 "sizeof(char)>::type>;"); 5220 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5221 5222 // Not template parameters. 5223 verifyFormat("return a < b && c > d;"); 5224 verifyFormat("void f() {\n" 5225 " while (a < b && c > d) {\n" 5226 " }\n" 5227 "}"); 5228 verifyFormat("template <typename... Types>\n" 5229 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5230 5231 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5233 getLLVMStyleWithColumns(60)); 5234 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5235 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5236 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5237 } 5238 5239 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5240 verifyFormat("COMPARE(a, ==, b);"); 5241 } 5242 5243 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5244 verifyFormat("int A::*x;"); 5245 verifyFormat("int (S::*func)(void *);"); 5246 verifyFormat("void f() { int (S::*func)(void *); }"); 5247 verifyFormat("typedef bool *(Class::*Member)() const;"); 5248 verifyFormat("void f() {\n" 5249 " (a->*f)();\n" 5250 " a->*x;\n" 5251 " (a.*f)();\n" 5252 " ((*a).*f)();\n" 5253 " a.*x;\n" 5254 "}"); 5255 verifyFormat("void f() {\n" 5256 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5257 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5258 "}"); 5259 verifyFormat( 5260 "(aaaaaaaaaa->*bbbbbbb)(\n" 5261 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5262 FormatStyle Style = getLLVMStyle(); 5263 Style.PointerAlignment = FormatStyle::PAS_Left; 5264 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5265 } 5266 5267 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5268 verifyFormat("int a = -2;"); 5269 verifyFormat("f(-1, -2, -3);"); 5270 verifyFormat("a[-1] = 5;"); 5271 verifyFormat("int a = 5 + -2;"); 5272 verifyFormat("if (i == -1) {\n}"); 5273 verifyFormat("if (i != -1) {\n}"); 5274 verifyFormat("if (i > -1) {\n}"); 5275 verifyFormat("if (i < -1) {\n}"); 5276 verifyFormat("++(a->f());"); 5277 verifyFormat("--(a->f());"); 5278 verifyFormat("(a->f())++;"); 5279 verifyFormat("a[42]++;"); 5280 verifyFormat("if (!(a->f())) {\n}"); 5281 5282 verifyFormat("a-- > b;"); 5283 verifyFormat("b ? -a : c;"); 5284 verifyFormat("n * sizeof char16;"); 5285 verifyFormat("n * alignof char16;", getGoogleStyle()); 5286 verifyFormat("sizeof(char);"); 5287 verifyFormat("alignof(char);", getGoogleStyle()); 5288 5289 verifyFormat("return -1;"); 5290 verifyFormat("switch (a) {\n" 5291 "case -1:\n" 5292 " break;\n" 5293 "}"); 5294 verifyFormat("#define X -1"); 5295 verifyFormat("#define X -kConstant"); 5296 5297 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5298 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5299 5300 verifyFormat("int a = /* confusing comment */ -1;"); 5301 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5302 verifyFormat("int a = i /* confusing comment */++;"); 5303 } 5304 5305 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5306 verifyFormat("if (!aaaaaaaaaa( // break\n" 5307 " aaaaa)) {\n" 5308 "}"); 5309 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5310 " aaaaa));"); 5311 verifyFormat("*aaa = aaaaaaa( // break\n" 5312 " bbbbbb);"); 5313 } 5314 5315 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5316 verifyFormat("bool operator<();"); 5317 verifyFormat("bool operator>();"); 5318 verifyFormat("bool operator=();"); 5319 verifyFormat("bool operator==();"); 5320 verifyFormat("bool operator!=();"); 5321 verifyFormat("int operator+();"); 5322 verifyFormat("int operator++();"); 5323 verifyFormat("bool operator();"); 5324 verifyFormat("bool operator()();"); 5325 verifyFormat("bool operator[]();"); 5326 verifyFormat("operator bool();"); 5327 verifyFormat("operator int();"); 5328 verifyFormat("operator void *();"); 5329 verifyFormat("operator SomeType<int>();"); 5330 verifyFormat("operator SomeType<int, int>();"); 5331 verifyFormat("operator SomeType<SomeType<int>>();"); 5332 verifyFormat("void *operator new(std::size_t size);"); 5333 verifyFormat("void *operator new[](std::size_t size);"); 5334 verifyFormat("void operator delete(void *ptr);"); 5335 verifyFormat("void operator delete[](void *ptr);"); 5336 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5337 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5338 5339 verifyFormat( 5340 "ostream &operator<<(ostream &OutputStream,\n" 5341 " SomeReallyLongType WithSomeReallyLongValue);"); 5342 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5343 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5344 " return left.group < right.group;\n" 5345 "}"); 5346 verifyFormat("SomeType &operator=(const SomeType &S);"); 5347 verifyFormat("f.template operator()<int>();"); 5348 5349 verifyGoogleFormat("operator void*();"); 5350 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5351 verifyGoogleFormat("operator ::A();"); 5352 5353 verifyFormat("using A::operator+;"); 5354 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5355 "int i;"); 5356 } 5357 5358 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5359 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5360 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5361 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5362 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5363 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5364 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5365 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5366 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5367 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5368 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5369 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5370 5371 FormatStyle AlignLeft = getLLVMStyle(); 5372 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5373 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5374 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5375 AlignLeft); 5376 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5377 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5378 5379 FormatStyle Spaces = getLLVMStyle(); 5380 Spaces.SpacesInCStyleCastParentheses = true; 5381 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5382 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5383 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5384 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5385 5386 Spaces.SpacesInCStyleCastParentheses = false; 5387 Spaces.SpacesInParentheses = true; 5388 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5389 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5390 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5391 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5392 } 5393 5394 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5395 verifyFormat("void f() {\n" 5396 " A *a = new A;\n" 5397 " A *a = new (placement) A;\n" 5398 " delete a;\n" 5399 " delete (A *)a;\n" 5400 "}"); 5401 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5402 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5403 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5404 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5405 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5406 verifyFormat("delete[] h->p;"); 5407 } 5408 5409 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5410 verifyFormat("int *f(int *a) {}"); 5411 verifyFormat("int main(int argc, char **argv) {}"); 5412 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5413 verifyIndependentOfContext("f(a, *a);"); 5414 verifyFormat("void g() { f(*a); }"); 5415 verifyIndependentOfContext("int a = b * 10;"); 5416 verifyIndependentOfContext("int a = 10 * b;"); 5417 verifyIndependentOfContext("int a = b * c;"); 5418 verifyIndependentOfContext("int a += b * c;"); 5419 verifyIndependentOfContext("int a -= b * c;"); 5420 verifyIndependentOfContext("int a *= b * c;"); 5421 verifyIndependentOfContext("int a /= b * c;"); 5422 verifyIndependentOfContext("int a = *b;"); 5423 verifyIndependentOfContext("int a = *b * c;"); 5424 verifyIndependentOfContext("int a = b * *c;"); 5425 verifyIndependentOfContext("int a = b * (10);"); 5426 verifyIndependentOfContext("S << b * (10);"); 5427 verifyIndependentOfContext("return 10 * b;"); 5428 verifyIndependentOfContext("return *b * *c;"); 5429 verifyIndependentOfContext("return a & ~b;"); 5430 verifyIndependentOfContext("f(b ? *c : *d);"); 5431 verifyIndependentOfContext("int a = b ? *c : *d;"); 5432 verifyIndependentOfContext("*b = a;"); 5433 verifyIndependentOfContext("a * ~b;"); 5434 verifyIndependentOfContext("a * !b;"); 5435 verifyIndependentOfContext("a * +b;"); 5436 verifyIndependentOfContext("a * -b;"); 5437 verifyIndependentOfContext("a * ++b;"); 5438 verifyIndependentOfContext("a * --b;"); 5439 verifyIndependentOfContext("a[4] * b;"); 5440 verifyIndependentOfContext("a[a * a] = 1;"); 5441 verifyIndependentOfContext("f() * b;"); 5442 verifyIndependentOfContext("a * [self dostuff];"); 5443 verifyIndependentOfContext("int x = a * (a + b);"); 5444 verifyIndependentOfContext("(a *)(a + b);"); 5445 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5446 verifyIndependentOfContext("int *pa = (int *)&a;"); 5447 verifyIndependentOfContext("return sizeof(int **);"); 5448 verifyIndependentOfContext("return sizeof(int ******);"); 5449 verifyIndependentOfContext("return (int **&)a;"); 5450 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5451 verifyFormat("void f(Type (*parameter)[10]) {}"); 5452 verifyFormat("void f(Type (¶meter)[10]) {}"); 5453 verifyGoogleFormat("return sizeof(int**);"); 5454 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5455 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5456 verifyFormat("auto a = [](int **&, int ***) {};"); 5457 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5458 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5459 verifyFormat("[](const decltype(*a) &value) {}"); 5460 verifyFormat("decltype(a * b) F();"); 5461 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5462 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5463 verifyIndependentOfContext("int i{a * b};"); 5464 verifyIndependentOfContext("aaa && aaa->f();"); 5465 verifyIndependentOfContext("int x = ~*p;"); 5466 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5467 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5468 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5469 verifyFormat("void f() { f(a, c * d); }"); 5470 verifyFormat("void f() { f(new a(), c * d); }"); 5471 5472 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5473 5474 verifyIndependentOfContext("A<int *> a;"); 5475 verifyIndependentOfContext("A<int **> a;"); 5476 verifyIndependentOfContext("A<int *, int *> a;"); 5477 verifyIndependentOfContext("A<int *[]> a;"); 5478 verifyIndependentOfContext( 5479 "const char *const p = reinterpret_cast<const char *const>(q);"); 5480 verifyIndependentOfContext("A<int **, int **> a;"); 5481 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5482 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5483 verifyFormat("for (; a && b;) {\n}"); 5484 verifyFormat("bool foo = true && [] { return false; }();"); 5485 5486 verifyFormat( 5487 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5489 5490 verifyGoogleFormat("**outparam = 1;"); 5491 verifyGoogleFormat("*outparam = a * b;"); 5492 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5493 verifyGoogleFormat("A<int*> a;"); 5494 verifyGoogleFormat("A<int**> a;"); 5495 verifyGoogleFormat("A<int*, int*> a;"); 5496 verifyGoogleFormat("A<int**, int**> a;"); 5497 verifyGoogleFormat("f(b ? *c : *d);"); 5498 verifyGoogleFormat("int a = b ? *c : *d;"); 5499 verifyGoogleFormat("Type* t = **x;"); 5500 verifyGoogleFormat("Type* t = *++*x;"); 5501 verifyGoogleFormat("*++*x;"); 5502 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5503 verifyGoogleFormat("Type* t = x++ * y;"); 5504 verifyGoogleFormat( 5505 "const char* const p = reinterpret_cast<const char* const>(q);"); 5506 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5507 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5508 verifyGoogleFormat("template <typename T>\n" 5509 "void f(int i = 0, SomeType** temps = NULL);"); 5510 5511 FormatStyle Left = getLLVMStyle(); 5512 Left.PointerAlignment = FormatStyle::PAS_Left; 5513 verifyFormat("x = *a(x) = *a(y);", Left); 5514 verifyFormat("for (;; * = b) {\n}", Left); 5515 5516 verifyIndependentOfContext("a = *(x + y);"); 5517 verifyIndependentOfContext("a = &(x + y);"); 5518 verifyIndependentOfContext("*(x + y).call();"); 5519 verifyIndependentOfContext("&(x + y)->call();"); 5520 verifyFormat("void f() { &(*I).first; }"); 5521 5522 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5523 verifyFormat( 5524 "int *MyValues = {\n" 5525 " *A, // Operator detection might be confused by the '{'\n" 5526 " *BB // Operator detection might be confused by previous comment\n" 5527 "};"); 5528 5529 verifyIndependentOfContext("if (int *a = &b)"); 5530 verifyIndependentOfContext("if (int &a = *b)"); 5531 verifyIndependentOfContext("if (a & b[i])"); 5532 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5533 verifyIndependentOfContext("if (*b[i])"); 5534 verifyIndependentOfContext("if (int *a = (&b))"); 5535 verifyIndependentOfContext("while (int *a = &b)"); 5536 verifyIndependentOfContext("size = sizeof *a;"); 5537 verifyIndependentOfContext("if (a && (b = c))"); 5538 verifyFormat("void f() {\n" 5539 " for (const int &v : Values) {\n" 5540 " }\n" 5541 "}"); 5542 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5543 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5544 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5545 5546 verifyFormat("#define A (!a * b)"); 5547 verifyFormat("#define MACRO \\\n" 5548 " int *i = a * b; \\\n" 5549 " void f(a *b);", 5550 getLLVMStyleWithColumns(19)); 5551 5552 verifyIndependentOfContext("A = new SomeType *[Length];"); 5553 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5554 verifyIndependentOfContext("T **t = new T *;"); 5555 verifyIndependentOfContext("T **t = new T *();"); 5556 verifyGoogleFormat("A = new SomeType*[Length]();"); 5557 verifyGoogleFormat("A = new SomeType*[Length];"); 5558 verifyGoogleFormat("T** t = new T*;"); 5559 verifyGoogleFormat("T** t = new T*();"); 5560 5561 FormatStyle PointerLeft = getLLVMStyle(); 5562 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5563 verifyFormat("delete *x;", PointerLeft); 5564 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5565 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5566 verifyFormat("template <bool a, bool b> " 5567 "typename t::if<x && y>::type f() {}"); 5568 verifyFormat("template <int *y> f() {}"); 5569 verifyFormat("vector<int *> v;"); 5570 verifyFormat("vector<int *const> v;"); 5571 verifyFormat("vector<int *const **const *> v;"); 5572 verifyFormat("vector<int *volatile> v;"); 5573 verifyFormat("vector<a * b> v;"); 5574 verifyFormat("foo<b && false>();"); 5575 verifyFormat("foo<b & 1>();"); 5576 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5577 verifyFormat( 5578 "template <class T, class = typename std::enable_if<\n" 5579 " std::is_integral<T>::value &&\n" 5580 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5581 "void F();", 5582 getLLVMStyleWithColumns(76)); 5583 verifyFormat( 5584 "template <class T,\n" 5585 " class = typename ::std::enable_if<\n" 5586 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5587 "void F();", 5588 getGoogleStyleWithColumns(68)); 5589 5590 verifyIndependentOfContext("MACRO(int *i);"); 5591 verifyIndependentOfContext("MACRO(auto *a);"); 5592 verifyIndependentOfContext("MACRO(const A *a);"); 5593 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5594 // FIXME: Is there a way to make this work? 5595 // verifyIndependentOfContext("MACRO(A *a);"); 5596 5597 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5598 verifyFormat("return options != nullptr && operator==(*options);"); 5599 5600 EXPECT_EQ("#define OP(x) \\\n" 5601 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5602 " return s << a.DebugString(); \\\n" 5603 " }", 5604 format("#define OP(x) \\\n" 5605 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5606 " return s << a.DebugString(); \\\n" 5607 " }", 5608 getLLVMStyleWithColumns(50))); 5609 5610 // FIXME: We cannot handle this case yet; we might be able to figure out that 5611 // foo<x> d > v; doesn't make sense. 5612 verifyFormat("foo<a<b && c> d> v;"); 5613 5614 FormatStyle PointerMiddle = getLLVMStyle(); 5615 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5616 verifyFormat("delete *x;", PointerMiddle); 5617 verifyFormat("int * x;", PointerMiddle); 5618 verifyFormat("template <int * y> f() {}", PointerMiddle); 5619 verifyFormat("int * f(int * a) {}", PointerMiddle); 5620 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5621 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5622 verifyFormat("A<int *> a;", PointerMiddle); 5623 verifyFormat("A<int **> a;", PointerMiddle); 5624 verifyFormat("A<int *, int *> a;", PointerMiddle); 5625 verifyFormat("A<int * []> a;", PointerMiddle); 5626 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5627 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5628 verifyFormat("T ** t = new T *;", PointerMiddle); 5629 5630 // Member function reference qualifiers aren't binary operators. 5631 verifyFormat("string // break\n" 5632 "operator()() & {}"); 5633 verifyFormat("string // break\n" 5634 "operator()() && {}"); 5635 verifyGoogleFormat("template <typename T>\n" 5636 "auto x() & -> int {}"); 5637 } 5638 5639 TEST_F(FormatTest, UnderstandsAttributes) { 5640 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5641 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5642 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5643 FormatStyle AfterType = getLLVMStyle(); 5644 AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 5645 verifyFormat("__attribute__((nodebug)) void\n" 5646 "foo() {}\n", 5647 AfterType); 5648 } 5649 5650 TEST_F(FormatTest, UnderstandsEllipsis) { 5651 verifyFormat("int printf(const char *fmt, ...);"); 5652 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5653 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5654 5655 FormatStyle PointersLeft = getLLVMStyle(); 5656 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5657 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5658 } 5659 5660 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5661 EXPECT_EQ("int *a;\n" 5662 "int *a;\n" 5663 "int *a;", 5664 format("int *a;\n" 5665 "int* a;\n" 5666 "int *a;", 5667 getGoogleStyle())); 5668 EXPECT_EQ("int* a;\n" 5669 "int* a;\n" 5670 "int* a;", 5671 format("int* a;\n" 5672 "int* a;\n" 5673 "int *a;", 5674 getGoogleStyle())); 5675 EXPECT_EQ("int *a;\n" 5676 "int *a;\n" 5677 "int *a;", 5678 format("int *a;\n" 5679 "int * a;\n" 5680 "int * a;", 5681 getGoogleStyle())); 5682 EXPECT_EQ("auto x = [] {\n" 5683 " int *a;\n" 5684 " int *a;\n" 5685 " int *a;\n" 5686 "};", 5687 format("auto x=[]{int *a;\n" 5688 "int * a;\n" 5689 "int * a;};", 5690 getGoogleStyle())); 5691 } 5692 5693 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5694 verifyFormat("int f(int &&a) {}"); 5695 verifyFormat("int f(int a, char &&b) {}"); 5696 verifyFormat("void f() { int &&a = b; }"); 5697 verifyGoogleFormat("int f(int a, char&& b) {}"); 5698 verifyGoogleFormat("void f() { int&& a = b; }"); 5699 5700 verifyIndependentOfContext("A<int &&> a;"); 5701 verifyIndependentOfContext("A<int &&, int &&> a;"); 5702 verifyGoogleFormat("A<int&&> a;"); 5703 verifyGoogleFormat("A<int&&, int&&> a;"); 5704 5705 // Not rvalue references: 5706 verifyFormat("template <bool B, bool C> class A {\n" 5707 " static_assert(B && C, \"Something is wrong\");\n" 5708 "};"); 5709 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5710 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5711 verifyFormat("#define A(a, b) (a && b)"); 5712 } 5713 5714 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5715 verifyFormat("void f() {\n" 5716 " x[aaaaaaaaa -\n" 5717 " b] = 23;\n" 5718 "}", 5719 getLLVMStyleWithColumns(15)); 5720 } 5721 5722 TEST_F(FormatTest, FormatsCasts) { 5723 verifyFormat("Type *A = static_cast<Type *>(P);"); 5724 verifyFormat("Type *A = (Type *)P;"); 5725 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5726 verifyFormat("int a = (int)(2.0f);"); 5727 verifyFormat("int a = (int)2.0f;"); 5728 verifyFormat("x[(int32)y];"); 5729 verifyFormat("x = (int32)y;"); 5730 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5731 verifyFormat("int a = (int)*b;"); 5732 verifyFormat("int a = (int)2.0f;"); 5733 verifyFormat("int a = (int)~0;"); 5734 verifyFormat("int a = (int)++a;"); 5735 verifyFormat("int a = (int)sizeof(int);"); 5736 verifyFormat("int a = (int)+2;"); 5737 verifyFormat("my_int a = (my_int)2.0f;"); 5738 verifyFormat("my_int a = (my_int)sizeof(int);"); 5739 verifyFormat("return (my_int)aaa;"); 5740 verifyFormat("#define x ((int)-1)"); 5741 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5742 verifyFormat("#define p(q) ((int *)&q)"); 5743 verifyFormat("fn(a)(b) + 1;"); 5744 5745 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5746 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5747 verifyFormat("my_int a = (my_int)~0;"); 5748 verifyFormat("my_int a = (my_int)++a;"); 5749 verifyFormat("my_int a = (my_int)-2;"); 5750 verifyFormat("my_int a = (my_int)1;"); 5751 verifyFormat("my_int a = (my_int *)1;"); 5752 verifyFormat("my_int a = (const my_int)-1;"); 5753 verifyFormat("my_int a = (const my_int *)-1;"); 5754 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5755 verifyFormat("my_int a = (ns::my_int)-2;"); 5756 verifyFormat("case (my_int)ONE:"); 5757 5758 // FIXME: single value wrapped with paren will be treated as cast. 5759 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5760 5761 verifyFormat("{ (void)F; }"); 5762 5763 // Don't break after a cast's 5764 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5765 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5766 " bbbbbbbbbbbbbbbbbbbbbb);"); 5767 5768 // These are not casts. 5769 verifyFormat("void f(int *) {}"); 5770 verifyFormat("f(foo)->b;"); 5771 verifyFormat("f(foo).b;"); 5772 verifyFormat("f(foo)(b);"); 5773 verifyFormat("f(foo)[b];"); 5774 verifyFormat("[](foo) { return 4; }(bar);"); 5775 verifyFormat("(*funptr)(foo)[4];"); 5776 verifyFormat("funptrs[4](foo)[4];"); 5777 verifyFormat("void f(int *);"); 5778 verifyFormat("void f(int *) = 0;"); 5779 verifyFormat("void f(SmallVector<int>) {}"); 5780 verifyFormat("void f(SmallVector<int>);"); 5781 verifyFormat("void f(SmallVector<int>) = 0;"); 5782 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5783 verifyFormat("int a = sizeof(int) * b;"); 5784 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5785 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5786 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5787 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5788 5789 // These are not casts, but at some point were confused with casts. 5790 verifyFormat("virtual void foo(int *) override;"); 5791 verifyFormat("virtual void foo(char &) const;"); 5792 verifyFormat("virtual void foo(int *a, char *) const;"); 5793 verifyFormat("int a = sizeof(int *) + b;"); 5794 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5795 verifyFormat("bool b = f(g<int>) && c;"); 5796 verifyFormat("typedef void (*f)(int i) func;"); 5797 5798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5799 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5800 // FIXME: The indentation here is not ideal. 5801 verifyFormat( 5802 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5803 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5804 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5805 } 5806 5807 TEST_F(FormatTest, FormatsFunctionTypes) { 5808 verifyFormat("A<bool()> a;"); 5809 verifyFormat("A<SomeType()> a;"); 5810 verifyFormat("A<void (*)(int, std::string)> a;"); 5811 verifyFormat("A<void *(int)>;"); 5812 verifyFormat("void *(*a)(int *, SomeType *);"); 5813 verifyFormat("int (*func)(void *);"); 5814 verifyFormat("void f() { int (*func)(void *); }"); 5815 verifyFormat("template <class CallbackClass>\n" 5816 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5817 5818 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5819 verifyGoogleFormat("void* (*a)(int);"); 5820 verifyGoogleFormat( 5821 "template <class CallbackClass>\n" 5822 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5823 5824 // Other constructs can look somewhat like function types: 5825 verifyFormat("A<sizeof(*x)> a;"); 5826 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5827 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5828 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5829 } 5830 5831 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5832 verifyFormat("A (*foo_)[6];"); 5833 verifyFormat("vector<int> (*foo_)[6];"); 5834 } 5835 5836 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5837 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5838 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5839 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5840 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5841 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5842 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5843 5844 // Different ways of ()-initializiation. 5845 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5846 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5847 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5848 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5849 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5850 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5851 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5852 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5853 } 5854 5855 TEST_F(FormatTest, BreaksLongDeclarations) { 5856 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5857 " AnotherNameForTheLongType;"); 5858 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5860 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5861 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5862 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5863 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5864 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5865 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5866 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5867 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5868 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5869 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5870 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5871 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5872 FormatStyle Indented = getLLVMStyle(); 5873 Indented.IndentWrappedFunctionNames = true; 5874 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5875 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5876 Indented); 5877 verifyFormat( 5878 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5879 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5880 Indented); 5881 verifyFormat( 5882 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5883 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5884 Indented); 5885 verifyFormat( 5886 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5887 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5888 Indented); 5889 5890 // FIXME: Without the comment, this breaks after "(". 5891 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5892 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5893 getGoogleStyle()); 5894 5895 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5896 " int LoooooooooooooooooooongParam2) {}"); 5897 verifyFormat( 5898 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5899 " SourceLocation L, IdentifierIn *II,\n" 5900 " Type *T) {}"); 5901 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5902 "ReallyReaaallyLongFunctionName(\n" 5903 " const std::string &SomeParameter,\n" 5904 " const SomeType<string, SomeOtherTemplateParameter>\n" 5905 " &ReallyReallyLongParameterName,\n" 5906 " const SomeType<string, SomeOtherTemplateParameter>\n" 5907 " &AnotherLongParameterName) {}"); 5908 verifyFormat("template <typename A>\n" 5909 "SomeLoooooooooooooooooooooongType<\n" 5910 " typename some_namespace::SomeOtherType<A>::Type>\n" 5911 "Function() {}"); 5912 5913 verifyGoogleFormat( 5914 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5915 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5916 verifyGoogleFormat( 5917 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5918 " SourceLocation L) {}"); 5919 verifyGoogleFormat( 5920 "some_namespace::LongReturnType\n" 5921 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5922 " int first_long_parameter, int second_parameter) {}"); 5923 5924 verifyGoogleFormat("template <typename T>\n" 5925 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5926 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5927 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5928 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5929 5930 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5931 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5932 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5933 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5934 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5935 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5936 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5937 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5940 } 5941 5942 TEST_F(FormatTest, FormatsArrays) { 5943 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5944 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5945 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5946 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5947 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5948 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5949 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5950 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5951 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5952 verifyFormat( 5953 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5954 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5955 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5956 5957 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5959 verifyFormat( 5960 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5961 " .aaaaaaa[0]\n" 5962 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5963 5964 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5965 } 5966 5967 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5968 verifyFormat("(a)->b();"); 5969 verifyFormat("--a;"); 5970 } 5971 5972 TEST_F(FormatTest, HandlesIncludeDirectives) { 5973 verifyFormat("#include <string>\n" 5974 "#include <a/b/c.h>\n" 5975 "#include \"a/b/string\"\n" 5976 "#include \"string.h\"\n" 5977 "#include \"string.h\"\n" 5978 "#include <a-a>\n" 5979 "#include < path with space >\n" 5980 "#include_next <test.h>" 5981 "#include \"abc.h\" // this is included for ABC\n" 5982 "#include \"some long include\" // with a comment\n" 5983 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 5984 getLLVMStyleWithColumns(35)); 5985 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 5986 EXPECT_EQ("#include <a>", format("#include<a>")); 5987 5988 verifyFormat("#import <string>"); 5989 verifyFormat("#import <a/b/c.h>"); 5990 verifyFormat("#import \"a/b/string\""); 5991 verifyFormat("#import \"string.h\""); 5992 verifyFormat("#import \"string.h\""); 5993 verifyFormat("#if __has_include(<strstream>)\n" 5994 "#include <strstream>\n" 5995 "#endif"); 5996 5997 verifyFormat("#define MY_IMPORT <a/b>"); 5998 5999 // Protocol buffer definition or missing "#". 6000 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6001 getLLVMStyleWithColumns(30)); 6002 6003 FormatStyle Style = getLLVMStyle(); 6004 Style.AlwaysBreakBeforeMultilineStrings = true; 6005 Style.ColumnLimit = 0; 6006 verifyFormat("#import \"abc.h\"", Style); 6007 6008 // But 'import' might also be a regular C++ namespace. 6009 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6011 } 6012 6013 //===----------------------------------------------------------------------===// 6014 // Error recovery tests. 6015 //===----------------------------------------------------------------------===// 6016 6017 TEST_F(FormatTest, IncompleteParameterLists) { 6018 FormatStyle NoBinPacking = getLLVMStyle(); 6019 NoBinPacking.BinPackParameters = false; 6020 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6021 " double *min_x,\n" 6022 " double *max_x,\n" 6023 " double *min_y,\n" 6024 " double *max_y,\n" 6025 " double *min_z,\n" 6026 " double *max_z, ) {}", 6027 NoBinPacking); 6028 } 6029 6030 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6031 verifyFormat("void f() { return; }\n42"); 6032 verifyFormat("void f() {\n" 6033 " if (0)\n" 6034 " return;\n" 6035 "}\n" 6036 "42"); 6037 verifyFormat("void f() { return }\n42"); 6038 verifyFormat("void f() {\n" 6039 " if (0)\n" 6040 " return\n" 6041 "}\n" 6042 "42"); 6043 } 6044 6045 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6046 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6047 EXPECT_EQ("void f() {\n" 6048 " if (a)\n" 6049 " return\n" 6050 "}", 6051 format("void f ( ) { if ( a ) return }")); 6052 EXPECT_EQ("namespace N {\n" 6053 "void f()\n" 6054 "}", 6055 format("namespace N { void f() }")); 6056 EXPECT_EQ("namespace N {\n" 6057 "void f() {}\n" 6058 "void g()\n" 6059 "}", 6060 format("namespace N { void f( ) { } void g( ) }")); 6061 } 6062 6063 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6064 verifyFormat("int aaaaaaaa =\n" 6065 " // Overlylongcomment\n" 6066 " b;", 6067 getLLVMStyleWithColumns(20)); 6068 verifyFormat("function(\n" 6069 " ShortArgument,\n" 6070 " LoooooooooooongArgument);\n", 6071 getLLVMStyleWithColumns(20)); 6072 } 6073 6074 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6075 verifyFormat("public:"); 6076 verifyFormat("class A {\n" 6077 "public\n" 6078 " void f() {}\n" 6079 "};"); 6080 verifyFormat("public\n" 6081 "int qwerty;"); 6082 verifyFormat("public\n" 6083 "B {}"); 6084 verifyFormat("public\n" 6085 "{}"); 6086 verifyFormat("public\n" 6087 "B { int x; }"); 6088 } 6089 6090 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6091 verifyFormat("{"); 6092 verifyFormat("#})"); 6093 verifyNoCrash("(/**/[:!] ?[)."); 6094 } 6095 6096 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6097 verifyFormat("do {\n}"); 6098 verifyFormat("do {\n}\n" 6099 "f();"); 6100 verifyFormat("do {\n}\n" 6101 "wheeee(fun);"); 6102 verifyFormat("do {\n" 6103 " f();\n" 6104 "}"); 6105 } 6106 6107 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6108 verifyFormat("if {\n foo;\n foo();\n}"); 6109 verifyFormat("switch {\n foo;\n foo();\n}"); 6110 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6111 verifyFormat("while {\n foo;\n foo();\n}"); 6112 verifyFormat("do {\n foo;\n foo();\n} while;"); 6113 } 6114 6115 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6116 verifyIncompleteFormat("namespace {\n" 6117 "class Foo { Foo (\n" 6118 "};\n" 6119 "} // comment"); 6120 } 6121 6122 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6123 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6124 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6125 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6126 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6127 6128 EXPECT_EQ("{\n" 6129 " {\n" 6130 " breakme(\n" 6131 " qwe);\n" 6132 " }\n", 6133 format("{\n" 6134 " {\n" 6135 " breakme(qwe);\n" 6136 "}\n", 6137 getLLVMStyleWithColumns(10))); 6138 } 6139 6140 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6141 verifyFormat("int x = {\n" 6142 " avariable,\n" 6143 " b(alongervariable)};", 6144 getLLVMStyleWithColumns(25)); 6145 } 6146 6147 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6148 verifyFormat("return (a)(b){1, 2, 3};"); 6149 } 6150 6151 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6152 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6153 verifyFormat("vector<int> x{\n" 6154 " 1, 2, 3, 4,\n" 6155 "};"); 6156 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6157 verifyFormat("f({1, 2});"); 6158 verifyFormat("auto v = Foo{-1};"); 6159 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6160 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6161 verifyFormat("new vector<int>{1, 2, 3};"); 6162 verifyFormat("new int[3]{1, 2, 3};"); 6163 verifyFormat("new int{1};"); 6164 verifyFormat("return {arg1, arg2};"); 6165 verifyFormat("return {arg1, SomeType{parameter}};"); 6166 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6167 verifyFormat("new T{arg1, arg2};"); 6168 verifyFormat("f(MyMap[{composite, key}]);"); 6169 verifyFormat("class Class {\n" 6170 " T member = {arg1, arg2};\n" 6171 "};"); 6172 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6173 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6174 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6175 6176 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6177 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6178 verifyFormat("auto i = decltype(x){};"); 6179 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6180 verifyFormat("Node n{1, Node{1000}, //\n" 6181 " 2};"); 6182 verifyFormat("Aaaa aaaaaaa{\n" 6183 " {\n" 6184 " aaaa,\n" 6185 " },\n" 6186 "};"); 6187 verifyFormat("class C : public D {\n" 6188 " SomeClass SC{2};\n" 6189 "};"); 6190 verifyFormat("class C : public A {\n" 6191 " class D : public B {\n" 6192 " void f() { int i{2}; }\n" 6193 " };\n" 6194 "};"); 6195 verifyFormat("#define A {a, a},"); 6196 6197 // In combination with BinPackArguments = false. 6198 FormatStyle NoBinPacking = getLLVMStyle(); 6199 NoBinPacking.BinPackArguments = false; 6200 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6201 " bbbbb,\n" 6202 " ccccc,\n" 6203 " ddddd,\n" 6204 " eeeee,\n" 6205 " ffffff,\n" 6206 " ggggg,\n" 6207 " hhhhhh,\n" 6208 " iiiiii,\n" 6209 " jjjjjj,\n" 6210 " kkkkkk};", 6211 NoBinPacking); 6212 verifyFormat("const Aaaaaa aaaaa = {\n" 6213 " aaaaa,\n" 6214 " bbbbb,\n" 6215 " ccccc,\n" 6216 " ddddd,\n" 6217 " eeeee,\n" 6218 " ffffff,\n" 6219 " ggggg,\n" 6220 " hhhhhh,\n" 6221 " iiiiii,\n" 6222 " jjjjjj,\n" 6223 " kkkkkk,\n" 6224 "};", 6225 NoBinPacking); 6226 verifyFormat( 6227 "const Aaaaaa aaaaa = {\n" 6228 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6229 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6230 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6231 "};", 6232 NoBinPacking); 6233 6234 // FIXME: The alignment of these trailing comments might be bad. Then again, 6235 // this might be utterly useless in real code. 6236 verifyFormat("Constructor::Constructor()\n" 6237 " : some_value{ //\n" 6238 " aaaaaaa, //\n" 6239 " bbbbbbb} {}"); 6240 6241 // In braced lists, the first comment is always assumed to belong to the 6242 // first element. Thus, it can be moved to the next or previous line as 6243 // appropriate. 6244 EXPECT_EQ("function({// First element:\n" 6245 " 1,\n" 6246 " // Second element:\n" 6247 " 2});", 6248 format("function({\n" 6249 " // First element:\n" 6250 " 1,\n" 6251 " // Second element:\n" 6252 " 2});")); 6253 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6254 " // First element:\n" 6255 " 1,\n" 6256 " // Second element:\n" 6257 " 2};", 6258 format("std::vector<int> MyNumbers{// First element:\n" 6259 " 1,\n" 6260 " // Second element:\n" 6261 " 2};", 6262 getLLVMStyleWithColumns(30))); 6263 // A trailing comma should still lead to an enforced line break. 6264 EXPECT_EQ("vector<int> SomeVector = {\n" 6265 " // aaa\n" 6266 " 1, 2,\n" 6267 "};", 6268 format("vector<int> SomeVector = { // aaa\n" 6269 " 1, 2, };")); 6270 6271 FormatStyle ExtraSpaces = getLLVMStyle(); 6272 ExtraSpaces.Cpp11BracedListStyle = false; 6273 ExtraSpaces.ColumnLimit = 75; 6274 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6275 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6276 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6277 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6278 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6279 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6280 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6281 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6282 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6283 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6284 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6285 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6286 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6287 verifyFormat("class Class {\n" 6288 " T member = { arg1, arg2 };\n" 6289 "};", 6290 ExtraSpaces); 6291 verifyFormat( 6292 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6293 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6294 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6295 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6296 ExtraSpaces); 6297 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6298 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6299 ExtraSpaces); 6300 verifyFormat( 6301 "someFunction(OtherParam,\n" 6302 " BracedList{ // comment 1 (Forcing interesting break)\n" 6303 " param1, param2,\n" 6304 " // comment 2\n" 6305 " param3, param4 });", 6306 ExtraSpaces); 6307 verifyFormat( 6308 "std::this_thread::sleep_for(\n" 6309 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6310 ExtraSpaces); 6311 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6312 " aaaaaaa,\n" 6313 " aaaaaaaaaa,\n" 6314 " aaaaa,\n" 6315 " aaaaaaaaaaaaaaa,\n" 6316 " aaa,\n" 6317 " aaaaaaaaaa,\n" 6318 " a,\n" 6319 " aaaaaaaaaaaaaaaaaaaaa,\n" 6320 " aaaaaaaaaaaa,\n" 6321 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6322 " aaaaaaa,\n" 6323 " a};"); 6324 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6325 } 6326 6327 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6328 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6329 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6330 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6331 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6332 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6333 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6334 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6335 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6336 " 1, 22, 333, 4444, 55555, //\n" 6337 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6338 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6339 verifyFormat( 6340 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6341 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6342 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6343 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6344 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6345 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6346 " 7777777};"); 6347 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6348 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6349 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6350 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6351 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6352 " // Separating comment.\n" 6353 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6354 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6355 " // Leading comment\n" 6356 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6357 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6358 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6359 " 1, 1, 1, 1};", 6360 getLLVMStyleWithColumns(39)); 6361 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6362 " 1, 1, 1, 1};", 6363 getLLVMStyleWithColumns(38)); 6364 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6365 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6366 getLLVMStyleWithColumns(43)); 6367 verifyFormat( 6368 "static unsigned SomeValues[10][3] = {\n" 6369 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6370 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6371 verifyFormat("static auto fields = new vector<string>{\n" 6372 " \"aaaaaaaaaaaaa\",\n" 6373 " \"aaaaaaaaaaaaa\",\n" 6374 " \"aaaaaaaaaaaa\",\n" 6375 " \"aaaaaaaaaaaaaa\",\n" 6376 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6377 " \"aaaaaaaaaaaa\",\n" 6378 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6379 "};"); 6380 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6381 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6382 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6383 " 3, cccccccccccccccccccccc};", 6384 getLLVMStyleWithColumns(60)); 6385 6386 // Trailing commas. 6387 verifyFormat("vector<int> x = {\n" 6388 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6389 "};", 6390 getLLVMStyleWithColumns(39)); 6391 verifyFormat("vector<int> x = {\n" 6392 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6393 "};", 6394 getLLVMStyleWithColumns(39)); 6395 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6396 " 1, 1, 1, 1,\n" 6397 " /**/ /**/};", 6398 getLLVMStyleWithColumns(39)); 6399 6400 // Trailing comment in the first line. 6401 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6402 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6403 " 111111111, 222222222, 3333333333, 444444444, //\n" 6404 " 11111111, 22222222, 333333333, 44444444};"); 6405 // Trailing comment in the last line. 6406 verifyFormat("int aaaaa[] = {\n" 6407 " 1, 2, 3, // comment\n" 6408 " 4, 5, 6 // comment\n" 6409 "};"); 6410 6411 // With nested lists, we should either format one item per line or all nested 6412 // lists one on line. 6413 // FIXME: For some nested lists, we can do better. 6414 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6415 " {aaaaaaaaaaaaaaaaaaa},\n" 6416 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6417 " {aaaaaaaaaaaaaaaaa}};", 6418 getLLVMStyleWithColumns(60)); 6419 verifyFormat( 6420 "SomeStruct my_struct_array = {\n" 6421 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6422 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6423 " {aaa, aaa},\n" 6424 " {aaa, aaa},\n" 6425 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6426 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6427 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6428 6429 // No column layout should be used here. 6430 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6431 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6432 6433 verifyNoCrash("a<,"); 6434 } 6435 6436 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6437 FormatStyle DoNotMerge = getLLVMStyle(); 6438 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6439 6440 verifyFormat("void f() { return 42; }"); 6441 verifyFormat("void f() {\n" 6442 " return 42;\n" 6443 "}", 6444 DoNotMerge); 6445 verifyFormat("void f() {\n" 6446 " // Comment\n" 6447 "}"); 6448 verifyFormat("{\n" 6449 "#error {\n" 6450 " int a;\n" 6451 "}"); 6452 verifyFormat("{\n" 6453 " int a;\n" 6454 "#error {\n" 6455 "}"); 6456 verifyFormat("void f() {} // comment"); 6457 verifyFormat("void f() { int a; } // comment"); 6458 verifyFormat("void f() {\n" 6459 "} // comment", 6460 DoNotMerge); 6461 verifyFormat("void f() {\n" 6462 " int a;\n" 6463 "} // comment", 6464 DoNotMerge); 6465 verifyFormat("void f() {\n" 6466 "} // comment", 6467 getLLVMStyleWithColumns(15)); 6468 6469 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6470 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6471 6472 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6473 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6474 verifyFormat("class C {\n" 6475 " C()\n" 6476 " : iiiiiiii(nullptr),\n" 6477 " kkkkkkk(nullptr),\n" 6478 " mmmmmmm(nullptr),\n" 6479 " nnnnnnn(nullptr) {}\n" 6480 "};", 6481 getGoogleStyle()); 6482 6483 FormatStyle NoColumnLimit = getLLVMStyle(); 6484 NoColumnLimit.ColumnLimit = 0; 6485 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6486 EXPECT_EQ("class C {\n" 6487 " A() : b(0) {}\n" 6488 "};", 6489 format("class C{A():b(0){}};", NoColumnLimit)); 6490 EXPECT_EQ("A()\n" 6491 " : b(0) {\n" 6492 "}", 6493 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6494 6495 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6496 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6497 FormatStyle::SFS_None; 6498 EXPECT_EQ("A()\n" 6499 " : b(0) {\n" 6500 "}", 6501 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6502 EXPECT_EQ("A()\n" 6503 " : b(0) {\n" 6504 "}", 6505 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6506 6507 verifyFormat("#define A \\\n" 6508 " void f() { \\\n" 6509 " int i; \\\n" 6510 " }", 6511 getLLVMStyleWithColumns(20)); 6512 verifyFormat("#define A \\\n" 6513 " void f() { int i; }", 6514 getLLVMStyleWithColumns(21)); 6515 verifyFormat("#define A \\\n" 6516 " void f() { \\\n" 6517 " int i; \\\n" 6518 " } \\\n" 6519 " int j;", 6520 getLLVMStyleWithColumns(22)); 6521 verifyFormat("#define A \\\n" 6522 " void f() { int i; } \\\n" 6523 " int j;", 6524 getLLVMStyleWithColumns(23)); 6525 } 6526 6527 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6528 FormatStyle MergeInlineOnly = getLLVMStyle(); 6529 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6530 verifyFormat("class C {\n" 6531 " int f() { return 42; }\n" 6532 "};", 6533 MergeInlineOnly); 6534 verifyFormat("int f() {\n" 6535 " return 42;\n" 6536 "}", 6537 MergeInlineOnly); 6538 } 6539 6540 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6541 // Elaborate type variable declarations. 6542 verifyFormat("struct foo a = {bar};\nint n;"); 6543 verifyFormat("class foo a = {bar};\nint n;"); 6544 verifyFormat("union foo a = {bar};\nint n;"); 6545 6546 // Elaborate types inside function definitions. 6547 verifyFormat("struct foo f() {}\nint n;"); 6548 verifyFormat("class foo f() {}\nint n;"); 6549 verifyFormat("union foo f() {}\nint n;"); 6550 6551 // Templates. 6552 verifyFormat("template <class X> void f() {}\nint n;"); 6553 verifyFormat("template <struct X> void f() {}\nint n;"); 6554 verifyFormat("template <union X> void f() {}\nint n;"); 6555 6556 // Actual definitions... 6557 verifyFormat("struct {\n} n;"); 6558 verifyFormat( 6559 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6560 verifyFormat("union Z {\n int n;\n} x;"); 6561 verifyFormat("class MACRO Z {\n} n;"); 6562 verifyFormat("class MACRO(X) Z {\n} n;"); 6563 verifyFormat("class __attribute__(X) Z {\n} n;"); 6564 verifyFormat("class __declspec(X) Z {\n} n;"); 6565 verifyFormat("class A##B##C {\n} n;"); 6566 verifyFormat("class alignas(16) Z {\n} n;"); 6567 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6568 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6569 6570 // Redefinition from nested context: 6571 verifyFormat("class A::B::C {\n} n;"); 6572 6573 // Template definitions. 6574 verifyFormat( 6575 "template <typename F>\n" 6576 "Matcher(const Matcher<F> &Other,\n" 6577 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6578 " !is_same<F, T>::value>::type * = 0)\n" 6579 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6580 6581 // FIXME: This is still incorrectly handled at the formatter side. 6582 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6583 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6584 6585 // FIXME: 6586 // This now gets parsed incorrectly as class definition. 6587 // verifyFormat("class A<int> f() {\n}\nint n;"); 6588 6589 // Elaborate types where incorrectly parsing the structural element would 6590 // break the indent. 6591 verifyFormat("if (true)\n" 6592 " class X x;\n" 6593 "else\n" 6594 " f();\n"); 6595 6596 // This is simply incomplete. Formatting is not important, but must not crash. 6597 verifyFormat("class A:"); 6598 } 6599 6600 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6601 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6602 format("#error Leave all white!!!!! space* alone!\n")); 6603 EXPECT_EQ( 6604 "#warning Leave all white!!!!! space* alone!\n", 6605 format("#warning Leave all white!!!!! space* alone!\n")); 6606 EXPECT_EQ("#error 1", format(" # error 1")); 6607 EXPECT_EQ("#warning 1", format(" # warning 1")); 6608 } 6609 6610 TEST_F(FormatTest, FormatHashIfExpressions) { 6611 verifyFormat("#if AAAA && BBBB"); 6612 verifyFormat("#if (AAAA && BBBB)"); 6613 verifyFormat("#elif (AAAA && BBBB)"); 6614 // FIXME: Come up with a better indentation for #elif. 6615 verifyFormat( 6616 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6617 " defined(BBBBBBBB)\n" 6618 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6619 " defined(BBBBBBBB)\n" 6620 "#endif", 6621 getLLVMStyleWithColumns(65)); 6622 } 6623 6624 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6625 FormatStyle AllowsMergedIf = getGoogleStyle(); 6626 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6627 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6628 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6629 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6630 EXPECT_EQ("if (true) return 42;", 6631 format("if (true)\nreturn 42;", AllowsMergedIf)); 6632 FormatStyle ShortMergedIf = AllowsMergedIf; 6633 ShortMergedIf.ColumnLimit = 25; 6634 verifyFormat("#define A \\\n" 6635 " if (true) return 42;", 6636 ShortMergedIf); 6637 verifyFormat("#define A \\\n" 6638 " f(); \\\n" 6639 " if (true)\n" 6640 "#define B", 6641 ShortMergedIf); 6642 verifyFormat("#define A \\\n" 6643 " f(); \\\n" 6644 " if (true)\n" 6645 "g();", 6646 ShortMergedIf); 6647 verifyFormat("{\n" 6648 "#ifdef A\n" 6649 " // Comment\n" 6650 " if (true) continue;\n" 6651 "#endif\n" 6652 " // Comment\n" 6653 " if (true) continue;\n" 6654 "}", 6655 ShortMergedIf); 6656 ShortMergedIf.ColumnLimit = 29; 6657 verifyFormat("#define A \\\n" 6658 " if (aaaaaaaaaa) return 1; \\\n" 6659 " return 2;", 6660 ShortMergedIf); 6661 ShortMergedIf.ColumnLimit = 28; 6662 verifyFormat("#define A \\\n" 6663 " if (aaaaaaaaaa) \\\n" 6664 " return 1; \\\n" 6665 " return 2;", 6666 ShortMergedIf); 6667 } 6668 6669 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6670 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6671 " f();\n" 6672 "}"); 6673 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6674 " f();\n" 6675 "} /* another comment */ else /* comment #3 */ {\n" 6676 " g();\n" 6677 "}"); 6678 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6679 " f();\n" 6680 "}"); 6681 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6682 " f();\n" 6683 "}"); 6684 verifyFormat("do /* a comment in a strange place */ {\n" 6685 " f();\n" 6686 "} /* another comment */ while (0);"); 6687 } 6688 6689 TEST_F(FormatTest, BlockComments) { 6690 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6691 format("/* *//* */ /* */\n/* *//* */ /* */")); 6692 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6693 EXPECT_EQ("#define A /*123*/ \\\n" 6694 " b\n" 6695 "/* */\n" 6696 "someCall(\n" 6697 " parameter);", 6698 format("#define A /*123*/ b\n" 6699 "/* */\n" 6700 "someCall(parameter);", 6701 getLLVMStyleWithColumns(15))); 6702 6703 EXPECT_EQ("#define A\n" 6704 "/* */ someCall(\n" 6705 " parameter);", 6706 format("#define A\n" 6707 "/* */someCall(parameter);", 6708 getLLVMStyleWithColumns(15))); 6709 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6710 EXPECT_EQ("/*\n" 6711 "*\n" 6712 " * aaaaaa\n" 6713 " * aaaaaa\n" 6714 "*/", 6715 format("/*\n" 6716 "*\n" 6717 " * aaaaaa aaaaaa\n" 6718 "*/", 6719 getLLVMStyleWithColumns(10))); 6720 EXPECT_EQ("/*\n" 6721 "**\n" 6722 "* aaaaaa\n" 6723 "*aaaaaa\n" 6724 "*/", 6725 format("/*\n" 6726 "**\n" 6727 "* aaaaaa aaaaaa\n" 6728 "*/", 6729 getLLVMStyleWithColumns(10))); 6730 6731 FormatStyle NoBinPacking = getLLVMStyle(); 6732 NoBinPacking.BinPackParameters = false; 6733 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6734 " 2, /* comment 2 */\n" 6735 " 3, /* comment 3 */\n" 6736 " aaaa,\n" 6737 " bbbb);", 6738 format("someFunction (1, /* comment 1 */\n" 6739 " 2, /* comment 2 */ \n" 6740 " 3, /* comment 3 */\n" 6741 "aaaa, bbbb );", 6742 NoBinPacking)); 6743 verifyFormat( 6744 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6746 EXPECT_EQ( 6747 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6748 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 6750 format( 6751 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6752 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 6754 EXPECT_EQ( 6755 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6756 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6757 "int cccccccccccccccccccccccccccccc; /* comment */\n", 6758 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6759 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6760 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 6761 6762 verifyFormat("void f(int * /* unused */) {}"); 6763 6764 EXPECT_EQ("/*\n" 6765 " **\n" 6766 " */", 6767 format("/*\n" 6768 " **\n" 6769 " */")); 6770 EXPECT_EQ("/*\n" 6771 " *q\n" 6772 " */", 6773 format("/*\n" 6774 " *q\n" 6775 " */")); 6776 EXPECT_EQ("/*\n" 6777 " * q\n" 6778 " */", 6779 format("/*\n" 6780 " * q\n" 6781 " */")); 6782 EXPECT_EQ("/*\n" 6783 " **/", 6784 format("/*\n" 6785 " **/")); 6786 EXPECT_EQ("/*\n" 6787 " ***/", 6788 format("/*\n" 6789 " ***/")); 6790 } 6791 6792 TEST_F(FormatTest, BlockCommentsInMacros) { 6793 EXPECT_EQ("#define A \\\n" 6794 " { \\\n" 6795 " /* one line */ \\\n" 6796 " someCall();", 6797 format("#define A { \\\n" 6798 " /* one line */ \\\n" 6799 " someCall();", 6800 getLLVMStyleWithColumns(20))); 6801 EXPECT_EQ("#define A \\\n" 6802 " { \\\n" 6803 " /* previous */ \\\n" 6804 " /* one line */ \\\n" 6805 " someCall();", 6806 format("#define A { \\\n" 6807 " /* previous */ \\\n" 6808 " /* one line */ \\\n" 6809 " someCall();", 6810 getLLVMStyleWithColumns(20))); 6811 } 6812 6813 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 6814 EXPECT_EQ("a = {\n" 6815 " 1111 /* */\n" 6816 "};", 6817 format("a = {1111 /* */\n" 6818 "};", 6819 getLLVMStyleWithColumns(15))); 6820 EXPECT_EQ("a = {\n" 6821 " 1111 /* */\n" 6822 "};", 6823 format("a = {1111 /* */\n" 6824 "};", 6825 getLLVMStyleWithColumns(15))); 6826 6827 // FIXME: The formatting is still wrong here. 6828 EXPECT_EQ("a = {\n" 6829 " 1111 /* a\n" 6830 " */\n" 6831 "};", 6832 format("a = {1111 /* a */\n" 6833 "};", 6834 getLLVMStyleWithColumns(15))); 6835 } 6836 6837 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 6838 // FIXME: This is not what we want... 6839 verifyFormat("{\n" 6840 "// a" 6841 "// b"); 6842 } 6843 6844 TEST_F(FormatTest, FormatStarDependingOnContext) { 6845 verifyFormat("void f(int *a);"); 6846 verifyFormat("void f() { f(fint * b); }"); 6847 verifyFormat("class A {\n void f(int *a);\n};"); 6848 verifyFormat("class A {\n int *a;\n};"); 6849 verifyFormat("namespace a {\n" 6850 "namespace b {\n" 6851 "class A {\n" 6852 " void f() {}\n" 6853 " int *a;\n" 6854 "};\n" 6855 "}\n" 6856 "}"); 6857 } 6858 6859 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6860 verifyFormat("while"); 6861 verifyFormat("operator"); 6862 } 6863 6864 //===----------------------------------------------------------------------===// 6865 // Objective-C tests. 6866 //===----------------------------------------------------------------------===// 6867 6868 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 6869 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 6870 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 6871 format("-(NSUInteger)indexOfObject:(id)anObject;")); 6872 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 6873 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 6874 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 6875 format("-(NSInteger)Method3:(id)anObject;")); 6876 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 6877 format("-(NSInteger)Method4:(id)anObject;")); 6878 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 6879 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 6880 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 6881 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 6882 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6883 "forAllCells:(BOOL)flag;", 6884 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6885 "forAllCells:(BOOL)flag;")); 6886 6887 // Very long objectiveC method declaration. 6888 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 6889 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 6890 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 6891 " inRange:(NSRange)range\n" 6892 " outRange:(NSRange)out_range\n" 6893 " outRange1:(NSRange)out_range1\n" 6894 " outRange2:(NSRange)out_range2\n" 6895 " outRange3:(NSRange)out_range3\n" 6896 " outRange4:(NSRange)out_range4\n" 6897 " outRange5:(NSRange)out_range5\n" 6898 " outRange6:(NSRange)out_range6\n" 6899 " outRange7:(NSRange)out_range7\n" 6900 " outRange8:(NSRange)out_range8\n" 6901 " outRange9:(NSRange)out_range9;"); 6902 6903 // When the function name has to be wrapped. 6904 FormatStyle Style = getLLVMStyle(); 6905 Style.IndentWrappedFunctionNames = false; 6906 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6907 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6908 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6909 "}", 6910 Style); 6911 Style.IndentWrappedFunctionNames = true; 6912 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6913 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6914 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6915 "}", 6916 Style); 6917 6918 verifyFormat("- (int)sum:(vector<int>)numbers;"); 6919 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 6920 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 6921 // protocol lists (but not for template classes): 6922 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 6923 6924 verifyFormat("- (int (*)())foo:(int (*)())f;"); 6925 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 6926 6927 // If there's no return type (very rare in practice!), LLVM and Google style 6928 // agree. 6929 verifyFormat("- foo;"); 6930 verifyFormat("- foo:(int)f;"); 6931 verifyGoogleFormat("- foo:(int)foo;"); 6932 } 6933 6934 TEST_F(FormatTest, FormatObjCInterface) { 6935 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 6936 "@public\n" 6937 " int field1;\n" 6938 "@protected\n" 6939 " int field2;\n" 6940 "@private\n" 6941 " int field3;\n" 6942 "@package\n" 6943 " int field4;\n" 6944 "}\n" 6945 "+ (id)init;\n" 6946 "@end"); 6947 6948 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 6949 " @public\n" 6950 " int field1;\n" 6951 " @protected\n" 6952 " int field2;\n" 6953 " @private\n" 6954 " int field3;\n" 6955 " @package\n" 6956 " int field4;\n" 6957 "}\n" 6958 "+ (id)init;\n" 6959 "@end"); 6960 6961 verifyFormat("@interface /* wait for it */ Foo\n" 6962 "+ (id)init;\n" 6963 "// Look, a comment!\n" 6964 "- (int)answerWith:(int)i;\n" 6965 "@end"); 6966 6967 verifyFormat("@interface Foo\n" 6968 "@end\n" 6969 "@interface Bar\n" 6970 "@end"); 6971 6972 verifyFormat("@interface Foo : Bar\n" 6973 "+ (id)init;\n" 6974 "@end"); 6975 6976 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 6977 "+ (id)init;\n" 6978 "@end"); 6979 6980 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 6981 "+ (id)init;\n" 6982 "@end"); 6983 6984 verifyFormat("@interface Foo (HackStuff)\n" 6985 "+ (id)init;\n" 6986 "@end"); 6987 6988 verifyFormat("@interface Foo ()\n" 6989 "+ (id)init;\n" 6990 "@end"); 6991 6992 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 6993 "+ (id)init;\n" 6994 "@end"); 6995 6996 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 6997 "+ (id)init;\n" 6998 "@end"); 6999 7000 verifyFormat("@interface Foo {\n" 7001 " int _i;\n" 7002 "}\n" 7003 "+ (id)init;\n" 7004 "@end"); 7005 7006 verifyFormat("@interface Foo : Bar {\n" 7007 " int _i;\n" 7008 "}\n" 7009 "+ (id)init;\n" 7010 "@end"); 7011 7012 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7013 " int _i;\n" 7014 "}\n" 7015 "+ (id)init;\n" 7016 "@end"); 7017 7018 verifyFormat("@interface Foo (HackStuff) {\n" 7019 " int _i;\n" 7020 "}\n" 7021 "+ (id)init;\n" 7022 "@end"); 7023 7024 verifyFormat("@interface Foo () {\n" 7025 " int _i;\n" 7026 "}\n" 7027 "+ (id)init;\n" 7028 "@end"); 7029 7030 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7031 " int _i;\n" 7032 "}\n" 7033 "+ (id)init;\n" 7034 "@end"); 7035 7036 FormatStyle OnePerLine = getGoogleStyle(); 7037 OnePerLine.BinPackParameters = false; 7038 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7043 "}", 7044 OnePerLine); 7045 } 7046 7047 TEST_F(FormatTest, FormatObjCImplementation) { 7048 verifyFormat("@implementation Foo : NSObject {\n" 7049 "@public\n" 7050 " int field1;\n" 7051 "@protected\n" 7052 " int field2;\n" 7053 "@private\n" 7054 " int field3;\n" 7055 "@package\n" 7056 " int field4;\n" 7057 "}\n" 7058 "+ (id)init {\n}\n" 7059 "@end"); 7060 7061 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7062 " @public\n" 7063 " int field1;\n" 7064 " @protected\n" 7065 " int field2;\n" 7066 " @private\n" 7067 " int field3;\n" 7068 " @package\n" 7069 " int field4;\n" 7070 "}\n" 7071 "+ (id)init {\n}\n" 7072 "@end"); 7073 7074 verifyFormat("@implementation Foo\n" 7075 "+ (id)init {\n" 7076 " if (true)\n" 7077 " return nil;\n" 7078 "}\n" 7079 "// Look, a comment!\n" 7080 "- (int)answerWith:(int)i {\n" 7081 " return i;\n" 7082 "}\n" 7083 "+ (int)answerWith:(int)i {\n" 7084 " return i;\n" 7085 "}\n" 7086 "@end"); 7087 7088 verifyFormat("@implementation Foo\n" 7089 "@end\n" 7090 "@implementation Bar\n" 7091 "@end"); 7092 7093 EXPECT_EQ("@implementation Foo : Bar\n" 7094 "+ (id)init {\n}\n" 7095 "- (void)foo {\n}\n" 7096 "@end", 7097 format("@implementation Foo : Bar\n" 7098 "+(id)init{}\n" 7099 "-(void)foo{}\n" 7100 "@end")); 7101 7102 verifyFormat("@implementation Foo {\n" 7103 " int _i;\n" 7104 "}\n" 7105 "+ (id)init {\n}\n" 7106 "@end"); 7107 7108 verifyFormat("@implementation Foo : Bar {\n" 7109 " int _i;\n" 7110 "}\n" 7111 "+ (id)init {\n}\n" 7112 "@end"); 7113 7114 verifyFormat("@implementation Foo (HackStuff)\n" 7115 "+ (id)init {\n}\n" 7116 "@end"); 7117 verifyFormat("@implementation ObjcClass\n" 7118 "- (void)method;\n" 7119 "{}\n" 7120 "@end"); 7121 } 7122 7123 TEST_F(FormatTest, FormatObjCProtocol) { 7124 verifyFormat("@protocol Foo\n" 7125 "@property(weak) id delegate;\n" 7126 "- (NSUInteger)numberOfThings;\n" 7127 "@end"); 7128 7129 verifyFormat("@protocol MyProtocol <NSObject>\n" 7130 "- (NSUInteger)numberOfThings;\n" 7131 "@end"); 7132 7133 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7134 "- (NSUInteger)numberOfThings;\n" 7135 "@end"); 7136 7137 verifyFormat("@protocol Foo;\n" 7138 "@protocol Bar;\n"); 7139 7140 verifyFormat("@protocol Foo\n" 7141 "@end\n" 7142 "@protocol Bar\n" 7143 "@end"); 7144 7145 verifyFormat("@protocol myProtocol\n" 7146 "- (void)mandatoryWithInt:(int)i;\n" 7147 "@optional\n" 7148 "- (void)optional;\n" 7149 "@required\n" 7150 "- (void)required;\n" 7151 "@optional\n" 7152 "@property(assign) int madProp;\n" 7153 "@end\n"); 7154 7155 verifyFormat("@property(nonatomic, assign, readonly)\n" 7156 " int *looooooooooooooooooooooooooooongNumber;\n" 7157 "@property(nonatomic, assign, readonly)\n" 7158 " NSString *looooooooooooooooooooooooooooongName;"); 7159 7160 verifyFormat("@implementation PR18406\n" 7161 "}\n" 7162 "@end"); 7163 } 7164 7165 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7166 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7167 " rect:(NSRect)theRect\n" 7168 " interval:(float)theInterval {\n" 7169 "}"); 7170 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7171 " longKeyword:(NSRect)theRect\n" 7172 " evenLongerKeyword:(float)theInterval\n" 7173 " error:(NSError **)theError {\n" 7174 "}"); 7175 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7176 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7177 " NS_DESIGNATED_INITIALIZER;", 7178 getLLVMStyleWithColumns(60)); 7179 7180 // Continuation indent width should win over aligning colons if the function 7181 // name is long. 7182 FormatStyle continuationStyle = getGoogleStyle(); 7183 continuationStyle.ColumnLimit = 40; 7184 continuationStyle.IndentWrappedFunctionNames = true; 7185 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7186 " dontAlignNamef:(NSRect)theRect {\n" 7187 "}", 7188 continuationStyle); 7189 7190 // Make sure we don't break aligning for short parameter names. 7191 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7192 " aShortf:(NSRect)theRect {\n" 7193 "}", 7194 continuationStyle); 7195 } 7196 7197 TEST_F(FormatTest, FormatObjCMethodExpr) { 7198 verifyFormat("[foo bar:baz];"); 7199 verifyFormat("return [foo bar:baz];"); 7200 verifyFormat("return (a)[foo bar:baz];"); 7201 verifyFormat("f([foo bar:baz]);"); 7202 verifyFormat("f(2, [foo bar:baz]);"); 7203 verifyFormat("f(2, a ? b : c);"); 7204 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7205 7206 // Unary operators. 7207 verifyFormat("int a = +[foo bar:baz];"); 7208 verifyFormat("int a = -[foo bar:baz];"); 7209 verifyFormat("int a = ![foo bar:baz];"); 7210 verifyFormat("int a = ~[foo bar:baz];"); 7211 verifyFormat("int a = ++[foo bar:baz];"); 7212 verifyFormat("int a = --[foo bar:baz];"); 7213 verifyFormat("int a = sizeof [foo bar:baz];"); 7214 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7215 verifyFormat("int a = &[foo bar:baz];"); 7216 verifyFormat("int a = *[foo bar:baz];"); 7217 // FIXME: Make casts work, without breaking f()[4]. 7218 // verifyFormat("int a = (int)[foo bar:baz];"); 7219 // verifyFormat("return (int)[foo bar:baz];"); 7220 // verifyFormat("(void)[foo bar:baz];"); 7221 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7222 7223 // Binary operators. 7224 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7225 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7226 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7227 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7228 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7229 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7230 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7231 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7232 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7233 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7234 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7235 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7236 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7237 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7238 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7239 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7240 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7241 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7242 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7243 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7244 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7245 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7246 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7247 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7248 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7249 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7250 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7251 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7252 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7253 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7254 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7255 // Whew! 7256 7257 verifyFormat("return in[42];"); 7258 verifyFormat("for (auto v : in[1]) {\n}"); 7259 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7260 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7261 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7262 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7263 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7264 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7265 "}"); 7266 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7267 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7268 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7269 7270 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7271 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7272 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7273 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7274 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7275 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7276 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7277 7278 verifyFormat("arr[[self indexForFoo:a]];"); 7279 verifyFormat("throw [self errorFor:a];"); 7280 verifyFormat("@throw [self errorFor:a];"); 7281 7282 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7283 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7284 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7285 7286 // This tests that the formatter doesn't break after "backing" but before ":", 7287 // which would be at 80 columns. 7288 verifyFormat( 7289 "void f() {\n" 7290 " if ((self = [super initWithContentRect:contentRect\n" 7291 " styleMask:styleMask ?: otherMask\n" 7292 " backing:NSBackingStoreBuffered\n" 7293 " defer:YES]))"); 7294 7295 verifyFormat( 7296 "[foo checkThatBreakingAfterColonWorksOk:\n" 7297 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7298 7299 verifyFormat("[myObj short:arg1 // Force line break\n" 7300 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7301 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7302 " error:arg4];"); 7303 verifyFormat( 7304 "void f() {\n" 7305 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7306 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7307 " pos.width(), pos.height())\n" 7308 " styleMask:NSBorderlessWindowMask\n" 7309 " backing:NSBackingStoreBuffered\n" 7310 " defer:NO]);\n" 7311 "}"); 7312 verifyFormat( 7313 "void f() {\n" 7314 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7315 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7316 " pos.width(), pos.height())\n" 7317 " syeMask:NSBorderlessWindowMask\n" 7318 " bking:NSBackingStoreBuffered\n" 7319 " der:NO]);\n" 7320 "}", 7321 getLLVMStyleWithColumns(70)); 7322 verifyFormat( 7323 "void f() {\n" 7324 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7325 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7326 " pos.width(), pos.height())\n" 7327 " styleMask:NSBorderlessWindowMask\n" 7328 " backing:NSBackingStoreBuffered\n" 7329 " defer:NO]);\n" 7330 "}", 7331 getChromiumStyle(FormatStyle::LK_Cpp)); 7332 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7333 " with:contentsNativeView];"); 7334 7335 verifyFormat( 7336 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7337 " owner:nillllll];"); 7338 7339 verifyFormat( 7340 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7341 " forType:kBookmarkButtonDragType];"); 7342 7343 verifyFormat("[defaultCenter addObserver:self\n" 7344 " selector:@selector(willEnterFullscreen)\n" 7345 " name:kWillEnterFullscreenNotification\n" 7346 " object:nil];"); 7347 verifyFormat("[image_rep drawInRect:drawRect\n" 7348 " fromRect:NSZeroRect\n" 7349 " operation:NSCompositeCopy\n" 7350 " fraction:1.0\n" 7351 " respectFlipped:NO\n" 7352 " hints:nil];"); 7353 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7355 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7357 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7358 " aaaaaaaaaaaaaaaaaaaaaa];"); 7359 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7360 " .aaaaaaaa];", // FIXME: Indentation seems off. 7361 getLLVMStyleWithColumns(60)); 7362 7363 verifyFormat( 7364 "scoped_nsobject<NSTextField> message(\n" 7365 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7366 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7367 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7368 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7369 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7370 " aaaa:bbb];"); 7371 verifyFormat("[self param:function( //\n" 7372 " parameter)]"); 7373 verifyFormat( 7374 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7375 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7376 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7377 7378 // FIXME: This violates the column limit. 7379 verifyFormat( 7380 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7381 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7382 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7383 getLLVMStyleWithColumns(60)); 7384 7385 // Variadic parameters. 7386 verifyFormat( 7387 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7388 verifyFormat( 7389 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7390 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7391 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7392 verifyFormat("[self // break\n" 7393 " a:a\n" 7394 " aaa:aaa];"); 7395 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7396 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7397 } 7398 7399 TEST_F(FormatTest, ObjCAt) { 7400 verifyFormat("@autoreleasepool"); 7401 verifyFormat("@catch"); 7402 verifyFormat("@class"); 7403 verifyFormat("@compatibility_alias"); 7404 verifyFormat("@defs"); 7405 verifyFormat("@dynamic"); 7406 verifyFormat("@encode"); 7407 verifyFormat("@end"); 7408 verifyFormat("@finally"); 7409 verifyFormat("@implementation"); 7410 verifyFormat("@import"); 7411 verifyFormat("@interface"); 7412 verifyFormat("@optional"); 7413 verifyFormat("@package"); 7414 verifyFormat("@private"); 7415 verifyFormat("@property"); 7416 verifyFormat("@protected"); 7417 verifyFormat("@protocol"); 7418 verifyFormat("@public"); 7419 verifyFormat("@required"); 7420 verifyFormat("@selector"); 7421 verifyFormat("@synchronized"); 7422 verifyFormat("@synthesize"); 7423 verifyFormat("@throw"); 7424 verifyFormat("@try"); 7425 7426 EXPECT_EQ("@interface", format("@ interface")); 7427 7428 // The precise formatting of this doesn't matter, nobody writes code like 7429 // this. 7430 verifyFormat("@ /*foo*/ interface"); 7431 } 7432 7433 TEST_F(FormatTest, ObjCSnippets) { 7434 verifyFormat("@autoreleasepool {\n" 7435 " foo();\n" 7436 "}"); 7437 verifyFormat("@class Foo, Bar;"); 7438 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7439 verifyFormat("@dynamic textColor;"); 7440 verifyFormat("char *buf1 = @encode(int *);"); 7441 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7442 verifyFormat("char *buf1 = @encode(int **);"); 7443 verifyFormat("Protocol *proto = @protocol(p1);"); 7444 verifyFormat("SEL s = @selector(foo:);"); 7445 verifyFormat("@synchronized(self) {\n" 7446 " f();\n" 7447 "}"); 7448 7449 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7450 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7451 7452 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7453 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7454 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7455 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7456 getMozillaStyle()); 7457 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7458 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7459 getWebKitStyle()); 7460 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7461 7462 verifyFormat("@import foo.bar;\n" 7463 "@import baz;"); 7464 } 7465 7466 TEST_F(FormatTest, ObjCForIn) { 7467 verifyFormat("- (void)test {\n" 7468 " for (NSString *n in arrayOfStrings) {\n" 7469 " foo(n);\n" 7470 " }\n" 7471 "}"); 7472 verifyFormat("- (void)test {\n" 7473 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 7474 " foo(n);\n" 7475 " }\n" 7476 "}"); 7477 } 7478 7479 TEST_F(FormatTest, ObjCLiterals) { 7480 verifyFormat("@\"String\""); 7481 verifyFormat("@1"); 7482 verifyFormat("@+4.8"); 7483 verifyFormat("@-4"); 7484 verifyFormat("@1LL"); 7485 verifyFormat("@.5"); 7486 verifyFormat("@'c'"); 7487 verifyFormat("@true"); 7488 7489 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7490 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7491 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7492 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7493 7494 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7495 } 7496 7497 TEST_F(FormatTest, ObjCDictLiterals) { 7498 verifyFormat("@{"); 7499 verifyFormat("@{}"); 7500 verifyFormat("@{@\"one\" : @1}"); 7501 verifyFormat("return @{@\"one\" : @1;"); 7502 verifyFormat("@{@\"one\" : @1}"); 7503 7504 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7505 verifyFormat("@{\n" 7506 " @\"one\" : @{@2 : @1},\n" 7507 "}"); 7508 7509 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7510 verifyIncompleteFormat("[self setDict:@{}"); 7511 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7512 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7513 verifyFormat( 7514 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7515 verifyFormat( 7516 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7517 7518 verifyFormat("NSDictionary *d = @{\n" 7519 " @\"nam\" : NSUserNam(),\n" 7520 " @\"dte\" : [NSDate date],\n" 7521 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7522 "};"); 7523 verifyFormat( 7524 "@{\n" 7525 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7526 "regularFont,\n" 7527 "};"); 7528 verifyGoogleFormat( 7529 "@{\n" 7530 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7531 "regularFont,\n" 7532 "};"); 7533 verifyFormat( 7534 "@{\n" 7535 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7536 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7537 "};"); 7538 7539 // We should try to be robust in case someone forgets the "@". 7540 verifyFormat("NSDictionary *d = {\n" 7541 " @\"nam\" : NSUserNam(),\n" 7542 " @\"dte\" : [NSDate date],\n" 7543 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7544 "};"); 7545 verifyFormat("NSMutableDictionary *dictionary =\n" 7546 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7547 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7548 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7549 " cccccccccccccccc : ccccccccccccccc\n" 7550 " }];"); 7551 7552 // Ensure that casts before the key are kept on the same line as the key. 7553 verifyFormat( 7554 "NSDictionary *d = @{\n" 7555 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7556 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 7557 "};"); 7558 } 7559 7560 TEST_F(FormatTest, ObjCArrayLiterals) { 7561 verifyIncompleteFormat("@["); 7562 verifyFormat("@[]"); 7563 verifyFormat( 7564 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7565 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7566 verifyFormat("NSArray *array = @[ [foo description] ];"); 7567 7568 verifyFormat( 7569 "NSArray *some_variable = @[\n" 7570 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7571 " @\"aaaaaaaaaaaaaaaaa\",\n" 7572 " @\"aaaaaaaaaaaaaaaaa\",\n" 7573 " @\"aaaaaaaaaaaaaaaaa\"\n" 7574 "];"); 7575 verifyFormat("NSArray *some_variable = @[\n" 7576 " @\"aaaaaaaaaaaaaaaaa\",\n" 7577 " @\"aaaaaaaaaaaaaaaaa\",\n" 7578 " @\"aaaaaaaaaaaaaaaaa\",\n" 7579 " @\"aaaaaaaaaaaaaaaaa\",\n" 7580 "];"); 7581 verifyGoogleFormat("NSArray *some_variable = @[\n" 7582 " @\"aaaaaaaaaaaaaaaaa\",\n" 7583 " @\"aaaaaaaaaaaaaaaaa\",\n" 7584 " @\"aaaaaaaaaaaaaaaaa\",\n" 7585 " @\"aaaaaaaaaaaaaaaaa\"\n" 7586 "];"); 7587 verifyFormat("NSArray *array = @[\n" 7588 " @\"a\",\n" 7589 " @\"a\",\n" // Trailing comma -> one per line. 7590 "];"); 7591 7592 // We should try to be robust in case someone forgets the "@". 7593 verifyFormat("NSArray *some_variable = [\n" 7594 " @\"aaaaaaaaaaaaaaaaa\",\n" 7595 " @\"aaaaaaaaaaaaaaaaa\",\n" 7596 " @\"aaaaaaaaaaaaaaaaa\",\n" 7597 " @\"aaaaaaaaaaaaaaaaa\",\n" 7598 "];"); 7599 verifyFormat( 7600 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7601 " index:(NSUInteger)index\n" 7602 " nonDigitAttributes:\n" 7603 " (NSDictionary *)noDigitAttributes;"); 7604 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7605 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7606 "]];"); 7607 } 7608 7609 TEST_F(FormatTest, BreaksStringLiterals) { 7610 EXPECT_EQ("\"some text \"\n" 7611 "\"other\";", 7612 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7613 EXPECT_EQ("\"some text \"\n" 7614 "\"other\";", 7615 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7616 EXPECT_EQ( 7617 "#define A \\\n" 7618 " \"some \" \\\n" 7619 " \"text \" \\\n" 7620 " \"other\";", 7621 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7622 EXPECT_EQ( 7623 "#define A \\\n" 7624 " \"so \" \\\n" 7625 " \"text \" \\\n" 7626 " \"other\";", 7627 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7628 7629 EXPECT_EQ("\"some text\"", 7630 format("\"some text\"", getLLVMStyleWithColumns(1))); 7631 EXPECT_EQ("\"some text\"", 7632 format("\"some text\"", getLLVMStyleWithColumns(11))); 7633 EXPECT_EQ("\"some \"\n" 7634 "\"text\"", 7635 format("\"some text\"", getLLVMStyleWithColumns(10))); 7636 EXPECT_EQ("\"some \"\n" 7637 "\"text\"", 7638 format("\"some text\"", getLLVMStyleWithColumns(7))); 7639 EXPECT_EQ("\"some\"\n" 7640 "\" tex\"\n" 7641 "\"t\"", 7642 format("\"some text\"", getLLVMStyleWithColumns(6))); 7643 EXPECT_EQ("\"some\"\n" 7644 "\" tex\"\n" 7645 "\" and\"", 7646 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7647 EXPECT_EQ("\"some\"\n" 7648 "\"/tex\"\n" 7649 "\"/and\"", 7650 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7651 7652 EXPECT_EQ("variable =\n" 7653 " \"long string \"\n" 7654 " \"literal\";", 7655 format("variable = \"long string literal\";", 7656 getLLVMStyleWithColumns(20))); 7657 7658 EXPECT_EQ("variable = f(\n" 7659 " \"long string \"\n" 7660 " \"literal\",\n" 7661 " short,\n" 7662 " loooooooooooooooooooong);", 7663 format("variable = f(\"long string literal\", short, " 7664 "loooooooooooooooooooong);", 7665 getLLVMStyleWithColumns(20))); 7666 7667 EXPECT_EQ( 7668 "f(g(\"long string \"\n" 7669 " \"literal\"),\n" 7670 " b);", 7671 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7672 EXPECT_EQ("f(g(\"long string \"\n" 7673 " \"literal\",\n" 7674 " a),\n" 7675 " b);", 7676 format("f(g(\"long string literal\", a), b);", 7677 getLLVMStyleWithColumns(20))); 7678 EXPECT_EQ( 7679 "f(\"one two\".split(\n" 7680 " variable));", 7681 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7682 EXPECT_EQ("f(\"one two three four five six \"\n" 7683 " \"seven\".split(\n" 7684 " really_looooong_variable));", 7685 format("f(\"one two three four five six seven\"." 7686 "split(really_looooong_variable));", 7687 getLLVMStyleWithColumns(33))); 7688 7689 EXPECT_EQ("f(\"some \"\n" 7690 " \"text\",\n" 7691 " other);", 7692 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7693 7694 // Only break as a last resort. 7695 verifyFormat( 7696 "aaaaaaaaaaaaaaaaaaaa(\n" 7697 " aaaaaaaaaaaaaaaaaaaa,\n" 7698 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7699 7700 EXPECT_EQ("\"splitmea\"\n" 7701 "\"trandomp\"\n" 7702 "\"oint\"", 7703 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7704 7705 EXPECT_EQ("\"split/\"\n" 7706 "\"pathat/\"\n" 7707 "\"slashes\"", 7708 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7709 7710 EXPECT_EQ("\"split/\"\n" 7711 "\"pathat/\"\n" 7712 "\"slashes\"", 7713 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7714 EXPECT_EQ("\"split at \"\n" 7715 "\"spaces/at/\"\n" 7716 "\"slashes.at.any$\"\n" 7717 "\"non-alphanumeric%\"\n" 7718 "\"1111111111characte\"\n" 7719 "\"rs\"", 7720 format("\"split at " 7721 "spaces/at/" 7722 "slashes.at." 7723 "any$non-" 7724 "alphanumeric%" 7725 "1111111111characte" 7726 "rs\"", 7727 getLLVMStyleWithColumns(20))); 7728 7729 // Verify that splitting the strings understands 7730 // Style::AlwaysBreakBeforeMultilineStrings. 7731 EXPECT_EQ( 7732 "aaaaaaaaaaaa(\n" 7733 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7734 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7735 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7736 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7737 "aaaaaaaaaaaaaaaaaaaaaa\");", 7738 getGoogleStyle())); 7739 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7740 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7741 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7742 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7743 "aaaaaaaaaaaaaaaaaaaaaa\";", 7744 getGoogleStyle())); 7745 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7746 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7747 format("llvm::outs() << " 7748 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7749 "aaaaaaaaaaaaaaaaaaa\";")); 7750 EXPECT_EQ("ffff(\n" 7751 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7752 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7753 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7754 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7755 getGoogleStyle())); 7756 7757 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7758 AlignLeft.AlignEscapedNewlinesLeft = true; 7759 EXPECT_EQ("#define A \\\n" 7760 " \"some \" \\\n" 7761 " \"text \" \\\n" 7762 " \"other\";", 7763 format("#define A \"some text other\";", AlignLeft)); 7764 } 7765 7766 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7767 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7768 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7769 EXPECT_EQ("int i = a(b());", 7770 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7771 } 7772 7773 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7774 EXPECT_EQ( 7775 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7776 "(\n" 7777 " \"x\t\");", 7778 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7779 "aaaaaaa(" 7780 "\"x\t\");")); 7781 } 7782 7783 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7784 EXPECT_EQ( 7785 "u8\"utf8 string \"\n" 7786 "u8\"literal\";", 7787 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7788 EXPECT_EQ( 7789 "u\"utf16 string \"\n" 7790 "u\"literal\";", 7791 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7792 EXPECT_EQ( 7793 "U\"utf32 string \"\n" 7794 "U\"literal\";", 7795 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7796 EXPECT_EQ("L\"wide string \"\n" 7797 "L\"literal\";", 7798 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7799 EXPECT_EQ("@\"NSString \"\n" 7800 "@\"literal\";", 7801 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7802 7803 // This input makes clang-format try to split the incomplete unicode escape 7804 // sequence, which used to lead to a crasher. 7805 verifyNoCrash( 7806 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7807 getLLVMStyleWithColumns(60)); 7808 } 7809 7810 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7811 FormatStyle Style = getGoogleStyleWithColumns(15); 7812 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7813 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7814 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7815 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7816 EXPECT_EQ("u8R\"x(raw literal)x\";", 7817 format("u8R\"x(raw literal)x\";", Style)); 7818 } 7819 7820 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7821 FormatStyle Style = getLLVMStyleWithColumns(20); 7822 EXPECT_EQ( 7823 "_T(\"aaaaaaaaaaaaaa\")\n" 7824 "_T(\"aaaaaaaaaaaaaa\")\n" 7825 "_T(\"aaaaaaaaaaaa\")", 7826 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7827 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 7828 " _T(\"aaaaaa\"),\n" 7829 " z);", 7830 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7831 7832 // FIXME: Handle embedded spaces in one iteration. 7833 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7834 // "_T(\"aaaaaaaaaaaaa\")\n" 7835 // "_T(\"aaaaaaaaaaaaa\")\n" 7836 // "_T(\"a\")", 7837 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7838 // getLLVMStyleWithColumns(20))); 7839 EXPECT_EQ( 7840 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7841 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7842 EXPECT_EQ("f(\n" 7843 "#if !TEST\n" 7844 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7845 "#endif\n" 7846 " );", 7847 format("f(\n" 7848 "#if !TEST\n" 7849 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7850 "#endif\n" 7851 ");")); 7852 EXPECT_EQ("f(\n" 7853 "\n" 7854 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7855 format("f(\n" 7856 "\n" 7857 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7858 } 7859 7860 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7861 EXPECT_EQ( 7862 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7865 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7868 } 7869 7870 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7871 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7872 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7873 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7874 "multiline raw string literal xxxxxxxxxxxxxx\n" 7875 ")x\",\n" 7876 " a),\n" 7877 " b);", 7878 format("fffffffffff(g(R\"x(\n" 7879 "multiline raw string literal xxxxxxxxxxxxxx\n" 7880 ")x\", a), b);", 7881 getGoogleStyleWithColumns(20))); 7882 EXPECT_EQ("fffffffffff(\n" 7883 " g(R\"x(qqq\n" 7884 "multiline raw string literal xxxxxxxxxxxxxx\n" 7885 ")x\",\n" 7886 " a),\n" 7887 " b);", 7888 format("fffffffffff(g(R\"x(qqq\n" 7889 "multiline raw string literal xxxxxxxxxxxxxx\n" 7890 ")x\", a), b);", 7891 getGoogleStyleWithColumns(20))); 7892 7893 EXPECT_EQ("fffffffffff(R\"x(\n" 7894 "multiline raw string literal xxxxxxxxxxxxxx\n" 7895 ")x\");", 7896 format("fffffffffff(R\"x(\n" 7897 "multiline raw string literal xxxxxxxxxxxxxx\n" 7898 ")x\");", 7899 getGoogleStyleWithColumns(20))); 7900 EXPECT_EQ("fffffffffff(R\"x(\n" 7901 "multiline raw string literal xxxxxxxxxxxxxx\n" 7902 ")x\" + bbbbbb);", 7903 format("fffffffffff(R\"x(\n" 7904 "multiline raw string literal xxxxxxxxxxxxxx\n" 7905 ")x\" + bbbbbb);", 7906 getGoogleStyleWithColumns(20))); 7907 EXPECT_EQ("fffffffffff(\n" 7908 " R\"x(\n" 7909 "multiline raw string literal xxxxxxxxxxxxxx\n" 7910 ")x\" +\n" 7911 " bbbbbb);", 7912 format("fffffffffff(\n" 7913 " R\"x(\n" 7914 "multiline raw string literal xxxxxxxxxxxxxx\n" 7915 ")x\" + bbbbbb);", 7916 getGoogleStyleWithColumns(20))); 7917 } 7918 7919 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7920 verifyFormat("string a = \"unterminated;"); 7921 EXPECT_EQ("function(\"unterminated,\n" 7922 " OtherParameter);", 7923 format("function( \"unterminated,\n" 7924 " OtherParameter);")); 7925 } 7926 7927 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7928 FormatStyle Style = getLLVMStyle(); 7929 Style.Standard = FormatStyle::LS_Cpp03; 7930 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7931 format("#define x(_a) printf(\"foo\"_a);", Style)); 7932 } 7933 7934 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7935 7936 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7937 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7938 " \"ddeeefff\");", 7939 format("someFunction(\"aaabbbcccdddeeefff\");", 7940 getLLVMStyleWithColumns(25))); 7941 EXPECT_EQ("someFunction1234567890(\n" 7942 " \"aaabbbcccdddeeefff\");", 7943 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7944 getLLVMStyleWithColumns(26))); 7945 EXPECT_EQ("someFunction1234567890(\n" 7946 " \"aaabbbcccdddeeeff\"\n" 7947 " \"f\");", 7948 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7949 getLLVMStyleWithColumns(25))); 7950 EXPECT_EQ("someFunction1234567890(\n" 7951 " \"aaabbbcccdddeeeff\"\n" 7952 " \"f\");", 7953 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7954 getLLVMStyleWithColumns(24))); 7955 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7956 " \"ddde \"\n" 7957 " \"efff\");", 7958 format("someFunction(\"aaabbbcc ddde efff\");", 7959 getLLVMStyleWithColumns(25))); 7960 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7961 " \"ddeeefff\");", 7962 format("someFunction(\"aaabbbccc ddeeefff\");", 7963 getLLVMStyleWithColumns(25))); 7964 EXPECT_EQ("someFunction1234567890(\n" 7965 " \"aaabb \"\n" 7966 " \"cccdddeeefff\");", 7967 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7968 getLLVMStyleWithColumns(25))); 7969 EXPECT_EQ("#define A \\\n" 7970 " string s = \\\n" 7971 " \"123456789\" \\\n" 7972 " \"0\"; \\\n" 7973 " int i;", 7974 format("#define A string s = \"1234567890\"; int i;", 7975 getLLVMStyleWithColumns(20))); 7976 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7977 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7978 " \"dddeeeff\"\n" 7979 " \"f\");", 7980 format("someFunction(\"aaabbbcc dddeeefff\");", 7981 getLLVMStyleWithColumns(25))); 7982 } 7983 7984 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7985 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7986 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7987 EXPECT_EQ("\"test\"\n" 7988 "\"\\n\"", 7989 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7990 EXPECT_EQ("\"tes\\\\\"\n" 7991 "\"n\"", 7992 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7993 EXPECT_EQ("\"\\\\\\\\\"\n" 7994 "\"\\n\"", 7995 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7996 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7997 EXPECT_EQ("\"\\uff01\"\n" 7998 "\"test\"", 7999 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8000 EXPECT_EQ("\"\\Uff01ff02\"", 8001 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8002 EXPECT_EQ("\"\\x000000000001\"\n" 8003 "\"next\"", 8004 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8005 EXPECT_EQ("\"\\x000000000001next\"", 8006 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8007 EXPECT_EQ("\"\\x000000000001\"", 8008 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8009 EXPECT_EQ("\"test\"\n" 8010 "\"\\000000\"\n" 8011 "\"000001\"", 8012 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8013 EXPECT_EQ("\"test\\000\"\n" 8014 "\"00000000\"\n" 8015 "\"1\"", 8016 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8017 } 8018 8019 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8020 verifyFormat("void f() {\n" 8021 " return g() {}\n" 8022 " void h() {}"); 8023 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8024 "g();\n" 8025 "}"); 8026 } 8027 8028 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8029 verifyFormat( 8030 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8031 } 8032 8033 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8034 verifyFormat("class X {\n" 8035 " void f() {\n" 8036 " }\n" 8037 "};", 8038 getLLVMStyleWithColumns(12)); 8039 } 8040 8041 TEST_F(FormatTest, ConfigurableIndentWidth) { 8042 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8043 EightIndent.IndentWidth = 8; 8044 EightIndent.ContinuationIndentWidth = 8; 8045 verifyFormat("void f() {\n" 8046 " someFunction();\n" 8047 " if (true) {\n" 8048 " f();\n" 8049 " }\n" 8050 "}", 8051 EightIndent); 8052 verifyFormat("class X {\n" 8053 " void f() {\n" 8054 " }\n" 8055 "};", 8056 EightIndent); 8057 verifyFormat("int x[] = {\n" 8058 " call(),\n" 8059 " call()};", 8060 EightIndent); 8061 } 8062 8063 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8064 verifyFormat("double\n" 8065 "f();", 8066 getLLVMStyleWithColumns(8)); 8067 } 8068 8069 TEST_F(FormatTest, ConfigurableUseOfTab) { 8070 FormatStyle Tab = getLLVMStyleWithColumns(42); 8071 Tab.IndentWidth = 8; 8072 Tab.UseTab = FormatStyle::UT_Always; 8073 Tab.AlignEscapedNewlinesLeft = true; 8074 8075 EXPECT_EQ("if (aaaaaaaa && // q\n" 8076 " bb)\t\t// w\n" 8077 "\t;", 8078 format("if (aaaaaaaa &&// q\n" 8079 "bb)// w\n" 8080 ";", 8081 Tab)); 8082 EXPECT_EQ("if (aaa && bbb) // w\n" 8083 "\t;", 8084 format("if(aaa&&bbb)// w\n" 8085 ";", 8086 Tab)); 8087 8088 verifyFormat("class X {\n" 8089 "\tvoid f() {\n" 8090 "\t\tsomeFunction(parameter1,\n" 8091 "\t\t\t parameter2);\n" 8092 "\t}\n" 8093 "};", 8094 Tab); 8095 verifyFormat("#define A \\\n" 8096 "\tvoid f() { \\\n" 8097 "\t\tsomeFunction( \\\n" 8098 "\t\t parameter1, \\\n" 8099 "\t\t parameter2); \\\n" 8100 "\t}", 8101 Tab); 8102 8103 Tab.TabWidth = 4; 8104 Tab.IndentWidth = 8; 8105 verifyFormat("class TabWidth4Indent8 {\n" 8106 "\t\tvoid f() {\n" 8107 "\t\t\t\tsomeFunction(parameter1,\n" 8108 "\t\t\t\t\t\t\t parameter2);\n" 8109 "\t\t}\n" 8110 "};", 8111 Tab); 8112 8113 Tab.TabWidth = 4; 8114 Tab.IndentWidth = 4; 8115 verifyFormat("class TabWidth4Indent4 {\n" 8116 "\tvoid f() {\n" 8117 "\t\tsomeFunction(parameter1,\n" 8118 "\t\t\t\t\t parameter2);\n" 8119 "\t}\n" 8120 "};", 8121 Tab); 8122 8123 Tab.TabWidth = 8; 8124 Tab.IndentWidth = 4; 8125 verifyFormat("class TabWidth8Indent4 {\n" 8126 " void f() {\n" 8127 "\tsomeFunction(parameter1,\n" 8128 "\t\t parameter2);\n" 8129 " }\n" 8130 "};", 8131 Tab); 8132 8133 Tab.TabWidth = 8; 8134 Tab.IndentWidth = 8; 8135 EXPECT_EQ("/*\n" 8136 "\t a\t\tcomment\n" 8137 "\t in multiple lines\n" 8138 " */", 8139 format(" /*\t \t \n" 8140 " \t \t a\t\tcomment\t \t\n" 8141 " \t \t in multiple lines\t\n" 8142 " \t */", 8143 Tab)); 8144 8145 Tab.UseTab = FormatStyle::UT_ForIndentation; 8146 verifyFormat("{\n" 8147 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8148 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8149 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8150 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8151 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8152 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8153 "};", 8154 Tab); 8155 verifyFormat("enum A {\n" 8156 "\ta1, // Force multiple lines\n" 8157 "\ta2,\n" 8158 "\ta3\n" 8159 "};", 8160 Tab); 8161 EXPECT_EQ("if (aaaaaaaa && // q\n" 8162 " bb) // w\n" 8163 "\t;", 8164 format("if (aaaaaaaa &&// q\n" 8165 "bb)// w\n" 8166 ";", 8167 Tab)); 8168 verifyFormat("class X {\n" 8169 "\tvoid f() {\n" 8170 "\t\tsomeFunction(parameter1,\n" 8171 "\t\t parameter2);\n" 8172 "\t}\n" 8173 "};", 8174 Tab); 8175 verifyFormat("{\n" 8176 "\tQ(\n" 8177 "\t {\n" 8178 "\t\t int a;\n" 8179 "\t\t someFunction(aaaaaaaa,\n" 8180 "\t\t bbbbbbb);\n" 8181 "\t },\n" 8182 "\t p);\n" 8183 "}", 8184 Tab); 8185 EXPECT_EQ("{\n" 8186 "\t/* aaaa\n" 8187 "\t bbbb */\n" 8188 "}", 8189 format("{\n" 8190 "/* aaaa\n" 8191 " bbbb */\n" 8192 "}", 8193 Tab)); 8194 EXPECT_EQ("{\n" 8195 "\t/*\n" 8196 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8197 "\t bbbbbbbbbbbbb\n" 8198 "\t*/\n" 8199 "}", 8200 format("{\n" 8201 "/*\n" 8202 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8203 "*/\n" 8204 "}", 8205 Tab)); 8206 EXPECT_EQ("{\n" 8207 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8208 "\t// bbbbbbbbbbbbb\n" 8209 "}", 8210 format("{\n" 8211 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8212 "}", 8213 Tab)); 8214 EXPECT_EQ("{\n" 8215 "\t/*\n" 8216 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8217 "\t bbbbbbbbbbbbb\n" 8218 "\t*/\n" 8219 "}", 8220 format("{\n" 8221 "\t/*\n" 8222 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8223 "\t*/\n" 8224 "}", 8225 Tab)); 8226 EXPECT_EQ("{\n" 8227 "\t/*\n" 8228 "\n" 8229 "\t*/\n" 8230 "}", 8231 format("{\n" 8232 "\t/*\n" 8233 "\n" 8234 "\t*/\n" 8235 "}", 8236 Tab)); 8237 EXPECT_EQ("{\n" 8238 "\t/*\n" 8239 " asdf\n" 8240 "\t*/\n" 8241 "}", 8242 format("{\n" 8243 "\t/*\n" 8244 " asdf\n" 8245 "\t*/\n" 8246 "}", 8247 Tab)); 8248 8249 Tab.UseTab = FormatStyle::UT_Never; 8250 EXPECT_EQ("/*\n" 8251 " a\t\tcomment\n" 8252 " in multiple lines\n" 8253 " */", 8254 format(" /*\t \t \n" 8255 " \t \t a\t\tcomment\t \t\n" 8256 " \t \t in multiple lines\t\n" 8257 " \t */", 8258 Tab)); 8259 EXPECT_EQ("/* some\n" 8260 " comment */", 8261 format(" \t \t /* some\n" 8262 " \t \t comment */", 8263 Tab)); 8264 EXPECT_EQ("int a; /* some\n" 8265 " comment */", 8266 format(" \t \t int a; /* some\n" 8267 " \t \t comment */", 8268 Tab)); 8269 8270 EXPECT_EQ("int a; /* some\n" 8271 "comment */", 8272 format(" \t \t int\ta; /* some\n" 8273 " \t \t comment */", 8274 Tab)); 8275 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8276 " comment */", 8277 format(" \t \t f(\"\t\t\"); /* some\n" 8278 " \t \t comment */", 8279 Tab)); 8280 EXPECT_EQ("{\n" 8281 " /*\n" 8282 " * Comment\n" 8283 " */\n" 8284 " int i;\n" 8285 "}", 8286 format("{\n" 8287 "\t/*\n" 8288 "\t * Comment\n" 8289 "\t */\n" 8290 "\t int i;\n" 8291 "}")); 8292 } 8293 8294 TEST_F(FormatTest, CalculatesOriginalColumn) { 8295 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8296 "q\"; /* some\n" 8297 " comment */", 8298 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8299 "q\"; /* some\n" 8300 " comment */", 8301 getLLVMStyle())); 8302 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8303 "/* some\n" 8304 " comment */", 8305 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8306 " /* some\n" 8307 " comment */", 8308 getLLVMStyle())); 8309 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8310 "qqq\n" 8311 "/* some\n" 8312 " comment */", 8313 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8314 "qqq\n" 8315 " /* some\n" 8316 " comment */", 8317 getLLVMStyle())); 8318 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8319 "wwww; /* some\n" 8320 " comment */", 8321 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8322 "wwww; /* some\n" 8323 " comment */", 8324 getLLVMStyle())); 8325 } 8326 8327 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8328 FormatStyle NoSpace = getLLVMStyle(); 8329 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8330 8331 verifyFormat("while(true)\n" 8332 " continue;", 8333 NoSpace); 8334 verifyFormat("for(;;)\n" 8335 " continue;", 8336 NoSpace); 8337 verifyFormat("if(true)\n" 8338 " f();\n" 8339 "else if(true)\n" 8340 " f();", 8341 NoSpace); 8342 verifyFormat("do {\n" 8343 " do_something();\n" 8344 "} while(something());", 8345 NoSpace); 8346 verifyFormat("switch(x) {\n" 8347 "default:\n" 8348 " break;\n" 8349 "}", 8350 NoSpace); 8351 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8352 verifyFormat("size_t x = sizeof(x);", NoSpace); 8353 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8354 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8355 verifyFormat("alignas(128) char a[128];", NoSpace); 8356 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8357 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8358 verifyFormat("int f() throw(Deprecated);", NoSpace); 8359 verifyFormat("typedef void (*cb)(int);", NoSpace); 8360 verifyFormat("T A::operator()();", NoSpace); 8361 verifyFormat("X A::operator++(T);", NoSpace); 8362 8363 FormatStyle Space = getLLVMStyle(); 8364 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8365 8366 verifyFormat("int f ();", Space); 8367 verifyFormat("void f (int a, T b) {\n" 8368 " while (true)\n" 8369 " continue;\n" 8370 "}", 8371 Space); 8372 verifyFormat("if (true)\n" 8373 " f ();\n" 8374 "else if (true)\n" 8375 " f ();", 8376 Space); 8377 verifyFormat("do {\n" 8378 " do_something ();\n" 8379 "} while (something ());", 8380 Space); 8381 verifyFormat("switch (x) {\n" 8382 "default:\n" 8383 " break;\n" 8384 "}", 8385 Space); 8386 verifyFormat("A::A () : a (1) {}", Space); 8387 verifyFormat("void f () __attribute__ ((asdf));", Space); 8388 verifyFormat("*(&a + 1);\n" 8389 "&((&a)[1]);\n" 8390 "a[(b + c) * d];\n" 8391 "(((a + 1) * 2) + 3) * 4;", 8392 Space); 8393 verifyFormat("#define A(x) x", Space); 8394 verifyFormat("#define A (x) x", Space); 8395 verifyFormat("#if defined(x)\n" 8396 "#endif", 8397 Space); 8398 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8399 verifyFormat("size_t x = sizeof (x);", Space); 8400 verifyFormat("auto f (int x) -> decltype (x);", Space); 8401 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8402 verifyFormat("alignas (128) char a[128];", Space); 8403 verifyFormat("size_t x = alignof (MyType);", Space); 8404 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8405 verifyFormat("int f () throw (Deprecated);", Space); 8406 verifyFormat("typedef void (*cb) (int);", Space); 8407 verifyFormat("T A::operator() ();", Space); 8408 verifyFormat("X A::operator++ (T);", Space); 8409 } 8410 8411 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8412 FormatStyle Spaces = getLLVMStyle(); 8413 8414 Spaces.SpacesInParentheses = true; 8415 verifyFormat("call( x, y, z );", Spaces); 8416 verifyFormat("call();", Spaces); 8417 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8418 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8419 Spaces); 8420 verifyFormat("while ( (bool)1 )\n" 8421 " continue;", 8422 Spaces); 8423 verifyFormat("for ( ;; )\n" 8424 " continue;", 8425 Spaces); 8426 verifyFormat("if ( true )\n" 8427 " f();\n" 8428 "else if ( true )\n" 8429 " f();", 8430 Spaces); 8431 verifyFormat("do {\n" 8432 " do_something( (int)i );\n" 8433 "} while ( something() );", 8434 Spaces); 8435 verifyFormat("switch ( x ) {\n" 8436 "default:\n" 8437 " break;\n" 8438 "}", 8439 Spaces); 8440 8441 Spaces.SpacesInParentheses = false; 8442 Spaces.SpacesInCStyleCastParentheses = true; 8443 verifyFormat("Type *A = ( Type * )P;", Spaces); 8444 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8445 verifyFormat("x = ( int32 )y;", Spaces); 8446 verifyFormat("int a = ( int )(2.0f);", Spaces); 8447 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8448 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8449 verifyFormat("#define x (( int )-1)", Spaces); 8450 8451 // Run the first set of tests again with: 8452 Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true; 8453 Spaces.SpacesInCStyleCastParentheses = true; 8454 verifyFormat("call(x, y, z);", Spaces); 8455 verifyFormat("call( );", Spaces); 8456 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8457 verifyFormat("while (( bool )1)\n" 8458 " continue;", 8459 Spaces); 8460 verifyFormat("for (;;)\n" 8461 " continue;", 8462 Spaces); 8463 verifyFormat("if (true)\n" 8464 " f( );\n" 8465 "else if (true)\n" 8466 " f( );", 8467 Spaces); 8468 verifyFormat("do {\n" 8469 " do_something(( int )i);\n" 8470 "} while (something( ));", 8471 Spaces); 8472 verifyFormat("switch (x) {\n" 8473 "default:\n" 8474 " break;\n" 8475 "}", 8476 Spaces); 8477 8478 // Run the first set of tests again with: 8479 Spaces.SpaceAfterCStyleCast = true; 8480 verifyFormat("call(x, y, z);", Spaces); 8481 verifyFormat("call( );", Spaces); 8482 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8483 verifyFormat("while (( bool ) 1)\n" 8484 " continue;", 8485 Spaces); 8486 verifyFormat("for (;;)\n" 8487 " continue;", 8488 Spaces); 8489 verifyFormat("if (true)\n" 8490 " f( );\n" 8491 "else if (true)\n" 8492 " f( );", 8493 Spaces); 8494 verifyFormat("do {\n" 8495 " do_something(( int ) i);\n" 8496 "} while (something( ));", 8497 Spaces); 8498 verifyFormat("switch (x) {\n" 8499 "default:\n" 8500 " break;\n" 8501 "}", 8502 Spaces); 8503 8504 // Run subset of tests again with: 8505 Spaces.SpacesInCStyleCastParentheses = false; 8506 Spaces.SpaceAfterCStyleCast = true; 8507 verifyFormat("while ((bool) 1)\n" 8508 " continue;", 8509 Spaces); 8510 verifyFormat("do {\n" 8511 " do_something((int) i);\n" 8512 "} while (something( ));", 8513 Spaces); 8514 } 8515 8516 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8517 verifyFormat("int a[5];"); 8518 verifyFormat("a[3] += 42;"); 8519 8520 FormatStyle Spaces = getLLVMStyle(); 8521 Spaces.SpacesInSquareBrackets = true; 8522 // Lambdas unchanged. 8523 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8524 verifyFormat("return [i, args...] {};", Spaces); 8525 8526 // Not lambdas. 8527 verifyFormat("int a[ 5 ];", Spaces); 8528 verifyFormat("a[ 3 ] += 42;", Spaces); 8529 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8530 verifyFormat("double &operator[](int i) { return 0; }\n" 8531 "int i;", 8532 Spaces); 8533 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8534 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8535 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8536 } 8537 8538 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8539 verifyFormat("int a = 5;"); 8540 verifyFormat("a += 42;"); 8541 verifyFormat("a or_eq 8;"); 8542 8543 FormatStyle Spaces = getLLVMStyle(); 8544 Spaces.SpaceBeforeAssignmentOperators = false; 8545 verifyFormat("int a= 5;", Spaces); 8546 verifyFormat("a+= 42;", Spaces); 8547 verifyFormat("a or_eq 8;", Spaces); 8548 } 8549 8550 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8551 FormatStyle Alignment = getLLVMStyle(); 8552 Alignment.AlignConsecutiveAssignments = false; 8553 verifyFormat("int a = 5;\n" 8554 "int oneTwoThree = 123;", 8555 Alignment); 8556 verifyFormat("int a = 5;\n" 8557 "int oneTwoThree = 123;", 8558 Alignment); 8559 8560 Alignment.AlignConsecutiveAssignments = true; 8561 verifyFormat("int a = 5;\n" 8562 "int oneTwoThree = 123;", 8563 Alignment); 8564 verifyFormat("int a = method();\n" 8565 "int oneTwoThree = 133;", 8566 Alignment); 8567 verifyFormat("a &= 5;\n" 8568 "bcd *= 5;\n" 8569 "ghtyf += 5;\n" 8570 "dvfvdb -= 5;\n" 8571 "a /= 5;\n" 8572 "vdsvsv %= 5;\n" 8573 "sfdbddfbdfbb ^= 5;\n" 8574 "dvsdsv |= 5;\n" 8575 "int dsvvdvsdvvv = 123;", 8576 Alignment); 8577 verifyFormat("int i = 1, j = 10;\n" 8578 "something = 2000;", 8579 Alignment); 8580 verifyFormat("something = 2000;\n" 8581 "int i = 1, j = 10;\n", 8582 Alignment); 8583 verifyFormat("something = 2000;\n" 8584 "another = 911;\n" 8585 "int i = 1, j = 10;\n" 8586 "oneMore = 1;\n" 8587 "i = 2;", 8588 Alignment); 8589 verifyFormat("int a = 5;\n" 8590 "int one = 1;\n" 8591 "method();\n" 8592 "int oneTwoThree = 123;\n" 8593 "int oneTwo = 12;", 8594 Alignment); 8595 verifyFormat("int oneTwoThree = 123;\n" 8596 "int oneTwo = 12;\n" 8597 "method();\n", 8598 Alignment); 8599 verifyFormat("int oneTwoThree = 123; // comment\n" 8600 "int oneTwo = 12; // comment", 8601 Alignment); 8602 EXPECT_EQ("int a = 5;\n" 8603 "\n" 8604 "int oneTwoThree = 123;", 8605 format("int a = 5;\n" 8606 "\n" 8607 "int oneTwoThree= 123;", 8608 Alignment)); 8609 EXPECT_EQ("int a = 5;\n" 8610 "int one = 1;\n" 8611 "\n" 8612 "int oneTwoThree = 123;", 8613 format("int a = 5;\n" 8614 "int one = 1;\n" 8615 "\n" 8616 "int oneTwoThree = 123;", 8617 Alignment)); 8618 EXPECT_EQ("int a = 5;\n" 8619 "int one = 1;\n" 8620 "\n" 8621 "int oneTwoThree = 123;\n" 8622 "int oneTwo = 12;", 8623 format("int a = 5;\n" 8624 "int one = 1;\n" 8625 "\n" 8626 "int oneTwoThree = 123;\n" 8627 "int oneTwo = 12;", 8628 Alignment)); 8629 Alignment.AlignEscapedNewlinesLeft = true; 8630 verifyFormat("#define A \\\n" 8631 " int aaaa = 12; \\\n" 8632 " int b = 23; \\\n" 8633 " int ccc = 234; \\\n" 8634 " int dddddddddd = 2345;", 8635 Alignment); 8636 Alignment.AlignEscapedNewlinesLeft = false; 8637 verifyFormat("#define A " 8638 " \\\n" 8639 " int aaaa = 12; " 8640 " \\\n" 8641 " int b = 23; " 8642 " \\\n" 8643 " int ccc = 234; " 8644 " \\\n" 8645 " int dddddddddd = 2345;", 8646 Alignment); 8647 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8648 "k = 4, int l = 5,\n" 8649 " int m = 6) {\n" 8650 " int j = 10;\n" 8651 " otherThing = 1;\n" 8652 "}", 8653 Alignment); 8654 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8655 " int i = 1;\n" 8656 " int j = 2;\n" 8657 " int big = 10000;\n" 8658 "}", 8659 Alignment); 8660 verifyFormat("class C {\n" 8661 "public:\n" 8662 " int i = 1;\n" 8663 " virtual void f() = 0;\n" 8664 "};", 8665 Alignment); 8666 verifyFormat("int i = 1;\n" 8667 "if (SomeType t = getSomething()) {\n" 8668 "}\n" 8669 "int j = 2;\n" 8670 "int big = 10000;", 8671 Alignment); 8672 verifyFormat("int j = 7;\n" 8673 "for (int k = 0; k < N; ++k) {\n" 8674 "}\n" 8675 "int j = 2;\n" 8676 "int big = 10000;\n" 8677 "}", 8678 Alignment); 8679 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8680 verifyFormat("int i = 1;\n" 8681 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8682 " = someLooooooooooooooooongFunction();\n" 8683 "int j = 2;", 8684 Alignment); 8685 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8686 verifyFormat("int i = 1;\n" 8687 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8688 " someLooooooooooooooooongFunction();\n" 8689 "int j = 2;", 8690 Alignment); 8691 8692 verifyFormat("auto lambda = []() {\n" 8693 " auto i = 0;\n" 8694 " return 0;\n" 8695 "};\n" 8696 "int i = 0;\n" 8697 "auto v = type{\n" 8698 " i = 1, //\n" 8699 " (i = 2), //\n" 8700 " i = 3 //\n" 8701 "};", 8702 Alignment); 8703 8704 // FIXME: Should align all three assignments 8705 verifyFormat( 8706 "int i = 1;\n" 8707 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8708 " loooooooooooooooooooooongParameterB);\n" 8709 "int j = 2;", 8710 Alignment); 8711 } 8712 8713 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8714 FormatStyle Alignment = getLLVMStyle(); 8715 Alignment.AlignConsecutiveDeclarations = false; 8716 verifyFormat("float const a = 5;\n" 8717 "int oneTwoThree = 123;", 8718 Alignment); 8719 verifyFormat("int a = 5;\n" 8720 "float const oneTwoThree = 123;", 8721 Alignment); 8722 8723 Alignment.AlignConsecutiveDeclarations = true; 8724 verifyFormat("float const a = 5;\n" 8725 "int oneTwoThree = 123;", 8726 Alignment); 8727 verifyFormat("int a = method();\n" 8728 "float const oneTwoThree = 133;", 8729 Alignment); 8730 verifyFormat("int i = 1, j = 10;\n" 8731 "something = 2000;", 8732 Alignment); 8733 verifyFormat("something = 2000;\n" 8734 "int i = 1, j = 10;\n", 8735 Alignment); 8736 verifyFormat("float something = 2000;\n" 8737 "double another = 911;\n" 8738 "int i = 1, j = 10;\n" 8739 "const int *oneMore = 1;\n" 8740 "unsigned i = 2;", 8741 Alignment); 8742 verifyFormat("float a = 5;\n" 8743 "int one = 1;\n" 8744 "method();\n" 8745 "const double oneTwoThree = 123;\n" 8746 "const unsigned int oneTwo = 12;", 8747 Alignment); 8748 verifyFormat("int oneTwoThree{0}; // comment\n" 8749 "unsigned oneTwo; // comment", 8750 Alignment); 8751 EXPECT_EQ("float const a = 5;\n" 8752 "\n" 8753 "int oneTwoThree = 123;", 8754 format("float const a = 5;\n" 8755 "\n" 8756 "int oneTwoThree= 123;", 8757 Alignment)); 8758 EXPECT_EQ("float a = 5;\n" 8759 "int one = 1;\n" 8760 "\n" 8761 "unsigned oneTwoThree = 123;", 8762 format("float a = 5;\n" 8763 "int one = 1;\n" 8764 "\n" 8765 "unsigned oneTwoThree = 123;", 8766 Alignment)); 8767 EXPECT_EQ("float a = 5;\n" 8768 "int one = 1;\n" 8769 "\n" 8770 "unsigned oneTwoThree = 123;\n" 8771 "int oneTwo = 12;", 8772 format("float a = 5;\n" 8773 "int one = 1;\n" 8774 "\n" 8775 "unsigned oneTwoThree = 123;\n" 8776 "int oneTwo = 12;", 8777 Alignment)); 8778 Alignment.AlignConsecutiveAssignments = true; 8779 verifyFormat("float something = 2000;\n" 8780 "double another = 911;\n" 8781 "int i = 1, j = 10;\n" 8782 "const int *oneMore = 1;\n" 8783 "unsigned i = 2;", 8784 Alignment); 8785 verifyFormat("int oneTwoThree = {0}; // comment\n" 8786 "unsigned oneTwo = 0; // comment", 8787 Alignment); 8788 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8789 " int const i = 1;\n" 8790 " int * j = 2;\n" 8791 " int big = 10000;\n" 8792 "\n" 8793 " unsigned oneTwoThree = 123;\n" 8794 " int oneTwo = 12;\n" 8795 " method();\n" 8796 " float k = 2;\n" 8797 " int ll = 10000;\n" 8798 "}", 8799 format("void SomeFunction(int parameter= 0) {\n" 8800 " int const i= 1;\n" 8801 " int *j=2;\n" 8802 " int big = 10000;\n" 8803 "\n" 8804 "unsigned oneTwoThree =123;\n" 8805 "int oneTwo = 12;\n" 8806 " method();\n" 8807 "float k= 2;\n" 8808 "int ll=10000;\n" 8809 "}", 8810 Alignment)); 8811 Alignment.AlignConsecutiveAssignments = false; 8812 Alignment.AlignEscapedNewlinesLeft = true; 8813 verifyFormat("#define A \\\n" 8814 " int aaaa = 12; \\\n" 8815 " float b = 23; \\\n" 8816 " const int ccc = 234; \\\n" 8817 " unsigned dddddddddd = 2345;", 8818 Alignment); 8819 Alignment.AlignEscapedNewlinesLeft = false; 8820 Alignment.ColumnLimit = 30; 8821 verifyFormat("#define A \\\n" 8822 " int aaaa = 12; \\\n" 8823 " float b = 23; \\\n" 8824 " const int ccc = 234; \\\n" 8825 " int dddddddddd = 2345;", 8826 Alignment); 8827 Alignment.ColumnLimit = 80; 8828 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8829 "k = 4, int l = 5,\n" 8830 " int m = 6) {\n" 8831 " const int j = 10;\n" 8832 " otherThing = 1;\n" 8833 "}", 8834 Alignment); 8835 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8836 " int const i = 1;\n" 8837 " int * j = 2;\n" 8838 " int big = 10000;\n" 8839 "}", 8840 Alignment); 8841 verifyFormat("class C {\n" 8842 "public:\n" 8843 " int i = 1;\n" 8844 " virtual void f() = 0;\n" 8845 "};", 8846 Alignment); 8847 verifyFormat("float i = 1;\n" 8848 "if (SomeType t = getSomething()) {\n" 8849 "}\n" 8850 "const unsigned j = 2;\n" 8851 "int big = 10000;", 8852 Alignment); 8853 verifyFormat("float j = 7;\n" 8854 "for (int k = 0; k < N; ++k) {\n" 8855 "}\n" 8856 "unsigned j = 2;\n" 8857 "int big = 10000;\n" 8858 "}", 8859 Alignment); 8860 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8861 verifyFormat("float i = 1;\n" 8862 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8863 " = someLooooooooooooooooongFunction();\n" 8864 "int j = 2;", 8865 Alignment); 8866 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8867 verifyFormat("int i = 1;\n" 8868 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8869 " someLooooooooooooooooongFunction();\n" 8870 "int j = 2;", 8871 Alignment); 8872 8873 Alignment.AlignConsecutiveAssignments = true; 8874 verifyFormat("auto lambda = []() {\n" 8875 " auto ii = 0;\n" 8876 " float j = 0;\n" 8877 " return 0;\n" 8878 "};\n" 8879 "int i = 0;\n" 8880 "float i2 = 0;\n" 8881 "auto v = type{\n" 8882 " i = 1, //\n" 8883 " (i = 2), //\n" 8884 " i = 3 //\n" 8885 "};", 8886 Alignment); 8887 Alignment.AlignConsecutiveAssignments = false; 8888 8889 // FIXME: Should align all three declarations 8890 verifyFormat( 8891 "int i = 1;\n" 8892 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8893 " loooooooooooooooooooooongParameterB);\n" 8894 "int j = 2;", 8895 Alignment); 8896 8897 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8898 // We expect declarations and assignments to align, as long as it doesn't 8899 // exceed the column limit, starting a new alignemnt sequence whenever it 8900 // happens. 8901 Alignment.AlignConsecutiveAssignments = true; 8902 Alignment.ColumnLimit = 30; 8903 verifyFormat("float ii = 1;\n" 8904 "unsigned j = 2;\n" 8905 "int someVerylongVariable = 1;\n" 8906 "AnotherLongType ll = 123456;\n" 8907 "VeryVeryLongType k = 2;\n" 8908 "int myvar = 1;", 8909 Alignment); 8910 Alignment.ColumnLimit = 80; 8911 } 8912 8913 TEST_F(FormatTest, LinuxBraceBreaking) { 8914 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8915 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8916 verifyFormat("namespace a\n" 8917 "{\n" 8918 "class A\n" 8919 "{\n" 8920 " void f()\n" 8921 " {\n" 8922 " if (true) {\n" 8923 " a();\n" 8924 " b();\n" 8925 " }\n" 8926 " }\n" 8927 " void g() { return; }\n" 8928 "};\n" 8929 "struct B {\n" 8930 " int x;\n" 8931 "};\n" 8932 "}\n", 8933 LinuxBraceStyle); 8934 verifyFormat("enum X {\n" 8935 " Y = 0,\n" 8936 "}\n", 8937 LinuxBraceStyle); 8938 verifyFormat("struct S {\n" 8939 " int Type;\n" 8940 " union {\n" 8941 " int x;\n" 8942 " double y;\n" 8943 " } Value;\n" 8944 " class C\n" 8945 " {\n" 8946 " MyFavoriteType Value;\n" 8947 " } Class;\n" 8948 "}\n", 8949 LinuxBraceStyle); 8950 } 8951 8952 TEST_F(FormatTest, MozillaBraceBreaking) { 8953 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8954 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8955 verifyFormat("namespace a {\n" 8956 "class A\n" 8957 "{\n" 8958 " void f()\n" 8959 " {\n" 8960 " if (true) {\n" 8961 " a();\n" 8962 " b();\n" 8963 " }\n" 8964 " }\n" 8965 " void g() { return; }\n" 8966 "};\n" 8967 "enum E\n" 8968 "{\n" 8969 " A,\n" 8970 " // foo\n" 8971 " B,\n" 8972 " C\n" 8973 "};\n" 8974 "struct B\n" 8975 "{\n" 8976 " int x;\n" 8977 "};\n" 8978 "}\n", 8979 MozillaBraceStyle); 8980 verifyFormat("struct S\n" 8981 "{\n" 8982 " int Type;\n" 8983 " union\n" 8984 " {\n" 8985 " int x;\n" 8986 " double y;\n" 8987 " } Value;\n" 8988 " class C\n" 8989 " {\n" 8990 " MyFavoriteType Value;\n" 8991 " } Class;\n" 8992 "}\n", 8993 MozillaBraceStyle); 8994 } 8995 8996 TEST_F(FormatTest, StroustrupBraceBreaking) { 8997 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8998 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8999 verifyFormat("namespace a {\n" 9000 "class A {\n" 9001 " void f()\n" 9002 " {\n" 9003 " if (true) {\n" 9004 " a();\n" 9005 " b();\n" 9006 " }\n" 9007 " }\n" 9008 " void g() { return; }\n" 9009 "};\n" 9010 "struct B {\n" 9011 " int x;\n" 9012 "};\n" 9013 "}\n", 9014 StroustrupBraceStyle); 9015 9016 verifyFormat("void foo()\n" 9017 "{\n" 9018 " if (a) {\n" 9019 " a();\n" 9020 " }\n" 9021 " else {\n" 9022 " b();\n" 9023 " }\n" 9024 "}\n", 9025 StroustrupBraceStyle); 9026 9027 verifyFormat("#ifdef _DEBUG\n" 9028 "int foo(int i = 0)\n" 9029 "#else\n" 9030 "int foo(int i = 5)\n" 9031 "#endif\n" 9032 "{\n" 9033 " return i;\n" 9034 "}", 9035 StroustrupBraceStyle); 9036 9037 verifyFormat("void foo() {}\n" 9038 "void bar()\n" 9039 "#ifdef _DEBUG\n" 9040 "{\n" 9041 " foo();\n" 9042 "}\n" 9043 "#else\n" 9044 "{\n" 9045 "}\n" 9046 "#endif", 9047 StroustrupBraceStyle); 9048 9049 verifyFormat("void foobar() { int i = 5; }\n" 9050 "#ifdef _DEBUG\n" 9051 "void bar() {}\n" 9052 "#else\n" 9053 "void bar() { foobar(); }\n" 9054 "#endif", 9055 StroustrupBraceStyle); 9056 } 9057 9058 TEST_F(FormatTest, AllmanBraceBreaking) { 9059 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9060 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9061 verifyFormat("namespace a\n" 9062 "{\n" 9063 "class A\n" 9064 "{\n" 9065 " void f()\n" 9066 " {\n" 9067 " if (true)\n" 9068 " {\n" 9069 " a();\n" 9070 " b();\n" 9071 " }\n" 9072 " }\n" 9073 " void g() { return; }\n" 9074 "};\n" 9075 "struct B\n" 9076 "{\n" 9077 " int x;\n" 9078 "};\n" 9079 "}", 9080 AllmanBraceStyle); 9081 9082 verifyFormat("void f()\n" 9083 "{\n" 9084 " if (true)\n" 9085 " {\n" 9086 " a();\n" 9087 " }\n" 9088 " else if (false)\n" 9089 " {\n" 9090 " b();\n" 9091 " }\n" 9092 " else\n" 9093 " {\n" 9094 " c();\n" 9095 " }\n" 9096 "}\n", 9097 AllmanBraceStyle); 9098 9099 verifyFormat("void f()\n" 9100 "{\n" 9101 " for (int i = 0; i < 10; ++i)\n" 9102 " {\n" 9103 " a();\n" 9104 " }\n" 9105 " while (false)\n" 9106 " {\n" 9107 " b();\n" 9108 " }\n" 9109 " do\n" 9110 " {\n" 9111 " c();\n" 9112 " } while (false)\n" 9113 "}\n", 9114 AllmanBraceStyle); 9115 9116 verifyFormat("void f(int a)\n" 9117 "{\n" 9118 " switch (a)\n" 9119 " {\n" 9120 " case 0:\n" 9121 " break;\n" 9122 " case 1:\n" 9123 " {\n" 9124 " break;\n" 9125 " }\n" 9126 " case 2:\n" 9127 " {\n" 9128 " }\n" 9129 " break;\n" 9130 " default:\n" 9131 " break;\n" 9132 " }\n" 9133 "}\n", 9134 AllmanBraceStyle); 9135 9136 verifyFormat("enum X\n" 9137 "{\n" 9138 " Y = 0,\n" 9139 "}\n", 9140 AllmanBraceStyle); 9141 verifyFormat("enum X\n" 9142 "{\n" 9143 " Y = 0\n" 9144 "}\n", 9145 AllmanBraceStyle); 9146 9147 verifyFormat("@interface BSApplicationController ()\n" 9148 "{\n" 9149 "@private\n" 9150 " id _extraIvar;\n" 9151 "}\n" 9152 "@end\n", 9153 AllmanBraceStyle); 9154 9155 verifyFormat("#ifdef _DEBUG\n" 9156 "int foo(int i = 0)\n" 9157 "#else\n" 9158 "int foo(int i = 5)\n" 9159 "#endif\n" 9160 "{\n" 9161 " return i;\n" 9162 "}", 9163 AllmanBraceStyle); 9164 9165 verifyFormat("void foo() {}\n" 9166 "void bar()\n" 9167 "#ifdef _DEBUG\n" 9168 "{\n" 9169 " foo();\n" 9170 "}\n" 9171 "#else\n" 9172 "{\n" 9173 "}\n" 9174 "#endif", 9175 AllmanBraceStyle); 9176 9177 verifyFormat("void foobar() { int i = 5; }\n" 9178 "#ifdef _DEBUG\n" 9179 "void bar() {}\n" 9180 "#else\n" 9181 "void bar() { foobar(); }\n" 9182 "#endif", 9183 AllmanBraceStyle); 9184 9185 // This shouldn't affect ObjC blocks.. 9186 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9187 " // ...\n" 9188 " int i;\n" 9189 "}];", 9190 AllmanBraceStyle); 9191 verifyFormat("void (^block)(void) = ^{\n" 9192 " // ...\n" 9193 " int i;\n" 9194 "};", 9195 AllmanBraceStyle); 9196 // .. or dict literals. 9197 verifyFormat("void f()\n" 9198 "{\n" 9199 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9200 "}", 9201 AllmanBraceStyle); 9202 verifyFormat("int f()\n" 9203 "{ // comment\n" 9204 " return 42;\n" 9205 "}", 9206 AllmanBraceStyle); 9207 9208 AllmanBraceStyle.ColumnLimit = 19; 9209 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9210 AllmanBraceStyle.ColumnLimit = 18; 9211 verifyFormat("void f()\n" 9212 "{\n" 9213 " int i;\n" 9214 "}", 9215 AllmanBraceStyle); 9216 AllmanBraceStyle.ColumnLimit = 80; 9217 9218 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9219 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9220 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9221 verifyFormat("void f(bool b)\n" 9222 "{\n" 9223 " if (b)\n" 9224 " {\n" 9225 " return;\n" 9226 " }\n" 9227 "}\n", 9228 BreakBeforeBraceShortIfs); 9229 verifyFormat("void f(bool b)\n" 9230 "{\n" 9231 " if (b) return;\n" 9232 "}\n", 9233 BreakBeforeBraceShortIfs); 9234 verifyFormat("void f(bool b)\n" 9235 "{\n" 9236 " while (b)\n" 9237 " {\n" 9238 " return;\n" 9239 " }\n" 9240 "}\n", 9241 BreakBeforeBraceShortIfs); 9242 } 9243 9244 TEST_F(FormatTest, GNUBraceBreaking) { 9245 FormatStyle GNUBraceStyle = getLLVMStyle(); 9246 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9247 verifyFormat("namespace a\n" 9248 "{\n" 9249 "class A\n" 9250 "{\n" 9251 " void f()\n" 9252 " {\n" 9253 " int a;\n" 9254 " {\n" 9255 " int b;\n" 9256 " }\n" 9257 " if (true)\n" 9258 " {\n" 9259 " a();\n" 9260 " b();\n" 9261 " }\n" 9262 " }\n" 9263 " void g() { return; }\n" 9264 "}\n" 9265 "}", 9266 GNUBraceStyle); 9267 9268 verifyFormat("void f()\n" 9269 "{\n" 9270 " if (true)\n" 9271 " {\n" 9272 " a();\n" 9273 " }\n" 9274 " else if (false)\n" 9275 " {\n" 9276 " b();\n" 9277 " }\n" 9278 " else\n" 9279 " {\n" 9280 " c();\n" 9281 " }\n" 9282 "}\n", 9283 GNUBraceStyle); 9284 9285 verifyFormat("void f()\n" 9286 "{\n" 9287 " for (int i = 0; i < 10; ++i)\n" 9288 " {\n" 9289 " a();\n" 9290 " }\n" 9291 " while (false)\n" 9292 " {\n" 9293 " b();\n" 9294 " }\n" 9295 " do\n" 9296 " {\n" 9297 " c();\n" 9298 " }\n" 9299 " while (false);\n" 9300 "}\n", 9301 GNUBraceStyle); 9302 9303 verifyFormat("void f(int a)\n" 9304 "{\n" 9305 " switch (a)\n" 9306 " {\n" 9307 " case 0:\n" 9308 " break;\n" 9309 " case 1:\n" 9310 " {\n" 9311 " break;\n" 9312 " }\n" 9313 " case 2:\n" 9314 " {\n" 9315 " }\n" 9316 " break;\n" 9317 " default:\n" 9318 " break;\n" 9319 " }\n" 9320 "}\n", 9321 GNUBraceStyle); 9322 9323 verifyFormat("enum X\n" 9324 "{\n" 9325 " Y = 0,\n" 9326 "}\n", 9327 GNUBraceStyle); 9328 9329 verifyFormat("@interface BSApplicationController ()\n" 9330 "{\n" 9331 "@private\n" 9332 " id _extraIvar;\n" 9333 "}\n" 9334 "@end\n", 9335 GNUBraceStyle); 9336 9337 verifyFormat("#ifdef _DEBUG\n" 9338 "int foo(int i = 0)\n" 9339 "#else\n" 9340 "int foo(int i = 5)\n" 9341 "#endif\n" 9342 "{\n" 9343 " return i;\n" 9344 "}", 9345 GNUBraceStyle); 9346 9347 verifyFormat("void foo() {}\n" 9348 "void bar()\n" 9349 "#ifdef _DEBUG\n" 9350 "{\n" 9351 " foo();\n" 9352 "}\n" 9353 "#else\n" 9354 "{\n" 9355 "}\n" 9356 "#endif", 9357 GNUBraceStyle); 9358 9359 verifyFormat("void foobar() { int i = 5; }\n" 9360 "#ifdef _DEBUG\n" 9361 "void bar() {}\n" 9362 "#else\n" 9363 "void bar() { foobar(); }\n" 9364 "#endif", 9365 GNUBraceStyle); 9366 } 9367 9368 TEST_F(FormatTest, WebKitBraceBreaking) { 9369 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9370 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9371 verifyFormat("namespace a {\n" 9372 "class A {\n" 9373 " void f()\n" 9374 " {\n" 9375 " if (true) {\n" 9376 " a();\n" 9377 " b();\n" 9378 " }\n" 9379 " }\n" 9380 " void g() { return; }\n" 9381 "};\n" 9382 "enum E {\n" 9383 " A,\n" 9384 " // foo\n" 9385 " B,\n" 9386 " C\n" 9387 "};\n" 9388 "struct B {\n" 9389 " int x;\n" 9390 "};\n" 9391 "}\n", 9392 WebKitBraceStyle); 9393 verifyFormat("struct S {\n" 9394 " int Type;\n" 9395 " union {\n" 9396 " int x;\n" 9397 " double y;\n" 9398 " } Value;\n" 9399 " class C {\n" 9400 " MyFavoriteType Value;\n" 9401 " } Class;\n" 9402 "};\n", 9403 WebKitBraceStyle); 9404 } 9405 9406 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9407 verifyFormat("void f() {\n" 9408 " try {\n" 9409 " } catch (const Exception &e) {\n" 9410 " }\n" 9411 "}\n", 9412 getLLVMStyle()); 9413 } 9414 9415 TEST_F(FormatTest, UnderstandsPragmas) { 9416 verifyFormat("#pragma omp reduction(| : var)"); 9417 verifyFormat("#pragma omp reduction(+ : var)"); 9418 9419 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9420 "(including parentheses).", 9421 format("#pragma mark Any non-hyphenated or hyphenated string " 9422 "(including parentheses).")); 9423 } 9424 9425 TEST_F(FormatTest, UnderstandPragmaOption) { 9426 verifyFormat("#pragma option -C -A"); 9427 9428 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9429 } 9430 9431 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9432 for (size_t i = 1; i < Styles.size(); ++i) \ 9433 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9434 << " differs from Style #0" 9435 9436 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9437 SmallVector<FormatStyle, 3> Styles; 9438 Styles.resize(3); 9439 9440 Styles[0] = getLLVMStyle(); 9441 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9442 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9443 EXPECT_ALL_STYLES_EQUAL(Styles); 9444 9445 Styles[0] = getGoogleStyle(); 9446 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9447 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9448 EXPECT_ALL_STYLES_EQUAL(Styles); 9449 9450 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9451 EXPECT_TRUE( 9452 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9453 EXPECT_TRUE( 9454 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9455 EXPECT_ALL_STYLES_EQUAL(Styles); 9456 9457 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9458 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9459 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9460 EXPECT_ALL_STYLES_EQUAL(Styles); 9461 9462 Styles[0] = getMozillaStyle(); 9463 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9464 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9465 EXPECT_ALL_STYLES_EQUAL(Styles); 9466 9467 Styles[0] = getWebKitStyle(); 9468 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9469 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9470 EXPECT_ALL_STYLES_EQUAL(Styles); 9471 9472 Styles[0] = getGNUStyle(); 9473 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9474 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9475 EXPECT_ALL_STYLES_EQUAL(Styles); 9476 9477 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9478 } 9479 9480 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9481 SmallVector<FormatStyle, 8> Styles; 9482 Styles.resize(2); 9483 9484 Styles[0] = getGoogleStyle(); 9485 Styles[1] = getLLVMStyle(); 9486 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9487 EXPECT_ALL_STYLES_EQUAL(Styles); 9488 9489 Styles.resize(5); 9490 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9491 Styles[1] = getLLVMStyle(); 9492 Styles[1].Language = FormatStyle::LK_JavaScript; 9493 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9494 9495 Styles[2] = getLLVMStyle(); 9496 Styles[2].Language = FormatStyle::LK_JavaScript; 9497 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9498 "BasedOnStyle: Google", 9499 &Styles[2]) 9500 .value()); 9501 9502 Styles[3] = getLLVMStyle(); 9503 Styles[3].Language = FormatStyle::LK_JavaScript; 9504 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9505 "Language: JavaScript", 9506 &Styles[3]) 9507 .value()); 9508 9509 Styles[4] = getLLVMStyle(); 9510 Styles[4].Language = FormatStyle::LK_JavaScript; 9511 EXPECT_EQ(0, parseConfiguration("---\n" 9512 "BasedOnStyle: LLVM\n" 9513 "IndentWidth: 123\n" 9514 "---\n" 9515 "BasedOnStyle: Google\n" 9516 "Language: JavaScript", 9517 &Styles[4]) 9518 .value()); 9519 EXPECT_ALL_STYLES_EQUAL(Styles); 9520 } 9521 9522 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9523 Style.FIELD = false; \ 9524 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9525 EXPECT_TRUE(Style.FIELD); \ 9526 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9527 EXPECT_FALSE(Style.FIELD); 9528 9529 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9530 9531 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9532 Style.STRUCT.FIELD = false; \ 9533 EXPECT_EQ(0, \ 9534 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9535 .value()); \ 9536 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9537 EXPECT_EQ(0, \ 9538 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9539 .value()); \ 9540 EXPECT_FALSE(Style.STRUCT.FIELD); 9541 9542 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9543 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9544 9545 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9546 EXPECT_NE(VALUE, Style.FIELD); \ 9547 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9548 EXPECT_EQ(VALUE, Style.FIELD) 9549 9550 TEST_F(FormatTest, ParsesConfigurationBools) { 9551 FormatStyle Style = {}; 9552 Style.Language = FormatStyle::LK_Cpp; 9553 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9554 CHECK_PARSE_BOOL(AlignOperands); 9555 CHECK_PARSE_BOOL(AlignTrailingComments); 9556 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9557 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9558 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9559 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9560 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9561 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9562 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9563 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9564 CHECK_PARSE_BOOL(BinPackArguments); 9565 CHECK_PARSE_BOOL(BinPackParameters); 9566 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9567 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9568 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9569 CHECK_PARSE_BOOL(DerivePointerAlignment); 9570 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9571 CHECK_PARSE_BOOL(DisableFormat); 9572 CHECK_PARSE_BOOL(IndentCaseLabels); 9573 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9574 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9575 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9576 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9577 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9578 CHECK_PARSE_BOOL(SortIncludes); 9579 CHECK_PARSE_BOOL(SpacesInParentheses); 9580 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9581 CHECK_PARSE_BOOL(SpacesInAngles); 9582 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9583 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9584 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9585 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9586 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9587 9588 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9589 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9590 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9591 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9592 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9593 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9594 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9595 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9596 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9597 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9598 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9599 } 9600 9601 #undef CHECK_PARSE_BOOL 9602 9603 TEST_F(FormatTest, ParsesConfiguration) { 9604 FormatStyle Style = {}; 9605 Style.Language = FormatStyle::LK_Cpp; 9606 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9607 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9608 ConstructorInitializerIndentWidth, 1234u); 9609 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9610 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9611 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9612 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9613 PenaltyBreakBeforeFirstCallParameter, 1234u); 9614 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9615 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9616 PenaltyReturnTypeOnItsOwnLine, 1234u); 9617 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9618 SpacesBeforeTrailingComments, 1234u); 9619 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9620 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9621 9622 Style.PointerAlignment = FormatStyle::PAS_Middle; 9623 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9624 FormatStyle::PAS_Left); 9625 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9626 FormatStyle::PAS_Right); 9627 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9628 FormatStyle::PAS_Middle); 9629 // For backward compatibility: 9630 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9631 FormatStyle::PAS_Left); 9632 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9633 FormatStyle::PAS_Right); 9634 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9635 FormatStyle::PAS_Middle); 9636 9637 Style.Standard = FormatStyle::LS_Auto; 9638 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9639 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9640 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9641 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9642 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9643 9644 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9645 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9646 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9647 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9648 FormatStyle::BOS_None); 9649 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9650 FormatStyle::BOS_All); 9651 // For backward compatibility: 9652 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9653 FormatStyle::BOS_None); 9654 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9655 FormatStyle::BOS_All); 9656 9657 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9658 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9659 FormatStyle::BAS_Align); 9660 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9661 FormatStyle::BAS_DontAlign); 9662 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9663 FormatStyle::BAS_AlwaysBreak); 9664 // For backward compatibility: 9665 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9666 FormatStyle::BAS_DontAlign); 9667 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9668 FormatStyle::BAS_Align); 9669 9670 Style.UseTab = FormatStyle::UT_ForIndentation; 9671 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9672 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9673 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9674 // For backward compatibility: 9675 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9676 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9677 9678 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9679 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9680 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9681 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9682 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9683 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9684 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9685 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9686 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9687 // For backward compatibility: 9688 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9689 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9690 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9691 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9692 9693 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9694 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9695 FormatStyle::SBPO_Never); 9696 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9697 FormatStyle::SBPO_Always); 9698 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9699 FormatStyle::SBPO_ControlStatements); 9700 // For backward compatibility: 9701 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9702 FormatStyle::SBPO_Never); 9703 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9704 FormatStyle::SBPO_ControlStatements); 9705 9706 Style.ColumnLimit = 123; 9707 FormatStyle BaseStyle = getLLVMStyle(); 9708 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9709 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9710 9711 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9712 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9713 FormatStyle::BS_Attach); 9714 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9715 FormatStyle::BS_Linux); 9716 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9717 FormatStyle::BS_Mozilla); 9718 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9719 FormatStyle::BS_Stroustrup); 9720 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9721 FormatStyle::BS_Allman); 9722 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9723 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9724 FormatStyle::BS_WebKit); 9725 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9726 FormatStyle::BS_Custom); 9727 9728 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9729 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9730 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9731 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9732 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9733 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9734 AlwaysBreakAfterDefinitionReturnType, 9735 FormatStyle::DRTBS_TopLevel); 9736 9737 Style.NamespaceIndentation = FormatStyle::NI_All; 9738 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9739 FormatStyle::NI_None); 9740 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9741 FormatStyle::NI_Inner); 9742 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9743 FormatStyle::NI_All); 9744 9745 // FIXME: This is required because parsing a configuration simply overwrites 9746 // the first N elements of the list instead of resetting it. 9747 Style.ForEachMacros.clear(); 9748 std::vector<std::string> BoostForeach; 9749 BoostForeach.push_back("BOOST_FOREACH"); 9750 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9751 std::vector<std::string> BoostAndQForeach; 9752 BoostAndQForeach.push_back("BOOST_FOREACH"); 9753 BoostAndQForeach.push_back("Q_FOREACH"); 9754 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9755 BoostAndQForeach); 9756 9757 Style.IncludeCategories.clear(); 9758 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9759 {".*", 1}}; 9760 CHECK_PARSE("IncludeCategories:\n" 9761 " - Regex: abc/.*\n" 9762 " Priority: 2\n" 9763 " - Regex: .*\n" 9764 " Priority: 1", 9765 IncludeCategories, ExpectedCategories); 9766 } 9767 9768 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9769 FormatStyle Style = {}; 9770 Style.Language = FormatStyle::LK_Cpp; 9771 CHECK_PARSE("Language: Cpp\n" 9772 "IndentWidth: 12", 9773 IndentWidth, 12u); 9774 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9775 "IndentWidth: 34", 9776 &Style), 9777 ParseError::Unsuitable); 9778 EXPECT_EQ(12u, Style.IndentWidth); 9779 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9780 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9781 9782 Style.Language = FormatStyle::LK_JavaScript; 9783 CHECK_PARSE("Language: JavaScript\n" 9784 "IndentWidth: 12", 9785 IndentWidth, 12u); 9786 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9787 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9788 "IndentWidth: 34", 9789 &Style), 9790 ParseError::Unsuitable); 9791 EXPECT_EQ(23u, Style.IndentWidth); 9792 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9793 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9794 9795 CHECK_PARSE("BasedOnStyle: LLVM\n" 9796 "IndentWidth: 67", 9797 IndentWidth, 67u); 9798 9799 CHECK_PARSE("---\n" 9800 "Language: JavaScript\n" 9801 "IndentWidth: 12\n" 9802 "---\n" 9803 "Language: Cpp\n" 9804 "IndentWidth: 34\n" 9805 "...\n", 9806 IndentWidth, 12u); 9807 9808 Style.Language = FormatStyle::LK_Cpp; 9809 CHECK_PARSE("---\n" 9810 "Language: JavaScript\n" 9811 "IndentWidth: 12\n" 9812 "---\n" 9813 "Language: Cpp\n" 9814 "IndentWidth: 34\n" 9815 "...\n", 9816 IndentWidth, 34u); 9817 CHECK_PARSE("---\n" 9818 "IndentWidth: 78\n" 9819 "---\n" 9820 "Language: JavaScript\n" 9821 "IndentWidth: 56\n" 9822 "...\n", 9823 IndentWidth, 78u); 9824 9825 Style.ColumnLimit = 123; 9826 Style.IndentWidth = 234; 9827 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9828 Style.TabWidth = 345; 9829 EXPECT_FALSE(parseConfiguration("---\n" 9830 "IndentWidth: 456\n" 9831 "BreakBeforeBraces: Allman\n" 9832 "---\n" 9833 "Language: JavaScript\n" 9834 "IndentWidth: 111\n" 9835 "TabWidth: 111\n" 9836 "---\n" 9837 "Language: Cpp\n" 9838 "BreakBeforeBraces: Stroustrup\n" 9839 "TabWidth: 789\n" 9840 "...\n", 9841 &Style)); 9842 EXPECT_EQ(123u, Style.ColumnLimit); 9843 EXPECT_EQ(456u, Style.IndentWidth); 9844 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9845 EXPECT_EQ(789u, Style.TabWidth); 9846 9847 EXPECT_EQ(parseConfiguration("---\n" 9848 "Language: JavaScript\n" 9849 "IndentWidth: 56\n" 9850 "---\n" 9851 "IndentWidth: 78\n" 9852 "...\n", 9853 &Style), 9854 ParseError::Error); 9855 EXPECT_EQ(parseConfiguration("---\n" 9856 "Language: JavaScript\n" 9857 "IndentWidth: 56\n" 9858 "---\n" 9859 "Language: JavaScript\n" 9860 "IndentWidth: 78\n" 9861 "...\n", 9862 &Style), 9863 ParseError::Error); 9864 9865 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9866 } 9867 9868 #undef CHECK_PARSE 9869 9870 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9871 FormatStyle Style = {}; 9872 Style.Language = FormatStyle::LK_JavaScript; 9873 Style.BreakBeforeTernaryOperators = true; 9874 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9875 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9876 9877 Style.BreakBeforeTernaryOperators = true; 9878 EXPECT_EQ(0, parseConfiguration("---\n" 9879 "BasedOnStyle: Google\n" 9880 "---\n" 9881 "Language: JavaScript\n" 9882 "IndentWidth: 76\n" 9883 "...\n", 9884 &Style) 9885 .value()); 9886 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9887 EXPECT_EQ(76u, Style.IndentWidth); 9888 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9889 } 9890 9891 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9892 FormatStyle Style = getLLVMStyle(); 9893 std::string YAML = configurationAsText(Style); 9894 FormatStyle ParsedStyle = {}; 9895 ParsedStyle.Language = FormatStyle::LK_Cpp; 9896 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9897 EXPECT_EQ(Style, ParsedStyle); 9898 } 9899 9900 TEST_F(FormatTest, WorksFor8bitEncodings) { 9901 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9902 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9903 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9904 "\"\xef\xee\xf0\xf3...\"", 9905 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9906 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9907 "\xef\xee\xf0\xf3...\"", 9908 getLLVMStyleWithColumns(12))); 9909 } 9910 9911 TEST_F(FormatTest, HandlesUTF8BOM) { 9912 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9913 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9914 format("\xef\xbb\xbf#include <iostream>")); 9915 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9916 format("\xef\xbb\xbf\n#include <iostream>")); 9917 } 9918 9919 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9920 #if !defined(_MSC_VER) 9921 9922 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9923 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9924 getLLVMStyleWithColumns(35)); 9925 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9926 getLLVMStyleWithColumns(31)); 9927 verifyFormat("// Однажды в студёную зимнюю пору...", 9928 getLLVMStyleWithColumns(36)); 9929 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9930 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9931 getLLVMStyleWithColumns(39)); 9932 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9933 getLLVMStyleWithColumns(35)); 9934 } 9935 9936 TEST_F(FormatTest, SplitsUTF8Strings) { 9937 // Non-printable characters' width is currently considered to be the length in 9938 // bytes in UTF8. The characters can be displayed in very different manner 9939 // (zero-width, single width with a substitution glyph, expanded to their code 9940 // (e.g. "<8d>"), so there's no single correct way to handle them. 9941 EXPECT_EQ("\"aaaaÄ\"\n" 9942 "\"\xc2\x8d\";", 9943 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9944 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9945 "\"\xc2\x8d\";", 9946 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9947 EXPECT_EQ("\"Однажды, в \"\n" 9948 "\"студёную \"\n" 9949 "\"зимнюю \"\n" 9950 "\"пору,\"", 9951 format("\"Однажды, в студёную зимнюю пору,\"", 9952 getLLVMStyleWithColumns(13))); 9953 EXPECT_EQ( 9954 "\"一 二 三 \"\n" 9955 "\"四 五六 \"\n" 9956 "\"七 八 九 \"\n" 9957 "\"十\"", 9958 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9959 EXPECT_EQ("\"一\t二 \"\n" 9960 "\"\t三 \"\n" 9961 "\"四 五\t六 \"\n" 9962 "\"\t七 \"\n" 9963 "\"八九十\tqq\"", 9964 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9965 getLLVMStyleWithColumns(11))); 9966 9967 // UTF8 character in an escape sequence. 9968 EXPECT_EQ("\"aaaaaa\"\n" 9969 "\"\\\xC2\x8D\"", 9970 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 9971 } 9972 9973 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9974 EXPECT_EQ("const char *sssss =\n" 9975 " \"一二三四五六七八\\\n" 9976 " 九 十\";", 9977 format("const char *sssss = \"一二三四五六七八\\\n" 9978 " 九 十\";", 9979 getLLVMStyleWithColumns(30))); 9980 } 9981 9982 TEST_F(FormatTest, SplitsUTF8LineComments) { 9983 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9984 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9985 EXPECT_EQ("// Я из лесу\n" 9986 "// вышел; был\n" 9987 "// сильный\n" 9988 "// мороз.", 9989 format("// Я из лесу вышел; был сильный мороз.", 9990 getLLVMStyleWithColumns(13))); 9991 EXPECT_EQ("// 一二三\n" 9992 "// 四五六七\n" 9993 "// 八 九\n" 9994 "// 十", 9995 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9996 } 9997 9998 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9999 EXPECT_EQ("/* Гляжу,\n" 10000 " * поднимается\n" 10001 " * медленно в\n" 10002 " * гору\n" 10003 " * Лошадка,\n" 10004 " * везущая\n" 10005 " * хворосту\n" 10006 " * воз. */", 10007 format("/* Гляжу, поднимается медленно в гору\n" 10008 " * Лошадка, везущая хворосту воз. */", 10009 getLLVMStyleWithColumns(13))); 10010 EXPECT_EQ( 10011 "/* 一二三\n" 10012 " * 四五六七\n" 10013 " * 八 九\n" 10014 " * 十 */", 10015 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10016 EXPECT_EQ("/* \n" 10017 " * \n" 10018 " * - */", 10019 format("/* - */", getLLVMStyleWithColumns(12))); 10020 } 10021 10022 #endif // _MSC_VER 10023 10024 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10025 FormatStyle Style = getLLVMStyle(); 10026 10027 Style.ConstructorInitializerIndentWidth = 4; 10028 verifyFormat( 10029 "SomeClass::Constructor()\n" 10030 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10031 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10032 Style); 10033 10034 Style.ConstructorInitializerIndentWidth = 2; 10035 verifyFormat( 10036 "SomeClass::Constructor()\n" 10037 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10038 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10039 Style); 10040 10041 Style.ConstructorInitializerIndentWidth = 0; 10042 verifyFormat( 10043 "SomeClass::Constructor()\n" 10044 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10045 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10046 Style); 10047 } 10048 10049 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10050 FormatStyle Style = getLLVMStyle(); 10051 Style.BreakConstructorInitializersBeforeComma = true; 10052 Style.ConstructorInitializerIndentWidth = 4; 10053 verifyFormat("SomeClass::Constructor()\n" 10054 " : a(a)\n" 10055 " , b(b)\n" 10056 " , c(c) {}", 10057 Style); 10058 verifyFormat("SomeClass::Constructor()\n" 10059 " : a(a) {}", 10060 Style); 10061 10062 Style.ColumnLimit = 0; 10063 verifyFormat("SomeClass::Constructor()\n" 10064 " : a(a) {}", 10065 Style); 10066 verifyFormat("SomeClass::Constructor()\n" 10067 " : a(a)\n" 10068 " , b(b)\n" 10069 " , c(c) {}", 10070 Style); 10071 verifyFormat("SomeClass::Constructor()\n" 10072 " : a(a) {\n" 10073 " foo();\n" 10074 " bar();\n" 10075 "}", 10076 Style); 10077 10078 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10079 verifyFormat("SomeClass::Constructor()\n" 10080 " : a(a)\n" 10081 " , b(b)\n" 10082 " , c(c) {\n}", 10083 Style); 10084 verifyFormat("SomeClass::Constructor()\n" 10085 " : a(a) {\n}", 10086 Style); 10087 10088 Style.ColumnLimit = 80; 10089 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10090 Style.ConstructorInitializerIndentWidth = 2; 10091 verifyFormat("SomeClass::Constructor()\n" 10092 " : a(a)\n" 10093 " , b(b)\n" 10094 " , c(c) {}", 10095 Style); 10096 10097 Style.ConstructorInitializerIndentWidth = 0; 10098 verifyFormat("SomeClass::Constructor()\n" 10099 ": a(a)\n" 10100 ", b(b)\n" 10101 ", c(c) {}", 10102 Style); 10103 10104 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10105 Style.ConstructorInitializerIndentWidth = 4; 10106 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10107 verifyFormat( 10108 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10109 Style); 10110 verifyFormat( 10111 "SomeClass::Constructor()\n" 10112 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10113 Style); 10114 Style.ConstructorInitializerIndentWidth = 4; 10115 Style.ColumnLimit = 60; 10116 verifyFormat("SomeClass::Constructor()\n" 10117 " : aaaaaaaa(aaaaaaaa)\n" 10118 " , aaaaaaaa(aaaaaaaa)\n" 10119 " , aaaaaaaa(aaaaaaaa) {}", 10120 Style); 10121 } 10122 10123 TEST_F(FormatTest, Destructors) { 10124 verifyFormat("void F(int &i) { i.~int(); }"); 10125 verifyFormat("void F(int &i) { i->~int(); }"); 10126 } 10127 10128 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10129 FormatStyle Style = getWebKitStyle(); 10130 10131 // Don't indent in outer namespaces. 10132 verifyFormat("namespace outer {\n" 10133 "int i;\n" 10134 "namespace inner {\n" 10135 " int i;\n" 10136 "} // namespace inner\n" 10137 "} // namespace outer\n" 10138 "namespace other_outer {\n" 10139 "int i;\n" 10140 "}", 10141 Style); 10142 10143 // Don't indent case labels. 10144 verifyFormat("switch (variable) {\n" 10145 "case 1:\n" 10146 "case 2:\n" 10147 " doSomething();\n" 10148 " break;\n" 10149 "default:\n" 10150 " ++variable;\n" 10151 "}", 10152 Style); 10153 10154 // Wrap before binary operators. 10155 EXPECT_EQ("void f()\n" 10156 "{\n" 10157 " if (aaaaaaaaaaaaaaaa\n" 10158 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10159 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10160 " return;\n" 10161 "}", 10162 format("void f() {\n" 10163 "if (aaaaaaaaaaaaaaaa\n" 10164 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10165 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10166 "return;\n" 10167 "}", 10168 Style)); 10169 10170 // Allow functions on a single line. 10171 verifyFormat("void f() { return; }", Style); 10172 10173 // Constructor initializers are formatted one per line with the "," on the 10174 // new line. 10175 verifyFormat("Constructor()\n" 10176 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10177 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10178 " aaaaaaaaaaaaaa)\n" 10179 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10180 "{\n" 10181 "}", 10182 Style); 10183 verifyFormat("SomeClass::Constructor()\n" 10184 " : a(a)\n" 10185 "{\n" 10186 "}", 10187 Style); 10188 EXPECT_EQ("SomeClass::Constructor()\n" 10189 " : a(a)\n" 10190 "{\n" 10191 "}", 10192 format("SomeClass::Constructor():a(a){}", Style)); 10193 verifyFormat("SomeClass::Constructor()\n" 10194 " : a(a)\n" 10195 " , b(b)\n" 10196 " , c(c)\n" 10197 "{\n" 10198 "}", 10199 Style); 10200 verifyFormat("SomeClass::Constructor()\n" 10201 " : a(a)\n" 10202 "{\n" 10203 " foo();\n" 10204 " bar();\n" 10205 "}", 10206 Style); 10207 10208 // Access specifiers should be aligned left. 10209 verifyFormat("class C {\n" 10210 "public:\n" 10211 " int i;\n" 10212 "};", 10213 Style); 10214 10215 // Do not align comments. 10216 verifyFormat("int a; // Do not\n" 10217 "double b; // align comments.", 10218 Style); 10219 10220 // Do not align operands. 10221 EXPECT_EQ("ASSERT(aaaa\n" 10222 " || bbbb);", 10223 format("ASSERT ( aaaa\n||bbbb);", Style)); 10224 10225 // Accept input's line breaks. 10226 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10227 " || bbbbbbbbbbbbbbb) {\n" 10228 " i++;\n" 10229 "}", 10230 format("if (aaaaaaaaaaaaaaa\n" 10231 "|| bbbbbbbbbbbbbbb) { i++; }", 10232 Style)); 10233 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10234 " i++;\n" 10235 "}", 10236 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10237 10238 // Don't automatically break all macro definitions (llvm.org/PR17842). 10239 verifyFormat("#define aNumber 10", Style); 10240 // However, generally keep the line breaks that the user authored. 10241 EXPECT_EQ("#define aNumber \\\n" 10242 " 10", 10243 format("#define aNumber \\\n" 10244 " 10", 10245 Style)); 10246 10247 // Keep empty and one-element array literals on a single line. 10248 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10249 " copyItems:YES];", 10250 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10251 "copyItems:YES];", 10252 Style)); 10253 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10254 " copyItems:YES];", 10255 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10256 " copyItems:YES];", 10257 Style)); 10258 // FIXME: This does not seem right, there should be more indentation before 10259 // the array literal's entries. Nested blocks have the same problem. 10260 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10261 " @\"a\",\n" 10262 " @\"a\"\n" 10263 "]\n" 10264 " copyItems:YES];", 10265 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10266 " @\"a\",\n" 10267 " @\"a\"\n" 10268 " ]\n" 10269 " copyItems:YES];", 10270 Style)); 10271 EXPECT_EQ( 10272 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10273 " copyItems:YES];", 10274 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10275 " copyItems:YES];", 10276 Style)); 10277 10278 verifyFormat("[self.a b:c c:d];", Style); 10279 EXPECT_EQ("[self.a b:c\n" 10280 " c:d];", 10281 format("[self.a b:c\n" 10282 "c:d];", 10283 Style)); 10284 } 10285 10286 TEST_F(FormatTest, FormatsLambdas) { 10287 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10288 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10289 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10290 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10291 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10292 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10293 verifyFormat("void f() {\n" 10294 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10295 "}\n"); 10296 verifyFormat("void f() {\n" 10297 " other(x.begin(), //\n" 10298 " x.end(), //\n" 10299 " [&](int, int) { return 1; });\n" 10300 "}\n"); 10301 verifyFormat("SomeFunction([]() { // A cool function...\n" 10302 " return 43;\n" 10303 "});"); 10304 EXPECT_EQ("SomeFunction([]() {\n" 10305 "#define A a\n" 10306 " return 43;\n" 10307 "});", 10308 format("SomeFunction([](){\n" 10309 "#define A a\n" 10310 "return 43;\n" 10311 "});")); 10312 verifyFormat("void f() {\n" 10313 " SomeFunction([](decltype(x), A *a) {});\n" 10314 "}"); 10315 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10316 " [](const aaaaaaaaaa &a) { return a; });"); 10317 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10318 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10319 "});"); 10320 verifyFormat("Constructor()\n" 10321 " : Field([] { // comment\n" 10322 " int i;\n" 10323 " }) {}"); 10324 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10325 " return some_parameter.size();\n" 10326 "};"); 10327 verifyFormat("int i = aaaaaa ? 1 //\n" 10328 " : [] {\n" 10329 " return 2; //\n" 10330 " }();"); 10331 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10332 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10333 " return x == 2; // force break\n" 10334 " });"); 10335 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n" 10336 " int iiiiiiiiiiii) {\n" 10337 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n" 10338 "});", 10339 getLLVMStyleWithColumns(60)); 10340 verifyFormat("SomeFunction({[&] {\n" 10341 " // comment\n" 10342 " },\n" 10343 " [&] {\n" 10344 " // comment\n" 10345 " }});"); 10346 verifyFormat("SomeFunction({[&] {\n" 10347 " // comment\n" 10348 "}});"); 10349 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10350 " [&]() { return true; },\n" 10351 " aaaaa aaaaaaaaa);"); 10352 10353 // Lambdas with return types. 10354 verifyFormat("int c = []() -> int { return 2; }();\n"); 10355 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10356 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10357 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10358 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10359 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10360 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10361 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10362 verifyFormat("[a, a]() -> a<1> {};"); 10363 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10364 " int j) -> int {\n" 10365 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10366 "};"); 10367 verifyFormat( 10368 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10369 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10370 " return aaaaaaaaaaaaaaaaa;\n" 10371 " });", 10372 getLLVMStyleWithColumns(70)); 10373 10374 // Multiple lambdas in the same parentheses change indentation rules. 10375 verifyFormat("SomeFunction(\n" 10376 " []() {\n" 10377 " int i = 42;\n" 10378 " return i;\n" 10379 " },\n" 10380 " []() {\n" 10381 " int j = 43;\n" 10382 " return j;\n" 10383 " });"); 10384 10385 // More complex introducers. 10386 verifyFormat("return [i, args...] {};"); 10387 10388 // Not lambdas. 10389 verifyFormat("constexpr char hello[]{\"hello\"};"); 10390 verifyFormat("double &operator[](int i) { return 0; }\n" 10391 "int i;"); 10392 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10393 verifyFormat("int i = a[a][a]->f();"); 10394 verifyFormat("int i = (*b)[a]->f();"); 10395 10396 // Other corner cases. 10397 verifyFormat("void f() {\n" 10398 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10399 " );\n" 10400 "}"); 10401 10402 // Lambdas created through weird macros. 10403 verifyFormat("void f() {\n" 10404 " MACRO((const AA &a) { return 1; });\n" 10405 "}"); 10406 10407 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10408 " doo_dah();\n" 10409 " doo_dah();\n" 10410 " })) {\n" 10411 "}"); 10412 verifyFormat("auto lambda = []() {\n" 10413 " int a = 2\n" 10414 "#if A\n" 10415 " + 2\n" 10416 "#endif\n" 10417 " ;\n" 10418 "};"); 10419 } 10420 10421 TEST_F(FormatTest, FormatsBlocks) { 10422 FormatStyle ShortBlocks = getLLVMStyle(); 10423 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10424 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10425 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10426 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10427 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10428 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10429 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10430 10431 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10432 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10433 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10434 10435 verifyFormat("[operation setCompletionBlock:^{\n" 10436 " [self onOperationDone];\n" 10437 "}];"); 10438 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10439 " [self onOperationDone];\n" 10440 "}]};"); 10441 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10442 " f();\n" 10443 "}];"); 10444 verifyFormat("int a = [operation block:^int(int *i) {\n" 10445 " return 1;\n" 10446 "}];"); 10447 verifyFormat("[myObject doSomethingWith:arg1\n" 10448 " aaa:^int(int *a) {\n" 10449 " return 1;\n" 10450 " }\n" 10451 " bbb:f(a * bbbbbbbb)];"); 10452 10453 verifyFormat("[operation setCompletionBlock:^{\n" 10454 " [self.delegate newDataAvailable];\n" 10455 "}];", 10456 getLLVMStyleWithColumns(60)); 10457 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10458 " NSString *path = [self sessionFilePath];\n" 10459 " if (path) {\n" 10460 " // ...\n" 10461 " }\n" 10462 "});"); 10463 verifyFormat("[[SessionService sharedService]\n" 10464 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10465 " if (window) {\n" 10466 " [self windowDidLoad:window];\n" 10467 " } else {\n" 10468 " [self errorLoadingWindow];\n" 10469 " }\n" 10470 " }];"); 10471 verifyFormat("void (^largeBlock)(void) = ^{\n" 10472 " // ...\n" 10473 "};\n", 10474 getLLVMStyleWithColumns(40)); 10475 verifyFormat("[[SessionService sharedService]\n" 10476 " loadWindowWithCompletionBlock: //\n" 10477 " ^(SessionWindow *window) {\n" 10478 " if (window) {\n" 10479 " [self windowDidLoad:window];\n" 10480 " } else {\n" 10481 " [self errorLoadingWindow];\n" 10482 " }\n" 10483 " }];", 10484 getLLVMStyleWithColumns(60)); 10485 verifyFormat("[myObject doSomethingWith:arg1\n" 10486 " firstBlock:^(Foo *a) {\n" 10487 " // ...\n" 10488 " int i;\n" 10489 " }\n" 10490 " secondBlock:^(Bar *b) {\n" 10491 " // ...\n" 10492 " int i;\n" 10493 " }\n" 10494 " thirdBlock:^Foo(Bar *b) {\n" 10495 " // ...\n" 10496 " int i;\n" 10497 " }];"); 10498 verifyFormat("[myObject doSomethingWith:arg1\n" 10499 " firstBlock:-1\n" 10500 " secondBlock:^(Bar *b) {\n" 10501 " // ...\n" 10502 " int i;\n" 10503 " }];"); 10504 10505 verifyFormat("f(^{\n" 10506 " @autoreleasepool {\n" 10507 " if (a) {\n" 10508 " g();\n" 10509 " }\n" 10510 " }\n" 10511 "});"); 10512 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10513 10514 FormatStyle FourIndent = getLLVMStyle(); 10515 FourIndent.ObjCBlockIndentWidth = 4; 10516 verifyFormat("[operation setCompletionBlock:^{\n" 10517 " [self onOperationDone];\n" 10518 "}];", 10519 FourIndent); 10520 } 10521 10522 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10523 FormatStyle ZeroColumn = getLLVMStyle(); 10524 ZeroColumn.ColumnLimit = 0; 10525 10526 verifyFormat("[[SessionService sharedService] " 10527 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10528 " if (window) {\n" 10529 " [self windowDidLoad:window];\n" 10530 " } else {\n" 10531 " [self errorLoadingWindow];\n" 10532 " }\n" 10533 "}];", 10534 ZeroColumn); 10535 EXPECT_EQ("[[SessionService sharedService]\n" 10536 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10537 " if (window) {\n" 10538 " [self windowDidLoad:window];\n" 10539 " } else {\n" 10540 " [self errorLoadingWindow];\n" 10541 " }\n" 10542 " }];", 10543 format("[[SessionService sharedService]\n" 10544 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10545 " if (window) {\n" 10546 " [self windowDidLoad:window];\n" 10547 " } else {\n" 10548 " [self errorLoadingWindow];\n" 10549 " }\n" 10550 "}];", 10551 ZeroColumn)); 10552 verifyFormat("[myObject doSomethingWith:arg1\n" 10553 " firstBlock:^(Foo *a) {\n" 10554 " // ...\n" 10555 " int i;\n" 10556 " }\n" 10557 " secondBlock:^(Bar *b) {\n" 10558 " // ...\n" 10559 " int i;\n" 10560 " }\n" 10561 " thirdBlock:^Foo(Bar *b) {\n" 10562 " // ...\n" 10563 " int i;\n" 10564 " }];", 10565 ZeroColumn); 10566 verifyFormat("f(^{\n" 10567 " @autoreleasepool {\n" 10568 " if (a) {\n" 10569 " g();\n" 10570 " }\n" 10571 " }\n" 10572 "});", 10573 ZeroColumn); 10574 verifyFormat("void (^largeBlock)(void) = ^{\n" 10575 " // ...\n" 10576 "};", 10577 ZeroColumn); 10578 10579 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10580 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10581 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10582 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10583 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10584 " int i;\n" 10585 "};", 10586 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10587 } 10588 10589 TEST_F(FormatTest, SupportsCRLF) { 10590 EXPECT_EQ("int a;\r\n" 10591 "int b;\r\n" 10592 "int c;\r\n", 10593 format("int a;\r\n" 10594 " int b;\r\n" 10595 " int c;\r\n", 10596 getLLVMStyle())); 10597 EXPECT_EQ("int a;\r\n" 10598 "int b;\r\n" 10599 "int c;\r\n", 10600 format("int a;\r\n" 10601 " int b;\n" 10602 " int c;\r\n", 10603 getLLVMStyle())); 10604 EXPECT_EQ("int a;\n" 10605 "int b;\n" 10606 "int c;\n", 10607 format("int a;\r\n" 10608 " int b;\n" 10609 " int c;\n", 10610 getLLVMStyle())); 10611 EXPECT_EQ("\"aaaaaaa \"\r\n" 10612 "\"bbbbbbb\";\r\n", 10613 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10614 EXPECT_EQ("#define A \\\r\n" 10615 " b; \\\r\n" 10616 " c; \\\r\n" 10617 " d;\r\n", 10618 format("#define A \\\r\n" 10619 " b; \\\r\n" 10620 " c; d; \r\n", 10621 getGoogleStyle())); 10622 10623 EXPECT_EQ("/*\r\n" 10624 "multi line block comments\r\n" 10625 "should not introduce\r\n" 10626 "an extra carriage return\r\n" 10627 "*/\r\n", 10628 format("/*\r\n" 10629 "multi line block comments\r\n" 10630 "should not introduce\r\n" 10631 "an extra carriage return\r\n" 10632 "*/\r\n")); 10633 } 10634 10635 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10636 verifyFormat("MY_CLASS(C) {\n" 10637 " int i;\n" 10638 " int j;\n" 10639 "};"); 10640 } 10641 10642 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10643 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10644 TwoIndent.ContinuationIndentWidth = 2; 10645 10646 EXPECT_EQ("int i =\n" 10647 " longFunction(\n" 10648 " arg);", 10649 format("int i = longFunction(arg);", TwoIndent)); 10650 10651 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10652 SixIndent.ContinuationIndentWidth = 6; 10653 10654 EXPECT_EQ("int i =\n" 10655 " longFunction(\n" 10656 " arg);", 10657 format("int i = longFunction(arg);", SixIndent)); 10658 } 10659 10660 TEST_F(FormatTest, SpacesInAngles) { 10661 FormatStyle Spaces = getLLVMStyle(); 10662 Spaces.SpacesInAngles = true; 10663 10664 verifyFormat("static_cast< int >(arg);", Spaces); 10665 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10666 verifyFormat("f< int, float >();", Spaces); 10667 verifyFormat("template <> g() {}", Spaces); 10668 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10669 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10670 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10671 Spaces); 10672 10673 Spaces.Standard = FormatStyle::LS_Cpp03; 10674 Spaces.SpacesInAngles = true; 10675 verifyFormat("A< A< int > >();", Spaces); 10676 10677 Spaces.SpacesInAngles = false; 10678 verifyFormat("A<A<int> >();", Spaces); 10679 10680 Spaces.Standard = FormatStyle::LS_Cpp11; 10681 Spaces.SpacesInAngles = true; 10682 verifyFormat("A< A< int > >();", Spaces); 10683 10684 Spaces.SpacesInAngles = false; 10685 verifyFormat("A<A<int>>();", Spaces); 10686 } 10687 10688 TEST_F(FormatTest, TripleAngleBrackets) { 10689 verifyFormat("f<<<1, 1>>>();"); 10690 verifyFormat("f<<<1, 1, 1, s>>>();"); 10691 verifyFormat("f<<<a, b, c, d>>>();"); 10692 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10693 verifyFormat("f<param><<<1, 1>>>();"); 10694 verifyFormat("f<1><<<1, 1>>>();"); 10695 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10696 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10697 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10698 } 10699 10700 TEST_F(FormatTest, MergeLessLessAtEnd) { 10701 verifyFormat("<<"); 10702 EXPECT_EQ("< < <", format("\\\n<<<")); 10703 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10704 "aaallvm::outs() <<"); 10705 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10706 "aaaallvm::outs()\n <<"); 10707 } 10708 10709 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10710 std::string code = "#if A\n" 10711 "#if B\n" 10712 "a.\n" 10713 "#endif\n" 10714 " a = 1;\n" 10715 "#else\n" 10716 "#endif\n" 10717 "#if C\n" 10718 "#else\n" 10719 "#endif\n"; 10720 EXPECT_EQ(code, format(code)); 10721 } 10722 10723 TEST_F(FormatTest, HandleConflictMarkers) { 10724 // Git/SVN conflict markers. 10725 EXPECT_EQ("int a;\n" 10726 "void f() {\n" 10727 " callme(some(parameter1,\n" 10728 "<<<<<<< text by the vcs\n" 10729 " parameter2),\n" 10730 "||||||| text by the vcs\n" 10731 " parameter2),\n" 10732 " parameter3,\n" 10733 "======= text by the vcs\n" 10734 " parameter2, parameter3),\n" 10735 ">>>>>>> text by the vcs\n" 10736 " otherparameter);\n", 10737 format("int a;\n" 10738 "void f() {\n" 10739 " callme(some(parameter1,\n" 10740 "<<<<<<< text by the vcs\n" 10741 " parameter2),\n" 10742 "||||||| text by the vcs\n" 10743 " parameter2),\n" 10744 " parameter3,\n" 10745 "======= text by the vcs\n" 10746 " parameter2,\n" 10747 " parameter3),\n" 10748 ">>>>>>> text by the vcs\n" 10749 " otherparameter);\n")); 10750 10751 // Perforce markers. 10752 EXPECT_EQ("void f() {\n" 10753 " function(\n" 10754 ">>>> text by the vcs\n" 10755 " parameter,\n" 10756 "==== text by the vcs\n" 10757 " parameter,\n" 10758 "==== text by the vcs\n" 10759 " parameter,\n" 10760 "<<<< text by the vcs\n" 10761 " parameter);\n", 10762 format("void f() {\n" 10763 " function(\n" 10764 ">>>> text by the vcs\n" 10765 " parameter,\n" 10766 "==== text by the vcs\n" 10767 " parameter,\n" 10768 "==== text by the vcs\n" 10769 " parameter,\n" 10770 "<<<< text by the vcs\n" 10771 " parameter);\n")); 10772 10773 EXPECT_EQ("<<<<<<<\n" 10774 "|||||||\n" 10775 "=======\n" 10776 ">>>>>>>", 10777 format("<<<<<<<\n" 10778 "|||||||\n" 10779 "=======\n" 10780 ">>>>>>>")); 10781 10782 EXPECT_EQ("<<<<<<<\n" 10783 "|||||||\n" 10784 "int i;\n" 10785 "=======\n" 10786 ">>>>>>>", 10787 format("<<<<<<<\n" 10788 "|||||||\n" 10789 "int i;\n" 10790 "=======\n" 10791 ">>>>>>>")); 10792 10793 // FIXME: Handle parsing of macros around conflict markers correctly: 10794 EXPECT_EQ("#define Macro \\\n" 10795 "<<<<<<<\n" 10796 "Something \\\n" 10797 "|||||||\n" 10798 "Else \\\n" 10799 "=======\n" 10800 "Other \\\n" 10801 ">>>>>>>\n" 10802 " End int i;\n", 10803 format("#define Macro \\\n" 10804 "<<<<<<<\n" 10805 " Something \\\n" 10806 "|||||||\n" 10807 " Else \\\n" 10808 "=======\n" 10809 " Other \\\n" 10810 ">>>>>>>\n" 10811 " End\n" 10812 "int i;\n")); 10813 } 10814 10815 TEST_F(FormatTest, DisableRegions) { 10816 EXPECT_EQ("int i;\n" 10817 "// clang-format off\n" 10818 " int j;\n" 10819 "// clang-format on\n" 10820 "int k;", 10821 format(" int i;\n" 10822 " // clang-format off\n" 10823 " int j;\n" 10824 " // clang-format on\n" 10825 " int k;")); 10826 EXPECT_EQ("int i;\n" 10827 "/* clang-format off */\n" 10828 " int j;\n" 10829 "/* clang-format on */\n" 10830 "int k;", 10831 format(" int i;\n" 10832 " /* clang-format off */\n" 10833 " int j;\n" 10834 " /* clang-format on */\n" 10835 " int k;")); 10836 } 10837 10838 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10839 format("? ) ="); 10840 verifyNoCrash("#define a\\\n /**/}"); 10841 } 10842 10843 } // end namespace 10844 } // end namespace format 10845 } // end namespace clang 10846