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