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(Style, Lex, Context.Sources, 36 Ranges, 37 new IgnoringDiagConsumer()); 38 ReplacementCount = Replace.size(); 39 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); 40 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); 41 return Context.getRewrittenText(ID); 42 } 43 44 std::string format(llvm::StringRef Code, 45 const FormatStyle &Style = getLLVMStyle()) { 46 return format(Code, 0, Code.size(), Style); 47 } 48 49 std::string messUp(llvm::StringRef Code) { 50 std::string MessedUp(Code.str()); 51 bool InComment = false; 52 bool InPreprocessorDirective = false; 53 bool JustReplacedNewline = false; 54 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { 55 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { 56 if (JustReplacedNewline) 57 MessedUp[i - 1] = '\n'; 58 InComment = true; 59 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { 60 if (i != 0) 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 "}", format("if(a){f();}")); 156 EXPECT_EQ(4, ReplacementCount); 157 EXPECT_EQ("if (a) {\n" 158 " f();\n" 159 "}", format("if (a) {\n" 160 " f();\n" 161 "}")); 162 EXPECT_EQ(0, ReplacementCount); 163 } 164 165 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { 166 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); 167 } 168 169 //===----------------------------------------------------------------------===// 170 // Tests for control statements. 171 //===----------------------------------------------------------------------===// 172 173 TEST_F(FormatTest, FormatIfWithoutCompountStatement) { 174 verifyFormat("if (true)\n f();\ng();"); 175 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 176 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 177 178 FormatStyle AllowsMergedIf = getGoogleStyle(); 179 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 180 verifyFormat("if (a)\n" 181 " // comment\n" 182 " f();", AllowsMergedIf); 183 184 verifyFormat("if (a) // Can't merge this\n" 185 " f();\n", AllowsMergedIf); 186 verifyFormat("if (a) /* still don't merge */\n" 187 " f();", AllowsMergedIf); 188 verifyFormat("if (a) { // Never merge this\n" 189 " f();\n" 190 "}", AllowsMergedIf); 191 verifyFormat("if (a) { /* Never merge this */\n" 192 " f();\n" 193 "}", AllowsMergedIf); 194 195 AllowsMergedIf.ColumnLimit = 14; 196 verifyFormat("if (a) return;", AllowsMergedIf); 197 verifyFormat("if (aaaaaaaaa)\n" 198 " return;", AllowsMergedIf); 199 200 AllowsMergedIf.ColumnLimit = 13; 201 verifyFormat("if (a)\n return;", AllowsMergedIf); 202 } 203 204 TEST_F(FormatTest, ParseIfElse) { 205 verifyFormat("if (true)\n" 206 " if (true)\n" 207 " if (true)\n" 208 " f();\n" 209 " else\n" 210 " g();\n" 211 " else\n" 212 " h();\n" 213 "else\n" 214 " i();"); 215 verifyFormat("if (true)\n" 216 " if (true)\n" 217 " if (true) {\n" 218 " if (true)\n" 219 " f();\n" 220 " } else {\n" 221 " g();\n" 222 " }\n" 223 " else\n" 224 " h();\n" 225 "else {\n" 226 " i();\n" 227 "}"); 228 } 229 230 TEST_F(FormatTest, ElseIf) { 231 verifyFormat("if (a) {\n} else if (b) {\n}"); 232 verifyFormat("if (a)\n" 233 " f();\n" 234 "else if (b)\n" 235 " g();\n" 236 "else\n" 237 " h();"); 238 } 239 240 TEST_F(FormatTest, FormatsForLoop) { 241 verifyFormat( 242 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 243 " ++VeryVeryLongLoopVariable)\n" 244 " ;"); 245 verifyFormat("for (;;)\n" 246 " f();"); 247 verifyFormat("for (;;) {\n}"); 248 verifyFormat("for (;;) {\n" 249 " f();\n" 250 "}"); 251 252 verifyFormat( 253 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 254 " E = UnwrappedLines.end();\n" 255 " I != E; ++I) {\n}"); 256 257 verifyFormat( 258 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 259 " ++IIIII) {\n}"); 260 verifyFormat( 261 "for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 262 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 263 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 264 265 // FIXME: Not sure whether we want extra identation in line 3 here: 266 verifyFormat( 267 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 271 " ++aaaaaaaaaaa) {\n}"); 272 273 verifyGoogleFormat( 274 "for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 275 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 276 "}"); 277 verifyGoogleFormat( 278 "for (int aaaaaaaaaaa = 1;\n" 279 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 280 " aaaaaaaaaaaaaaaa,\n" 281 " aaaaaaaaaaaaaaaa,\n" 282 " aaaaaaaaaaaaaaaa);\n" 283 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 284 "}"); 285 } 286 287 TEST_F(FormatTest, RangeBasedForLoops) { 288 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 290 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 291 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 292 } 293 294 TEST_F(FormatTest, FormatsWhileLoop) { 295 verifyFormat("while (true) {\n}"); 296 verifyFormat("while (true)\n" 297 " f();"); 298 verifyFormat("while () {\n}"); 299 verifyFormat("while () {\n" 300 " f();\n" 301 "}"); 302 } 303 304 TEST_F(FormatTest, FormatsDoWhile) { 305 verifyFormat("do {\n" 306 " do_something();\n" 307 "} while (something());"); 308 verifyFormat("do\n" 309 " do_something();\n" 310 "while (something());"); 311 } 312 313 TEST_F(FormatTest, FormatsSwitchStatement) { 314 verifyFormat("switch (x) {\n" 315 "case 1:\n" 316 " f();\n" 317 " break;\n" 318 "case kFoo:\n" 319 "case ns::kBar:\n" 320 "case kBaz:\n" 321 " break;\n" 322 "default:\n" 323 " g();\n" 324 " break;\n" 325 "}"); 326 verifyFormat("switch (x) {\n" 327 "case 1: {\n" 328 " f();\n" 329 " break;\n" 330 "}\n" 331 "}"); 332 verifyFormat("switch (x) {\n" 333 "case 1: {\n" 334 " f();\n" 335 " {\n" 336 " g();\n" 337 " h();\n" 338 " }\n" 339 " break;\n" 340 "}\n" 341 "}"); 342 verifyFormat("switch (x) {\n" 343 "case 1: {\n" 344 " f();\n" 345 " if (foo) {\n" 346 " g();\n" 347 " h();\n" 348 " }\n" 349 " break;\n" 350 "}\n" 351 "}"); 352 verifyFormat("switch (x) {\n" 353 "case 1: {\n" 354 " f();\n" 355 " g();\n" 356 "} break;\n" 357 "}"); 358 verifyFormat("switch (test)\n" 359 " ;"); 360 361 // FIXME: Improve formatting of case labels in macros. 362 verifyFormat("#define SOMECASES \\\n" 363 "case 1: \\\n" 364 " case 2\n", getLLVMStyleWithColumns(20)); 365 366 verifyGoogleFormat("switch (x) {\n" 367 " case 1:\n" 368 " f();\n" 369 " break;\n" 370 " case kFoo:\n" 371 " case ns::kBar:\n" 372 " case kBaz:\n" 373 " break;\n" 374 " default:\n" 375 " g();\n" 376 " break;\n" 377 "}"); 378 verifyGoogleFormat("switch (x) {\n" 379 " case 1: {\n" 380 " f();\n" 381 " break;\n" 382 " }\n" 383 "}"); 384 verifyGoogleFormat("switch (test)\n" 385 " ;"); 386 } 387 388 TEST_F(FormatTest, FormatsLabels) { 389 verifyFormat("void f() {\n" 390 " some_code();\n" 391 "test_label:\n" 392 " some_other_code();\n" 393 " {\n" 394 " some_more_code();\n" 395 " another_label:\n" 396 " some_more_code();\n" 397 " }\n" 398 "}"); 399 verifyFormat("some_code();\n" 400 "test_label:\n" 401 "some_other_code();"); 402 } 403 404 //===----------------------------------------------------------------------===// 405 // Tests for comments. 406 //===----------------------------------------------------------------------===// 407 408 TEST_F(FormatTest, UnderstandsSingleLineComments) { 409 verifyFormat("// line 1\n" 410 "// line 2\n" 411 "void f() {}\n"); 412 413 verifyFormat("void f() {\n" 414 " // Doesn't do anything\n" 415 "}"); 416 verifyFormat("void f(int i, // some comment (probably for i)\n" 417 " int j, // some comment (probably for j)\n" 418 " int k); // some comment (probably for k)"); 419 verifyFormat("void f(int i,\n" 420 " // some comment (probably for j)\n" 421 " int j,\n" 422 " // some comment (probably for k)\n" 423 " int k);"); 424 425 verifyFormat("int i // This is a fancy variable\n" 426 " = 5; // with nicely aligned comment."); 427 428 verifyFormat("// Leading comment.\n" 429 "int a; // Trailing comment."); 430 verifyFormat("int a; // Trailing comment\n" 431 " // on 2\n" 432 " // or 3 lines.\n" 433 "int b;"); 434 verifyFormat("int a; // Trailing comment\n" 435 "\n" 436 "// Leading comment.\n" 437 "int b;"); 438 verifyFormat("int a; // Comment.\n" 439 " // More details.\n" 440 "int bbbb; // Another comment."); 441 verifyFormat( 442 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 443 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 444 "int cccccccccccccccccccccccccccccc; // comment\n" 445 "int ddd; // looooooooooooooooooooooooong comment\n" 446 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 447 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 448 "int ccccccccccccccccccc; // comment"); 449 450 verifyFormat("#include \"a\" // comment\n" 451 "#include \"a/b/c\" // comment"); 452 verifyFormat("#include <a> // comment\n" 453 "#include <a/b/c> // comment"); 454 455 verifyFormat("enum E {\n" 456 " // comment\n" 457 " VAL_A, // comment\n" 458 " VAL_B\n" 459 "};"); 460 461 verifyFormat( 462 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 463 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 464 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 465 " // Comment inside a statement.\n" 466 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 467 verifyFormat( 468 "bool aaaaaaaaaaaaa = // comment\n" 469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 471 472 verifyFormat("int aaaa; // aaaaa\n" 473 "int aa; // aaaaaaa", getLLVMStyleWithColumns(20)); 474 475 EXPECT_EQ("void f() { // This does something ..\n" 476 "}\n" 477 "int a; // This is unrelated", 478 format("void f() { // This does something ..\n" 479 " }\n" 480 "int a; // This is unrelated")); 481 EXPECT_EQ("void f() { // This does something ..\n" 482 "} // awesome..\n" 483 "\n" 484 "int a; // This is unrelated", 485 format("void f() { // This does something ..\n" 486 " } // awesome..\n" 487 " \n" 488 "int a; // This is unrelated")); 489 490 EXPECT_EQ("int i; // single line trailing comment", 491 format("int i;\\\n// single line trailing comment")); 492 493 verifyGoogleFormat("int a; // Trailing comment."); 494 495 verifyFormat("someFunction(anotherFunction( // Force break.\n" 496 " parameter));"); 497 498 verifyGoogleFormat("#endif // HEADER_GUARD"); 499 500 verifyFormat("const char *test[] = {\n" 501 " // A\n" 502 " \"aaaa\",\n" 503 " // B\n" 504 " \"aaaaa\",\n" 505 "};"); 506 verifyGoogleFormat( 507 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 508 " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); 509 } 510 511 TEST_F(FormatTest, UnderstandsMultiLineComments) { 512 verifyFormat("f(/*test=*/ true);"); 513 EXPECT_EQ( 514 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 515 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 516 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" 517 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 518 EXPECT_EQ( 519 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 520 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 521 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 522 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 523 524 verifyGoogleFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 525 " /* parameter 2 */ aaaaaa,\n" 526 " /* parameter 3 */ aaaaaa,\n" 527 " /* parameter 4 */ aaaaaa);"); 528 } 529 530 TEST_F(FormatTest, CommentsInStaticInitializers) { 531 EXPECT_EQ( 532 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 533 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 534 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 535 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 536 " aaaaaaaaaaaaaaaaaaaa };", 537 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 538 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 539 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 540 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 541 " aaaaaaaaaaaaaaaaaaaa };")); 542 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 543 " bbbbbbbbbbb, ccccccccccc };"); 544 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" 545 " // comment for bb....\n" 546 " bbbbbbbbbbb, ccccccccccc };"); 547 verifyGoogleFormat( 548 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 549 " bbbbbbbbbbb, ccccccccccc };"); 550 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" 551 " // comment for bb....\n" 552 " bbbbbbbbbbb, ccccccccccc };"); 553 554 verifyFormat("S s = { { a, b, c }, // Group #1\n" 555 " { d, e, f }, // Group #2\n" 556 " { g, h, i } }; // Group #3"); 557 verifyFormat("S s = { { // Group #1\n" 558 " a, b, c },\n" 559 " { // Group #2\n" 560 " d, e, f },\n" 561 " { // Group #3\n" 562 " g, h, i } };"); 563 } 564 565 //===----------------------------------------------------------------------===// 566 // Tests for classes, namespaces, etc. 567 //===----------------------------------------------------------------------===// 568 569 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 570 verifyFormat("class A {\n};"); 571 } 572 573 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 574 verifyFormat("class A {\n" 575 "public:\n" 576 "protected:\n" 577 "private:\n" 578 " void f() {}\n" 579 "};"); 580 verifyGoogleFormat("class A {\n" 581 " public:\n" 582 " protected:\n" 583 " private:\n" 584 " void f() {}\n" 585 "};"); 586 } 587 588 TEST_F(FormatTest, FormatsDerivedClass) { 589 verifyFormat("class A : public B {\n};"); 590 verifyFormat("class A : public ::B {\n};"); 591 592 verifyFormat( 593 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 594 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" 595 "};\n"); 596 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n" 597 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 598 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" 599 "};\n"); 600 verifyFormat( 601 "class A : public B, public C, public D, public E, public F, public G {\n" 602 "};"); 603 verifyFormat("class AAAAAAAAAAAA : public B,\n" 604 " public C,\n" 605 " public D,\n" 606 " public E,\n" 607 " public F,\n" 608 " public G {\n" 609 "};"); 610 } 611 612 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 613 verifyFormat("class A {\n} a, b;"); 614 verifyFormat("struct A {\n} a, b;"); 615 verifyFormat("union A {\n} a;"); 616 } 617 618 TEST_F(FormatTest, FormatsEnum) { 619 verifyFormat("enum {\n" 620 " Zero,\n" 621 " One = 1,\n" 622 " Two = One + 1,\n" 623 " Three = (One + Two),\n" 624 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 625 " Five = (One, Two, Three, Four, 5)\n" 626 "};"); 627 verifyFormat("enum Enum {\n" 628 "};"); 629 verifyFormat("enum {\n" 630 "};"); 631 verifyFormat("enum X E {\n} d;"); 632 verifyFormat("enum __attribute__((...)) E {\n} d;"); 633 verifyFormat("enum __declspec__((...)) E {\n} d;"); 634 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 635 } 636 637 TEST_F(FormatTest, FormatsBitfields) { 638 verifyFormat("struct Bitfields {\n" 639 " unsigned sClass : 8;\n" 640 " unsigned ValueKind : 2;\n" 641 "};"); 642 } 643 644 TEST_F(FormatTest, FormatsNamespaces) { 645 verifyFormat("namespace some_namespace {\n" 646 "class A {\n};\n" 647 "void f() { f(); }\n" 648 "}"); 649 verifyFormat("namespace {\n" 650 "class A {\n};\n" 651 "void f() { f(); }\n" 652 "}"); 653 verifyFormat("inline namespace X {\n" 654 "class A {\n};\n" 655 "void f() { f(); }\n" 656 "}"); 657 verifyFormat("using namespace some_namespace;\n" 658 "class A {\n};\n" 659 "void f() { f(); }"); 660 661 // This code is more common than we thought; if we 662 // layout this correctly the semicolon will go into 663 // its own line, which is undesireable. 664 verifyFormat("namespace {\n};"); 665 verifyFormat("namespace {\n" 666 "class A {\n" 667 "};\n" 668 "};"); 669 } 670 671 TEST_F(FormatTest, FormatsExternC) { 672 verifyFormat("extern \"C\" {\nint a;"); 673 } 674 675 TEST_F(FormatTest, FormatTryCatch) { 676 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll 677 // also not create single-line-blocks. 678 verifyFormat("try {\n" 679 " throw a * b;\n" 680 "}\n" 681 "catch (int a) {\n" 682 " // Do nothing.\n" 683 "}\n" 684 "catch (...) {\n" 685 " exit(42);\n" 686 "}"); 687 688 // Function-level try statements. 689 verifyFormat("int f() try { return 4; }\n" 690 "catch (...) {\n" 691 " return 5;\n" 692 "}"); 693 verifyFormat("class A {\n" 694 " int a;\n" 695 " A() try : a(0) {}\n" 696 " catch (...) {\n" 697 " throw;\n" 698 " }\n" 699 "};\n"); 700 } 701 702 TEST_F(FormatTest, FormatObjCTryCatch) { 703 verifyFormat("@try {\n" 704 " f();\n" 705 "}\n" 706 "@catch (NSException e) {\n" 707 " @throw;\n" 708 "}\n" 709 "@finally {\n" 710 " exit(42);\n" 711 "}"); 712 } 713 714 TEST_F(FormatTest, StaticInitializers) { 715 verifyFormat("static SomeClass SC = { 1, 'a' };"); 716 717 // FIXME: Format like enums if the static initializer does not fit on a line. 718 verifyFormat( 719 "static SomeClass WithALoooooooooooooooooooongName = {\n" 720 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 721 "};"); 722 723 verifyFormat( 724 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 725 " looooooooooooooooooooooooooooooooooongname,\n" 726 " looooooooooooooooooooooooooooooong };"); 727 // Allow bin-packing in static initializers as this would often lead to 728 // terrible results, e.g.: 729 verifyGoogleFormat( 730 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 731 " looooooooooooooooooooooooooooooooooongname,\n" 732 " looooooooooooooooooooooooooooooong };"); 733 } 734 735 TEST_F(FormatTest, NestedStaticInitializers) { 736 verifyFormat("static A x = { { {} } };\n"); 737 verifyFormat( 738 "static A x = { { { init1, init2, init3, init4 },\n" 739 " { init1, init2, init3, init4 } } };"); 740 741 verifyFormat( 742 "somes Status::global_reps[3] = {\n" 743 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 744 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 745 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 746 "};"); 747 verifyGoogleFormat( 748 "somes Status::global_reps[3] = {\n" 749 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 750 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 751 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 752 "};"); 753 verifyFormat( 754 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" 755 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" 756 " } };"); 757 758 verifyFormat( 759 "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" 760 " { 111111111111111111111111111111,\n" 761 " 222222222222222222222222222222,\n" 762 " 333333333333333333333333333333 },\n" 763 " { 1, 2, 3 }, { 1, 2, 3 } } };"); 764 verifyFormat( 765 "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" 766 " { { 111111111111111111111111111111,\n" 767 " 222222222222222222222222222222,\n" 768 " 333333333333333333333333333333 } },\n" 769 " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); 770 771 // FIXME: We might at some point want to handle this similar to parameter 772 // lists, where we have an option to put each on a single line. 773 verifyFormat("struct {\n" 774 " unsigned bit;\n" 775 " const char *const name;\n" 776 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" 777 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); 778 } 779 780 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 781 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 782 " \\\n" 783 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 784 } 785 786 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 787 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 788 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 789 } 790 791 TEST_F(FormatTest, LayoutUnknownPPDirective) { 792 EXPECT_EQ("#123 \"A string literal\"", 793 format(" # 123 \"A string literal\"")); 794 EXPECT_EQ("#;", format("#;")); 795 verifyFormat("#\n;\n;\n;"); 796 } 797 798 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 799 EXPECT_EQ("#line 42 \"test\"\n", 800 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 801 EXPECT_EQ("#define A B\n", 802 format("# \\\n define \\\n A \\\n B\n", 803 getLLVMStyleWithColumns(12))); 804 } 805 806 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 807 EXPECT_EQ("#line 42 \"test\"", 808 format("# \\\n line \\\n 42 \\\n \"test\"")); 809 EXPECT_EQ("#define A B", 810 format("# \\\n define \\\n A \\\n B")); 811 } 812 813 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 814 // If the macro fits in one line, we still do not get the full 815 // line, as only the next line decides whether we need an escaped newline and 816 // thus use the last column. 817 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); 818 819 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); 820 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); 821 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 822 823 verifyFormat("#define A A\n#define A A"); 824 verifyFormat("#define A(X) A\n#define A A"); 825 826 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24)); 827 verifyFormat("#define Something \\\n" 828 " Other", getLLVMStyleWithColumns(23)); 829 } 830 831 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 832 EXPECT_EQ("// some comment\n" 833 "#include \"a.h\"\n" 834 "#define A(A,\\\n" 835 " B)\n" 836 "#include \"b.h\"\n" 837 "// some comment\n", 838 format(" // some comment\n" 839 " #include \"a.h\"\n" 840 "#define A(A,\\\n" 841 " B)\n" 842 " #include \"b.h\"\n" 843 " // some comment\n", getLLVMStyleWithColumns(13))); 844 } 845 846 TEST_F(FormatTest, LayoutSingleHash) { 847 EXPECT_EQ("#\na;", format("#\na;")); 848 } 849 850 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 851 EXPECT_EQ("#define A \\\n" 852 " c; \\\n" 853 " e;\n" 854 "f;", format("#define A c; e;\n" 855 "f;", getLLVMStyleWithColumns(14))); 856 } 857 858 TEST_F(FormatTest, LayoutRemainingTokens) { 859 EXPECT_EQ("{}", format("{}")); 860 } 861 862 TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { 863 EXPECT_EQ("# define A\\\n b;", 864 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); 865 } 866 867 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 868 EXPECT_EQ("int x,\n" 869 "#define A\n" 870 " y;", format("int x,\n#define A\ny;")); 871 } 872 873 TEST_F(FormatTest, HashInMacroDefinition) { 874 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 875 verifyFormat("#define A \\\n" 876 " { \\\n" 877 " f(#c);\\\n" 878 " }", getLLVMStyleWithColumns(11)); 879 880 verifyFormat("#define A(X) \\\n" 881 " void function##X()", getLLVMStyleWithColumns(22)); 882 883 verifyFormat("#define A(a, b, c) \\\n" 884 " void a##b##c()", getLLVMStyleWithColumns(22)); 885 886 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 887 } 888 889 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 890 verifyFormat("#define A (1)"); 891 } 892 893 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 894 EXPECT_EQ("#define A b;", format("#define A \\\n" 895 " \\\n" 896 " b;", getLLVMStyleWithColumns(25))); 897 EXPECT_EQ("#define A \\\n" 898 " \\\n" 899 " a; \\\n" 900 " b;", format("#define A \\\n" 901 " \\\n" 902 " a; \\\n" 903 " b;", getLLVMStyleWithColumns(11))); 904 EXPECT_EQ("#define A \\\n" 905 " a; \\\n" 906 " \\\n" 907 " b;", format("#define A \\\n" 908 " a; \\\n" 909 " \\\n" 910 " b;", getLLVMStyleWithColumns(11))); 911 } 912 913 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 914 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 915 } 916 917 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 918 verifyFormat("{\n { a #c; }\n}"); 919 } 920 921 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 922 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 923 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 924 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 925 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 926 } 927 928 TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { 929 EXPECT_EQ( 930 "#define A \\\n int i; \\\n int j;", 931 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 932 } 933 934 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 935 verifyFormat("#define A \\\n" 936 " int v( \\\n" 937 " a); \\\n" 938 " int i;", getLLVMStyleWithColumns(11)); 939 } 940 941 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 942 EXPECT_EQ( 943 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 944 " \\\n" 945 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 946 "\n" 947 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 948 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 949 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 950 "\\\n" 951 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 952 " \n" 953 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 954 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 955 } 956 957 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 958 EXPECT_EQ("int\n" 959 "#define A\n" 960 " a;", 961 format("int\n#define A\na;")); 962 verifyFormat( 963 "functionCallTo(\n" 964 " someOtherFunction(\n" 965 " withSomeParameters, whichInSequence,\n" 966 " areLongerThanALine(andAnotherCall,\n" 967 "#define A B\n" 968 " withMoreParamters,\n" 969 " whichStronglyInfluenceTheLayout),\n" 970 " andMoreParameters), trailing);", 971 getLLVMStyleWithColumns(69)); 972 } 973 974 TEST_F(FormatTest, LayoutBlockInsideParens) { 975 EXPECT_EQ("functionCall({\n" 976 " int i;\n" 977 "});", format(" functionCall ( {int i;} );")); 978 } 979 980 TEST_F(FormatTest, LayoutBlockInsideStatement) { 981 EXPECT_EQ("SOME_MACRO { int i; }\n" 982 "int i;", format(" SOME_MACRO {int i;} int i;")); 983 } 984 985 TEST_F(FormatTest, LayoutNestedBlocks) { 986 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 987 " struct s {\n" 988 " int i;\n" 989 " };\n" 990 " s kBitsToOs[] = { { 10 } };\n" 991 " for (int i = 0; i < 10; ++i)\n" 992 " return;\n" 993 "}"); 994 } 995 996 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 997 EXPECT_EQ("{}", format("{}")); 998 999 // Negative test for enum. 1000 verifyFormat("enum E {\n};"); 1001 1002 // Note that when there's a missing ';', we still join... 1003 verifyFormat("enum E {}"); 1004 } 1005 1006 //===----------------------------------------------------------------------===// 1007 // Line break tests. 1008 //===----------------------------------------------------------------------===// 1009 1010 TEST_F(FormatTest, FormatsFunctionDefinition) { 1011 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," 1012 " int h, int j, int f,\n" 1013 " int c, int ddddddddddddd) {\n}"); 1014 } 1015 1016 TEST_F(FormatTest, FormatsAwesomeMethodCall) { 1017 verifyFormat( 1018 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 1019 " parameter, parameter, parameter)),\n" 1020 " SecondLongCall(parameter));"); 1021 } 1022 1023 TEST_F(FormatTest, PreventConfusingIndents) { 1024 verifyFormat( 1025 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1026 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 1027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1028 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 1029 verifyFormat( 1030 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" 1031 " aaaaaaaaaaaaaaaaaaaaaaaa[\n" 1032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" 1033 " aaaaaaaaaaaaaaaaaaaaaaaa];"); 1034 verifyFormat( 1035 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 1036 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 1037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 1038 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 1039 verifyFormat("int a = bbbb && ccc && fffff(\n" 1040 "#define A Just forcing a new line\n" 1041 " ddd);"); 1042 } 1043 1044 TEST_F(FormatTest, ConstructorInitializers) { 1045 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 1046 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 1047 getLLVMStyleWithColumns(45)); 1048 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}", 1049 getLLVMStyleWithColumns(44)); 1050 verifyFormat("Constructor()\n" 1051 " : Inttializer(FitsOnTheLine) {\n}", 1052 getLLVMStyleWithColumns(43)); 1053 1054 verifyFormat( 1055 "SomeClass::Constructor()\n" 1056 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); 1057 1058 verifyFormat( 1059 "SomeClass::Constructor()\n" 1060 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1061 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); 1062 verifyGoogleFormat( 1063 "SomeClass::Constructor()\n" 1064 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1065 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1066 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); 1067 verifyGoogleFormat( 1068 "SomeClass::Constructor()\n" 1069 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 1070 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1071 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); 1072 1073 verifyFormat( 1074 "SomeClass::Constructor()\n" 1075 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1076 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); 1077 1078 verifyFormat("Constructor()\n" 1079 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1080 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1081 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1082 " aaaaaaaaaaaaaaaaaaaaaaa() {\n}"); 1083 1084 verifyFormat("Constructor()\n" 1085 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1087 1088 // Here a line could be saved by splitting the second initializer onto two 1089 // lines, but that is not desireable. 1090 verifyFormat( 1091 "Constructor()\n" 1092 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 1093 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 1094 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1095 1096 verifyGoogleFormat("MyClass::MyClass(int var)\n" 1097 " : some_var_(var), // 4 space indent\n" 1098 " some_other_var_(var + 1) { // lined up\n" 1099 "}"); 1100 1101 // This test takes VERY long when memoization is broken. 1102 std::string input = "Constructor()\n" 1103 " : aaaa(a,\n"; 1104 for (unsigned i = 0, e = 80; i != e; ++i) { 1105 input += " a,\n"; 1106 } 1107 input += " a) {\n}"; 1108 verifyGoogleFormat(input); 1109 } 1110 1111 TEST_F(FormatTest, BreaksAsHighAsPossible) { 1112 verifyFormat( 1113 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 1114 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 1115 " f();"); 1116 } 1117 1118 TEST_F(FormatTest, BreaksDesireably) { 1119 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1120 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1121 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 1122 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1124 "}"); 1125 1126 verifyFormat( 1127 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1129 1130 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1133 1134 verifyFormat( 1135 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1137 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 1139 1140 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1141 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1142 1143 verifyFormat( 1144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1146 verifyFormat( 1147 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1149 verifyFormat( 1150 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1152 1153 // This test case breaks on an incorrect memoization, i.e. an optimization not 1154 // taking into account the StopAt value. 1155 verifyFormat( 1156 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1157 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1158 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1159 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1160 1161 verifyFormat("{\n {\n {\n" 1162 " Annotation.SpaceRequiredBefore =\n" 1163 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 1164 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 1165 " }\n }\n}"); 1166 } 1167 1168 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 1169 verifyGoogleFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 1170 " aaaaaaaaaaaaaaaaaaaa,\n" 1171 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);"); 1172 verifyGoogleFormat( 1173 "aaaaaaa(aaaaaaaaaaaaa,\n" 1174 " aaaaaaaaaaaaa,\n" 1175 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1176 verifyGoogleFormat( 1177 "aaaaaaaa(aaaaaaaaaaaaa,\n" 1178 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1180 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 1182 verifyGoogleFormat( 1183 "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 1184 " .aaaaaaaaaaaaaaaaaa();"); 1185 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1186 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);"); 1187 1188 verifyGoogleFormat( 1189 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1190 " aaaaaaaaaaaa,\n" 1191 " aaaaaaaaaaaa);"); 1192 verifyGoogleFormat( 1193 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 1194 " ddddddddddddddddddddddddddddd),\n" 1195 " test);"); 1196 1197 verifyGoogleFormat( 1198 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 1199 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 1200 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;"); 1201 verifyGoogleFormat("a(\"a\"\n" 1202 " \"a\",\n" 1203 " a);"); 1204 1205 FormatStyle Style = getGoogleStyle(); 1206 Style.AllowAllParametersOfDeclarationOnNextLine = false; 1207 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 1208 " aaaaaaaaa,\n" 1209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 1210 Style); 1211 verifyFormat( 1212 "void f() {\n" 1213 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 1214 " .aaaaaaa();\n" 1215 "}", Style); 1216 } 1217 1218 TEST_F(FormatTest, FormatsBuilderPattern) { 1219 verifyFormat( 1220 "return llvm::StringSwitch<Reference::Kind>(name)\n" 1221 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 1222 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" 1223 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" 1224 " .Default(ORDER_TEXT);\n"); 1225 } 1226 1227 TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 1228 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1229 " GUARDED_BY(aaaaaaaaaaaaa);"); 1230 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1231 " GUARDED_BY(aaaaaaaaaaaaa);"); 1232 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1233 " GUARDED_BY(aaaaaaaaaaaaa) {\n}"); 1234 } 1235 1236 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 1237 verifyFormat( 1238 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1239 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 1240 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1241 " ccccccccccccccccccccccccc) {\n}"); 1242 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1243 " ccccccccccccccccccccccccc) {\n}"); 1244 verifyFormat( 1245 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 1246 " ccccccccccccccccccccccccc) {\n}"); 1247 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 1248 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 1249 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 1250 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 1251 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 1252 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 1253 " aaaaaaaaaaaaaaa != aa) {\n}"); 1254 } 1255 1256 TEST_F(FormatTest, BreaksAfterAssignments) { 1257 verifyFormat( 1258 "unsigned Cost =\n" 1259 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 1260 " SI->getPointerAddressSpaceee());\n"); 1261 verifyFormat( 1262 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 1263 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 1264 1265 verifyFormat( 1266 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 1267 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 1268 } 1269 1270 TEST_F(FormatTest, AlignsAfterAssignments) { 1271 verifyFormat( 1272 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1273 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1274 verifyFormat( 1275 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1276 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1277 verifyFormat( 1278 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1279 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1280 verifyFormat( 1281 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1282 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1283 verifyFormat( 1284 "double LooooooooooooooooooooooooongResult =\n" 1285 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 1286 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 1287 } 1288 1289 TEST_F(FormatTest, AlignsAfterReturn) { 1290 verifyFormat( 1291 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1292 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1293 verifyFormat( 1294 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1295 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1296 } 1297 1298 TEST_F(FormatTest, BreaksConditionalExpressions) { 1299 verifyFormat( 1300 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 1301 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1302 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1303 verifyFormat( 1304 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1305 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1306 verifyFormat( 1307 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 1308 " : aaaaaaaaaaaaa);"); 1309 verifyFormat( 1310 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1311 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n" 1312 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1313 " aaaaaaaaaaaaa);"); 1314 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1315 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1317 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1319 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1321 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1323 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1325 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1326 verifyFormat( 1327 "unsigned Indent = formatFirstToken(\n" 1328 " TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 1329 " ? IndentForLevel[TheLine.Level] : TheLine * 2,\n" 1330 " TheLine.InPPDirective, PreviousEndOfLineColumn);"); 1331 } 1332 1333 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 1334 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 1335 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 1336 verifyFormat("bool a = true, b = false;"); 1337 1338 // FIXME: Indentation looks weird. 1339 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 1341 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 1342 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 1343 1344 // FIXME: This is bad as we hide "d". 1345 verifyFormat( 1346 "bool aaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 1347 " cccccccccccccccccccccccccccc, d = e && f;"); 1348 1349 } 1350 1351 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 1352 verifyFormat("arr[foo ? bar : baz];"); 1353 verifyFormat("f()[foo ? bar : baz];"); 1354 verifyFormat("(a + b)[foo ? bar : baz];"); 1355 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 1356 } 1357 1358 TEST_F(FormatTest, AlignsStringLiterals) { 1359 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 1360 " \"short literal\");"); 1361 verifyFormat( 1362 "looooooooooooooooooooooooongFunction(\n" 1363 " \"short literal\"\n" 1364 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 1365 } 1366 1367 TEST_F(FormatTest, AlignsPipes) { 1368 verifyFormat( 1369 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1370 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1371 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1372 verifyFormat( 1373 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 1374 " << aaaaaaaaaaaaaaaaaaaa;"); 1375 verifyFormat( 1376 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1377 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1378 verifyFormat( 1379 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1380 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 1381 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 1382 verifyFormat( 1383 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1384 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1385 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1386 1387 verifyFormat("return out << \"somepacket = {\\n\"\n" 1388 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 1389 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 1390 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 1391 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 1392 " << \"}\";"); 1393 1394 verifyFormat( 1395 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 1396 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 1397 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 1398 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 1399 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 1400 } 1401 1402 TEST_F(FormatTest, UnderstandsEquals) { 1403 verifyFormat( 1404 "aaaaaaaaaaaaaaaaa =\n" 1405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1406 verifyFormat( 1407 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1409 verifyFormat( 1410 "if (a) {\n" 1411 " f();\n" 1412 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1414 "}"); 1415 1416 verifyFormat( 1417 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1418 " 100000000 + 10000000) {\n}"); 1419 } 1420 1421 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 1422 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1423 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 1424 1425 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1426 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 1427 1428 verifyFormat( 1429 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 1430 " Parameter2);"); 1431 1432 verifyFormat( 1433 "ShortObject->shortFunction(\n" 1434 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 1435 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 1436 1437 verifyFormat("loooooooooooooongFunction(\n" 1438 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 1439 1440 verifyFormat( 1441 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 1442 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 1443 1444 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 1445 " .WillRepeatedly(Return(SomeValue));"); 1446 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" 1447 " .insert(ccccccccccccccccccccccc);"); 1448 1449 // Here, it is not necessary to wrap at "." or "->". 1450 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 1451 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1452 verifyFormat( 1453 "aaaaaaaaaaa->aaaaaaaaa(\n" 1454 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1455 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 1456 1457 verifyFormat( 1458 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 1460 } 1461 1462 TEST_F(FormatTest, WrapsTemplateDeclarations) { 1463 verifyFormat("template <typename T>\n" 1464 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 1465 verifyFormat( 1466 "template <typename T>\n" 1467 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 1468 verifyFormat( 1469 "template <typename T>\n" 1470 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 1471 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 1472 verifyFormat( 1473 "template <typename T>\n" 1474 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 1475 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 1476 verifyFormat( 1477 "template <typename T>\n" 1478 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 1479 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 1480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1481 verifyFormat("template <typename T>\n" 1482 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1483 " int aaaaaaaaaaaaaaaaa);"); 1484 verifyFormat( 1485 "template <typename T1, typename T2 = char, typename T3 = char,\n" 1486 " typename T4 = char>\n" 1487 "void f();"); 1488 verifyFormat( 1489 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 1490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1491 1492 verifyFormat( 1493 "a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 1494 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1495 } 1496 1497 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 1498 verifyFormat( 1499 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1501 verifyFormat( 1502 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 1505 1506 // FIXME: Should we have an extra indent after the second break? 1507 verifyFormat( 1508 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1511 1512 // FIXME: Look into whether we should indent 4 from the start or 4 from 1513 // "bbbbb..." here instead of what we are doing now. 1514 verifyFormat( 1515 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 1516 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 1517 1518 // Breaking at nested name specifiers is generally not desirable. 1519 verifyFormat( 1520 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1521 " aaaaaaaaaaaaaaaaaaaaaaa);"); 1522 1523 verifyFormat( 1524 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1526 " aaaaaaaaaaaaaaaaaaaaa);", 1527 getLLVMStyleWithColumns(74)); 1528 } 1529 1530 TEST_F(FormatTest, UnderstandsTemplateParameters) { 1531 verifyFormat("A<int> a;"); 1532 verifyFormat("A<A<A<int> > > a;"); 1533 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 1534 verifyFormat("bool x = a < 1 || 2 > a;"); 1535 verifyFormat("bool x = 5 < f<int>();"); 1536 verifyFormat("bool x = f<int>() > 5;"); 1537 verifyFormat("bool x = 5 < a<int>::x;"); 1538 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 1539 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 1540 1541 verifyGoogleFormat("A<A<int>> a;"); 1542 verifyGoogleFormat("A<A<A<int>>> a;"); 1543 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 1544 verifyGoogleFormat("A<A<int> > a;"); 1545 verifyGoogleFormat("A<A<A<int> > > a;"); 1546 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 1547 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 1548 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 1549 1550 verifyFormat("test >> a >> b;"); 1551 verifyFormat("test << a >> b;"); 1552 1553 verifyFormat("f<int>();"); 1554 verifyFormat("template <typename T> void f() {}"); 1555 } 1556 1557 TEST_F(FormatTest, UnderstandsUnaryOperators) { 1558 verifyFormat("int a = -2;"); 1559 verifyFormat("f(-1, -2, -3);"); 1560 verifyFormat("a[-1] = 5;"); 1561 verifyFormat("int a = 5 + -2;"); 1562 verifyFormat("if (i == -1) {\n}"); 1563 verifyFormat("if (i != -1) {\n}"); 1564 verifyFormat("if (i > -1) {\n}"); 1565 verifyFormat("if (i < -1) {\n}"); 1566 verifyFormat("++(a->f());"); 1567 verifyFormat("--(a->f());"); 1568 verifyFormat("(a->f())++;"); 1569 verifyFormat("a[42]++;"); 1570 verifyFormat("if (!(a->f())) {\n}"); 1571 1572 verifyFormat("a-- > b;"); 1573 verifyFormat("b ? -a : c;"); 1574 verifyFormat("n * sizeof char16;"); 1575 verifyFormat("n * alignof char16;"); 1576 verifyFormat("sizeof(char);"); 1577 verifyFormat("alignof(char);"); 1578 1579 verifyFormat("return -1;"); 1580 verifyFormat("switch (a) {\n" 1581 "case -1:\n" 1582 " break;\n" 1583 "}"); 1584 1585 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 1586 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 1587 1588 verifyFormat("int a = /* confusing comment */ -1;"); 1589 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 1590 verifyFormat("int a = i /* confusing comment */++;"); 1591 } 1592 1593 TEST_F(FormatTest, UndestandsOverloadedOperators) { 1594 verifyFormat("bool operator<();"); 1595 verifyFormat("bool operator>();"); 1596 verifyFormat("bool operator=();"); 1597 verifyFormat("bool operator==();"); 1598 verifyFormat("bool operator!=();"); 1599 verifyFormat("int operator+();"); 1600 verifyFormat("int operator++();"); 1601 verifyFormat("bool operator();"); 1602 verifyFormat("bool operator()();"); 1603 verifyFormat("bool operator[]();"); 1604 verifyFormat("operator bool();"); 1605 verifyFormat("operator int();"); 1606 verifyFormat("operator void *();"); 1607 verifyFormat("operator SomeType<int>();"); 1608 verifyFormat("operator SomeType<int, int>();"); 1609 verifyFormat("operator SomeType<SomeType<int> >();"); 1610 verifyFormat("void *operator new(std::size_t size);"); 1611 verifyFormat("void *operator new[](std::size_t size);"); 1612 verifyFormat("void operator delete(void *ptr);"); 1613 verifyFormat("void operator delete[](void *ptr);"); 1614 1615 verifyGoogleFormat("operator void*();"); 1616 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 1617 } 1618 1619 TEST_F(FormatTest, UnderstandsNewAndDelete) { 1620 verifyFormat("A *a = new A;"); 1621 verifyFormat("A *a = new (placement) A;"); 1622 verifyFormat("delete a;"); 1623 verifyFormat("delete (A *)a;"); 1624 } 1625 1626 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 1627 verifyFormat("int *f(int *a) {}"); 1628 verifyFormat("int main(int argc, char **argv) {}"); 1629 verifyIndependentOfContext("f(a, *a);"); 1630 verifyIndependentOfContext("f(*a);"); 1631 verifyIndependentOfContext("int a = b * 10;"); 1632 verifyIndependentOfContext("int a = 10 * b;"); 1633 verifyIndependentOfContext("int a = b * c;"); 1634 verifyIndependentOfContext("int a += b * c;"); 1635 verifyIndependentOfContext("int a -= b * c;"); 1636 verifyIndependentOfContext("int a *= b * c;"); 1637 verifyIndependentOfContext("int a /= b * c;"); 1638 verifyIndependentOfContext("int a = *b;"); 1639 verifyIndependentOfContext("int a = *b * c;"); 1640 verifyIndependentOfContext("int a = b * *c;"); 1641 verifyIndependentOfContext("return 10 * b;"); 1642 verifyIndependentOfContext("return *b * *c;"); 1643 verifyIndependentOfContext("return a & ~b;"); 1644 verifyIndependentOfContext("f(b ? *c : *d);"); 1645 verifyIndependentOfContext("int a = b ? *c : *d;"); 1646 verifyIndependentOfContext("*b = a;"); 1647 verifyIndependentOfContext("a * ~b;"); 1648 verifyIndependentOfContext("a * !b;"); 1649 verifyIndependentOfContext("a * +b;"); 1650 verifyIndependentOfContext("a * -b;"); 1651 verifyIndependentOfContext("a * ++b;"); 1652 verifyIndependentOfContext("a * --b;"); 1653 verifyIndependentOfContext("a[4] * b;"); 1654 verifyIndependentOfContext("f() * b;"); 1655 verifyIndependentOfContext("a * [self dostuff];"); 1656 verifyIndependentOfContext("a * (a + b);"); 1657 verifyIndependentOfContext("(a *)(a + b);"); 1658 verifyIndependentOfContext("int *pa = (int *)&a;"); 1659 verifyIndependentOfContext("return sizeof(int **);"); 1660 verifyIndependentOfContext("return sizeof(int ******);"); 1661 verifyIndependentOfContext("return (int **&)a;"); 1662 verifyGoogleFormat("return sizeof(int**);"); 1663 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 1664 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 1665 // FIXME: The newline is wrong. 1666 verifyFormat("auto a = [](int **&, int ***) {}\n;"); 1667 1668 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 1669 1670 verifyIndependentOfContext("A<int *> a;"); 1671 verifyIndependentOfContext("A<int **> a;"); 1672 verifyIndependentOfContext("A<int *, int *> a;"); 1673 verifyIndependentOfContext( 1674 "const char *const p = reinterpret_cast<const char *const>(q);"); 1675 verifyIndependentOfContext("A<int **, int **> a;"); 1676 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 1677 1678 verifyFormat( 1679 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1681 1682 verifyGoogleFormat("int main(int argc, char** argv) {}"); 1683 verifyGoogleFormat("A<int*> a;"); 1684 verifyGoogleFormat("A<int**> a;"); 1685 verifyGoogleFormat("A<int*, int*> a;"); 1686 verifyGoogleFormat("A<int**, int**> a;"); 1687 verifyGoogleFormat("f(b ? *c : *d);"); 1688 verifyGoogleFormat("int a = b ? *c : *d;"); 1689 verifyGoogleFormat("Type* t = **x;"); 1690 verifyGoogleFormat("Type* t = *++*x;"); 1691 verifyGoogleFormat("*++*x;"); 1692 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 1693 verifyGoogleFormat("Type* t = x++ * y;"); 1694 verifyGoogleFormat( 1695 "const char* const p = reinterpret_cast<const char* const>(q);"); 1696 1697 verifyIndependentOfContext("a = *(x + y);"); 1698 verifyIndependentOfContext("a = &(x + y);"); 1699 verifyIndependentOfContext("*(x + y).call();"); 1700 verifyIndependentOfContext("&(x + y)->call();"); 1701 verifyIndependentOfContext("&(*I).first"); 1702 1703 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 1704 verifyFormat( 1705 "int *MyValues = {\n" 1706 " *A, // Operator detection might be confused by the '{'\n" 1707 " *BB // Operator detection might be confused by previous comment\n" 1708 "};"); 1709 1710 verifyIndependentOfContext("if (int *a = &b)"); 1711 verifyIndependentOfContext("if (int &a = *b)"); 1712 verifyIndependentOfContext("if (a & b[i])"); 1713 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 1714 verifyIndependentOfContext("if (*b[i])"); 1715 verifyIndependentOfContext("if (int *a = (&b))"); 1716 verifyIndependentOfContext("while (int *a = &b)"); 1717 verifyFormat("void f() {\n" 1718 " for (const int &v : Values) {\n" 1719 " }\n" 1720 "}"); 1721 1722 verifyIndependentOfContext("A = new SomeType *[Length]();"); 1723 verifyGoogleFormat("A = new SomeType* [Length]();"); 1724 1725 EXPECT_EQ("int *a;\n" 1726 "int *a;\n" 1727 "int *a;", format("int *a;\n" 1728 "int* a;\n" 1729 "int *a;", getGoogleStyle())); 1730 EXPECT_EQ("int* a;\n" 1731 "int* a;\n" 1732 "int* a;", format("int* a;\n" 1733 "int* a;\n" 1734 "int *a;", getGoogleStyle())); 1735 EXPECT_EQ("int *a;\n" 1736 "int *a;\n" 1737 "int *a;", format("int *a;\n" 1738 "int * a;\n" 1739 "int * a;", getGoogleStyle())); 1740 } 1741 1742 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 1743 verifyFormat("void f() {\n" 1744 " x[aaaaaaaaa -\n" 1745 " b] = 23;\n" 1746 "}", getLLVMStyleWithColumns(15)); 1747 } 1748 1749 TEST_F(FormatTest, FormatsCasts) { 1750 verifyFormat("Type *A = static_cast<Type *>(P);"); 1751 verifyFormat("Type *A = (Type *)P;"); 1752 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 1753 verifyFormat("int a = (int)(2.0f);"); 1754 1755 // FIXME: These also need to be identified. 1756 verifyFormat("int a = (int) 2.0f;"); 1757 verifyFormat("int a = (int) * b;"); 1758 1759 // These are not casts. 1760 verifyFormat("void f(int *) {}"); 1761 verifyFormat("f(foo)->b;"); 1762 verifyFormat("f(foo).b;"); 1763 verifyFormat("f(foo)(b);"); 1764 verifyFormat("f(foo)[b];"); 1765 verifyFormat("[](foo) { return 4; }(bar)];"); 1766 verifyFormat("(*funptr)(foo)[4];"); 1767 verifyFormat("funptrs[4](foo)[4];"); 1768 verifyFormat("void f(int *);"); 1769 verifyFormat("void f(int *) = 0;"); 1770 verifyFormat("void f(SmallVector<int>) {}"); 1771 verifyFormat("void f(SmallVector<int>);"); 1772 verifyFormat("void f(SmallVector<int>) = 0;"); 1773 verifyFormat("void f(int i = (kValue) * kMask) {}"); 1774 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 1775 verifyFormat("int a = sizeof(int) * b;"); 1776 verifyFormat("int a = alignof(int) * b;"); 1777 } 1778 1779 TEST_F(FormatTest, FormatsFunctionTypes) { 1780 // FIXME: Determine the cases that need a space after the return type and fix. 1781 verifyFormat("A<bool()> a;"); 1782 verifyFormat("A<SomeType()> a;"); 1783 verifyFormat("A<void(*)(int, std::string)> a;"); 1784 1785 verifyFormat("int(*func)(void *);"); 1786 } 1787 1788 TEST_F(FormatTest, BreaksFunctionDeclarations) { 1789 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 1790 " int LoooooooooooooooooooongParam2) {\n}"); 1791 verifyFormat( 1792 "TypeSpecDecl *\n" 1793 "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" 1794 " IdentifierIn *II, Type *T) {\n}"); 1795 verifyGoogleFormat( 1796 "TypeSpecDecl* TypeSpecDecl::Create(\n" 1797 " ASTContext& C, DeclContext* DC, SourceLocation L) {\n}"); 1798 verifyGoogleFormat( 1799 "some_namespace::LongReturnType\n" 1800 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 1801 " int first_long_parameter, int second_parameter) {\n}"); 1802 1803 verifyGoogleFormat("template <typename T>\n" 1804 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 1805 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {\n}"); 1806 } 1807 1808 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 1809 verifyFormat("(a)->b();"); 1810 verifyFormat("--a;"); 1811 } 1812 1813 TEST_F(FormatTest, HandlesIncludeDirectives) { 1814 verifyFormat("#include <string>\n" 1815 "#include <a/b/c.h>\n" 1816 "#include \"a/b/string\"\n" 1817 "#include \"string.h\"\n" 1818 "#include \"string.h\"\n" 1819 "#include <a-a>\n" 1820 "#include < path with space >\n"); 1821 1822 verifyFormat("#import <string>"); 1823 verifyFormat("#import <a/b/c.h>"); 1824 verifyFormat("#import \"a/b/string\""); 1825 verifyFormat("#import \"string.h\""); 1826 verifyFormat("#import \"string.h\""); 1827 } 1828 1829 //===----------------------------------------------------------------------===// 1830 // Error recovery tests. 1831 //===----------------------------------------------------------------------===// 1832 1833 TEST_F(FormatTest, IncompleteParameterLists) { 1834 verifyGoogleFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 1835 " double *min_x,\n" 1836 " double *max_x,\n" 1837 " double *min_y,\n" 1838 " double *max_y,\n" 1839 " double *min_z,\n" 1840 " double *max_z, ) {\n" 1841 "}"); 1842 } 1843 1844 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 1845 verifyFormat("void f() { return; }\n42"); 1846 verifyFormat("void f() {\n" 1847 " if (0)\n" 1848 " return;\n" 1849 "}\n" 1850 "42"); 1851 verifyFormat("void f() { return }\n42"); 1852 verifyFormat("void f() {\n" 1853 " if (0)\n" 1854 " return\n" 1855 "}\n" 1856 "42"); 1857 } 1858 1859 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 1860 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 1861 EXPECT_EQ("void f() {\n" 1862 " if (a)\n" 1863 " return\n" 1864 "}", format("void f ( ) { if ( a ) return }")); 1865 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); 1866 EXPECT_EQ("namespace N {\n" 1867 "void f() {}\n" 1868 "void g()\n" 1869 "}", format("namespace N { void f( ) { } void g( ) }")); 1870 } 1871 1872 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 1873 verifyFormat("int aaaaaaaa =\n" 1874 " // Overly long comment\n" 1875 " b;", getLLVMStyleWithColumns(20)); 1876 verifyFormat("function(\n" 1877 " ShortArgument,\n" 1878 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); 1879 } 1880 1881 TEST_F(FormatTest, IncorrectAccessSpecifier) { 1882 verifyFormat("public:"); 1883 verifyFormat("class A {\n" 1884 "public\n" 1885 " void f() {}\n" 1886 "};"); 1887 verifyFormat("public\n" 1888 "int qwerty;"); 1889 verifyFormat("public\n" 1890 "B {}"); 1891 verifyFormat("public\n" 1892 "{}"); 1893 verifyFormat("public\n" 1894 "B { int x; }"); 1895 } 1896 1897 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 1898 verifyFormat("{"); 1899 } 1900 1901 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 1902 verifyFormat("do {\n}"); 1903 verifyFormat("do {\n}\n" 1904 "f();"); 1905 verifyFormat("do {\n}\n" 1906 "wheeee(fun);"); 1907 verifyFormat("do {\n" 1908 " f();\n" 1909 "}"); 1910 } 1911 1912 TEST_F(FormatTest, IncorrectCodeMissingParens) { 1913 verifyFormat("if {\n foo;\n foo();\n}"); 1914 verifyFormat("switch {\n foo;\n foo();\n}"); 1915 verifyFormat("for {\n foo;\n foo();\n}"); 1916 verifyFormat("while {\n foo;\n foo();\n}"); 1917 verifyFormat("do {\n foo;\n foo();\n} while;"); 1918 } 1919 1920 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 1921 verifyFormat("namespace {\n" 1922 "class Foo { Foo ( }; } // comment"); 1923 } 1924 1925 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 1926 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 1927 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 1928 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 1929 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 1930 1931 EXPECT_EQ("{\n" 1932 " {\n" 1933 " breakme(\n" 1934 " qwe);\n" 1935 "}\n", format("{\n" 1936 " {\n" 1937 " breakme(qwe);\n" 1938 "}\n", getLLVMStyleWithColumns(10))); 1939 } 1940 1941 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 1942 verifyFormat( 1943 "int x = {\n" 1944 " avariable,\n" 1945 " b(alongervariable)\n" 1946 "};", getLLVMStyleWithColumns(25)); 1947 } 1948 1949 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 1950 verifyFormat("return (a)(b) { 1, 2, 3 };"); 1951 } 1952 1953 TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 1954 verifyFormat( 1955 "Aaa({\n" 1956 " int i;\n" 1957 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1958 " ccccccccccccccccc));"); 1959 } 1960 1961 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 1962 verifyFormat("void f() { return 42; }"); 1963 verifyFormat("void f() {\n" 1964 " // Comment\n" 1965 "}"); 1966 verifyFormat("{\n" 1967 "#error {\n" 1968 " int a;\n" 1969 "}"); 1970 verifyFormat("{\n" 1971 " int a;\n" 1972 "#error {\n" 1973 "}"); 1974 1975 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 1976 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 1977 1978 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 1979 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 1980 } 1981 1982 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 1983 // Elaborate type variable declarations. 1984 verifyFormat("struct foo a = { bar };\nint n;"); 1985 verifyFormat("class foo a = { bar };\nint n;"); 1986 verifyFormat("union foo a = { bar };\nint n;"); 1987 1988 // Elaborate types inside function definitions. 1989 verifyFormat("struct foo f() {}\nint n;"); 1990 verifyFormat("class foo f() {}\nint n;"); 1991 verifyFormat("union foo f() {}\nint n;"); 1992 1993 // Templates. 1994 verifyFormat("template <class X> void f() {}\nint n;"); 1995 verifyFormat("template <struct X> void f() {}\nint n;"); 1996 verifyFormat("template <union X> void f() {}\nint n;"); 1997 1998 // Actual definitions... 1999 verifyFormat("struct {\n} n;"); 2000 verifyFormat( 2001 "template <template <class T, class Y>, class Z> class X {\n} n;"); 2002 verifyFormat("union Z {\n int n;\n} x;"); 2003 verifyFormat("class MACRO Z {\n} n;"); 2004 verifyFormat("class MACRO(X) Z {\n} n;"); 2005 verifyFormat("class __attribute__(X) Z {\n} n;"); 2006 verifyFormat("class __declspec(X) Z {\n} n;"); 2007 verifyFormat("class A##B##C {\n} n;"); 2008 2009 // Redefinition from nested context: 2010 verifyFormat("class A::B::C {\n} n;"); 2011 2012 // Template definitions. 2013 // FIXME: This is still incorrectly handled at the formatter side. 2014 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); 2015 2016 // FIXME: 2017 // This now gets parsed incorrectly as class definition. 2018 // verifyFormat("class A<int> f() {\n}\nint n;"); 2019 2020 // Elaborate types where incorrectly parsing the structural element would 2021 // break the indent. 2022 verifyFormat("if (true)\n" 2023 " class X x;\n" 2024 "else\n" 2025 " f();\n"); 2026 } 2027 2028 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 2029 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 2030 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 2031 EXPECT_EQ("#error 1", format(" # error 1")); 2032 EXPECT_EQ("#warning 1", format(" # warning 1")); 2033 } 2034 2035 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 2036 FormatStyle AllowsMergedIf = getGoogleStyle(); 2037 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 2038 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 2039 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 2040 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 2041 EXPECT_EQ("if (true) return 42;", 2042 format("if (true)\nreturn 42;", AllowsMergedIf)); 2043 FormatStyle ShortMergedIf = AllowsMergedIf; 2044 ShortMergedIf.ColumnLimit = 25; 2045 verifyFormat("#define A \\\n" 2046 " if (true) return 42;", ShortMergedIf); 2047 verifyFormat("#define A \\\n" 2048 " f(); \\\n" 2049 " if (true)\n" 2050 "#define B", ShortMergedIf); 2051 verifyFormat("#define A \\\n" 2052 " f(); \\\n" 2053 " if (true)\n" 2054 "g();", ShortMergedIf); 2055 verifyFormat("{\n" 2056 "#ifdef A\n" 2057 " // Comment\n" 2058 " if (true) continue;\n" 2059 "#endif\n" 2060 " // Comment\n" 2061 " if (true) continue;", ShortMergedIf); 2062 } 2063 2064 TEST_F(FormatTest, BlockCommentsInControlLoops) { 2065 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2066 " f();\n" 2067 "}"); 2068 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2069 " f();\n" 2070 "} /* another comment */ else /* comment #3 */ {\n" 2071 " g();\n" 2072 "}"); 2073 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2074 " f();\n" 2075 "}"); 2076 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2077 " f();\n" 2078 "}"); 2079 verifyFormat("do /* a comment in a strange place */ {\n" 2080 " f();\n" 2081 "} /* another comment */ while (0);"); 2082 } 2083 2084 TEST_F(FormatTest, BlockComments) { 2085 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2086 format("/* *//* */ /* */\n/* *//* */ /* */")); 2087 EXPECT_EQ("/* */ a /* */ b;", 2088 format(" /* */ a/* */ b;")); 2089 EXPECT_EQ("#define A /* */\\\n" 2090 " b\n" 2091 "/* */\n" 2092 "someCall(\n" 2093 " parameter);", 2094 format("#define A /* */ b\n" 2095 "/* */\n" 2096 "someCall(parameter);", getLLVMStyleWithColumns(15))); 2097 2098 EXPECT_EQ("#define A\n" 2099 "/* */ someCall(\n" 2100 " parameter);", 2101 format("#define A\n" 2102 "/* */someCall(parameter);", getLLVMStyleWithColumns(15))); 2103 2104 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2105 " 2, /* comment 2 */\n" 2106 " 3, /* comment 3 */\n" 2107 " aaaa,\n" 2108 " bbbb);", 2109 format("someFunction (1, /* comment 1 */\n" 2110 " 2, /* comment 2 */ \n" 2111 " 3, /* comment 3 */\n" 2112 "aaaa, bbbb );", getGoogleStyle())); 2113 verifyFormat( 2114 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2116 EXPECT_EQ( 2117 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2118 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2120 format( 2121 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2122 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2124 EXPECT_EQ( 2125 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2126 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2127 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2128 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2129 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2130 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2131 } 2132 2133 TEST_F(FormatTest, BlockCommentsInMacros) { 2134 EXPECT_EQ("#define A \\\n" 2135 " { \\\n" 2136 " /* one line */ \\\n" 2137 " someCall();", 2138 format("#define A { \\\n" 2139 " /* one line */ \\\n" 2140 " someCall();", getLLVMStyleWithColumns(20))); 2141 EXPECT_EQ("#define A \\\n" 2142 " { \\\n" 2143 " /* previous */ \\\n" 2144 " /* one line */ \\\n" 2145 " someCall();", 2146 format("#define A { \\\n" 2147 " /* previous */ \\\n" 2148 " /* one line */ \\\n" 2149 " someCall();", getLLVMStyleWithColumns(20))); 2150 } 2151 2152 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2153 // FIXME: This is not what we want... 2154 verifyFormat("{\n" 2155 "// a" 2156 "// b"); 2157 } 2158 2159 TEST_F(FormatTest, FormatStarDependingOnContext) { 2160 verifyFormat("void f(int *a);"); 2161 verifyFormat("void f() { f(fint * b); }"); 2162 verifyFormat("class A {\n void f(int *a);\n};"); 2163 verifyFormat("class A {\n int *a;\n};"); 2164 verifyFormat("namespace a {\n" 2165 "namespace b {\n" 2166 "class A {\n" 2167 " void f() {}\n" 2168 " int *a;\n" 2169 "};\n" 2170 "}\n" 2171 "}"); 2172 } 2173 2174 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 2175 verifyFormat("while"); 2176 verifyFormat("operator"); 2177 } 2178 2179 //===----------------------------------------------------------------------===// 2180 // Objective-C tests. 2181 //===----------------------------------------------------------------------===// 2182 2183 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 2184 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 2185 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 2186 format("-(NSUInteger)indexOfObject:(id)anObject;")); 2187 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 2188 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 2189 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 2190 format("-(NSInteger)Method3:(id)anObject;")); 2191 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 2192 format("-(NSInteger)Method4:(id)anObject;")); 2193 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 2194 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 2195 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 2196 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 2197 EXPECT_EQ( 2198 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 2199 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 2200 2201 // Very long objectiveC method declaration. 2202 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 2203 " inRange:(NSRange)range\n" 2204 " outRange:(NSRange)out_range\n" 2205 " outRange1:(NSRange)out_range1\n" 2206 " outRange2:(NSRange)out_range2\n" 2207 " outRange3:(NSRange)out_range3\n" 2208 " outRange4:(NSRange)out_range4\n" 2209 " outRange5:(NSRange)out_range5\n" 2210 " outRange6:(NSRange)out_range6\n" 2211 " outRange7:(NSRange)out_range7\n" 2212 " outRange8:(NSRange)out_range8\n" 2213 " outRange9:(NSRange)out_range9;"); 2214 2215 verifyFormat("- (int)sum:(vector<int>)numbers;"); 2216 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 2217 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 2218 // protocol lists (but not for template classes): 2219 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 2220 2221 verifyFormat("- (int(*)())foo:(int(*)())f;"); 2222 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); 2223 2224 // If there's no return type (very rare in practice!), LLVM and Google style 2225 // agree. 2226 verifyFormat("- foo:(int)f;"); 2227 verifyGoogleFormat("- foo:(int)foo;"); 2228 } 2229 2230 TEST_F(FormatTest, FormatObjCBlocks) { 2231 verifyFormat("int (^Block)(int, int);"); 2232 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 2233 } 2234 2235 TEST_F(FormatTest, FormatObjCInterface) { 2236 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 2237 "@public\n" 2238 " int field1;\n" 2239 "@protected\n" 2240 " int field2;\n" 2241 "@private\n" 2242 " int field3;\n" 2243 "@package\n" 2244 " int field4;\n" 2245 "}\n" 2246 "+ (id)init;\n" 2247 "@end"); 2248 2249 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 2250 " @public\n" 2251 " int field1;\n" 2252 " @protected\n" 2253 " int field2;\n" 2254 " @private\n" 2255 " int field3;\n" 2256 " @package\n" 2257 " int field4;\n" 2258 "}\n" 2259 "+ (id)init;\n" 2260 "@end"); 2261 2262 verifyFormat("@interface /* wait for it */ Foo\n" 2263 "+ (id)init;\n" 2264 "// Look, a comment!\n" 2265 "- (int)answerWith:(int)i;\n" 2266 "@end"); 2267 2268 verifyFormat("@interface Foo\n" 2269 "@end\n" 2270 "@interface Bar\n" 2271 "@end"); 2272 2273 verifyFormat("@interface Foo : Bar\n" 2274 "+ (id)init;\n" 2275 "@end"); 2276 2277 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 2278 "+ (id)init;\n" 2279 "@end"); 2280 2281 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 2282 "+ (id)init;\n" 2283 "@end"); 2284 2285 verifyFormat("@interface Foo (HackStuff)\n" 2286 "+ (id)init;\n" 2287 "@end"); 2288 2289 verifyFormat("@interface Foo ()\n" 2290 "+ (id)init;\n" 2291 "@end"); 2292 2293 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 2294 "+ (id)init;\n" 2295 "@end"); 2296 2297 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 2298 "+ (id)init;\n" 2299 "@end"); 2300 2301 verifyFormat("@interface Foo {\n" 2302 " int _i;\n" 2303 "}\n" 2304 "+ (id)init;\n" 2305 "@end"); 2306 2307 verifyFormat("@interface Foo : Bar {\n" 2308 " int _i;\n" 2309 "}\n" 2310 "+ (id)init;\n" 2311 "@end"); 2312 2313 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 2314 " int _i;\n" 2315 "}\n" 2316 "+ (id)init;\n" 2317 "@end"); 2318 2319 verifyFormat("@interface Foo (HackStuff) {\n" 2320 " int _i;\n" 2321 "}\n" 2322 "+ (id)init;\n" 2323 "@end"); 2324 2325 verifyFormat("@interface Foo () {\n" 2326 " int _i;\n" 2327 "}\n" 2328 "+ (id)init;\n" 2329 "@end"); 2330 2331 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 2332 " int _i;\n" 2333 "}\n" 2334 "+ (id)init;\n" 2335 "@end"); 2336 } 2337 2338 TEST_F(FormatTest, FormatObjCImplementation) { 2339 verifyFormat("@implementation Foo : NSObject {\n" 2340 "@public\n" 2341 " int field1;\n" 2342 "@protected\n" 2343 " int field2;\n" 2344 "@private\n" 2345 " int field3;\n" 2346 "@package\n" 2347 " int field4;\n" 2348 "}\n" 2349 "+ (id)init {\n}\n" 2350 "@end"); 2351 2352 verifyGoogleFormat("@implementation Foo : NSObject {\n" 2353 " @public\n" 2354 " int field1;\n" 2355 " @protected\n" 2356 " int field2;\n" 2357 " @private\n" 2358 " int field3;\n" 2359 " @package\n" 2360 " int field4;\n" 2361 "}\n" 2362 "+ (id)init {\n}\n" 2363 "@end"); 2364 2365 verifyFormat("@implementation Foo\n" 2366 "+ (id)init {\n" 2367 " if (true)\n" 2368 " return nil;\n" 2369 "}\n" 2370 "// Look, a comment!\n" 2371 "- (int)answerWith:(int)i {\n" 2372 " return i;\n" 2373 "}\n" 2374 "+ (int)answerWith:(int)i {\n" 2375 " return i;\n" 2376 "}\n" 2377 "@end"); 2378 2379 verifyFormat("@implementation Foo\n" 2380 "@end\n" 2381 "@implementation Bar\n" 2382 "@end"); 2383 2384 verifyFormat("@implementation Foo : Bar\n" 2385 "+ (id)init {\n}\n" 2386 "- (void)foo {\n}\n" 2387 "@end"); 2388 2389 verifyFormat("@implementation Foo {\n" 2390 " int _i;\n" 2391 "}\n" 2392 "+ (id)init {\n}\n" 2393 "@end"); 2394 2395 verifyFormat("@implementation Foo : Bar {\n" 2396 " int _i;\n" 2397 "}\n" 2398 "+ (id)init {\n}\n" 2399 "@end"); 2400 2401 verifyFormat("@implementation Foo (HackStuff)\n" 2402 "+ (id)init {\n}\n" 2403 "@end"); 2404 } 2405 2406 TEST_F(FormatTest, FormatObjCProtocol) { 2407 verifyFormat("@protocol Foo\n" 2408 "@property(weak) id delegate;\n" 2409 "- (NSUInteger)numberOfThings;\n" 2410 "@end"); 2411 2412 verifyFormat("@protocol MyProtocol <NSObject>\n" 2413 "- (NSUInteger)numberOfThings;\n" 2414 "@end"); 2415 2416 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 2417 "- (NSUInteger)numberOfThings;\n" 2418 "@end"); 2419 2420 verifyFormat("@protocol Foo;\n" 2421 "@protocol Bar;\n"); 2422 2423 verifyFormat("@protocol Foo\n" 2424 "@end\n" 2425 "@protocol Bar\n" 2426 "@end"); 2427 2428 verifyFormat("@protocol myProtocol\n" 2429 "- (void)mandatoryWithInt:(int)i;\n" 2430 "@optional\n" 2431 "- (void)optional;\n" 2432 "@required\n" 2433 "- (void)required;\n" 2434 "@optional\n" 2435 "@property(assign) int madProp;\n" 2436 "@end\n"); 2437 } 2438 2439 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 2440 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 2441 " rect:(NSRect)theRect\n" 2442 " interval:(float)theInterval {\n" 2443 "}"); 2444 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 2445 " longKeyword:(NSRect)theRect\n" 2446 " evenLongerKeyword:(float)theInterval\n" 2447 " error:(NSError **)theError {\n" 2448 "}"); 2449 } 2450 2451 TEST_F(FormatTest, FormatObjCMethodExpr) { 2452 verifyFormat("[foo bar:baz];"); 2453 verifyFormat("return [foo bar:baz];"); 2454 verifyFormat("f([foo bar:baz]);"); 2455 verifyFormat("f(2, [foo bar:baz]);"); 2456 verifyFormat("f(2, a ? b : c);"); 2457 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 2458 2459 // Unary operators. 2460 verifyFormat("int a = +[foo bar:baz];"); 2461 verifyFormat("int a = -[foo bar:baz];"); 2462 verifyFormat("int a = ![foo bar:baz];"); 2463 verifyFormat("int a = ~[foo bar:baz];"); 2464 verifyFormat("int a = ++[foo bar:baz];"); 2465 verifyFormat("int a = --[foo bar:baz];"); 2466 verifyFormat("int a = sizeof [foo bar:baz];"); 2467 verifyFormat("int a = alignof [foo bar:baz];"); 2468 verifyFormat("int a = &[foo bar:baz];"); 2469 verifyFormat("int a = *[foo bar:baz];"); 2470 // FIXME: Make casts work, without breaking f()[4]. 2471 //verifyFormat("int a = (int)[foo bar:baz];"); 2472 //verifyFormat("return (int)[foo bar:baz];"); 2473 //verifyFormat("(void)[foo bar:baz];"); 2474 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 2475 2476 // Binary operators. 2477 verifyFormat("[foo bar:baz], [foo bar:baz];"); 2478 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 2479 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 2480 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 2481 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 2482 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 2483 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 2484 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 2485 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 2486 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 2487 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 2488 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 2489 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 2490 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 2491 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 2492 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 2493 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 2494 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 2495 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 2496 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 2497 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 2498 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 2499 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 2500 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 2501 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 2502 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 2503 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 2504 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 2505 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 2506 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 2507 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 2508 // Whew! 2509 2510 verifyFormat("return in[42];"); 2511 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 2512 "}"); 2513 2514 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 2515 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 2516 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 2517 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 2518 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 2519 verifyFormat("[button setAction:@selector(zoomOut:)];"); 2520 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 2521 2522 verifyFormat("arr[[self indexForFoo:a]];"); 2523 verifyFormat("throw [self errorFor:a];"); 2524 verifyFormat("@throw [self errorFor:a];"); 2525 2526 // This tests that the formatter doesn't break after "backing" but before ":", 2527 // which would be at 80 columns. 2528 verifyFormat( 2529 "void f() {\n" 2530 " if ((self = [super initWithContentRect:contentRect\n" 2531 " styleMask:styleMask\n" 2532 " backing:NSBackingStoreBuffered\n" 2533 " defer:YES]))"); 2534 2535 verifyFormat( 2536 "[foo checkThatBreakingAfterColonWorksOk:\n" 2537 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 2538 2539 verifyFormat("[myObj short:arg1 // Force line break\n" 2540 " longKeyword:arg2\n" 2541 " evenLongerKeyword:arg3\n" 2542 " error:arg4];"); 2543 verifyFormat( 2544 "void f() {\n" 2545 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 2546 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 2547 " pos.width(), pos.height())\n" 2548 " styleMask:NSBorderlessWindowMask\n" 2549 " backing:NSBackingStoreBuffered\n" 2550 " defer:NO]);\n" 2551 "}"); 2552 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 2553 " with:contentsNativeView];"); 2554 2555 verifyFormat( 2556 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 2557 " owner:nillllll];"); 2558 2559 verifyFormat( 2560 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 2561 " forType:kBookmarkButtonDragType];"); 2562 2563 verifyFormat("[defaultCenter addObserver:self\n" 2564 " selector:@selector(willEnterFullscreen)\n" 2565 " name:kWillEnterFullscreenNotification\n" 2566 " object:nil];"); 2567 verifyFormat("[image_rep drawInRect:drawRect\n" 2568 " fromRect:NSZeroRect\n" 2569 " operation:NSCompositeCopy\n" 2570 " fraction:1.0\n" 2571 " respectFlipped:NO\n" 2572 " hints:nil];"); 2573 2574 verifyFormat( 2575 "scoped_nsobject<NSTextField> message(\n" 2576 " // The frame will be fixed up when |-setMessageText:| is called.\n" 2577 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 2578 } 2579 2580 TEST_F(FormatTest, ObjCAt) { 2581 verifyFormat("@autoreleasepool"); 2582 verifyFormat("@catch"); 2583 verifyFormat("@class"); 2584 verifyFormat("@compatibility_alias"); 2585 verifyFormat("@defs"); 2586 verifyFormat("@dynamic"); 2587 verifyFormat("@encode"); 2588 verifyFormat("@end"); 2589 verifyFormat("@finally"); 2590 verifyFormat("@implementation"); 2591 verifyFormat("@import"); 2592 verifyFormat("@interface"); 2593 verifyFormat("@optional"); 2594 verifyFormat("@package"); 2595 verifyFormat("@private"); 2596 verifyFormat("@property"); 2597 verifyFormat("@protected"); 2598 verifyFormat("@protocol"); 2599 verifyFormat("@public"); 2600 verifyFormat("@required"); 2601 verifyFormat("@selector"); 2602 verifyFormat("@synchronized"); 2603 verifyFormat("@synthesize"); 2604 verifyFormat("@throw"); 2605 verifyFormat("@try"); 2606 2607 EXPECT_EQ("@interface", format("@ interface")); 2608 2609 // The precise formatting of this doesn't matter, nobody writes code like 2610 // this. 2611 verifyFormat("@ /*foo*/ interface"); 2612 } 2613 2614 TEST_F(FormatTest, ObjCSnippets) { 2615 verifyFormat("@autoreleasepool {\n" 2616 " foo();\n" 2617 "}"); 2618 verifyFormat("@class Foo, Bar;"); 2619 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 2620 verifyFormat("@dynamic textColor;"); 2621 verifyFormat("char *buf1 = @encode(int *);"); 2622 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 2623 verifyFormat("char *buf1 = @encode(int **);"); 2624 verifyFormat("Protocol *proto = @protocol(p1);"); 2625 verifyFormat("SEL s = @selector(foo:);"); 2626 verifyFormat("@synchronized(self) {\n" 2627 " f();\n" 2628 "}"); 2629 2630 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2631 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2632 2633 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 2634 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 2635 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 2636 } 2637 2638 TEST_F(FormatTest, ObjCLiterals) { 2639 verifyFormat("@\"String\""); 2640 verifyFormat("@1"); 2641 verifyFormat("@+4.8"); 2642 verifyFormat("@-4"); 2643 verifyFormat("@1LL"); 2644 verifyFormat("@.5"); 2645 verifyFormat("@'c'"); 2646 verifyFormat("@true"); 2647 2648 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 2649 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 2650 verifyFormat("NSNumber *favoriteColor = @(Green);"); 2651 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 2652 2653 verifyFormat("@["); 2654 verifyFormat("@[]"); 2655 verifyFormat( 2656 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 2657 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 2658 2659 verifyFormat("@{"); 2660 verifyFormat("@{}"); 2661 verifyFormat("@{ @\"one\" : @1 }"); 2662 verifyFormat("return @{ @\"one\" : @1 };"); 2663 verifyFormat("@{ @\"one\" : @1, }"); 2664 verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); 2665 verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); 2666 verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); 2667 verifyFormat("[self setDict:@{}"); 2668 verifyFormat("[self setDict:@{ @1 : @2 }"); 2669 verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); 2670 verifyFormat( 2671 "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); 2672 verifyFormat( 2673 "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); 2674 2675 // FIXME: Nested and multi-line array and dictionary literals need more work. 2676 verifyFormat( 2677 "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" 2678 " @\"processInfo\" : [NSProcessInfo processInfo] };"); 2679 } 2680 2681 TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 2682 EXPECT_EQ("{\n" 2683 "{\n" 2684 "a;\n" 2685 "b;\n" 2686 "}\n" 2687 "}", format("{\n" 2688 "{\n" 2689 "a;\n" 2690 " b;\n" 2691 "}\n" 2692 "}", 13, 2, getLLVMStyle())); 2693 EXPECT_EQ("{\n" 2694 "{\n" 2695 " a;\n" 2696 "b;\n" 2697 "}\n" 2698 "}", format("{\n" 2699 "{\n" 2700 " a;\n" 2701 "b;\n" 2702 "}\n" 2703 "}", 9, 2, getLLVMStyle())); 2704 EXPECT_EQ("{\n" 2705 "{\n" 2706 "public:\n" 2707 " b;\n" 2708 "}\n" 2709 "}", format("{\n" 2710 "{\n" 2711 "public:\n" 2712 " b;\n" 2713 "}\n" 2714 "}", 17, 2, getLLVMStyle())); 2715 EXPECT_EQ("{\n" 2716 "{\n" 2717 "a;\n" 2718 "}\n" 2719 "{\n" 2720 " b;\n" 2721 "}\n" 2722 "}", format("{\n" 2723 "{\n" 2724 "a;\n" 2725 "}\n" 2726 "{\n" 2727 " b;\n" 2728 "}\n" 2729 "}", 22, 2, getLLVMStyle())); 2730 EXPECT_EQ(" {\n" 2731 " a;\n" 2732 " }", format(" {\n" 2733 "a;\n" 2734 " }", 4, 2, getLLVMStyle())); 2735 EXPECT_EQ("void f() {}\n" 2736 "void g() {}", format("void f() {}\n" 2737 "void g() {}", 13, 0, getLLVMStyle())); 2738 } 2739 2740 } // end namespace tooling 2741 } // end namespace clang 2742