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