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