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 "../Tooling/RewriterTestContext.h" 14 #include "clang/Lex/Lexer.h" 15 #include "llvm/Support/Debug.h" 16 #include "gtest/gtest.h" 17 18 namespace clang { 19 namespace format { 20 21 class FormatTest : public ::testing::Test { 22 protected: 23 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 24 const FormatStyle &Style) { 25 DEBUG(llvm::errs() << "---\n"); 26 RewriterTestContext Context; 27 FileID ID = Context.createInMemoryFile("input.cc", Code); 28 SourceLocation Start = 29 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); 30 std::vector<CharSourceRange> Ranges( 31 1, 32 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); 33 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, 34 getFormattingLangOpts()); 35 tooling::Replacements Replace = reformat( 36 Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer()); 37 ReplacementCount = Replace.size(); 38 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); 39 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); 40 return Context.getRewrittenText(ID); 41 } 42 43 std::string 44 format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { 45 return format(Code, 0, Code.size(), Style); 46 } 47 48 std::string messUp(llvm::StringRef Code) { 49 std::string MessedUp(Code.str()); 50 bool InComment = false; 51 bool InPreprocessorDirective = false; 52 bool JustReplacedNewline = false; 53 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { 54 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { 55 if (JustReplacedNewline) 56 MessedUp[i - 1] = '\n'; 57 InComment = true; 58 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { 59 if (i != 0) 60 MessedUp[i - 1] = '\n'; 61 InPreprocessorDirective = true; 62 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { 63 MessedUp[i] = ' '; 64 MessedUp[i + 1] = ' '; 65 } else if (MessedUp[i] == '\n') { 66 if (InComment) { 67 InComment = false; 68 } else if (InPreprocessorDirective) { 69 InPreprocessorDirective = false; 70 } else { 71 JustReplacedNewline = true; 72 MessedUp[i] = ' '; 73 } 74 } else if (MessedUp[i] != ' ') { 75 JustReplacedNewline = false; 76 } 77 } 78 return MessedUp; 79 } 80 81 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 82 FormatStyle Style = getLLVMStyle(); 83 Style.ColumnLimit = ColumnLimit; 84 return Style; 85 } 86 87 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 88 FormatStyle Style = getGoogleStyle(); 89 Style.ColumnLimit = ColumnLimit; 90 return Style; 91 } 92 93 void verifyFormat(llvm::StringRef Code, 94 const FormatStyle &Style = getLLVMStyle()) { 95 EXPECT_EQ(Code.str(), format(messUp(Code), Style)); 96 } 97 98 void verifyGoogleFormat(llvm::StringRef Code) { 99 verifyFormat(Code, getGoogleStyle()); 100 } 101 102 void verifyIndependentOfContext(llvm::StringRef text) { 103 verifyFormat(text); 104 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 105 } 106 107 int ReplacementCount; 108 }; 109 110 TEST_F(FormatTest, MessUp) { 111 EXPECT_EQ("1 2 3", messUp("1 2 3")); 112 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); 113 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); 114 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); 115 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); 116 } 117 118 //===----------------------------------------------------------------------===// 119 // Basic function tests. 120 //===----------------------------------------------------------------------===// 121 122 TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { 123 EXPECT_EQ(";", format(";")); 124 } 125 126 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 127 EXPECT_EQ("int i;", format(" int i;")); 128 EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); 129 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 130 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 131 } 132 133 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 134 EXPECT_EQ("int i;", format("int\ni;")); 135 } 136 137 TEST_F(FormatTest, FormatsNestedBlockStatements) { 138 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 139 } 140 141 TEST_F(FormatTest, FormatsNestedCall) { 142 verifyFormat("Method(f1, f2(f3));"); 143 verifyFormat("Method(f1(f2, f3()));"); 144 verifyFormat("Method(f1(f2, (f3())));"); 145 } 146 147 TEST_F(FormatTest, NestedNameSpecifiers) { 148 verifyFormat("vector< ::Type> v;"); 149 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 150 } 151 152 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 153 EXPECT_EQ("if (a) {\n" 154 " f();\n" 155 "}", 156 format("if(a){f();}")); 157 EXPECT_EQ(4, ReplacementCount); 158 EXPECT_EQ("if (a) {\n" 159 " f();\n" 160 "}", 161 format("if (a) {\n" 162 " f();\n" 163 "}")); 164 EXPECT_EQ(0, ReplacementCount); 165 } 166 167 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { 168 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); 169 EXPECT_EQ("int a;", format("int a; ")); 170 EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); 171 EXPECT_EQ("int a;\nint b; ", 172 format("int a; \nint b; ", 0, 0, getLLVMStyle())); 173 } 174 175 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { 176 EXPECT_EQ("int b;\nint a;", 177 format("int b;\n int a;", 7, 0, getLLVMStyle())); 178 EXPECT_EQ("int b;\n int a;", 179 format("int b;\n int a;", 6, 0, getLLVMStyle())); 180 181 EXPECT_EQ("#define A \\\n" 182 " int a; \\\n" 183 " int b;", 184 format("#define A \\\n" 185 " int a; \\\n" 186 " int b;", 187 26, 0, getLLVMStyleWithColumns(12))); 188 EXPECT_EQ("#define A \\\n" 189 " int a; \\\n" 190 " int b;", 191 format("#define A \\\n" 192 " int a; \\\n" 193 " int b;", 194 25, 0, getLLVMStyleWithColumns(12))); 195 } 196 197 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 198 EXPECT_EQ("int a;\n\n int b;", 199 format("int a;\n \n\n int b;", 7, 0, getLLVMStyle())); 200 EXPECT_EQ("int a;\n\n int b;", 201 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); 202 } 203 204 TEST_F(FormatTest, ReformatsMovedLines) { 205 EXPECT_EQ( 206 "template <typename T> T *getFETokenInfo() const {\n" 207 " return static_cast<T *>(FETokenInfo);\n" 208 "}\n" 209 " int a; // <- Should not be formatted", 210 format( 211 "template<typename T>\n" 212 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 213 " int a; // <- Should not be formatted", 214 9, 5, getLLVMStyle())); 215 } 216 217 //===----------------------------------------------------------------------===// 218 // Tests for control statements. 219 //===----------------------------------------------------------------------===// 220 221 TEST_F(FormatTest, FormatIfWithoutCompountStatement) { 222 verifyFormat("if (true)\n f();\ng();"); 223 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 224 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 225 226 FormatStyle AllowsMergedIf = getGoogleStyle(); 227 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 228 verifyFormat("if (a)\n" 229 " // comment\n" 230 " f();", 231 AllowsMergedIf); 232 233 verifyFormat("if (a) // Can't merge this\n" 234 " f();\n", 235 AllowsMergedIf); 236 verifyFormat("if (a) /* still don't merge */\n" 237 " f();", 238 AllowsMergedIf); 239 verifyFormat("if (a) { // Never merge this\n" 240 " f();\n" 241 "}", 242 AllowsMergedIf); 243 verifyFormat("if (a) { /* Never merge this */\n" 244 " f();\n" 245 "}", 246 AllowsMergedIf); 247 248 AllowsMergedIf.ColumnLimit = 14; 249 verifyFormat("if (a) return;", AllowsMergedIf); 250 verifyFormat("if (aaaaaaaaa)\n" 251 " return;", 252 AllowsMergedIf); 253 254 AllowsMergedIf.ColumnLimit = 13; 255 verifyFormat("if (a)\n return;", AllowsMergedIf); 256 } 257 258 TEST_F(FormatTest, ParseIfElse) { 259 verifyFormat("if (true)\n" 260 " if (true)\n" 261 " if (true)\n" 262 " f();\n" 263 " else\n" 264 " g();\n" 265 " else\n" 266 " h();\n" 267 "else\n" 268 " i();"); 269 verifyFormat("if (true)\n" 270 " if (true)\n" 271 " if (true) {\n" 272 " if (true)\n" 273 " f();\n" 274 " } else {\n" 275 " g();\n" 276 " }\n" 277 " else\n" 278 " h();\n" 279 "else {\n" 280 " i();\n" 281 "}"); 282 } 283 284 TEST_F(FormatTest, ElseIf) { 285 verifyFormat("if (a) {\n} else if (b) {\n}"); 286 verifyFormat("if (a)\n" 287 " f();\n" 288 "else if (b)\n" 289 " g();\n" 290 "else\n" 291 " h();"); 292 } 293 294 TEST_F(FormatTest, FormatsForLoop) { 295 verifyFormat( 296 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 297 " ++VeryVeryLongLoopVariable)\n" 298 " ;"); 299 verifyFormat("for (;;)\n" 300 " f();"); 301 verifyFormat("for (;;) {\n}"); 302 verifyFormat("for (;;) {\n" 303 " f();\n" 304 "}"); 305 306 verifyFormat( 307 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 308 " E = UnwrappedLines.end();\n" 309 " I != E; ++I) {\n}"); 310 311 verifyFormat( 312 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 313 " ++IIIII) {\n}"); 314 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 315 " aaaaaaaaaaa = aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 316 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 317 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 318 " I = FD->getDeclsInPrototypeScope().begin(),\n" 319 " E = FD->getDeclsInPrototypeScope().end();\n" 320 " I != E; ++I) {\n}"); 321 322 // FIXME: Not sure whether we want extra identation in line 3 here: 323 verifyFormat( 324 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 327 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 328 " ++aaaaaaaaaaa) {\n}"); 329 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 330 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 331 "}"); 332 333 FormatStyle NoBinPacking = getLLVMStyle(); 334 NoBinPacking.BinPackParameters = false; 335 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 336 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 337 " aaaaaaaaaaaaaaaa,\n" 338 " aaaaaaaaaaaaaaaa,\n" 339 " aaaaaaaaaaaaaaaa);\n" 340 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 341 "}", 342 NoBinPacking); 343 verifyFormat( 344 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 345 " E = UnwrappedLines.end();\n" 346 " I != E;\n" 347 " ++I) {\n}", 348 NoBinPacking); 349 } 350 351 TEST_F(FormatTest, RangeBasedForLoops) { 352 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 354 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 355 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 356 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 358 } 359 360 TEST_F(FormatTest, FormatsWhileLoop) { 361 verifyFormat("while (true) {\n}"); 362 verifyFormat("while (true)\n" 363 " f();"); 364 verifyFormat("while () {\n}"); 365 verifyFormat("while () {\n" 366 " f();\n" 367 "}"); 368 } 369 370 TEST_F(FormatTest, FormatsDoWhile) { 371 verifyFormat("do {\n" 372 " do_something();\n" 373 "} while (something());"); 374 verifyFormat("do\n" 375 " do_something();\n" 376 "while (something());"); 377 } 378 379 TEST_F(FormatTest, FormatsSwitchStatement) { 380 verifyFormat("switch (x) {\n" 381 "case 1:\n" 382 " f();\n" 383 " break;\n" 384 "case kFoo:\n" 385 "case ns::kBar:\n" 386 "case kBaz:\n" 387 " break;\n" 388 "default:\n" 389 " g();\n" 390 " break;\n" 391 "}"); 392 verifyFormat("switch (x) {\n" 393 "case 1: {\n" 394 " f();\n" 395 " break;\n" 396 "}\n" 397 "}"); 398 verifyFormat("switch (x) {\n" 399 "case 1: {\n" 400 " f();\n" 401 " {\n" 402 " g();\n" 403 " h();\n" 404 " }\n" 405 " break;\n" 406 "}\n" 407 "}"); 408 verifyFormat("switch (x) {\n" 409 "case 1: {\n" 410 " f();\n" 411 " if (foo) {\n" 412 " g();\n" 413 " h();\n" 414 " }\n" 415 " break;\n" 416 "}\n" 417 "}"); 418 verifyFormat("switch (x) {\n" 419 "case 1: {\n" 420 " f();\n" 421 " g();\n" 422 "} break;\n" 423 "}"); 424 verifyFormat("switch (test)\n" 425 " ;"); 426 verifyFormat("switch (x) {\n" 427 "default: {\n" 428 " // Do nothing.\n" 429 "}"); 430 verifyFormat("switch (x) {\n" 431 "// if 1, do f()\n" 432 "case 1:\n" 433 " f();\n" 434 "}"); 435 verifyFormat("switch (x) {\n" 436 "case 1:\n" 437 " // Do amazing stuff\n" 438 " {\n" 439 " f();\n" 440 " g();\n" 441 " }\n" 442 " break;\n" 443 "}"); 444 verifyFormat("#define A \\\n" 445 " switch (x) { \\\n" 446 " case a: \\\n" 447 " foo = b; \\\n" 448 " }", getLLVMStyleWithColumns(20)); 449 450 verifyGoogleFormat("switch (x) {\n" 451 " case 1:\n" 452 " f();\n" 453 " break;\n" 454 " case kFoo:\n" 455 " case ns::kBar:\n" 456 " case kBaz:\n" 457 " break;\n" 458 " default:\n" 459 " g();\n" 460 " break;\n" 461 "}"); 462 verifyGoogleFormat("switch (x) {\n" 463 " case 1: {\n" 464 " f();\n" 465 " break;\n" 466 " }\n" 467 "}"); 468 verifyGoogleFormat("switch (test)\n" 469 " ;"); 470 } 471 472 TEST_F(FormatTest, FormatsLabels) { 473 verifyFormat("void f() {\n" 474 " some_code();\n" 475 "test_label:\n" 476 " some_other_code();\n" 477 " {\n" 478 " some_more_code();\n" 479 " another_label:\n" 480 " some_more_code();\n" 481 " }\n" 482 "}"); 483 verifyFormat("some_code();\n" 484 "test_label:\n" 485 "some_other_code();"); 486 } 487 488 //===----------------------------------------------------------------------===// 489 // Tests for comments. 490 //===----------------------------------------------------------------------===// 491 492 TEST_F(FormatTest, UnderstandsSingleLineComments) { 493 verifyFormat("// line 1\n" 494 "// line 2\n" 495 "void f() {}\n"); 496 497 verifyFormat("void f() {\n" 498 " // Doesn't do anything\n" 499 "}"); 500 verifyFormat("void f(int i, // some comment (probably for i)\n" 501 " int j, // some comment (probably for j)\n" 502 " int k); // some comment (probably for k)"); 503 verifyFormat("void f(int i,\n" 504 " // some comment (probably for j)\n" 505 " int j,\n" 506 " // some comment (probably for k)\n" 507 " int k);"); 508 509 verifyFormat("int i // This is a fancy variable\n" 510 " = 5; // with nicely aligned comment."); 511 512 verifyFormat("// Leading comment.\n" 513 "int a; // Trailing comment."); 514 verifyFormat("int a; // Trailing comment\n" 515 " // on 2\n" 516 " // or 3 lines.\n" 517 "int b;"); 518 verifyFormat("int a; // Trailing comment\n" 519 "\n" 520 "// Leading comment.\n" 521 "int b;"); 522 verifyFormat("int a; // Comment.\n" 523 " // More details.\n" 524 "int bbbb; // Another comment."); 525 verifyFormat( 526 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 527 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 528 "int cccccccccccccccccccccccccccccc; // comment\n" 529 "int ddd; // looooooooooooooooooooooooong comment\n" 530 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 531 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 532 "int ccccccccccccccccccc; // comment"); 533 534 verifyFormat("#include \"a\" // comment\n" 535 "#include \"a/b/c\" // comment"); 536 verifyFormat("#include <a> // comment\n" 537 "#include <a/b/c> // comment"); 538 539 verifyFormat("enum E {\n" 540 " // comment\n" 541 " VAL_A, // comment\n" 542 " VAL_B\n" 543 "};"); 544 545 verifyFormat( 546 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 547 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 548 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 549 " // Comment inside a statement.\n" 550 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 551 verifyFormat( 552 "bool aaaaaaaaaaaaa = // comment\n" 553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 555 556 verifyFormat("int aaaa; // aaaaa\n" 557 "int aa; // aaaaaaa", 558 getLLVMStyleWithColumns(20)); 559 560 EXPECT_EQ("void f() { // This does something ..\n" 561 "}\n" 562 "int a; // This is unrelated", 563 format("void f() { // This does something ..\n" 564 " }\n" 565 "int a; // This is unrelated")); 566 EXPECT_EQ("void f() { // This does something ..\n" 567 "} // awesome..\n" 568 "\n" 569 "int a; // This is unrelated", 570 format("void f() { // This does something ..\n" 571 " } // awesome..\n" 572 " \n" 573 "int a; // This is unrelated")); 574 575 EXPECT_EQ("int i; // single line trailing comment", 576 format("int i;\\\n// single line trailing comment")); 577 578 verifyGoogleFormat("int a; // Trailing comment."); 579 580 verifyFormat("someFunction(anotherFunction( // Force break.\n" 581 " parameter));"); 582 583 verifyGoogleFormat("#endif // HEADER_GUARD"); 584 585 verifyFormat("const char *test[] = {\n" 586 " // A\n" 587 " \"aaaa\",\n" 588 " // B\n" 589 " \"aaaaa\",\n" 590 "};"); 591 verifyGoogleFormat( 592 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 593 " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); 594 } 595 596 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 597 EXPECT_EQ("// comment", format("// comment ")); 598 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 599 format("int aaaaaaa, bbbbbbb; // comment ", 600 getLLVMStyleWithColumns(33))); 601 } 602 603 TEST_F(FormatTest, UnderstandsMultiLineComments) { 604 verifyFormat("f(/*test=*/ true);"); 605 EXPECT_EQ( 606 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 607 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 608 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" 609 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 610 EXPECT_EQ( 611 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 612 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 613 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 614 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 615 616 FormatStyle NoBinPacking = getLLVMStyle(); 617 NoBinPacking.BinPackParameters = false; 618 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 619 " /* parameter 2 */ aaaaaa,\n" 620 " /* parameter 3 */ aaaaaa,\n" 621 " /* parameter 4 */ aaaaaa);", 622 NoBinPacking); 623 } 624 625 TEST_F(FormatTest, AlignsMultiLineComments) { 626 EXPECT_EQ("/*\n" 627 " * Really multi-line\n" 628 " * comment.\n" 629 " */\n" 630 "void f() {}", 631 format(" /*\n" 632 " * Really multi-line\n" 633 " * comment.\n" 634 " */\n" 635 " void f() {}")); 636 EXPECT_EQ("class C {\n" 637 " /*\n" 638 " * Another multi-line\n" 639 " * comment.\n" 640 " */\n" 641 " void f() {}\n" 642 "};", 643 format("class C {\n" 644 "/*\n" 645 " * Another multi-line\n" 646 " * comment.\n" 647 " */\n" 648 "void f() {}\n" 649 "};")); 650 EXPECT_EQ("/*\n" 651 " 1. This is a comment with non-trivial formatting.\n" 652 " 1.1. We have to indent/outdent all lines equally\n" 653 " 1.1.1. to keep the formatting.\n" 654 " */", 655 format(" /*\n" 656 " 1. This is a comment with non-trivial formatting.\n" 657 " 1.1. We have to indent/outdent all lines equally\n" 658 " 1.1.1. to keep the formatting.\n" 659 " */")); 660 EXPECT_EQ("/*\n" 661 " Don't try to outdent if there's not enough inentation.\n" 662 " */", 663 format(" /*\n" 664 " Don't try to outdent if there's not enough inentation.\n" 665 " */")); 666 } 667 668 TEST_F(FormatTest, SplitsLongLinesInComments) { 669 EXPECT_EQ("/* This is a long\n" 670 "comment that doesn't\n" 671 "fit on one line. */", 672 format("/* " 673 "This is a long " 674 "comment that doesn't " 675 "fit on one line. */", 676 getLLVMStyleWithColumns(20))); 677 EXPECT_EQ("/*\n" 678 "This is a long\n" 679 "comment that doesn't\n" 680 "fit on one line.\n" 681 "*/", 682 format("/*\n" 683 "This is a long " 684 "comment that doesn't " 685 "fit on one line. \n" 686 "*/", getLLVMStyleWithColumns(20))); 687 EXPECT_EQ("/*\n" 688 " * This is a long\n" 689 " * comment that\n" 690 " * doesn't fit on\n" 691 " * one line.\n" 692 " */", 693 format("/*\n" 694 " * This is a long " 695 " comment that " 696 " doesn't fit on " 697 " one line. \n" 698 " */", getLLVMStyleWithColumns(20))); 699 EXPECT_EQ("/*\n" 700 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 701 " * so_it_should_be_broken\n" 702 " * wherever_a_space_occurs\n" 703 " */", 704 format("/*\n" 705 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 706 " so_it_should_be_broken " 707 " wherever_a_space_occurs \n" 708 " */", 709 getLLVMStyleWithColumns(20))); 710 EXPECT_EQ("/*\n" 711 " * This_comment_can_not_be_broken_into_lines\n" 712 " */", 713 format("/*\n" 714 " * This_comment_can_not_be_broken_into_lines\n" 715 " */", 716 getLLVMStyleWithColumns(20))); 717 EXPECT_EQ("{\n" 718 " /*\n" 719 " This is another\n" 720 " long comment that\n" 721 " doesn't fit on one\n" 722 " line 1234567890\n" 723 " */\n" 724 "}", 725 format("{\n" 726 "/*\n" 727 "This is another " 728 " long comment that " 729 " doesn't fit on one" 730 " line 1234567890\n" 731 "*/\n" 732 "}", getLLVMStyleWithColumns(20))); 733 EXPECT_EQ("{\n" 734 " /*\n" 735 " * This i s\n" 736 " * another comment\n" 737 " * t hat doesn' t\n" 738 " * fit on one l i\n" 739 " * n e\n" 740 " */\n" 741 "}", 742 format("{\n" 743 "/*\n" 744 " * This i s" 745 " another comment" 746 " t hat doesn' t" 747 " fit on one l i" 748 " n e\n" 749 " */\n" 750 "}", getLLVMStyleWithColumns(20))); 751 EXPECT_EQ("/*\n" 752 " * This is a long\n" 753 " * comment that\n" 754 " * doesn't fit on\n" 755 " * one line\n" 756 " */", 757 format(" /*\n" 758 " * This is a long comment that doesn't fit on one line\n" 759 " */", getLLVMStyleWithColumns(20))); 760 } 761 762 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 763 EXPECT_EQ("#define X \\\n" 764 // FIXME: Backslash should be added here. 765 " /*\n" 766 " Macro comment \\\n" 767 " with a long \\\n" 768 " line \\\n" 769 // FIXME: We should look at the length of the last line of the token 770 // instead of the full token's length. 771 //" */ \\\n" 772 " */\\\n" 773 " A + B", 774 format("#define X \\\n" 775 " /*\n" 776 " Macro comment with a long line\n" 777 " */ \\\n" 778 " A + B", 779 getLLVMStyleWithColumns(20))); 780 EXPECT_EQ("#define X \\\n" 781 " /* Macro comment \\\n" 782 // FIXME: Indent comment continuations when the comment is a first 783 // token in a line. 784 "with a long line \\\n" 785 // FIXME: We should look at the length of the last line of the token 786 // instead of the full token's length. 787 //"*/ \\\n" 788 "*/\\\n" 789 " A + B", 790 format("#define X \\\n" 791 " /* Macro comment with a long line */ \\\n" 792 " A + B", 793 getLLVMStyleWithColumns(20))); 794 } 795 796 TEST_F(FormatTest, CommentsInStaticInitializers) { 797 EXPECT_EQ( 798 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 799 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 800 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 801 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 802 " aaaaaaaaaaaaaaaaaaaa };", 803 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 804 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 805 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 806 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 807 " aaaaaaaaaaaaaaaaaaaa };")); 808 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 809 " bbbbbbbbbbb, ccccccccccc };"); 810 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" 811 " // comment for bb....\n" 812 " bbbbbbbbbbb, ccccccccccc };"); 813 verifyGoogleFormat( 814 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 815 " bbbbbbbbbbb, ccccccccccc };"); 816 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" 817 " // comment for bb....\n" 818 " bbbbbbbbbbb, ccccccccccc };"); 819 820 verifyFormat("S s = { { a, b, c }, // Group #1\n" 821 " { d, e, f }, // Group #2\n" 822 " { g, h, i } }; // Group #3"); 823 verifyFormat("S s = { { // Group #1\n" 824 " a, b, c },\n" 825 " { // Group #2\n" 826 " d, e, f },\n" 827 " { // Group #3\n" 828 " g, h, i } };"); 829 830 EXPECT_EQ("S s = {\n" 831 " // Some comment\n" 832 " a,\n" 833 "\n" 834 " // Comment after empty line\n" 835 " b\n" 836 "}", 837 format("S s = {\n" 838 " // Some comment\n" 839 " a,\n" 840 " \n" 841 " // Comment after empty line\n" 842 " b\n" 843 "}")); 844 EXPECT_EQ("S s = { a, b };", format("S s = {\n" 845 " a,\n" 846 "\n" 847 " b\n" 848 "};")); 849 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 850 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 851 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 852 " 0x00, 0x00, 0x00, 0x00 // comment\n" 853 "};"); 854 } 855 856 //===----------------------------------------------------------------------===// 857 // Tests for classes, namespaces, etc. 858 //===----------------------------------------------------------------------===// 859 860 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 861 verifyFormat("class A {\n};"); 862 } 863 864 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 865 verifyFormat("class A {\n" 866 "public:\n" 867 "protected:\n" 868 "private:\n" 869 " void f() {}\n" 870 "};"); 871 verifyGoogleFormat("class A {\n" 872 " public:\n" 873 " protected:\n" 874 " private:\n" 875 " void f() {}\n" 876 "};"); 877 } 878 879 TEST_F(FormatTest, FormatsDerivedClass) { 880 verifyFormat("class A : public B {\n};"); 881 verifyFormat("class A : public ::B {\n};"); 882 883 verifyFormat( 884 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 885 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" 886 "};\n"); 887 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n" 888 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 889 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" 890 "};\n"); 891 verifyFormat( 892 "class A : public B, public C, public D, public E, public F, public G {\n" 893 "};"); 894 verifyFormat("class AAAAAAAAAAAA : public B,\n" 895 " public C,\n" 896 " public D,\n" 897 " public E,\n" 898 " public F,\n" 899 " public G {\n" 900 "};"); 901 } 902 903 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 904 verifyFormat("class A {\n} a, b;"); 905 verifyFormat("struct A {\n} a, b;"); 906 verifyFormat("union A {\n} a;"); 907 } 908 909 TEST_F(FormatTest, FormatsEnum) { 910 verifyFormat("enum {\n" 911 " Zero,\n" 912 " One = 1,\n" 913 " Two = One + 1,\n" 914 " Three = (One + Two),\n" 915 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 916 " Five = (One, Two, Three, Four, 5)\n" 917 "};"); 918 verifyFormat("enum Enum {\n" 919 "};"); 920 verifyFormat("enum {\n" 921 "};"); 922 verifyFormat("enum X E {\n} d;"); 923 verifyFormat("enum __attribute__((...)) E {\n} d;"); 924 verifyFormat("enum __declspec__((...)) E {\n} d;"); 925 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 926 } 927 928 TEST_F(FormatTest, FormatsBitfields) { 929 verifyFormat("struct Bitfields {\n" 930 " unsigned sClass : 8;\n" 931 " unsigned ValueKind : 2;\n" 932 "};"); 933 } 934 935 TEST_F(FormatTest, FormatsNamespaces) { 936 verifyFormat("namespace some_namespace {\n" 937 "class A {\n};\n" 938 "void f() { f(); }\n" 939 "}"); 940 verifyFormat("namespace {\n" 941 "class A {\n};\n" 942 "void f() { f(); }\n" 943 "}"); 944 verifyFormat("inline namespace X {\n" 945 "class A {\n};\n" 946 "void f() { f(); }\n" 947 "}"); 948 verifyFormat("using namespace some_namespace;\n" 949 "class A {\n};\n" 950 "void f() { f(); }"); 951 952 // This code is more common than we thought; if we 953 // layout this correctly the semicolon will go into 954 // its own line, which is undesireable. 955 verifyFormat("namespace {\n};"); 956 verifyFormat("namespace {\n" 957 "class A {\n" 958 "};\n" 959 "};"); 960 } 961 962 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 963 964 TEST_F(FormatTest, FormatsInlineASM) { 965 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 966 verifyFormat( 967 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 968 " \"cpuid\\n\\t\"\n" 969 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 970 " : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n" 971 " : \"a\"(value));"); 972 } 973 974 TEST_F(FormatTest, FormatTryCatch) { 975 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll 976 // also not create single-line-blocks. 977 verifyFormat("try {\n" 978 " throw a * b;\n" 979 "}\n" 980 "catch (int a) {\n" 981 " // Do nothing.\n" 982 "}\n" 983 "catch (...) {\n" 984 " exit(42);\n" 985 "}"); 986 987 // Function-level try statements. 988 verifyFormat("int f() try { return 4; }\n" 989 "catch (...) {\n" 990 " return 5;\n" 991 "}"); 992 verifyFormat("class A {\n" 993 " int a;\n" 994 " A() try : a(0) {}\n" 995 " catch (...) {\n" 996 " throw;\n" 997 " }\n" 998 "};\n"); 999 } 1000 1001 TEST_F(FormatTest, FormatObjCTryCatch) { 1002 verifyFormat("@try {\n" 1003 " f();\n" 1004 "}\n" 1005 "@catch (NSException e) {\n" 1006 " @throw;\n" 1007 "}\n" 1008 "@finally {\n" 1009 " exit(42);\n" 1010 "}"); 1011 } 1012 1013 TEST_F(FormatTest, StaticInitializers) { 1014 verifyFormat("static SomeClass SC = { 1, 'a' };"); 1015 1016 // FIXME: Format like enums if the static initializer does not fit on a line. 1017 verifyFormat( 1018 "static SomeClass WithALoooooooooooooooooooongName = {\n" 1019 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1020 "};"); 1021 1022 verifyFormat( 1023 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 1024 " looooooooooooooooooooooooooooooooooongname,\n" 1025 " looooooooooooooooooooooooooooooong };"); 1026 // Allow bin-packing in static initializers as this would often lead to 1027 // terrible results, e.g.: 1028 verifyGoogleFormat( 1029 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 1030 " looooooooooooooooooooooooooooooooooongname,\n" 1031 " looooooooooooooooooooooooooooooong };"); 1032 } 1033 1034 TEST_F(FormatTest, NestedStaticInitializers) { 1035 verifyFormat("static A x = { { {} } };\n"); 1036 verifyFormat("static A x = { { { init1, init2, init3, init4 },\n" 1037 " { init1, init2, init3, init4 } } };"); 1038 1039 verifyFormat("somes Status::global_reps[3] = {\n" 1040 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 1041 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 1042 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 1043 "};"); 1044 verifyGoogleFormat("somes Status::global_reps[3] = {\n" 1045 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 1046 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 1047 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 1048 "};"); 1049 verifyFormat( 1050 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" 1051 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" 1052 " } };"); 1053 1054 verifyFormat( 1055 "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" 1056 " { 111111111111111111111111111111,\n" 1057 " 222222222222222222222222222222,\n" 1058 " 333333333333333333333333333333 },\n" 1059 " { 1, 2, 3 }, { 1, 2, 3 } } };"); 1060 verifyFormat( 1061 "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" 1062 " { { 111111111111111111111111111111,\n" 1063 " 222222222222222222222222222222,\n" 1064 " 333333333333333333333333333333 } },\n" 1065 " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); 1066 1067 // FIXME: We might at some point want to handle this similar to parameter 1068 // lists, where we have an option to put each on a single line. 1069 verifyFormat( 1070 "struct {\n" 1071 " unsigned bit;\n" 1072 " const char *const name;\n" 1073 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" 1074 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); 1075 } 1076 1077 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1078 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1079 " \\\n" 1080 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1081 } 1082 1083 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1084 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 1085 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1086 } 1087 1088 TEST_F(FormatTest, LayoutUnknownPPDirective) { 1089 EXPECT_EQ("#123 \"A string literal\"", 1090 format(" # 123 \"A string literal\"")); 1091 EXPECT_EQ("#;", format("#;")); 1092 verifyFormat("#\n;\n;\n;"); 1093 } 1094 1095 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1096 EXPECT_EQ("#line 42 \"test\"\n", 1097 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1098 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1099 getLLVMStyleWithColumns(12))); 1100 } 1101 1102 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1103 EXPECT_EQ("#line 42 \"test\"", 1104 format("# \\\n line \\\n 42 \\\n \"test\"")); 1105 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1106 } 1107 1108 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1109 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1110 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1111 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1112 // FIXME: We never break before the macro name. 1113 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); 1114 1115 verifyFormat("#define A A\n#define A A"); 1116 verifyFormat("#define A(X) A\n#define A A"); 1117 1118 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1119 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1120 } 1121 1122 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1123 EXPECT_EQ("// some comment\n" 1124 "#include \"a.h\"\n" 1125 "#define A( \\\n" 1126 " A, B)\n" 1127 "#include \"b.h\"\n" 1128 "// some comment\n", 1129 format(" // some comment\n" 1130 " #include \"a.h\"\n" 1131 "#define A(A,\\\n" 1132 " B)\n" 1133 " #include \"b.h\"\n" 1134 " // some comment\n", 1135 getLLVMStyleWithColumns(13))); 1136 } 1137 1138 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1139 1140 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1141 EXPECT_EQ("#define A \\\n" 1142 " c; \\\n" 1143 " e;\n" 1144 "f;", 1145 format("#define A c; e;\n" 1146 "f;", 1147 getLLVMStyleWithColumns(14))); 1148 } 1149 1150 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1151 1152 TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { 1153 EXPECT_EQ("# define A\\\n b;", 1154 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); 1155 } 1156 1157 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1158 EXPECT_EQ("int x,\n" 1159 "#define A\n" 1160 " y;", 1161 format("int x,\n#define A\ny;")); 1162 } 1163 1164 TEST_F(FormatTest, HashInMacroDefinition) { 1165 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 1166 verifyFormat("#define A \\\n" 1167 " { \\\n" 1168 " f(#c);\\\n" 1169 " }", 1170 getLLVMStyleWithColumns(11)); 1171 1172 verifyFormat("#define A(X) \\\n" 1173 " void function##X()", 1174 getLLVMStyleWithColumns(22)); 1175 1176 verifyFormat("#define A(a, b, c) \\\n" 1177 " void a##b##c()", 1178 getLLVMStyleWithColumns(22)); 1179 1180 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 1181 } 1182 1183 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1184 verifyFormat("#define A (1)"); 1185 } 1186 1187 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 1188 EXPECT_EQ("#define A b;", format("#define A \\\n" 1189 " \\\n" 1190 " b;", 1191 getLLVMStyleWithColumns(25))); 1192 EXPECT_EQ("#define A \\\n" 1193 " \\\n" 1194 " a; \\\n" 1195 " b;", 1196 format("#define A \\\n" 1197 " \\\n" 1198 " a; \\\n" 1199 " b;", 1200 getLLVMStyleWithColumns(11))); 1201 EXPECT_EQ("#define A \\\n" 1202 " a; \\\n" 1203 " \\\n" 1204 " b;", 1205 format("#define A \\\n" 1206 " a; \\\n" 1207 " \\\n" 1208 " b;", 1209 getLLVMStyleWithColumns(11))); 1210 } 1211 1212 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 1213 verifyFormat("#define A :"); 1214 1215 // FIXME: Improve formatting of case labels in macros. 1216 verifyFormat("#define SOMECASES \\\n" 1217 " case 1: \\\n" 1218 " case 2\n", 1219 getLLVMStyleWithColumns(20)); 1220 1221 verifyFormat("#define A template <typename T>"); 1222 verifyFormat("#define STR(x) #x\n" 1223 "f(STR(this_is_a_string_literal{));"); 1224 } 1225 1226 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 1227 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 1228 } 1229 1230 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 1231 verifyFormat("{\n { a #c; }\n}"); 1232 } 1233 1234 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 1235 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 1236 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 1237 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 1238 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 1239 } 1240 1241 TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { 1242 EXPECT_EQ( 1243 "#define A \\\n int i; \\\n int j;", 1244 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 1245 } 1246 1247 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 1248 verifyFormat("#define A \\\n" 1249 " int v( \\\n" 1250 " a); \\\n" 1251 " int i;", 1252 getLLVMStyleWithColumns(11)); 1253 } 1254 1255 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 1256 EXPECT_EQ( 1257 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1258 " \\\n" 1259 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 1260 "\n" 1261 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 1262 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 1263 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1264 "\\\n" 1265 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 1266 " \n" 1267 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 1268 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 1269 } 1270 1271 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 1272 EXPECT_EQ("int\n" 1273 "#define A\n" 1274 " a;", 1275 format("int\n#define A\na;")); 1276 verifyFormat("functionCallTo(\n" 1277 " someOtherFunction(\n" 1278 " withSomeParameters, whichInSequence,\n" 1279 " areLongerThanALine(andAnotherCall,\n" 1280 "#define A B\n" 1281 " withMoreParamters,\n" 1282 " whichStronglyInfluenceTheLayout),\n" 1283 " andMoreParameters),\n" 1284 " trailing);", 1285 getLLVMStyleWithColumns(69)); 1286 } 1287 1288 TEST_F(FormatTest, LayoutBlockInsideParens) { 1289 EXPECT_EQ("functionCall({\n" 1290 " int i;\n" 1291 "});", 1292 format(" functionCall ( {int i;} );")); 1293 } 1294 1295 TEST_F(FormatTest, LayoutBlockInsideStatement) { 1296 EXPECT_EQ("SOME_MACRO { int i; }\n" 1297 "int i;", 1298 format(" SOME_MACRO {int i;} int i;")); 1299 } 1300 1301 TEST_F(FormatTest, LayoutNestedBlocks) { 1302 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 1303 " struct s {\n" 1304 " int i;\n" 1305 " };\n" 1306 " s kBitsToOs[] = { { 10 } };\n" 1307 " for (int i = 0; i < 10; ++i)\n" 1308 " return;\n" 1309 "}"); 1310 } 1311 1312 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 1313 EXPECT_EQ("{}", format("{}")); 1314 1315 // Negative test for enum. 1316 verifyFormat("enum E {\n};"); 1317 1318 // Note that when there's a missing ';', we still join... 1319 verifyFormat("enum E {}"); 1320 } 1321 1322 //===----------------------------------------------------------------------===// 1323 // Line break tests. 1324 //===----------------------------------------------------------------------===// 1325 1326 TEST_F(FormatTest, FormatsFunctionDefinition) { 1327 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," 1328 " int h, int j, int f,\n" 1329 " int c, int ddddddddddddd) {}"); 1330 } 1331 1332 TEST_F(FormatTest, FormatsAwesomeMethodCall) { 1333 verifyFormat( 1334 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 1335 " parameter, parameter, parameter)),\n" 1336 " SecondLongCall(parameter));"); 1337 } 1338 1339 TEST_F(FormatTest, PreventConfusingIndents) { 1340 verifyFormat( 1341 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1342 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 1343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1344 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 1345 verifyFormat( 1346 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" 1347 " aaaaaaaaaaaaaaaaaaaaaaaa[\n" 1348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" 1349 " aaaaaaaaaaaaaaaaaaaaaaaa];"); 1350 verifyFormat( 1351 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 1352 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 1353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 1354 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 1355 verifyFormat("int a = bbbb && ccc && fffff(\n" 1356 "#define A Just forcing a new line\n" 1357 " ddd);"); 1358 } 1359 1360 TEST_F(FormatTest, ConstructorInitializers) { 1361 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 1362 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 1363 getLLVMStyleWithColumns(45)); 1364 verifyFormat("Constructor()\n" 1365 " : Inttializer(FitsOnTheLine) {}", 1366 getLLVMStyleWithColumns(44)); 1367 verifyFormat("Constructor()\n" 1368 " : Inttializer(FitsOnTheLine) {}", 1369 getLLVMStyleWithColumns(43)); 1370 1371 verifyFormat( 1372 "SomeClass::Constructor()\n" 1373 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 1374 1375 verifyFormat( 1376 "SomeClass::Constructor()\n" 1377 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1378 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 1379 verifyFormat( 1380 "SomeClass::Constructor()\n" 1381 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1382 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 1383 1384 verifyFormat("Constructor()\n" 1385 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1386 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1387 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1388 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 1389 1390 verifyFormat("Constructor()\n" 1391 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 1393 1394 verifyFormat("Constructor(int Parameter = 0)\n" 1395 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 1396 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 1397 1398 // Here a line could be saved by splitting the second initializer onto two 1399 // lines, but that is not desireable. 1400 verifyFormat("Constructor()\n" 1401 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 1402 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 1403 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 1404 1405 FormatStyle OnePerLine = getLLVMStyle(); 1406 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 1407 verifyFormat("SomeClass::Constructor()\n" 1408 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1409 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1410 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 1411 OnePerLine); 1412 verifyFormat("SomeClass::Constructor()\n" 1413 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 1414 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1415 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 1416 OnePerLine); 1417 verifyFormat("MyClass::MyClass(int var)\n" 1418 " : some_var_(var), // 4 space indent\n" 1419 " some_other_var_(var + 1) { // lined up\n" 1420 "}", 1421 OnePerLine); 1422 verifyFormat("Constructor()\n" 1423 " : aaaaa(aaaaaa),\n" 1424 " aaaaa(aaaaaa),\n" 1425 " aaaaa(aaaaaa),\n" 1426 " aaaaa(aaaaaa),\n" 1427 " aaaaa(aaaaaa) {}", 1428 OnePerLine); 1429 1430 // This test takes VERY long when memoization is broken. 1431 OnePerLine.BinPackParameters = false; 1432 std::string input = "Constructor()\n" 1433 " : aaaa(a,\n"; 1434 for (unsigned i = 0, e = 80; i != e; ++i) { 1435 input += " a,\n"; 1436 } 1437 input += " a) {}"; 1438 verifyFormat(input, OnePerLine); 1439 } 1440 1441 TEST_F(FormatTest, BreaksAsHighAsPossible) { 1442 verifyFormat( 1443 "void f() {\n" 1444 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 1445 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 1446 " f();\n" 1447 "}"); 1448 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 1449 " Intervals[i - 1].getRange().getLast()) {\n}"); 1450 } 1451 1452 TEST_F(FormatTest, BreaksDesireably) { 1453 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1454 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1455 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 1456 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1458 "}"); 1459 1460 verifyFormat( 1461 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 1463 1464 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1467 1468 verifyFormat( 1469 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1471 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 1473 1474 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1475 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1476 1477 verifyFormat( 1478 "void f() {\n" 1479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1481 "}"); 1482 verifyFormat( 1483 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1485 verifyFormat( 1486 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1487 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1488 verifyFormat( 1489 "aaaaaaaaaaaaaaaaa(\n" 1490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1492 1493 // This test case breaks on an incorrect memoization, i.e. an optimization not 1494 // taking into account the StopAt value. 1495 verifyFormat( 1496 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1497 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1498 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1499 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1500 1501 verifyFormat("{\n {\n {\n" 1502 " Annotation.SpaceRequiredBefore =\n" 1503 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 1504 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 1505 " }\n }\n}"); 1506 } 1507 1508 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 1509 FormatStyle NoBinPacking = getLLVMStyle(); 1510 NoBinPacking.BinPackParameters = false; 1511 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 1512 " aaaaaaaaaaaaaaaaaaaa,\n" 1513 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 1514 NoBinPacking); 1515 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 1516 " aaaaaaaaaaaaa,\n" 1517 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 1518 NoBinPacking); 1519 verifyFormat( 1520 "aaaaaaaa(aaaaaaaaaaaaa,\n" 1521 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1523 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 1525 NoBinPacking); 1526 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 1527 " .aaaaaaaaaaaaaaaaaa();", 1528 NoBinPacking); 1529 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1530 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", 1531 NoBinPacking); 1532 1533 verifyFormat( 1534 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1535 " aaaaaaaaaaaa,\n" 1536 " aaaaaaaaaaaa);", 1537 NoBinPacking); 1538 verifyFormat( 1539 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 1540 " ddddddddddddddddddddddddddddd),\n" 1541 " test);", 1542 NoBinPacking); 1543 1544 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 1545 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 1546 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 1547 NoBinPacking); 1548 verifyFormat("a(\"a\"\n" 1549 " \"a\",\n" 1550 " a);"); 1551 1552 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 1553 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 1554 " aaaaaaaaa,\n" 1555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 1556 NoBinPacking); 1557 verifyFormat( 1558 "void f() {\n" 1559 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 1560 " .aaaaaaa();\n" 1561 "}", 1562 NoBinPacking); 1563 } 1564 1565 TEST_F(FormatTest, FormatsBuilderPattern) { 1566 verifyFormat( 1567 "return llvm::StringSwitch<Reference::Kind>(name)\n" 1568 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 1569 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" 1570 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" 1571 " .Default(ORDER_TEXT);\n"); 1572 1573 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 1574 " aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 1575 verifyFormat( 1576 "aaaaaaa->aaaaaaa\n" 1577 " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1578 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 1579 verifyFormat( 1580 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 1581 " aaaaaaaaaaaaaa);"); 1582 verifyFormat( 1583 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" 1584 " ->aaaaaaaaaaaaaaaa(\n" 1585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1586 " ->aaaaaaaaaaaaaaaaa();"); 1587 } 1588 1589 TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 1590 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1591 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 1592 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1593 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 1594 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1595 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 1596 verifyFormat( 1597 "void aaaaaaaaaaaaaaaaaa()\n" 1598 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 1599 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 1600 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1601 " __attribute__((unused));"); 1602 1603 // FIXME: This is bad indentation, but generally hard to distinguish from a 1604 // function declaration. 1605 verifyFormat( 1606 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1607 "GUARDED_BY(aaaaaaaaaaaa);"); 1608 } 1609 1610 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 1611 verifyFormat( 1612 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1613 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 1614 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1615 " ccccccccccccccccccccccccc) {\n}"); 1616 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1617 " ccccccccccccccccccccccccc) {\n}"); 1618 verifyFormat( 1619 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 1620 " ccccccccccccccccccccccccc) {\n}"); 1621 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 1622 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 1623 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 1624 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 1625 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 1626 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 1627 " aaaaaaaaaaaaaaa != aa) {\n}"); 1628 } 1629 1630 TEST_F(FormatTest, BreaksAfterAssignments) { 1631 verifyFormat( 1632 "unsigned Cost =\n" 1633 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 1634 " SI->getPointerAddressSpaceee());\n"); 1635 verifyFormat( 1636 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 1637 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 1638 1639 verifyFormat( 1640 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 1641 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 1642 } 1643 1644 TEST_F(FormatTest, AlignsAfterAssignments) { 1645 verifyFormat( 1646 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1647 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1648 verifyFormat( 1649 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1650 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1651 verifyFormat( 1652 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1653 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1654 verifyFormat( 1655 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1656 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1657 verifyFormat("double LooooooooooooooooooooooooongResult =\n" 1658 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 1659 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 1660 } 1661 1662 TEST_F(FormatTest, AlignsAfterReturn) { 1663 verifyFormat( 1664 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1665 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1666 verifyFormat( 1667 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1668 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1669 } 1670 1671 TEST_F(FormatTest, BreaksConditionalExpressions) { 1672 verifyFormat( 1673 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 1674 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1675 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1676 verifyFormat( 1677 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1678 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1679 verifyFormat( 1680 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 1681 " : aaaaaaaaaaaaa);"); 1682 verifyFormat( 1683 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1684 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1685 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1686 " aaaaaaaaaaaaa);"); 1687 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1688 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1690 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1692 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1694 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1696 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1698 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1699 1700 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1701 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1702 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1703 verifyFormat( 1704 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1705 " ? aaaaaaaaaaaaaaa\n" 1706 " : aaaaaaaaaaaaaaa;"); 1707 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 1708 " aaaaaaaaa\n" 1709 " ? b\n" 1710 " : c);"); 1711 verifyFormat( 1712 "unsigned Indent =\n" 1713 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 1714 " ? IndentForLevel[TheLine.Level]\n" 1715 " : TheLine * 2,\n" 1716 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 1717 getLLVMStyleWithColumns(70)); 1718 1719 FormatStyle NoBinPacking = getLLVMStyle(); 1720 NoBinPacking.BinPackParameters = false; 1721 verifyFormat( 1722 "void f() {\n" 1723 " g(aaa,\n" 1724 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 1725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1726 " ? aaaaaaaaaaaaaaa\n" 1727 " : aaaaaaaaaaaaaaa);\n" 1728 "}", 1729 NoBinPacking); 1730 } 1731 1732 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 1733 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 1734 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 1735 verifyFormat("bool a = true, b = false;"); 1736 1737 // FIXME: Indentation looks weird. 1738 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 1740 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 1741 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 1742 verifyFormat( 1743 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 1744 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 1745 " d = e && f;"); 1746 1747 } 1748 1749 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 1750 verifyFormat("arr[foo ? bar : baz];"); 1751 verifyFormat("f()[foo ? bar : baz];"); 1752 verifyFormat("(a + b)[foo ? bar : baz];"); 1753 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 1754 } 1755 1756 TEST_F(FormatTest, AlignsStringLiterals) { 1757 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 1758 " \"short literal\");"); 1759 verifyFormat( 1760 "looooooooooooooooooooooooongFunction(\n" 1761 " \"short literal\"\n" 1762 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 1763 verifyFormat("someFunction(\"Always break between multi-line\"\n" 1764 " \" string literals\",\n" 1765 " and, other, parameters);"); 1766 EXPECT_EQ("fun + \"1243\" /* comment */\n" 1767 " \"5678\";", 1768 format("fun + \"1243\" /* comment */\n" 1769 " \"5678\";", 1770 getLLVMStyleWithColumns(28))); 1771 EXPECT_EQ( 1772 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 1773 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 1774 " \"aaaaaaaaaaaaaaaa\";", 1775 format("aaaaaa =" 1776 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 1777 "aaaaaaaaaaaaaaaaaaaaa\" " 1778 "\"aaaaaaaaaaaaaaaa\";")); 1779 verifyFormat("a = a + \"a\"\n" 1780 " \"a\"\n" 1781 " \"a\";"); 1782 1783 verifyFormat( 1784 "#define LL_FORMAT \"ll\"\n" 1785 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 1786 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 1787 } 1788 1789 TEST_F(FormatTest, AlignsPipes) { 1790 verifyFormat( 1791 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1792 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1793 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1794 verifyFormat( 1795 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 1796 " << aaaaaaaaaaaaaaaaaaaa;"); 1797 verifyFormat( 1798 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1799 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1800 verifyFormat( 1801 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1802 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 1803 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 1804 verifyFormat( 1805 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1806 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1807 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1808 1809 verifyFormat("return out << \"somepacket = {\\n\"\n" 1810 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 1811 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 1812 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 1813 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 1814 " << \"}\";"); 1815 1816 verifyFormat( 1817 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 1818 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 1819 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 1820 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 1821 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 1822 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 1823 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 1824 } 1825 1826 TEST_F(FormatTest, UnderstandsEquals) { 1827 verifyFormat( 1828 "aaaaaaaaaaaaaaaaa =\n" 1829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1830 verifyFormat( 1831 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1833 verifyFormat( 1834 "if (a) {\n" 1835 " f();\n" 1836 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1838 "}"); 1839 1840 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1841 " 100000000 + 10000000) {\n}"); 1842 } 1843 1844 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 1845 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1846 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 1847 1848 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1849 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 1850 1851 verifyFormat( 1852 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 1853 " Parameter2);"); 1854 1855 verifyFormat( 1856 "ShortObject->shortFunction(\n" 1857 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 1858 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 1859 1860 verifyFormat("loooooooooooooongFunction(\n" 1861 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 1862 1863 verifyFormat( 1864 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 1865 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 1866 1867 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 1868 " .WillRepeatedly(Return(SomeValue));"); 1869 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" 1870 " .insert(ccccccccccccccccccccccc);"); 1871 verifyFormat( 1872 "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1874 " .aaaaaaaaaaaaaaa(\n" 1875 " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1876 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1877 1878 // Here, it is not necessary to wrap at "." or "->". 1879 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 1880 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1881 verifyFormat( 1882 "aaaaaaaaaaa->aaaaaaaaa(\n" 1883 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1884 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 1885 1886 verifyFormat( 1887 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 1889 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 1890 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 1891 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 1892 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 1893 1894 // FIXME: Should we break before .a()? 1895 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); 1897 1898 FormatStyle NoBinPacking = getLLVMStyle(); 1899 NoBinPacking.BinPackParameters = false; 1900 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 1901 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 1902 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 1903 " aaaaaaaaaaaaaaaaaaa,\n" 1904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 1905 NoBinPacking); 1906 } 1907 1908 TEST_F(FormatTest, WrapsTemplateDeclarations) { 1909 verifyFormat("template <typename T>\n" 1910 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 1911 verifyFormat( 1912 "template <typename T>\n" 1913 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 1914 verifyFormat("template <typename T>\n" 1915 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 1916 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 1917 verifyFormat( 1918 "template <typename T>\n" 1919 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 1920 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 1921 verifyFormat( 1922 "template <typename T>\n" 1923 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 1924 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 1925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1926 verifyFormat("template <typename T>\n" 1927 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1928 " int aaaaaaaaaaaaaaaaa);"); 1929 verifyFormat( 1930 "template <typename T1, typename T2 = char, typename T3 = char,\n" 1931 " typename T4 = char>\n" 1932 "void f();"); 1933 verifyFormat( 1934 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 1935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1936 1937 verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 1938 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1939 } 1940 1941 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 1942 verifyFormat( 1943 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1945 verifyFormat( 1946 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 1949 1950 // FIXME: Should we have an extra indent after the second break? 1951 verifyFormat( 1952 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1955 1956 // FIXME: Look into whether we should indent 4 from the start or 4 from 1957 // "bbbbb..." here instead of what we are doing now. 1958 verifyFormat( 1959 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 1960 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 1961 1962 // Breaking at nested name specifiers is generally not desirable. 1963 verifyFormat( 1964 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1965 " aaaaaaaaaaaaaaaaaaaaaaa);"); 1966 1967 verifyFormat( 1968 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1969 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1970 " aaaaaaaaaaaaaaaaaaaaa);", 1971 getLLVMStyleWithColumns(74)); 1972 } 1973 1974 TEST_F(FormatTest, UnderstandsTemplateParameters) { 1975 verifyFormat("A<int> a;"); 1976 verifyFormat("A<A<A<int> > > a;"); 1977 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 1978 verifyFormat("bool x = a < 1 || 2 > a;"); 1979 verifyFormat("bool x = 5 < f<int>();"); 1980 verifyFormat("bool x = f<int>() > 5;"); 1981 verifyFormat("bool x = 5 < a<int>::x;"); 1982 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 1983 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 1984 1985 verifyGoogleFormat("A<A<int>> a;"); 1986 verifyGoogleFormat("A<A<A<int>>> a;"); 1987 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 1988 verifyGoogleFormat("A<A<int> > a;"); 1989 verifyGoogleFormat("A<A<A<int> > > a;"); 1990 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 1991 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 1992 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 1993 1994 verifyFormat("test >> a >> b;"); 1995 verifyFormat("test << a >> b;"); 1996 1997 verifyFormat("f<int>();"); 1998 verifyFormat("template <typename T> void f() {}"); 1999 } 2000 2001 TEST_F(FormatTest, UnderstandsBinaryOperators) { 2002 verifyFormat("COMPARE(a, ==, b);"); 2003 } 2004 2005 TEST_F(FormatTest, UnderstandsPointersToMembers) { 2006 verifyFormat("int A::*x;"); 2007 // FIXME: Recognize pointers to member functions. 2008 //verifyFormat("int (S::*func)(void *);"); 2009 verifyFormat("int(S::*func)(void *);"); 2010 verifyFormat("(a->*f)();"); 2011 verifyFormat("a->*x;"); 2012 verifyFormat("(a.*f)();"); 2013 verifyFormat("((*a).*f)();"); 2014 verifyFormat("a.*x;"); 2015 } 2016 2017 TEST_F(FormatTest, UnderstandsUnaryOperators) { 2018 verifyFormat("int a = -2;"); 2019 verifyFormat("f(-1, -2, -3);"); 2020 verifyFormat("a[-1] = 5;"); 2021 verifyFormat("int a = 5 + -2;"); 2022 verifyFormat("if (i == -1) {\n}"); 2023 verifyFormat("if (i != -1) {\n}"); 2024 verifyFormat("if (i > -1) {\n}"); 2025 verifyFormat("if (i < -1) {\n}"); 2026 verifyFormat("++(a->f());"); 2027 verifyFormat("--(a->f());"); 2028 verifyFormat("(a->f())++;"); 2029 verifyFormat("a[42]++;"); 2030 verifyFormat("if (!(a->f())) {\n}"); 2031 2032 verifyFormat("a-- > b;"); 2033 verifyFormat("b ? -a : c;"); 2034 verifyFormat("n * sizeof char16;"); 2035 verifyFormat("n * alignof char16;"); 2036 verifyFormat("sizeof(char);"); 2037 verifyFormat("alignof(char);"); 2038 2039 verifyFormat("return -1;"); 2040 verifyFormat("switch (a) {\n" 2041 "case -1:\n" 2042 " break;\n" 2043 "}"); 2044 2045 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 2046 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 2047 2048 verifyFormat("int a = /* confusing comment */ -1;"); 2049 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 2050 verifyFormat("int a = i /* confusing comment */++;"); 2051 } 2052 2053 TEST_F(FormatTest, UndestandsOverloadedOperators) { 2054 verifyFormat("bool operator<();"); 2055 verifyFormat("bool operator>();"); 2056 verifyFormat("bool operator=();"); 2057 verifyFormat("bool operator==();"); 2058 verifyFormat("bool operator!=();"); 2059 verifyFormat("int operator+();"); 2060 verifyFormat("int operator++();"); 2061 verifyFormat("bool operator();"); 2062 verifyFormat("bool operator()();"); 2063 verifyFormat("bool operator[]();"); 2064 verifyFormat("operator bool();"); 2065 verifyFormat("operator int();"); 2066 verifyFormat("operator void *();"); 2067 verifyFormat("operator SomeType<int>();"); 2068 verifyFormat("operator SomeType<int, int>();"); 2069 verifyFormat("operator SomeType<SomeType<int> >();"); 2070 verifyFormat("void *operator new(std::size_t size);"); 2071 verifyFormat("void *operator new[](std::size_t size);"); 2072 verifyFormat("void operator delete(void *ptr);"); 2073 verifyFormat("void operator delete[](void *ptr);"); 2074 2075 verifyFormat( 2076 "ostream &operator<<(ostream &OutputStream,\n" 2077 " SomeReallyLongType WithSomeReallyLongValue);"); 2078 2079 verifyGoogleFormat("operator void*();"); 2080 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 2081 } 2082 2083 TEST_F(FormatTest, UnderstandsNewAndDelete) { 2084 verifyFormat("void f() {\n" 2085 " A *a = new A;\n" 2086 " A *a = new (placement) A;\n" 2087 " delete a;\n" 2088 " delete (A *)a;\n" 2089 "}"); 2090 } 2091 2092 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 2093 verifyFormat("int *f(int *a) {}"); 2094 verifyFormat("int main(int argc, char **argv) {}"); 2095 verifyFormat("Test::Test(int b) : a(b * b) {}"); 2096 verifyIndependentOfContext("f(a, *a);"); 2097 verifyFormat("void g() { f(*a); }"); 2098 verifyIndependentOfContext("int a = b * 10;"); 2099 verifyIndependentOfContext("int a = 10 * b;"); 2100 verifyIndependentOfContext("int a = b * c;"); 2101 verifyIndependentOfContext("int a += b * c;"); 2102 verifyIndependentOfContext("int a -= b * c;"); 2103 verifyIndependentOfContext("int a *= b * c;"); 2104 verifyIndependentOfContext("int a /= b * c;"); 2105 verifyIndependentOfContext("int a = *b;"); 2106 verifyIndependentOfContext("int a = *b * c;"); 2107 verifyIndependentOfContext("int a = b * *c;"); 2108 verifyIndependentOfContext("return 10 * b;"); 2109 verifyIndependentOfContext("return *b * *c;"); 2110 verifyIndependentOfContext("return a & ~b;"); 2111 verifyIndependentOfContext("f(b ? *c : *d);"); 2112 verifyIndependentOfContext("int a = b ? *c : *d;"); 2113 verifyIndependentOfContext("*b = a;"); 2114 verifyIndependentOfContext("a * ~b;"); 2115 verifyIndependentOfContext("a * !b;"); 2116 verifyIndependentOfContext("a * +b;"); 2117 verifyIndependentOfContext("a * -b;"); 2118 verifyIndependentOfContext("a * ++b;"); 2119 verifyIndependentOfContext("a * --b;"); 2120 verifyIndependentOfContext("a[4] * b;"); 2121 verifyIndependentOfContext("a[a * a] = 1;"); 2122 verifyIndependentOfContext("f() * b;"); 2123 verifyIndependentOfContext("a * [self dostuff];"); 2124 verifyIndependentOfContext("a * (a + b);"); 2125 verifyIndependentOfContext("(a *)(a + b);"); 2126 verifyIndependentOfContext("int *pa = (int *)&a;"); 2127 verifyIndependentOfContext("return sizeof(int **);"); 2128 verifyIndependentOfContext("return sizeof(int ******);"); 2129 verifyIndependentOfContext("return (int **&)a;"); 2130 verifyFormat("void f(Type (*parameter)[10]) {}"); 2131 verifyGoogleFormat("return sizeof(int**);"); 2132 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 2133 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 2134 // FIXME: The newline is wrong. 2135 verifyFormat("auto a = [](int **&, int ***) {}\n;"); 2136 2137 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 2138 2139 verifyIndependentOfContext("A<int *> a;"); 2140 verifyIndependentOfContext("A<int **> a;"); 2141 verifyIndependentOfContext("A<int *, int *> a;"); 2142 verifyIndependentOfContext( 2143 "const char *const p = reinterpret_cast<const char *const>(q);"); 2144 verifyIndependentOfContext("A<int **, int **> a;"); 2145 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 2146 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 2147 2148 verifyFormat( 2149 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 2151 2152 verifyGoogleFormat("int main(int argc, char** argv) {}"); 2153 verifyGoogleFormat("A<int*> a;"); 2154 verifyGoogleFormat("A<int**> a;"); 2155 verifyGoogleFormat("A<int*, int*> a;"); 2156 verifyGoogleFormat("A<int**, int**> a;"); 2157 verifyGoogleFormat("f(b ? *c : *d);"); 2158 verifyGoogleFormat("int a = b ? *c : *d;"); 2159 verifyGoogleFormat("Type* t = **x;"); 2160 verifyGoogleFormat("Type* t = *++*x;"); 2161 verifyGoogleFormat("*++*x;"); 2162 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 2163 verifyGoogleFormat("Type* t = x++ * y;"); 2164 verifyGoogleFormat( 2165 "const char* const p = reinterpret_cast<const char* const>(q);"); 2166 2167 verifyIndependentOfContext("a = *(x + y);"); 2168 verifyIndependentOfContext("a = &(x + y);"); 2169 verifyIndependentOfContext("*(x + y).call();"); 2170 verifyIndependentOfContext("&(x + y)->call();"); 2171 verifyFormat("void f() { &(*I).first; }"); 2172 2173 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 2174 verifyFormat( 2175 "int *MyValues = {\n" 2176 " *A, // Operator detection might be confused by the '{'\n" 2177 " *BB // Operator detection might be confused by previous comment\n" 2178 "};"); 2179 2180 verifyIndependentOfContext("if (int *a = &b)"); 2181 verifyIndependentOfContext("if (int &a = *b)"); 2182 verifyIndependentOfContext("if (a & b[i])"); 2183 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 2184 verifyIndependentOfContext("if (*b[i])"); 2185 verifyIndependentOfContext("if (int *a = (&b))"); 2186 verifyIndependentOfContext("while (int *a = &b)"); 2187 verifyFormat("void f() {\n" 2188 " for (const int &v : Values) {\n" 2189 " }\n" 2190 "}"); 2191 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 2192 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 2193 2194 verifyIndependentOfContext("A = new SomeType *[Length];"); 2195 verifyIndependentOfContext("A = new SomeType *[Length]();"); 2196 verifyGoogleFormat("A = new SomeType* [Length]();"); 2197 verifyGoogleFormat("A = new SomeType* [Length];"); 2198 } 2199 2200 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 2201 EXPECT_EQ("int *a;\n" 2202 "int *a;\n" 2203 "int *a;", 2204 format("int *a;\n" 2205 "int* a;\n" 2206 "int *a;", 2207 getGoogleStyle())); 2208 EXPECT_EQ("int* a;\n" 2209 "int* a;\n" 2210 "int* a;", 2211 format("int* a;\n" 2212 "int* a;\n" 2213 "int *a;", 2214 getGoogleStyle())); 2215 EXPECT_EQ("int *a;\n" 2216 "int *a;\n" 2217 "int *a;", 2218 format("int *a;\n" 2219 "int * a;\n" 2220 "int * a;", 2221 getGoogleStyle())); 2222 } 2223 2224 TEST_F(FormatTest, UnderstandsRvalueReferences) { 2225 verifyFormat("int f(int &&a) {}"); 2226 verifyFormat("int f(int a, char &&b) {}"); 2227 verifyFormat("void f() { int &&a = b; }"); 2228 verifyGoogleFormat("int f(int a, char&& b) {}"); 2229 verifyGoogleFormat("void f() { int&& a = b; }"); 2230 2231 // FIXME: These require somewhat deeper changes in template arguments 2232 // formatting. 2233 // verifyIndependentOfContext("A<int &&> a;"); 2234 // verifyIndependentOfContext("A<int &&, int &&> a;"); 2235 // verifyGoogleFormat("A<int&&> a;"); 2236 // verifyGoogleFormat("A<int&&, int&&> a;"); 2237 } 2238 2239 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 2240 verifyFormat("void f() {\n" 2241 " x[aaaaaaaaa -\n" 2242 " b] = 23;\n" 2243 "}", 2244 getLLVMStyleWithColumns(15)); 2245 } 2246 2247 TEST_F(FormatTest, FormatsCasts) { 2248 verifyFormat("Type *A = static_cast<Type *>(P);"); 2249 verifyFormat("Type *A = (Type *)P;"); 2250 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 2251 verifyFormat("int a = (int)(2.0f);"); 2252 2253 // FIXME: These also need to be identified. 2254 verifyFormat("int a = (int) 2.0f;"); 2255 verifyFormat("int a = (int) * b;"); 2256 2257 // These are not casts. 2258 verifyFormat("void f(int *) {}"); 2259 verifyFormat("f(foo)->b;"); 2260 verifyFormat("f(foo).b;"); 2261 verifyFormat("f(foo)(b);"); 2262 verifyFormat("f(foo)[b];"); 2263 verifyFormat("[](foo) { return 4; }(bar)];"); 2264 verifyFormat("(*funptr)(foo)[4];"); 2265 verifyFormat("funptrs[4](foo)[4];"); 2266 verifyFormat("void f(int *);"); 2267 verifyFormat("void f(int *) = 0;"); 2268 verifyFormat("void f(SmallVector<int>) {}"); 2269 verifyFormat("void f(SmallVector<int>);"); 2270 verifyFormat("void f(SmallVector<int>) = 0;"); 2271 verifyFormat("void f(int i = (kValue) * kMask) {}"); 2272 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 2273 verifyFormat("int a = sizeof(int) * b;"); 2274 verifyFormat("int a = alignof(int) * b;"); 2275 2276 // These are not casts, but at some point were confused with casts. 2277 verifyFormat("virtual void foo(int *) override;"); 2278 verifyFormat("virtual void foo(char &) const;"); 2279 verifyFormat("virtual void foo(int *a, char *) const;"); 2280 verifyFormat("int a = sizeof(int *) + b;"); 2281 verifyFormat("int a = alignof(int *) + b;"); 2282 } 2283 2284 TEST_F(FormatTest, FormatsFunctionTypes) { 2285 verifyFormat("A<bool()> a;"); 2286 verifyFormat("A<SomeType()> a;"); 2287 verifyFormat("A<void(*)(int, std::string)> a;"); 2288 verifyFormat("A<void *(int)>;"); 2289 2290 // FIXME: Inconsistent. 2291 verifyFormat("int (*func)(void *);"); 2292 verifyFormat("void f() { int(*func)(void *); }"); 2293 2294 verifyGoogleFormat("A<void*(int)>;"); 2295 } 2296 2297 TEST_F(FormatTest, BreaksLongDeclarations) { 2298 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 2299 " int LoooooooooooooooooooongParam2) {}"); 2300 verifyFormat( 2301 "TypeSpecDecl *\n" 2302 "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" 2303 " IdentifierIn *II, Type *T) {}"); 2304 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 2305 "ReallyReallyLongFunctionName(\n" 2306 " const std::string &SomeParameter,\n" 2307 " const SomeType<string, SomeOtherTemplateParameter> &\n" 2308 " ReallyReallyLongParameterName,\n" 2309 " const SomeType<string, SomeOtherTemplateParameter> &\n" 2310 " AnotherLongParameterName) {}"); 2311 verifyFormat( 2312 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 2313 "aaaaaaaaaaaaaaaaaaaaaaa;"); 2314 2315 verifyGoogleFormat( 2316 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 2317 " SourceLocation L) {}"); 2318 verifyGoogleFormat( 2319 "some_namespace::LongReturnType\n" 2320 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 2321 " int first_long_parameter, int second_parameter) {}"); 2322 2323 verifyGoogleFormat("template <typename T>\n" 2324 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 2325 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 2326 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2327 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 2328 } 2329 2330 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 2331 verifyFormat("(a)->b();"); 2332 verifyFormat("--a;"); 2333 } 2334 2335 TEST_F(FormatTest, HandlesIncludeDirectives) { 2336 verifyFormat("#include <string>\n" 2337 "#include <a/b/c.h>\n" 2338 "#include \"a/b/string\"\n" 2339 "#include \"string.h\"\n" 2340 "#include \"string.h\"\n" 2341 "#include <a-a>\n" 2342 "#include < path with space >\n" 2343 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 2344 getLLVMStyleWithColumns(35)); 2345 2346 verifyFormat("#import <string>"); 2347 verifyFormat("#import <a/b/c.h>"); 2348 verifyFormat("#import \"a/b/string\""); 2349 verifyFormat("#import \"string.h\""); 2350 verifyFormat("#import \"string.h\""); 2351 } 2352 2353 //===----------------------------------------------------------------------===// 2354 // Error recovery tests. 2355 //===----------------------------------------------------------------------===// 2356 2357 TEST_F(FormatTest, IncompleteParameterLists) { 2358 FormatStyle NoBinPacking = getLLVMStyle(); 2359 NoBinPacking.BinPackParameters = false; 2360 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 2361 " double *min_x,\n" 2362 " double *max_x,\n" 2363 " double *min_y,\n" 2364 " double *max_y,\n" 2365 " double *min_z,\n" 2366 " double *max_z, ) {}", 2367 NoBinPacking); 2368 } 2369 2370 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 2371 verifyFormat("void f() { return; }\n42"); 2372 verifyFormat("void f() {\n" 2373 " if (0)\n" 2374 " return;\n" 2375 "}\n" 2376 "42"); 2377 verifyFormat("void f() { return }\n42"); 2378 verifyFormat("void f() {\n" 2379 " if (0)\n" 2380 " return\n" 2381 "}\n" 2382 "42"); 2383 } 2384 2385 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 2386 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 2387 EXPECT_EQ("void f() {\n" 2388 " if (a)\n" 2389 " return\n" 2390 "}", 2391 format("void f ( ) { if ( a ) return }")); 2392 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); 2393 EXPECT_EQ("namespace N {\n" 2394 "void f() {}\n" 2395 "void g()\n" 2396 "}", 2397 format("namespace N { void f( ) { } void g( ) }")); 2398 } 2399 2400 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 2401 verifyFormat("int aaaaaaaa =\n" 2402 " // Overly long comment\n" 2403 " b;", 2404 getLLVMStyleWithColumns(20)); 2405 verifyFormat("function(\n" 2406 " ShortArgument,\n" 2407 " LoooooooooooongArgument);\n", 2408 getLLVMStyleWithColumns(20)); 2409 } 2410 2411 TEST_F(FormatTest, IncorrectAccessSpecifier) { 2412 verifyFormat("public:"); 2413 verifyFormat("class A {\n" 2414 "public\n" 2415 " void f() {}\n" 2416 "};"); 2417 verifyFormat("public\n" 2418 "int qwerty;"); 2419 verifyFormat("public\n" 2420 "B {}"); 2421 verifyFormat("public\n" 2422 "{}"); 2423 verifyFormat("public\n" 2424 "B { int x; }"); 2425 } 2426 2427 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 2428 verifyFormat("{"); 2429 verifyFormat("#})"); 2430 } 2431 2432 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 2433 verifyFormat("do {\n}"); 2434 verifyFormat("do {\n}\n" 2435 "f();"); 2436 verifyFormat("do {\n}\n" 2437 "wheeee(fun);"); 2438 verifyFormat("do {\n" 2439 " f();\n" 2440 "}"); 2441 } 2442 2443 TEST_F(FormatTest, IncorrectCodeMissingParens) { 2444 verifyFormat("if {\n foo;\n foo();\n}"); 2445 verifyFormat("switch {\n foo;\n foo();\n}"); 2446 verifyFormat("for {\n foo;\n foo();\n}"); 2447 verifyFormat("while {\n foo;\n foo();\n}"); 2448 verifyFormat("do {\n foo;\n foo();\n} while;"); 2449 } 2450 2451 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 2452 verifyFormat("namespace {\n" 2453 "class Foo { Foo ( }; } // comment"); 2454 } 2455 2456 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 2457 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 2458 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 2459 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 2460 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 2461 2462 EXPECT_EQ("{\n" 2463 " {\n" 2464 " breakme(\n" 2465 " qwe);\n" 2466 "}\n", 2467 format("{\n" 2468 " {\n" 2469 " breakme(qwe);\n" 2470 "}\n", 2471 getLLVMStyleWithColumns(10))); 2472 } 2473 2474 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 2475 verifyFormat("int x = {\n" 2476 " avariable,\n" 2477 " b(alongervariable)\n" 2478 "};", 2479 getLLVMStyleWithColumns(25)); 2480 } 2481 2482 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 2483 verifyFormat("return (a)(b) { 1, 2, 3 };"); 2484 } 2485 2486 TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 2487 // FIXME: This is bad, find a better and more generic solution. 2488 verifyFormat( 2489 "Aaa({\n" 2490 " int i;\n" 2491 "},\n" 2492 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2493 " ccccccccccccccccc));"); 2494 } 2495 2496 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 2497 verifyFormat("void f() { return 42; }"); 2498 verifyFormat("void f() {\n" 2499 " // Comment\n" 2500 "}"); 2501 verifyFormat("{\n" 2502 "#error {\n" 2503 " int a;\n" 2504 "}"); 2505 verifyFormat("{\n" 2506 " int a;\n" 2507 "#error {\n" 2508 "}"); 2509 2510 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 2511 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 2512 2513 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 2514 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 2515 } 2516 2517 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 2518 // Elaborate type variable declarations. 2519 verifyFormat("struct foo a = { bar };\nint n;"); 2520 verifyFormat("class foo a = { bar };\nint n;"); 2521 verifyFormat("union foo a = { bar };\nint n;"); 2522 2523 // Elaborate types inside function definitions. 2524 verifyFormat("struct foo f() {}\nint n;"); 2525 verifyFormat("class foo f() {}\nint n;"); 2526 verifyFormat("union foo f() {}\nint n;"); 2527 2528 // Templates. 2529 verifyFormat("template <class X> void f() {}\nint n;"); 2530 verifyFormat("template <struct X> void f() {}\nint n;"); 2531 verifyFormat("template <union X> void f() {}\nint n;"); 2532 2533 // Actual definitions... 2534 verifyFormat("struct {\n} n;"); 2535 verifyFormat( 2536 "template <template <class T, class Y>, class Z> class X {\n} n;"); 2537 verifyFormat("union Z {\n int n;\n} x;"); 2538 verifyFormat("class MACRO Z {\n} n;"); 2539 verifyFormat("class MACRO(X) Z {\n} n;"); 2540 verifyFormat("class __attribute__(X) Z {\n} n;"); 2541 verifyFormat("class __declspec(X) Z {\n} n;"); 2542 verifyFormat("class A##B##C {\n} n;"); 2543 2544 // Redefinition from nested context: 2545 verifyFormat("class A::B::C {\n} n;"); 2546 2547 // Template definitions. 2548 // FIXME: This is still incorrectly handled at the formatter side. 2549 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); 2550 2551 // FIXME: 2552 // This now gets parsed incorrectly as class definition. 2553 // verifyFormat("class A<int> f() {\n}\nint n;"); 2554 2555 // Elaborate types where incorrectly parsing the structural element would 2556 // break the indent. 2557 verifyFormat("if (true)\n" 2558 " class X x;\n" 2559 "else\n" 2560 " f();\n"); 2561 2562 // This is simply incomplete. Formatting is not important, but must not crash. 2563 verifyFormat("class A:"); 2564 } 2565 2566 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 2567 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 2568 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 2569 EXPECT_EQ("#error 1", format(" # error 1")); 2570 EXPECT_EQ("#warning 1", format(" # warning 1")); 2571 } 2572 2573 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 2574 FormatStyle AllowsMergedIf = getGoogleStyle(); 2575 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 2576 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 2577 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 2578 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 2579 EXPECT_EQ("if (true) return 42;", 2580 format("if (true)\nreturn 42;", AllowsMergedIf)); 2581 FormatStyle ShortMergedIf = AllowsMergedIf; 2582 ShortMergedIf.ColumnLimit = 25; 2583 verifyFormat("#define A \\\n" 2584 " if (true) return 42;", 2585 ShortMergedIf); 2586 verifyFormat("#define A \\\n" 2587 " f(); \\\n" 2588 " if (true)\n" 2589 "#define B", 2590 ShortMergedIf); 2591 verifyFormat("#define A \\\n" 2592 " f(); \\\n" 2593 " if (true)\n" 2594 "g();", 2595 ShortMergedIf); 2596 verifyFormat("{\n" 2597 "#ifdef A\n" 2598 " // Comment\n" 2599 " if (true) continue;\n" 2600 "#endif\n" 2601 " // Comment\n" 2602 " if (true) continue;", 2603 ShortMergedIf); 2604 } 2605 2606 TEST_F(FormatTest, BlockCommentsInControlLoops) { 2607 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2608 " f();\n" 2609 "}"); 2610 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2611 " f();\n" 2612 "} /* another comment */ else /* comment #3 */ {\n" 2613 " g();\n" 2614 "}"); 2615 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2616 " f();\n" 2617 "}"); 2618 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2619 " f();\n" 2620 "}"); 2621 verifyFormat("do /* a comment in a strange place */ {\n" 2622 " f();\n" 2623 "} /* another comment */ while (0);"); 2624 } 2625 2626 TEST_F(FormatTest, BlockComments) { 2627 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2628 format("/* *//* */ /* */\n/* *//* */ /* */")); 2629 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 2630 EXPECT_EQ("#define A /* */\\\n" 2631 " b\n" 2632 "/* */\n" 2633 "someCall(\n" 2634 " parameter);", 2635 format("#define A /* */ b\n" 2636 "/* */\n" 2637 "someCall(parameter);", 2638 getLLVMStyleWithColumns(15))); 2639 2640 EXPECT_EQ("#define A\n" 2641 "/* */ someCall(\n" 2642 " parameter);", 2643 format("#define A\n" 2644 "/* */someCall(parameter);", 2645 getLLVMStyleWithColumns(15))); 2646 2647 FormatStyle NoBinPacking = getLLVMStyle(); 2648 NoBinPacking.BinPackParameters = false; 2649 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2650 " 2, /* comment 2 */\n" 2651 " 3, /* comment 3 */\n" 2652 " aaaa,\n" 2653 " bbbb);", 2654 format("someFunction (1, /* comment 1 */\n" 2655 " 2, /* comment 2 */ \n" 2656 " 3, /* comment 3 */\n" 2657 "aaaa, bbbb );", 2658 NoBinPacking)); 2659 verifyFormat( 2660 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2662 EXPECT_EQ( 2663 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2664 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2666 format( 2667 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2668 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2669 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2670 EXPECT_EQ( 2671 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2672 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2673 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2674 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2675 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2676 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2677 } 2678 2679 TEST_F(FormatTest, BlockCommentsInMacros) { 2680 EXPECT_EQ("#define A \\\n" 2681 " { \\\n" 2682 " /* one line */ \\\n" 2683 " someCall();", 2684 format("#define A { \\\n" 2685 " /* one line */ \\\n" 2686 " someCall();", 2687 getLLVMStyleWithColumns(20))); 2688 EXPECT_EQ("#define A \\\n" 2689 " { \\\n" 2690 " /* previous */ \\\n" 2691 " /* one line */ \\\n" 2692 " someCall();", 2693 format("#define A { \\\n" 2694 " /* previous */ \\\n" 2695 " /* one line */ \\\n" 2696 " someCall();", 2697 getLLVMStyleWithColumns(20))); 2698 } 2699 2700 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2701 // FIXME: This is not what we want... 2702 verifyFormat("{\n" 2703 "// a" 2704 "// b"); 2705 } 2706 2707 TEST_F(FormatTest, FormatStarDependingOnContext) { 2708 verifyFormat("void f(int *a);"); 2709 verifyFormat("void f() { f(fint * b); }"); 2710 verifyFormat("class A {\n void f(int *a);\n};"); 2711 verifyFormat("class A {\n int *a;\n};"); 2712 verifyFormat("namespace a {\n" 2713 "namespace b {\n" 2714 "class A {\n" 2715 " void f() {}\n" 2716 " int *a;\n" 2717 "};\n" 2718 "}\n" 2719 "}"); 2720 } 2721 2722 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 2723 verifyFormat("while"); 2724 verifyFormat("operator"); 2725 } 2726 2727 //===----------------------------------------------------------------------===// 2728 // Objective-C tests. 2729 //===----------------------------------------------------------------------===// 2730 2731 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 2732 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 2733 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 2734 format("-(NSUInteger)indexOfObject:(id)anObject;")); 2735 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 2736 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 2737 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 2738 format("-(NSInteger)Method3:(id)anObject;")); 2739 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 2740 format("-(NSInteger)Method4:(id)anObject;")); 2741 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 2742 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 2743 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 2744 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 2745 EXPECT_EQ( 2746 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 2747 format( 2748 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 2749 2750 // Very long objectiveC method declaration. 2751 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 2752 " inRange:(NSRange)range\n" 2753 " outRange:(NSRange)out_range\n" 2754 " outRange1:(NSRange)out_range1\n" 2755 " outRange2:(NSRange)out_range2\n" 2756 " outRange3:(NSRange)out_range3\n" 2757 " outRange4:(NSRange)out_range4\n" 2758 " outRange5:(NSRange)out_range5\n" 2759 " outRange6:(NSRange)out_range6\n" 2760 " outRange7:(NSRange)out_range7\n" 2761 " outRange8:(NSRange)out_range8\n" 2762 " outRange9:(NSRange)out_range9;"); 2763 2764 verifyFormat("- (int)sum:(vector<int>)numbers;"); 2765 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 2766 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 2767 // protocol lists (but not for template classes): 2768 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 2769 2770 verifyFormat("- (int(*)())foo:(int(*)())f;"); 2771 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); 2772 2773 // If there's no return type (very rare in practice!), LLVM and Google style 2774 // agree. 2775 verifyFormat("- foo:(int)f;"); 2776 verifyGoogleFormat("- foo:(int)foo;"); 2777 } 2778 2779 TEST_F(FormatTest, FormatObjCBlocks) { 2780 verifyFormat("int (^Block)(int, int);"); 2781 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 2782 } 2783 2784 TEST_F(FormatTest, FormatObjCInterface) { 2785 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 2786 "@public\n" 2787 " int field1;\n" 2788 "@protected\n" 2789 " int field2;\n" 2790 "@private\n" 2791 " int field3;\n" 2792 "@package\n" 2793 " int field4;\n" 2794 "}\n" 2795 "+ (id)init;\n" 2796 "@end"); 2797 2798 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 2799 " @public\n" 2800 " int field1;\n" 2801 " @protected\n" 2802 " int field2;\n" 2803 " @private\n" 2804 " int field3;\n" 2805 " @package\n" 2806 " int field4;\n" 2807 "}\n" 2808 "+ (id)init;\n" 2809 "@end"); 2810 2811 verifyFormat("@interface /* wait for it */ Foo\n" 2812 "+ (id)init;\n" 2813 "// Look, a comment!\n" 2814 "- (int)answerWith:(int)i;\n" 2815 "@end"); 2816 2817 verifyFormat("@interface Foo\n" 2818 "@end\n" 2819 "@interface Bar\n" 2820 "@end"); 2821 2822 verifyFormat("@interface Foo : Bar\n" 2823 "+ (id)init;\n" 2824 "@end"); 2825 2826 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 2827 "+ (id)init;\n" 2828 "@end"); 2829 2830 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 2831 "+ (id)init;\n" 2832 "@end"); 2833 2834 verifyFormat("@interface Foo (HackStuff)\n" 2835 "+ (id)init;\n" 2836 "@end"); 2837 2838 verifyFormat("@interface Foo ()\n" 2839 "+ (id)init;\n" 2840 "@end"); 2841 2842 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 2843 "+ (id)init;\n" 2844 "@end"); 2845 2846 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 2847 "+ (id)init;\n" 2848 "@end"); 2849 2850 verifyFormat("@interface Foo {\n" 2851 " int _i;\n" 2852 "}\n" 2853 "+ (id)init;\n" 2854 "@end"); 2855 2856 verifyFormat("@interface Foo : Bar {\n" 2857 " int _i;\n" 2858 "}\n" 2859 "+ (id)init;\n" 2860 "@end"); 2861 2862 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 2863 " int _i;\n" 2864 "}\n" 2865 "+ (id)init;\n" 2866 "@end"); 2867 2868 verifyFormat("@interface Foo (HackStuff) {\n" 2869 " int _i;\n" 2870 "}\n" 2871 "+ (id)init;\n" 2872 "@end"); 2873 2874 verifyFormat("@interface Foo () {\n" 2875 " int _i;\n" 2876 "}\n" 2877 "+ (id)init;\n" 2878 "@end"); 2879 2880 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 2881 " int _i;\n" 2882 "}\n" 2883 "+ (id)init;\n" 2884 "@end"); 2885 } 2886 2887 TEST_F(FormatTest, FormatObjCImplementation) { 2888 verifyFormat("@implementation Foo : NSObject {\n" 2889 "@public\n" 2890 " int field1;\n" 2891 "@protected\n" 2892 " int field2;\n" 2893 "@private\n" 2894 " int field3;\n" 2895 "@package\n" 2896 " int field4;\n" 2897 "}\n" 2898 "+ (id)init {\n}\n" 2899 "@end"); 2900 2901 verifyGoogleFormat("@implementation Foo : NSObject {\n" 2902 " @public\n" 2903 " int field1;\n" 2904 " @protected\n" 2905 " int field2;\n" 2906 " @private\n" 2907 " int field3;\n" 2908 " @package\n" 2909 " int field4;\n" 2910 "}\n" 2911 "+ (id)init {\n}\n" 2912 "@end"); 2913 2914 verifyFormat("@implementation Foo\n" 2915 "+ (id)init {\n" 2916 " if (true)\n" 2917 " return nil;\n" 2918 "}\n" 2919 "// Look, a comment!\n" 2920 "- (int)answerWith:(int)i {\n" 2921 " return i;\n" 2922 "}\n" 2923 "+ (int)answerWith:(int)i {\n" 2924 " return i;\n" 2925 "}\n" 2926 "@end"); 2927 2928 verifyFormat("@implementation Foo\n" 2929 "@end\n" 2930 "@implementation Bar\n" 2931 "@end"); 2932 2933 verifyFormat("@implementation Foo : Bar\n" 2934 "+ (id)init {\n}\n" 2935 "- (void)foo {\n}\n" 2936 "@end"); 2937 2938 verifyFormat("@implementation Foo {\n" 2939 " int _i;\n" 2940 "}\n" 2941 "+ (id)init {\n}\n" 2942 "@end"); 2943 2944 verifyFormat("@implementation Foo : Bar {\n" 2945 " int _i;\n" 2946 "}\n" 2947 "+ (id)init {\n}\n" 2948 "@end"); 2949 2950 verifyFormat("@implementation Foo (HackStuff)\n" 2951 "+ (id)init {\n}\n" 2952 "@end"); 2953 } 2954 2955 TEST_F(FormatTest, FormatObjCProtocol) { 2956 verifyFormat("@protocol Foo\n" 2957 "@property(weak) id delegate;\n" 2958 "- (NSUInteger)numberOfThings;\n" 2959 "@end"); 2960 2961 verifyFormat("@protocol MyProtocol <NSObject>\n" 2962 "- (NSUInteger)numberOfThings;\n" 2963 "@end"); 2964 2965 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 2966 "- (NSUInteger)numberOfThings;\n" 2967 "@end"); 2968 2969 verifyFormat("@protocol Foo;\n" 2970 "@protocol Bar;\n"); 2971 2972 verifyFormat("@protocol Foo\n" 2973 "@end\n" 2974 "@protocol Bar\n" 2975 "@end"); 2976 2977 verifyFormat("@protocol myProtocol\n" 2978 "- (void)mandatoryWithInt:(int)i;\n" 2979 "@optional\n" 2980 "- (void)optional;\n" 2981 "@required\n" 2982 "- (void)required;\n" 2983 "@optional\n" 2984 "@property(assign) int madProp;\n" 2985 "@end\n"); 2986 } 2987 2988 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 2989 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 2990 " rect:(NSRect)theRect\n" 2991 " interval:(float)theInterval {\n" 2992 "}"); 2993 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 2994 " longKeyword:(NSRect)theRect\n" 2995 " evenLongerKeyword:(float)theInterval\n" 2996 " error:(NSError **)theError {\n" 2997 "}"); 2998 } 2999 3000 TEST_F(FormatTest, FormatObjCMethodExpr) { 3001 verifyFormat("[foo bar:baz];"); 3002 verifyFormat("return [foo bar:baz];"); 3003 verifyFormat("f([foo bar:baz]);"); 3004 verifyFormat("f(2, [foo bar:baz]);"); 3005 verifyFormat("f(2, a ? b : c);"); 3006 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 3007 3008 // Unary operators. 3009 verifyFormat("int a = +[foo bar:baz];"); 3010 verifyFormat("int a = -[foo bar:baz];"); 3011 verifyFormat("int a = ![foo bar:baz];"); 3012 verifyFormat("int a = ~[foo bar:baz];"); 3013 verifyFormat("int a = ++[foo bar:baz];"); 3014 verifyFormat("int a = --[foo bar:baz];"); 3015 verifyFormat("int a = sizeof [foo bar:baz];"); 3016 verifyFormat("int a = alignof [foo bar:baz];"); 3017 verifyFormat("int a = &[foo bar:baz];"); 3018 verifyFormat("int a = *[foo bar:baz];"); 3019 // FIXME: Make casts work, without breaking f()[4]. 3020 //verifyFormat("int a = (int)[foo bar:baz];"); 3021 //verifyFormat("return (int)[foo bar:baz];"); 3022 //verifyFormat("(void)[foo bar:baz];"); 3023 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 3024 3025 // Binary operators. 3026 verifyFormat("[foo bar:baz], [foo bar:baz];"); 3027 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 3028 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 3029 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 3030 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 3031 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 3032 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 3033 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 3034 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 3035 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 3036 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 3037 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 3038 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 3039 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 3040 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 3041 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 3042 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 3043 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 3044 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 3045 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 3046 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 3047 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 3048 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 3049 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 3050 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 3051 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 3052 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 3053 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 3054 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 3055 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 3056 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 3057 // Whew! 3058 3059 verifyFormat("return in[42];"); 3060 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 3061 "}"); 3062 3063 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 3064 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 3065 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 3066 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 3067 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 3068 verifyFormat("[button setAction:@selector(zoomOut:)];"); 3069 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 3070 3071 verifyFormat("arr[[self indexForFoo:a]];"); 3072 verifyFormat("throw [self errorFor:a];"); 3073 verifyFormat("@throw [self errorFor:a];"); 3074 3075 // This tests that the formatter doesn't break after "backing" but before ":", 3076 // which would be at 80 columns. 3077 verifyFormat( 3078 "void f() {\n" 3079 " if ((self = [super initWithContentRect:contentRect\n" 3080 " styleMask:styleMask\n" 3081 " backing:NSBackingStoreBuffered\n" 3082 " defer:YES]))"); 3083 3084 verifyFormat( 3085 "[foo checkThatBreakingAfterColonWorksOk:\n" 3086 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 3087 3088 verifyFormat("[myObj short:arg1 // Force line break\n" 3089 " longKeyword:arg2\n" 3090 " evenLongerKeyword:arg3\n" 3091 " error:arg4];"); 3092 verifyFormat( 3093 "void f() {\n" 3094 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 3095 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 3096 " pos.width(), pos.height())\n" 3097 " styleMask:NSBorderlessWindowMask\n" 3098 " backing:NSBackingStoreBuffered\n" 3099 " defer:NO]);\n" 3100 "}"); 3101 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 3102 " with:contentsNativeView];"); 3103 3104 verifyFormat( 3105 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 3106 " owner:nillllll];"); 3107 3108 verifyFormat( 3109 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 3110 " forType:kBookmarkButtonDragType];"); 3111 3112 verifyFormat("[defaultCenter addObserver:self\n" 3113 " selector:@selector(willEnterFullscreen)\n" 3114 " name:kWillEnterFullscreenNotification\n" 3115 " object:nil];"); 3116 verifyFormat("[image_rep drawInRect:drawRect\n" 3117 " fromRect:NSZeroRect\n" 3118 " operation:NSCompositeCopy\n" 3119 " fraction:1.0\n" 3120 " respectFlipped:NO\n" 3121 " hints:nil];"); 3122 3123 verifyFormat( 3124 "scoped_nsobject<NSTextField> message(\n" 3125 " // The frame will be fixed up when |-setMessageText:| is called.\n" 3126 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 3127 } 3128 3129 TEST_F(FormatTest, ObjCAt) { 3130 verifyFormat("@autoreleasepool"); 3131 verifyFormat("@catch"); 3132 verifyFormat("@class"); 3133 verifyFormat("@compatibility_alias"); 3134 verifyFormat("@defs"); 3135 verifyFormat("@dynamic"); 3136 verifyFormat("@encode"); 3137 verifyFormat("@end"); 3138 verifyFormat("@finally"); 3139 verifyFormat("@implementation"); 3140 verifyFormat("@import"); 3141 verifyFormat("@interface"); 3142 verifyFormat("@optional"); 3143 verifyFormat("@package"); 3144 verifyFormat("@private"); 3145 verifyFormat("@property"); 3146 verifyFormat("@protected"); 3147 verifyFormat("@protocol"); 3148 verifyFormat("@public"); 3149 verifyFormat("@required"); 3150 verifyFormat("@selector"); 3151 verifyFormat("@synchronized"); 3152 verifyFormat("@synthesize"); 3153 verifyFormat("@throw"); 3154 verifyFormat("@try"); 3155 3156 EXPECT_EQ("@interface", format("@ interface")); 3157 3158 // The precise formatting of this doesn't matter, nobody writes code like 3159 // this. 3160 verifyFormat("@ /*foo*/ interface"); 3161 } 3162 3163 TEST_F(FormatTest, ObjCSnippets) { 3164 verifyFormat("@autoreleasepool {\n" 3165 " foo();\n" 3166 "}"); 3167 verifyFormat("@class Foo, Bar;"); 3168 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 3169 verifyFormat("@dynamic textColor;"); 3170 verifyFormat("char *buf1 = @encode(int *);"); 3171 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 3172 verifyFormat("char *buf1 = @encode(int **);"); 3173 verifyFormat("Protocol *proto = @protocol(p1);"); 3174 verifyFormat("SEL s = @selector(foo:);"); 3175 verifyFormat("@synchronized(self) {\n" 3176 " f();\n" 3177 "}"); 3178 3179 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 3180 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 3181 3182 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 3183 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 3184 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 3185 } 3186 3187 TEST_F(FormatTest, ObjCLiterals) { 3188 verifyFormat("@\"String\""); 3189 verifyFormat("@1"); 3190 verifyFormat("@+4.8"); 3191 verifyFormat("@-4"); 3192 verifyFormat("@1LL"); 3193 verifyFormat("@.5"); 3194 verifyFormat("@'c'"); 3195 verifyFormat("@true"); 3196 3197 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 3198 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 3199 verifyFormat("NSNumber *favoriteColor = @(Green);"); 3200 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 3201 3202 verifyFormat("@["); 3203 verifyFormat("@[]"); 3204 verifyFormat( 3205 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 3206 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 3207 3208 verifyFormat("@{"); 3209 verifyFormat("@{}"); 3210 verifyFormat("@{ @\"one\" : @1 }"); 3211 verifyFormat("return @{ @\"one\" : @1 };"); 3212 verifyFormat("@{ @\"one\" : @1, }"); 3213 verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); 3214 verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); 3215 verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); 3216 verifyFormat("[self setDict:@{}"); 3217 verifyFormat("[self setDict:@{ @1 : @2 }"); 3218 verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); 3219 verifyFormat( 3220 "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); 3221 verifyFormat( 3222 "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); 3223 3224 // FIXME: Nested and multi-line array and dictionary literals need more work. 3225 verifyFormat( 3226 "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" 3227 " @\"processInfo\" : [NSProcessInfo processInfo] };"); 3228 } 3229 3230 TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 3231 EXPECT_EQ("{\n" 3232 "{\n" 3233 "a;\n" 3234 "b;\n" 3235 "}\n" 3236 "}", 3237 format("{\n" 3238 "{\n" 3239 "a;\n" 3240 " b;\n" 3241 "}\n" 3242 "}", 3243 13, 2, getLLVMStyle())); 3244 EXPECT_EQ("{\n" 3245 "{\n" 3246 " a;\n" 3247 "b;\n" 3248 "}\n" 3249 "}", 3250 format("{\n" 3251 "{\n" 3252 " a;\n" 3253 "b;\n" 3254 "}\n" 3255 "}", 3256 9, 2, getLLVMStyle())); 3257 EXPECT_EQ("{\n" 3258 "{\n" 3259 "public:\n" 3260 " b;\n" 3261 "}\n" 3262 "}", 3263 format("{\n" 3264 "{\n" 3265 "public:\n" 3266 " b;\n" 3267 "}\n" 3268 "}", 3269 17, 2, getLLVMStyle())); 3270 EXPECT_EQ("{\n" 3271 "{\n" 3272 "a;\n" 3273 "}\n" 3274 "{\n" 3275 " b;\n" 3276 "}\n" 3277 "}", 3278 format("{\n" 3279 "{\n" 3280 "a;\n" 3281 "}\n" 3282 "{\n" 3283 " b;\n" 3284 "}\n" 3285 "}", 3286 22, 2, getLLVMStyle())); 3287 EXPECT_EQ(" {\n" 3288 " a;\n" 3289 " }", 3290 format(" {\n" 3291 "a;\n" 3292 " }", 3293 4, 2, getLLVMStyle())); 3294 EXPECT_EQ("void f() {}\n" 3295 "void g() {}", 3296 format("void f() {}\n" 3297 "void g() {}", 3298 13, 0, getLLVMStyle())); 3299 EXPECT_EQ("int a; // comment\n" 3300 " // line 2\n" 3301 "int b;", 3302 format("int a; // comment\n" 3303 " // line 2\n" 3304 " int b;", 3305 35, 0, getLLVMStyle())); 3306 } 3307 3308 TEST_F(FormatTest, BreakStringLiterals) { 3309 EXPECT_EQ("\"some text \"\n" 3310 "\"other\";", 3311 format("\"some text other\";", getLLVMStyleWithColumns(12))); 3312 EXPECT_EQ( 3313 "#define A \\\n" 3314 " \"some \" \\\n" 3315 " \"text \" \\\n" 3316 " \"other\";", 3317 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 3318 EXPECT_EQ( 3319 "#define A \\\n" 3320 " \"so \" \\\n" 3321 " \"text \" \\\n" 3322 " \"other\";", 3323 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 3324 3325 EXPECT_EQ("\"some text\"", 3326 format("\"some text\"", getLLVMStyleWithColumns(1))); 3327 EXPECT_EQ("\"some text\"", 3328 format("\"some text\"", getLLVMStyleWithColumns(11))); 3329 EXPECT_EQ("\"some \"\n" 3330 "\"text\"", 3331 format("\"some text\"", getLLVMStyleWithColumns(10))); 3332 EXPECT_EQ("\"some \"\n" 3333 "\"text\"", 3334 format("\"some text\"", getLLVMStyleWithColumns(7))); 3335 EXPECT_EQ("\"some\"\n" 3336 "\" text\"", 3337 format("\"some text\"", getLLVMStyleWithColumns(6))); 3338 EXPECT_EQ("\"some\"\n" 3339 "\" tex\"\n" 3340 "\" and\"", 3341 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 3342 EXPECT_EQ("\"some\"\n" 3343 "\"/tex\"\n" 3344 "\"/and\"", 3345 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 3346 3347 EXPECT_EQ("variable =\n" 3348 " \"long string \"\n" 3349 " \"literal\";", 3350 format("variable = \"long string literal\";", 3351 getLLVMStyleWithColumns(20))); 3352 3353 EXPECT_EQ("variable = f(\n" 3354 " \"long string \"\n" 3355 " \"literal\",\n" 3356 " short,\n" 3357 " loooooooooooooooooooong);", 3358 format("variable = f(\"long string literal\", short, " 3359 "loooooooooooooooooooong);", 3360 getLLVMStyleWithColumns(20))); 3361 EXPECT_EQ( 3362 "f(\"one two\".split(\n" 3363 " variable));", 3364 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 3365 EXPECT_EQ("f(\"one two three four five six \"\n" 3366 " \"seven\".split(\n" 3367 " really_looooong_variable));", 3368 format("f(\"one two three four five six seven\"." 3369 "split(really_looooong_variable));", 3370 getLLVMStyleWithColumns(33))); 3371 3372 EXPECT_EQ("f(\"some \"\n" 3373 " \"text\",\n" 3374 " other);", 3375 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 3376 3377 // Only break as a last resort. 3378 verifyFormat( 3379 "aaaaaaaaaaaaaaaaaaaa(\n" 3380 " aaaaaaaaaaaaaaaaaaaa,\n" 3381 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 3382 3383 EXPECT_EQ( 3384 "\"splitmea\"\n" 3385 "\"trandomp\"\n" 3386 "\"oint\"", 3387 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 3388 3389 EXPECT_EQ( 3390 "\"split/\"\n" 3391 "\"pathat/\"\n" 3392 "\"slashes\"", 3393 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 3394 } 3395 3396 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 3397 EXPECT_EQ("\"\\a\"", 3398 format("\"\\a\"", getLLVMStyleWithColumns(3))); 3399 EXPECT_EQ("\"\\\"", 3400 format("\"\\\"", getLLVMStyleWithColumns(2))); 3401 EXPECT_EQ("\"test\"\n" 3402 "\"\\n\"", 3403 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 3404 EXPECT_EQ("\"tes\\\\\"\n" 3405 "\"n\"", 3406 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 3407 EXPECT_EQ("\"\\\\\\\\\"\n" 3408 "\"\\n\"", 3409 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 3410 EXPECT_EQ("\"\\uff01\"", 3411 format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 3412 EXPECT_EQ("\"\\uff01\"\n" 3413 "\"test\"", 3414 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 3415 EXPECT_EQ("\"\\Uff01ff02\"", 3416 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 3417 EXPECT_EQ("\"\\x000000000001\"\n" 3418 "\"next\"", 3419 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 3420 EXPECT_EQ("\"\\x000000000001next\"", 3421 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 3422 EXPECT_EQ("\"\\x000000000001\"", 3423 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 3424 EXPECT_EQ("\"test\"\n" 3425 "\"\\000000\"\n" 3426 "\"000001\"", 3427 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 3428 EXPECT_EQ("\"test\\000\"\n" 3429 "\"000000001\"", 3430 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 3431 EXPECT_EQ("R\"(\\x\\x00)\"\n", 3432 format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7))); 3433 } 3434 3435 } // end namespace tooling 3436 } // end namespace clang 3437