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