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