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