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