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