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