FormatTest.cpp revision 54e6c9d1f15153d1756642080866f10a91cc86ea
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#define DEBUG_TYPE "format-test" 11 12#include "clang/Format/Format.h" 13#include "clang/Lex/Lexer.h" 14#include "llvm/Support/Debug.h" 15#include "gtest/gtest.h" 16 17namespace clang { 18namespace format { 19 20class FormatTest : public ::testing::Test { 21protected: 22 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 23 const FormatStyle &Style) { 24 DEBUG(llvm::errs() << "---\n"); 25 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 26 tooling::Replacements Replaces = reformat(Style, Code, Ranges); 27 ReplacementCount = Replaces.size(); 28 std::string Result = applyAllReplacements(Code, Replaces); 29 EXPECT_NE("", Result); 30 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 31 return Result; 32 } 33 34 std::string 35 format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { 36 return format(Code, 0, Code.size(), Style); 37 } 38 39 std::string messUp(llvm::StringRef Code) { 40 std::string MessedUp(Code.str()); 41 bool InComment = false; 42 bool InPreprocessorDirective = false; 43 bool JustReplacedNewline = false; 44 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { 45 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { 46 if (JustReplacedNewline) 47 MessedUp[i - 1] = '\n'; 48 InComment = true; 49 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { 50 if (i != 0) 51 MessedUp[i - 1] = '\n'; 52 InPreprocessorDirective = true; 53 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { 54 MessedUp[i] = ' '; 55 MessedUp[i + 1] = ' '; 56 } else if (MessedUp[i] == '\n') { 57 if (InComment) { 58 InComment = false; 59 } else if (InPreprocessorDirective) { 60 InPreprocessorDirective = false; 61 } else { 62 JustReplacedNewline = true; 63 MessedUp[i] = ' '; 64 } 65 } else if (MessedUp[i] != ' ') { 66 JustReplacedNewline = false; 67 } 68 } 69 return MessedUp; 70 } 71 72 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 73 FormatStyle Style = getLLVMStyle(); 74 Style.ColumnLimit = ColumnLimit; 75 return Style; 76 } 77 78 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 79 FormatStyle Style = getGoogleStyle(); 80 Style.ColumnLimit = ColumnLimit; 81 return Style; 82 } 83 84 void verifyFormat(llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 EXPECT_EQ(Code.str(), format(messUp(Code), Style)); 87 } 88 89 void verifyGoogleFormat(llvm::StringRef Code) { 90 verifyFormat(Code, getGoogleStyle()); 91 } 92 93 void verifyIndependentOfContext(llvm::StringRef text) { 94 verifyFormat(text); 95 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 96 } 97 98 int ReplacementCount; 99}; 100 101TEST_F(FormatTest, MessUp) { 102 EXPECT_EQ("1 2 3", messUp("1 2 3")); 103 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); 104 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); 105 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); 106 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); 107} 108 109//===----------------------------------------------------------------------===// 110// Basic function tests. 111//===----------------------------------------------------------------------===// 112 113TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { 114 EXPECT_EQ(";", format(";")); 115} 116 117TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 118 EXPECT_EQ("int i;", format(" int i;")); 119 EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); 120 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 121 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 122} 123 124TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 125 EXPECT_EQ("int i;", format("int\ni;")); 126} 127 128TEST_F(FormatTest, FormatsNestedBlockStatements) { 129 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 130} 131 132TEST_F(FormatTest, FormatsNestedCall) { 133 verifyFormat("Method(f1, f2(f3));"); 134 verifyFormat("Method(f1(f2, f3()));"); 135 verifyFormat("Method(f1(f2, (f3())));"); 136} 137 138TEST_F(FormatTest, NestedNameSpecifiers) { 139 verifyFormat("vector< ::Type> v;"); 140 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 141} 142 143TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 144 EXPECT_EQ("if (a) {\n" 145 " f();\n" 146 "}", 147 format("if(a){f();}")); 148 EXPECT_EQ(4, ReplacementCount); 149 EXPECT_EQ("if (a) {\n" 150 " f();\n" 151 "}", 152 format("if (a) {\n" 153 " f();\n" 154 "}")); 155 EXPECT_EQ(0, ReplacementCount); 156} 157 158TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { 159 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); 160 EXPECT_EQ("int a;", format("int a; ")); 161 EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); 162 EXPECT_EQ("int a;\nint b; ", 163 format("int a; \nint b; ", 0, 0, getLLVMStyle())); 164} 165 166TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { 167 EXPECT_EQ("int b;\nint a;", 168 format("int b;\n int a;", 7, 0, getLLVMStyle())); 169 EXPECT_EQ("int b;\n int a;", 170 format("int b;\n int a;", 6, 0, getLLVMStyle())); 171 172 EXPECT_EQ("#define A \\\n" 173 " int a; \\\n" 174 " int b;", 175 format("#define A \\\n" 176 " int a; \\\n" 177 " int b;", 178 26, 0, getLLVMStyleWithColumns(12))); 179 EXPECT_EQ("#define A \\\n" 180 " int a; \\\n" 181 " int b;", 182 format("#define A \\\n" 183 " int a; \\\n" 184 " int b;", 185 25, 0, getLLVMStyleWithColumns(12))); 186} 187 188TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 189 EXPECT_EQ("int a;\n\n int b;", 190 format("int a;\n \n\n int b;", 7, 0, getLLVMStyle())); 191 EXPECT_EQ("int a;\n\n int b;", 192 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); 193} 194 195TEST_F(FormatTest, RemovesEmptyLines) { 196 EXPECT_EQ("class C {\n" 197 " int i;\n" 198 "};", 199 format("class C {\n" 200 " int i;\n" 201 "\n" 202 "};")); 203 204 // Don't remove empty lines in more complex control statements. 205 EXPECT_EQ("void f() {\n" 206 " if (a) {\n" 207 " f();\n" 208 "\n" 209 " } else if (b) {\n" 210 " f();\n" 211 " }\n" 212 "}", 213 format("void f() {\n" 214 " if (a) {\n" 215 " f();\n" 216 "\n" 217 " } else if (b) {\n" 218 " f();\n" 219 "\n" 220 " }\n" 221 "\n" 222 "}")); 223 224 // FIXME: This is slightly inconsistent. 225 EXPECT_EQ("namespace {\n" 226 "int i;\n" 227 "}", 228 format("namespace {\n" 229 "int i;\n" 230 "\n" 231 "}")); 232 EXPECT_EQ("namespace {\n" 233 "int i;\n" 234 "\n" 235 "} // namespace", 236 format("namespace {\n" 237 "int i;\n" 238 "\n" 239 "} // namespace")); 240} 241 242TEST_F(FormatTest, ReformatsMovedLines) { 243 EXPECT_EQ( 244 "template <typename T> T *getFETokenInfo() const {\n" 245 " return static_cast<T *>(FETokenInfo);\n" 246 "}\n" 247 " int a; // <- Should not be formatted", 248 format( 249 "template<typename T>\n" 250 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 251 " int a; // <- Should not be formatted", 252 9, 5, getLLVMStyle())); 253} 254 255//===----------------------------------------------------------------------===// 256// Tests for control statements. 257//===----------------------------------------------------------------------===// 258 259TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 260 verifyFormat("if (true)\n f();\ng();"); 261 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 262 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 263 264 FormatStyle AllowsMergedIf = getLLVMStyle(); 265 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 266 verifyFormat("if (a)\n" 267 " // comment\n" 268 " f();", 269 AllowsMergedIf); 270 verifyFormat("if (a)\n" 271 " ;", 272 AllowsMergedIf); 273 verifyFormat("if (a)\n" 274 " if (b) return;", 275 AllowsMergedIf); 276 277 verifyFormat("if (a) // Can't merge this\n" 278 " f();\n", 279 AllowsMergedIf); 280 verifyFormat("if (a) /* still don't merge */\n" 281 " f();", 282 AllowsMergedIf); 283 verifyFormat("if (a) { // Never merge this\n" 284 " f();\n" 285 "}", 286 AllowsMergedIf); 287 verifyFormat("if (a) { /* Never merge this */\n" 288 " f();\n" 289 "}", 290 AllowsMergedIf); 291 292 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); 293 EXPECT_EQ("if (a) return; // comment", 294 format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); 295 296 AllowsMergedIf.ColumnLimit = 14; 297 verifyFormat("if (a) return;", AllowsMergedIf); 298 verifyFormat("if (aaaaaaaaa)\n" 299 " return;", 300 AllowsMergedIf); 301 302 AllowsMergedIf.ColumnLimit = 13; 303 verifyFormat("if (a)\n return;", AllowsMergedIf); 304} 305 306TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 307 FormatStyle AllowsMergedLoops = getLLVMStyle(); 308 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 309 verifyFormat("while (true) continue;", AllowsMergedLoops); 310 verifyFormat("for (;;) continue;", AllowsMergedLoops); 311 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 312 verifyFormat("while (true)\n" 313 " ;", 314 AllowsMergedLoops); 315 verifyFormat("for (;;)\n" 316 " ;", 317 AllowsMergedLoops); 318 verifyFormat("for (;;)\n" 319 " for (;;) continue;", 320 AllowsMergedLoops); 321 verifyFormat("for (;;) // Can't merge this\n" 322 " continue;", 323 AllowsMergedLoops); 324 verifyFormat("for (;;) /* still don't merge */\n" 325 " continue;", 326 AllowsMergedLoops); 327} 328 329TEST_F(FormatTest, ParseIfElse) { 330 verifyFormat("if (true)\n" 331 " if (true)\n" 332 " if (true)\n" 333 " f();\n" 334 " else\n" 335 " g();\n" 336 " else\n" 337 " h();\n" 338 "else\n" 339 " i();"); 340 verifyFormat("if (true)\n" 341 " if (true)\n" 342 " if (true) {\n" 343 " if (true)\n" 344 " f();\n" 345 " } else {\n" 346 " g();\n" 347 " }\n" 348 " else\n" 349 " h();\n" 350 "else {\n" 351 " i();\n" 352 "}"); 353} 354 355TEST_F(FormatTest, ElseIf) { 356 verifyFormat("if (a) {\n} else if (b) {\n}"); 357 verifyFormat("if (a)\n" 358 " f();\n" 359 "else if (b)\n" 360 " g();\n" 361 "else\n" 362 " h();"); 363} 364 365TEST_F(FormatTest, FormatsForLoop) { 366 verifyFormat( 367 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 368 " ++VeryVeryLongLoopVariable)\n" 369 " ;"); 370 verifyFormat("for (;;)\n" 371 " f();"); 372 verifyFormat("for (;;) {\n}"); 373 verifyFormat("for (;;) {\n" 374 " f();\n" 375 "}"); 376 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 377 378 verifyFormat( 379 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 380 " E = UnwrappedLines.end();\n" 381 " I != E; ++I) {\n}"); 382 383 verifyFormat( 384 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 385 " ++IIIII) {\n}"); 386 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 387 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 388 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 389 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 390 " I = FD->getDeclsInPrototypeScope().begin(),\n" 391 " E = FD->getDeclsInPrototypeScope().end();\n" 392 " I != E; ++I) {\n}"); 393 394 // FIXME: Not sure whether we want extra identation in line 3 here: 395 verifyFormat( 396 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 397 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 400 " ++aaaaaaaaaaa) {\n}"); 401 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 402 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 403 "}"); 404 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 405 " aaaaaaaaaa);\n" 406 " iter; ++iter) {\n" 407 "}"); 408 409 FormatStyle NoBinPacking = getLLVMStyle(); 410 NoBinPacking.BinPackParameters = false; 411 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 412 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 413 " aaaaaaaaaaaaaaaa,\n" 414 " aaaaaaaaaaaaaaaa,\n" 415 " aaaaaaaaaaaaaaaa);\n" 416 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 417 "}", 418 NoBinPacking); 419 verifyFormat( 420 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 421 " E = UnwrappedLines.end();\n" 422 " I != E;\n" 423 " ++I) {\n}", 424 NoBinPacking); 425} 426 427TEST_F(FormatTest, RangeBasedForLoops) { 428 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 430 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 431 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 432 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 434} 435 436TEST_F(FormatTest, FormatsWhileLoop) { 437 verifyFormat("while (true) {\n}"); 438 verifyFormat("while (true)\n" 439 " f();"); 440 verifyFormat("while () {\n}"); 441 verifyFormat("while () {\n" 442 " f();\n" 443 "}"); 444} 445 446TEST_F(FormatTest, FormatsDoWhile) { 447 verifyFormat("do {\n" 448 " do_something();\n" 449 "} while (something());"); 450 verifyFormat("do\n" 451 " do_something();\n" 452 "while (something());"); 453} 454 455TEST_F(FormatTest, FormatsSwitchStatement) { 456 verifyFormat("switch (x) {\n" 457 "case 1:\n" 458 " f();\n" 459 " break;\n" 460 "case kFoo:\n" 461 "case ns::kBar:\n" 462 "case kBaz:\n" 463 " break;\n" 464 "default:\n" 465 " g();\n" 466 " break;\n" 467 "}"); 468 verifyFormat("switch (x) {\n" 469 "case 1: {\n" 470 " f();\n" 471 " break;\n" 472 "}\n" 473 "}"); 474 verifyFormat("switch (x) {\n" 475 "case 1: {\n" 476 " f();\n" 477 " {\n" 478 " g();\n" 479 " h();\n" 480 " }\n" 481 " break;\n" 482 "}\n" 483 "}"); 484 verifyFormat("switch (x) {\n" 485 "case 1: {\n" 486 " f();\n" 487 " if (foo) {\n" 488 " g();\n" 489 " h();\n" 490 " }\n" 491 " break;\n" 492 "}\n" 493 "}"); 494 verifyFormat("switch (x) {\n" 495 "case 1: {\n" 496 " f();\n" 497 " g();\n" 498 "} break;\n" 499 "}"); 500 verifyFormat("switch (test)\n" 501 " ;"); 502 verifyFormat("switch (x) {\n" 503 "default: {\n" 504 " // Do nothing.\n" 505 "}\n" 506 "}"); 507 verifyFormat("switch (x) {\n" 508 "// comment\n" 509 "// if 1, do f()\n" 510 "case 1:\n" 511 " f();\n" 512 "}"); 513 verifyFormat("switch (x) {\n" 514 "case 1:\n" 515 " // Do amazing stuff\n" 516 " {\n" 517 " f();\n" 518 " g();\n" 519 " }\n" 520 " break;\n" 521 "}"); 522 verifyFormat("#define A \\\n" 523 " switch (x) { \\\n" 524 " case a: \\\n" 525 " foo = b; \\\n" 526 " }", getLLVMStyleWithColumns(20)); 527 528 verifyGoogleFormat("switch (x) {\n" 529 " case 1:\n" 530 " f();\n" 531 " break;\n" 532 " case kFoo:\n" 533 " case ns::kBar:\n" 534 " case kBaz:\n" 535 " break;\n" 536 " default:\n" 537 " g();\n" 538 " break;\n" 539 "}"); 540 verifyGoogleFormat("switch (x) {\n" 541 " case 1: {\n" 542 " f();\n" 543 " break;\n" 544 " }\n" 545 "}"); 546 verifyGoogleFormat("switch (test)\n" 547 " ;"); 548} 549 550TEST_F(FormatTest, FormatsLabels) { 551 verifyFormat("void f() {\n" 552 " some_code();\n" 553 "test_label:\n" 554 " some_other_code();\n" 555 " {\n" 556 " some_more_code();\n" 557 " another_label:\n" 558 " some_more_code();\n" 559 " }\n" 560 "}"); 561 verifyFormat("some_code();\n" 562 "test_label:\n" 563 "some_other_code();"); 564} 565 566//===----------------------------------------------------------------------===// 567// Tests for comments. 568//===----------------------------------------------------------------------===// 569 570TEST_F(FormatTest, UnderstandsSingleLineComments) { 571 verifyFormat("//* */"); 572 verifyFormat("// line 1\n" 573 "// line 2\n" 574 "void f() {}\n"); 575 576 verifyFormat("void f() {\n" 577 " // Doesn't do anything\n" 578 "}"); 579 verifyFormat("void f(int i, // some comment (probably for i)\n" 580 " int j, // some comment (probably for j)\n" 581 " int k); // some comment (probably for k)"); 582 verifyFormat("void f(int i,\n" 583 " // some comment (probably for j)\n" 584 " int j,\n" 585 " // some comment (probably for k)\n" 586 " int k);"); 587 588 verifyFormat("int i // This is a fancy variable\n" 589 " = 5; // with nicely aligned comment."); 590 591 verifyFormat("// Leading comment.\n" 592 "int a; // Trailing comment."); 593 verifyFormat("int a; // Trailing comment\n" 594 " // on 2\n" 595 " // or 3 lines.\n" 596 "int b;"); 597 verifyFormat("int a; // Trailing comment\n" 598 "\n" 599 "// Leading comment.\n" 600 "int b;"); 601 verifyFormat("int a; // Comment.\n" 602 " // More details.\n" 603 "int bbbb; // Another comment."); 604 verifyFormat( 605 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 606 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 607 "int cccccccccccccccccccccccccccccc; // comment\n" 608 "int ddd; // looooooooooooooooooooooooong comment\n" 609 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 610 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 611 "int ccccccccccccccccccc; // comment"); 612 613 verifyFormat("#include \"a\" // comment\n" 614 "#include \"a/b/c\" // comment"); 615 verifyFormat("#include <a> // comment\n" 616 "#include <a/b/c> // comment"); 617 EXPECT_EQ("#include \\\n" 618 " \"a\" // comment\n" 619 "#include \"a/b/c\" // comment", 620 format("#include \\\n" 621 " \"a\" // comment\n" 622 "#include \"a/b/c\" // comment")); 623 624 verifyFormat("enum E {\n" 625 " // comment\n" 626 " VAL_A, // comment\n" 627 " VAL_B\n" 628 "};"); 629 630 verifyFormat( 631 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 632 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 633 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 634 " // Comment inside a statement.\n" 635 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 636 verifyFormat( 637 "bool aaaaaaaaaaaaa = // comment\n" 638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 640 641 verifyFormat("int aaaa; // aaaaa\n" 642 "int aa; // aaaaaaa", 643 getLLVMStyleWithColumns(20)); 644 645 EXPECT_EQ("void f() { // This does something ..\n" 646 "}\n" 647 "int a; // This is unrelated", 648 format("void f() { // This does something ..\n" 649 " }\n" 650 "int a; // This is unrelated")); 651 EXPECT_EQ("void f() { // This does something ..\n" 652 "} // awesome..\n" 653 "\n" 654 "int a; // This is unrelated", 655 format("void f() { // This does something ..\n" 656 " } // awesome..\n" 657 " \n" 658 "int a; // This is unrelated")); 659 660 EXPECT_EQ("int i; // single line trailing comment", 661 format("int i;\\\n// single line trailing comment")); 662 663 verifyGoogleFormat("int a; // Trailing comment."); 664 665 verifyFormat("someFunction(anotherFunction( // Force break.\n" 666 " parameter));"); 667 668 verifyGoogleFormat("#endif // HEADER_GUARD"); 669 670 verifyFormat("const char *test[] = {\n" 671 " // A\n" 672 " \"aaaa\",\n" 673 " // B\n" 674 " \"aaaaa\",\n" 675 "};"); 676 verifyGoogleFormat( 677 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 678 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 679 EXPECT_EQ("D(a, {\n" 680 " // test\n" 681 " int a;\n" 682 "});", 683 format("D(a, {\n" 684 "// test\n" 685 "int a;\n" 686 "});")); 687 688 EXPECT_EQ("lineWith(); // comment\n" 689 "// at start\n" 690 "otherLine();", 691 format("lineWith(); // comment\n" 692 "// at start\n" 693 "otherLine();")); 694 EXPECT_EQ("lineWith(); // comment\n" 695 " // at start\n" 696 "otherLine();", 697 format("lineWith(); // comment\n" 698 " // at start\n" 699 "otherLine();")); 700 701 EXPECT_EQ("lineWith(); // comment\n" 702 "// at start\n" 703 "otherLine(); // comment", 704 format("lineWith(); // comment\n" 705 "// at start\n" 706 "otherLine(); // comment")); 707 EXPECT_EQ("lineWith();\n" 708 "// at start\n" 709 "otherLine(); // comment", 710 format("lineWith();\n" 711 " // at start\n" 712 "otherLine(); // comment")); 713 EXPECT_EQ("// first\n" 714 "// at start\n" 715 "otherLine(); // comment", 716 format("// first\n" 717 " // at start\n" 718 "otherLine(); // comment")); 719 EXPECT_EQ("f();\n" 720 "// first\n" 721 "// at start\n" 722 "otherLine(); // comment", 723 format("f();\n" 724 "// first\n" 725 " // at start\n" 726 "otherLine(); // comment")); 727} 728 729TEST_F(FormatTest, CanFormatCommentsLocally) { 730 EXPECT_EQ("int a; // comment\n" 731 "int b; // comment", 732 format("int a; // comment\n" 733 "int b; // comment", 734 0, 0, getLLVMStyle())); 735 EXPECT_EQ("int a; // comment\n" 736 " // line 2\n" 737 "int b;", 738 format("int a; // comment\n" 739 " // line 2\n" 740 "int b;", 741 28, 0, getLLVMStyle())); 742 EXPECT_EQ("int aaaaaa; // comment\n" 743 "int b;\n" 744 "int c; // unrelated comment", 745 format("int aaaaaa; // comment\n" 746 "int b;\n" 747 "int c; // unrelated comment", 748 31, 0, getLLVMStyle())); 749} 750 751TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 752 EXPECT_EQ("// comment", format("// comment ")); 753 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 754 format("int aaaaaaa, bbbbbbb; // comment ", 755 getLLVMStyleWithColumns(33))); 756} 757 758TEST_F(FormatTest, UnderstandsMultiLineComments) { 759 verifyFormat("f(/*test=*/ true);"); 760 EXPECT_EQ( 761 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 762 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 763 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n" 764 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 765 EXPECT_EQ( 766 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 767 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 768 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 769 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 770 EXPECT_EQ( 771 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 772 " aaaaaaaaaaaaaaaaaa,\n" 773 " aaaaaaaaaaaaaaaaaa) { /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n" 774 "}", 775 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 776 " aaaaaaaaaaaaaaaaaa ,\n" 777 " aaaaaaaaaaaaaaaaaa) { /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n" 778 "}")); 779 780 FormatStyle NoBinPacking = getLLVMStyle(); 781 NoBinPacking.BinPackParameters = false; 782 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 783 " /* parameter 2 */ aaaaaa,\n" 784 " /* parameter 3 */ aaaaaa,\n" 785 " /* parameter 4 */ aaaaaa);", 786 NoBinPacking); 787} 788 789TEST_F(FormatTest, AlignsMultiLineComments) { 790 EXPECT_EQ("/*\n" 791 " * Really multi-line\n" 792 " * comment.\n" 793 " */\n" 794 "void f() {}", 795 format(" /*\n" 796 " * Really multi-line\n" 797 " * comment.\n" 798 " */\n" 799 " void f() {}")); 800 EXPECT_EQ("class C {\n" 801 " /*\n" 802 " * Another multi-line\n" 803 " * comment.\n" 804 " */\n" 805 " void f() {}\n" 806 "};", 807 format("class C {\n" 808 "/*\n" 809 " * Another multi-line\n" 810 " * comment.\n" 811 " */\n" 812 "void f() {}\n" 813 "};")); 814 EXPECT_EQ("/*\n" 815 " 1. This is a comment with non-trivial formatting.\n" 816 " 1.1. We have to indent/outdent all lines equally\n" 817 " 1.1.1. to keep the formatting.\n" 818 " */", 819 format(" /*\n" 820 " 1. This is a comment with non-trivial formatting.\n" 821 " 1.1. We have to indent/outdent all lines equally\n" 822 " 1.1.1. to keep the formatting.\n" 823 " */")); 824 EXPECT_EQ("/*\n" 825 "Don't try to outdent if there's not enough inentation.\n" 826 "*/", 827 format(" /*\n" 828 " Don't try to outdent if there's not enough inentation.\n" 829 " */")); 830 831 EXPECT_EQ("int i; /* Comment with empty...\n" 832 " *\n" 833 " * line. */", 834 format("int i; /* Comment with empty...\n" 835 " *\n" 836 " * line. */")); 837} 838 839TEST_F(FormatTest, SplitsLongCxxComments) { 840 EXPECT_EQ("// A comment that\n" 841 "// doesn't fit on\n" 842 "// one line", 843 format("// A comment that doesn't fit on one line", 844 getLLVMStyleWithColumns(20))); 845 EXPECT_EQ("// a b c d\n" 846 "// e f g\n" 847 "// h i j k", 848 format("// a b c d e f g h i j k", 849 getLLVMStyleWithColumns(10))); 850 EXPECT_EQ("// a b c d\n" 851 "// e f g\n" 852 "// h i j k", 853 format("\\\n// a b c d e f g h i j k", 854 getLLVMStyleWithColumns(10))); 855 EXPECT_EQ("if (true) // A comment that\n" 856 " // doesn't fit on\n" 857 " // one line", 858 format("if (true) // A comment that doesn't fit on one line ", 859 getLLVMStyleWithColumns(30))); 860 EXPECT_EQ("// Don't_touch_leading_whitespace", 861 format("// Don't_touch_leading_whitespace", 862 getLLVMStyleWithColumns(20))); 863 EXPECT_EQ( 864 "//Don't add leading\n" 865 "//whitespace", 866 format("//Don't add leading whitespace", getLLVMStyleWithColumns(20))); 867 EXPECT_EQ("// A comment before\n" 868 "// a macro\n" 869 "// definition\n" 870 "#define a b", 871 format("// A comment before a macro definition\n" 872 "#define a b", 873 getLLVMStyleWithColumns(20))); 874} 875 876TEST_F(FormatTest, PriorityOfCommentBreaking) { 877 EXPECT_EQ("if (xxx == yyy && // aaaaaaaaaaaa\n" 878 " // bbbbbbbbb\n" 879 " zzz)\n" 880 " q();", 881 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 882 " zzz) q();", 883 getLLVMStyleWithColumns(40))); 884 EXPECT_EQ("if (xxxxxxxxxx ==\n" 885 " yyy && // aaaaaa bbbbbbbb cccc\n" 886 " zzz)\n" 887 " q();", 888 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 889 " zzz) q();", 890 getLLVMStyleWithColumns(40))); 891 EXPECT_EQ("if (xxxxxxxxxx &&\n" 892 " yyy || // aaaaaa bbbbbbbb cccc\n" 893 " zzz)\n" 894 " q();", 895 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 896 " zzz) q();", 897 getLLVMStyleWithColumns(40))); 898 EXPECT_EQ("fffffffff(&xxx, // aaaaaaaaaaaa\n" 899 " // bbbbbbbbbbb\n" 900 " zzz);", 901 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 902 " zzz);", 903 getLLVMStyleWithColumns(40))); 904} 905 906TEST_F(FormatTest, MultiLineCommentsInDefines) { 907 // FIXME: The line breaks are still suboptimal (current guess 908 // is that this is due to the token length being misused), but 909 // the comment handling is correct. 910 EXPECT_EQ("#define A( \\\n" 911 " x) /* \\\n" 912 "a comment \\\n" 913 "inside */ \\\n" 914 " f();", 915 format("#define A(x) /* \\\n" 916 " a comment \\\n" 917 " inside */ \\\n" 918 " f();", 919 getLLVMStyleWithColumns(17))); 920 EXPECT_EQ("#define A(x) /* \\\n" 921 " a \\\n" 922 " comment \\\n" 923 " inside \\\n" 924 " */ \\\n" 925 " f();", 926 format("#define A( \\\n" 927 " x) /* \\\n" 928 " a comment \\\n" 929 " inside */ \\\n" 930 " f();", 931 getLLVMStyleWithColumns(17))); 932} 933 934TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 935 EXPECT_EQ("namespace {}\n// Test\n#define A", 936 format("namespace {}\n // Test\n#define A")); 937 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 938 format("namespace {}\n /* Test */\n#define A")); 939 EXPECT_EQ("namespace {}\n/* Test */ #define A", 940 format("namespace {}\n /* Test */ #define A")); 941} 942 943TEST_F(FormatTest, SplitsLongLinesInComments) { 944 EXPECT_EQ("/* This is a long\n" 945 " * comment that\n" 946 " * doesn't\n" 947 " * fit on one line.\n" 948 " */", 949 format("/* " 950 "This is a long " 951 "comment that " 952 "doesn't " 953 "fit on one line. */", 954 getLLVMStyleWithColumns(20))); 955 EXPECT_EQ("/* a b c d\n" 956 " * e f g\n" 957 " * h i j k\n" 958 " */", 959 format("/* a b c d e f g h i j k */", 960 getLLVMStyleWithColumns(10))); 961 EXPECT_EQ("/* a b c d\n" 962 " * e f g\n" 963 " * h i j k\n" 964 " */", 965 format("\\\n/* a b c d e f g h i j k */", 966 getLLVMStyleWithColumns(10))); 967 EXPECT_EQ("/*\n" 968 "This is a long\n" 969 "comment that doesn't\n" 970 "fit on one line.\n" 971 "*/", 972 format("/*\n" 973 "This is a long " 974 "comment that doesn't " 975 "fit on one line. \n" 976 "*/", getLLVMStyleWithColumns(20))); 977 EXPECT_EQ("/*\n" 978 " * This is a long\n" 979 " * comment that\n" 980 " * doesn't fit on\n" 981 " * one line.\n" 982 " */", 983 format("/* \n" 984 " * This is a long " 985 " comment that " 986 " doesn't fit on " 987 " one line. \n" 988 " */", getLLVMStyleWithColumns(20))); 989 EXPECT_EQ("/*\n" 990 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 991 " * so_it_should_be_broken\n" 992 " * wherever_a_space_occurs\n" 993 " */", 994 format("/*\n" 995 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 996 " so_it_should_be_broken " 997 " wherever_a_space_occurs \n" 998 " */", 999 getLLVMStyleWithColumns(20))); 1000 EXPECT_EQ("/*\n" 1001 " * This_comment_can_not_be_broken_into_lines\n" 1002 " */", 1003 format("/*\n" 1004 " * This_comment_can_not_be_broken_into_lines\n" 1005 " */", 1006 getLLVMStyleWithColumns(20))); 1007 EXPECT_EQ("{\n" 1008 " /*\n" 1009 " This is another\n" 1010 " long comment that\n" 1011 " doesn't fit on one\n" 1012 " line 1234567890\n" 1013 " */\n" 1014 "}", 1015 format("{\n" 1016 "/*\n" 1017 "This is another " 1018 " long comment that " 1019 " doesn't fit on one" 1020 " line 1234567890\n" 1021 "*/\n" 1022 "}", getLLVMStyleWithColumns(20))); 1023 EXPECT_EQ("{\n" 1024 " /*\n" 1025 " * This i s\n" 1026 " * another comment\n" 1027 " * t hat doesn' t\n" 1028 " * fit on one l i\n" 1029 " * n e\n" 1030 " */\n" 1031 "}", 1032 format("{\n" 1033 "/*\n" 1034 " * This i s" 1035 " another comment" 1036 " t hat doesn' t" 1037 " fit on one l i" 1038 " n e\n" 1039 " */\n" 1040 "}", getLLVMStyleWithColumns(20))); 1041 EXPECT_EQ("/*\n" 1042 " * This is a long\n" 1043 " * comment that\n" 1044 " * doesn't fit on\n" 1045 " * one line\n" 1046 " */", 1047 format(" /*\n" 1048 " * This is a long comment that doesn't fit on one line\n" 1049 " */", getLLVMStyleWithColumns(20))); 1050 EXPECT_EQ("{\n" 1051 " if (something) /* This is a\n" 1052 " long\n" 1053 " comment */\n" 1054 " ;\n" 1055 "}", 1056 format("{\n" 1057 " if (something) /* This is a long comment */\n" 1058 " ;\n" 1059 "}", 1060 getLLVMStyleWithColumns(30))); 1061 1062 EXPECT_EQ("/* A comment before\n" 1063 " * a macro\n" 1064 " * definition */\n" 1065 "#define a b", 1066 format("/* A comment before a macro definition */\n" 1067 "#define a b", 1068 getLLVMStyleWithColumns(20))); 1069 1070 EXPECT_EQ("/* some comment\n" 1071 " * a comment\n" 1072 "* that we break\n" 1073 " * another comment\n" 1074 "* we have to break\n" 1075 "* a left comment\n" 1076 " */", 1077 format(" /* some comment\n" 1078 " * a comment that we break\n" 1079 " * another comment we have to break\n" 1080 "* a left comment\n" 1081 " */", 1082 getLLVMStyleWithColumns(20))); 1083 1084 EXPECT_EQ("/*\n" 1085 "\n" 1086 "\n" 1087 " */\n", 1088 format(" /* \n" 1089 " \n" 1090 " \n" 1091 " */\n")); 1092} 1093 1094TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1095 EXPECT_EQ("#define X \\\n" 1096 " /* \\\n" 1097 " Test \\\n" 1098 " Macro comment \\\n" 1099 " with a long \\\n" 1100 " line \\\n" 1101 " */ \\\n" 1102 " A + B", 1103 format("#define X \\\n" 1104 " /*\n" 1105 " Test\n" 1106 " Macro comment with a long line\n" 1107 " */ \\\n" 1108 " A + B", 1109 getLLVMStyleWithColumns(20))); 1110 EXPECT_EQ("#define X \\\n" 1111 " /* Macro comment \\\n" 1112 " with a long \\\n" 1113 " line */ \\\n" 1114 " A + B", 1115 format("#define X \\\n" 1116 " /* Macro comment with a long\n" 1117 " line */ \\\n" 1118 " A + B", 1119 getLLVMStyleWithColumns(20))); 1120 EXPECT_EQ("#define X \\\n" 1121 " /* Macro comment \\\n" 1122 " * with a long \\\n" 1123 " * line */ \\\n" 1124 " A + B", 1125 format("#define X \\\n" 1126 " /* Macro comment with a long line */ \\\n" 1127 " A + B", 1128 getLLVMStyleWithColumns(20))); 1129} 1130 1131TEST_F(FormatTest, CommentsInStaticInitializers) { 1132 EXPECT_EQ( 1133 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1134 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1135 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1136 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1137 " aaaaaaaaaaaaaaaaaaaa };", 1138 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1139 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1140 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1141 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1142 " aaaaaaaaaaaaaaaaaaaa };")); 1143 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 1144 " bbbbbbbbbbb, ccccccccccc };"); 1145 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" 1146 " // comment for bb....\n" 1147 " bbbbbbbbbbb, ccccccccccc };"); 1148 verifyGoogleFormat( 1149 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1150 " bbbbbbbbbbb, ccccccccccc};"); 1151 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1152 " // comment for bb....\n" 1153 " bbbbbbbbbbb, ccccccccccc};"); 1154 1155 verifyFormat("S s = { { a, b, c }, // Group #1\n" 1156 " { d, e, f }, // Group #2\n" 1157 " { g, h, i } }; // Group #3"); 1158 verifyFormat("S s = { { // Group #1\n" 1159 " a, b, c },\n" 1160 " { // Group #2\n" 1161 " d, e, f },\n" 1162 " { // Group #3\n" 1163 " g, h, i } };"); 1164 1165 EXPECT_EQ("S s = {\n" 1166 " // Some comment\n" 1167 " a,\n" 1168 "\n" 1169 " // Comment after empty line\n" 1170 " b\n" 1171 "}", 1172 format("S s = {\n" 1173 " // Some comment\n" 1174 " a,\n" 1175 " \n" 1176 " // Comment after empty line\n" 1177 " b\n" 1178 "}")); 1179 EXPECT_EQ("S s = { a, b };", format("S s = {\n" 1180 " a,\n" 1181 "\n" 1182 " b\n" 1183 "};")); 1184 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1185 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1186 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1187 " 0x00, 0x00, 0x00, 0x00 // comment\n" 1188 "};"); 1189} 1190 1191TEST_F(FormatTest, IgnoresIf0Contents) { 1192 EXPECT_EQ("#if 0\n" 1193 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1194 "#endif\n" 1195 "void f() {}", 1196 format("#if 0\n" 1197 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1198 "#endif\n" 1199 "void f( ) { }")); 1200 EXPECT_EQ("#if false\n" 1201 "void f( ) { }\n" 1202 "#endif\n" 1203 "void g() {}\n", 1204 format("#if false\n" 1205 "void f( ) { }\n" 1206 "#endif\n" 1207 "void g( ) { }\n")); 1208 EXPECT_EQ("enum E {\n" 1209 " One,\n" 1210 " Two,\n" 1211 "#if 0\n" 1212 "Three,\n" 1213 " Four,\n" 1214 "#endif\n" 1215 " Five\n" 1216 "};", 1217 format("enum E {\n" 1218 " One,Two,\n" 1219 "#if 0\n" 1220 "Three,\n" 1221 " Four,\n" 1222 "#endif\n" 1223 " Five};")); 1224 EXPECT_EQ("enum F {\n" 1225 " One,\n" 1226 "#if 1\n" 1227 " Two,\n" 1228 "#if 0\n" 1229 "Three,\n" 1230 " Four,\n" 1231 "#endif\n" 1232 " Five\n" 1233 "#endif\n" 1234 "};", 1235 format("enum F {\n" 1236 "One,\n" 1237 "#if 1\n" 1238 "Two,\n" 1239 "#if 0\n" 1240 "Three,\n" 1241 " Four,\n" 1242 "#endif\n" 1243 "Five\n" 1244 "#endif\n" 1245 "};")); 1246 EXPECT_EQ("enum G {\n" 1247 " One,\n" 1248 "#if 0\n" 1249 "Two,\n" 1250 "#else\n" 1251 " Three,\n" 1252 "#endif\n" 1253 " Four\n" 1254 "};", 1255 format("enum G {\n" 1256 "One,\n" 1257 "#if 0\n" 1258 "Two,\n" 1259 "#else\n" 1260 "Three,\n" 1261 "#endif\n" 1262 "Four\n" 1263 "};")); 1264 EXPECT_EQ("enum H {\n" 1265 " One,\n" 1266 "#if 0\n" 1267 "#ifdef Q\n" 1268 "Two,\n" 1269 "#else\n" 1270 "Three,\n" 1271 "#endif\n" 1272 "#endif\n" 1273 " Four\n" 1274 "};", 1275 format("enum H {\n" 1276 "One,\n" 1277 "#if 0\n" 1278 "#ifdef Q\n" 1279 "Two,\n" 1280 "#else\n" 1281 "Three,\n" 1282 "#endif\n" 1283 "#endif\n" 1284 "Four\n" 1285 "};")); 1286 EXPECT_EQ("enum I {\n" 1287 " One,\n" 1288 "#if /* test */ 0 || 1\n" 1289 "Two,\n" 1290 "Three,\n" 1291 "#endif\n" 1292 " Four\n" 1293 "};", 1294 format("enum I {\n" 1295 "One,\n" 1296 "#if /* test */ 0 || 1\n" 1297 "Two,\n" 1298 "Three,\n" 1299 "#endif\n" 1300 "Four\n" 1301 "};")); 1302 EXPECT_EQ("enum J {\n" 1303 " One,\n" 1304 "#if 0\n" 1305 "#if 0\n" 1306 "Two,\n" 1307 "#else\n" 1308 "Three,\n" 1309 "#endif\n" 1310 "Four,\n" 1311 "#endif\n" 1312 " Five\n" 1313 "};", 1314 format("enum J {\n" 1315 "One,\n" 1316 "#if 0\n" 1317 "#if 0\n" 1318 "Two,\n" 1319 "#else\n" 1320 "Three,\n" 1321 "#endif\n" 1322 "Four,\n" 1323 "#endif\n" 1324 "Five\n" 1325 "};")); 1326 1327} 1328 1329//===----------------------------------------------------------------------===// 1330// Tests for classes, namespaces, etc. 1331//===----------------------------------------------------------------------===// 1332 1333TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1334 verifyFormat("class A {};"); 1335} 1336 1337TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1338 verifyFormat("class A {\n" 1339 "public:\n" 1340 "public: // comment\n" 1341 "protected:\n" 1342 "private:\n" 1343 " void f() {}\n" 1344 "};"); 1345 verifyGoogleFormat("class A {\n" 1346 " public:\n" 1347 " protected:\n" 1348 " private:\n" 1349 " void f() {}\n" 1350 "};"); 1351} 1352 1353TEST_F(FormatTest, SeparatesLogicalBlocks) { 1354 EXPECT_EQ("class A {\n" 1355 "public:\n" 1356 " void f();\n" 1357 "\n" 1358 "private:\n" 1359 " void g() {}\n" 1360 " // test\n" 1361 "protected:\n" 1362 " int h;\n" 1363 "};", 1364 format("class A {\n" 1365 "public:\n" 1366 "void f();\n" 1367 "private:\n" 1368 "void g() {}\n" 1369 "// test\n" 1370 "protected:\n" 1371 "int h;\n" 1372 "};")); 1373} 1374 1375TEST_F(FormatTest, FormatsClasses) { 1376 verifyFormat("class A : public B {};"); 1377 verifyFormat("class A : public ::B {};"); 1378 1379 verifyFormat( 1380 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1381 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1382 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1383 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1384 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1385 verifyFormat( 1386 "class A : public B, public C, public D, public E, public F {};"); 1387 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1388 " public C,\n" 1389 " public D,\n" 1390 " public E,\n" 1391 " public F,\n" 1392 " public G {};"); 1393 1394 verifyFormat("class\n" 1395 " ReallyReallyLongClassName {\n};", 1396 getLLVMStyleWithColumns(32)); 1397} 1398 1399TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1400 verifyFormat("class A {\n} a, b;"); 1401 verifyFormat("struct A {\n} a, b;"); 1402 verifyFormat("union A {\n} a;"); 1403} 1404 1405TEST_F(FormatTest, FormatsEnum) { 1406 verifyFormat("enum {\n" 1407 " Zero,\n" 1408 " One = 1,\n" 1409 " Two = One + 1,\n" 1410 " Three = (One + Two),\n" 1411 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1412 " Five = (One, Two, Three, Four, 5)\n" 1413 "};"); 1414 verifyFormat("enum Enum {};"); 1415 verifyFormat("enum {};"); 1416 verifyFormat("enum X E {\n} d;"); 1417 verifyFormat("enum __attribute__((...)) E {\n} d;"); 1418 verifyFormat("enum __declspec__((...)) E {\n} d;"); 1419 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 1420} 1421 1422TEST_F(FormatTest, FormatsBitfields) { 1423 verifyFormat("struct Bitfields {\n" 1424 " unsigned sClass : 8;\n" 1425 " unsigned ValueKind : 2;\n" 1426 "};"); 1427} 1428 1429TEST_F(FormatTest, FormatsNamespaces) { 1430 verifyFormat("namespace some_namespace {\n" 1431 "class A {};\n" 1432 "void f() { f(); }\n" 1433 "}"); 1434 verifyFormat("namespace {\n" 1435 "class A {};\n" 1436 "void f() { f(); }\n" 1437 "}"); 1438 verifyFormat("inline namespace X {\n" 1439 "class A {};\n" 1440 "void f() { f(); }\n" 1441 "}"); 1442 verifyFormat("using namespace some_namespace;\n" 1443 "class A {};\n" 1444 "void f() { f(); }"); 1445 1446 // This code is more common than we thought; if we 1447 // layout this correctly the semicolon will go into 1448 // its own line, which is undesireable. 1449 verifyFormat("namespace {};"); 1450 verifyFormat("namespace {\n" 1451 "class A {};\n" 1452 "};"); 1453} 1454 1455TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1456 1457TEST_F(FormatTest, FormatsInlineASM) { 1458 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1459 verifyFormat( 1460 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1461 " \"cpuid\\n\\t\"\n" 1462 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1463 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1464 " : \"a\"(value));"); 1465} 1466 1467TEST_F(FormatTest, FormatTryCatch) { 1468 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll 1469 // also not create single-line-blocks. 1470 verifyFormat("try {\n" 1471 " throw a * b;\n" 1472 "}\n" 1473 "catch (int a) {\n" 1474 " // Do nothing.\n" 1475 "}\n" 1476 "catch (...) {\n" 1477 " exit(42);\n" 1478 "}"); 1479 1480 // Function-level try statements. 1481 verifyFormat("int f() try { return 4; }\n" 1482 "catch (...) {\n" 1483 " return 5;\n" 1484 "}"); 1485 verifyFormat("class A {\n" 1486 " int a;\n" 1487 " A() try : a(0) {}\n" 1488 " catch (...) {\n" 1489 " throw;\n" 1490 " }\n" 1491 "};\n"); 1492} 1493 1494TEST_F(FormatTest, FormatObjCTryCatch) { 1495 verifyFormat("@try {\n" 1496 " f();\n" 1497 "}\n" 1498 "@catch (NSException e) {\n" 1499 " @throw;\n" 1500 "}\n" 1501 "@finally {\n" 1502 " exit(42);\n" 1503 "}"); 1504} 1505 1506TEST_F(FormatTest, StaticInitializers) { 1507 verifyFormat("static SomeClass SC = { 1, 'a' };"); 1508 1509 verifyFormat( 1510 "static SomeClass WithALoooooooooooooooooooongName = {\n" 1511 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1512 "};"); 1513 1514 verifyFormat( 1515 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 1516 " looooooooooooooooooooooooooooooooooongname,\n" 1517 " looooooooooooooooooooooooooooooong };"); 1518 // Allow bin-packing in static initializers as this would often lead to 1519 // terrible results, e.g.: 1520 verifyGoogleFormat( 1521 "static SomeClass = {a, b, c, d, e, f, g, h, i, j,\n" 1522 " looooooooooooooooooooooooooooooooooongname,\n" 1523 " looooooooooooooooooooooooooooooong};"); 1524 // Here, everything other than the "}" would fit on a line. 1525 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1526 " 100000000000000000000000\n" 1527 "};"); 1528 1529 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1530 // line. However, the formatting looks a bit off and this probably doesn't 1531 // happen often in practice. 1532 verifyFormat("static int Variable[1] = {\n" 1533 " { 1000000000000000000000000000000000000 }\n" 1534 "};", 1535 getLLVMStyleWithColumns(40)); 1536} 1537 1538TEST_F(FormatTest, DesignatedInitializers) { 1539 verifyFormat("const struct A a = { .a = 1, .b = 2 };"); 1540 verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n" 1541 " .bbbbbbbbbb = 2,\n" 1542 " .cccccccccc = 3,\n" 1543 " .dddddddddd = 4,\n" 1544 " .eeeeeeeeee = 5 };"); 1545 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1546 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1547 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1548 " .ccccccccccccccccccccccccccc = 3,\n" 1549 " .ddddddddddddddddddddddddddd = 4,\n" 1550 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n" 1551 "};"); 1552 1553 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1554} 1555 1556TEST_F(FormatTest, NestedStaticInitializers) { 1557 verifyFormat("static A x = { { {} } };\n"); 1558 verifyFormat("static A x = { { { init1, init2, init3, init4 },\n" 1559 " { init1, init2, init3, init4 } } };"); 1560 1561 verifyFormat("somes Status::global_reps[3] = {\n" 1562 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 1563 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 1564 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 1565 "};"); 1566 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1567 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1568 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1569 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}\n" 1570 "};"); 1571 verifyFormat( 1572 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" 1573 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" 1574 " } };"); 1575 1576 verifyFormat( 1577 "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" 1578 " { 111111111111111111111111111111,\n" 1579 " 222222222222222222222222222222,\n" 1580 " 333333333333333333333333333333 },\n" 1581 " { 1, 2, 3 }, { 1, 2, 3 } } };"); 1582 verifyFormat( 1583 "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" 1584 " { { 111111111111111111111111111111,\n" 1585 " 222222222222222222222222222222,\n" 1586 " 333333333333333333333333333333 } },\n" 1587 " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); 1588 verifyGoogleFormat( 1589 "SomeArrayOfSomeType a = {{{1, 2, 3}}, {{1, 2, 3}},\n" 1590 " {{111111111111111111111111111111,\n" 1591 " 222222222222222222222222222222,\n" 1592 " 333333333333333333333333333333}},\n" 1593 " {{1, 2, 3}}, {{1, 2, 3}}};"); 1594 1595 // FIXME: We might at some point want to handle this similar to parameter 1596 // lists, where we have an option to put each on a single line. 1597 verifyFormat( 1598 "struct {\n" 1599 " unsigned bit;\n" 1600 " const char *const name;\n" 1601 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" 1602 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); 1603} 1604 1605TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1606 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1607 " \\\n" 1608 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1609} 1610 1611TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1612 verifyFormat( 1613 "virtual void write(ELFWriter *writerrr,\n" 1614 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1615} 1616 1617TEST_F(FormatTest, LayoutUnknownPPDirective) { 1618 EXPECT_EQ("#123 \"A string literal\"", 1619 format(" # 123 \"A string literal\"")); 1620 EXPECT_EQ("#;", format("#;")); 1621 verifyFormat("#\n;\n;\n;"); 1622} 1623 1624TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1625 EXPECT_EQ("#line 42 \"test\"\n", 1626 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1627 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1628 getLLVMStyleWithColumns(12))); 1629} 1630 1631TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1632 EXPECT_EQ("#line 42 \"test\"", 1633 format("# \\\n line \\\n 42 \\\n \"test\"")); 1634 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1635} 1636 1637TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1638 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1639 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1640 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1641 // FIXME: We never break before the macro name. 1642 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); 1643 1644 verifyFormat("#define A A\n#define A A"); 1645 verifyFormat("#define A(X) A\n#define A A"); 1646 1647 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1648 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1649} 1650 1651TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1652 EXPECT_EQ("// somecomment\n" 1653 "#include \"a.h\"\n" 1654 "#define A( \\\n" 1655 " A, B)\n" 1656 "#include \"b.h\"\n" 1657 "// somecomment\n", 1658 format(" // somecomment\n" 1659 " #include \"a.h\"\n" 1660 "#define A(A,\\\n" 1661 " B)\n" 1662 " #include \"b.h\"\n" 1663 " // somecomment\n", 1664 getLLVMStyleWithColumns(13))); 1665} 1666 1667TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1668 1669TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1670 EXPECT_EQ("#define A \\\n" 1671 " c; \\\n" 1672 " e;\n" 1673 "f;", 1674 format("#define A c; e;\n" 1675 "f;", 1676 getLLVMStyleWithColumns(14))); 1677} 1678 1679TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1680 1681TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) { 1682 EXPECT_EQ("int i;\n" 1683 "#define A \\\n" 1684 " int i; \\\n" 1685 " int j\n" 1686 "int k;", 1687 format("int i;\n" 1688 "#define A \\\n" 1689 " int i ; \\\n" 1690 " int j\n" 1691 "int k;", 1692 8, 0, getGoogleStyle())); // 8: position of "#define". 1693 EXPECT_EQ("int i;\n" 1694 "#define A \\\n" 1695 " int i; \\\n" 1696 " int j\n" 1697 "int k;", 1698 format("int i;\n" 1699 "#define A \\\n" 1700 " int i ; \\\n" 1701 " int j\n" 1702 "int k;", 1703 45, 0, getGoogleStyle())); // 45: position of "j". 1704} 1705 1706TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1707 EXPECT_EQ("int x,\n" 1708 "#define A\n" 1709 " y;", 1710 format("int x,\n#define A\ny;")); 1711} 1712 1713TEST_F(FormatTest, HashInMacroDefinition) { 1714 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 1715 verifyFormat("#define A \\\n" 1716 " { \\\n" 1717 " f(#c);\\\n" 1718 " }", 1719 getLLVMStyleWithColumns(11)); 1720 1721 verifyFormat("#define A(X) \\\n" 1722 " void function##X()", 1723 getLLVMStyleWithColumns(22)); 1724 1725 verifyFormat("#define A(a, b, c) \\\n" 1726 " void a##b##c()", 1727 getLLVMStyleWithColumns(22)); 1728 1729 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 1730} 1731 1732TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1733 verifyFormat("#define A (1)"); 1734} 1735 1736TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 1737 EXPECT_EQ("#define A b;", format("#define A \\\n" 1738 " \\\n" 1739 " b;", 1740 getLLVMStyleWithColumns(25))); 1741 EXPECT_EQ("#define A \\\n" 1742 " \\\n" 1743 " a; \\\n" 1744 " b;", 1745 format("#define A \\\n" 1746 " \\\n" 1747 " a; \\\n" 1748 " b;", 1749 getLLVMStyleWithColumns(11))); 1750 EXPECT_EQ("#define A \\\n" 1751 " a; \\\n" 1752 " \\\n" 1753 " b;", 1754 format("#define A \\\n" 1755 " a; \\\n" 1756 " \\\n" 1757 " b;", 1758 getLLVMStyleWithColumns(11))); 1759} 1760 1761TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 1762 verifyFormat("#define A :"); 1763 verifyFormat("#define SOMECASES \\\n" 1764 " case 1: \\\n" 1765 " case 2\n", 1766 getLLVMStyleWithColumns(20)); 1767 verifyFormat("#define A template <typename T>"); 1768 verifyFormat("#define STR(x) #x\n" 1769 "f(STR(this_is_a_string_literal{));"); 1770} 1771 1772TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 1773 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 1774 EXPECT_EQ("class A : public QObject {\n" 1775 " Q_OBJECT\n" 1776 "\n" 1777 " A() {}\n" 1778 "};", 1779 format("class A : public QObject {\n" 1780 " Q_OBJECT\n" 1781 "\n" 1782 " A() {\n}\n" 1783 "} ;")); 1784 EXPECT_EQ("SOME_MACRO\n" 1785 "namespace {\n" 1786 "void f();\n" 1787 "}", 1788 format("SOME_MACRO\n" 1789 " namespace {\n" 1790 "void f( );\n" 1791 "}")); 1792 // Only if the identifier contains at least 5 characters. 1793 EXPECT_EQ("HTTP f();", 1794 format("HTTP\nf();")); 1795 EXPECT_EQ("MACRO\nf();", 1796 format("MACRO\nf();")); 1797 // Only if everything is upper case. 1798 EXPECT_EQ("class A : public QObject {\n" 1799 " Q_Object A() {}\n" 1800 "};", 1801 format("class A : public QObject {\n" 1802 " Q_Object\n" 1803 "\n" 1804 " A() {\n}\n" 1805 "} ;")); 1806} 1807 1808TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 1809 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 1810 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 1811 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 1812 "class X {};\n" 1813 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 1814 "int *createScopDetectionPass() { return 0; }", 1815 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 1816 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 1817 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 1818 " class X {};\n" 1819 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 1820 " int *createScopDetectionPass() { return 0; }")); 1821 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 1822 // braces, so that inner block is indented one level more. 1823 EXPECT_EQ("int q() {\n" 1824 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 1825 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 1826 " IPC_END_MESSAGE_MAP()\n" 1827 "}", 1828 format("int q() {\n" 1829 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 1830 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 1831 " IPC_END_MESSAGE_MAP()\n" 1832 "}")); 1833 EXPECT_EQ("int q() {\n" 1834 " f(x);\n" 1835 " f(x) {}\n" 1836 " f(x)->g();\n" 1837 " f(x)->*g();\n" 1838 " f(x).g();\n" 1839 " f(x) = x;\n" 1840 " f(x) += x;\n" 1841 " f(x) -= x;\n" 1842 " f(x) *= x;\n" 1843 " f(x) /= x;\n" 1844 " f(x) %= x;\n" 1845 " f(x) &= x;\n" 1846 " f(x) |= x;\n" 1847 " f(x) ^= x;\n" 1848 " f(x) >>= x;\n" 1849 " f(x) <<= x;\n" 1850 " f(x)[y].z();\n" 1851 " LOG(INFO) << x;\n" 1852 " ifstream(x) >> x;\n" 1853 "}\n", 1854 format("int q() {\n" 1855 " f(x)\n;\n" 1856 " f(x)\n {}\n" 1857 " f(x)\n->g();\n" 1858 " f(x)\n->*g();\n" 1859 " f(x)\n.g();\n" 1860 " f(x)\n = x;\n" 1861 " f(x)\n += x;\n" 1862 " f(x)\n -= x;\n" 1863 " f(x)\n *= x;\n" 1864 " f(x)\n /= x;\n" 1865 " f(x)\n %= x;\n" 1866 " f(x)\n &= x;\n" 1867 " f(x)\n |= x;\n" 1868 " f(x)\n ^= x;\n" 1869 " f(x)\n >>= x;\n" 1870 " f(x)\n <<= x;\n" 1871 " f(x)\n[y].z();\n" 1872 " LOG(INFO)\n << x;\n" 1873 " ifstream(x)\n >> x;\n" 1874 "}\n")); 1875 EXPECT_EQ("int q() {\n" 1876 " f(x)\n" 1877 " if (1) {\n" 1878 " }\n" 1879 " f(x)\n" 1880 " while (1) {\n" 1881 " }\n" 1882 " f(x)\n" 1883 " g(x);\n" 1884 " f(x)\n" 1885 " try {\n" 1886 " q();\n" 1887 " }\n" 1888 " catch (...) {\n" 1889 " }\n" 1890 "}\n", 1891 format("int q() {\n" 1892 "f(x)\n" 1893 "if (1) {}\n" 1894 "f(x)\n" 1895 "while (1) {}\n" 1896 "f(x)\n" 1897 "g(x);\n" 1898 "f(x)\n" 1899 "try { q(); } catch (...) {}\n" 1900 "}\n")); 1901 EXPECT_EQ("class A {\n" 1902 " A() : t(0) {}\n" 1903 " A(X x)\n" // FIXME: function-level try blocks are broken. 1904 " try : t(0) {\n" 1905 " }\n" 1906 " catch (...) {\n" 1907 " }\n" 1908 "};", 1909 format("class A {\n" 1910 " A()\n : t(0) {}\n" 1911 " A(X x)\n" 1912 " try : t(0) {} catch (...) {}\n" 1913 "};")); 1914} 1915 1916TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 1917 verifyFormat("#define A \\\n" 1918 " f({ \\\n" 1919 " g(); \\\n" 1920 " });", getLLVMStyleWithColumns(11)); 1921} 1922 1923TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 1924 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 1925} 1926 1927TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 1928 verifyFormat("{\n { a #c; }\n}"); 1929} 1930 1931TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 1932 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 1933 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 1934 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 1935 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 1936} 1937 1938TEST_F(FormatTest, EscapedNewlineAtStartOfToken) { 1939 EXPECT_EQ( 1940 "#define A \\\n int i; \\\n int j;", 1941 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 1942 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 1943} 1944 1945TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 1946 verifyFormat("#define A \\\n" 1947 " int v( \\\n" 1948 " a); \\\n" 1949 " int i;", 1950 getLLVMStyleWithColumns(11)); 1951} 1952 1953TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 1954 EXPECT_EQ( 1955 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1956 " \\\n" 1957 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 1958 "\n" 1959 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 1960 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 1961 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1962 "\\\n" 1963 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 1964 " \n" 1965 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 1966 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 1967} 1968 1969TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 1970 EXPECT_EQ("int\n" 1971 "#define A\n" 1972 " a;", 1973 format("int\n#define A\na;")); 1974 verifyFormat("functionCallTo(\n" 1975 " someOtherFunction(\n" 1976 " withSomeParameters, whichInSequence,\n" 1977 " areLongerThanALine(andAnotherCall,\n" 1978 "#define A B\n" 1979 " withMoreParamters,\n" 1980 " whichStronglyInfluenceTheLayout),\n" 1981 " andMoreParameters),\n" 1982 " trailing);", 1983 getLLVMStyleWithColumns(69)); 1984} 1985 1986TEST_F(FormatTest, LayoutBlockInsideParens) { 1987 EXPECT_EQ("functionCall({\n" 1988 " int i;\n" 1989 "});", 1990 format(" functionCall ( {int i;} );")); 1991 1992 // FIXME: This is bad, find a better and more generic solution. 1993 EXPECT_EQ("functionCall({\n" 1994 " int i;\n" 1995 "},\n" 1996 " aaaa, bbbb, cccc);", 1997 format(" functionCall ( {int i;}, aaaa, bbbb, cccc);")); 1998 verifyFormat( 1999 "Aaa({\n" 2000 " int i;\n" 2001 "},\n" 2002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2003 " ccccccccccccccccc));"); 2004} 2005 2006TEST_F(FormatTest, LayoutBlockInsideStatement) { 2007 EXPECT_EQ("SOME_MACRO { int i; }\n" 2008 "int i;", 2009 format(" SOME_MACRO {int i;} int i;")); 2010} 2011 2012TEST_F(FormatTest, LayoutNestedBlocks) { 2013 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2014 " struct s {\n" 2015 " int i;\n" 2016 " };\n" 2017 " s kBitsToOs[] = { { 10 } };\n" 2018 " for (int i = 0; i < 10; ++i)\n" 2019 " return;\n" 2020 "}"); 2021} 2022 2023TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2024 EXPECT_EQ("{}", format("{}")); 2025 verifyFormat("enum E {};"); 2026 verifyFormat("enum E {}"); 2027} 2028 2029//===----------------------------------------------------------------------===// 2030// Line break tests. 2031//===----------------------------------------------------------------------===// 2032 2033TEST_F(FormatTest, FormatsAwesomeMethodCall) { 2034 verifyFormat( 2035 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2036 " parameter, parameter, parameter)),\n" 2037 " SecondLongCall(parameter));"); 2038} 2039 2040TEST_F(FormatTest, PreventConfusingIndents) { 2041 verifyFormat( 2042 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2043 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2045 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2046 verifyFormat( 2047 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" 2048 " aaaaaaaaaaaaaaaaaaaaaaaa[\n" 2049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" 2050 " aaaaaaaaaaaaaaaaaaaaaaaa];"); 2051 verifyFormat( 2052 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2053 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2055 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2056 verifyFormat("int a = bbbb && ccc && fffff(\n" 2057 "#define A Just forcing a new line\n" 2058 " ddd);"); 2059} 2060 2061TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2062 verifyFormat( 2063 "bool aaaaaaa =\n" 2064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2065 " bbbbbbbb();"); 2066 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2068 " ccccccccc == ddddddddddd;"); 2069 2070 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2071 " aaaaaa) &&\n" 2072 " bbbbbb && cccccc;"); 2073 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2074 " aaaaaa) >>\n" 2075 " bbbbbb;"); 2076 verifyFormat("Whitespaces.addUntouchableComment(\n" 2077 " SourceMgr.getSpellingColumnNumber(\n" 2078 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2079 " 1);"); 2080 2081 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2082 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2083 " cccccc) {\n}"); 2084 2085 FormatStyle OnePerLine = getLLVMStyle(); 2086 OnePerLine.BinPackParameters = false; 2087 verifyFormat( 2088 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2091 OnePerLine); 2092} 2093 2094TEST_F(FormatTest, ExpressionIndentation) { 2095 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2099 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2100 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2103 " ccccccccccccccccccccccccccccccccccccccccc;"); 2104 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2107 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2108 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2111 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2112 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2115 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2116 verifyFormat("if () {\n" 2117 "} else if (aaaaa && bbbbb > // break\n" 2118 " ccccc) {\n" 2119 "}"); 2120} 2121 2122TEST_F(FormatTest, ConstructorInitializers) { 2123 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2124 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2125 getLLVMStyleWithColumns(45)); 2126 verifyFormat("Constructor()\n" 2127 " : Inttializer(FitsOnTheLine) {}", 2128 getLLVMStyleWithColumns(44)); 2129 verifyFormat("Constructor()\n" 2130 " : Inttializer(FitsOnTheLine) {}", 2131 getLLVMStyleWithColumns(43)); 2132 2133 verifyFormat( 2134 "SomeClass::Constructor()\n" 2135 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2136 2137 verifyFormat( 2138 "SomeClass::Constructor()\n" 2139 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2140 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2141 verifyFormat( 2142 "SomeClass::Constructor()\n" 2143 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2144 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2145 2146 verifyFormat("Constructor()\n" 2147 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2148 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2149 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2150 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 2151 2152 verifyFormat("Constructor()\n" 2153 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2155 2156 verifyFormat("Constructor(int Parameter = 0)\n" 2157 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2158 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 2159 2160 // Here a line could be saved by splitting the second initializer onto two 2161 // lines, but that is not desireable. 2162 verifyFormat("Constructor()\n" 2163 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 2164 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 2165 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2166 2167 FormatStyle OnePerLine = getLLVMStyle(); 2168 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2169 verifyFormat("SomeClass::Constructor()\n" 2170 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2171 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2173 OnePerLine); 2174 verifyFormat("SomeClass::Constructor()\n" 2175 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 2176 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2177 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2178 OnePerLine); 2179 verifyFormat("MyClass::MyClass(int var)\n" 2180 " : some_var_(var), // 4 space indent\n" 2181 " some_other_var_(var + 1) { // lined up\n" 2182 "}", 2183 OnePerLine); 2184 verifyFormat("Constructor()\n" 2185 " : aaaaa(aaaaaa),\n" 2186 " aaaaa(aaaaaa),\n" 2187 " aaaaa(aaaaaa),\n" 2188 " aaaaa(aaaaaa),\n" 2189 " aaaaa(aaaaaa) {}", 2190 OnePerLine); 2191 verifyFormat("Constructor()\n" 2192 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 2193 " aaaaaaaaaaaaaaaaaaaaaa) {}", 2194 OnePerLine); 2195} 2196 2197TEST_F(FormatTest, MemoizationTests) { 2198 // This breaks if the memoization lookup does not take \c Indent and 2199 // \c LastSpace into account. 2200 verifyFormat( 2201 "extern CFRunLoopTimerRef\n" 2202 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 2203 " CFTimeInterval interval, CFOptionFlags flags,\n" 2204 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 2205 " CFRunLoopTimerContext *context) {}"); 2206 2207 // Deep nesting somewhat works around our memoization. 2208 verifyFormat( 2209 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2210 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2211 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2212 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2213 " aaaaa())))))))))))))))))))))))))))))))))))))));", 2214 getLLVMStyleWithColumns(65)); 2215 verifyFormat( 2216 "aaaaa(\n" 2217 " aaaaa,\n" 2218 " aaaaa(\n" 2219 " aaaaa,\n" 2220 " aaaaa(\n" 2221 " aaaaa,\n" 2222 " aaaaa(\n" 2223 " aaaaa,\n" 2224 " aaaaa(\n" 2225 " aaaaa,\n" 2226 " aaaaa(\n" 2227 " aaaaa,\n" 2228 " aaaaa(\n" 2229 " aaaaa,\n" 2230 " aaaaa(\n" 2231 " aaaaa,\n" 2232 " aaaaa(\n" 2233 " aaaaa,\n" 2234 " aaaaa(\n" 2235 " aaaaa,\n" 2236 " aaaaa(\n" 2237 " aaaaa,\n" 2238 " aaaaa(\n" 2239 " aaaaa,\n" 2240 " aaaaa))))))))))));", 2241 getLLVMStyleWithColumns(65)); 2242 verifyFormat( 2243 "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" 2244 " a),\n" 2245 " a),\n" 2246 " a),\n" 2247 " a),\n" 2248 " a),\n" 2249 " a),\n" 2250 " a),\n" 2251 " a),\n" 2252 " a),\n" 2253 " a),\n" 2254 " a),\n" 2255 " a),\n" 2256 " a),\n" 2257 " a),\n" 2258 " a),\n" 2259 " a),\n" 2260 " a)", 2261 getLLVMStyleWithColumns(65)); 2262 2263 // This test takes VERY long when memoization is broken. 2264 FormatStyle OnePerLine = getLLVMStyle(); 2265 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2266 OnePerLine.BinPackParameters = false; 2267 std::string input = "Constructor()\n" 2268 " : aaaa(a,\n"; 2269 for (unsigned i = 0, e = 80; i != e; ++i) { 2270 input += " a,\n"; 2271 } 2272 input += " a) {}"; 2273 verifyFormat(input, OnePerLine); 2274} 2275 2276TEST_F(FormatTest, BreaksAsHighAsPossible) { 2277 verifyFormat( 2278 "void f() {\n" 2279 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 2280 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 2281 " f();\n" 2282 "}"); 2283 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 2284 " Intervals[i - 1].getRange().getLast()) {\n}"); 2285} 2286 2287TEST_F(FormatTest, BreaksFunctionDeclarations) { 2288 // Principially, we break function declarations in a certain order: 2289 // 1) break amongst arguments. 2290 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 2291 " Cccccccccccccc cccccccccccccc);"); 2292 2293 // 2) break after return type. 2294 verifyFormat( 2295 "Aaaaaaaaaaaaaaaaaaaaaaaa\n" 2296 " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); 2297 2298 // 3) break after (. 2299 verifyFormat( 2300 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 2301 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); 2302 2303 // 4) break before after nested name specifiers. 2304 verifyFormat( 2305 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2306 " SomeClasssssssssssssssssssssssssssssssssssssss::\n" 2307 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); 2308 2309 // However, there are exceptions, if a sufficient amount of lines can be 2310 // saved. 2311 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 2312 // more adjusting. 2313 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 2314 " Cccccccccccccc cccccccccc,\n" 2315 " Cccccccccccccc cccccccccc,\n" 2316 " Cccccccccccccc cccccccccc,\n" 2317 " Cccccccccccccc cccccccccc);"); 2318 verifyFormat( 2319 "Aaaaaaaaaaaaaaaaaa\n" 2320 " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2321 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2322 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 2323 verifyFormat( 2324 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 2325 " Cccccccccccccc cccccccccc,\n" 2326 " Cccccccccccccc cccccccccc,\n" 2327 " Cccccccccccccc cccccccccc,\n" 2328 " Cccccccccccccc cccccccccc,\n" 2329 " Cccccccccccccc cccccccccc,\n" 2330 " Cccccccccccccc cccccccccc);"); 2331 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 2332 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2333 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2334 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2335 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 2336 2337 // Break after multi-line parameters. 2338 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2341 " bbbb bbbb);"); 2342} 2343 2344TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 2345 verifyFormat("void someLongFunction(\n" 2346 " int someLongParameter) const {}", 2347 getLLVMStyleWithColumns(46)); 2348 FormatStyle Style = getGoogleStyle(); 2349 Style.ColumnLimit = 47; 2350 verifyFormat("void\n" 2351 "someLongFunction(int someLongParameter) const {\n}", 2352 getLLVMStyleWithColumns(47)); 2353 verifyFormat("void someLongFunction(\n" 2354 " int someLongParameter) const {}", 2355 Style); 2356 verifyFormat("LoooooongReturnType\n" 2357 "someLoooooooongFunction() const {}", 2358 getLLVMStyleWithColumns(47)); 2359 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 2360 " const {}", 2361 Style); 2362 2363 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2364 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 2365 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 2366 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 2367 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 2368 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 2369 2370 verifyFormat( 2371 "void aaaaaaaaaaaaaaaaaa()\n" 2372 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 2373 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 2374 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2375 " __attribute__((unused));"); 2376 verifyFormat( 2377 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2378 " GUARDED_BY(aaaaaaaaaaaa);"); 2379} 2380 2381 2382TEST_F(FormatTest, BreaksDesireably) { 2383 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 2384 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 2385 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 2386 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2388 "}"); 2389 2390 verifyFormat( 2391 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2393 2394 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2397 2398 verifyFormat( 2399 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2400 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 2401 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 2403 2404 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2405 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2406 2407 verifyFormat( 2408 "void f() {\n" 2409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 2410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2411 "}"); 2412 verifyFormat( 2413 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2415 verifyFormat( 2416 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2418 verifyFormat( 2419 "aaaaaaaaaaaaaaaaa(\n" 2420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2422 2423 // This test case breaks on an incorrect memoization, i.e. an optimization not 2424 // taking into account the StopAt value. 2425 verifyFormat( 2426 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 2427 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 2428 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 2429 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2430 2431 verifyFormat("{\n {\n {\n" 2432 " Annotation.SpaceRequiredBefore =\n" 2433 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 2434 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 2435 " }\n }\n}"); 2436} 2437 2438TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 2439 FormatStyle NoBinPacking = getGoogleStyle(); 2440 NoBinPacking.BinPackParameters = false; 2441 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 2442 " aaaaaaaaaaaaaaaaaaaa,\n" 2443 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 2444 NoBinPacking); 2445 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 2446 " aaaaaaaaaaaaa,\n" 2447 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 2448 NoBinPacking); 2449 verifyFormat( 2450 "aaaaaaaa(aaaaaaaaaaaaa,\n" 2451 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 2453 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 2455 NoBinPacking); 2456 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 2457 " .aaaaaaaaaaaaaaaaaa();", 2458 NoBinPacking); 2459 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2460 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", 2461 NoBinPacking); 2462 2463 verifyFormat( 2464 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2465 " aaaaaaaaaaaa,\n" 2466 " aaaaaaaaaaaa);", 2467 NoBinPacking); 2468 verifyFormat( 2469 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 2470 " ddddddddddddddddddddddddddddd),\n" 2471 " test);", 2472 NoBinPacking); 2473 2474 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 2475 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 2476 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 2477 NoBinPacking); 2478 verifyFormat("a(\"a\"\n" 2479 " \"a\",\n" 2480 " a);"); 2481 2482 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 2483 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 2484 " aaaaaaaaa,\n" 2485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2486 NoBinPacking); 2487 verifyFormat( 2488 "void f() {\n" 2489 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 2490 " .aaaaaaa();\n" 2491 "}", 2492 NoBinPacking); 2493 verifyFormat( 2494 "template <class SomeType, class SomeOtherType>\n" 2495 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 2496 NoBinPacking); 2497} 2498 2499TEST_F(FormatTest, FormatsBuilderPattern) { 2500 verifyFormat( 2501 "return llvm::StringSwitch<Reference::Kind>(name)\n" 2502 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 2503 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" 2504 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" 2505 " .Default(ORDER_TEXT);\n"); 2506 2507 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 2508 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 2509 verifyFormat( 2510 "aaaaaaa->aaaaaaa\n" 2511 " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2512 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 2513 verifyFormat( 2514 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 2515 " aaaaaaaaaaaaaa);"); 2516 verifyFormat( 2517 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" 2518 " ->aaaaaaaaaaaaaaaa(\n" 2519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2520 " ->aaaaaaaaaaaaaaaaa();"); 2521} 2522 2523TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 2524 verifyFormat( 2525 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2526 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 2527 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 2528 " ccccccccccccccccccccccccc) {\n}"); 2529 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 2530 " ccccccccccccccccccccccccc) {\n}"); 2531 verifyFormat( 2532 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 2533 " ccccccccccccccccccccccccc) {\n}"); 2534 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 2535 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 2536 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 2537 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 2538 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 2539 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 2540 " aaaaaaaaaaaaaaa != aa) {\n}"); 2541} 2542 2543TEST_F(FormatTest, BreaksAfterAssignments) { 2544 verifyFormat( 2545 "unsigned Cost =\n" 2546 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 2547 " SI->getPointerAddressSpaceee());\n"); 2548 verifyFormat( 2549 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 2550 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 2551 2552 verifyFormat( 2553 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 2554 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 2555 verifyFormat("unsigned OriginalStartColumn =\n" 2556 " SourceMgr.getSpellingColumnNumber(\n" 2557 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 2558 " 1;"); 2559} 2560 2561TEST_F(FormatTest, AlignsAfterAssignments) { 2562 verifyFormat( 2563 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2564 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2565 verifyFormat( 2566 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2567 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2568 verifyFormat( 2569 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2570 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2571 verifyFormat( 2572 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2573 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 2574 verifyFormat("double LooooooooooooooooooooooooongResult =\n" 2575 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 2576 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 2577} 2578 2579TEST_F(FormatTest, AlignsAfterReturn) { 2580 verifyFormat( 2581 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2582 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2583 verifyFormat( 2584 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2585 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 2586 verifyFormat( 2587 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 2588 " aaaaaaaaaaaaaaaaaaaaaa();"); 2589 verifyFormat( 2590 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 2591 " aaaaaaaaaaaaaaaaaaaaaa());"); 2592 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2594 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 2596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2597} 2598 2599TEST_F(FormatTest, BreaksConditionalExpressions) { 2600 verifyFormat( 2601 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 2602 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2603 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2604 verifyFormat( 2605 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2606 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2607 verifyFormat( 2608 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 2609 " : aaaaaaaaaaaaa);"); 2610 verifyFormat( 2611 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2612 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2613 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2614 " aaaaaaaaaaaaa);"); 2615 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2616 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2618 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2620 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2622 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2624 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2626 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2627 2628 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2629 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2630 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2631 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 2632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2633 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2634 " : aaaaaaaaaaaaaaaa;"); 2635 verifyFormat( 2636 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2637 " ? aaaaaaaaaaaaaaa\n" 2638 " : aaaaaaaaaaaaaaa;"); 2639 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 2640 " aaaaaaaaa\n" 2641 " ? b\n" 2642 " : c);"); 2643 verifyFormat( 2644 "unsigned Indent =\n" 2645 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 2646 " ? IndentForLevel[TheLine.Level]\n" 2647 " : TheLine * 2,\n" 2648 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 2649 getLLVMStyleWithColumns(70)); 2650 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 2651 " ? aaaaaaaaaaaaaaa\n" 2652 " : bbbbbbbbbbbbbbb //\n" 2653 " ? ccccccccccccccc\n" 2654 " : ddddddddddddddd;"); 2655 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 2656 " ? aaaaaaaaaaaaaaa\n" 2657 " : (bbbbbbbbbbbbbbb //\n" 2658 " ? ccccccccccccccc\n" 2659 " : ddddddddddddddd);"); 2660 2661 FormatStyle NoBinPacking = getLLVMStyle(); 2662 NoBinPacking.BinPackParameters = false; 2663 verifyFormat( 2664 "void f() {\n" 2665 " g(aaa,\n" 2666 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 2667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2668 " ? aaaaaaaaaaaaaaa\n" 2669 " : aaaaaaaaaaaaaaa);\n" 2670 "}", 2671 NoBinPacking); 2672} 2673 2674TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 2675 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 2676 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 2677 verifyFormat("bool a = true, b = false;"); 2678 2679 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 2681 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 2682 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 2683 verifyFormat( 2684 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2685 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 2686 " d = e && f;"); 2687 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 2688 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 2689 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 2690 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 2691 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 2692 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 2693 // FIXME: If multiple variables are defined, the "*" needs to move to the new 2694 // line. Also fix indent for breaking after the type, this looks bad. 2695 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 2697 " *b = bbbbbbbbbbbbbbbbbbb;"); 2698 2699 // Not ideal, but pointer-with-type does not allow much here. 2700 verifyGoogleFormat( 2701 "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 2702 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;"); 2703} 2704 2705TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 2706 verifyFormat("arr[foo ? bar : baz];"); 2707 verifyFormat("f()[foo ? bar : baz];"); 2708 verifyFormat("(a + b)[foo ? bar : baz];"); 2709 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 2710} 2711 2712TEST_F(FormatTest, AlignsStringLiterals) { 2713 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 2714 " \"short literal\");"); 2715 verifyFormat( 2716 "looooooooooooooooooooooooongFunction(\n" 2717 " \"short literal\"\n" 2718 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 2719 verifyFormat("someFunction(\"Always break between multi-line\"\n" 2720 " \" string literals\",\n" 2721 " and, other, parameters);"); 2722 EXPECT_EQ("fun + \"1243\" /* comment */\n" 2723 " \"5678\";", 2724 format("fun + \"1243\" /* comment */\n" 2725 " \"5678\";", 2726 getLLVMStyleWithColumns(28))); 2727 EXPECT_EQ( 2728 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 2729 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 2730 " \"aaaaaaaaaaaaaaaa\";", 2731 format("aaaaaa =" 2732 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 2733 "aaaaaaaaaaaaaaaaaaaaa\" " 2734 "\"aaaaaaaaaaaaaaaa\";")); 2735 verifyFormat("a = a + \"a\"\n" 2736 " \"a\"\n" 2737 " \"a\";"); 2738 verifyFormat("f(\"a\", \"b\"\n" 2739 " \"c\");"); 2740 2741 verifyFormat( 2742 "#define LL_FORMAT \"ll\"\n" 2743 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 2744 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 2745 2746 verifyFormat("#define A(X) \\\n" 2747 " \"aaaaa\" #X \"bbbbbb\" \\\n" 2748 " \"ccccc\"", 2749 getLLVMStyleWithColumns(23)); 2750 verifyFormat("#define A \"def\"\n" 2751 "f(\"abc\" A \"ghi\"\n" 2752 " \"jkl\");"); 2753} 2754 2755TEST_F(FormatTest, AlignsPipes) { 2756 verifyFormat( 2757 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2758 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2759 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2760 verifyFormat( 2761 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 2762 " << aaaaaaaaaaaaaaaaaaaa;"); 2763 verifyFormat( 2764 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2765 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2766 verifyFormat( 2767 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 2768 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 2769 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 2770 verifyFormat( 2771 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2772 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2773 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2774 2775 verifyFormat("return out << \"somepacket = {\\n\"\n" 2776 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 2777 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 2778 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 2779 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 2780 " << \"}\";"); 2781 2782 verifyFormat( 2783 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 2784 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 2785 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 2786 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 2787 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 2788 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 2789 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 2790 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaa: \"\n" 2791 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2792 2793 verifyFormat( 2794 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2795 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 2796} 2797 2798TEST_F(FormatTest, UnderstandsEquals) { 2799 verifyFormat( 2800 "aaaaaaaaaaaaaaaaa =\n" 2801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2802 verifyFormat( 2803 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2804 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2805 verifyFormat( 2806 "if (a) {\n" 2807 " f();\n" 2808 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2810 "}"); 2811 2812 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2813 " 100000000 + 10000000) {\n}"); 2814} 2815 2816TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 2817 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 2818 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 2819 2820 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 2821 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 2822 2823 verifyFormat( 2824 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 2825 " Parameter2);"); 2826 2827 verifyFormat( 2828 "ShortObject->shortFunction(\n" 2829 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 2830 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 2831 2832 verifyFormat("loooooooooooooongFunction(\n" 2833 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 2834 2835 verifyFormat( 2836 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 2837 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 2838 2839 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 2840 " .WillRepeatedly(Return(SomeValue));"); 2841 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" 2842 " .insert(ccccccccccccccccccccccc);"); 2843 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2844 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n" 2845 " aaaaaaaaaaaaaaaaaaaaa);"); 2846 verifyFormat( 2847 "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2849 " .aaaaaaaaaaaaaaa(\n" 2850 " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2851 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2852 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2853 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2854 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2855 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 2856 "}"); 2857 2858 // Here, it is not necessary to wrap at "." or "->". 2859 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 2860 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2861 verifyFormat( 2862 "aaaaaaaaaaa->aaaaaaaaa(\n" 2863 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2864 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 2865 2866 verifyFormat( 2867 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2868 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 2869 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 2870 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 2871 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 2872 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 2873 2874 // FIXME: Should we break before .a()? 2875 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); 2877 2878 FormatStyle NoBinPacking = getLLVMStyle(); 2879 NoBinPacking.BinPackParameters = false; 2880 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 2881 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 2882 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 2883 " aaaaaaaaaaaaaaaaaaa,\n" 2884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2885 NoBinPacking); 2886} 2887 2888TEST_F(FormatTest, WrapsTemplateDeclarations) { 2889 verifyFormat("template <typename T>\n" 2890 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 2891 verifyFormat( 2892 "template <typename T>\n" 2893 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 2894 verifyFormat("template <typename T>\n" 2895 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 2896 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 2897 verifyFormat( 2898 "template <typename T>\n" 2899 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 2900 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 2901 verifyFormat( 2902 "template <typename T>\n" 2903 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 2904 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 2905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2906 verifyFormat("template <typename T>\n" 2907 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2908 " int aaaaaaaaaaaaaaaaaaaaaa);"); 2909 verifyFormat( 2910 "template <typename T1, typename T2 = char, typename T3 = char,\n" 2911 " typename T4 = char>\n" 2912 "void f();"); 2913 verifyFormat( 2914 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 2915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2916 2917 verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 2918 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 2919 2920 verifyFormat("template <typename T> class C {};"); 2921 verifyFormat("template <typename T> void f();"); 2922 verifyFormat("template <typename T> void f() {}"); 2923 2924 FormatStyle AlwaysBreak = getLLVMStyle(); 2925 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 2926 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 2927 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 2928 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 2929 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2930 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 2931 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 2932} 2933 2934TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 2935 verifyFormat( 2936 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 2938 verifyFormat( 2939 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 2942 2943 // FIXME: Should we have the extra indent after the second break? 2944 verifyFormat( 2945 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 2948 2949 verifyFormat( 2950 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 2951 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 2952 2953 // Breaking at nested name specifiers is generally not desirable. 2954 verifyFormat( 2955 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2956 " aaaaaaaaaaaaaaaaaaaaaaa);"); 2957 2958 verifyFormat( 2959 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2961 " aaaaaaaaaaaaaaaaaaaaa);", 2962 getLLVMStyleWithColumns(74)); 2963 2964 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2966 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 2967} 2968 2969TEST_F(FormatTest, UnderstandsTemplateParameters) { 2970 verifyFormat("A<int> a;"); 2971 verifyFormat("A<A<A<int> > > a;"); 2972 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 2973 verifyFormat("bool x = a < 1 || 2 > a;"); 2974 verifyFormat("bool x = 5 < f<int>();"); 2975 verifyFormat("bool x = f<int>() > 5;"); 2976 verifyFormat("bool x = 5 < a<int>::x;"); 2977 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 2978 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 2979 2980 verifyGoogleFormat("A<A<int>> a;"); 2981 verifyGoogleFormat("A<A<A<int>>> a;"); 2982 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 2983 verifyGoogleFormat("A<A<int> > a;"); 2984 verifyGoogleFormat("A<A<A<int> > > a;"); 2985 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 2986 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 2987 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 2988 2989 verifyFormat("test >> a >> b;"); 2990 verifyFormat("test << a >> b;"); 2991 2992 verifyFormat("f<int>();"); 2993 verifyFormat("template <typename T> void f() {}"); 2994 2995 // Not template parameters. 2996 verifyFormat("return a < b && c > d;"); 2997 verifyFormat("void f() {\n" 2998 " while (a < b && c > d) {\n" 2999 " }\n" 3000 "}"); 3001} 3002 3003TEST_F(FormatTest, UnderstandsBinaryOperators) { 3004 verifyFormat("COMPARE(a, ==, b);"); 3005} 3006 3007TEST_F(FormatTest, UnderstandsPointersToMembers) { 3008 verifyFormat("int A::*x;"); 3009 verifyFormat("int (S::*func)(void *);"); 3010 verifyFormat("void f() { int (S::*func)(void *); }"); 3011 verifyFormat("typedef bool *(Class::*Member)() const;"); 3012 verifyFormat("void f() {\n" 3013 " (a->*f)();\n" 3014 " a->*x;\n" 3015 " (a.*f)();\n" 3016 " ((*a).*f)();\n" 3017 " a.*x;\n" 3018 "}"); 3019 FormatStyle Style = getLLVMStyle(); 3020 Style.PointerBindsToType = true; 3021 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 3022} 3023 3024TEST_F(FormatTest, UnderstandsUnaryOperators) { 3025 verifyFormat("int a = -2;"); 3026 verifyFormat("f(-1, -2, -3);"); 3027 verifyFormat("a[-1] = 5;"); 3028 verifyFormat("int a = 5 + -2;"); 3029 verifyFormat("if (i == -1) {\n}"); 3030 verifyFormat("if (i != -1) {\n}"); 3031 verifyFormat("if (i > -1) {\n}"); 3032 verifyFormat("if (i < -1) {\n}"); 3033 verifyFormat("++(a->f());"); 3034 verifyFormat("--(a->f());"); 3035 verifyFormat("(a->f())++;"); 3036 verifyFormat("a[42]++;"); 3037 verifyFormat("if (!(a->f())) {\n}"); 3038 3039 verifyFormat("a-- > b;"); 3040 verifyFormat("b ? -a : c;"); 3041 verifyFormat("n * sizeof char16;"); 3042 verifyFormat("n * alignof char16;"); 3043 verifyFormat("sizeof(char);"); 3044 verifyFormat("alignof(char);"); 3045 3046 verifyFormat("return -1;"); 3047 verifyFormat("switch (a) {\n" 3048 "case -1:\n" 3049 " break;\n" 3050 "}"); 3051 verifyFormat("#define X -1"); 3052 verifyFormat("#define X -kConstant"); 3053 3054 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 3055 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 3056 3057 verifyFormat("int a = /* confusing comment */ -1;"); 3058 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 3059 verifyFormat("int a = i /* confusing comment */++;"); 3060} 3061 3062TEST_F(FormatTest, UndestandsOverloadedOperators) { 3063 verifyFormat("bool operator<();"); 3064 verifyFormat("bool operator>();"); 3065 verifyFormat("bool operator=();"); 3066 verifyFormat("bool operator==();"); 3067 verifyFormat("bool operator!=();"); 3068 verifyFormat("int operator+();"); 3069 verifyFormat("int operator++();"); 3070 verifyFormat("bool operator();"); 3071 verifyFormat("bool operator()();"); 3072 verifyFormat("bool operator[]();"); 3073 verifyFormat("operator bool();"); 3074 verifyFormat("operator int();"); 3075 verifyFormat("operator void *();"); 3076 verifyFormat("operator SomeType<int>();"); 3077 verifyFormat("operator SomeType<int, int>();"); 3078 verifyFormat("operator SomeType<SomeType<int> >();"); 3079 verifyFormat("void *operator new(std::size_t size);"); 3080 verifyFormat("void *operator new[](std::size_t size);"); 3081 verifyFormat("void operator delete(void *ptr);"); 3082 verifyFormat("void operator delete[](void *ptr);"); 3083 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 3084 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 3085 3086 verifyFormat( 3087 "ostream &operator<<(ostream &OutputStream,\n" 3088 " SomeReallyLongType WithSomeReallyLongValue);"); 3089 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 3090 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 3091 " return left.group < right.group;\n" 3092 "}"); 3093 verifyFormat("SomeType &operator=(const SomeType &S);"); 3094 3095 verifyGoogleFormat("operator void*();"); 3096 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 3097} 3098 3099TEST_F(FormatTest, UnderstandsNewAndDelete) { 3100 verifyFormat("void f() {\n" 3101 " A *a = new A;\n" 3102 " A *a = new (placement) A;\n" 3103 " delete a;\n" 3104 " delete (A *)a;\n" 3105 "}"); 3106} 3107 3108TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 3109 verifyFormat("int *f(int *a) {}"); 3110 verifyFormat("int main(int argc, char **argv) {}"); 3111 verifyFormat("Test::Test(int b) : a(b * b) {}"); 3112 verifyIndependentOfContext("f(a, *a);"); 3113 verifyFormat("void g() { f(*a); }"); 3114 verifyIndependentOfContext("int a = b * 10;"); 3115 verifyIndependentOfContext("int a = 10 * b;"); 3116 verifyIndependentOfContext("int a = b * c;"); 3117 verifyIndependentOfContext("int a += b * c;"); 3118 verifyIndependentOfContext("int a -= b * c;"); 3119 verifyIndependentOfContext("int a *= b * c;"); 3120 verifyIndependentOfContext("int a /= b * c;"); 3121 verifyIndependentOfContext("int a = *b;"); 3122 verifyIndependentOfContext("int a = *b * c;"); 3123 verifyIndependentOfContext("int a = b * *c;"); 3124 verifyIndependentOfContext("return 10 * b;"); 3125 verifyIndependentOfContext("return *b * *c;"); 3126 verifyIndependentOfContext("return a & ~b;"); 3127 verifyIndependentOfContext("f(b ? *c : *d);"); 3128 verifyIndependentOfContext("int a = b ? *c : *d;"); 3129 verifyIndependentOfContext("*b = a;"); 3130 verifyIndependentOfContext("a * ~b;"); 3131 verifyIndependentOfContext("a * !b;"); 3132 verifyIndependentOfContext("a * +b;"); 3133 verifyIndependentOfContext("a * -b;"); 3134 verifyIndependentOfContext("a * ++b;"); 3135 verifyIndependentOfContext("a * --b;"); 3136 verifyIndependentOfContext("a[4] * b;"); 3137 verifyIndependentOfContext("a[a * a] = 1;"); 3138 verifyIndependentOfContext("f() * b;"); 3139 verifyIndependentOfContext("a * [self dostuff];"); 3140 verifyIndependentOfContext("int x = a * (a + b);"); 3141 verifyIndependentOfContext("(a *)(a + b);"); 3142 verifyIndependentOfContext("int *pa = (int *)&a;"); 3143 verifyIndependentOfContext("return sizeof(int **);"); 3144 verifyIndependentOfContext("return sizeof(int ******);"); 3145 verifyIndependentOfContext("return (int **&)a;"); 3146 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 3147 verifyFormat("void f(Type (*parameter)[10]) {}"); 3148 verifyGoogleFormat("return sizeof(int**);"); 3149 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 3150 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 3151 verifyFormat("auto a = [](int **&, int ***) {};"); 3152 3153 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 3154 3155 verifyIndependentOfContext("A<int *> a;"); 3156 verifyIndependentOfContext("A<int **> a;"); 3157 verifyIndependentOfContext("A<int *, int *> a;"); 3158 verifyIndependentOfContext( 3159 "const char *const p = reinterpret_cast<const char *const>(q);"); 3160 verifyIndependentOfContext("A<int **, int **> a;"); 3161 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 3162 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 3163 verifyFormat("for (; a && b;) {\n}"); 3164 3165 verifyFormat( 3166 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3168 3169 verifyGoogleFormat("int main(int argc, char** argv) {}"); 3170 verifyGoogleFormat("A<int*> a;"); 3171 verifyGoogleFormat("A<int**> a;"); 3172 verifyGoogleFormat("A<int*, int*> a;"); 3173 verifyGoogleFormat("A<int**, int**> a;"); 3174 verifyGoogleFormat("f(b ? *c : *d);"); 3175 verifyGoogleFormat("int a = b ? *c : *d;"); 3176 verifyGoogleFormat("Type* t = **x;"); 3177 verifyGoogleFormat("Type* t = *++*x;"); 3178 verifyGoogleFormat("*++*x;"); 3179 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 3180 verifyGoogleFormat("Type* t = x++ * y;"); 3181 verifyGoogleFormat( 3182 "const char* const p = reinterpret_cast<const char* const>(q);"); 3183 3184 verifyIndependentOfContext("a = *(x + y);"); 3185 verifyIndependentOfContext("a = &(x + y);"); 3186 verifyIndependentOfContext("*(x + y).call();"); 3187 verifyIndependentOfContext("&(x + y)->call();"); 3188 verifyFormat("void f() { &(*I).first; }"); 3189 3190 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 3191 verifyFormat( 3192 "int *MyValues = {\n" 3193 " *A, // Operator detection might be confused by the '{'\n" 3194 " *BB // Operator detection might be confused by previous comment\n" 3195 "};"); 3196 3197 verifyIndependentOfContext("if (int *a = &b)"); 3198 verifyIndependentOfContext("if (int &a = *b)"); 3199 verifyIndependentOfContext("if (a & b[i])"); 3200 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 3201 verifyIndependentOfContext("if (*b[i])"); 3202 verifyIndependentOfContext("if (int *a = (&b))"); 3203 verifyIndependentOfContext("while (int *a = &b)"); 3204 verifyFormat("void f() {\n" 3205 " for (const int &v : Values) {\n" 3206 " }\n" 3207 "}"); 3208 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 3209 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 3210 3211 verifyFormat("#define MACRO \\\n" 3212 " int *i = a * b; \\\n" 3213 " void f(a *b);", 3214 getLLVMStyleWithColumns(19)); 3215 3216 verifyIndependentOfContext("A = new SomeType *[Length];"); 3217 verifyIndependentOfContext("A = new SomeType *[Length]();"); 3218 verifyGoogleFormat("A = new SomeType* [Length]();"); 3219 verifyGoogleFormat("A = new SomeType* [Length];"); 3220 3221 FormatStyle PointerLeft = getLLVMStyle(); 3222 PointerLeft.PointerBindsToType = true; 3223 verifyFormat("delete *x;", PointerLeft); 3224} 3225 3226TEST_F(FormatTest, UnderstandsEllipsis) { 3227 verifyFormat("int printf(const char *fmt, ...);"); 3228 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 3229} 3230 3231TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 3232 EXPECT_EQ("int *a;\n" 3233 "int *a;\n" 3234 "int *a;", 3235 format("int *a;\n" 3236 "int* a;\n" 3237 "int *a;", 3238 getGoogleStyle())); 3239 EXPECT_EQ("int* a;\n" 3240 "int* a;\n" 3241 "int* a;", 3242 format("int* a;\n" 3243 "int* a;\n" 3244 "int *a;", 3245 getGoogleStyle())); 3246 EXPECT_EQ("int *a;\n" 3247 "int *a;\n" 3248 "int *a;", 3249 format("int *a;\n" 3250 "int * a;\n" 3251 "int * a;", 3252 getGoogleStyle())); 3253} 3254 3255TEST_F(FormatTest, UnderstandsRvalueReferences) { 3256 verifyFormat("int f(int &&a) {}"); 3257 verifyFormat("int f(int a, char &&b) {}"); 3258 verifyFormat("void f() { int &&a = b; }"); 3259 verifyGoogleFormat("int f(int a, char&& b) {}"); 3260 verifyGoogleFormat("void f() { int&& a = b; }"); 3261 3262 verifyIndependentOfContext("A<int &&> a;"); 3263 verifyIndependentOfContext("A<int &&, int &&> a;"); 3264 verifyGoogleFormat("A<int&&> a;"); 3265 verifyGoogleFormat("A<int&&, int&&> a;"); 3266} 3267 3268TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 3269 verifyFormat("void f() {\n" 3270 " x[aaaaaaaaa -\n" 3271 " b] = 23;\n" 3272 "}", 3273 getLLVMStyleWithColumns(15)); 3274} 3275 3276TEST_F(FormatTest, FormatsCasts) { 3277 verifyFormat("Type *A = static_cast<Type *>(P);"); 3278 verifyFormat("Type *A = (Type *)P;"); 3279 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 3280 verifyFormat("int a = (int)(2.0f);"); 3281 verifyFormat("int a = (int)2.0f;"); 3282 verifyFormat("x[(int32)y];"); 3283 verifyFormat("x = (int32)y;"); 3284 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 3285 verifyFormat("int a = (int)*b;"); 3286 verifyFormat("int a = (int)2.0f;"); 3287 verifyFormat("int a = (int)~0;"); 3288 verifyFormat("int a = (int)++a;"); 3289 verifyFormat("int a = (int)sizeof(int);"); 3290 verifyFormat("int a = (int)+2;"); 3291 verifyFormat("my_int a = (my_int)2.0f;"); 3292 verifyFormat("my_int a = (my_int)sizeof(int);"); 3293 verifyFormat("return (my_int)aaa;"); 3294 3295 // FIXME: Without type knowledge, this can still fall apart miserably. 3296 verifyFormat("void f() { my_int a = (my_int) * b; }"); 3297 verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }"); 3298 verifyFormat("my_int a = (my_int) ~0;"); 3299 verifyFormat("my_int a = (my_int)++ a;"); 3300 verifyFormat("my_int a = (my_int) + 2;"); 3301 3302 // These are not casts. 3303 verifyFormat("void f(int *) {}"); 3304 verifyFormat("f(foo)->b;"); 3305 verifyFormat("f(foo).b;"); 3306 verifyFormat("f(foo)(b);"); 3307 verifyFormat("f(foo)[b];"); 3308 verifyFormat("[](foo) { return 4; }(bar)];"); 3309 verifyFormat("(*funptr)(foo)[4];"); 3310 verifyFormat("funptrs[4](foo)[4];"); 3311 verifyFormat("void f(int *);"); 3312 verifyFormat("void f(int *) = 0;"); 3313 verifyFormat("void f(SmallVector<int>) {}"); 3314 verifyFormat("void f(SmallVector<int>);"); 3315 verifyFormat("void f(SmallVector<int>) = 0;"); 3316 verifyFormat("void f(int i = (kValue) * kMask) {}"); 3317 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 3318 verifyFormat("int a = sizeof(int) * b;"); 3319 verifyFormat("int a = alignof(int) * b;"); 3320 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 3321 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 3322 3323 // These are not casts, but at some point were confused with casts. 3324 verifyFormat("virtual void foo(int *) override;"); 3325 verifyFormat("virtual void foo(char &) const;"); 3326 verifyFormat("virtual void foo(int *a, char *) const;"); 3327 verifyFormat("int a = sizeof(int *) + b;"); 3328 verifyFormat("int a = alignof(int *) + b;"); 3329} 3330 3331TEST_F(FormatTest, FormatsFunctionTypes) { 3332 verifyFormat("A<bool()> a;"); 3333 verifyFormat("A<SomeType()> a;"); 3334 verifyFormat("A<void (*)(int, std::string)> a;"); 3335 verifyFormat("A<void *(int)>;"); 3336 verifyFormat("void *(*a)(int *, SomeType *);"); 3337 verifyFormat("int (*func)(void *);"); 3338 verifyFormat("void f() { int (*func)(void *); }"); 3339 3340 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 3341 verifyGoogleFormat("void* (*a)(int);"); 3342 3343 // Other constructs can look somewhat like function types: 3344 verifyFormat("A<sizeof(*x)> a;"); 3345 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 3346} 3347 3348TEST_F(FormatTest, BreaksLongDeclarations) { 3349 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 3350 " AnotherNameForTheLongType;"); 3351 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 3352 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 3353 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 3354 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 3355 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 3356 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 3357 3358 // FIXME: Without the comment, this breaks after "(". 3359 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 3360 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); 3361 3362 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 3363 " int LoooooooooooooooooooongParam2) {}"); 3364 verifyFormat( 3365 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 3366 " SourceLocation L, IdentifierIn *II,\n" 3367 " Type *T) {}"); 3368 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 3369 "ReallyReallyLongFunctionName(\n" 3370 " const std::string &SomeParameter,\n" 3371 " const SomeType<string, SomeOtherTemplateParameter> &\n" 3372 " ReallyReallyLongParameterName,\n" 3373 " const SomeType<string, SomeOtherTemplateParameter> &\n" 3374 " AnotherLongParameterName) {}"); 3375 verifyFormat("template <typename A>\n" 3376 "SomeLoooooooooooooooooooooongType<\n" 3377 " typename some_namespace::SomeOtherType<A>::Type>\n" 3378 "Function() {}"); 3379 verifyFormat( 3380 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 3381 " aaaaaaaaaaaaaaaaaaaaaaa;"); 3382 3383 verifyGoogleFormat( 3384 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 3385 " SourceLocation L) {}"); 3386 verifyGoogleFormat( 3387 "some_namespace::LongReturnType\n" 3388 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 3389 " int first_long_parameter, int second_parameter) {}"); 3390 3391 verifyGoogleFormat("template <typename T>\n" 3392 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3393 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 3394 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3395 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 3396} 3397 3398TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 3399 verifyFormat("(a)->b();"); 3400 verifyFormat("--a;"); 3401} 3402 3403TEST_F(FormatTest, HandlesIncludeDirectives) { 3404 verifyFormat("#include <string>\n" 3405 "#include <a/b/c.h>\n" 3406 "#include \"a/b/string\"\n" 3407 "#include \"string.h\"\n" 3408 "#include \"string.h\"\n" 3409 "#include <a-a>\n" 3410 "#include < path with space >\n" 3411 "#include \"abc.h\" // this is included for ABC\n" 3412 "#include \"some long include\" // with a comment\n" 3413 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 3414 getLLVMStyleWithColumns(35)); 3415 3416 verifyFormat("#import <string>"); 3417 verifyFormat("#import <a/b/c.h>"); 3418 verifyFormat("#import \"a/b/string\""); 3419 verifyFormat("#import \"string.h\""); 3420 verifyFormat("#import \"string.h\""); 3421} 3422 3423//===----------------------------------------------------------------------===// 3424// Error recovery tests. 3425//===----------------------------------------------------------------------===// 3426 3427TEST_F(FormatTest, IncompleteParameterLists) { 3428 FormatStyle NoBinPacking = getLLVMStyle(); 3429 NoBinPacking.BinPackParameters = false; 3430 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 3431 " double *min_x,\n" 3432 " double *max_x,\n" 3433 " double *min_y,\n" 3434 " double *max_y,\n" 3435 " double *min_z,\n" 3436 " double *max_z, ) {}", 3437 NoBinPacking); 3438} 3439 3440TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 3441 verifyFormat("void f() { return; }\n42"); 3442 verifyFormat("void f() {\n" 3443 " if (0)\n" 3444 " return;\n" 3445 "}\n" 3446 "42"); 3447 verifyFormat("void f() { return }\n42"); 3448 verifyFormat("void f() {\n" 3449 " if (0)\n" 3450 " return\n" 3451 "}\n" 3452 "42"); 3453} 3454 3455TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 3456 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 3457 EXPECT_EQ("void f() {\n" 3458 " if (a)\n" 3459 " return\n" 3460 "}", 3461 format("void f ( ) { if ( a ) return }")); 3462 EXPECT_EQ("namespace N {\n" 3463 "void f()\n" 3464 "}", 3465 format("namespace N { void f() }")); 3466 EXPECT_EQ("namespace N {\n" 3467 "void f() {}\n" 3468 "void g()\n" 3469 "}", 3470 format("namespace N { void f( ) { } void g( ) }")); 3471} 3472 3473TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 3474 verifyFormat("int aaaaaaaa =\n" 3475 " // Overlylongcomment\n" 3476 " b;", 3477 getLLVMStyleWithColumns(20)); 3478 verifyFormat("function(\n" 3479 " ShortArgument,\n" 3480 " LoooooooooooongArgument);\n", 3481 getLLVMStyleWithColumns(20)); 3482} 3483 3484TEST_F(FormatTest, IncorrectAccessSpecifier) { 3485 verifyFormat("public:"); 3486 verifyFormat("class A {\n" 3487 "public\n" 3488 " void f() {}\n" 3489 "};"); 3490 verifyFormat("public\n" 3491 "int qwerty;"); 3492 verifyFormat("public\n" 3493 "B {}"); 3494 verifyFormat("public\n" 3495 "{}"); 3496 verifyFormat("public\n" 3497 "B { int x; }"); 3498} 3499 3500TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 3501 verifyFormat("{"); 3502 verifyFormat("#})"); 3503} 3504 3505TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 3506 verifyFormat("do {\n}"); 3507 verifyFormat("do {\n}\n" 3508 "f();"); 3509 verifyFormat("do {\n}\n" 3510 "wheeee(fun);"); 3511 verifyFormat("do {\n" 3512 " f();\n" 3513 "}"); 3514} 3515 3516TEST_F(FormatTest, IncorrectCodeMissingParens) { 3517 verifyFormat("if {\n foo;\n foo();\n}"); 3518 verifyFormat("switch {\n foo;\n foo();\n}"); 3519 verifyFormat("for {\n foo;\n foo();\n}"); 3520 verifyFormat("while {\n foo;\n foo();\n}"); 3521 verifyFormat("do {\n foo;\n foo();\n} while;"); 3522} 3523 3524TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 3525 verifyFormat("namespace {\n" 3526 "class Foo { Foo (\n" 3527 "};\n" 3528 "} // comment"); 3529} 3530 3531TEST_F(FormatTest, IncorrectCodeErrorDetection) { 3532 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 3533 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 3534 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 3535 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 3536 3537 EXPECT_EQ("{\n" 3538 " {\n" 3539 " breakme(\n" 3540 " qwe);\n" 3541 "}\n", 3542 format("{\n" 3543 " {\n" 3544 " breakme(qwe);\n" 3545 "}\n", 3546 getLLVMStyleWithColumns(10))); 3547} 3548 3549TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 3550 verifyFormat("int x = {\n" 3551 " avariable,\n" 3552 " b(alongervariable)\n" 3553 "};", 3554 getLLVMStyleWithColumns(25)); 3555} 3556 3557TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 3558 verifyFormat("return (a)(b) { 1, 2, 3 };"); 3559} 3560 3561TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { 3562 verifyFormat("vector<int> x{ 1, 2, 3, 4 };"); 3563 verifyFormat("vector<T> x{ {}, {}, {}, {} };"); 3564 verifyFormat("f({ 1, 2 });"); 3565 verifyFormat("auto v = Foo{ 1 };"); 3566 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });"); 3567 verifyFormat("Class::Class : member{ 1, 2, 3 } {}"); 3568 verifyFormat("new vector<int>{ 1, 2, 3 };"); 3569 verifyFormat("new int[3]{ 1, 2, 3 };"); 3570 verifyFormat("return { arg1, arg2 };"); 3571 verifyFormat("return { arg1, SomeType{ parameter } };"); 3572 verifyFormat("new T{ arg1, arg2 };"); 3573 verifyFormat("class Class {\n" 3574 " T member = { arg1, arg2 };\n" 3575 "};"); 3576 verifyFormat( 3577 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3578 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 3579 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3580 " bbbbbbbbbbbbbbbbbbbb, bbbbb };"); 3581 3582 FormatStyle NoSpaces = getLLVMStyle(); 3583 NoSpaces.SpacesInBracedLists = false; 3584 verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces); 3585 verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces); 3586 verifyFormat("f({1, 2});", NoSpaces); 3587 verifyFormat("auto v = Foo{-1};", NoSpaces); 3588 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces); 3589 verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces); 3590 verifyFormat("new vector<int>{1, 2, 3};", NoSpaces); 3591 verifyFormat("new int[3]{1, 2, 3};", NoSpaces); 3592 verifyFormat("return {arg1, arg2};", NoSpaces); 3593 verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces); 3594 verifyFormat("new T{arg1, arg2};", NoSpaces); 3595 verifyFormat("class Class {\n" 3596 " T member = {arg1, arg2};\n" 3597 "};", 3598 NoSpaces); 3599} 3600 3601TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 3602 verifyFormat("void f() { return 42; }"); 3603 verifyFormat("void f() {\n" 3604 " // Comment\n" 3605 "}"); 3606 verifyFormat("{\n" 3607 "#error {\n" 3608 " int a;\n" 3609 "}"); 3610 verifyFormat("{\n" 3611 " int a;\n" 3612 "#error {\n" 3613 "}"); 3614 verifyFormat("void f() {} // comment"); 3615 verifyFormat("void f() { int a; } // comment"); 3616 verifyFormat("void f() {\n" 3617 "} // comment", 3618 getLLVMStyleWithColumns(15)); 3619 3620 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 3621 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 3622 3623 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 3624 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 3625} 3626 3627TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 3628 // Elaborate type variable declarations. 3629 verifyFormat("struct foo a = { bar };\nint n;"); 3630 verifyFormat("class foo a = { bar };\nint n;"); 3631 verifyFormat("union foo a = { bar };\nint n;"); 3632 3633 // Elaborate types inside function definitions. 3634 verifyFormat("struct foo f() {}\nint n;"); 3635 verifyFormat("class foo f() {}\nint n;"); 3636 verifyFormat("union foo f() {}\nint n;"); 3637 3638 // Templates. 3639 verifyFormat("template <class X> void f() {}\nint n;"); 3640 verifyFormat("template <struct X> void f() {}\nint n;"); 3641 verifyFormat("template <union X> void f() {}\nint n;"); 3642 3643 // Actual definitions... 3644 verifyFormat("struct {\n} n;"); 3645 verifyFormat( 3646 "template <template <class T, class Y>, class Z> class X {\n} n;"); 3647 verifyFormat("union Z {\n int n;\n} x;"); 3648 verifyFormat("class MACRO Z {\n} n;"); 3649 verifyFormat("class MACRO(X) Z {\n} n;"); 3650 verifyFormat("class __attribute__(X) Z {\n} n;"); 3651 verifyFormat("class __declspec(X) Z {\n} n;"); 3652 verifyFormat("class A##B##C {\n} n;"); 3653 3654 // Redefinition from nested context: 3655 verifyFormat("class A::B::C {\n} n;"); 3656 3657 // Template definitions. 3658 verifyFormat( 3659 "template <typename F>\n" 3660 "Matcher(const Matcher<F> &Other,\n" 3661 " typename enable_if_c<is_base_of<F, T>::value &&\n" 3662 " !is_same<F, T>::value>::type * = 0)\n" 3663 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 3664 3665 // FIXME: This is still incorrectly handled at the formatter side. 3666 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {};"); 3667 3668 // FIXME: 3669 // This now gets parsed incorrectly as class definition. 3670 // verifyFormat("class A<int> f() {\n}\nint n;"); 3671 3672 // Elaborate types where incorrectly parsing the structural element would 3673 // break the indent. 3674 verifyFormat("if (true)\n" 3675 " class X x;\n" 3676 "else\n" 3677 " f();\n"); 3678 3679 // This is simply incomplete. Formatting is not important, but must not crash. 3680 verifyFormat("class A:"); 3681} 3682 3683TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 3684 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 3685 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 3686 EXPECT_EQ("#error 1", format(" # error 1")); 3687 EXPECT_EQ("#warning 1", format(" # warning 1")); 3688} 3689 3690TEST_F(FormatTest, FormatHashIfExpressions) { 3691 // FIXME: Come up with a better indentation for #elif. 3692 verifyFormat( 3693 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 3694 " defined(BBBBBBBB)\n" 3695 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 3696 " defined(BBBBBBBB)\n" 3697 "#endif", 3698 getLLVMStyleWithColumns(65)); 3699} 3700 3701TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 3702 FormatStyle AllowsMergedIf = getGoogleStyle(); 3703 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 3704 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 3705 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 3706 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 3707 EXPECT_EQ("if (true) return 42;", 3708 format("if (true)\nreturn 42;", AllowsMergedIf)); 3709 FormatStyle ShortMergedIf = AllowsMergedIf; 3710 ShortMergedIf.ColumnLimit = 25; 3711 verifyFormat("#define A \\\n" 3712 " if (true) return 42;", 3713 ShortMergedIf); 3714 verifyFormat("#define A \\\n" 3715 " f(); \\\n" 3716 " if (true)\n" 3717 "#define B", 3718 ShortMergedIf); 3719 verifyFormat("#define A \\\n" 3720 " f(); \\\n" 3721 " if (true)\n" 3722 "g();", 3723 ShortMergedIf); 3724 verifyFormat("{\n" 3725 "#ifdef A\n" 3726 " // Comment\n" 3727 " if (true) continue;\n" 3728 "#endif\n" 3729 " // Comment\n" 3730 " if (true) continue;", 3731 ShortMergedIf); 3732} 3733 3734TEST_F(FormatTest, BlockCommentsInControlLoops) { 3735 verifyFormat("if (0) /* a comment in a strange place */ {\n" 3736 " f();\n" 3737 "}"); 3738 verifyFormat("if (0) /* a comment in a strange place */ {\n" 3739 " f();\n" 3740 "} /* another comment */ else /* comment #3 */ {\n" 3741 " g();\n" 3742 "}"); 3743 verifyFormat("while (0) /* a comment in a strange place */ {\n" 3744 " f();\n" 3745 "}"); 3746 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 3747 " f();\n" 3748 "}"); 3749 verifyFormat("do /* a comment in a strange place */ {\n" 3750 " f();\n" 3751 "} /* another comment */ while (0);"); 3752} 3753 3754TEST_F(FormatTest, BlockComments) { 3755 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 3756 format("/* *//* */ /* */\n/* *//* */ /* */")); 3757 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 3758 EXPECT_EQ("#define A /*123*/\\\n" 3759 " b\n" 3760 "/* */\n" 3761 "someCall(\n" 3762 " parameter);", 3763 format("#define A /*123*/ b\n" 3764 "/* */\n" 3765 "someCall(parameter);", 3766 getLLVMStyleWithColumns(15))); 3767 3768 EXPECT_EQ("#define A\n" 3769 "/* */ someCall(\n" 3770 " parameter);", 3771 format("#define A\n" 3772 "/* */someCall(parameter);", 3773 getLLVMStyleWithColumns(15))); 3774 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 3775 EXPECT_EQ("/*\n" 3776 "*\n" 3777 " * aaaaaa\n" 3778 "* aaaaaa\n" 3779 "*/", 3780 format("/*\n" 3781 "*\n" 3782 " * aaaaaa aaaaaa\n" 3783 "*/", 3784 getLLVMStyleWithColumns(10))); 3785 EXPECT_EQ("/*\n" 3786 "**\n" 3787 "* aaaaaa\n" 3788 "* aaaaaa\n" 3789 "*/", 3790 format("/*\n" 3791 "**\n" 3792 "* aaaaaa aaaaaa\n" 3793 "*/", 3794 getLLVMStyleWithColumns(10))); 3795 3796 FormatStyle NoBinPacking = getLLVMStyle(); 3797 NoBinPacking.BinPackParameters = false; 3798 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 3799 " 2, /* comment 2 */\n" 3800 " 3, /* comment 3 */\n" 3801 " aaaa,\n" 3802 " bbbb);", 3803 format("someFunction (1, /* comment 1 */\n" 3804 " 2, /* comment 2 */ \n" 3805 " 3, /* comment 3 */\n" 3806 "aaaa, bbbb );", 3807 NoBinPacking)); 3808 verifyFormat( 3809 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3811 EXPECT_EQ( 3812 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 3813 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 3815 format( 3816 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 3817 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3818 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 3819 EXPECT_EQ( 3820 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 3821 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 3822 "int cccccccccccccccccccccccccccccc; /* comment */\n", 3823 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 3824 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 3825 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 3826} 3827 3828TEST_F(FormatTest, BlockCommentsInMacros) { 3829 EXPECT_EQ("#define A \\\n" 3830 " { \\\n" 3831 " /* one line */ \\\n" 3832 " someCall();", 3833 format("#define A { \\\n" 3834 " /* one line */ \\\n" 3835 " someCall();", 3836 getLLVMStyleWithColumns(20))); 3837 EXPECT_EQ("#define A \\\n" 3838 " { \\\n" 3839 " /* previous */ \\\n" 3840 " /* one line */ \\\n" 3841 " someCall();", 3842 format("#define A { \\\n" 3843 " /* previous */ \\\n" 3844 " /* one line */ \\\n" 3845 " someCall();", 3846 getLLVMStyleWithColumns(20))); 3847} 3848 3849TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 3850 EXPECT_EQ("a = {\n" 3851 " 1111 /* */\n" 3852 "};", 3853 format("a = {1111\n" 3854 "/* */\n" 3855 "};", 3856 getLLVMStyleWithColumns(15))); 3857 EXPECT_EQ("a = {\n" 3858 " 1111 /* */\n" 3859 "};", 3860 format("a = {1111\n" 3861 "/* */\n" 3862 "};", 3863 getLLVMStyleWithColumns(15))); 3864 3865 // FIXME: The formatting is still wrong here. 3866 EXPECT_EQ("a = {\n" 3867 " 1111 /* a\n" 3868 " */\n" 3869 "};", 3870 format("a = {1111\n" 3871 "/* a */\n" 3872 "};", 3873 getLLVMStyleWithColumns(15))); 3874} 3875 3876TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 3877 // FIXME: This is not what we want... 3878 verifyFormat("{\n" 3879 "// a" 3880 "// b"); 3881} 3882 3883TEST_F(FormatTest, FormatStarDependingOnContext) { 3884 verifyFormat("void f(int *a);"); 3885 verifyFormat("void f() { f(fint * b); }"); 3886 verifyFormat("class A {\n void f(int *a);\n};"); 3887 verifyFormat("class A {\n int *a;\n};"); 3888 verifyFormat("namespace a {\n" 3889 "namespace b {\n" 3890 "class A {\n" 3891 " void f() {}\n" 3892 " int *a;\n" 3893 "};\n" 3894 "}\n" 3895 "}"); 3896} 3897 3898TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 3899 verifyFormat("while"); 3900 verifyFormat("operator"); 3901} 3902 3903//===----------------------------------------------------------------------===// 3904// Objective-C tests. 3905//===----------------------------------------------------------------------===// 3906 3907TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 3908 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 3909 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 3910 format("-(NSUInteger)indexOfObject:(id)anObject;")); 3911 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 3912 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 3913 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 3914 format("-(NSInteger)Method3:(id)anObject;")); 3915 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 3916 format("-(NSInteger)Method4:(id)anObject;")); 3917 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 3918 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 3919 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 3920 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 3921 EXPECT_EQ( 3922 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 3923 format( 3924 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 3925 3926 // Very long objectiveC method declaration. 3927 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 3928 " inRange:(NSRange)range\n" 3929 " outRange:(NSRange)out_range\n" 3930 " outRange1:(NSRange)out_range1\n" 3931 " outRange2:(NSRange)out_range2\n" 3932 " outRange3:(NSRange)out_range3\n" 3933 " outRange4:(NSRange)out_range4\n" 3934 " outRange5:(NSRange)out_range5\n" 3935 " outRange6:(NSRange)out_range6\n" 3936 " outRange7:(NSRange)out_range7\n" 3937 " outRange8:(NSRange)out_range8\n" 3938 " outRange9:(NSRange)out_range9;"); 3939 3940 verifyFormat("- (int)sum:(vector<int>)numbers;"); 3941 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 3942 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 3943 // protocol lists (but not for template classes): 3944 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 3945 3946 verifyFormat("- (int (*)())foo:(int (*)())f;"); 3947 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 3948 3949 // If there's no return type (very rare in practice!), LLVM and Google style 3950 // agree. 3951 verifyFormat("- foo;"); 3952 verifyFormat("- foo:(int)f;"); 3953 verifyGoogleFormat("- foo:(int)foo;"); 3954} 3955 3956TEST_F(FormatTest, FormatObjCBlocks) { 3957 verifyFormat("int (^Block)(int, int);"); 3958 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 3959} 3960 3961TEST_F(FormatTest, FormatObjCInterface) { 3962 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 3963 "@public\n" 3964 " int field1;\n" 3965 "@protected\n" 3966 " int field2;\n" 3967 "@private\n" 3968 " int field3;\n" 3969 "@package\n" 3970 " int field4;\n" 3971 "}\n" 3972 "+ (id)init;\n" 3973 "@end"); 3974 3975 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 3976 " @public\n" 3977 " int field1;\n" 3978 " @protected\n" 3979 " int field2;\n" 3980 " @private\n" 3981 " int field3;\n" 3982 " @package\n" 3983 " int field4;\n" 3984 "}\n" 3985 "+ (id)init;\n" 3986 "@end"); 3987 3988 verifyFormat("@interface /* wait for it */ Foo\n" 3989 "+ (id)init;\n" 3990 "// Look, a comment!\n" 3991 "- (int)answerWith:(int)i;\n" 3992 "@end"); 3993 3994 verifyFormat("@interface Foo\n" 3995 "@end\n" 3996 "@interface Bar\n" 3997 "@end"); 3998 3999 verifyFormat("@interface Foo : Bar\n" 4000 "+ (id)init;\n" 4001 "@end"); 4002 4003 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 4004 "+ (id)init;\n" 4005 "@end"); 4006 4007 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 4008 "+ (id)init;\n" 4009 "@end"); 4010 4011 verifyFormat("@interface Foo (HackStuff)\n" 4012 "+ (id)init;\n" 4013 "@end"); 4014 4015 verifyFormat("@interface Foo ()\n" 4016 "+ (id)init;\n" 4017 "@end"); 4018 4019 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 4020 "+ (id)init;\n" 4021 "@end"); 4022 4023 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 4024 "+ (id)init;\n" 4025 "@end"); 4026 4027 verifyFormat("@interface Foo {\n" 4028 " int _i;\n" 4029 "}\n" 4030 "+ (id)init;\n" 4031 "@end"); 4032 4033 verifyFormat("@interface Foo : Bar {\n" 4034 " int _i;\n" 4035 "}\n" 4036 "+ (id)init;\n" 4037 "@end"); 4038 4039 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 4040 " int _i;\n" 4041 "}\n" 4042 "+ (id)init;\n" 4043 "@end"); 4044 4045 verifyFormat("@interface Foo (HackStuff) {\n" 4046 " int _i;\n" 4047 "}\n" 4048 "+ (id)init;\n" 4049 "@end"); 4050 4051 verifyFormat("@interface Foo () {\n" 4052 " int _i;\n" 4053 "}\n" 4054 "+ (id)init;\n" 4055 "@end"); 4056 4057 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 4058 " int _i;\n" 4059 "}\n" 4060 "+ (id)init;\n" 4061 "@end"); 4062} 4063 4064TEST_F(FormatTest, FormatObjCImplementation) { 4065 verifyFormat("@implementation Foo : NSObject {\n" 4066 "@public\n" 4067 " int field1;\n" 4068 "@protected\n" 4069 " int field2;\n" 4070 "@private\n" 4071 " int field3;\n" 4072 "@package\n" 4073 " int field4;\n" 4074 "}\n" 4075 "+ (id)init {\n}\n" 4076 "@end"); 4077 4078 verifyGoogleFormat("@implementation Foo : NSObject {\n" 4079 " @public\n" 4080 " int field1;\n" 4081 " @protected\n" 4082 " int field2;\n" 4083 " @private\n" 4084 " int field3;\n" 4085 " @package\n" 4086 " int field4;\n" 4087 "}\n" 4088 "+ (id)init {\n}\n" 4089 "@end"); 4090 4091 verifyFormat("@implementation Foo\n" 4092 "+ (id)init {\n" 4093 " if (true)\n" 4094 " return nil;\n" 4095 "}\n" 4096 "// Look, a comment!\n" 4097 "- (int)answerWith:(int)i {\n" 4098 " return i;\n" 4099 "}\n" 4100 "+ (int)answerWith:(int)i {\n" 4101 " return i;\n" 4102 "}\n" 4103 "@end"); 4104 4105 verifyFormat("@implementation Foo\n" 4106 "@end\n" 4107 "@implementation Bar\n" 4108 "@end"); 4109 4110 verifyFormat("@implementation Foo : Bar\n" 4111 "+ (id)init {\n}\n" 4112 "- (void)foo {\n}\n" 4113 "@end"); 4114 4115 verifyFormat("@implementation Foo {\n" 4116 " int _i;\n" 4117 "}\n" 4118 "+ (id)init {\n}\n" 4119 "@end"); 4120 4121 verifyFormat("@implementation Foo : Bar {\n" 4122 " int _i;\n" 4123 "}\n" 4124 "+ (id)init {\n}\n" 4125 "@end"); 4126 4127 verifyFormat("@implementation Foo (HackStuff)\n" 4128 "+ (id)init {\n}\n" 4129 "@end"); 4130} 4131 4132TEST_F(FormatTest, FormatObjCProtocol) { 4133 verifyFormat("@protocol Foo\n" 4134 "@property(weak) id delegate;\n" 4135 "- (NSUInteger)numberOfThings;\n" 4136 "@end"); 4137 4138 verifyFormat("@protocol MyProtocol <NSObject>\n" 4139 "- (NSUInteger)numberOfThings;\n" 4140 "@end"); 4141 4142 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 4143 "- (NSUInteger)numberOfThings;\n" 4144 "@end"); 4145 4146 verifyFormat("@protocol Foo;\n" 4147 "@protocol Bar;\n"); 4148 4149 verifyFormat("@protocol Foo\n" 4150 "@end\n" 4151 "@protocol Bar\n" 4152 "@end"); 4153 4154 verifyFormat("@protocol myProtocol\n" 4155 "- (void)mandatoryWithInt:(int)i;\n" 4156 "@optional\n" 4157 "- (void)optional;\n" 4158 "@required\n" 4159 "- (void)required;\n" 4160 "@optional\n" 4161 "@property(assign) int madProp;\n" 4162 "@end\n"); 4163} 4164 4165TEST_F(FormatTest, FormatObjCMethodDeclarations) { 4166 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 4167 " rect:(NSRect)theRect\n" 4168 " interval:(float)theInterval {\n" 4169 "}"); 4170 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 4171 " longKeyword:(NSRect)theRect\n" 4172 " evenLongerKeyword:(float)theInterval\n" 4173 " error:(NSError **)theError {\n" 4174 "}"); 4175} 4176 4177TEST_F(FormatTest, FormatObjCMethodExpr) { 4178 verifyFormat("[foo bar:baz];"); 4179 verifyFormat("return [foo bar:baz];"); 4180 verifyFormat("f([foo bar:baz]);"); 4181 verifyFormat("f(2, [foo bar:baz]);"); 4182 verifyFormat("f(2, a ? b : c);"); 4183 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 4184 4185 // Unary operators. 4186 verifyFormat("int a = +[foo bar:baz];"); 4187 verifyFormat("int a = -[foo bar:baz];"); 4188 verifyFormat("int a = ![foo bar:baz];"); 4189 verifyFormat("int a = ~[foo bar:baz];"); 4190 verifyFormat("int a = ++[foo bar:baz];"); 4191 verifyFormat("int a = --[foo bar:baz];"); 4192 verifyFormat("int a = sizeof [foo bar:baz];"); 4193 verifyFormat("int a = alignof [foo bar:baz];"); 4194 verifyFormat("int a = &[foo bar:baz];"); 4195 verifyFormat("int a = *[foo bar:baz];"); 4196 // FIXME: Make casts work, without breaking f()[4]. 4197 //verifyFormat("int a = (int)[foo bar:baz];"); 4198 //verifyFormat("return (int)[foo bar:baz];"); 4199 //verifyFormat("(void)[foo bar:baz];"); 4200 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 4201 4202 // Binary operators. 4203 verifyFormat("[foo bar:baz], [foo bar:baz];"); 4204 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 4205 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 4206 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 4207 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 4208 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 4209 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 4210 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 4211 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 4212 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 4213 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 4214 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 4215 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 4216 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 4217 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 4218 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 4219 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 4220 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 4221 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 4222 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 4223 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 4224 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 4225 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 4226 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 4227 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 4228 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 4229 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 4230 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 4231 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 4232 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 4233 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 4234 // Whew! 4235 4236 verifyFormat("return in[42];"); 4237 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 4238 "}"); 4239 4240 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 4241 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 4242 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 4243 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 4244 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 4245 verifyFormat("[button setAction:@selector(zoomOut:)];"); 4246 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 4247 4248 verifyFormat("arr[[self indexForFoo:a]];"); 4249 verifyFormat("throw [self errorFor:a];"); 4250 verifyFormat("@throw [self errorFor:a];"); 4251 4252 // This tests that the formatter doesn't break after "backing" but before ":", 4253 // which would be at 80 columns. 4254 verifyFormat( 4255 "void f() {\n" 4256 " if ((self = [super initWithContentRect:contentRect\n" 4257 " styleMask:styleMask\n" 4258 " backing:NSBackingStoreBuffered\n" 4259 " defer:YES]))"); 4260 4261 verifyFormat( 4262 "[foo checkThatBreakingAfterColonWorksOk:\n" 4263 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 4264 4265 verifyFormat("[myObj short:arg1 // Force line break\n" 4266 " longKeyword:arg2\n" 4267 " evenLongerKeyword:arg3\n" 4268 " error:arg4];"); 4269 verifyFormat( 4270 "void f() {\n" 4271 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 4272 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 4273 " pos.width(), pos.height())\n" 4274 " styleMask:NSBorderlessWindowMask\n" 4275 " backing:NSBackingStoreBuffered\n" 4276 " defer:NO]);\n" 4277 "}"); 4278 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 4279 " with:contentsNativeView];"); 4280 4281 verifyFormat( 4282 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 4283 " owner:nillllll];"); 4284 4285 verifyFormat( 4286 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 4287 " forType:kBookmarkButtonDragType];"); 4288 4289 verifyFormat("[defaultCenter addObserver:self\n" 4290 " selector:@selector(willEnterFullscreen)\n" 4291 " name:kWillEnterFullscreenNotification\n" 4292 " object:nil];"); 4293 verifyFormat("[image_rep drawInRect:drawRect\n" 4294 " fromRect:NSZeroRect\n" 4295 " operation:NSCompositeCopy\n" 4296 " fraction:1.0\n" 4297 " respectFlipped:NO\n" 4298 " hints:nil];"); 4299 4300 verifyFormat( 4301 "scoped_nsobject<NSTextField> message(\n" 4302 " // The frame will be fixed up when |-setMessageText:| is called.\n" 4303 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 4304} 4305 4306TEST_F(FormatTest, ObjCAt) { 4307 verifyFormat("@autoreleasepool"); 4308 verifyFormat("@catch"); 4309 verifyFormat("@class"); 4310 verifyFormat("@compatibility_alias"); 4311 verifyFormat("@defs"); 4312 verifyFormat("@dynamic"); 4313 verifyFormat("@encode"); 4314 verifyFormat("@end"); 4315 verifyFormat("@finally"); 4316 verifyFormat("@implementation"); 4317 verifyFormat("@import"); 4318 verifyFormat("@interface"); 4319 verifyFormat("@optional"); 4320 verifyFormat("@package"); 4321 verifyFormat("@private"); 4322 verifyFormat("@property"); 4323 verifyFormat("@protected"); 4324 verifyFormat("@protocol"); 4325 verifyFormat("@public"); 4326 verifyFormat("@required"); 4327 verifyFormat("@selector"); 4328 verifyFormat("@synchronized"); 4329 verifyFormat("@synthesize"); 4330 verifyFormat("@throw"); 4331 verifyFormat("@try"); 4332 4333 EXPECT_EQ("@interface", format("@ interface")); 4334 4335 // The precise formatting of this doesn't matter, nobody writes code like 4336 // this. 4337 verifyFormat("@ /*foo*/ interface"); 4338} 4339 4340TEST_F(FormatTest, ObjCSnippets) { 4341 verifyFormat("@autoreleasepool {\n" 4342 " foo();\n" 4343 "}"); 4344 verifyFormat("@class Foo, Bar;"); 4345 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 4346 verifyFormat("@dynamic textColor;"); 4347 verifyFormat("char *buf1 = @encode(int *);"); 4348 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 4349 verifyFormat("char *buf1 = @encode(int **);"); 4350 verifyFormat("Protocol *proto = @protocol(p1);"); 4351 verifyFormat("SEL s = @selector(foo:);"); 4352 verifyFormat("@synchronized(self) {\n" 4353 " f();\n" 4354 "}"); 4355 4356 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 4357 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 4358 4359 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 4360 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 4361 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 4362} 4363 4364TEST_F(FormatTest, ObjCLiterals) { 4365 verifyFormat("@\"String\""); 4366 verifyFormat("@1"); 4367 verifyFormat("@+4.8"); 4368 verifyFormat("@-4"); 4369 verifyFormat("@1LL"); 4370 verifyFormat("@.5"); 4371 verifyFormat("@'c'"); 4372 verifyFormat("@true"); 4373 4374 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 4375 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 4376 verifyFormat("NSNumber *favoriteColor = @(Green);"); 4377 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 4378 4379 verifyFormat("@["); 4380 verifyFormat("@[]"); 4381 verifyFormat( 4382 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 4383 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 4384 4385 verifyFormat("@{"); 4386 verifyFormat("@{}"); 4387 verifyFormat("@{ @\"one\" : @1 }"); 4388 verifyFormat("return @{ @\"one\" : @1 };"); 4389 verifyFormat("@{ @\"one\" : @1, }"); 4390 4391 verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); 4392 verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); 4393 4394 verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); 4395 verifyFormat("[self setDict:@{}"); 4396 verifyFormat("[self setDict:@{ @1 : @2 }"); 4397 verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); 4398 verifyFormat( 4399 "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); 4400 verifyFormat( 4401 "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); 4402 4403 // FIXME: Nested and multi-line array and dictionary literals need more work. 4404 verifyFormat( 4405 "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" 4406 " @\"processInfo\" : [NSProcessInfo processInfo] };"); 4407 verifyFormat( 4408 "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 4409 " regularFont, };"); 4410 4411} 4412 4413TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 4414 EXPECT_EQ("{\n" 4415 "{\n" 4416 "a;\n" 4417 "b;\n" 4418 "}\n" 4419 "}", 4420 format("{\n" 4421 "{\n" 4422 "a;\n" 4423 " b;\n" 4424 "}\n" 4425 "}", 4426 13, 2, getLLVMStyle())); 4427 EXPECT_EQ("{\n" 4428 "{\n" 4429 " a;\n" 4430 "b;\n" 4431 "}\n" 4432 "}", 4433 format("{\n" 4434 "{\n" 4435 " a;\n" 4436 "b;\n" 4437 "}\n" 4438 "}", 4439 9, 2, getLLVMStyle())); 4440 EXPECT_EQ("{\n" 4441 "{\n" 4442 "public:\n" 4443 " b;\n" 4444 "}\n" 4445 "}", 4446 format("{\n" 4447 "{\n" 4448 "public:\n" 4449 " b;\n" 4450 "}\n" 4451 "}", 4452 17, 2, getLLVMStyle())); 4453 EXPECT_EQ("{\n" 4454 "{\n" 4455 "a;\n" 4456 "}\n" 4457 "{\n" 4458 " b; //\n" 4459 "}\n" 4460 "}", 4461 format("{\n" 4462 "{\n" 4463 "a;\n" 4464 "}\n" 4465 "{\n" 4466 " b; //\n" 4467 "}\n" 4468 "}", 4469 22, 2, getLLVMStyle())); 4470 EXPECT_EQ(" {\n" 4471 " a; //\n" 4472 " }", 4473 format(" {\n" 4474 "a; //\n" 4475 " }", 4476 4, 2, getLLVMStyle())); 4477 EXPECT_EQ("void f() {}\n" 4478 "void g() {}", 4479 format("void f() {}\n" 4480 "void g() {}", 4481 13, 0, getLLVMStyle())); 4482 EXPECT_EQ("int a; // comment\n" 4483 " // line 2\n" 4484 "int b;", 4485 format("int a; // comment\n" 4486 " // line 2\n" 4487 " int b;", 4488 35, 0, getLLVMStyle())); 4489} 4490 4491TEST_F(FormatTest, BreakStringLiterals) { 4492 EXPECT_EQ("\"some text \"\n" 4493 "\"other\";", 4494 format("\"some text other\";", getLLVMStyleWithColumns(12))); 4495 EXPECT_EQ("\"some text \"\n" 4496 "\"other\";", 4497 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 4498 EXPECT_EQ( 4499 "#define A \\\n" 4500 " \"some \" \\\n" 4501 " \"text \" \\\n" 4502 " \"other\";", 4503 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 4504 EXPECT_EQ( 4505 "#define A \\\n" 4506 " \"so \" \\\n" 4507 " \"text \" \\\n" 4508 " \"other\";", 4509 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 4510 4511 EXPECT_EQ("\"some text\"", 4512 format("\"some text\"", getLLVMStyleWithColumns(1))); 4513 EXPECT_EQ("\"some text\"", 4514 format("\"some text\"", getLLVMStyleWithColumns(11))); 4515 EXPECT_EQ("\"some \"\n" 4516 "\"text\"", 4517 format("\"some text\"", getLLVMStyleWithColumns(10))); 4518 EXPECT_EQ("\"some \"\n" 4519 "\"text\"", 4520 format("\"some text\"", getLLVMStyleWithColumns(7))); 4521 EXPECT_EQ("\"some\"\n" 4522 "\" tex\"\n" 4523 "\"t\"", 4524 format("\"some text\"", getLLVMStyleWithColumns(6))); 4525 EXPECT_EQ("\"some\"\n" 4526 "\" tex\"\n" 4527 "\" and\"", 4528 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 4529 EXPECT_EQ("\"some\"\n" 4530 "\"/tex\"\n" 4531 "\"/and\"", 4532 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 4533 4534 EXPECT_EQ("variable =\n" 4535 " \"long string \"\n" 4536 " \"literal\";", 4537 format("variable = \"long string literal\";", 4538 getLLVMStyleWithColumns(20))); 4539 4540 EXPECT_EQ("variable = f(\n" 4541 " \"long string \"\n" 4542 " \"literal\",\n" 4543 " short,\n" 4544 " loooooooooooooooooooong);", 4545 format("variable = f(\"long string literal\", short, " 4546 "loooooooooooooooooooong);", 4547 getLLVMStyleWithColumns(20))); 4548 EXPECT_EQ( 4549 "f(\"one two\".split(\n" 4550 " variable));", 4551 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 4552 EXPECT_EQ("f(\"one two three four five six \"\n" 4553 " \"seven\".split(\n" 4554 " really_looooong_variable));", 4555 format("f(\"one two three four five six seven\"." 4556 "split(really_looooong_variable));", 4557 getLLVMStyleWithColumns(33))); 4558 4559 EXPECT_EQ("f(\"some \"\n" 4560 " \"text\",\n" 4561 " other);", 4562 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 4563 4564 // Only break as a last resort. 4565 verifyFormat( 4566 "aaaaaaaaaaaaaaaaaaaa(\n" 4567 " aaaaaaaaaaaaaaaaaaaa,\n" 4568 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 4569 4570 EXPECT_EQ( 4571 "\"splitmea\"\n" 4572 "\"trandomp\"\n" 4573 "\"oint\"", 4574 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 4575 4576 EXPECT_EQ( 4577 "\"split/\"\n" 4578 "\"pathat/\"\n" 4579 "\"slashes\"", 4580 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 4581 4582 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 4583 AlignLeft.AlignEscapedNewlinesLeft = true; 4584 EXPECT_EQ( 4585 "#define A \\\n" 4586 " \"some \" \\\n" 4587 " \"text \" \\\n" 4588 " \"other\";", 4589 format("#define A \"some text other\";", AlignLeft)); 4590} 4591 4592TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 4593 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 4594 " \"ddeeefff\");", 4595 format("someFunction(\"aaabbbcccdddeeefff\");", 4596 getLLVMStyleWithColumns(25))); 4597 EXPECT_EQ("someFunction1234567890(\n" 4598 " \"aaabbbcccdddeeefff\");", 4599 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 4600 getLLVMStyleWithColumns(26))); 4601 EXPECT_EQ("someFunction1234567890(\n" 4602 " \"aaabbbcccdddeeeff\"\n" 4603 " \"f\");", 4604 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 4605 getLLVMStyleWithColumns(25))); 4606 EXPECT_EQ("someFunction1234567890(\n" 4607 " \"aaabbbcccdddeeeff\"\n" 4608 " \"f\");", 4609 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 4610 getLLVMStyleWithColumns(24))); 4611 EXPECT_EQ("someFunction(\n" 4612 " \"aaabbbcc \"\n" 4613 " \"dddeeefff\");", 4614 format("someFunction(\"aaabbbcc dddeeefff\");", 4615 getLLVMStyleWithColumns(25))); 4616 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 4617 " \"ddeeefff\");", 4618 format("someFunction(\"aaabbbccc ddeeefff\");", 4619 getLLVMStyleWithColumns(25))); 4620 EXPECT_EQ("someFunction1234567890(\n" 4621 " \"aaabb \"\n" 4622 " \"cccdddeeefff\");", 4623 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 4624 getLLVMStyleWithColumns(25))); 4625 EXPECT_EQ("#define A \\\n" 4626 " string s = \\\n" 4627 " \"123456789\" \\\n" 4628 " \"0\"; \\\n" 4629 " int i;", 4630 format("#define A string s = \"1234567890\"; int i;", 4631 getLLVMStyleWithColumns(20))); 4632} 4633 4634TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 4635 EXPECT_EQ("\"\\a\"", 4636 format("\"\\a\"", getLLVMStyleWithColumns(3))); 4637 EXPECT_EQ("\"\\\"", 4638 format("\"\\\"", getLLVMStyleWithColumns(2))); 4639 EXPECT_EQ("\"test\"\n" 4640 "\"\\n\"", 4641 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 4642 EXPECT_EQ("\"tes\\\\\"\n" 4643 "\"n\"", 4644 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 4645 EXPECT_EQ("\"\\\\\\\\\"\n" 4646 "\"\\n\"", 4647 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 4648 EXPECT_EQ("\"\\uff01\"", 4649 format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 4650 EXPECT_EQ("\"\\uff01\"\n" 4651 "\"test\"", 4652 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 4653 EXPECT_EQ("\"\\Uff01ff02\"", 4654 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 4655 EXPECT_EQ("\"\\x000000000001\"\n" 4656 "\"next\"", 4657 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 4658 EXPECT_EQ("\"\\x000000000001next\"", 4659 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 4660 EXPECT_EQ("\"\\x000000000001\"", 4661 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 4662 EXPECT_EQ("\"test\"\n" 4663 "\"\\000000\"\n" 4664 "\"000001\"", 4665 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 4666 EXPECT_EQ("\"test\\000\"\n" 4667 "\"00000000\"\n" 4668 "\"1\"", 4669 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 4670 EXPECT_EQ("R\"(\\x\\x00)\"\n", 4671 format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7))); 4672} 4673 4674TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 4675 verifyFormat("void f() {\n" 4676 " return g() {}\n" 4677 " void h() {}"); 4678 verifyFormat("if (foo)\n" 4679 " return { forgot_closing_brace();\n" 4680 "test();"); 4681 verifyFormat("int a[] = { void forgot_closing_brace() { f();\n" 4682 "g();\n" 4683 "}"); 4684} 4685 4686TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 4687 verifyFormat("class X {\n" 4688 " void f() {\n" 4689 " }\n" 4690 "};", 4691 getLLVMStyleWithColumns(12)); 4692} 4693 4694TEST_F(FormatTest, ConfigurableIndentWidth) { 4695 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 4696 EightIndent.IndentWidth = 8; 4697 verifyFormat("void f() {\n" 4698 " someFunction();\n" 4699 " if (true) {\n" 4700 " f();\n" 4701 " }\n" 4702 "}", 4703 EightIndent); 4704 verifyFormat("class X {\n" 4705 " void f() {\n" 4706 " }\n" 4707 "};", 4708 EightIndent); 4709 verifyFormat("int x[] = {\n" 4710 " call(),\n" 4711 " call(),\n" 4712 "};", 4713 EightIndent); 4714} 4715 4716TEST_F(FormatTest, ConfigurableUseOfTab) { 4717 FormatStyle Tab = getLLVMStyleWithColumns(42); 4718 Tab.IndentWidth = 8; 4719 Tab.UseTab = true; 4720 Tab.AlignEscapedNewlinesLeft = true; 4721 verifyFormat("class X {\n" 4722 "\tvoid f() {\n" 4723 "\t\tsomeFunction(parameter1,\n" 4724 "\t\t\t parameter2);\n" 4725 "\t}\n" 4726 "};", 4727 Tab); 4728 verifyFormat("#define A \\\n" 4729 "\tvoid f() { \\\n" 4730 "\t\tsomeFunction( \\\n" 4731 "\t\t parameter1, \\\n" 4732 "\t\t parameter2); \\\n" 4733 "\t}", 4734 Tab); 4735 4736 4737 // FIXME: To correctly count mixed whitespace we need to 4738 // also correctly count mixed whitespace in front of the comment. 4739 // 4740 // EXPECT_EQ("/*\n" 4741 // "\t a\t\tcomment\n" 4742 // "\t in multiple lines\n" 4743 // " */", 4744 // format(" /*\t \t \n" 4745 // " \t \t a\t\tcomment\t \t\n" 4746 // " \t \t in multiple lines\t\n" 4747 // " \t */", 4748 // Tab)); 4749 // Tab.UseTab = false; 4750 // EXPECT_EQ("/*\n" 4751 // " a\t\tcomment\n" 4752 // " in multiple lines\n" 4753 // " */", 4754 // format(" /*\t \t \n" 4755 // " \t \t a\t\tcomment\t \t\n" 4756 // " \t \t in multiple lines\t\n" 4757 // " \t */", 4758 // Tab)); 4759 // EXPECT_EQ("/* some\n" 4760 // " comment */", 4761 // format(" \t \t /* some\n" 4762 // " \t \t comment */", 4763 // Tab)); 4764 4765 EXPECT_EQ("{\n" 4766 " /*\n" 4767 " * Comment\n" 4768 " */\n" 4769 " int i;\n" 4770 "}", 4771 format("{\n" 4772 "\t/*\n" 4773 "\t * Comment\n" 4774 "\t */\n" 4775 "\t int i;\n" 4776 "}")); 4777} 4778 4779TEST_F(FormatTest, LinuxBraceBreaking) { 4780 FormatStyle BreakBeforeBrace = getLLVMStyle(); 4781 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux; 4782 verifyFormat("namespace a\n" 4783 "{\n" 4784 "class A\n" 4785 "{\n" 4786 " void f()\n" 4787 " {\n" 4788 " if (true) {\n" 4789 " a();\n" 4790 " b();\n" 4791 " }\n" 4792 " }\n" 4793 " void g()\n" 4794 " {\n" 4795 " return;\n" 4796 " }\n" 4797 "}\n" 4798 "}", 4799 BreakBeforeBrace); 4800} 4801 4802TEST_F(FormatTest, StroustrupBraceBreaking) { 4803 FormatStyle BreakBeforeBrace = getLLVMStyle(); 4804 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4805 verifyFormat("namespace a {\n" 4806 "class A {\n" 4807 " void f()\n" 4808 " {\n" 4809 " if (true) {\n" 4810 " a();\n" 4811 " b();\n" 4812 " }\n" 4813 " }\n" 4814 " void g()\n" 4815 " {\n" 4816 " return;\n" 4817 " }\n" 4818 "}\n" 4819 "}", 4820 BreakBeforeBrace); 4821} 4822 4823bool allStylesEqual(ArrayRef<FormatStyle> Styles) { 4824 for (size_t i = 1; i < Styles.size(); ++i) 4825 if (!(Styles[0] == Styles[i])) 4826 return false; 4827 return true; 4828} 4829 4830TEST_F(FormatTest, GetsPredefinedStyleByName) { 4831 FormatStyle Styles[3]; 4832 4833 Styles[0] = getLLVMStyle(); 4834 EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1])); 4835 EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2])); 4836 EXPECT_TRUE(allStylesEqual(Styles)); 4837 4838 Styles[0] = getGoogleStyle(); 4839 EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1])); 4840 EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2])); 4841 EXPECT_TRUE(allStylesEqual(Styles)); 4842 4843 Styles[0] = getChromiumStyle(); 4844 EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1])); 4845 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2])); 4846 EXPECT_TRUE(allStylesEqual(Styles)); 4847 4848 Styles[0] = getMozillaStyle(); 4849 EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1])); 4850 EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2])); 4851 EXPECT_TRUE(allStylesEqual(Styles)); 4852 4853 EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0])); 4854} 4855 4856TEST_F(FormatTest, ParsesConfiguration) { 4857 FormatStyle Style = {}; 4858#define CHECK_PARSE(TEXT, FIELD, VALUE) \ 4859 EXPECT_NE(VALUE, Style.FIELD); \ 4860 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 4861 EXPECT_EQ(VALUE, Style.FIELD) 4862 4863#define CHECK_PARSE_BOOL(FIELD) \ 4864 Style.FIELD = false; \ 4865 EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \ 4866 EXPECT_TRUE(Style.FIELD); \ 4867 EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \ 4868 EXPECT_FALSE(Style.FIELD); 4869 4870 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 4871 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 4872 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 4873 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 4874 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 4875 CHECK_PARSE_BOOL(BinPackParameters); 4876 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 4877 CHECK_PARSE_BOOL(DerivePointerBinding); 4878 CHECK_PARSE_BOOL(IndentCaseLabels); 4879 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 4880 CHECK_PARSE_BOOL(PointerBindsToType); 4881 CHECK_PARSE_BOOL(SpacesInBracedLists); 4882 CHECK_PARSE_BOOL(UseTab); 4883 4884 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 4885 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 4886 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 4887 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 4888 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 4889 PenaltyReturnTypeOnItsOwnLine, 1234u); 4890 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 4891 SpacesBeforeTrailingComments, 1234u); 4892 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 4893 4894 Style.Standard = FormatStyle::LS_Auto; 4895 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 4896 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 4897 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 4898 4899 Style.ColumnLimit = 123; 4900 FormatStyle BaseStyle = getLLVMStyle(); 4901 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 4902 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 4903 4904 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4905 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 4906 FormatStyle::BS_Attach); 4907 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 4908 FormatStyle::BS_Linux); 4909 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 4910 FormatStyle::BS_Stroustrup); 4911 4912#undef CHECK_PARSE 4913#undef CHECK_PARSE_BOOL 4914} 4915 4916TEST_F(FormatTest, ConfigurationRoundTripTest) { 4917 FormatStyle Style = getLLVMStyle(); 4918 std::string YAML = configurationAsText(Style); 4919 FormatStyle ParsedStyle = {}; 4920 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 4921 EXPECT_EQ(Style, ParsedStyle); 4922} 4923 4924TEST_F(FormatTest, WorksFor8bitEncodings) { 4925 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 4926 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 4927 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 4928 "\"\xef\xee\xf0\xf3...\"", 4929 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 4930 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 4931 "\xef\xee\xf0\xf3...\"", 4932 getLLVMStyleWithColumns(12))); 4933} 4934 4935// FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 4936#if !defined(_MSC_VER) 4937 4938TEST_F(FormatTest, CountsUTF8CharactersProperly) { 4939 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 4940 getLLVMStyleWithColumns(35)); 4941 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 4942 getLLVMStyleWithColumns(21)); 4943 verifyFormat("// Однажды в студёную зимнюю пору...", 4944 getLLVMStyleWithColumns(36)); 4945 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", 4946 getLLVMStyleWithColumns(22)); 4947 verifyFormat("/* Однажды в студёную зимнюю пору... */", 4948 getLLVMStyleWithColumns(39)); 4949 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 4950 getLLVMStyleWithColumns(25)); 4951} 4952 4953TEST_F(FormatTest, SplitsUTF8Strings) { 4954 EXPECT_EQ( 4955 "\"Однажды, в \"\n" 4956 "\"студёную \"\n" 4957 "\"зимнюю \"\n" 4958 "\"пору,\"", 4959 format("\"Однажды, в студёную зимнюю пору,\"", 4960 getLLVMStyleWithColumns(13))); 4961 EXPECT_EQ("\"一 二 三 四 \"\n" 4962 "\"五 六 七 八 \"\n" 4963 "\"九 十\"", 4964 format("\"一 二 三 四 五 六 七 八 九 十\"", 4965 getLLVMStyleWithColumns(10))); 4966} 4967 4968TEST_F(FormatTest, SplitsUTF8LineComments) { 4969 EXPECT_EQ("// Я из лесу\n" 4970 "// вышел; был\n" 4971 "// сильный\n" 4972 "// мороз.", 4973 format("// Я из лесу вышел; был сильный мороз.", 4974 getLLVMStyleWithColumns(13))); 4975 EXPECT_EQ("// 一二三\n" 4976 "// 四五六七\n" 4977 "// 八\n" 4978 "// 九 十", 4979 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(6))); 4980} 4981 4982TEST_F(FormatTest, SplitsUTF8BlockComments) { 4983 EXPECT_EQ("/* Гляжу,\n" 4984 " * поднимается\n" 4985 " * медленно в\n" 4986 " * гору\n" 4987 " * Лошадка,\n" 4988 " * везущая\n" 4989 " * хворосту\n" 4990 " * воз. */", 4991 format("/* Гляжу, поднимается медленно в гору\n" 4992 " * Лошадка, везущая хворосту воз. */", 4993 getLLVMStyleWithColumns(13))); 4994 EXPECT_EQ("/* 一二三\n" 4995 " * 四五六七\n" 4996 " * 八\n" 4997 " * 九 十\n" 4998 " */", 4999 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(6))); 5000 EXPECT_EQ("/* \n" 5001 " * \n" 5002 " * - */", 5003 format("/* - */", getLLVMStyleWithColumns(12))); 5004} 5005 5006#endif 5007 5008} // end namespace tooling 5009} // end namespace clang 5010