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