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