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 17 namespace clang { 18 namespace format { 19 20 class FormatTest : public ::testing::Test { 21 protected: 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 101 TEST_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 113 TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { 114 EXPECT_EQ(";", format(";")); 115 } 116 117 TEST_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 124 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 125 EXPECT_EQ("int i;", format("int\ni;")); 126 } 127 128 TEST_F(FormatTest, FormatsNestedBlockStatements) { 129 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 130 } 131 132 TEST_F(FormatTest, FormatsNestedCall) { 133 verifyFormat("Method(f1, f2(f3));"); 134 verifyFormat("Method(f1(f2, f3()));"); 135 verifyFormat("Method(f1(f2, (f3())));"); 136 } 137 138 TEST_F(FormatTest, NestedNameSpecifiers) { 139 verifyFormat("vector< ::Type> v;"); 140 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 141 } 142 143 TEST_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 158 TEST_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 166 TEST_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 188 TEST_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 195 TEST_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 242 TEST_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 259 TEST_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 306 TEST_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 329 TEST_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 355 TEST_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 365 TEST_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 427 TEST_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 436 TEST_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 446 TEST_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 455 TEST_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 550 TEST_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 570 TEST_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 729 TEST_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 751 TEST_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 758 TEST_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 789 TEST_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 839 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 840 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 842 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 844 } 845 846 TEST_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 901 TEST_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 931 TEST_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 954 TEST_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 963 TEST_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 1114 TEST_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 1151 TEST_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 1220 TEST_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 1362 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1363 verifyFormat("class A {};"); 1364 } 1365 1366 TEST_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 1382 TEST_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 1404 TEST_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 1428 TEST_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 1434 TEST_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 1451 TEST_F(FormatTest, FormatsBitfields) { 1452 verifyFormat("struct Bitfields {\n" 1453 " unsigned sClass : 8;\n" 1454 " unsigned ValueKind : 2;\n" 1455 "};"); 1456 } 1457 1458 TEST_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 1484 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1485 1486 TEST_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 1496 TEST_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 1523 TEST_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 1535 TEST_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 1572 TEST_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 1590 TEST_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 1639 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1640 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1641 " \\\n" 1642 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1643 } 1644 1645 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1646 verifyFormat( 1647 "virtual void write(ELFWriter *writerrr,\n" 1648 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1649 } 1650 1651 TEST_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 1658 TEST_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 1665 TEST_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 1671 TEST_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 1685 TEST_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 1701 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1702 1703 TEST_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 1713 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1714 1715 TEST_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 1740 TEST_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 1747 TEST_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 1766 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1767 verifyFormat("#define A (1)"); 1768 } 1769 1770 TEST_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 1795 TEST_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 1806 TEST_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 1842 TEST_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 1950 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 1951 verifyFormat("#define A \\\n" 1952 " f({ \\\n" 1953 " g(); \\\n" 1954 " });", getLLVMStyleWithColumns(11)); 1955 } 1956 1957 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 1958 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 1959 } 1960 1961 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 1962 verifyFormat("{\n { a #c; }\n}"); 1963 } 1964 1965 TEST_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 1972 TEST_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 1979 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) { 1980 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 1981 } 1982 1983 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 1984 verifyFormat("#define A \\\n" 1985 " int v( \\\n" 1986 " a); \\\n" 1987 " int i;", 1988 getLLVMStyleWithColumns(11)); 1989 } 1990 1991 TEST_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 2007 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2008 EXPECT_EQ("int\n" 2009 "#define A\n" 2010 " a;", 2011 format("int\n#define A\na;")); 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 2024 TEST_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 2044 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2045 EXPECT_EQ("SOME_MACRO { int i; }\n" 2046 "int i;", 2047 format(" SOME_MACRO {int i;} int i;")); 2048 } 2049 2050 TEST_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 2061 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2062 EXPECT_EQ("{}", format("{}")); 2063 verifyFormat("enum E {};"); 2064 verifyFormat("enum E {}"); 2065 } 2066 2067 //===----------------------------------------------------------------------===// 2068 // Line break tests. 2069 //===----------------------------------------------------------------------===// 2070 2071 TEST_F(FormatTest, FormatsAwesomeMethodCall) { 2072 verifyFormat( 2073 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2074 " parameter, parameter, parameter)),\n" 2075 " SecondLongCall(parameter));"); 2076 } 2077 2078 TEST_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 2099 TEST_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 2132 TEST_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 2167 TEST_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 2242 TEST_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 2321 TEST_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 2332 TEST_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 "Aaaaaaaaaaaaaaaaaaaaaaaa\n" 2341 " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); 2342 2343 // 3) break after (. 2344 verifyFormat( 2345 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 2346 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); 2347 2348 // 4) break before after nested name specifiers. 2349 verifyFormat( 2350 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2351 " SomeClasssssssssssssssssssssssssssssssssssssss::\n" 2352 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); 2353 2354 // However, there are exceptions, if a sufficient amount of lines can be 2355 // saved. 2356 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 2357 // more adjusting. 2358 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 2359 " Cccccccccccccc cccccccccc,\n" 2360 " Cccccccccccccc cccccccccc,\n" 2361 " Cccccccccccccc cccccccccc,\n" 2362 " Cccccccccccccc cccccccccc);"); 2363 verifyFormat( 2364 "Aaaaaaaaaaaaaaaaaa\n" 2365 " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2366 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2367 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 2368 verifyFormat( 2369 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 2370 " Cccccccccccccc cccccccccc,\n" 2371 " Cccccccccccccc cccccccccc,\n" 2372 " Cccccccccccccc cccccccccc,\n" 2373 " Cccccccccccccc cccccccccc,\n" 2374 " Cccccccccccccc cccccccccc,\n" 2375 " Cccccccccccccc cccccccccc);"); 2376 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 2377 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2378 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2379 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2380 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 2381 2382 // Break after multi-line parameters. 2383 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2386 " bbbb bbbb);"); 2387 } 2388 2389 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 2390 verifyFormat("void someLongFunction(\n" 2391 " int someLongParameter) const {}", 2392 getLLVMStyleWithColumns(46)); 2393 FormatStyle Style = getGoogleStyle(); 2394 Style.ColumnLimit = 47; 2395 verifyFormat("void\n" 2396 "someLongFunction(int someLongParameter) const {\n}", 2397 getLLVMStyleWithColumns(47)); 2398 verifyFormat("void someLongFunction(\n" 2399 " int someLongParameter) const {}", 2400 Style); 2401 verifyFormat("LoooooongReturnType\n" 2402 "someLoooooooongFunction() const {}", 2403 getLLVMStyleWithColumns(47)); 2404 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 2405 " const {}", 2406 Style); 2407 2408 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2409 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 2410 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 2411 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 2412 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 2413 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 2414 2415 verifyFormat( 2416 "void aaaaaaaaaaaaaaaaaa()\n" 2417 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 2418 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 2419 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2420 " __attribute__((unused));"); 2421 verifyFormat( 2422 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2423 " GUARDED_BY(aaaaaaaaaaaa);"); 2424 } 2425 2426 2427 TEST_F(FormatTest, BreaksDesireably) { 2428 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 2429 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 2430 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 2431 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2433 "}"); 2434 2435 verifyFormat( 2436 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2438 2439 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2442 2443 verifyFormat( 2444 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 2446 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 2448 2449 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2450 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2451 2452 verifyFormat( 2453 "void f() {\n" 2454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 2455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2456 "}"); 2457 verifyFormat( 2458 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2460 verifyFormat( 2461 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2463 verifyFormat( 2464 "aaaaaaaaaaaaaaaaa(\n" 2465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2467 2468 // This test case breaks on an incorrect memoization, i.e. an optimization not 2469 // taking into account the StopAt value. 2470 verifyFormat( 2471 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 2472 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 2473 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 2474 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2475 2476 verifyFormat("{\n {\n {\n" 2477 " Annotation.SpaceRequiredBefore =\n" 2478 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 2479 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 2480 " }\n }\n}"); 2481 2482 // Break on an outer level if there was a break on an inner level. 2483 EXPECT_EQ("f(g(h(a, // comment\n" 2484 " b, c),\n" 2485 " d, e),\n" 2486 " x, y);", 2487 format("f(g(h(a, // comment\n" 2488 " b, c), d, e), x, y);")); 2489 } 2490 2491 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 2492 FormatStyle NoBinPacking = getGoogleStyle(); 2493 NoBinPacking.BinPackParameters = false; 2494 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 2495 " aaaaaaaaaaaaaaaaaaaa,\n" 2496 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 2497 NoBinPacking); 2498 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 2499 " aaaaaaaaaaaaa,\n" 2500 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 2501 NoBinPacking); 2502 verifyFormat( 2503 "aaaaaaaa(aaaaaaaaaaaaa,\n" 2504 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 2506 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 2508 NoBinPacking); 2509 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 2510 " .aaaaaaaaaaaaaaaaaa();", 2511 NoBinPacking); 2512 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2513 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", 2514 NoBinPacking); 2515 2516 verifyFormat( 2517 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2518 " aaaaaaaaaaaa,\n" 2519 " aaaaaaaaaaaa);", 2520 NoBinPacking); 2521 verifyFormat( 2522 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 2523 " ddddddddddddddddddddddddddddd),\n" 2524 " test);", 2525 NoBinPacking); 2526 2527 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 2528 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 2529 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 2530 NoBinPacking); 2531 verifyFormat("a(\"a\"\n" 2532 " \"a\",\n" 2533 " a);"); 2534 2535 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 2536 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 2537 " aaaaaaaaa,\n" 2538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2539 NoBinPacking); 2540 verifyFormat( 2541 "void f() {\n" 2542 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 2543 " .aaaaaaa();\n" 2544 "}", 2545 NoBinPacking); 2546 verifyFormat( 2547 "template <class SomeType, class SomeOtherType>\n" 2548 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 2549 NoBinPacking); 2550 } 2551 2552 TEST_F(FormatTest, FormatsBuilderPattern) { 2553 verifyFormat( 2554 "return llvm::StringSwitch<Reference::Kind>(name)\n" 2555 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 2556 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" 2557 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" 2558 " .Default(ORDER_TEXT);\n"); 2559 2560 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 2561 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 2562 verifyFormat( 2563 "aaaaaaa->aaaaaaa\n" 2564 " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2565 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 2566 verifyFormat( 2567 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 2568 " aaaaaaaaaaaaaa);"); 2569 verifyFormat( 2570 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" 2571 " ->aaaaaaaaaaaaaaaa(\n" 2572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2573 " ->aaaaaaaaaaaaaaaaa();"); 2574 } 2575 2576 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 2577 verifyFormat( 2578 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2579 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 2580 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 2581 " ccccccccccccccccccccccccc) {\n}"); 2582 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 2583 " ccccccccccccccccccccccccc) {\n}"); 2584 verifyFormat( 2585 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 2586 " ccccccccccccccccccccccccc) {\n}"); 2587 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 2588 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 2589 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 2590 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 2591 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 2592 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 2593 " aaaaaaaaaaaaaaa != aa) {\n}"); 2594 } 2595 2596 TEST_F(FormatTest, BreaksAfterAssignments) { 2597 verifyFormat( 2598 "unsigned Cost =\n" 2599 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 2600 " SI->getPointerAddressSpaceee());\n"); 2601 verifyFormat( 2602 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 2603 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 2604 2605 verifyFormat( 2606 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 2607 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 2608 verifyFormat("unsigned OriginalStartColumn =\n" 2609 " SourceMgr.getSpellingColumnNumber(\n" 2610 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 2611 " 1;"); 2612 } 2613 2614 TEST_F(FormatTest, AlignsAfterAssignments) { 2615 verifyFormat( 2616 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2617 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2618 verifyFormat( 2619 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2620 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2621 verifyFormat( 2622 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2623 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2624 verifyFormat( 2625 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2626 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 2627 verifyFormat("double LooooooooooooooooooooooooongResult =\n" 2628 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 2629 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 2630 } 2631 2632 TEST_F(FormatTest, AlignsAfterReturn) { 2633 verifyFormat( 2634 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2635 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 2636 verifyFormat( 2637 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2638 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 2639 verifyFormat( 2640 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 2641 " aaaaaaaaaaaaaaaaaaaaaa();"); 2642 verifyFormat( 2643 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 2644 " aaaaaaaaaaaaaaaaaaaaaa());"); 2645 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2647 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 2649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2650 } 2651 2652 TEST_F(FormatTest, BreaksConditionalExpressions) { 2653 verifyFormat( 2654 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 2655 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2656 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2657 verifyFormat( 2658 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2659 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2660 verifyFormat( 2661 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 2662 " : aaaaaaaaaaaaa);"); 2663 verifyFormat( 2664 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2665 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2666 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2667 " aaaaaaaaaaaaa);"); 2668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2669 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2671 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2672 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2673 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2675 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2677 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2679 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2680 2681 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2682 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2683 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2684 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 2685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2686 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2687 " : aaaaaaaaaaaaaaaa;"); 2688 verifyFormat( 2689 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2690 " ? aaaaaaaaaaaaaaa\n" 2691 " : aaaaaaaaaaaaaaa;"); 2692 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 2693 " aaaaaaaaa\n" 2694 " ? b\n" 2695 " : c);"); 2696 verifyFormat( 2697 "unsigned Indent =\n" 2698 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 2699 " ? IndentForLevel[TheLine.Level]\n" 2700 " : TheLine * 2,\n" 2701 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 2702 getLLVMStyleWithColumns(70)); 2703 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 2704 " ? aaaaaaaaaaaaaaa\n" 2705 " : bbbbbbbbbbbbbbb //\n" 2706 " ? ccccccccccccccc\n" 2707 " : ddddddddddddddd;"); 2708 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 2709 " ? aaaaaaaaaaaaaaa\n" 2710 " : (bbbbbbbbbbbbbbb //\n" 2711 " ? ccccccccccccccc\n" 2712 " : ddddddddddddddd);"); 2713 2714 FormatStyle NoBinPacking = getLLVMStyle(); 2715 NoBinPacking.BinPackParameters = false; 2716 verifyFormat( 2717 "void f() {\n" 2718 " g(aaa,\n" 2719 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 2720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2721 " ? aaaaaaaaaaaaaaa\n" 2722 " : aaaaaaaaaaaaaaa);\n" 2723 "}", 2724 NoBinPacking); 2725 } 2726 2727 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 2728 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 2729 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 2730 verifyFormat("bool a = true, b = false;"); 2731 2732 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 2734 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 2735 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 2736 verifyFormat( 2737 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2738 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 2739 " d = e && f;"); 2740 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 2741 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 2742 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 2743 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 2744 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 2745 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 2746 // FIXME: If multiple variables are defined, the "*" needs to move to the new 2747 // line. Also fix indent for breaking after the type, this looks bad. 2748 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 2750 " *b = bbbbbbbbbbbbbbbbbbb;"); 2751 2752 // Not ideal, but pointer-with-type does not allow much here. 2753 verifyGoogleFormat( 2754 "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 2755 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;"); 2756 } 2757 2758 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 2759 verifyFormat("arr[foo ? bar : baz];"); 2760 verifyFormat("f()[foo ? bar : baz];"); 2761 verifyFormat("(a + b)[foo ? bar : baz];"); 2762 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 2763 } 2764 2765 TEST_F(FormatTest, AlignsStringLiterals) { 2766 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 2767 " \"short literal\");"); 2768 verifyFormat( 2769 "looooooooooooooooooooooooongFunction(\n" 2770 " \"short literal\"\n" 2771 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 2772 verifyFormat("someFunction(\"Always break between multi-line\"\n" 2773 " \" string literals\",\n" 2774 " and, other, parameters);"); 2775 EXPECT_EQ("fun + \"1243\" /* comment */\n" 2776 " \"5678\";", 2777 format("fun + \"1243\" /* comment */\n" 2778 " \"5678\";", 2779 getLLVMStyleWithColumns(28))); 2780 EXPECT_EQ( 2781 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 2782 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 2783 " \"aaaaaaaaaaaaaaaa\";", 2784 format("aaaaaa =" 2785 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 2786 "aaaaaaaaaaaaaaaaaaaaa\" " 2787 "\"aaaaaaaaaaaaaaaa\";")); 2788 verifyFormat("a = a + \"a\"\n" 2789 " \"a\"\n" 2790 " \"a\";"); 2791 verifyFormat("f(\"a\", \"b\"\n" 2792 " \"c\");"); 2793 2794 verifyFormat( 2795 "#define LL_FORMAT \"ll\"\n" 2796 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 2797 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 2798 2799 verifyFormat("#define A(X) \\\n" 2800 " \"aaaaa\" #X \"bbbbbb\" \\\n" 2801 " \"ccccc\"", 2802 getLLVMStyleWithColumns(23)); 2803 verifyFormat("#define A \"def\"\n" 2804 "f(\"abc\" A \"ghi\"\n" 2805 " \"jkl\");"); 2806 } 2807 2808 TEST_F(FormatTest, AlignsPipes) { 2809 verifyFormat( 2810 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2811 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2813 verifyFormat( 2814 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 2815 " << aaaaaaaaaaaaaaaaaaaa;"); 2816 verifyFormat( 2817 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2818 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2819 verifyFormat( 2820 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 2821 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 2822 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 2823 verifyFormat( 2824 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2825 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2826 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2827 2828 verifyFormat("return out << \"somepacket = {\\n\"\n" 2829 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 2830 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 2831 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 2832 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 2833 " << \"}\";"); 2834 2835 verifyFormat( 2836 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 2837 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 2838 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 2839 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 2840 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 2841 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 2842 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 2843 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaa: \"\n" 2844 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2845 2846 verifyFormat( 2847 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2848 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 2849 } 2850 2851 TEST_F(FormatTest, UnderstandsEquals) { 2852 verifyFormat( 2853 "aaaaaaaaaaaaaaaaa =\n" 2854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2855 verifyFormat( 2856 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2858 verifyFormat( 2859 "if (a) {\n" 2860 " f();\n" 2861 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2863 "}"); 2864 2865 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2866 " 100000000 + 10000000) {\n}"); 2867 } 2868 2869 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 2870 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 2871 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 2872 2873 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 2874 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 2875 2876 verifyFormat( 2877 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 2878 " Parameter2);"); 2879 2880 verifyFormat( 2881 "ShortObject->shortFunction(\n" 2882 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 2883 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 2884 2885 verifyFormat("loooooooooooooongFunction(\n" 2886 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 2887 2888 verifyFormat( 2889 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 2890 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 2891 2892 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 2893 " .WillRepeatedly(Return(SomeValue));"); 2894 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" 2895 " .insert(ccccccccccccccccccccccc);"); 2896 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n" 2898 " aaaaaaaaaaaaaaaaaaaaa);"); 2899 verifyFormat( 2900 "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2901 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2902 " .aaaaaaaaaaaaaaa(\n" 2903 " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2904 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 2905 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2906 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2907 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2908 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 2909 "}"); 2910 2911 // Here, it is not necessary to wrap at "." or "->". 2912 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 2913 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2914 verifyFormat( 2915 "aaaaaaaaaaa->aaaaaaaaa(\n" 2916 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2917 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 2918 2919 verifyFormat( 2920 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 2922 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 2923 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 2924 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 2925 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 2926 2927 // FIXME: Should we break before .a()? 2928 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); 2930 2931 FormatStyle NoBinPacking = getLLVMStyle(); 2932 NoBinPacking.BinPackParameters = false; 2933 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 2934 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 2935 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 2936 " aaaaaaaaaaaaaaaaaaa,\n" 2937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2938 NoBinPacking); 2939 } 2940 2941 TEST_F(FormatTest, WrapsTemplateDeclarations) { 2942 verifyFormat("template <typename T>\n" 2943 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 2944 verifyFormat( 2945 "template <typename T>\n" 2946 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 2947 verifyFormat("template <typename T>\n" 2948 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 2949 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 2950 verifyFormat( 2951 "template <typename T>\n" 2952 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 2953 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 2954 verifyFormat( 2955 "template <typename T>\n" 2956 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 2957 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 2958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2959 verifyFormat("template <typename T>\n" 2960 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2961 " int aaaaaaaaaaaaaaaaaaaaaa);"); 2962 verifyFormat( 2963 "template <typename T1, typename T2 = char, typename T3 = char,\n" 2964 " typename T4 = char>\n" 2965 "void f();"); 2966 verifyFormat( 2967 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 2968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2969 2970 verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 2971 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 2972 2973 verifyFormat("template <typename T> class C {};"); 2974 verifyFormat("template <typename T> void f();"); 2975 verifyFormat("template <typename T> void f() {}"); 2976 2977 FormatStyle AlwaysBreak = getLLVMStyle(); 2978 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 2979 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 2980 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 2981 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 2982 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2983 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 2984 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 2985 } 2986 2987 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 2988 verifyFormat( 2989 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 2991 verifyFormat( 2992 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2993 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 2995 2996 // FIXME: Should we have the extra indent after the second break? 2997 verifyFormat( 2998 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 2999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 3000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3001 3002 verifyFormat( 3003 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 3004 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 3005 3006 // Breaking at nested name specifiers is generally not desirable. 3007 verifyFormat( 3008 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3009 " aaaaaaaaaaaaaaaaaaaaaaa);"); 3010 3011 verifyFormat( 3012 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 3013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3014 " aaaaaaaaaaaaaaaaaaaaa);", 3015 getLLVMStyleWithColumns(74)); 3016 3017 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 3018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3019 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3020 } 3021 3022 TEST_F(FormatTest, UnderstandsTemplateParameters) { 3023 verifyFormat("A<int> a;"); 3024 verifyFormat("A<A<A<int> > > a;"); 3025 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 3026 verifyFormat("bool x = a < 1 || 2 > a;"); 3027 verifyFormat("bool x = 5 < f<int>();"); 3028 verifyFormat("bool x = f<int>() > 5;"); 3029 verifyFormat("bool x = 5 < a<int>::x;"); 3030 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 3031 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 3032 3033 verifyGoogleFormat("A<A<int>> a;"); 3034 verifyGoogleFormat("A<A<A<int>>> a;"); 3035 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 3036 verifyGoogleFormat("A<A<int> > a;"); 3037 verifyGoogleFormat("A<A<A<int> > > a;"); 3038 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 3039 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 3040 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 3041 3042 verifyFormat("test >> a >> b;"); 3043 verifyFormat("test << a >> b;"); 3044 3045 verifyFormat("f<int>();"); 3046 verifyFormat("template <typename T> void f() {}"); 3047 3048 // Not template parameters. 3049 verifyFormat("return a < b && c > d;"); 3050 verifyFormat("void f() {\n" 3051 " while (a < b && c > d) {\n" 3052 " }\n" 3053 "}"); 3054 } 3055 3056 TEST_F(FormatTest, UnderstandsBinaryOperators) { 3057 verifyFormat("COMPARE(a, ==, b);"); 3058 } 3059 3060 TEST_F(FormatTest, UnderstandsPointersToMembers) { 3061 verifyFormat("int A::*x;"); 3062 verifyFormat("int (S::*func)(void *);"); 3063 verifyFormat("void f() { int (S::*func)(void *); }"); 3064 verifyFormat("typedef bool *(Class::*Member)() const;"); 3065 verifyFormat("void f() {\n" 3066 " (a->*f)();\n" 3067 " a->*x;\n" 3068 " (a.*f)();\n" 3069 " ((*a).*f)();\n" 3070 " a.*x;\n" 3071 "}"); 3072 FormatStyle Style = getLLVMStyle(); 3073 Style.PointerBindsToType = true; 3074 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 3075 } 3076 3077 TEST_F(FormatTest, UnderstandsUnaryOperators) { 3078 verifyFormat("int a = -2;"); 3079 verifyFormat("f(-1, -2, -3);"); 3080 verifyFormat("a[-1] = 5;"); 3081 verifyFormat("int a = 5 + -2;"); 3082 verifyFormat("if (i == -1) {\n}"); 3083 verifyFormat("if (i != -1) {\n}"); 3084 verifyFormat("if (i > -1) {\n}"); 3085 verifyFormat("if (i < -1) {\n}"); 3086 verifyFormat("++(a->f());"); 3087 verifyFormat("--(a->f());"); 3088 verifyFormat("(a->f())++;"); 3089 verifyFormat("a[42]++;"); 3090 verifyFormat("if (!(a->f())) {\n}"); 3091 3092 verifyFormat("a-- > b;"); 3093 verifyFormat("b ? -a : c;"); 3094 verifyFormat("n * sizeof char16;"); 3095 verifyFormat("n * alignof char16;"); 3096 verifyFormat("sizeof(char);"); 3097 verifyFormat("alignof(char);"); 3098 3099 verifyFormat("return -1;"); 3100 verifyFormat("switch (a) {\n" 3101 "case -1:\n" 3102 " break;\n" 3103 "}"); 3104 verifyFormat("#define X -1"); 3105 verifyFormat("#define X -kConstant"); 3106 3107 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 3108 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 3109 3110 verifyFormat("int a = /* confusing comment */ -1;"); 3111 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 3112 verifyFormat("int a = i /* confusing comment */++;"); 3113 } 3114 3115 TEST_F(FormatTest, UndestandsOverloadedOperators) { 3116 verifyFormat("bool operator<();"); 3117 verifyFormat("bool operator>();"); 3118 verifyFormat("bool operator=();"); 3119 verifyFormat("bool operator==();"); 3120 verifyFormat("bool operator!=();"); 3121 verifyFormat("int operator+();"); 3122 verifyFormat("int operator++();"); 3123 verifyFormat("bool operator();"); 3124 verifyFormat("bool operator()();"); 3125 verifyFormat("bool operator[]();"); 3126 verifyFormat("operator bool();"); 3127 verifyFormat("operator int();"); 3128 verifyFormat("operator void *();"); 3129 verifyFormat("operator SomeType<int>();"); 3130 verifyFormat("operator SomeType<int, int>();"); 3131 verifyFormat("operator SomeType<SomeType<int> >();"); 3132 verifyFormat("void *operator new(std::size_t size);"); 3133 verifyFormat("void *operator new[](std::size_t size);"); 3134 verifyFormat("void operator delete(void *ptr);"); 3135 verifyFormat("void operator delete[](void *ptr);"); 3136 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 3137 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 3138 3139 verifyFormat( 3140 "ostream &operator<<(ostream &OutputStream,\n" 3141 " SomeReallyLongType WithSomeReallyLongValue);"); 3142 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 3143 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 3144 " return left.group < right.group;\n" 3145 "}"); 3146 verifyFormat("SomeType &operator=(const SomeType &S);"); 3147 3148 verifyGoogleFormat("operator void*();"); 3149 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 3150 } 3151 3152 TEST_F(FormatTest, UnderstandsNewAndDelete) { 3153 verifyFormat("void f() {\n" 3154 " A *a = new A;\n" 3155 " A *a = new (placement) A;\n" 3156 " delete a;\n" 3157 " delete (A *)a;\n" 3158 "}"); 3159 } 3160 3161 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 3162 verifyFormat("int *f(int *a) {}"); 3163 verifyFormat("int main(int argc, char **argv) {}"); 3164 verifyFormat("Test::Test(int b) : a(b * b) {}"); 3165 verifyIndependentOfContext("f(a, *a);"); 3166 verifyFormat("void g() { f(*a); }"); 3167 verifyIndependentOfContext("int a = b * 10;"); 3168 verifyIndependentOfContext("int a = 10 * b;"); 3169 verifyIndependentOfContext("int a = b * c;"); 3170 verifyIndependentOfContext("int a += b * c;"); 3171 verifyIndependentOfContext("int a -= b * c;"); 3172 verifyIndependentOfContext("int a *= b * c;"); 3173 verifyIndependentOfContext("int a /= b * c;"); 3174 verifyIndependentOfContext("int a = *b;"); 3175 verifyIndependentOfContext("int a = *b * c;"); 3176 verifyIndependentOfContext("int a = b * *c;"); 3177 verifyIndependentOfContext("return 10 * b;"); 3178 verifyIndependentOfContext("return *b * *c;"); 3179 verifyIndependentOfContext("return a & ~b;"); 3180 verifyIndependentOfContext("f(b ? *c : *d);"); 3181 verifyIndependentOfContext("int a = b ? *c : *d;"); 3182 verifyIndependentOfContext("*b = a;"); 3183 verifyIndependentOfContext("a * ~b;"); 3184 verifyIndependentOfContext("a * !b;"); 3185 verifyIndependentOfContext("a * +b;"); 3186 verifyIndependentOfContext("a * -b;"); 3187 verifyIndependentOfContext("a * ++b;"); 3188 verifyIndependentOfContext("a * --b;"); 3189 verifyIndependentOfContext("a[4] * b;"); 3190 verifyIndependentOfContext("a[a * a] = 1;"); 3191 verifyIndependentOfContext("f() * b;"); 3192 verifyIndependentOfContext("a * [self dostuff];"); 3193 verifyIndependentOfContext("int x = a * (a + b);"); 3194 verifyIndependentOfContext("(a *)(a + b);"); 3195 verifyIndependentOfContext("int *pa = (int *)&a;"); 3196 verifyIndependentOfContext("return sizeof(int **);"); 3197 verifyIndependentOfContext("return sizeof(int ******);"); 3198 verifyIndependentOfContext("return (int **&)a;"); 3199 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 3200 verifyFormat("void f(Type (*parameter)[10]) {}"); 3201 verifyGoogleFormat("return sizeof(int**);"); 3202 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 3203 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 3204 verifyFormat("auto a = [](int **&, int ***) {};"); 3205 3206 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 3207 3208 verifyIndependentOfContext("A<int *> a;"); 3209 verifyIndependentOfContext("A<int **> a;"); 3210 verifyIndependentOfContext("A<int *, int *> a;"); 3211 verifyIndependentOfContext( 3212 "const char *const p = reinterpret_cast<const char *const>(q);"); 3213 verifyIndependentOfContext("A<int **, int **> a;"); 3214 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 3215 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 3216 verifyFormat("for (; a && b;) {\n}"); 3217 3218 verifyFormat( 3219 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3221 3222 verifyGoogleFormat("int main(int argc, char** argv) {}"); 3223 verifyGoogleFormat("A<int*> a;"); 3224 verifyGoogleFormat("A<int**> a;"); 3225 verifyGoogleFormat("A<int*, int*> a;"); 3226 verifyGoogleFormat("A<int**, int**> a;"); 3227 verifyGoogleFormat("f(b ? *c : *d);"); 3228 verifyGoogleFormat("int a = b ? *c : *d;"); 3229 verifyGoogleFormat("Type* t = **x;"); 3230 verifyGoogleFormat("Type* t = *++*x;"); 3231 verifyGoogleFormat("*++*x;"); 3232 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 3233 verifyGoogleFormat("Type* t = x++ * y;"); 3234 verifyGoogleFormat( 3235 "const char* const p = reinterpret_cast<const char* const>(q);"); 3236 3237 verifyIndependentOfContext("a = *(x + y);"); 3238 verifyIndependentOfContext("a = &(x + y);"); 3239 verifyIndependentOfContext("*(x + y).call();"); 3240 verifyIndependentOfContext("&(x + y)->call();"); 3241 verifyFormat("void f() { &(*I).first; }"); 3242 3243 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 3244 verifyFormat( 3245 "int *MyValues = {\n" 3246 " *A, // Operator detection might be confused by the '{'\n" 3247 " *BB // Operator detection might be confused by previous comment\n" 3248 "};"); 3249 3250 verifyIndependentOfContext("if (int *a = &b)"); 3251 verifyIndependentOfContext("if (int &a = *b)"); 3252 verifyIndependentOfContext("if (a & b[i])"); 3253 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 3254 verifyIndependentOfContext("if (*b[i])"); 3255 verifyIndependentOfContext("if (int *a = (&b))"); 3256 verifyIndependentOfContext("while (int *a = &b)"); 3257 verifyFormat("void f() {\n" 3258 " for (const int &v : Values) {\n" 3259 " }\n" 3260 "}"); 3261 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 3262 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 3263 3264 verifyFormat("#define MACRO \\\n" 3265 " int *i = a * b; \\\n" 3266 " void f(a *b);", 3267 getLLVMStyleWithColumns(19)); 3268 3269 verifyIndependentOfContext("A = new SomeType *[Length];"); 3270 verifyIndependentOfContext("A = new SomeType *[Length]();"); 3271 verifyGoogleFormat("A = new SomeType* [Length]();"); 3272 verifyGoogleFormat("A = new SomeType* [Length];"); 3273 3274 FormatStyle PointerLeft = getLLVMStyle(); 3275 PointerLeft.PointerBindsToType = true; 3276 verifyFormat("delete *x;", PointerLeft); 3277 } 3278 3279 TEST_F(FormatTest, UnderstandsEllipsis) { 3280 verifyFormat("int printf(const char *fmt, ...);"); 3281 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 3282 } 3283 3284 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 3285 EXPECT_EQ("int *a;\n" 3286 "int *a;\n" 3287 "int *a;", 3288 format("int *a;\n" 3289 "int* a;\n" 3290 "int *a;", 3291 getGoogleStyle())); 3292 EXPECT_EQ("int* a;\n" 3293 "int* a;\n" 3294 "int* a;", 3295 format("int* a;\n" 3296 "int* a;\n" 3297 "int *a;", 3298 getGoogleStyle())); 3299 EXPECT_EQ("int *a;\n" 3300 "int *a;\n" 3301 "int *a;", 3302 format("int *a;\n" 3303 "int * a;\n" 3304 "int * a;", 3305 getGoogleStyle())); 3306 } 3307 3308 TEST_F(FormatTest, UnderstandsRvalueReferences) { 3309 verifyFormat("int f(int &&a) {}"); 3310 verifyFormat("int f(int a, char &&b) {}"); 3311 verifyFormat("void f() { int &&a = b; }"); 3312 verifyGoogleFormat("int f(int a, char&& b) {}"); 3313 verifyGoogleFormat("void f() { int&& a = b; }"); 3314 3315 verifyIndependentOfContext("A<int &&> a;"); 3316 verifyIndependentOfContext("A<int &&, int &&> a;"); 3317 verifyGoogleFormat("A<int&&> a;"); 3318 verifyGoogleFormat("A<int&&, int&&> a;"); 3319 } 3320 3321 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 3322 verifyFormat("void f() {\n" 3323 " x[aaaaaaaaa -\n" 3324 " b] = 23;\n" 3325 "}", 3326 getLLVMStyleWithColumns(15)); 3327 } 3328 3329 TEST_F(FormatTest, FormatsCasts) { 3330 verifyFormat("Type *A = static_cast<Type *>(P);"); 3331 verifyFormat("Type *A = (Type *)P;"); 3332 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 3333 verifyFormat("int a = (int)(2.0f);"); 3334 verifyFormat("int a = (int)2.0f;"); 3335 verifyFormat("x[(int32)y];"); 3336 verifyFormat("x = (int32)y;"); 3337 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 3338 verifyFormat("int a = (int)*b;"); 3339 verifyFormat("int a = (int)2.0f;"); 3340 verifyFormat("int a = (int)~0;"); 3341 verifyFormat("int a = (int)++a;"); 3342 verifyFormat("int a = (int)sizeof(int);"); 3343 verifyFormat("int a = (int)+2;"); 3344 verifyFormat("my_int a = (my_int)2.0f;"); 3345 verifyFormat("my_int a = (my_int)sizeof(int);"); 3346 verifyFormat("return (my_int)aaa;"); 3347 3348 // FIXME: Without type knowledge, this can still fall apart miserably. 3349 verifyFormat("void f() { my_int a = (my_int) * b; }"); 3350 verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }"); 3351 verifyFormat("my_int a = (my_int) ~0;"); 3352 verifyFormat("my_int a = (my_int)++ a;"); 3353 verifyFormat("my_int a = (my_int) + 2;"); 3354 3355 // These are not casts. 3356 verifyFormat("void f(int *) {}"); 3357 verifyFormat("f(foo)->b;"); 3358 verifyFormat("f(foo).b;"); 3359 verifyFormat("f(foo)(b);"); 3360 verifyFormat("f(foo)[b];"); 3361 verifyFormat("[](foo) { return 4; }(bar)];"); 3362 verifyFormat("(*funptr)(foo)[4];"); 3363 verifyFormat("funptrs[4](foo)[4];"); 3364 verifyFormat("void f(int *);"); 3365 verifyFormat("void f(int *) = 0;"); 3366 verifyFormat("void f(SmallVector<int>) {}"); 3367 verifyFormat("void f(SmallVector<int>);"); 3368 verifyFormat("void f(SmallVector<int>) = 0;"); 3369 verifyFormat("void f(int i = (kValue) * kMask) {}"); 3370 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 3371 verifyFormat("int a = sizeof(int) * b;"); 3372 verifyFormat("int a = alignof(int) * b;"); 3373 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 3374 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 3375 3376 // These are not casts, but at some point were confused with casts. 3377 verifyFormat("virtual void foo(int *) override;"); 3378 verifyFormat("virtual void foo(char &) const;"); 3379 verifyFormat("virtual void foo(int *a, char *) const;"); 3380 verifyFormat("int a = sizeof(int *) + b;"); 3381 verifyFormat("int a = alignof(int *) + b;"); 3382 } 3383 3384 TEST_F(FormatTest, FormatsFunctionTypes) { 3385 verifyFormat("A<bool()> a;"); 3386 verifyFormat("A<SomeType()> a;"); 3387 verifyFormat("A<void (*)(int, std::string)> a;"); 3388 verifyFormat("A<void *(int)>;"); 3389 verifyFormat("void *(*a)(int *, SomeType *);"); 3390 verifyFormat("int (*func)(void *);"); 3391 verifyFormat("void f() { int (*func)(void *); }"); 3392 3393 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 3394 verifyGoogleFormat("void* (*a)(int);"); 3395 3396 // Other constructs can look somewhat like function types: 3397 verifyFormat("A<sizeof(*x)> a;"); 3398 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 3399 } 3400 3401 TEST_F(FormatTest, BreaksLongDeclarations) { 3402 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 3403 " AnotherNameForTheLongType;"); 3404 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 3405 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 3406 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 3407 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 3408 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 3409 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 3410 3411 // FIXME: Without the comment, this breaks after "(". 3412 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 3413 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); 3414 3415 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 3416 " int LoooooooooooooooooooongParam2) {}"); 3417 verifyFormat( 3418 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 3419 " SourceLocation L, IdentifierIn *II,\n" 3420 " Type *T) {}"); 3421 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 3422 "ReallyReallyLongFunctionName(\n" 3423 " const std::string &SomeParameter,\n" 3424 " const SomeType<string, SomeOtherTemplateParameter> &\n" 3425 " ReallyReallyLongParameterName,\n" 3426 " const SomeType<string, SomeOtherTemplateParameter> &\n" 3427 " AnotherLongParameterName) {}"); 3428 verifyFormat("template <typename A>\n" 3429 "SomeLoooooooooooooooooooooongType<\n" 3430 " typename some_namespace::SomeOtherType<A>::Type>\n" 3431 "Function() {}"); 3432 verifyFormat( 3433 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 3434 " aaaaaaaaaaaaaaaaaaaaaaa;"); 3435 3436 verifyGoogleFormat( 3437 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 3438 " SourceLocation L) {}"); 3439 verifyGoogleFormat( 3440 "some_namespace::LongReturnType\n" 3441 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 3442 " int first_long_parameter, int second_parameter) {}"); 3443 3444 verifyGoogleFormat("template <typename T>\n" 3445 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3446 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 3447 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3448 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 3449 } 3450 3451 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 3452 verifyFormat("(a)->b();"); 3453 verifyFormat("--a;"); 3454 } 3455 3456 TEST_F(FormatTest, HandlesIncludeDirectives) { 3457 verifyFormat("#include <string>\n" 3458 "#include <a/b/c.h>\n" 3459 "#include \"a/b/string\"\n" 3460 "#include \"string.h\"\n" 3461 "#include \"string.h\"\n" 3462 "#include <a-a>\n" 3463 "#include < path with space >\n" 3464 "#include \"abc.h\" // this is included for ABC\n" 3465 "#include \"some long include\" // with a comment\n" 3466 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 3467 getLLVMStyleWithColumns(35)); 3468 3469 verifyFormat("#import <string>"); 3470 verifyFormat("#import <a/b/c.h>"); 3471 verifyFormat("#import \"a/b/string\""); 3472 verifyFormat("#import \"string.h\""); 3473 verifyFormat("#import \"string.h\""); 3474 } 3475 3476 //===----------------------------------------------------------------------===// 3477 // Error recovery tests. 3478 //===----------------------------------------------------------------------===// 3479 3480 TEST_F(FormatTest, IncompleteParameterLists) { 3481 FormatStyle NoBinPacking = getLLVMStyle(); 3482 NoBinPacking.BinPackParameters = false; 3483 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 3484 " double *min_x,\n" 3485 " double *max_x,\n" 3486 " double *min_y,\n" 3487 " double *max_y,\n" 3488 " double *min_z,\n" 3489 " double *max_z, ) {}", 3490 NoBinPacking); 3491 } 3492 3493 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 3494 verifyFormat("void f() { return; }\n42"); 3495 verifyFormat("void f() {\n" 3496 " if (0)\n" 3497 " return;\n" 3498 "}\n" 3499 "42"); 3500 verifyFormat("void f() { return }\n42"); 3501 verifyFormat("void f() {\n" 3502 " if (0)\n" 3503 " return\n" 3504 "}\n" 3505 "42"); 3506 } 3507 3508 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 3509 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 3510 EXPECT_EQ("void f() {\n" 3511 " if (a)\n" 3512 " return\n" 3513 "}", 3514 format("void f ( ) { if ( a ) return }")); 3515 EXPECT_EQ("namespace N {\n" 3516 "void f()\n" 3517 "}", 3518 format("namespace N { void f() }")); 3519 EXPECT_EQ("namespace N {\n" 3520 "void f() {}\n" 3521 "void g()\n" 3522 "}", 3523 format("namespace N { void f( ) { } void g( ) }")); 3524 } 3525 3526 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 3527 verifyFormat("int aaaaaaaa =\n" 3528 " // Overlylongcomment\n" 3529 " b;", 3530 getLLVMStyleWithColumns(20)); 3531 verifyFormat("function(\n" 3532 " ShortArgument,\n" 3533 " LoooooooooooongArgument);\n", 3534 getLLVMStyleWithColumns(20)); 3535 } 3536 3537 TEST_F(FormatTest, IncorrectAccessSpecifier) { 3538 verifyFormat("public:"); 3539 verifyFormat("class A {\n" 3540 "public\n" 3541 " void f() {}\n" 3542 "};"); 3543 verifyFormat("public\n" 3544 "int qwerty;"); 3545 verifyFormat("public\n" 3546 "B {}"); 3547 verifyFormat("public\n" 3548 "{}"); 3549 verifyFormat("public\n" 3550 "B { int x; }"); 3551 } 3552 3553 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 3554 verifyFormat("{"); 3555 verifyFormat("#})"); 3556 } 3557 3558 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 3559 verifyFormat("do {\n}"); 3560 verifyFormat("do {\n}\n" 3561 "f();"); 3562 verifyFormat("do {\n}\n" 3563 "wheeee(fun);"); 3564 verifyFormat("do {\n" 3565 " f();\n" 3566 "}"); 3567 } 3568 3569 TEST_F(FormatTest, IncorrectCodeMissingParens) { 3570 verifyFormat("if {\n foo;\n foo();\n}"); 3571 verifyFormat("switch {\n foo;\n foo();\n}"); 3572 verifyFormat("for {\n foo;\n foo();\n}"); 3573 verifyFormat("while {\n foo;\n foo();\n}"); 3574 verifyFormat("do {\n foo;\n foo();\n} while;"); 3575 } 3576 3577 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 3578 verifyFormat("namespace {\n" 3579 "class Foo { Foo (\n" 3580 "};\n" 3581 "} // comment"); 3582 } 3583 3584 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 3585 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 3586 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 3587 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 3588 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 3589 3590 EXPECT_EQ("{\n" 3591 " {\n" 3592 " breakme(\n" 3593 " qwe);\n" 3594 "}\n", 3595 format("{\n" 3596 " {\n" 3597 " breakme(qwe);\n" 3598 "}\n", 3599 getLLVMStyleWithColumns(10))); 3600 } 3601 3602 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 3603 verifyFormat("int x = {\n" 3604 " avariable,\n" 3605 " b(alongervariable)\n" 3606 "};", 3607 getLLVMStyleWithColumns(25)); 3608 } 3609 3610 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 3611 verifyFormat("return (a)(b) { 1, 2, 3 };"); 3612 } 3613 3614 TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { 3615 verifyFormat("vector<int> x{ 1, 2, 3, 4 };"); 3616 verifyFormat("vector<T> x{ {}, {}, {}, {} };"); 3617 verifyFormat("f({ 1, 2 });"); 3618 verifyFormat("auto v = Foo{ 1 };"); 3619 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });"); 3620 verifyFormat("Class::Class : member{ 1, 2, 3 } {}"); 3621 verifyFormat("new vector<int>{ 1, 2, 3 };"); 3622 verifyFormat("new int[3]{ 1, 2, 3 };"); 3623 verifyFormat("return { arg1, arg2 };"); 3624 verifyFormat("return { arg1, SomeType{ parameter } };"); 3625 verifyFormat("new T{ arg1, arg2 };"); 3626 verifyFormat("class Class {\n" 3627 " T member = { arg1, arg2 };\n" 3628 "};"); 3629 verifyFormat( 3630 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3631 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 3632 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3633 " bbbbbbbbbbbbbbbbbbbb, bbbbb };"); 3634 3635 FormatStyle NoSpaces = getLLVMStyle(); 3636 NoSpaces.SpacesInBracedLists = false; 3637 verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces); 3638 verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces); 3639 verifyFormat("f({1, 2});", NoSpaces); 3640 verifyFormat("auto v = Foo{-1};", NoSpaces); 3641 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces); 3642 verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces); 3643 verifyFormat("new vector<int>{1, 2, 3};", NoSpaces); 3644 verifyFormat("new int[3]{1, 2, 3};", NoSpaces); 3645 verifyFormat("return {arg1, arg2};", NoSpaces); 3646 verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces); 3647 verifyFormat("new T{arg1, arg2};", NoSpaces); 3648 verifyFormat("class Class {\n" 3649 " T member = {arg1, arg2};\n" 3650 "};", 3651 NoSpaces); 3652 } 3653 3654 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 3655 verifyFormat("void f() { return 42; }"); 3656 verifyFormat("void f() {\n" 3657 " // Comment\n" 3658 "}"); 3659 verifyFormat("{\n" 3660 "#error {\n" 3661 " int a;\n" 3662 "}"); 3663 verifyFormat("{\n" 3664 " int a;\n" 3665 "#error {\n" 3666 "}"); 3667 verifyFormat("void f() {} // comment"); 3668 verifyFormat("void f() { int a; } // comment"); 3669 verifyFormat("void f() {\n" 3670 "} // comment", 3671 getLLVMStyleWithColumns(15)); 3672 3673 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 3674 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 3675 3676 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 3677 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 3678 } 3679 3680 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 3681 // Elaborate type variable declarations. 3682 verifyFormat("struct foo a = { bar };\nint n;"); 3683 verifyFormat("class foo a = { bar };\nint n;"); 3684 verifyFormat("union foo a = { bar };\nint n;"); 3685 3686 // Elaborate types inside function definitions. 3687 verifyFormat("struct foo f() {}\nint n;"); 3688 verifyFormat("class foo f() {}\nint n;"); 3689 verifyFormat("union foo f() {}\nint n;"); 3690 3691 // Templates. 3692 verifyFormat("template <class X> void f() {}\nint n;"); 3693 verifyFormat("template <struct X> void f() {}\nint n;"); 3694 verifyFormat("template <union X> void f() {}\nint n;"); 3695 3696 // Actual definitions... 3697 verifyFormat("struct {\n} n;"); 3698 verifyFormat( 3699 "template <template <class T, class Y>, class Z> class X {\n} n;"); 3700 verifyFormat("union Z {\n int n;\n} x;"); 3701 verifyFormat("class MACRO Z {\n} n;"); 3702 verifyFormat("class MACRO(X) Z {\n} n;"); 3703 verifyFormat("class __attribute__(X) Z {\n} n;"); 3704 verifyFormat("class __declspec(X) Z {\n} n;"); 3705 verifyFormat("class A##B##C {\n} n;"); 3706 3707 // Redefinition from nested context: 3708 verifyFormat("class A::B::C {\n} n;"); 3709 3710 // Template definitions. 3711 verifyFormat( 3712 "template <typename F>\n" 3713 "Matcher(const Matcher<F> &Other,\n" 3714 " typename enable_if_c<is_base_of<F, T>::value &&\n" 3715 " !is_same<F, T>::value>::type * = 0)\n" 3716 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 3717 3718 // FIXME: This is still incorrectly handled at the formatter side. 3719 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {};"); 3720 3721 // FIXME: 3722 // This now gets parsed incorrectly as class definition. 3723 // verifyFormat("class A<int> f() {\n}\nint n;"); 3724 3725 // Elaborate types where incorrectly parsing the structural element would 3726 // break the indent. 3727 verifyFormat("if (true)\n" 3728 " class X x;\n" 3729 "else\n" 3730 " f();\n"); 3731 3732 // This is simply incomplete. Formatting is not important, but must not crash. 3733 verifyFormat("class A:"); 3734 } 3735 3736 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 3737 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 3738 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 3739 EXPECT_EQ("#error 1", format(" # error 1")); 3740 EXPECT_EQ("#warning 1", format(" # warning 1")); 3741 } 3742 3743 TEST_F(FormatTest, FormatHashIfExpressions) { 3744 // FIXME: Come up with a better indentation for #elif. 3745 verifyFormat( 3746 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 3747 " defined(BBBBBBBB)\n" 3748 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 3749 " defined(BBBBBBBB)\n" 3750 "#endif", 3751 getLLVMStyleWithColumns(65)); 3752 } 3753 3754 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 3755 FormatStyle AllowsMergedIf = getGoogleStyle(); 3756 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 3757 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 3758 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 3759 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 3760 EXPECT_EQ("if (true) return 42;", 3761 format("if (true)\nreturn 42;", AllowsMergedIf)); 3762 FormatStyle ShortMergedIf = AllowsMergedIf; 3763 ShortMergedIf.ColumnLimit = 25; 3764 verifyFormat("#define A \\\n" 3765 " if (true) return 42;", 3766 ShortMergedIf); 3767 verifyFormat("#define A \\\n" 3768 " f(); \\\n" 3769 " if (true)\n" 3770 "#define B", 3771 ShortMergedIf); 3772 verifyFormat("#define A \\\n" 3773 " f(); \\\n" 3774 " if (true)\n" 3775 "g();", 3776 ShortMergedIf); 3777 verifyFormat("{\n" 3778 "#ifdef A\n" 3779 " // Comment\n" 3780 " if (true) continue;\n" 3781 "#endif\n" 3782 " // Comment\n" 3783 " if (true) continue;", 3784 ShortMergedIf); 3785 } 3786 3787 TEST_F(FormatTest, BlockCommentsInControlLoops) { 3788 verifyFormat("if (0) /* a comment in a strange place */ {\n" 3789 " f();\n" 3790 "}"); 3791 verifyFormat("if (0) /* a comment in a strange place */ {\n" 3792 " f();\n" 3793 "} /* another comment */ else /* comment #3 */ {\n" 3794 " g();\n" 3795 "}"); 3796 verifyFormat("while (0) /* a comment in a strange place */ {\n" 3797 " f();\n" 3798 "}"); 3799 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 3800 " f();\n" 3801 "}"); 3802 verifyFormat("do /* a comment in a strange place */ {\n" 3803 " f();\n" 3804 "} /* another comment */ while (0);"); 3805 } 3806 3807 TEST_F(FormatTest, BlockComments) { 3808 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 3809 format("/* *//* */ /* */\n/* *//* */ /* */")); 3810 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 3811 EXPECT_EQ("#define A /*123*/\\\n" 3812 " b\n" 3813 "/* */\n" 3814 "someCall(\n" 3815 " parameter);", 3816 format("#define A /*123*/ b\n" 3817 "/* */\n" 3818 "someCall(parameter);", 3819 getLLVMStyleWithColumns(15))); 3820 3821 EXPECT_EQ("#define A\n" 3822 "/* */ someCall(\n" 3823 " parameter);", 3824 format("#define A\n" 3825 "/* */someCall(parameter);", 3826 getLLVMStyleWithColumns(15))); 3827 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 3828 EXPECT_EQ("/*\n" 3829 "*\n" 3830 " * aaaaaa\n" 3831 "* aaaaaa\n" 3832 "*/", 3833 format("/*\n" 3834 "*\n" 3835 " * aaaaaa aaaaaa\n" 3836 "*/", 3837 getLLVMStyleWithColumns(10))); 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 3849 FormatStyle NoBinPacking = getLLVMStyle(); 3850 NoBinPacking.BinPackParameters = false; 3851 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 3852 " 2, /* comment 2 */\n" 3853 " 3, /* comment 3 */\n" 3854 " aaaa,\n" 3855 " bbbb);", 3856 format("someFunction (1, /* comment 1 */\n" 3857 " 2, /* comment 2 */ \n" 3858 " 3, /* comment 3 */\n" 3859 "aaaa, bbbb );", 3860 NoBinPacking)); 3861 verifyFormat( 3862 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3864 EXPECT_EQ( 3865 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 3866 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 3868 format( 3869 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 3870 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 3872 EXPECT_EQ( 3873 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 3874 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 3875 "int cccccccccccccccccccccccccccccc; /* comment */\n", 3876 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 3877 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 3878 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 3879 } 3880 3881 TEST_F(FormatTest, BlockCommentsInMacros) { 3882 EXPECT_EQ("#define A \\\n" 3883 " { \\\n" 3884 " /* one line */ \\\n" 3885 " someCall();", 3886 format("#define A { \\\n" 3887 " /* one line */ \\\n" 3888 " someCall();", 3889 getLLVMStyleWithColumns(20))); 3890 EXPECT_EQ("#define A \\\n" 3891 " { \\\n" 3892 " /* previous */ \\\n" 3893 " /* one line */ \\\n" 3894 " someCall();", 3895 format("#define A { \\\n" 3896 " /* previous */ \\\n" 3897 " /* one line */ \\\n" 3898 " someCall();", 3899 getLLVMStyleWithColumns(20))); 3900 } 3901 3902 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 3903 EXPECT_EQ("a = {\n" 3904 " 1111 /* */\n" 3905 "};", 3906 format("a = {1111 /* */\n" 3907 "};", 3908 getLLVMStyleWithColumns(15))); 3909 EXPECT_EQ("a = {\n" 3910 " 1111 /* */\n" 3911 "};", 3912 format("a = {1111 /* */\n" 3913 "};", 3914 getLLVMStyleWithColumns(15))); 3915 3916 // FIXME: The formatting is still wrong here. 3917 EXPECT_EQ("a = {\n" 3918 " 1111 /* a\n" 3919 " */\n" 3920 "};", 3921 format("a = {1111 /* a */\n" 3922 "};", 3923 getLLVMStyleWithColumns(15))); 3924 } 3925 3926 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 3927 // FIXME: This is not what we want... 3928 verifyFormat("{\n" 3929 "// a" 3930 "// b"); 3931 } 3932 3933 TEST_F(FormatTest, FormatStarDependingOnContext) { 3934 verifyFormat("void f(int *a);"); 3935 verifyFormat("void f() { f(fint * b); }"); 3936 verifyFormat("class A {\n void f(int *a);\n};"); 3937 verifyFormat("class A {\n int *a;\n};"); 3938 verifyFormat("namespace a {\n" 3939 "namespace b {\n" 3940 "class A {\n" 3941 " void f() {}\n" 3942 " int *a;\n" 3943 "};\n" 3944 "}\n" 3945 "}"); 3946 } 3947 3948 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 3949 verifyFormat("while"); 3950 verifyFormat("operator"); 3951 } 3952 3953 //===----------------------------------------------------------------------===// 3954 // Objective-C tests. 3955 //===----------------------------------------------------------------------===// 3956 3957 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 3958 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 3959 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 3960 format("-(NSUInteger)indexOfObject:(id)anObject;")); 3961 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 3962 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 3963 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 3964 format("-(NSInteger)Method3:(id)anObject;")); 3965 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 3966 format("-(NSInteger)Method4:(id)anObject;")); 3967 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 3968 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 3969 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 3970 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 3971 EXPECT_EQ( 3972 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 3973 format( 3974 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 3975 3976 // Very long objectiveC method declaration. 3977 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 3978 " inRange:(NSRange)range\n" 3979 " outRange:(NSRange)out_range\n" 3980 " outRange1:(NSRange)out_range1\n" 3981 " outRange2:(NSRange)out_range2\n" 3982 " outRange3:(NSRange)out_range3\n" 3983 " outRange4:(NSRange)out_range4\n" 3984 " outRange5:(NSRange)out_range5\n" 3985 " outRange6:(NSRange)out_range6\n" 3986 " outRange7:(NSRange)out_range7\n" 3987 " outRange8:(NSRange)out_range8\n" 3988 " outRange9:(NSRange)out_range9;"); 3989 3990 verifyFormat("- (int)sum:(vector<int>)numbers;"); 3991 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 3992 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 3993 // protocol lists (but not for template classes): 3994 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 3995 3996 verifyFormat("- (int (*)())foo:(int (*)())f;"); 3997 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 3998 3999 // If there's no return type (very rare in practice!), LLVM and Google style 4000 // agree. 4001 verifyFormat("- foo;"); 4002 verifyFormat("- foo:(int)f;"); 4003 verifyGoogleFormat("- foo:(int)foo;"); 4004 } 4005 4006 TEST_F(FormatTest, FormatObjCBlocks) { 4007 verifyFormat("int (^Block)(int, int);"); 4008 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 4009 } 4010 4011 TEST_F(FormatTest, FormatObjCInterface) { 4012 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 4013 "@public\n" 4014 " int field1;\n" 4015 "@protected\n" 4016 " int field2;\n" 4017 "@private\n" 4018 " int field3;\n" 4019 "@package\n" 4020 " int field4;\n" 4021 "}\n" 4022 "+ (id)init;\n" 4023 "@end"); 4024 4025 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 4026 " @public\n" 4027 " int field1;\n" 4028 " @protected\n" 4029 " int field2;\n" 4030 " @private\n" 4031 " int field3;\n" 4032 " @package\n" 4033 " int field4;\n" 4034 "}\n" 4035 "+ (id)init;\n" 4036 "@end"); 4037 4038 verifyFormat("@interface /* wait for it */ Foo\n" 4039 "+ (id)init;\n" 4040 "// Look, a comment!\n" 4041 "- (int)answerWith:(int)i;\n" 4042 "@end"); 4043 4044 verifyFormat("@interface Foo\n" 4045 "@end\n" 4046 "@interface Bar\n" 4047 "@end"); 4048 4049 verifyFormat("@interface Foo : Bar\n" 4050 "+ (id)init;\n" 4051 "@end"); 4052 4053 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 4054 "+ (id)init;\n" 4055 "@end"); 4056 4057 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 4058 "+ (id)init;\n" 4059 "@end"); 4060 4061 verifyFormat("@interface Foo (HackStuff)\n" 4062 "+ (id)init;\n" 4063 "@end"); 4064 4065 verifyFormat("@interface Foo ()\n" 4066 "+ (id)init;\n" 4067 "@end"); 4068 4069 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 4070 "+ (id)init;\n" 4071 "@end"); 4072 4073 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 4074 "+ (id)init;\n" 4075 "@end"); 4076 4077 verifyFormat("@interface Foo {\n" 4078 " int _i;\n" 4079 "}\n" 4080 "+ (id)init;\n" 4081 "@end"); 4082 4083 verifyFormat("@interface Foo : Bar {\n" 4084 " int _i;\n" 4085 "}\n" 4086 "+ (id)init;\n" 4087 "@end"); 4088 4089 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 4090 " int _i;\n" 4091 "}\n" 4092 "+ (id)init;\n" 4093 "@end"); 4094 4095 verifyFormat("@interface Foo (HackStuff) {\n" 4096 " int _i;\n" 4097 "}\n" 4098 "+ (id)init;\n" 4099 "@end"); 4100 4101 verifyFormat("@interface Foo () {\n" 4102 " int _i;\n" 4103 "}\n" 4104 "+ (id)init;\n" 4105 "@end"); 4106 4107 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 4108 " int _i;\n" 4109 "}\n" 4110 "+ (id)init;\n" 4111 "@end"); 4112 } 4113 4114 TEST_F(FormatTest, FormatObjCImplementation) { 4115 verifyFormat("@implementation Foo : NSObject {\n" 4116 "@public\n" 4117 " int field1;\n" 4118 "@protected\n" 4119 " int field2;\n" 4120 "@private\n" 4121 " int field3;\n" 4122 "@package\n" 4123 " int field4;\n" 4124 "}\n" 4125 "+ (id)init {\n}\n" 4126 "@end"); 4127 4128 verifyGoogleFormat("@implementation Foo : NSObject {\n" 4129 " @public\n" 4130 " int field1;\n" 4131 " @protected\n" 4132 " int field2;\n" 4133 " @private\n" 4134 " int field3;\n" 4135 " @package\n" 4136 " int field4;\n" 4137 "}\n" 4138 "+ (id)init {\n}\n" 4139 "@end"); 4140 4141 verifyFormat("@implementation Foo\n" 4142 "+ (id)init {\n" 4143 " if (true)\n" 4144 " return nil;\n" 4145 "}\n" 4146 "// Look, a comment!\n" 4147 "- (int)answerWith:(int)i {\n" 4148 " return i;\n" 4149 "}\n" 4150 "+ (int)answerWith:(int)i {\n" 4151 " return i;\n" 4152 "}\n" 4153 "@end"); 4154 4155 verifyFormat("@implementation Foo\n" 4156 "@end\n" 4157 "@implementation Bar\n" 4158 "@end"); 4159 4160 verifyFormat("@implementation Foo : Bar\n" 4161 "+ (id)init {\n}\n" 4162 "- (void)foo {\n}\n" 4163 "@end"); 4164 4165 verifyFormat("@implementation Foo {\n" 4166 " int _i;\n" 4167 "}\n" 4168 "+ (id)init {\n}\n" 4169 "@end"); 4170 4171 verifyFormat("@implementation Foo : Bar {\n" 4172 " int _i;\n" 4173 "}\n" 4174 "+ (id)init {\n}\n" 4175 "@end"); 4176 4177 verifyFormat("@implementation Foo (HackStuff)\n" 4178 "+ (id)init {\n}\n" 4179 "@end"); 4180 } 4181 4182 TEST_F(FormatTest, FormatObjCProtocol) { 4183 verifyFormat("@protocol Foo\n" 4184 "@property(weak) id delegate;\n" 4185 "- (NSUInteger)numberOfThings;\n" 4186 "@end"); 4187 4188 verifyFormat("@protocol MyProtocol <NSObject>\n" 4189 "- (NSUInteger)numberOfThings;\n" 4190 "@end"); 4191 4192 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 4193 "- (NSUInteger)numberOfThings;\n" 4194 "@end"); 4195 4196 verifyFormat("@protocol Foo;\n" 4197 "@protocol Bar;\n"); 4198 4199 verifyFormat("@protocol Foo\n" 4200 "@end\n" 4201 "@protocol Bar\n" 4202 "@end"); 4203 4204 verifyFormat("@protocol myProtocol\n" 4205 "- (void)mandatoryWithInt:(int)i;\n" 4206 "@optional\n" 4207 "- (void)optional;\n" 4208 "@required\n" 4209 "- (void)required;\n" 4210 "@optional\n" 4211 "@property(assign) int madProp;\n" 4212 "@end\n"); 4213 } 4214 4215 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 4216 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 4217 " rect:(NSRect)theRect\n" 4218 " interval:(float)theInterval {\n" 4219 "}"); 4220 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 4221 " longKeyword:(NSRect)theRect\n" 4222 " evenLongerKeyword:(float)theInterval\n" 4223 " error:(NSError **)theError {\n" 4224 "}"); 4225 } 4226 4227 TEST_F(FormatTest, FormatObjCMethodExpr) { 4228 verifyFormat("[foo bar:baz];"); 4229 verifyFormat("return [foo bar:baz];"); 4230 verifyFormat("f([foo bar:baz]);"); 4231 verifyFormat("f(2, [foo bar:baz]);"); 4232 verifyFormat("f(2, a ? b : c);"); 4233 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 4234 4235 // Unary operators. 4236 verifyFormat("int a = +[foo bar:baz];"); 4237 verifyFormat("int a = -[foo bar:baz];"); 4238 verifyFormat("int a = ![foo bar:baz];"); 4239 verifyFormat("int a = ~[foo bar:baz];"); 4240 verifyFormat("int a = ++[foo bar:baz];"); 4241 verifyFormat("int a = --[foo bar:baz];"); 4242 verifyFormat("int a = sizeof [foo bar:baz];"); 4243 verifyFormat("int a = alignof [foo bar:baz];"); 4244 verifyFormat("int a = &[foo bar:baz];"); 4245 verifyFormat("int a = *[foo bar:baz];"); 4246 // FIXME: Make casts work, without breaking f()[4]. 4247 //verifyFormat("int a = (int)[foo bar:baz];"); 4248 //verifyFormat("return (int)[foo bar:baz];"); 4249 //verifyFormat("(void)[foo bar:baz];"); 4250 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 4251 4252 // Binary operators. 4253 verifyFormat("[foo bar:baz], [foo bar:baz];"); 4254 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 4255 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 4256 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 4257 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 4258 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 4259 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 4260 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 4261 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 4262 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 4263 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 4264 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 4265 verifyFormat("[foo bar:baz] ? [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];"); 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 // Whew! 4285 4286 verifyFormat("return in[42];"); 4287 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 4288 "}"); 4289 4290 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 4291 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 4292 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 4293 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 4294 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 4295 verifyFormat("[button setAction:@selector(zoomOut:)];"); 4296 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 4297 4298 verifyFormat("arr[[self indexForFoo:a]];"); 4299 verifyFormat("throw [self errorFor:a];"); 4300 verifyFormat("@throw [self errorFor:a];"); 4301 4302 // This tests that the formatter doesn't break after "backing" but before ":", 4303 // which would be at 80 columns. 4304 verifyFormat( 4305 "void f() {\n" 4306 " if ((self = [super initWithContentRect:contentRect\n" 4307 " styleMask:styleMask\n" 4308 " backing:NSBackingStoreBuffered\n" 4309 " defer:YES]))"); 4310 4311 verifyFormat( 4312 "[foo checkThatBreakingAfterColonWorksOk:\n" 4313 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 4314 4315 verifyFormat("[myObj short:arg1 // Force line break\n" 4316 " longKeyword:arg2\n" 4317 " evenLongerKeyword:arg3\n" 4318 " error:arg4];"); 4319 verifyFormat( 4320 "void f() {\n" 4321 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 4322 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 4323 " pos.width(), pos.height())\n" 4324 " styleMask:NSBorderlessWindowMask\n" 4325 " backing:NSBackingStoreBuffered\n" 4326 " defer:NO]);\n" 4327 "}"); 4328 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 4329 " with:contentsNativeView];"); 4330 4331 verifyFormat( 4332 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 4333 " owner:nillllll];"); 4334 4335 verifyFormat( 4336 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 4337 " forType:kBookmarkButtonDragType];"); 4338 4339 verifyFormat("[defaultCenter addObserver:self\n" 4340 " selector:@selector(willEnterFullscreen)\n" 4341 " name:kWillEnterFullscreenNotification\n" 4342 " object:nil];"); 4343 verifyFormat("[image_rep drawInRect:drawRect\n" 4344 " fromRect:NSZeroRect\n" 4345 " operation:NSCompositeCopy\n" 4346 " fraction:1.0\n" 4347 " respectFlipped:NO\n" 4348 " hints:nil];"); 4349 4350 verifyFormat( 4351 "scoped_nsobject<NSTextField> message(\n" 4352 " // The frame will be fixed up when |-setMessageText:| is called.\n" 4353 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 4354 } 4355 4356 TEST_F(FormatTest, ObjCAt) { 4357 verifyFormat("@autoreleasepool"); 4358 verifyFormat("@catch"); 4359 verifyFormat("@class"); 4360 verifyFormat("@compatibility_alias"); 4361 verifyFormat("@defs"); 4362 verifyFormat("@dynamic"); 4363 verifyFormat("@encode"); 4364 verifyFormat("@end"); 4365 verifyFormat("@finally"); 4366 verifyFormat("@implementation"); 4367 verifyFormat("@import"); 4368 verifyFormat("@interface"); 4369 verifyFormat("@optional"); 4370 verifyFormat("@package"); 4371 verifyFormat("@private"); 4372 verifyFormat("@property"); 4373 verifyFormat("@protected"); 4374 verifyFormat("@protocol"); 4375 verifyFormat("@public"); 4376 verifyFormat("@required"); 4377 verifyFormat("@selector"); 4378 verifyFormat("@synchronized"); 4379 verifyFormat("@synthesize"); 4380 verifyFormat("@throw"); 4381 verifyFormat("@try"); 4382 4383 EXPECT_EQ("@interface", format("@ interface")); 4384 4385 // The precise formatting of this doesn't matter, nobody writes code like 4386 // this. 4387 verifyFormat("@ /*foo*/ interface"); 4388 } 4389 4390 TEST_F(FormatTest, ObjCSnippets) { 4391 verifyFormat("@autoreleasepool {\n" 4392 " foo();\n" 4393 "}"); 4394 verifyFormat("@class Foo, Bar;"); 4395 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 4396 verifyFormat("@dynamic textColor;"); 4397 verifyFormat("char *buf1 = @encode(int *);"); 4398 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 4399 verifyFormat("char *buf1 = @encode(int **);"); 4400 verifyFormat("Protocol *proto = @protocol(p1);"); 4401 verifyFormat("SEL s = @selector(foo:);"); 4402 verifyFormat("@synchronized(self) {\n" 4403 " f();\n" 4404 "}"); 4405 4406 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 4407 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 4408 4409 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 4410 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 4411 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 4412 } 4413 4414 TEST_F(FormatTest, ObjCLiterals) { 4415 verifyFormat("@\"String\""); 4416 verifyFormat("@1"); 4417 verifyFormat("@+4.8"); 4418 verifyFormat("@-4"); 4419 verifyFormat("@1LL"); 4420 verifyFormat("@.5"); 4421 verifyFormat("@'c'"); 4422 verifyFormat("@true"); 4423 4424 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 4425 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 4426 verifyFormat("NSNumber *favoriteColor = @(Green);"); 4427 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 4428 4429 verifyFormat("@["); 4430 verifyFormat("@[]"); 4431 verifyFormat( 4432 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 4433 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 4434 4435 verifyFormat("@{"); 4436 verifyFormat("@{}"); 4437 verifyFormat("@{ @\"one\" : @1 }"); 4438 verifyFormat("return @{ @\"one\" : @1 };"); 4439 verifyFormat("@{ @\"one\" : @1, }"); 4440 4441 verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); 4442 verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); 4443 4444 verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); 4445 verifyFormat("[self setDict:@{}"); 4446 verifyFormat("[self setDict:@{ @1 : @2 }"); 4447 verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); 4448 verifyFormat( 4449 "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); 4450 verifyFormat( 4451 "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); 4452 4453 // FIXME: Nested and multi-line array and dictionary literals need more work. 4454 verifyFormat( 4455 "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" 4456 " @\"processInfo\" : [NSProcessInfo processInfo] };"); 4457 verifyFormat( 4458 "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 4459 " regularFont, };"); 4460 4461 } 4462 4463 TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 4464 EXPECT_EQ("{\n" 4465 "{\n" 4466 "a;\n" 4467 "b;\n" 4468 "}\n" 4469 "}", 4470 format("{\n" 4471 "{\n" 4472 "a;\n" 4473 " b;\n" 4474 "}\n" 4475 "}", 4476 13, 2, getLLVMStyle())); 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 9, 2, getLLVMStyle())); 4490 EXPECT_EQ("{\n" 4491 "{\n" 4492 "public:\n" 4493 " b;\n" 4494 "}\n" 4495 "}", 4496 format("{\n" 4497 "{\n" 4498 "public:\n" 4499 " b;\n" 4500 "}\n" 4501 "}", 4502 17, 2, getLLVMStyle())); 4503 EXPECT_EQ("{\n" 4504 "{\n" 4505 "a;\n" 4506 "}\n" 4507 "{\n" 4508 " b; //\n" 4509 "}\n" 4510 "}", 4511 format("{\n" 4512 "{\n" 4513 "a;\n" 4514 "}\n" 4515 "{\n" 4516 " b; //\n" 4517 "}\n" 4518 "}", 4519 22, 2, getLLVMStyle())); 4520 EXPECT_EQ(" {\n" 4521 " a; //\n" 4522 " }", 4523 format(" {\n" 4524 "a; //\n" 4525 " }", 4526 4, 2, getLLVMStyle())); 4527 EXPECT_EQ("void f() {}\n" 4528 "void g() {}", 4529 format("void f() {}\n" 4530 "void g() {}", 4531 13, 0, getLLVMStyle())); 4532 EXPECT_EQ("int a; // comment\n" 4533 " // line 2\n" 4534 "int b;", 4535 format("int a; // comment\n" 4536 " // line 2\n" 4537 " int b;", 4538 35, 0, getLLVMStyle())); 4539 } 4540 4541 TEST_F(FormatTest, BreakStringLiterals) { 4542 EXPECT_EQ("\"some text \"\n" 4543 "\"other\";", 4544 format("\"some text other\";", getLLVMStyleWithColumns(12))); 4545 EXPECT_EQ("\"some text \"\n" 4546 "\"other\";", 4547 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 4548 EXPECT_EQ( 4549 "#define A \\\n" 4550 " \"some \" \\\n" 4551 " \"text \" \\\n" 4552 " \"other\";", 4553 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 4554 EXPECT_EQ( 4555 "#define A \\\n" 4556 " \"so \" \\\n" 4557 " \"text \" \\\n" 4558 " \"other\";", 4559 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 4560 4561 EXPECT_EQ("\"some text\"", 4562 format("\"some text\"", getLLVMStyleWithColumns(1))); 4563 EXPECT_EQ("\"some text\"", 4564 format("\"some text\"", getLLVMStyleWithColumns(11))); 4565 EXPECT_EQ("\"some \"\n" 4566 "\"text\"", 4567 format("\"some text\"", getLLVMStyleWithColumns(10))); 4568 EXPECT_EQ("\"some \"\n" 4569 "\"text\"", 4570 format("\"some text\"", getLLVMStyleWithColumns(7))); 4571 EXPECT_EQ("\"some\"\n" 4572 "\" tex\"\n" 4573 "\"t\"", 4574 format("\"some text\"", getLLVMStyleWithColumns(6))); 4575 EXPECT_EQ("\"some\"\n" 4576 "\" tex\"\n" 4577 "\" and\"", 4578 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 4579 EXPECT_EQ("\"some\"\n" 4580 "\"/tex\"\n" 4581 "\"/and\"", 4582 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 4583 4584 EXPECT_EQ("variable =\n" 4585 " \"long string \"\n" 4586 " \"literal\";", 4587 format("variable = \"long string literal\";", 4588 getLLVMStyleWithColumns(20))); 4589 4590 EXPECT_EQ("variable = f(\n" 4591 " \"long string \"\n" 4592 " \"literal\",\n" 4593 " short,\n" 4594 " loooooooooooooooooooong);", 4595 format("variable = f(\"long string literal\", short, " 4596 "loooooooooooooooooooong);", 4597 getLLVMStyleWithColumns(20))); 4598 4599 EXPECT_EQ("f(g(\"long string \"\n" 4600 " \"literal\"),\n" 4601 " b);", 4602 format("f(g(\"long string literal\"), b);", 4603 getLLVMStyleWithColumns(20))); 4604 EXPECT_EQ("f(g(\"long string \"\n" 4605 " \"literal\",\n" 4606 " a),\n" 4607 " b);", 4608 format("f(g(\"long string literal\", a), b);", 4609 getLLVMStyleWithColumns(20))); 4610 EXPECT_EQ( 4611 "f(\"one two\".split(\n" 4612 " variable));", 4613 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 4614 EXPECT_EQ("f(\"one two three four five six \"\n" 4615 " \"seven\".split(\n" 4616 " really_looooong_variable));", 4617 format("f(\"one two three four five six seven\"." 4618 "split(really_looooong_variable));", 4619 getLLVMStyleWithColumns(33))); 4620 4621 EXPECT_EQ("f(\"some \"\n" 4622 " \"text\",\n" 4623 " other);", 4624 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 4625 4626 // Only break as a last resort. 4627 verifyFormat( 4628 "aaaaaaaaaaaaaaaaaaaa(\n" 4629 " aaaaaaaaaaaaaaaaaaaa,\n" 4630 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 4631 4632 EXPECT_EQ( 4633 "\"splitmea\"\n" 4634 "\"trandomp\"\n" 4635 "\"oint\"", 4636 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 4637 4638 EXPECT_EQ( 4639 "\"split/\"\n" 4640 "\"pathat/\"\n" 4641 "\"slashes\"", 4642 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 4643 4644 EXPECT_EQ( 4645 "\"split/\"\n" 4646 "\"pathat/\"\n" 4647 "\"slashes\"", 4648 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 4649 EXPECT_EQ("\"split at \"\n" 4650 "\"spaces/at/\"\n" 4651 "\"slashes.at.any$\"\n" 4652 "\"non-alphanumeric%\"\n" 4653 "\"1111111111characte\"\n" 4654 "\"rs\"", 4655 format("\"split at " 4656 "spaces/at/" 4657 "slashes.at." 4658 "any$non-" 4659 "alphanumeric%" 4660 "1111111111characte" 4661 "rs\"", 4662 getLLVMStyleWithColumns(20))); 4663 4664 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 4665 AlignLeft.AlignEscapedNewlinesLeft = true; 4666 EXPECT_EQ( 4667 "#define A \\\n" 4668 " \"some \" \\\n" 4669 " \"text \" \\\n" 4670 " \"other\";", 4671 format("#define A \"some text other\";", AlignLeft)); 4672 } 4673 4674 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 4675 EXPECT_EQ("u8\"unsupported literal\";", 4676 format("u8\"unsupported literal\";", getLLVMStyleWithColumns(15))); 4677 EXPECT_EQ("u\"unsupported literal\";", 4678 format("u\"unsupported literal\";", getLLVMStyleWithColumns(15))); 4679 EXPECT_EQ("U\"unsupported literal\";", 4680 format("U\"unsupported literal\";", getLLVMStyleWithColumns(15))); 4681 EXPECT_EQ("L\"unsupported literal\";", 4682 format("L\"unsupported literal\";", getLLVMStyleWithColumns(15))); 4683 EXPECT_EQ("R\"x(raw literal)x\";", 4684 format("R\"x(raw literal)x\";", getLLVMStyleWithColumns(15))); 4685 } 4686 4687 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 4688 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 4689 " \"ddeeefff\");", 4690 format("someFunction(\"aaabbbcccdddeeefff\");", 4691 getLLVMStyleWithColumns(25))); 4692 EXPECT_EQ("someFunction1234567890(\n" 4693 " \"aaabbbcccdddeeefff\");", 4694 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 4695 getLLVMStyleWithColumns(26))); 4696 EXPECT_EQ("someFunction1234567890(\n" 4697 " \"aaabbbcccdddeeeff\"\n" 4698 " \"f\");", 4699 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 4700 getLLVMStyleWithColumns(25))); 4701 EXPECT_EQ("someFunction1234567890(\n" 4702 " \"aaabbbcccdddeeeff\"\n" 4703 " \"f\");", 4704 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 4705 getLLVMStyleWithColumns(24))); 4706 EXPECT_EQ("someFunction(\n" 4707 " \"aaabbbcc \"\n" 4708 " \"dddeeefff\");", 4709 format("someFunction(\"aaabbbcc dddeeefff\");", 4710 getLLVMStyleWithColumns(25))); 4711 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 4712 " \"ddeeefff\");", 4713 format("someFunction(\"aaabbbccc ddeeefff\");", 4714 getLLVMStyleWithColumns(25))); 4715 EXPECT_EQ("someFunction1234567890(\n" 4716 " \"aaabb \"\n" 4717 " \"cccdddeeefff\");", 4718 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 4719 getLLVMStyleWithColumns(25))); 4720 EXPECT_EQ("#define A \\\n" 4721 " string s = \\\n" 4722 " \"123456789\" \\\n" 4723 " \"0\"; \\\n" 4724 " int i;", 4725 format("#define A string s = \"1234567890\"; int i;", 4726 getLLVMStyleWithColumns(20))); 4727 } 4728 4729 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 4730 EXPECT_EQ("\"\\a\"", 4731 format("\"\\a\"", getLLVMStyleWithColumns(3))); 4732 EXPECT_EQ("\"\\\"", 4733 format("\"\\\"", getLLVMStyleWithColumns(2))); 4734 EXPECT_EQ("\"test\"\n" 4735 "\"\\n\"", 4736 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 4737 EXPECT_EQ("\"tes\\\\\"\n" 4738 "\"n\"", 4739 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 4740 EXPECT_EQ("\"\\\\\\\\\"\n" 4741 "\"\\n\"", 4742 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 4743 EXPECT_EQ("\"\\uff01\"", 4744 format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 4745 EXPECT_EQ("\"\\uff01\"\n" 4746 "\"test\"", 4747 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 4748 EXPECT_EQ("\"\\Uff01ff02\"", 4749 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 4750 EXPECT_EQ("\"\\x000000000001\"\n" 4751 "\"next\"", 4752 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 4753 EXPECT_EQ("\"\\x000000000001next\"", 4754 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 4755 EXPECT_EQ("\"\\x000000000001\"", 4756 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 4757 EXPECT_EQ("\"test\"\n" 4758 "\"\\000000\"\n" 4759 "\"000001\"", 4760 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 4761 EXPECT_EQ("\"test\\000\"\n" 4762 "\"00000000\"\n" 4763 "\"1\"", 4764 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 4765 EXPECT_EQ("R\"(\\x\\x00)\"\n", 4766 format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7))); 4767 } 4768 4769 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 4770 verifyFormat("void f() {\n" 4771 " return g() {}\n" 4772 " void h() {}"); 4773 verifyFormat("if (foo)\n" 4774 " return { forgot_closing_brace();\n" 4775 "test();"); 4776 verifyFormat("int a[] = { void forgot_closing_brace() { f();\n" 4777 "g();\n" 4778 "}"); 4779 } 4780 4781 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 4782 verifyFormat("class X {\n" 4783 " void f() {\n" 4784 " }\n" 4785 "};", 4786 getLLVMStyleWithColumns(12)); 4787 } 4788 4789 TEST_F(FormatTest, ConfigurableIndentWidth) { 4790 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 4791 EightIndent.IndentWidth = 8; 4792 verifyFormat("void f() {\n" 4793 " someFunction();\n" 4794 " if (true) {\n" 4795 " f();\n" 4796 " }\n" 4797 "}", 4798 EightIndent); 4799 verifyFormat("class X {\n" 4800 " void f() {\n" 4801 " }\n" 4802 "};", 4803 EightIndent); 4804 verifyFormat("int x[] = {\n" 4805 " call(),\n" 4806 " call(),\n" 4807 "};", 4808 EightIndent); 4809 } 4810 4811 TEST_F(FormatTest, ConfigurableUseOfTab) { 4812 FormatStyle Tab = getLLVMStyleWithColumns(42); 4813 Tab.IndentWidth = 8; 4814 Tab.UseTab = true; 4815 Tab.AlignEscapedNewlinesLeft = true; 4816 verifyFormat("class X {\n" 4817 "\tvoid f() {\n" 4818 "\t\tsomeFunction(parameter1,\n" 4819 "\t\t\t parameter2);\n" 4820 "\t}\n" 4821 "};", 4822 Tab); 4823 verifyFormat("#define A \\\n" 4824 "\tvoid f() { \\\n" 4825 "\t\tsomeFunction( \\\n" 4826 "\t\t parameter1, \\\n" 4827 "\t\t parameter2); \\\n" 4828 "\t}", 4829 Tab); 4830 4831 4832 // FIXME: To correctly count mixed whitespace we need to 4833 // also correctly count mixed whitespace in front of the comment. 4834 // 4835 // EXPECT_EQ("/*\n" 4836 // "\t a\t\tcomment\n" 4837 // "\t in multiple lines\n" 4838 // " */", 4839 // format(" /*\t \t \n" 4840 // " \t \t a\t\tcomment\t \t\n" 4841 // " \t \t in multiple lines\t\n" 4842 // " \t */", 4843 // Tab)); 4844 // Tab.UseTab = false; 4845 // EXPECT_EQ("/*\n" 4846 // " a\t\tcomment\n" 4847 // " in multiple lines\n" 4848 // " */", 4849 // format(" /*\t \t \n" 4850 // " \t \t a\t\tcomment\t \t\n" 4851 // " \t \t in multiple lines\t\n" 4852 // " \t */", 4853 // Tab)); 4854 // EXPECT_EQ("/* some\n" 4855 // " comment */", 4856 // format(" \t \t /* some\n" 4857 // " \t \t comment */", 4858 // Tab)); 4859 4860 EXPECT_EQ("{\n" 4861 " /*\n" 4862 " * Comment\n" 4863 " */\n" 4864 " int i;\n" 4865 "}", 4866 format("{\n" 4867 "\t/*\n" 4868 "\t * Comment\n" 4869 "\t */\n" 4870 "\t int i;\n" 4871 "}")); 4872 } 4873 4874 TEST_F(FormatTest, LinuxBraceBreaking) { 4875 FormatStyle BreakBeforeBrace = getLLVMStyle(); 4876 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux; 4877 verifyFormat("namespace a\n" 4878 "{\n" 4879 "class A\n" 4880 "{\n" 4881 " void f()\n" 4882 " {\n" 4883 " if (true) {\n" 4884 " a();\n" 4885 " b();\n" 4886 " }\n" 4887 " }\n" 4888 " void g()\n" 4889 " {\n" 4890 " return;\n" 4891 " }\n" 4892 "}\n" 4893 "}", 4894 BreakBeforeBrace); 4895 } 4896 4897 TEST_F(FormatTest, StroustrupBraceBreaking) { 4898 FormatStyle BreakBeforeBrace = getLLVMStyle(); 4899 BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4900 verifyFormat("namespace a {\n" 4901 "class A {\n" 4902 " void f()\n" 4903 " {\n" 4904 " if (true) {\n" 4905 " a();\n" 4906 " b();\n" 4907 " }\n" 4908 " }\n" 4909 " void g()\n" 4910 " {\n" 4911 " return;\n" 4912 " }\n" 4913 "}\n" 4914 "}", 4915 BreakBeforeBrace); 4916 } 4917 4918 bool allStylesEqual(ArrayRef<FormatStyle> Styles) { 4919 for (size_t i = 1; i < Styles.size(); ++i) 4920 if (!(Styles[0] == Styles[i])) 4921 return false; 4922 return true; 4923 } 4924 4925 TEST_F(FormatTest, GetsPredefinedStyleByName) { 4926 FormatStyle Styles[3]; 4927 4928 Styles[0] = getLLVMStyle(); 4929 EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1])); 4930 EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2])); 4931 EXPECT_TRUE(allStylesEqual(Styles)); 4932 4933 Styles[0] = getGoogleStyle(); 4934 EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1])); 4935 EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2])); 4936 EXPECT_TRUE(allStylesEqual(Styles)); 4937 4938 Styles[0] = getChromiumStyle(); 4939 EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1])); 4940 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2])); 4941 EXPECT_TRUE(allStylesEqual(Styles)); 4942 4943 Styles[0] = getMozillaStyle(); 4944 EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1])); 4945 EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2])); 4946 EXPECT_TRUE(allStylesEqual(Styles)); 4947 4948 EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0])); 4949 } 4950 4951 TEST_F(FormatTest, ParsesConfiguration) { 4952 FormatStyle Style = {}; 4953 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 4954 EXPECT_NE(VALUE, Style.FIELD); \ 4955 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 4956 EXPECT_EQ(VALUE, Style.FIELD) 4957 4958 #define CHECK_PARSE_BOOL(FIELD) \ 4959 Style.FIELD = false; \ 4960 EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \ 4961 EXPECT_TRUE(Style.FIELD); \ 4962 EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \ 4963 EXPECT_FALSE(Style.FIELD); 4964 4965 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 4966 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 4967 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 4968 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 4969 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 4970 CHECK_PARSE_BOOL(BinPackParameters); 4971 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 4972 CHECK_PARSE_BOOL(DerivePointerBinding); 4973 CHECK_PARSE_BOOL(IndentCaseLabels); 4974 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 4975 CHECK_PARSE_BOOL(PointerBindsToType); 4976 CHECK_PARSE_BOOL(SpacesInBracedLists); 4977 CHECK_PARSE_BOOL(UseTab); 4978 4979 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 4980 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 4981 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 4982 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 4983 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 4984 PenaltyReturnTypeOnItsOwnLine, 1234u); 4985 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 4986 SpacesBeforeTrailingComments, 1234u); 4987 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 4988 4989 Style.Standard = FormatStyle::LS_Auto; 4990 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 4991 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 4992 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 4993 4994 Style.ColumnLimit = 123; 4995 FormatStyle BaseStyle = getLLVMStyle(); 4996 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 4997 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 4998 4999 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5000 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 5001 FormatStyle::BS_Attach); 5002 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 5003 FormatStyle::BS_Linux); 5004 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 5005 FormatStyle::BS_Stroustrup); 5006 5007 #undef CHECK_PARSE 5008 #undef CHECK_PARSE_BOOL 5009 } 5010 5011 TEST_F(FormatTest, ConfigurationRoundTripTest) { 5012 FormatStyle Style = getLLVMStyle(); 5013 std::string YAML = configurationAsText(Style); 5014 FormatStyle ParsedStyle = {}; 5015 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 5016 EXPECT_EQ(Style, ParsedStyle); 5017 } 5018 5019 TEST_F(FormatTest, WorksFor8bitEncodings) { 5020 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 5021 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 5022 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 5023 "\"\xef\xee\xf0\xf3...\"", 5024 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 5025 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 5026 "\xef\xee\xf0\xf3...\"", 5027 getLLVMStyleWithColumns(12))); 5028 } 5029 5030 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 5031 #if !defined(_MSC_VER) 5032 5033 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 5034 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 5035 getLLVMStyleWithColumns(35)); 5036 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 5037 getLLVMStyleWithColumns(21)); 5038 verifyFormat("// Однажды в студёную зимнюю пору...", 5039 getLLVMStyleWithColumns(36)); 5040 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", 5041 getLLVMStyleWithColumns(22)); 5042 verifyFormat("/* Однажды в студёную зимнюю пору... */", 5043 getLLVMStyleWithColumns(39)); 5044 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 5045 getLLVMStyleWithColumns(25)); 5046 } 5047 5048 TEST_F(FormatTest, SplitsUTF8Strings) { 5049 EXPECT_EQ( 5050 "\"Однажды, в \"\n" 5051 "\"студёную \"\n" 5052 "\"зимнюю \"\n" 5053 "\"пору,\"", 5054 format("\"Однажды, в студёную зимнюю пору,\"", 5055 getLLVMStyleWithColumns(13))); 5056 EXPECT_EQ("\"一 二 三 四 \"\n" 5057 "\"五 六 七 八 \"\n" 5058 "\"九 十\"", 5059 format("\"一 二 三 四 五 六 七 八 九 十\"", 5060 getLLVMStyleWithColumns(10))); 5061 } 5062 5063 TEST_F(FormatTest, SplitsUTF8LineComments) { 5064 EXPECT_EQ("// Я из лесу\n" 5065 "// вышел; был\n" 5066 "// сильный\n" 5067 "// мороз.", 5068 format("// Я из лесу вышел; был сильный мороз.", 5069 getLLVMStyleWithColumns(13))); 5070 EXPECT_EQ("// 一二三\n" 5071 "// 四五六七\n" 5072 "// 八\n" 5073 "// 九 十", 5074 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(6))); 5075 } 5076 5077 TEST_F(FormatTest, SplitsUTF8BlockComments) { 5078 EXPECT_EQ("/* Гляжу,\n" 5079 " * поднимается\n" 5080 " * медленно в\n" 5081 " * гору\n" 5082 " * Лошадка,\n" 5083 " * везущая\n" 5084 " * хворосту\n" 5085 " * воз. */", 5086 format("/* Гляжу, поднимается медленно в гору\n" 5087 " * Лошадка, везущая хворосту воз. */", 5088 getLLVMStyleWithColumns(13))); 5089 EXPECT_EQ("/* 一二三\n" 5090 " * 四五六七\n" 5091 " * 八\n" 5092 " * 九 十\n" 5093 " */", 5094 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(6))); 5095 EXPECT_EQ("/* \n" 5096 " * \n" 5097 " * - */", 5098 format("/* - */", getLLVMStyleWithColumns(12))); 5099 } 5100 5101 #endif 5102 5103 } // end namespace tooling 5104 } // end namespace clang 5105