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