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