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