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