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