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