FormatTest.cpp revision 515f65df40624a767bc8763a0b6b678146b8e3c9
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 verifyFormat( 1259 "aaaaaaa->aaaaaaa\n" 1260 " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1261 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 1262} 1263 1264TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 1265 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1266 " GUARDED_BY(aaaaaaaaaaaaa);"); 1267 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1268 " GUARDED_BY(aaaaaaaaaaaaa);"); 1269 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1270 " GUARDED_BY(aaaaaaaaaaaaa) {\n}"); 1271} 1272 1273TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 1274 verifyFormat( 1275 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1276 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 1277 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1278 " ccccccccccccccccccccccccc) {\n}"); 1279 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1280 " ccccccccccccccccccccccccc) {\n}"); 1281 verifyFormat( 1282 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 1283 " ccccccccccccccccccccccccc) {\n}"); 1284 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 1285 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 1286 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 1287 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 1288 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 1289 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 1290 " aaaaaaaaaaaaaaa != aa) {\n}"); 1291} 1292 1293TEST_F(FormatTest, BreaksAfterAssignments) { 1294 verifyFormat( 1295 "unsigned Cost =\n" 1296 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 1297 " SI->getPointerAddressSpaceee());\n"); 1298 verifyFormat( 1299 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 1300 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 1301 1302 verifyFormat( 1303 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 1304 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 1305} 1306 1307TEST_F(FormatTest, AlignsAfterAssignments) { 1308 verifyFormat( 1309 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1310 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1311 verifyFormat( 1312 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1313 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1314 verifyFormat( 1315 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1316 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1317 verifyFormat( 1318 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1319 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1320 verifyFormat("double LooooooooooooooooooooooooongResult =\n" 1321 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 1322 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 1323} 1324 1325TEST_F(FormatTest, AlignsAfterReturn) { 1326 verifyFormat( 1327 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1328 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1329 verifyFormat( 1330 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1331 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1332} 1333 1334TEST_F(FormatTest, BreaksConditionalExpressions) { 1335 verifyFormat( 1336 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 1337 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1338 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1339 verifyFormat( 1340 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1341 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1342 verifyFormat( 1343 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 1344 " : aaaaaaaaaaaaa);"); 1345 verifyFormat( 1346 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1347 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n" 1348 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1349 " aaaaaaaaaaaaa);"); 1350 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1351 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1353 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1355 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1357 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1359 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1361 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1362 1363 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1364 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1365 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1366 1367 // FIXME: The trailing third parameter here is kind of hidden. Prefer putting 1368 // it on the next line. 1369 verifyFormat( 1370 "unsigned Indent =\n" 1371 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 1372 " ? IndentForLevel[TheLine.Level]\n" 1373 " : TheLine * 2, TheLine.InPPDirective,\n" 1374 " PreviousEndOfLineColumn);", 1375 getLLVMStyleWithColumns(70)); 1376 1377} 1378 1379TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 1380 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 1381 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 1382 verifyFormat("bool a = true, b = false;"); 1383 1384 // FIXME: Indentation looks weird. 1385 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 1387 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 1388 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 1389 1390 // FIXME: This is bad as we hide "d". 1391 verifyFormat( 1392 "bool aaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 1393 " cccccccccccccccccccccccccccc, d = e && f;"); 1394 1395} 1396 1397TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 1398 verifyFormat("arr[foo ? bar : baz];"); 1399 verifyFormat("f()[foo ? bar : baz];"); 1400 verifyFormat("(a + b)[foo ? bar : baz];"); 1401 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 1402} 1403 1404TEST_F(FormatTest, AlignsStringLiterals) { 1405 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 1406 " \"short literal\");"); 1407 verifyFormat( 1408 "looooooooooooooooooooooooongFunction(\n" 1409 " \"short literal\"\n" 1410 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 1411 verifyFormat("someFunction(\"Always break between multi-line\"\n" 1412 " \" string literals\",\n" 1413 " and, other, parameters);"); 1414} 1415 1416TEST_F(FormatTest, AlignsPipes) { 1417 verifyFormat( 1418 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1419 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1420 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1421 verifyFormat( 1422 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 1423 " << aaaaaaaaaaaaaaaaaaaa;"); 1424 verifyFormat( 1425 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1426 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1427 verifyFormat( 1428 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1429 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 1430 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 1431 verifyFormat( 1432 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1433 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1434 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1435 1436 verifyFormat("return out << \"somepacket = {\\n\"\n" 1437 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 1438 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 1439 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 1440 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 1441 " << \"}\";"); 1442 1443 verifyFormat( 1444 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 1445 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 1446 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 1447 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 1448 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 1449} 1450 1451TEST_F(FormatTest, UnderstandsEquals) { 1452 verifyFormat( 1453 "aaaaaaaaaaaaaaaaa =\n" 1454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1455 verifyFormat( 1456 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1458 verifyFormat( 1459 "if (a) {\n" 1460 " f();\n" 1461 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1463 "}"); 1464 1465 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1466 " 100000000 + 10000000) {\n}"); 1467} 1468 1469TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 1470 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1471 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 1472 1473 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1474 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 1475 1476 verifyFormat( 1477 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 1478 " Parameter2);"); 1479 1480 verifyFormat( 1481 "ShortObject->shortFunction(\n" 1482 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 1483 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 1484 1485 verifyFormat("loooooooooooooongFunction(\n" 1486 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 1487 1488 verifyFormat( 1489 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 1490 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 1491 1492 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 1493 " .WillRepeatedly(Return(SomeValue));"); 1494 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" 1495 " .insert(ccccccccccccccccccccccc);"); 1496 1497 verifyGoogleFormat( 1498 "aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 1499 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 1500 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 1501 " aaaaaaaaaaaaaaaaaaa,\n" 1502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1503 1504 // Here, it is not necessary to wrap at "." or "->". 1505 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 1506 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1507 verifyFormat( 1508 "aaaaaaaaaaa->aaaaaaaaa(\n" 1509 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1510 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 1511 1512 verifyFormat( 1513 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 1515} 1516 1517TEST_F(FormatTest, WrapsTemplateDeclarations) { 1518 verifyFormat("template <typename T>\n" 1519 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 1520 verifyFormat( 1521 "template <typename T>\n" 1522 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 1523 verifyFormat("template <typename T>\n" 1524 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 1525 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 1526 verifyFormat( 1527 "template <typename T>\n" 1528 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 1529 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 1530 verifyFormat( 1531 "template <typename T>\n" 1532 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 1533 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 1534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1535 verifyFormat("template <typename T>\n" 1536 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1537 " int aaaaaaaaaaaaaaaaa);"); 1538 verifyFormat( 1539 "template <typename T1, typename T2 = char, typename T3 = char,\n" 1540 " typename T4 = char>\n" 1541 "void f();"); 1542 verifyFormat( 1543 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 1544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1545 1546 verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 1547 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1548} 1549 1550TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 1551 verifyFormat( 1552 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1554 verifyFormat( 1555 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 1558 1559 // FIXME: Should we have an extra indent after the second break? 1560 verifyFormat( 1561 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1564 1565 // FIXME: Look into whether we should indent 4 from the start or 4 from 1566 // "bbbbb..." here instead of what we are doing now. 1567 verifyFormat( 1568 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 1569 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 1570 1571 // Breaking at nested name specifiers is generally not desirable. 1572 verifyFormat( 1573 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1574 " aaaaaaaaaaaaaaaaaaaaaaa);"); 1575 1576 verifyFormat( 1577 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1579 " aaaaaaaaaaaaaaaaaaaaa);", 1580 getLLVMStyleWithColumns(74)); 1581} 1582 1583TEST_F(FormatTest, UnderstandsTemplateParameters) { 1584 verifyFormat("A<int> a;"); 1585 verifyFormat("A<A<A<int> > > a;"); 1586 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 1587 verifyFormat("bool x = a < 1 || 2 > a;"); 1588 verifyFormat("bool x = 5 < f<int>();"); 1589 verifyFormat("bool x = f<int>() > 5;"); 1590 verifyFormat("bool x = 5 < a<int>::x;"); 1591 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 1592 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 1593 1594 verifyGoogleFormat("A<A<int>> a;"); 1595 verifyGoogleFormat("A<A<A<int>>> a;"); 1596 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 1597 verifyGoogleFormat("A<A<int> > a;"); 1598 verifyGoogleFormat("A<A<A<int> > > a;"); 1599 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 1600 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 1601 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 1602 1603 verifyFormat("test >> a >> b;"); 1604 verifyFormat("test << a >> b;"); 1605 1606 verifyFormat("f<int>();"); 1607 verifyFormat("template <typename T> void f() {}"); 1608} 1609 1610TEST_F(FormatTest, UnderstandsUnaryOperators) { 1611 verifyFormat("int a = -2;"); 1612 verifyFormat("f(-1, -2, -3);"); 1613 verifyFormat("a[-1] = 5;"); 1614 verifyFormat("int a = 5 + -2;"); 1615 verifyFormat("if (i == -1) {\n}"); 1616 verifyFormat("if (i != -1) {\n}"); 1617 verifyFormat("if (i > -1) {\n}"); 1618 verifyFormat("if (i < -1) {\n}"); 1619 verifyFormat("++(a->f());"); 1620 verifyFormat("--(a->f());"); 1621 verifyFormat("(a->f())++;"); 1622 verifyFormat("a[42]++;"); 1623 verifyFormat("if (!(a->f())) {\n}"); 1624 1625 verifyFormat("a-- > b;"); 1626 verifyFormat("b ? -a : c;"); 1627 verifyFormat("n * sizeof char16;"); 1628 verifyFormat("n * alignof char16;"); 1629 verifyFormat("sizeof(char);"); 1630 verifyFormat("alignof(char);"); 1631 1632 verifyFormat("return -1;"); 1633 verifyFormat("switch (a) {\n" 1634 "case -1:\n" 1635 " break;\n" 1636 "}"); 1637 1638 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 1639 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 1640 1641 verifyFormat("int a = /* confusing comment */ -1;"); 1642 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 1643 verifyFormat("int a = i /* confusing comment */++;"); 1644} 1645 1646TEST_F(FormatTest, UndestandsOverloadedOperators) { 1647 verifyFormat("bool operator<();"); 1648 verifyFormat("bool operator>();"); 1649 verifyFormat("bool operator=();"); 1650 verifyFormat("bool operator==();"); 1651 verifyFormat("bool operator!=();"); 1652 verifyFormat("int operator+();"); 1653 verifyFormat("int operator++();"); 1654 verifyFormat("bool operator();"); 1655 verifyFormat("bool operator()();"); 1656 verifyFormat("bool operator[]();"); 1657 verifyFormat("operator bool();"); 1658 verifyFormat("operator int();"); 1659 verifyFormat("operator void *();"); 1660 verifyFormat("operator SomeType<int>();"); 1661 verifyFormat("operator SomeType<int, int>();"); 1662 verifyFormat("operator SomeType<SomeType<int> >();"); 1663 verifyFormat("void *operator new(std::size_t size);"); 1664 verifyFormat("void *operator new[](std::size_t size);"); 1665 verifyFormat("void operator delete(void *ptr);"); 1666 verifyFormat("void operator delete[](void *ptr);"); 1667 1668 verifyFormat( 1669 "ostream &operator<<(ostream &OutputStream,\n" 1670 " SomeReallyLongType WithSomeReallyLongValue);"); 1671 1672 verifyGoogleFormat("operator void*();"); 1673 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 1674} 1675 1676TEST_F(FormatTest, UnderstandsNewAndDelete) { 1677 verifyFormat("A *a = new A;"); 1678 verifyFormat("A *a = new (placement) A;"); 1679 verifyFormat("delete a;"); 1680 verifyFormat("delete (A *)a;"); 1681} 1682 1683TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 1684 verifyFormat("int *f(int *a) {}"); 1685 verifyFormat("int main(int argc, char **argv) {}"); 1686 verifyFormat("Test::Test(int b) : a(b * b) {}"); 1687 verifyIndependentOfContext("f(a, *a);"); 1688 verifyIndependentOfContext("f(*a);"); 1689 verifyIndependentOfContext("int a = b * 10;"); 1690 verifyIndependentOfContext("int a = 10 * b;"); 1691 verifyIndependentOfContext("int a = b * c;"); 1692 verifyIndependentOfContext("int a += b * c;"); 1693 verifyIndependentOfContext("int a -= b * c;"); 1694 verifyIndependentOfContext("int a *= b * c;"); 1695 verifyIndependentOfContext("int a /= b * c;"); 1696 verifyIndependentOfContext("int a = *b;"); 1697 verifyIndependentOfContext("int a = *b * c;"); 1698 verifyIndependentOfContext("int a = b * *c;"); 1699 verifyIndependentOfContext("return 10 * b;"); 1700 verifyIndependentOfContext("return *b * *c;"); 1701 verifyIndependentOfContext("return a & ~b;"); 1702 verifyIndependentOfContext("f(b ? *c : *d);"); 1703 verifyIndependentOfContext("int a = b ? *c : *d;"); 1704 verifyIndependentOfContext("*b = a;"); 1705 verifyIndependentOfContext("a * ~b;"); 1706 verifyIndependentOfContext("a * !b;"); 1707 verifyIndependentOfContext("a * +b;"); 1708 verifyIndependentOfContext("a * -b;"); 1709 verifyIndependentOfContext("a * ++b;"); 1710 verifyIndependentOfContext("a * --b;"); 1711 verifyIndependentOfContext("a[4] * b;"); 1712 verifyIndependentOfContext("f() * b;"); 1713 verifyIndependentOfContext("a * [self dostuff];"); 1714 verifyIndependentOfContext("a * (a + b);"); 1715 verifyIndependentOfContext("(a *)(a + b);"); 1716 verifyIndependentOfContext("int *pa = (int *)&a;"); 1717 verifyIndependentOfContext("return sizeof(int **);"); 1718 verifyIndependentOfContext("return sizeof(int ******);"); 1719 verifyIndependentOfContext("return (int **&)a;"); 1720 verifyGoogleFormat("return sizeof(int**);"); 1721 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 1722 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 1723 // FIXME: The newline is wrong. 1724 verifyFormat("auto a = [](int **&, int ***) {}\n;"); 1725 1726 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 1727 1728 verifyIndependentOfContext("A<int *> a;"); 1729 verifyIndependentOfContext("A<int **> a;"); 1730 verifyIndependentOfContext("A<int *, int *> a;"); 1731 verifyIndependentOfContext( 1732 "const char *const p = reinterpret_cast<const char *const>(q);"); 1733 verifyIndependentOfContext("A<int **, int **> a;"); 1734 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 1735 1736 verifyFormat( 1737 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1739 1740 verifyGoogleFormat("int main(int argc, char** argv) {}"); 1741 verifyGoogleFormat("A<int*> a;"); 1742 verifyGoogleFormat("A<int**> a;"); 1743 verifyGoogleFormat("A<int*, int*> a;"); 1744 verifyGoogleFormat("A<int**, int**> a;"); 1745 verifyGoogleFormat("f(b ? *c : *d);"); 1746 verifyGoogleFormat("int a = b ? *c : *d;"); 1747 verifyGoogleFormat("Type* t = **x;"); 1748 verifyGoogleFormat("Type* t = *++*x;"); 1749 verifyGoogleFormat("*++*x;"); 1750 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 1751 verifyGoogleFormat("Type* t = x++ * y;"); 1752 verifyGoogleFormat( 1753 "const char* const p = reinterpret_cast<const char* const>(q);"); 1754 1755 verifyIndependentOfContext("a = *(x + y);"); 1756 verifyIndependentOfContext("a = &(x + y);"); 1757 verifyIndependentOfContext("*(x + y).call();"); 1758 verifyIndependentOfContext("&(x + y)->call();"); 1759 verifyIndependentOfContext("&(*I).first"); 1760 1761 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 1762 verifyFormat( 1763 "int *MyValues = {\n" 1764 " *A, // Operator detection might be confused by the '{'\n" 1765 " *BB // Operator detection might be confused by previous comment\n" 1766 "};"); 1767 1768 verifyIndependentOfContext("if (int *a = &b)"); 1769 verifyIndependentOfContext("if (int &a = *b)"); 1770 verifyIndependentOfContext("if (a & b[i])"); 1771 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 1772 verifyIndependentOfContext("if (*b[i])"); 1773 verifyIndependentOfContext("if (int *a = (&b))"); 1774 verifyIndependentOfContext("while (int *a = &b)"); 1775 verifyFormat("void f() {\n" 1776 " for (const int &v : Values) {\n" 1777 " }\n" 1778 "}"); 1779 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 1780 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 1781 1782 verifyIndependentOfContext("A = new SomeType *[Length]();"); 1783 verifyGoogleFormat("A = new SomeType* [Length]();"); 1784 1785 EXPECT_EQ("int *a;\n" 1786 "int *a;\n" 1787 "int *a;", 1788 format("int *a;\n" 1789 "int* a;\n" 1790 "int *a;", 1791 getGoogleStyle())); 1792 EXPECT_EQ("int* a;\n" 1793 "int* a;\n" 1794 "int* a;", 1795 format("int* a;\n" 1796 "int* a;\n" 1797 "int *a;", 1798 getGoogleStyle())); 1799 EXPECT_EQ("int *a;\n" 1800 "int *a;\n" 1801 "int *a;", 1802 format("int *a;\n" 1803 "int * a;\n" 1804 "int * a;", 1805 getGoogleStyle())); 1806} 1807 1808TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 1809 verifyFormat("void f() {\n" 1810 " x[aaaaaaaaa -\n" 1811 " b] = 23;\n" 1812 "}", 1813 getLLVMStyleWithColumns(15)); 1814} 1815 1816TEST_F(FormatTest, FormatsCasts) { 1817 verifyFormat("Type *A = static_cast<Type *>(P);"); 1818 verifyFormat("Type *A = (Type *)P;"); 1819 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 1820 verifyFormat("int a = (int)(2.0f);"); 1821 1822 // FIXME: These also need to be identified. 1823 verifyFormat("int a = (int) 2.0f;"); 1824 verifyFormat("int a = (int) * b;"); 1825 1826 // These are not casts. 1827 verifyFormat("void f(int *) {}"); 1828 verifyFormat("f(foo)->b;"); 1829 verifyFormat("f(foo).b;"); 1830 verifyFormat("f(foo)(b);"); 1831 verifyFormat("f(foo)[b];"); 1832 verifyFormat("[](foo) { return 4; }(bar)];"); 1833 verifyFormat("(*funptr)(foo)[4];"); 1834 verifyFormat("funptrs[4](foo)[4];"); 1835 verifyFormat("void f(int *);"); 1836 verifyFormat("void f(int *) = 0;"); 1837 verifyFormat("void f(SmallVector<int>) {}"); 1838 verifyFormat("void f(SmallVector<int>);"); 1839 verifyFormat("void f(SmallVector<int>) = 0;"); 1840 verifyFormat("void f(int i = (kValue) * kMask) {}"); 1841 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 1842 verifyFormat("int a = sizeof(int) * b;"); 1843 verifyFormat("int a = alignof(int) * b;"); 1844} 1845 1846TEST_F(FormatTest, FormatsFunctionTypes) { 1847 // FIXME: Determine the cases that need a space after the return type and fix. 1848 verifyFormat("A<bool()> a;"); 1849 verifyFormat("A<SomeType()> a;"); 1850 verifyFormat("A<void(*)(int, std::string)> a;"); 1851 1852 verifyFormat("int(*func)(void *);"); 1853} 1854 1855TEST_F(FormatTest, BreaksFunctionDeclarations) { 1856 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 1857 " int LoooooooooooooooooooongParam2) {\n}"); 1858 verifyFormat( 1859 "TypeSpecDecl *\n" 1860 "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" 1861 " IdentifierIn *II, Type *T) {\n}"); 1862 verifyGoogleFormat( 1863 "TypeSpecDecl* TypeSpecDecl::Create(\n" 1864 " ASTContext& C, DeclContext* DC, SourceLocation L) {\n}"); 1865 verifyGoogleFormat( 1866 "some_namespace::LongReturnType\n" 1867 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 1868 " int first_long_parameter, int second_parameter) {\n}"); 1869 1870 verifyGoogleFormat("template <typename T>\n" 1871 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 1872 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {\n}"); 1873} 1874 1875TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 1876 verifyFormat("(a)->b();"); 1877 verifyFormat("--a;"); 1878} 1879 1880TEST_F(FormatTest, HandlesIncludeDirectives) { 1881 verifyFormat("#include <string>\n" 1882 "#include <a/b/c.h>\n" 1883 "#include \"a/b/string\"\n" 1884 "#include \"string.h\"\n" 1885 "#include \"string.h\"\n" 1886 "#include <a-a>\n" 1887 "#include < path with space >\n"); 1888 1889 verifyFormat("#import <string>"); 1890 verifyFormat("#import <a/b/c.h>"); 1891 verifyFormat("#import \"a/b/string\""); 1892 verifyFormat("#import \"string.h\""); 1893 verifyFormat("#import \"string.h\""); 1894} 1895 1896//===----------------------------------------------------------------------===// 1897// Error recovery tests. 1898//===----------------------------------------------------------------------===// 1899 1900TEST_F(FormatTest, IncompleteParameterLists) { 1901 verifyGoogleFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 1902 " double *min_x,\n" 1903 " double *max_x,\n" 1904 " double *min_y,\n" 1905 " double *max_y,\n" 1906 " double *min_z,\n" 1907 " double *max_z, ) {\n" 1908 "}"); 1909} 1910 1911TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 1912 verifyFormat("void f() { return; }\n42"); 1913 verifyFormat("void f() {\n" 1914 " if (0)\n" 1915 " return;\n" 1916 "}\n" 1917 "42"); 1918 verifyFormat("void f() { return }\n42"); 1919 verifyFormat("void f() {\n" 1920 " if (0)\n" 1921 " return\n" 1922 "}\n" 1923 "42"); 1924} 1925 1926TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 1927 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 1928 EXPECT_EQ("void f() {\n" 1929 " if (a)\n" 1930 " return\n" 1931 "}", 1932 format("void f ( ) { if ( a ) return }")); 1933 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); 1934 EXPECT_EQ("namespace N {\n" 1935 "void f() {}\n" 1936 "void g()\n" 1937 "}", 1938 format("namespace N { void f( ) { } void g( ) }")); 1939} 1940 1941TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 1942 verifyFormat("int aaaaaaaa =\n" 1943 " // Overly long comment\n" 1944 " b;", 1945 getLLVMStyleWithColumns(20)); 1946 verifyFormat("function(\n" 1947 " ShortArgument,\n" 1948 " LoooooooooooongArgument);\n", 1949 getLLVMStyleWithColumns(20)); 1950} 1951 1952TEST_F(FormatTest, IncorrectAccessSpecifier) { 1953 verifyFormat("public:"); 1954 verifyFormat("class A {\n" 1955 "public\n" 1956 " void f() {}\n" 1957 "};"); 1958 verifyFormat("public\n" 1959 "int qwerty;"); 1960 verifyFormat("public\n" 1961 "B {}"); 1962 verifyFormat("public\n" 1963 "{}"); 1964 verifyFormat("public\n" 1965 "B { int x; }"); 1966} 1967 1968TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); } 1969 1970TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 1971 verifyFormat("do {\n}"); 1972 verifyFormat("do {\n}\n" 1973 "f();"); 1974 verifyFormat("do {\n}\n" 1975 "wheeee(fun);"); 1976 verifyFormat("do {\n" 1977 " f();\n" 1978 "}"); 1979} 1980 1981TEST_F(FormatTest, IncorrectCodeMissingParens) { 1982 verifyFormat("if {\n foo;\n foo();\n}"); 1983 verifyFormat("switch {\n foo;\n foo();\n}"); 1984 verifyFormat("for {\n foo;\n foo();\n}"); 1985 verifyFormat("while {\n foo;\n foo();\n}"); 1986 verifyFormat("do {\n foo;\n foo();\n} while;"); 1987} 1988 1989TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 1990 verifyFormat("namespace {\n" 1991 "class Foo { Foo ( }; } // comment"); 1992} 1993 1994TEST_F(FormatTest, IncorrectCodeErrorDetection) { 1995 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 1996 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 1997 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 1998 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 1999 2000 EXPECT_EQ("{\n" 2001 " {\n" 2002 " breakme(\n" 2003 " qwe);\n" 2004 "}\n", 2005 format("{\n" 2006 " {\n" 2007 " breakme(qwe);\n" 2008 "}\n", 2009 getLLVMStyleWithColumns(10))); 2010} 2011 2012TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 2013 verifyFormat("int x = {\n" 2014 " avariable,\n" 2015 " b(alongervariable)\n" 2016 "};", 2017 getLLVMStyleWithColumns(25)); 2018} 2019 2020TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 2021 verifyFormat("return (a)(b) { 1, 2, 3 };"); 2022} 2023 2024TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 2025 verifyFormat( 2026 "Aaa({\n" 2027 " int i;\n" 2028 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2029 " ccccccccccccccccc));"); 2030} 2031 2032TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 2033 verifyFormat("void f() { return 42; }"); 2034 verifyFormat("void f() {\n" 2035 " // Comment\n" 2036 "}"); 2037 verifyFormat("{\n" 2038 "#error {\n" 2039 " int a;\n" 2040 "}"); 2041 verifyFormat("{\n" 2042 " int a;\n" 2043 "#error {\n" 2044 "}"); 2045 2046 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 2047 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 2048 2049 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 2050 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 2051} 2052 2053TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 2054 // Elaborate type variable declarations. 2055 verifyFormat("struct foo a = { bar };\nint n;"); 2056 verifyFormat("class foo a = { bar };\nint n;"); 2057 verifyFormat("union foo a = { bar };\nint n;"); 2058 2059 // Elaborate types inside function definitions. 2060 verifyFormat("struct foo f() {}\nint n;"); 2061 verifyFormat("class foo f() {}\nint n;"); 2062 verifyFormat("union foo f() {}\nint n;"); 2063 2064 // Templates. 2065 verifyFormat("template <class X> void f() {}\nint n;"); 2066 verifyFormat("template <struct X> void f() {}\nint n;"); 2067 verifyFormat("template <union X> void f() {}\nint n;"); 2068 2069 // Actual definitions... 2070 verifyFormat("struct {\n} n;"); 2071 verifyFormat( 2072 "template <template <class T, class Y>, class Z> class X {\n} n;"); 2073 verifyFormat("union Z {\n int n;\n} x;"); 2074 verifyFormat("class MACRO Z {\n} n;"); 2075 verifyFormat("class MACRO(X) Z {\n} n;"); 2076 verifyFormat("class __attribute__(X) Z {\n} n;"); 2077 verifyFormat("class __declspec(X) Z {\n} n;"); 2078 verifyFormat("class A##B##C {\n} n;"); 2079 2080 // Redefinition from nested context: 2081 verifyFormat("class A::B::C {\n} n;"); 2082 2083 // Template definitions. 2084 // FIXME: This is still incorrectly handled at the formatter side. 2085 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); 2086 2087 // FIXME: 2088 // This now gets parsed incorrectly as class definition. 2089 // verifyFormat("class A<int> f() {\n}\nint n;"); 2090 2091 // Elaborate types where incorrectly parsing the structural element would 2092 // break the indent. 2093 verifyFormat("if (true)\n" 2094 " class X x;\n" 2095 "else\n" 2096 " f();\n"); 2097} 2098 2099TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 2100 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 2101 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 2102 EXPECT_EQ("#error 1", format(" # error 1")); 2103 EXPECT_EQ("#warning 1", format(" # warning 1")); 2104} 2105 2106TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 2107 FormatStyle AllowsMergedIf = getGoogleStyle(); 2108 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 2109 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 2110 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 2111 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 2112 EXPECT_EQ("if (true) return 42;", 2113 format("if (true)\nreturn 42;", AllowsMergedIf)); 2114 FormatStyle ShortMergedIf = AllowsMergedIf; 2115 ShortMergedIf.ColumnLimit = 25; 2116 verifyFormat("#define A \\\n" 2117 " if (true) return 42;", 2118 ShortMergedIf); 2119 verifyFormat("#define A \\\n" 2120 " f(); \\\n" 2121 " if (true)\n" 2122 "#define B", 2123 ShortMergedIf); 2124 verifyFormat("#define A \\\n" 2125 " f(); \\\n" 2126 " if (true)\n" 2127 "g();", 2128 ShortMergedIf); 2129 verifyFormat("{\n" 2130 "#ifdef A\n" 2131 " // Comment\n" 2132 " if (true) continue;\n" 2133 "#endif\n" 2134 " // Comment\n" 2135 " if (true) continue;", 2136 ShortMergedIf); 2137} 2138 2139TEST_F(FormatTest, BlockCommentsInControlLoops) { 2140 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2141 " f();\n" 2142 "}"); 2143 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2144 " f();\n" 2145 "} /* another comment */ else /* comment #3 */ {\n" 2146 " g();\n" 2147 "}"); 2148 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2149 " f();\n" 2150 "}"); 2151 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2152 " f();\n" 2153 "}"); 2154 verifyFormat("do /* a comment in a strange place */ {\n" 2155 " f();\n" 2156 "} /* another comment */ while (0);"); 2157} 2158 2159TEST_F(FormatTest, BlockComments) { 2160 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2161 format("/* *//* */ /* */\n/* *//* */ /* */")); 2162 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 2163 EXPECT_EQ("#define A /* */\\\n" 2164 " b\n" 2165 "/* */\n" 2166 "someCall(\n" 2167 " parameter);", 2168 format("#define A /* */ b\n" 2169 "/* */\n" 2170 "someCall(parameter);", 2171 getLLVMStyleWithColumns(15))); 2172 2173 EXPECT_EQ("#define A\n" 2174 "/* */ someCall(\n" 2175 " parameter);", 2176 format("#define A\n" 2177 "/* */someCall(parameter);", 2178 getLLVMStyleWithColumns(15))); 2179 2180 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2181 " 2, /* comment 2 */\n" 2182 " 3, /* comment 3 */\n" 2183 " aaaa,\n" 2184 " bbbb);", 2185 format("someFunction (1, /* comment 1 */\n" 2186 " 2, /* comment 2 */ \n" 2187 " 3, /* comment 3 */\n" 2188 "aaaa, bbbb );", 2189 getGoogleStyle())); 2190 verifyFormat( 2191 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2193 EXPECT_EQ( 2194 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2195 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2197 format( 2198 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2199 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2201 EXPECT_EQ( 2202 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2203 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2204 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2205 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2206 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2207 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2208} 2209 2210TEST_F(FormatTest, BlockCommentsInMacros) { 2211 EXPECT_EQ("#define A \\\n" 2212 " { \\\n" 2213 " /* one line */ \\\n" 2214 " someCall();", 2215 format("#define A { \\\n" 2216 " /* one line */ \\\n" 2217 " someCall();", 2218 getLLVMStyleWithColumns(20))); 2219 EXPECT_EQ("#define A \\\n" 2220 " { \\\n" 2221 " /* previous */ \\\n" 2222 " /* one line */ \\\n" 2223 " someCall();", 2224 format("#define A { \\\n" 2225 " /* previous */ \\\n" 2226 " /* one line */ \\\n" 2227 " someCall();", 2228 getLLVMStyleWithColumns(20))); 2229} 2230 2231TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2232 // FIXME: This is not what we want... 2233 verifyFormat("{\n" 2234 "// a" 2235 "// b"); 2236} 2237 2238TEST_F(FormatTest, FormatStarDependingOnContext) { 2239 verifyFormat("void f(int *a);"); 2240 verifyFormat("void f() { f(fint * b); }"); 2241 verifyFormat("class A {\n void f(int *a);\n};"); 2242 verifyFormat("class A {\n int *a;\n};"); 2243 verifyFormat("namespace a {\n" 2244 "namespace b {\n" 2245 "class A {\n" 2246 " void f() {}\n" 2247 " int *a;\n" 2248 "};\n" 2249 "}\n" 2250 "}"); 2251} 2252 2253TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 2254 verifyFormat("while"); 2255 verifyFormat("operator"); 2256} 2257 2258//===----------------------------------------------------------------------===// 2259// Objective-C tests. 2260//===----------------------------------------------------------------------===// 2261 2262TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 2263 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 2264 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 2265 format("-(NSUInteger)indexOfObject:(id)anObject;")); 2266 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 2267 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 2268 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 2269 format("-(NSInteger)Method3:(id)anObject;")); 2270 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 2271 format("-(NSInteger)Method4:(id)anObject;")); 2272 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 2273 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 2274 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 2275 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 2276 EXPECT_EQ( 2277 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 2278 format( 2279 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 2280 2281 // Very long objectiveC method declaration. 2282 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 2283 " inRange:(NSRange)range\n" 2284 " outRange:(NSRange)out_range\n" 2285 " outRange1:(NSRange)out_range1\n" 2286 " outRange2:(NSRange)out_range2\n" 2287 " outRange3:(NSRange)out_range3\n" 2288 " outRange4:(NSRange)out_range4\n" 2289 " outRange5:(NSRange)out_range5\n" 2290 " outRange6:(NSRange)out_range6\n" 2291 " outRange7:(NSRange)out_range7\n" 2292 " outRange8:(NSRange)out_range8\n" 2293 " outRange9:(NSRange)out_range9;"); 2294 2295 verifyFormat("- (int)sum:(vector<int>)numbers;"); 2296 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 2297 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 2298 // protocol lists (but not for template classes): 2299 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 2300 2301 verifyFormat("- (int(*)())foo:(int(*)())f;"); 2302 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); 2303 2304 // If there's no return type (very rare in practice!), LLVM and Google style 2305 // agree. 2306 verifyFormat("- foo:(int)f;"); 2307 verifyGoogleFormat("- foo:(int)foo;"); 2308} 2309 2310TEST_F(FormatTest, FormatObjCBlocks) { 2311 verifyFormat("int (^Block)(int, int);"); 2312 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 2313} 2314 2315TEST_F(FormatTest, FormatObjCInterface) { 2316 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 2317 "@public\n" 2318 " int field1;\n" 2319 "@protected\n" 2320 " int field2;\n" 2321 "@private\n" 2322 " int field3;\n" 2323 "@package\n" 2324 " int field4;\n" 2325 "}\n" 2326 "+ (id)init;\n" 2327 "@end"); 2328 2329 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 2330 " @public\n" 2331 " int field1;\n" 2332 " @protected\n" 2333 " int field2;\n" 2334 " @private\n" 2335 " int field3;\n" 2336 " @package\n" 2337 " int field4;\n" 2338 "}\n" 2339 "+ (id)init;\n" 2340 "@end"); 2341 2342 verifyFormat("@interface /* wait for it */ Foo\n" 2343 "+ (id)init;\n" 2344 "// Look, a comment!\n" 2345 "- (int)answerWith:(int)i;\n" 2346 "@end"); 2347 2348 verifyFormat("@interface Foo\n" 2349 "@end\n" 2350 "@interface Bar\n" 2351 "@end"); 2352 2353 verifyFormat("@interface Foo : Bar\n" 2354 "+ (id)init;\n" 2355 "@end"); 2356 2357 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 2358 "+ (id)init;\n" 2359 "@end"); 2360 2361 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 2362 "+ (id)init;\n" 2363 "@end"); 2364 2365 verifyFormat("@interface Foo (HackStuff)\n" 2366 "+ (id)init;\n" 2367 "@end"); 2368 2369 verifyFormat("@interface Foo ()\n" 2370 "+ (id)init;\n" 2371 "@end"); 2372 2373 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 2374 "+ (id)init;\n" 2375 "@end"); 2376 2377 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 2378 "+ (id)init;\n" 2379 "@end"); 2380 2381 verifyFormat("@interface Foo {\n" 2382 " int _i;\n" 2383 "}\n" 2384 "+ (id)init;\n" 2385 "@end"); 2386 2387 verifyFormat("@interface Foo : Bar {\n" 2388 " int _i;\n" 2389 "}\n" 2390 "+ (id)init;\n" 2391 "@end"); 2392 2393 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 2394 " int _i;\n" 2395 "}\n" 2396 "+ (id)init;\n" 2397 "@end"); 2398 2399 verifyFormat("@interface Foo (HackStuff) {\n" 2400 " int _i;\n" 2401 "}\n" 2402 "+ (id)init;\n" 2403 "@end"); 2404 2405 verifyFormat("@interface Foo () {\n" 2406 " int _i;\n" 2407 "}\n" 2408 "+ (id)init;\n" 2409 "@end"); 2410 2411 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 2412 " int _i;\n" 2413 "}\n" 2414 "+ (id)init;\n" 2415 "@end"); 2416} 2417 2418TEST_F(FormatTest, FormatObjCImplementation) { 2419 verifyFormat("@implementation Foo : NSObject {\n" 2420 "@public\n" 2421 " int field1;\n" 2422 "@protected\n" 2423 " int field2;\n" 2424 "@private\n" 2425 " int field3;\n" 2426 "@package\n" 2427 " int field4;\n" 2428 "}\n" 2429 "+ (id)init {\n}\n" 2430 "@end"); 2431 2432 verifyGoogleFormat("@implementation Foo : NSObject {\n" 2433 " @public\n" 2434 " int field1;\n" 2435 " @protected\n" 2436 " int field2;\n" 2437 " @private\n" 2438 " int field3;\n" 2439 " @package\n" 2440 " int field4;\n" 2441 "}\n" 2442 "+ (id)init {\n}\n" 2443 "@end"); 2444 2445 verifyFormat("@implementation Foo\n" 2446 "+ (id)init {\n" 2447 " if (true)\n" 2448 " return nil;\n" 2449 "}\n" 2450 "// Look, a comment!\n" 2451 "- (int)answerWith:(int)i {\n" 2452 " return i;\n" 2453 "}\n" 2454 "+ (int)answerWith:(int)i {\n" 2455 " return i;\n" 2456 "}\n" 2457 "@end"); 2458 2459 verifyFormat("@implementation Foo\n" 2460 "@end\n" 2461 "@implementation Bar\n" 2462 "@end"); 2463 2464 verifyFormat("@implementation Foo : Bar\n" 2465 "+ (id)init {\n}\n" 2466 "- (void)foo {\n}\n" 2467 "@end"); 2468 2469 verifyFormat("@implementation Foo {\n" 2470 " int _i;\n" 2471 "}\n" 2472 "+ (id)init {\n}\n" 2473 "@end"); 2474 2475 verifyFormat("@implementation Foo : Bar {\n" 2476 " int _i;\n" 2477 "}\n" 2478 "+ (id)init {\n}\n" 2479 "@end"); 2480 2481 verifyFormat("@implementation Foo (HackStuff)\n" 2482 "+ (id)init {\n}\n" 2483 "@end"); 2484} 2485 2486TEST_F(FormatTest, FormatObjCProtocol) { 2487 verifyFormat("@protocol Foo\n" 2488 "@property(weak) id delegate;\n" 2489 "- (NSUInteger)numberOfThings;\n" 2490 "@end"); 2491 2492 verifyFormat("@protocol MyProtocol <NSObject>\n" 2493 "- (NSUInteger)numberOfThings;\n" 2494 "@end"); 2495 2496 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 2497 "- (NSUInteger)numberOfThings;\n" 2498 "@end"); 2499 2500 verifyFormat("@protocol Foo;\n" 2501 "@protocol Bar;\n"); 2502 2503 verifyFormat("@protocol Foo\n" 2504 "@end\n" 2505 "@protocol Bar\n" 2506 "@end"); 2507 2508 verifyFormat("@protocol myProtocol\n" 2509 "- (void)mandatoryWithInt:(int)i;\n" 2510 "@optional\n" 2511 "- (void)optional;\n" 2512 "@required\n" 2513 "- (void)required;\n" 2514 "@optional\n" 2515 "@property(assign) int madProp;\n" 2516 "@end\n"); 2517} 2518 2519TEST_F(FormatTest, FormatObjCMethodDeclarations) { 2520 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 2521 " rect:(NSRect)theRect\n" 2522 " interval:(float)theInterval {\n" 2523 "}"); 2524 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 2525 " longKeyword:(NSRect)theRect\n" 2526 " evenLongerKeyword:(float)theInterval\n" 2527 " error:(NSError **)theError {\n" 2528 "}"); 2529} 2530 2531TEST_F(FormatTest, FormatObjCMethodExpr) { 2532 verifyFormat("[foo bar:baz];"); 2533 verifyFormat("return [foo bar:baz];"); 2534 verifyFormat("f([foo bar:baz]);"); 2535 verifyFormat("f(2, [foo bar:baz]);"); 2536 verifyFormat("f(2, a ? b : c);"); 2537 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 2538 2539 // Unary operators. 2540 verifyFormat("int a = +[foo bar:baz];"); 2541 verifyFormat("int a = -[foo bar:baz];"); 2542 verifyFormat("int a = ![foo bar:baz];"); 2543 verifyFormat("int a = ~[foo bar:baz];"); 2544 verifyFormat("int a = ++[foo bar:baz];"); 2545 verifyFormat("int a = --[foo bar:baz];"); 2546 verifyFormat("int a = sizeof [foo bar:baz];"); 2547 verifyFormat("int a = alignof [foo bar:baz];"); 2548 verifyFormat("int a = &[foo bar:baz];"); 2549 verifyFormat("int a = *[foo bar:baz];"); 2550 // FIXME: Make casts work, without breaking f()[4]. 2551 //verifyFormat("int a = (int)[foo bar:baz];"); 2552 //verifyFormat("return (int)[foo bar:baz];"); 2553 //verifyFormat("(void)[foo bar:baz];"); 2554 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 2555 2556 // Binary operators. 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];"); 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] : [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 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 2585 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 2586 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 2587 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 2588 // Whew! 2589 2590 verifyFormat("return in[42];"); 2591 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 2592 "}"); 2593 2594 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 2595 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 2596 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 2597 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 2598 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 2599 verifyFormat("[button setAction:@selector(zoomOut:)];"); 2600 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 2601 2602 verifyFormat("arr[[self indexForFoo:a]];"); 2603 verifyFormat("throw [self errorFor:a];"); 2604 verifyFormat("@throw [self errorFor:a];"); 2605 2606 // This tests that the formatter doesn't break after "backing" but before ":", 2607 // which would be at 80 columns. 2608 verifyFormat( 2609 "void f() {\n" 2610 " if ((self = [super initWithContentRect:contentRect\n" 2611 " styleMask:styleMask\n" 2612 " backing:NSBackingStoreBuffered\n" 2613 " defer:YES]))"); 2614 2615 verifyFormat( 2616 "[foo checkThatBreakingAfterColonWorksOk:\n" 2617 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 2618 2619 verifyFormat("[myObj short:arg1 // Force line break\n" 2620 " longKeyword:arg2\n" 2621 " evenLongerKeyword:arg3\n" 2622 " error:arg4];"); 2623 verifyFormat( 2624 "void f() {\n" 2625 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 2626 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 2627 " pos.width(), pos.height())\n" 2628 " styleMask:NSBorderlessWindowMask\n" 2629 " backing:NSBackingStoreBuffered\n" 2630 " defer:NO]);\n" 2631 "}"); 2632 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 2633 " with:contentsNativeView];"); 2634 2635 verifyFormat( 2636 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 2637 " owner:nillllll];"); 2638 2639 verifyFormat( 2640 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 2641 " forType:kBookmarkButtonDragType];"); 2642 2643 verifyFormat("[defaultCenter addObserver:self\n" 2644 " selector:@selector(willEnterFullscreen)\n" 2645 " name:kWillEnterFullscreenNotification\n" 2646 " object:nil];"); 2647 verifyFormat("[image_rep drawInRect:drawRect\n" 2648 " fromRect:NSZeroRect\n" 2649 " operation:NSCompositeCopy\n" 2650 " fraction:1.0\n" 2651 " respectFlipped:NO\n" 2652 " hints:nil];"); 2653 2654 verifyFormat( 2655 "scoped_nsobject<NSTextField> message(\n" 2656 " // The frame will be fixed up when |-setMessageText:| is called.\n" 2657 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 2658} 2659 2660TEST_F(FormatTest, ObjCAt) { 2661 verifyFormat("@autoreleasepool"); 2662 verifyFormat("@catch"); 2663 verifyFormat("@class"); 2664 verifyFormat("@compatibility_alias"); 2665 verifyFormat("@defs"); 2666 verifyFormat("@dynamic"); 2667 verifyFormat("@encode"); 2668 verifyFormat("@end"); 2669 verifyFormat("@finally"); 2670 verifyFormat("@implementation"); 2671 verifyFormat("@import"); 2672 verifyFormat("@interface"); 2673 verifyFormat("@optional"); 2674 verifyFormat("@package"); 2675 verifyFormat("@private"); 2676 verifyFormat("@property"); 2677 verifyFormat("@protected"); 2678 verifyFormat("@protocol"); 2679 verifyFormat("@public"); 2680 verifyFormat("@required"); 2681 verifyFormat("@selector"); 2682 verifyFormat("@synchronized"); 2683 verifyFormat("@synthesize"); 2684 verifyFormat("@throw"); 2685 verifyFormat("@try"); 2686 2687 EXPECT_EQ("@interface", format("@ interface")); 2688 2689 // The precise formatting of this doesn't matter, nobody writes code like 2690 // this. 2691 verifyFormat("@ /*foo*/ interface"); 2692} 2693 2694TEST_F(FormatTest, ObjCSnippets) { 2695 verifyFormat("@autoreleasepool {\n" 2696 " foo();\n" 2697 "}"); 2698 verifyFormat("@class Foo, Bar;"); 2699 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 2700 verifyFormat("@dynamic textColor;"); 2701 verifyFormat("char *buf1 = @encode(int *);"); 2702 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 2703 verifyFormat("char *buf1 = @encode(int **);"); 2704 verifyFormat("Protocol *proto = @protocol(p1);"); 2705 verifyFormat("SEL s = @selector(foo:);"); 2706 verifyFormat("@synchronized(self) {\n" 2707 " f();\n" 2708 "}"); 2709 2710 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2711 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2712 2713 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 2714 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 2715 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 2716} 2717 2718TEST_F(FormatTest, ObjCLiterals) { 2719 verifyFormat("@\"String\""); 2720 verifyFormat("@1"); 2721 verifyFormat("@+4.8"); 2722 verifyFormat("@-4"); 2723 verifyFormat("@1LL"); 2724 verifyFormat("@.5"); 2725 verifyFormat("@'c'"); 2726 verifyFormat("@true"); 2727 2728 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 2729 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 2730 verifyFormat("NSNumber *favoriteColor = @(Green);"); 2731 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 2732 2733 verifyFormat("@["); 2734 verifyFormat("@[]"); 2735 verifyFormat( 2736 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 2737 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 2738 2739 verifyFormat("@{"); 2740 verifyFormat("@{}"); 2741 verifyFormat("@{ @\"one\" : @1 }"); 2742 verifyFormat("return @{ @\"one\" : @1 };"); 2743 verifyFormat("@{ @\"one\" : @1, }"); 2744 verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); 2745 verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); 2746 verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); 2747 verifyFormat("[self setDict:@{}"); 2748 verifyFormat("[self setDict:@{ @1 : @2 }"); 2749 verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); 2750 verifyFormat( 2751 "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); 2752 verifyFormat( 2753 "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); 2754 2755 // FIXME: Nested and multi-line array and dictionary literals need more work. 2756 verifyFormat( 2757 "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" 2758 " @\"processInfo\" : [NSProcessInfo processInfo] };"); 2759} 2760 2761TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 2762 EXPECT_EQ("{\n" 2763 "{\n" 2764 "a;\n" 2765 "b;\n" 2766 "}\n" 2767 "}", 2768 format("{\n" 2769 "{\n" 2770 "a;\n" 2771 " b;\n" 2772 "}\n" 2773 "}", 2774 13, 2, getLLVMStyle())); 2775 EXPECT_EQ("{\n" 2776 "{\n" 2777 " a;\n" 2778 "b;\n" 2779 "}\n" 2780 "}", 2781 format("{\n" 2782 "{\n" 2783 " a;\n" 2784 "b;\n" 2785 "}\n" 2786 "}", 2787 9, 2, getLLVMStyle())); 2788 EXPECT_EQ("{\n" 2789 "{\n" 2790 "public:\n" 2791 " b;\n" 2792 "}\n" 2793 "}", 2794 format("{\n" 2795 "{\n" 2796 "public:\n" 2797 " b;\n" 2798 "}\n" 2799 "}", 2800 17, 2, getLLVMStyle())); 2801 EXPECT_EQ("{\n" 2802 "{\n" 2803 "a;\n" 2804 "}\n" 2805 "{\n" 2806 " b;\n" 2807 "}\n" 2808 "}", 2809 format("{\n" 2810 "{\n" 2811 "a;\n" 2812 "}\n" 2813 "{\n" 2814 " b;\n" 2815 "}\n" 2816 "}", 2817 22, 2, getLLVMStyle())); 2818 EXPECT_EQ(" {\n" 2819 " a;\n" 2820 " }", 2821 format(" {\n" 2822 "a;\n" 2823 " }", 2824 4, 2, getLLVMStyle())); 2825 EXPECT_EQ("void f() {}\n" 2826 "void g() {}", 2827 format("void f() {}\n" 2828 "void g() {}", 2829 13, 0, getLLVMStyle())); 2830} 2831 2832} // end namespace tooling 2833} // end namespace clang 2834